linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iommu/io-pgtable-arm: Don't use dma_to_phys()
@ 2015-09-17 14:22 Robin Murphy
  2015-09-17 14:52 ` Will Deacon
  2015-09-17 16:42 ` [PATCH v2] " Robin Murphy
  0 siblings, 2 replies; 10+ messages in thread
From: Robin Murphy @ 2015-09-17 14:22 UTC (permalink / raw)
  To: linux-arm-kernel

In checking whether DMA addresses differ from physical addresses, using
dma_to_phys() is actually the wrong thing to do, since it may hide any
DMA offset, which is precisely one of the things we are checking for.
Simply casting between the two address types, whilst ugly, is in fact
the appropriate course of action.

We can also reject any device with a fixed DMA offset up-front at page
table creation, leaving the allocation-time check for the more subtle
cases like bounce buffering due to an incorrect DMA mask.

Furthermore, we can then fix the hackish KConfig dependency so that
architectures without a dma_to_phys() implementation may still
COMPILE_TEST (or even use!) the code. The true dependency is on the
DMA API, so use the appropriate symbol for that.

Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 drivers/iommu/Kconfig          |  3 +--
 drivers/iommu/io-pgtable-arm.c | 16 ++++++++--------
 2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/iommu/Kconfig b/drivers/iommu/Kconfig
index 4664c2a..3dc1bcb 100644
--- a/drivers/iommu/Kconfig
+++ b/drivers/iommu/Kconfig
@@ -23,8 +23,7 @@ config IOMMU_IO_PGTABLE
 config IOMMU_IO_PGTABLE_LPAE
 	bool "ARMv7/v8 Long Descriptor Format"
 	select IOMMU_IO_PGTABLE
-	# SWIOTLB guarantees a dma_to_phys() implementation
-	depends on ARM || ARM64 || (COMPILE_TEST && SWIOTLB)
+	depends on HAS_DMA && (ARM || ARM64 || COMPILE_TEST)
 	help
 	  Enable support for the ARM long descriptor pagetable format.
 	  This allocator supports 4K/2M/1G, 16K/32M and 64K/512M page
diff --git a/drivers/iommu/io-pgtable-arm.c b/drivers/iommu/io-pgtable-arm.c
index 73c0748..e7f9ab9 100644
--- a/drivers/iommu/io-pgtable-arm.c
+++ b/drivers/iommu/io-pgtable-arm.c
@@ -202,11 +202,6 @@ typedef u64 arm_lpae_iopte;
 
 static bool selftest_running = false;
 
-static dma_addr_t __arm_lpae_dma_addr(struct device *dev, void *pages)
-{
-	return phys_to_dma(dev, virt_to_phys(pages));
-}
-
 static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 				    struct io_pgtable_cfg *cfg)
 {
@@ -226,7 +221,7 @@ static void *__arm_lpae_alloc_pages(size_t size, gfp_t gfp,
 		 * address directly, so if the DMA layer suggests it can't by
 		 * giving us back some translation, that bodes very badly...
 		 */
-		if (dma != __arm_lpae_dma_addr(dev, pages))
+		if ((u64)dma != (u64)virt_to_phys(pages))
 			goto out_unmap;
 	}
 
@@ -246,7 +241,7 @@ static void __arm_lpae_free_pages(void *pages, size_t size,
 	struct device *dev = cfg->iommu_dev;
 
 	if (!selftest_running)
-		dma_unmap_single(dev, __arm_lpae_dma_addr(dev, pages),
+		dma_unmap_single(dev, (dma_addr_t)virt_to_phys(pages),
 				 size, DMA_TO_DEVICE);
 	free_pages_exact(pages, size);
 }
@@ -259,7 +254,7 @@ static void __arm_lpae_set_pte(arm_lpae_iopte *ptep, arm_lpae_iopte pte,
 	*ptep = pte;
 
 	if (!selftest_running)
-		dma_sync_single_for_device(dev, __arm_lpae_dma_addr(dev, ptep),
+		dma_sync_single_for_device(dev, (dma_addr_t)virt_to_phys(ptep),
 					   sizeof(pte), DMA_TO_DEVICE);
 }
 
@@ -629,6 +624,11 @@ arm_lpae_alloc_pgtable(struct io_pgtable_cfg *cfg)
 	if (cfg->oas > ARM_LPAE_MAX_ADDR_BITS)
 		return NULL;
 
+	if (cfg->iommu_dev->dma_pfn_offset) {
+		dev_err(cfg->iommu_dev, "Cannot accommodate DMA offset for IOMMU page tables\n");
+		return NULL;
+	}
+
 	data = kmalloc(sizeof(*data), GFP_KERNEL);
 	if (!data)
 		return NULL;
-- 
1.9.1

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

end of thread, other threads:[~2015-09-22 16:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-17 14:22 [PATCH] iommu/io-pgtable-arm: Don't use dma_to_phys() Robin Murphy
2015-09-17 14:52 ` Will Deacon
2015-09-17 15:53   ` Robin Murphy
2015-09-17 16:42 ` [PATCH v2] " Robin Murphy
2015-09-18  8:55   ` Yong Wu
2015-09-18 11:04     ` Robin Murphy
2015-09-18 12:29       ` Russell King - ARM Linux
2015-09-22 13:25       ` Yong Wu
2015-09-22 16:23         ` Robin Murphy
2015-09-22 16:26           ` Will Deacon

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).