linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions
       [not found] <1458252137-24497-1-git-send-email-okaya@codeaurora.org>
@ 2016-03-17 22:02 ` Sinan Kaya
  2016-03-18 12:12   ` Robin Murphy
  0 siblings, 1 reply; 7+ messages in thread
From: Sinan Kaya @ 2016-03-17 22:02 UTC (permalink / raw)
  To: linux-arm-kernel, timur, cov, nwatters
  Cc: Sinan Kaya, Russell King, Catalin Marinas, Will Deacon,
	Tony Luck, Fenghua Yu, Ralf Baechle, Benjamin Herrenschmidt,
	Paul Mackerras, Michael Ellerman, Chris Metcalf, Guan Xuetao,
	Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86,
	Konrad Rzeszutek Wilk, Bjorn Helgaas, Chris Zankel, Max Filippov,
	Andrew Morton, Valentin Rothberg, Marek Szyprowski, Joe Perches,
	Suthikulpanit, Suravee, Robin Murphy, Arnd Bergmann,
	Jisheng Zhang, Dean Nelson, Guenter Roeck, Aaro Koskinen,
	Florian Fainelli, Huacai Chen, Denys Vlasenko, Akinobu Mita,
	linux-kernel, linux-ia64, linux-mips, linuxppc-dev, linux-pci,
	linux-xtensa

Prefixing dma_to_phys and phys_to_dma API with swiotlb so that
they are no longer part of the DMA API. These APIs do not exist
on all architectures and breaks compatibility.

In preparation for the clean up, also make the ARCH implementation
known by defining swiotlb_phys_do_dma and swiotlb_dma_to_phys.

Signed-off-by: Sinan Kaya <okaya@codeaurora.org>
---
 arch/arm/include/asm/dma-mapping.h                 |  8 ++-
 arch/arm64/include/asm/dma-mapping.h               |  9 ++-
 arch/arm64/mm/dma-mapping.c                        | 75 ++++++++++++++--------
 arch/ia64/include/asm/dma-mapping.h                |  8 ++-
 arch/mips/cavium-octeon/dma-octeon.c               |  8 +--
 .../include/asm/mach-cavium-octeon/dma-coherence.h |  7 +-
 arch/mips/include/asm/mach-generic/dma-coherence.h |  8 ++-
 .../include/asm/mach-loongson64/dma-coherence.h    | 14 ++--
 arch/mips/loongson64/common/dma-swiotlb.c          |  4 +-
 arch/powerpc/include/asm/dma-mapping.h             |  8 ++-
 arch/tile/include/asm/dma-mapping.h                |  8 ++-
 arch/unicore32/include/asm/dma-mapping.h           |  8 ++-
 arch/x86/include/asm/dma-mapping.h                 | 15 +++--
 arch/x86/kernel/pci-swiotlb.c                      |  2 +-
 arch/x86/pci/sta2x11-fixup.c                       | 10 +--
 arch/xtensa/include/asm/dma-mapping.h              |  8 ++-
 lib/swiotlb.c                                      | 28 ++++----
 17 files changed, 150 insertions(+), 78 deletions(-)

diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h
index 6ad1ced..e176689 100644
--- a/arch/arm/include/asm/dma-mapping.h
+++ b/arch/arm/include/asm/dma-mapping.h
@@ -129,17 +129,21 @@ static inline bool is_device_dma_coherent(struct device *dev)
 	return dev->archdata.dma_coherent;
 }
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	unsigned int offset = paddr & ~PAGE_MASK;
 	return pfn_to_dma(dev, __phys_to_pfn(paddr)) + offset;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t dev_addr)
 {
 	unsigned int offset = dev_addr & ~PAGE_MASK;
 	return __pfn_to_phys(dma_to_pfn(dev, dev_addr)) + offset;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 {
diff --git a/arch/arm64/include/asm/dma-mapping.h b/arch/arm64/include/asm/dma-mapping.h
index ba437f0..39f21e8 100644
--- a/arch/arm64/include/asm/dma-mapping.h
+++ b/arch/arm64/include/asm/dma-mapping.h
@@ -64,15 +64,19 @@ static inline bool is_device_dma_coherent(struct device *dev)
 	return dev->archdata.dma_coherent;
 }
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return (dma_addr_t)paddr;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t dev_addr)
 {
 	return (phys_addr_t)dev_addr;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 {
@@ -88,3 +92,4 @@ static inline void dma_mark_clean(void *addr, size_t size)
 
 #endif	/* __KERNEL__ */
 #endif	/* __ASM_DMA_MAPPING_H */
+
diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
index a6e757c..ada00c3 100644
--- a/arch/arm64/mm/dma-mapping.c
+++ b/arch/arm64/mm/dma-mapping.c
@@ -107,7 +107,7 @@ static void *__dma_alloc_coherent(struct device *dev, size_t size,
 		if (!page)
 			return NULL;
 
-		*dma_handle = phys_to_dma(dev, page_to_phys(page));
+		*dma_handle = swiotlb_phys_to_dma(dev, page_to_phys(page));
 		addr = page_address(page);
 		memset(addr, 0, size);
 		return addr;
@@ -121,7 +121,7 @@ static void __dma_free_coherent(struct device *dev, size_t size,
 				struct dma_attrs *attrs)
 {
 	bool freed;
-	phys_addr_t paddr = dma_to_phys(dev, dma_handle);
+	phys_addr_t paddr = swiotlb_dma_to_phys(dev, dma_handle);
 
 	if (dev == NULL) {
 		WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
@@ -151,7 +151,8 @@ static void *__dma_alloc(struct device *dev, size_t size,
 		void *addr = __alloc_from_pool(size, &page, flags);
 
 		if (addr)
-			*dma_handle = phys_to_dma(dev, page_to_phys(page));
+			*dma_handle = swiotlb_phys_to_dma(dev,
+							  page_to_phys(page));
 
 		return addr;
 	}
@@ -187,7 +188,7 @@ static void __dma_free(struct device *dev, size_t size,
 		       void *vaddr, dma_addr_t dma_handle,
 		       struct dma_attrs *attrs)
 {
-	void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
+	void *swiotlb_addr = phys_to_virt(swiotlb_dma_to_phys(dev, dma_handle));
 
 	size = PAGE_ALIGN(size);
 
@@ -208,7 +209,8 @@ static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
 
 	dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
 	if (!is_device_dma_coherent(dev))
-		__dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
+		__dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
+			       size, dir);
 
 	return dev_addr;
 }
@@ -218,8 +220,11 @@ static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
 				 size_t size, enum dma_data_direction dir,
 				 struct dma_attrs *attrs)
 {
-	if (!is_device_dma_coherent(dev))
-		__dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
+	if (!is_device_dma_coherent(dev)) {
+		phys_addr_t phys = swiotlb_dma_to_phys(dev, dev_addr);
+
+		__dma_unmap_area(phys_to_virt(phys), size, dir);
+	}
 	swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
 }
 
@@ -232,9 +237,12 @@ static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
 
 	ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
 	if (!is_device_dma_coherent(dev))
-		for_each_sg(sgl, sg, ret, i)
-			__dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
-				       sg->length, dir);
+		for_each_sg(sgl, sg, ret, i) {
+			dma_addr_t dma = sg->dma_address;
+			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
+
+			__dma_map_area(phys_to_virt(phys), sg->length, dir);
+		}
 
 	return ret;
 }
@@ -248,9 +256,12 @@ static void __swiotlb_unmap_sg_attrs(struct device *dev,
 	int i;
 
 	if (!is_device_dma_coherent(dev))
-		for_each_sg(sgl, sg, nelems, i)
-			__dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
-					 sg->length, dir);
+		for_each_sg(sgl, sg, nelems, i) {
+			dma_addr_t dma = sg->dma_address;
+			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
+
+			__dma_unmap_area(phys_to_virt(phys), sg->length, dir);
+		}
 	swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
 }
 
