From 98081ba7f59438b1c8f9176b5467b924b9b392f5 Mon Sep 17 00:00:00 2001 From: Nikita Yushchenko Date: Sun, 24 Jun 2018 18:04:23 +0300 Subject: [PATCH 006/122] arm64: dma: check parent bus restrictions in dma_capable() It may happen that although hardware device is capable of full 64-bit addressing in DMA operations, way device is connected to the system imposes addressing limitations. Example: NVMe device connected to PCIe host sitting on AXI bus with 32-bit addressing (and no iommu configured). There is no agreement on setting dma_mask in this case. For low level, dma_mask must be 32-bit - that's what hardware provides for software. But for higher level, dma_mask must be 64-bit - that's what platform (hardware + swiotlb) provides for higher levels. dma_capable() is swiotlb's check if current address is DMAable by device directly or swiotlb should provide bounce buffer for it. Using dma_mask in dma_capable() assumes that dma_mask is set for low level - which does not always match reality. This patch changes dma_capable() to check additional restrictions. It is a workaround against ambiguous dma_mask semantics. Signed-off-by: Nikita Yushchenko --- arch/arm64/include/asm/device.h | 1 + arch/arm64/include/asm/dma-mapping.h | 9 ++++++++- arch/arm64/mm/dma-mapping.c | 13 ++++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/device.h b/arch/arm64/include/asm/device.h index 5a5fa47..ac3eb0c 100644 --- a/arch/arm64/include/asm/device.h +++ b/arch/arm64/include/asm/device.h @@ -24,6 +24,7 @@ struct dev_archdata { const struct dma_map_ops *dev_dma_ops; #endif bool dma_coherent; + u64 parent_dma_mask; }; struct pdev_archdata { diff --git a/arch/arm64/include/asm/dma-mapping.h b/arch/arm64/include/asm/dma-mapping.h index 0df756b..15edb0c 100644 --- a/arch/arm64/include/asm/dma-mapping.h +++ b/arch/arm64/include/asm/dma-mapping.h @@ -66,10 +66,17 @@ static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr) static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size) { + u64 mask; + if (!dev->dma_mask) return false; - return addr + size - 1 <= *dev->dma_mask; + mask = *dev->dma_mask; + if (dev->archdata.parent_dma_mask && + mask > dev->archdata.parent_dma_mask) + mask = dev->archdata.parent_dma_mask; + + return addr + size - 1 <= mask; } static inline void dma_mark_clean(void *addr, size_t size) diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c index 58470b1..c9c1892 100644 --- a/arch/arm64/mm/dma-mapping.c +++ b/arch/arm64/mm/dma-mapping.c @@ -928,7 +928,18 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, dev->dma_ops = &swiotlb_dma_ops; dev->archdata.dma_coherent = coherent; - __iommu_setup_dma_ops(dev, dma_base, size, iommu); + + if (!iommu) { + /* + * we don't yet support buses that have a non-zero mapping. + * Let's hope we won't need it + */ + WARN_ON(dma_base != 0); + + /* save parent_dma_mask for swiotlb's dma_capable() */ + dev->archdata.parent_dma_mask = size - 1; + } else + __iommu_setup_dma_ops(dev, dma_base, size, iommu); #ifdef CONFIG_XEN if (xen_initial_domain()) { -- 2.7.4