summaryrefslogtreecommitdiffstats
path: root/bsp/meta-rcar/meta-rcar-gen3-adas/recipes-kernel/linux/linux-renesas/0009-arm64-dma-check-parent-bus-restrictions-in-dma_capab.patch
blob: 7aa801148d36eb009aa34366e9d310103ddaf731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
From 98081ba7f59438b1c8f9176b5467b924b9b392f5 Mon Sep 17 00:00:00 2001
From: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
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 <nikita.yoush@cogentembedded.com>
---
 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