@@ -258,8 +269,11 @@ static void __swiotlb_sync_single_for_cpu(struct device *dev,
 					  dma_addr_t dev_addr, size_t size,
 					  enum dma_data_direction dir)
 {
-	if (!is_device_dma_coherent(dev))
-		__dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
+	if (!is_device_dma_coherent(dev)) {
+		phys_addr_t phys = swiotlb_dma_to_phys(dev, dev_addr);
+
+		__dma_unmap_area(phys_to_virt(phys), size, dir);
+	}
 	swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
 }
 
@@ -269,7 +283,8 @@ static void __swiotlb_sync_single_for_device(struct device *dev,
 {
 	swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
 	if (!is_device_dma_coherent(dev))
-		__dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
+		__dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
+			       size, dir);
 }
 
 static void __swiotlb_sync_sg_for_cpu(struct device *dev,
@@ -280,9 +295,12 @@ static void __swiotlb_sync_sg_for_cpu(struct device *dev,
 	int i;
 
 	if (!is_device_dma_coherent(dev))
-		for_each_sg(sgl, sg, nelems, i)
-			__dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
-					 sg->length, dir);
+		for_each_sg(sgl, sg, nelems, i) {
+			dma_addr_t dma = sg->dma_address;
+			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
+
+			__dma_unmap_area(phys_to_virt(phys), sg->length, dir);
+		}
 	swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
 }
 
@@ -295,9 +313,12 @@ static void __swiotlb_sync_sg_for_device(struct device *dev,
 
 	swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
 	if (!is_device_dma_coherent(dev))
-		for_each_sg(sgl, sg, nelems, i)
-			__dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
-				       sg->length, dir);
+		for_each_sg(sgl, sg, nelems, i) {
+			dma_addr_t dma = sg->dma_address;
+			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
+
+			__dma_map_area(phys_to_virt(phys), sg->length, dir);
+		}
 }
 
 static int __swiotlb_mmap(struct device *dev,
@@ -309,7 +330,7 @@ static int __swiotlb_mmap(struct device *dev,
 	unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
 					PAGE_SHIFT;
 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
-	unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
+	unsigned long pfn = swiotlb_dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
 	unsigned long off = vma->vm_pgoff;
 
 	vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
@@ -334,9 +355,11 @@ static int __swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
 {
 	int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
 
-	if (!ret)
-		sg_set_page(sgt->sgl, phys_to_page(dma_to_phys(dev, handle)),
-			    PAGE_ALIGN(size), 0);
+	if (!ret) {
+		phys_addr_t phys = swiotlb_dma_to_phys(dev, handle);
+
+		sg_set_page(sgt->sgl, phys_to_page(phys), PAGE_ALIGN(size), 0);
+	}
 
 	return ret;
 }
diff --git a/arch/ia64/include/asm/dma-mapping.h b/arch/ia64/include/asm/dma-mapping.h
index d472805..a8736b9 100644
--- a/arch/ia64/include/asm/dma-mapping.h
+++ b/arch/ia64/include/asm/dma-mapping.h
@@ -33,15 +33,19 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 	return addr + size - 1 <= *dev->dma_mask;
 }
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return paddr;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t daddr)
 {
 	return daddr;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 static inline void
 dma_cache_sync (struct device *dev, void *vaddr, size_t size,
diff --git a/arch/mips/cavium-octeon/dma-octeon.c b/arch/mips/cavium-octeon/dma-octeon.c
index 2cd45f5..9ea7e9b 100644
--- a/arch/mips/cavium-octeon/dma-octeon.c
+++ b/arch/mips/cavium-octeon/dma-octeon.c
@@ -210,7 +210,7 @@ struct octeon_dma_map_ops {
 	phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);
 };
 
-dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+dma_addr_t swiotlb_phys_to_dma(struct device *dev, phys_addr_t paddr)
 {
 	struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
 						      struct octeon_dma_map_ops,
@@ -218,9 +218,9 @@ dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 
 	return ops->phys_to_dma(dev, paddr);
 }
-EXPORT_SYMBOL(phys_to_dma);
+EXPORT_SYMBOL(swiotlb_phys_to_dma);
 
-phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+phys_addr_t swiotlb_dma_to_phys(struct device *dev, dma_addr_t daddr)
 {
 	struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),
 						      struct octeon_dma_map_ops,
@@ -228,7 +228,7 @@ phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
 
 	return ops->dma_to_phys(dev, daddr);
 }
-EXPORT_SYMBOL(dma_to_phys);
+EXPORT_SYMBOL(swiotlb_dma_to_phys);
 
 static struct octeon_dma_map_ops octeon_linear_dma_map_ops = {
 	.dma_map_ops = {
diff --git a/arch/mips/include/asm/mach-cavium-octeon/dma-coherence.h b/arch/mips/include/asm/mach-cavium-octeon/dma-coherence.h
index 460042e..5f0f13a 100644
--- a/arch/mips/include/asm/mach-cavium-octeon/dma-coherence.h
+++ b/arch/mips/include/asm/mach-cavium-octeon/dma-coherence.h
@@ -61,8 +61,11 @@ static inline void plat_post_dma_flush(struct device *dev)
 {
 }
 
-dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr);
-phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr);
+dma_addr_t swiotlb_phys_to_dma(struct device *dev, phys_addr_t paddr);
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
+
+phys_addr_t swiotlb_dma_to_phys(struct device *dev, dma_addr_t daddr);
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 struct dma_map_ops;
 extern struct dma_map_ops *octeon_pci_dma_map_ops;
diff --git a/arch/mips/include/asm/mach-generic/dma-coherence.h b/arch/mips/include/asm/mach-generic/dma-coherence.h
index 0f8a354..54fde22 100644
--- a/arch/mips/include/asm/mach-generic/dma-coherence.h
+++ b/arch/mips/include/asm/mach-generic/dma-coherence.h
@@ -59,15 +59,19 @@ static inline void plat_post_dma_flush(struct device *dev)
 #endif
 
 #ifdef CONFIG_SWIOTLB
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return paddr;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t daddr)
 {
 	return daddr;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 #endif
 
 #endif /* __ASM_MACH_GENERIC_DMA_COHERENCE_H */
diff --git a/arch/mips/include/asm/mach-loongson64/dma-coherence.h b/arch/mips/include/asm/mach-loongson64/dma-coherence.h
index 1602a9e..26fe763 100644
--- a/arch/mips/include/asm/mach-loongson64/dma-coherence.h
+++ b/arch/mips/include/asm/mach-loongson64/dma-coherence.h
@@ -17,13 +17,17 @@
 
 struct device;
 
-extern dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr);
-extern phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr);
+extern dma_addr_t swiotlb_phys_to_dma(struct device *dev, phys_addr_t paddr);
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
+
+extern phys_addr_t swiotlb_dma_to_phys(struct device *dev, dma_addr_t daddr);
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
+
 static inline dma_addr_t plat_map_dma_mem(struct device *dev, void *addr,
 					  size_t size)
 {
 #ifdef CONFIG_CPU_LOONGSON3
-	return phys_to_dma(dev, virt_to_phys(addr));
+	return swiotlb_phys_to_dma(dev, virt_to_phys(addr));
 #else
 	return virt_to_phys(addr) | 0x80000000;
 #endif
@@ -33,7 +37,7 @@ static inline dma_addr_t plat_map_dma_mem_page(struct device *dev,
 					       struct page *page)
 {
 #ifdef CONFIG_CPU_LOONGSON3
-	return phys_to_dma(dev, page_to_phys(page));
+	return swiotlb_phys_to_dma(dev, page_to_phys(page));
 #else
 	return page_to_phys(page) | 0x80000000;
 #endif
@@ -43,7 +47,7 @@ static inline unsigned long plat_dma_addr_to_phys(struct device *dev,
 	dma_addr_t dma_addr)
 {
 #if defined(CONFIG_CPU_LOONGSON3) && defined(CONFIG_64BIT)
-	return dma_to_phys(dev, dma_addr);
+	return swiotlb_dma_to_phys(dev, dma_addr);
 #elif defined(CONFIG_CPU_LOONGSON2F) && defined(CONFIG_64BIT)
 	return (dma_addr > 0x8fffffff) ? dma_addr : (dma_addr & 0x0fffffff);
 #else
diff --git a/arch/mips/loongson64/common/dma-swiotlb.c b/arch/mips/loongson64/common/dma-swiotlb.c
index 4ffa6fc..88ef4cf 100644
--- a/arch/mips/loongson64/common/dma-swiotlb.c
+++ b/arch/mips/loongson64/common/dma-swiotlb.c
@@ -98,7 +98,7 @@ static int loongson_dma_set_mask(struct device *dev, u64 mask)
 	return 0;
 }
 
-dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+dma_addr_t swiotlb_phys_to_dma(struct device *dev, phys_addr_t paddr)
 {
 	long nid;
 #ifdef CONFIG_PHYS48_TO_HT40
@@ -110,7 +110,7 @@ dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 	return paddr;
 }
 
-phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+phys_addr_t swiotlb_dma_to_phys(struct device *dev, dma_addr_t daddr)
 {
 	long nid;
 #ifdef CONFIG_PHYS48_TO_HT40
diff --git a/arch/powerpc/include/asm/dma-mapping.h b/arch/powerpc/include/asm/dma-mapping.h
index 77816ac..3a9d6f2 100644
--- a/arch/powerpc/include/asm/dma-mapping.h
+++ b/arch/powerpc/include/asm/dma-mapping.h
@@ -143,15 +143,19 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 	return addr + size - 1 <= *dev->dma_mask;
 }
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return paddr + get_dma_offset(dev);
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t daddr)
 {
 	return daddr - get_dma_offset(dev);
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 #define ARCH_HAS_DMA_MMAP_COHERENT
 
diff --git a/arch/tile/include/asm/dma-mapping.h b/arch/tile/include/asm/dma-mapping.h
index 01ceb4a..87c205a 100644
--- a/arch/tile/include/asm/dma-mapping.h
+++ b/arch/tile/include/asm/dma-mapping.h
@@ -47,15 +47,19 @@ static inline void set_dma_offset(struct device *dev, dma_addr_t off)
 	dev->archdata.dma_offset = off;
 }
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return paddr;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t daddr)
 {
 	return daddr;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 static inline void dma_mark_clean(void *addr, size_t size) {}
 
diff --git a/arch/unicore32/include/asm/dma-mapping.h b/arch/unicore32/include/asm/dma-mapping.h
index 4749854..762cdd8 100644
--- a/arch/unicore32/include/asm/dma-mapping.h
+++ b/arch/unicore32/include/asm/dma-mapping.h
@@ -36,15 +36,19 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 	return 1;
 }
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return paddr;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t daddr)
 {
 	return daddr;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 static inline void dma_mark_clean(void *addr, size_t size) {}
 
diff --git a/arch/x86/include/asm/dma-mapping.h b/arch/x86/include/asm/dma-mapping.h
index 3a27b93..be8b76e 100644
--- a/arch/x86/include/asm/dma-mapping.h
+++ b/arch/x86/include/asm/dma-mapping.h
@@ -56,8 +56,11 @@ extern void dma_generic_free_coherent(struct device *dev, size_t size,
 
 #ifdef CONFIG_X86_DMA_REMAP /* Platform code defines bridge-specific code */
 extern bool dma_capable(struct device *dev, dma_addr_t addr, size_t size);
-extern dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr);
-extern phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr);
+extern dma_addr_t swiotlb_phys_to_dma(struct device *dev, phys_addr_t paddr);
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
+
+extern phys_addr_t swiotlb_dma_to_phys(struct device *dev, dma_addr_t daddr);
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 #else
 
 static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
@@ -68,15 +71,19 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 	return addr + size - 1 <= *dev->dma_mask;
 }
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return paddr;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t daddr)
 {
 	return daddr;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 #endif /* CONFIG_X86_DMA_REMAP */
 
 static inline void
diff --git a/arch/x86/kernel/pci-swiotlb.c b/arch/x86/kernel/pci-swiotlb.c
index 7c577a1..9333fbd 100644
--- a/arch/x86/kernel/pci-swiotlb.c
+++ b/arch/x86/kernel/pci-swiotlb.c
@@ -39,7 +39,7 @@ void x86_swiotlb_free_coherent(struct device *dev, size_t size,
 				      void *vaddr, dma_addr_t dma_addr,
 				      struct dma_attrs *attrs)
 {
-	if (is_swiotlb_buffer(dma_to_phys(dev, dma_addr)))
+	if (is_swiotlb_buffer(swiotlb_dma_to_phys(dev, dma_addr)))
 		swiotlb_free_coherent(dev, size, vaddr, dma_addr);
 	else
 		dma_generic_free_coherent(dev, size, vaddr, dma_addr, attrs);
diff --git a/arch/x86/pci/sta2x11-fixup.c b/arch/x86/pci/sta2x11-fixup.c
index 5ceda85..6df3eb4 100644
--- a/arch/x86/pci/sta2x11-fixup.c
+++ b/arch/x86/pci/sta2x11-fixup.c
@@ -241,11 +241,12 @@ bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 }
 
 /**
- * phys_to_dma - Return the DMA AMBA address used for this STA2x11 device
+ * swiotlb_phys_to_dma - Return the DMA AMBA address used for this
+ *	STA2x11 device
  * @dev: device for a PCI device
  * @paddr: Physical address
  */
-dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+dma_addr_t swiotlb_phys_to_dma(struct device *dev, phys_addr_t paddr)
 {
 	if (dev->archdata.dma_ops != &sta2x11_dma_ops)
 		return paddr;
@@ -253,11 +254,12 @@ dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 }
 
 /**
- * dma_to_phys - Return the physical address used for this STA2x11 DMA address
+ * swiotlb_dma_to_phys - Return the physical address used for this
+ *	STA2x11 DMA address
  * @dev: device for a PCI device
  * @daddr: STA2x11 AMBA DMA address
  */
-phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+phys_addr_t swiotlb_dma_to_phys(struct device *dev, dma_addr_t daddr)
 {
 	if (dev->archdata.dma_ops != &sta2x11_dma_ops)
 		return daddr;
diff --git a/arch/xtensa/include/asm/dma-mapping.h b/arch/xtensa/include/asm/dma-mapping.h
index 3fc1170..b0d725d 100644
--- a/arch/xtensa/include/asm/dma-mapping.h
+++ b/arch/xtensa/include/asm/dma-mapping.h
@@ -31,14 +31,18 @@ static inline struct dma_map_ops *get_dma_ops(struct device *dev)
 void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
 		    enum dma_data_direction direction);
 
-static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
+static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev,
+					     phys_addr_t paddr)
 {
 	return (dma_addr_t)paddr;
 }
+#define swiotlb_phys_to_dma swiotlb_phys_to_dma
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
+static inline phys_addr_t swiotlb_dma_to_phys(struct device *dev,
+					      dma_addr_t daddr)
 {
 	return (phys_addr_t)daddr;
 }
+#define swiotlb_dma_to_phys swiotlb_dma_to_phys
 
 #endif	/* _XTENSA_DMA_MAPPING_H */
diff --git a/lib/swiotlb.c b/lib/swiotlb.c
index 76f29ec..5aadeca 100644
--- a/lib/swiotlb.c
+++ b/lib/swiotlb.c
@@ -135,7 +135,7 @@ unsigned long swiotlb_size_or_default(void)
 static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
 				      volatile void *address)
 {
-	return phys_to_dma(hwdev, virt_to_phys(address));
+	return swiotlb_phys_to_dma(hwdev, virt_to_phys(address));
 }
 
 static bool no_iotlb_memory;
@@ -541,7 +541,7 @@ static phys_addr_t
 map_single(struct device *hwdev, phys_addr_t phys, size_t size,
 	   enum dma_data_direction dir)
 {
-	dma_addr_t start_dma_addr = phys_to_dma(hwdev, io_tlb_start);
+	dma_addr_t start_dma_addr = swiotlb_phys_to_dma(hwdev, io_tlb_start);
 
 	return swiotlb_tbl_map_single(hwdev, start_dma_addr, phys, size, dir);
 }
@@ -659,7 +659,7 @@ swiotlb_alloc_coherent(struct device *hwdev, size_t size,
 			goto err_warn;
 
 		ret = phys_to_virt(paddr);
-		dev_addr = phys_to_dma(hwdev, paddr);
+		dev_addr = swiotlb_phys_to_dma(hwdev, paddr);
 
 		/* Confirm address can be DMA'd by device */
 		if (dev_addr + size - 1 > dma_mask) {
@@ -692,7 +692,7 @@ void
 swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
 		      dma_addr_t dev_addr)
 {
-	phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
+	phys_addr_t paddr = swiotlb_dma_to_phys(hwdev, dev_addr);
 
 	WARN_ON(irqs_disabled());
 	if (!is_swiotlb_buffer(paddr))
@@ -741,7 +741,7 @@ dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
 			    struct dma_attrs *attrs)
 {
 	phys_addr_t map, phys = page_to_phys(page) + offset;
-	dma_addr_t dev_addr = phys_to_dma(dev, phys);
+	dma_addr_t dev_addr = swiotlb_phys_to_dma(dev, phys);
 
 	BUG_ON(dir == DMA_NONE);
 	/*
@@ -758,15 +758,15 @@ dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
 	map = map_single(dev, phys, size, dir);
 	if (map == SWIOTLB_MAP_ERROR) {
 		swiotlb_full(dev, size, dir, 1);
-		return phys_to_dma(dev, io_tlb_overflow_buffer);
+		return swiotlb_phys_to_dma(dev, io_tlb_overflow_buffer);
 	}
 
-	dev_addr = phys_to_dma(dev, map);
+	dev_addr = swiotlb_phys_to_dma(dev, map);
 
 	/* Ensure that the address returned is DMA'ble */
 	if (!dma_capable(dev, dev_addr, size)) {
 		swiotlb_tbl_unmap_single(dev, map, size, dir);
-		return phys_to_dma(dev, io_tlb_overflow_buffer);
+		return swiotlb_phys_to_dma(dev, io_tlb_overflow_buffer);
 	}
 
 	return dev_addr;
@@ -784,7 +784,7 @@ EXPORT_SYMBOL_GPL(swiotlb_map_page);
 static void unmap_single(struct device *hwdev, dma_addr_t dev_addr,
 			 size_t size, enum dma_data_direction dir)
 {
-	phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
+	phys_addr_t paddr = swiotlb_dma_to_phys(hwdev, dev_addr);
 
 	BUG_ON(dir == DMA_NONE);
 
@@ -828,7 +828,7 @@ swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
 		    size_t size, enum dma_data_direction dir,
 		    enum dma_sync_target target)
 {
-	phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
+	phys_addr_t paddr = swiotlb_dma_to_phys(hwdev, dev_addr);
 
 	BUG_ON(dir == DMA_NONE);
 
@@ -886,7 +886,7 @@ swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
 
 	for_each_sg(sgl, sg, nelems, i) {
 		phys_addr_t paddr = sg_phys(sg);
-		dma_addr_t dev_addr = phys_to_dma(hwdev, paddr);
+		dma_addr_t dev_addr = swiotlb_phys_to_dma(hwdev, paddr);
 
 		if (swiotlb_force ||
 		    !dma_capable(hwdev, dev_addr, sg->length)) {
@@ -901,7 +901,7 @@ swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
 				sg_dma_len(sgl) = 0;
 				return 0;
 			}
-			sg->dma_address = phys_to_dma(hwdev, map);
+			sg->dma_address = swiotlb_phys_to_dma(hwdev, map);
 		} else
 			sg->dma_address = dev_addr;
 		sg_dma_len(sg) = sg->length;
@@ -984,7 +984,7 @@ EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
 int
 swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
 {
-	return (dma_addr == phys_to_dma(hwdev, io_tlb_overflow_buffer));
+	return (dma_addr == swiotlb_phys_to_dma(hwdev, io_tlb_overflow_buffer));
 }
 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
 
@@ -997,6 +997,6 @@ EXPORT_SYMBOL(swiotlb_dma_mapping_error);
 int
 swiotlb_dma_supported(struct device *hwdev, u64 mask)
 {
-	return phys_to_dma(hwdev, io_tlb_end - 1) <= mask;
+	return swiotlb_phys_to_dma(hwdev, io_tlb_end - 1) <= mask;
 }
 EXPORT_SYMBOL(swiotlb_dma_supported);
-- 
1.8.2.1

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

* Re: [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions
  2016-03-17 22:02 ` [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions Sinan Kaya
@ 2016-03-18 12:12   ` Robin Murphy
  2016-03-18 15:00     ` Sinan Kaya
  0 siblings, 1 reply; 7+ messages in thread
From: Robin Murphy @ 2016-03-18 12:12 UTC (permalink / raw)
  To: Sinan Kaya, linux-arm-kernel, timur, cov, nwatters
  Cc: linux-mips, linux-ia64, linux-xtensa, Catalin Marinas,
	Will Deacon, Max Filippov, Chris Metcalf, Paul Mackerras,
	H. Peter Anvin, Huacai Chen, Guan Xuetao, Marek Szyprowski,
	Jisheng Zhang, Florian Fainelli, Russell King, Aaro Koskinen,
	Michael Ellerman, x86, Ingo Molnar, Benjamin Herrenschmidt,
	Guenter Roeck, Fenghua Yu, Denys Vlasenko, Arnd Bergmann,
	Konrad Rzeszutek Wilk, Akinobu Mita, Bjorn Helgaas,
	Thomas Gleixner, Valentin Rothberg, Chris Zankel, Tony Luck,
	linux-pci, linuxppc-dev, Dean Nelson, linux-kernel, Ralf Baechle,
	Suthikulpanit, Suravee, Joe Perches, Andrew Morton

On 17/03/16 22:02, Sinan Kaya wrote:
> Prefixing dma_to_phys and phys_to_dma API with swiotlb so that
> they are no longer part of the DMA API. These APIs do not exist
> on all architectures and breaks compatibility.
>
> In preparation for the clean up, also make the ARCH implementation
> known by defining swiotlb_phys_do_dma and swiotlb_dma_to_phys.
>
> Signed-off-by: Sinan Kaya <okaya@codeaurora.org>
> ---
>   arch/arm/include/asm/dma-mapping.h                 |  8 ++-
>   arch/arm64/include/asm/dma-mapping.h               |  9 ++-
>   arch/arm64/mm/dma-mapping.c                        | 75 ++++++++++++++--------

[...]

> diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
> index a6e757c..ada00c3 100644
> --- a/arch/arm64/mm/dma-mapping.c
> +++ b/arch/arm64/mm/dma-mapping.c
> @@ -107,7 +107,7 @@ static void *__dma_alloc_coherent(struct device *dev, size_t size,
>   		if (!page)
>   			return NULL;
>
> -		*dma_handle = phys_to_dma(dev, page_to_phys(page));
> +		*dma_handle = swiotlb_phys_to_dma(dev, page_to_phys(page));
>   		addr = page_address(page);
>   		memset(addr, 0, size);
>   		return addr;
> @@ -121,7 +121,7 @@ static void __dma_free_coherent(struct device *dev, size_t size,
>   				struct dma_attrs *attrs)
>   {
>   	bool freed;
> -	phys_addr_t paddr = dma_to_phys(dev, dma_handle);
> +	phys_addr_t paddr = swiotlb_dma_to_phys(dev, dma_handle);
>
>   	if (dev == NULL) {
>   		WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
> @@ -151,7 +151,8 @@ static void *__dma_alloc(struct device *dev, size_t size,
>   		void *addr = __alloc_from_pool(size, &page, flags);
>
>   		if (addr)
> -			*dma_handle = phys_to_dma(dev, page_to_phys(page));
> +			*dma_handle = swiotlb_phys_to_dma(dev,
> +							  page_to_phys(page));
>
>   		return addr;
>   	}
> @@ -187,7 +188,7 @@ static void __dma_free(struct device *dev, size_t size,
>   		       void *vaddr, dma_addr_t dma_handle,
>   		       struct dma_attrs *attrs)
>   {
> -	void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
> +	void *swiotlb_addr = phys_to_virt(swiotlb_dma_to_phys(dev, dma_handle));
>
>   	size = PAGE_ALIGN(size);
>
> @@ -208,7 +209,8 @@ static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
>
>   	dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
>   	if (!is_device_dma_coherent(dev))
> -		__dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
> +		__dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
> +			       size, dir);
>
>   	return dev_addr;
>   }
> @@ -218,8 +220,11 @@ static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
>   				 size_t size, enum dma_data_direction dir,
>   				 struct dma_attrs *attrs)
>   {
> -	if (!is_device_dma_coherent(dev))
> -		__dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
> +	if (!is_device_dma_coherent(dev)) {
> +		phys_addr_t phys = swiotlb_dma_to_phys(dev, dev_addr);
> +
> +		__dma_unmap_area(phys_to_virt(phys), size, dir);
> +	}
>   	swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
>   }
>
> @@ -232,9 +237,12 @@ static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
>
>   	ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
>   	if (!is_device_dma_coherent(dev))
> -		for_each_sg(sgl, sg, ret, i)
> -			__dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
> -				       sg->length, dir);
> +		for_each_sg(sgl, sg, ret, i) {
> +			dma_addr_t dma = sg->dma_address;
> +			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
> +
> +			__dma_map_area(phys_to_virt(phys), sg->length, dir);
> +		}
>
>   	return ret;
>   }
> @@ -248,9 +256,12 @@ static void __swiotlb_unmap_sg_attrs(struct device *dev,
>   	int i;
>
>   	if (!is_device_dma_coherent(dev))
> -		for_each_sg(sgl, sg, nelems, i)
> -			__dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
> -					 sg->length, dir);
> +		for_each_sg(sgl, sg, nelems, i) {
> +			dma_addr_t dma = sg->dma_address;
> +			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
> +
> +			__dma_unmap_area(phys_to_virt(phys), sg->length, dir);
> +		}
>   	swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
>   }
>
> @@ -258,8 +269,11 @@ static void __swiotlb_sync_single_for_cpu(struct device *dev,
>   					  dma_addr_t dev_addr, size_t size,
>   					  enum dma_data_direction dir)
>   {
> -	if (!is_device_dma_coherent(dev))
> -		__dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
> +	if (!is_device_dma_coherent(dev)) {
> +		phys_addr_t phys = swiotlb_dma_to_phys(dev, dev_addr);
> +
> +		__dma_unmap_area(phys_to_virt(phys), size, dir);
> +	}
>   	swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
>   }
>
> @@ -269,7 +283,8 @@ static void __swiotlb_sync_single_for_device(struct device *dev,
>   {
>   	swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
>   	if (!is_device_dma_coherent(dev))
> -		__dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
> +		__dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
> +			       size, dir);
>   }
>
>   static void __swiotlb_sync_sg_for_cpu(struct device *dev,
> @@ -280,9 +295,12 @@ static void __swiotlb_sync_sg_for_cpu(struct device *dev,
>   	int i;
>
>   	if (!is_device_dma_coherent(dev))
> -		for_each_sg(sgl, sg, nelems, i)
> -			__dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
> -					 sg->length, dir);
> +		for_each_sg(sgl, sg, nelems, i) {
> +			dma_addr_t dma = sg->dma_address;
> +			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
> +
> +			__dma_unmap_area(phys_to_virt(phys), sg->length, dir);
> +		}
>   	swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
>   }
>
> @@ -295,9 +313,12 @@ static void __swiotlb_sync_sg_for_device(struct device *dev,
>
>   	swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
>   	if (!is_device_dma_coherent(dev))
> -		for_each_sg(sgl, sg, nelems, i)
> -			__dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
> -				       sg->length, dir);
> +		for_each_sg(sgl, sg, nelems, i) {
> +			dma_addr_t dma = sg->dma_address;
> +			phys_addr_t phys = swiotlb_dma_to_phys(dev, dma);
> +
> +			__dma_map_area(phys_to_virt(phys), sg->length, dir);
> +		}
>   }
>
>   static int __swiotlb_mmap(struct device *dev,
> @@ -309,7 +330,7 @@ static int __swiotlb_mmap(struct device *dev,
>   	unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
>   					PAGE_SHIFT;
>   	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
> -	unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
> +	unsigned long pfn = swiotlb_dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
>   	unsigned long off = vma->vm_pgoff;
>
>   	vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot,
> @@ -334,9 +355,11 @@ static int __swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
>   {
>   	int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
>
> -	if (!ret)
> -		sg_set_page(sgt->sgl, phys_to_page(dma_to_phys(dev, handle)),
> -			    PAGE_ALIGN(size), 0);
> +	if (!ret) {
> +		phys_addr_t phys = swiotlb_dma_to_phys(dev, handle);
> +
> +		sg_set_page(sgt->sgl, phys_to_page(phys), PAGE_ALIGN(size), 0);
> +	}
>
>   	return ret;
>   }

Since we know for sure that swiotlb_to_phys is a no-op on arm64, it 
might be cleaner to simply not reference it at all. I suppose we could 
have some private local wrappers, e.g.:

#define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))

to keep the intent of the code clear (and just in case anyone ever 
builds a system mad enough to warrant switching out that definition, but 
I'd hope that never happens).

Otherwise, looks good - thanks for doing this!

Robin.

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

* Re: [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions
  2016-03-18 12:12   ` Robin Murphy
@ 2016-03-18 15:00     ` Sinan Kaya
  2016-03-28 18:29       ` Konrad Rzeszutek Wilk
  0 siblings, 1 reply; 7+ messages in thread
From: Sinan Kaya @ 2016-03-18 15:00 UTC (permalink / raw)
  To: Robin Murphy, linux-arm-kernel, timur, cov, nwatters
  Cc: linux-mips, linux-ia64, linux-xtensa, Catalin Marinas,
	Will Deacon, Max Filippov, Chris Metcalf, Paul Mackerras,
	H. Peter Anvin, Huacai Chen, Guan Xuetao, Marek Szyprowski,
	Jisheng Zhang, Florian Fainelli, Russell King, Aaro Koskinen,
	Michael Ellerman, x86, Ingo Molnar, Benjamin Herrenschmidt,
	Guenter Roeck, Fenghua Yu, Denys Vlasenko, Arnd Bergmann,
	Konrad Rzeszutek Wilk, Akinobu Mita, Bjorn Helgaas,
	Thomas Gleixner, Valentin Rothberg, Chris Zankel, Tony Luck,
	linux-pci, linuxppc-dev, Dean Nelson, linux-kernel, Ralf Baechle,
	Suthikulpanit, Suravee, Joe Perches, Andrew Morton

On 3/18/2016 8:12 AM, Robin Murphy wrote:
> Since we know for sure that swiotlb_to_phys is a no-op on arm64, it might be cleaner to simply not reference it at all. I suppose we could have some private local wrappers, e.g.:
> 
> #define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))
> 
> to keep the intent of the code clear (and just in case anyone ever builds a system mad enough to warrant switching out that definition, but I'd hope that never happens).
> 
> Otherwise, looks good - thanks for doing this!

OK. I added this. Reviewed-by?

I'm not happy to submit such a big patch for all different ARCHs. I couldn't
find a cleaner solution. I'm willing to split this patch into multiple if there
is a better way.

diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
index ada00c3..8c0f66b 100644
--- a/arch/arm64/mm/dma-mapping.c
+++ b/arch/arm64/mm/dma-mapping.c
@@ -29,6 +29,14 @@

 #include <asm/cacheflush.h>

+/*
+ * If you are building a system without IOMMU, then you are using SWIOTLB
+ * library. The ARM64 adaptation of this library does not support address
+ * translation and it assumes that physical address = dma address for such
+ * a use case. Please don't build a platform that violates this.
+ */
+#define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))
+
 static pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot,
                                 bool coherent)
 {
@@ -188,7 +196,7 @@ static void __dma_free(struct device *dev, size_t size,
                       void *vaddr, dma_addr_t dma_handle,
                       struct dma_attrs *attrs)
 {
-       void *swiotlb_addr = phys_to_virt(swiotlb_dma_to_phys(dev, dma_handle));
+       void *swiotlb_addr = swiotlb_to_virt(dma_handle);

        size = PAGE_ALIGN(size);

@@ -209,8 +217,7 @@ static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,

        dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
        if (!is_device_dma_coherent(dev))
-               __dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
-                              size, dir);
+               __dma_map_area(swiotlb_to_virt(dev_addr), size, dir);

        return dev_addr;
 }
@@ -283,8 +290,7 @@ static void __swiotlb_sync_single_for_device(struct device *dev,
 {
        swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
        if (!is_device_dma_coherent(dev))
-               __dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
-                              size, dir);
+               __dma_map_area(swiotlb_to_virt(dev_addr), size, dir);
 }




-- 
Sinan Kaya
Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

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

* Re: [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions
  2016-03-18 15:00     ` Sinan Kaya
@ 2016-03-28 18:29       ` Konrad Rzeszutek Wilk
  2016-03-29 12:44         ` Stefano Stabellini
  2016-03-29 19:32         ` Arnd Bergmann
  0 siblings, 2 replies; 7+ messages in thread
From: Konrad Rzeszutek Wilk @ 2016-03-28 18:29 UTC (permalink / raw)
  To: Sinan Kaya
  Cc: Robin Murphy, linux-arm-kernel, timur, cov, nwatters, linux-mips,
	linux-ia64, linux-xtensa, Catalin Marinas, Will Deacon,
	Max Filippov, Chris Metcalf, Paul Mackerras, H. Peter Anvin,
	Huacai Chen, Guan Xuetao, Marek Szyprowski, Jisheng Zhang,
	Florian Fainelli, Russell King, Aaro Koskinen, Michael Ellerman,
	X86, Ingo Molnar, Benjamin Herrenschmidt, Guenter Roeck,
	Fenghua Yu, Denys Vlasenko, Arnd Bergmann, Konrad Rzeszutek Wilk,
	Akinobu Mita, Bjorn Helgaas, Thomas Gleixner, Valentin Rothberg,
	Chris Zankel, Tony Luck, linux-pci, linuxppc-dev, Dean Nelson,
	LKML, Ralf Baechle, Suthikulpanit, Suravee, Joe Perches,
	Andrew Morton, Stefano Stabellini

On Fri, Mar 18, 2016 at 11:00 AM, Sinan Kaya <okaya@codeaurora.org> wrote:
> On 3/18/2016 8:12 AM, Robin Murphy wrote:
>> Since we know for sure that swiotlb_to_phys is a no-op on arm64, it might be cleaner to simply not reference it at all. I suppose we could have some private local wrappers, e.g.:
>>
>> #define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))
>>
>> to keep the intent of the code clear (and just in case anyone ever builds a system mad enough to warrant switching out that definition, but I'd hope that never happens).
>>
>> Otherwise, looks good - thanks for doing this!
>
> OK. I added this. Reviewed-by?
>
> I'm not happy to submit such a big patch for all different ARCHs. I couldn't
> find a cleaner solution. I'm willing to split this patch into multiple if there
> is a better way.
>
> diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
> index ada00c3..8c0f66b 100644
> --- a/arch/arm64/mm/dma-mapping.c
> +++ b/arch/arm64/mm/dma-mapping.c
> @@ -29,6 +29,14 @@
>
>  #include <asm/cacheflush.h>
>
> +/*
> + * If you are building a system without IOMMU, then you are using SWIOTLB
> + * library. The ARM64 adaptation of this library does not support address
> + * translation and it assumes that physical address = dma address for such
> + * a use case. Please don't build a platform that violates this.
> + */

Why not just expand the ARM64 part to support address translation?

> +#define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))
> +

Adding Stefano here.

>  static pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot,
>                                  bool coherent)
>  {
> @@ -188,7 +196,7 @@ static void __dma_free(struct device *dev, size_t size,
>                        void *vaddr, dma_addr_t dma_handle,
>                        struct dma_attrs *attrs)
>  {
> -       void *swiotlb_addr = phys_to_virt(swiotlb_dma_to_phys(dev, dma_handle));
> +       void *swiotlb_addr = swiotlb_to_virt(dma_handle);
>
>         size = PAGE_ALIGN(size);
>
> @@ -209,8 +217,7 @@ static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
>
>         dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
>         if (!is_device_dma_coherent(dev))
> -               __dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
> -                              size, dir);
> +               __dma_map_area(swiotlb_to_virt(dev_addr), size, dir);
>
>         return dev_addr;
>  }
> @@ -283,8 +290,7 @@ static void __swiotlb_sync_single_for_device(struct device *dev,
>  {
>         swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
>         if (!is_device_dma_coherent(dev))
> -               __dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
> -                              size, dir);
> +               __dma_map_area(swiotlb_to_virt(dev_addr), size, dir);
>  }
>
>
>
>
> --
> Sinan Kaya
> Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

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

* Re: [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions
  2016-03-28 18:29       ` Konrad Rzeszutek Wilk
@ 2016-03-29 12:44         ` Stefano Stabellini
  2016-03-29 12:57           ` Sinan Kaya
  2016-03-29 19:32         ` Arnd Bergmann
  1 sibling, 1 reply; 7+ messages in thread
From: Stefano Stabellini @ 2016-03-29 12:44 UTC (permalink / raw)
  To: Konrad Rzeszutek Wilk
  Cc: Sinan Kaya, Robin Murphy, linux-arm-kernel, timur, cov, nwatters,
	linux-mips, linux-ia64, linux-xtensa, Catalin Marinas,
	Will Deacon, Max Filippov, Chris Metcalf, Paul Mackerras,
	H. Peter Anvin, Huacai Chen, Guan Xuetao, Marek Szyprowski,
	Jisheng Zhang, Florian Fainelli, Russell King, Aaro Koskinen,
	Michael Ellerman, X86, Ingo Molnar, Benjamin Herrenschmidt,
	Guenter Roeck, Fenghua Yu, Denys Vlasenko, Arnd Bergmann,
	Konrad Rzeszutek Wilk, Akinobu Mita, Bjorn Helgaas,
	Thomas Gleixner, Valentin Rothberg, Chris Zankel, Tony Luck,
	linux-pci, linuxppc-dev, Dean Nelson, LKML, Ralf Baechle,
	Suthikulpanit, Suravee, Joe Perches, Andrew Morton, sstabellini

On Mon, 28 Mar 2016, Konrad Rzeszutek Wilk wrote:
> On Fri, Mar 18, 2016 at 11:00 AM, Sinan Kaya <okaya@codeaurora.org> wrote:
> > On 3/18/2016 8:12 AM, Robin Murphy wrote:
> >> Since we know for sure that swiotlb_to_phys is a no-op on arm64, it might be cleaner to simply not reference it at all. I suppose we could have some private local wrappers, e.g.:
> >>
> >> #define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))
> >>
> >> to keep the intent of the code clear (and just in case anyone ever builds a system mad enough to warrant switching out that definition, but I'd hope that never happens).
> >>
> >> Otherwise, looks good - thanks for doing this!
> >
> > OK. I added this. Reviewed-by?
> >
> > I'm not happy to submit such a big patch for all different ARCHs. I couldn't
> > find a cleaner solution. I'm willing to split this patch into multiple if there
> > is a better way.
> >
> > diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
> > index ada00c3..8c0f66b 100644
> > --- a/arch/arm64/mm/dma-mapping.c
> > +++ b/arch/arm64/mm/dma-mapping.c
> > @@ -29,6 +29,14 @@
> >
> >  #include <asm/cacheflush.h>
> >
> > +/*
> > + * If you are building a system without IOMMU, then you are using SWIOTLB
> > + * library. The ARM64 adaptation of this library does not support address
> > + * translation and it assumes that physical address = dma address for such
> > + * a use case. Please don't build a platform that violates this.
> > + */
> 
> Why not just expand the ARM64 part to support address translation?
>
> > +#define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))
> > +
> 
> Adding Stefano here.

Could you please explain what is the problem that you are trying to
solve? In other words, what is the issue with assuming that physical
address = dma address (and the current dma_to_phys and phys_to_dma
static inlines) if no arm64 platforms violate it? That's pretty much
what is done on x86 too (without X86_DMA_REMAP).

If you want to make sure that the assumption is not violated, you can
introduce a boot time check or a BUG_ON somewhere.

If there is an arm64 platform with phys_addr != dma_addr, we need proper
support for it. In fact even if there is an IOMMU on that platform, when
running Xen on it, the IOMMU would be used by the hypervisor and Linux
would still end up without it, using the swiotlb.

 
> >  static pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot,
> >                                  bool coherent)
> >  {
> > @@ -188,7 +196,7 @@ static void __dma_free(struct device *dev, size_t size,
> >                        void *vaddr, dma_addr_t dma_handle,
> >                        struct dma_attrs *attrs)
> >  {
> > -       void *swiotlb_addr = phys_to_virt(swiotlb_dma_to_phys(dev, dma_handle));
> > +       void *swiotlb_addr = swiotlb_to_virt(dma_handle);
> >
> >         size = PAGE_ALIGN(size);
> >
> > @@ -209,8 +217,7 @@ static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
> >
> >         dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
> >         if (!is_device_dma_coherent(dev))
> > -               __dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
> > -                              size, dir);
> > +               __dma_map_area(swiotlb_to_virt(dev_addr), size, dir);
> >
> >         return dev_addr;
> >  }
> > @@ -283,8 +290,7 @@ static void __swiotlb_sync_single_for_device(struct device *dev,
> >  {
> >         swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
> >         if (!is_device_dma_coherent(dev))
> > -               __dma_map_area(phys_to_virt(swiotlb_dma_to_phys(dev, dev_addr)),
> > -                              size, dir);
> > +               __dma_map_area(swiotlb_to_virt(dev_addr), size, dir);
> >  }
> >
> >
> >
> >
> > --
> > Sinan Kaya
> > Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
> > Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
> 

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

* Re: [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions
  2016-03-29 12:44         ` Stefano Stabellini
@ 2016-03-29 12:57           ` Sinan Kaya
  0 siblings, 0 replies; 7+ messages in thread
From: Sinan Kaya @ 2016-03-29 12:57 UTC (permalink / raw)
  To: Stefano Stabellini, Konrad Rzeszutek Wilk
  Cc: Robin Murphy, linux-arm-kernel, timur, cov, nwatters, linux-mips,
	linux-ia64, linux-xtensa, Catalin Marinas, Will Deacon,
	Max Filippov, Chris Metcalf, Paul Mackerras, H. Peter Anvin,
	Huacai Chen, Guan Xuetao, Marek Szyprowski, Jisheng Zhang,
	Florian Fainelli, Russell King, Aaro Koskinen, Michael Ellerman,
	X86, Ingo Molnar, Benjamin Herrenschmidt, Guenter Roeck,
	Fenghua Yu, Denys Vlasenko, Arnd Bergmann, Konrad Rzeszutek Wilk,
	Akinobu Mita, Bjorn Helgaas, Thomas Gleixner, Valentin Rothberg,
	Chris Zankel, Tony Luck, linux-pci, linuxppc-dev, Dean Nelson,
	LKML, Ralf Baechle, Suthikulpanit, Suravee, Joe Perches,
	Andrew Morton

On 3/29/2016 8:44 AM, Stefano Stabellini wrote:
> Could you please explain what is the problem that you are trying to
> solve? In other words, what is the issue with assuming that physical
> address = dma address (and the current dma_to_phys and phys_to_dma
> static inlines) if no arm64 platforms violate it? That's pretty much
> what is done on x86 too (without X86_DMA_REMAP).
> 
> If you want to make sure that the assumption is not violated, you can
> introduce a boot time check or a BUG_ON somewhere.
> 
> If there is an arm64 platform with phys_addr != dma_addr, we need proper
> support for it. In fact even if there is an IOMMU on that platform, when
> running Xen on it, the IOMMU would be used by the hypervisor and Linux
> would still end up without it, using the swiotlb.


The problem is that device drivers are trying to use dma_to_phys and phys_to_dma
APIs to do address translation even though these two API do not exist in DMA mapping
layer. (see patch #1 and I made the same mistake in my HIDMA code)

Especially, when a device is behind an IOMMU; the DMA addresses are not equal
to physical addresses. Usage of dma_to_phys causes a crash on the system.

I'm trying to prefix the dma_to_phys and phys_to_dma API with swiotlb so that
we know what it is intended for and usage of these API in drivers need to be
discouraged in the future during code reviews.

Since I renamed the API, Robin Murphy made a suggestion to convert this 

phys_to_virt(swiotlb_dma_to_phys(dev, dma_handle))

to this

#define swiotlb_to_virt(addr) phys_to_virt((phys_addr_t)(addr))

swiotlb_to_virt(dma_handle)

just to reduce code clutter since we know swiotlb_dma_to_phys returns phys already
for ARM64 architecture.

I think we can do this exercise later. I'll undo this change for the moment. 
Let's focus on the API rename. 

I don't want this general purpose dma_to_phys API returning phys=dma value. This is
not correct.

-- 
Sinan Kaya
Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

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

* Re: [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions
  2016-03-28 18:29       ` Konrad Rzeszutek Wilk
  2016-03-29 12:44         ` Stefano Stabellini
@ 2016-03-29 19:32         ` Arnd Bergmann
  1 sibling, 0 replies; 7+ messages in thread
From: Arnd Bergmann @ 2016-03-29 19:32 UTC (permalink / raw)
  To: linux-arm-kernel, Denys Vlasenko
  Cc: Konrad Rzeszutek Wilk, Sinan Kaya, linux-mips, linux-ia64,
	Fenghua Yu, Catalin Marinas, Will Deacon, Max Filippov,
	Chris Metcalf, Paul Mackerras, H. Peter Anvin, Huacai Chen,
	Guan Xuetao, Stefano Stabellini, Marek Szyprowski, Jisheng Zhang,
	Florian Fainelli, Russell King, Aaro Koskinen, Michael Ellerman,
	timur, X86, Ingo Molnar, Benjamin Herrenschmidt, Guenter Roeck,
	Konrad Rzeszutek Wilk, Akinobu Mita, cov, Bjorn Helgaas,
	Thomas Gleixner, Valentin Rothberg, Chris Zankel, Tony Luck,
	linux-pci, linuxppc-dev, Dean Nelson, LKML, Ralf Baechle,
	Suthikulpanit, Suravee, Joe Perches, Andrew Morton, Robin Murphy,
	nwatters

On Monday 28 March 2016 14:29:29 Konrad Rzeszutek Wilk wrote:
> > diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
> > index ada00c3..8c0f66b 100644
> > --- a/arch/arm64/mm/dma-mapping.c
> > +++ b/arch/arm64/mm/dma-mapping.c
> > @@ -29,6 +29,14 @@
> >
> >  #include <asm/cacheflush.h>
> >
> > +/*
> > + * If you are building a system without IOMMU, then you are using SWIOTLB
> > + * library. The ARM64 adaptation of this library does not support address
> > + * translation and it assumes that physical address = dma address for such
> > + * a use case. Please don't build a platform that violates this.
> > + */
> 
> Why not just expand the ARM64 part to support address translation?

Because so far all hardware we have is relatively sane. We only
need to implement this if someone accidentally puts their DMA
space at the wrong address.

There is at least one platform that could in theory use this because
their RAM starts at an address that is outside of the reach of 32-bit
devices, and a static IOMMU mapping (created by firmware) could be
used to map the start of RAM into DMA address zero, to avoid having
to use an IOMMU all the time, but it was considered not worth the
effort to implement that.

	Arnd

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

end of thread, other threads:[~2016-03-29 19:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1458252137-24497-1-git-send-email-okaya@codeaurora.org>
2016-03-17 22:02 ` [PATCH 2/3] swiotlb: prefix dma_to_phys and phys_to_dma functions Sinan Kaya
2016-03-18 12:12   ` Robin Murphy
2016-03-18 15:00     ` Sinan Kaya
2016-03-28 18:29       ` Konrad Rzeszutek Wilk
2016-03-29 12:44         ` Stefano Stabellini
2016-03-29 12:57           ` Sinan Kaya
2016-03-29 19:32         ` Arnd Bergmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).