Skip to contentSkip to conversation

arm64/pci: fix dma coherency inheritance for PCI devices

PATCH In review Mark Salter 5 participants 7 messages Sep 19, 2014 – Sep 22, 2014 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64/pci: fix dma coherency inheritance for PCI devices

The default dma_ops for devices on arm64 systems are noncoherent in nature and rely upon special operations and bounce buffers to perform a device DMA operation to/from memory. Some drivers rely upon coherent operations involving suitably capable hardware. In this case, a "dma-coherent" property will exist on the corresponding Device Tree node for the bridge device, or one of its ancestors. This patch adds support for applying a DMA coherent dma_ops for PCI devices in the case of such a property.

[added search for device with of_node]
---
 arch/arm64/mm/dma-mapping.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/mm/dma-mapping.c +19 -2
index 4164c5a..0e26bd7 100644
@@ -23,10 +23,12 @@
 #include <linux/dma-mapping.h>
 #include <linux/dma-contiguous.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/platform_device.h>
 #include <linux/vmalloc.h>
 #include <linux/swiotlb.h>
 #include <linux/amba/bus.h>
+#include <linux/pci.h>
 
 #include <asm/cacheflush.h>
 
@@ -316,8 +318,18 @@ static int dma_bus_notifier(struct notifier_block *nb,
 	if (event != BUS_NOTIFY_ADD_DEVICE)
 		return NOTIFY_DONE;
 
-	if (of_property_read_bool(dev->of_node, "dma-coherent"))
-		set_dma_ops(dev, &coherent_swiotlb_dma_ops);
+	/*
+	 * Some devices won't have an of_node but a bus controller/bridge will.
+	 * Search up the device chain until we find an of_node to check.
+	 */
+	while (dev) {
+		if (dev->of_node) {
+			if (of_dma_is_coherent(dev->of_node))
+				set_dma_ops(_dev, &coherent_swiotlb_dma_ops);
+			break;
+		}
+		dev = dev->parent;
+	}
 
 	return NOTIFY_OK;
 }
@@ -330,6 +342,10 @@ static struct notifier_block amba_bus_nb = {
 	.notifier_call = dma_bus_notifier,
 };
 
+static struct notifier_block pci_bus_nb = {
+	.notifier_call = dma_bus_notifier,
+};
+
 extern int swiotlb_late_init_with_default_size(size_t default_size);
 
 static int __init swiotlb_late_init(void)
@@ -341,6 +357,7 @@ static int __init swiotlb_late_init(void)
 	 */
 	bus_register_notifier(&platform_bus_type, &platform_bus_nb);
 	bus_register_notifier(&amba_bustype, &amba_bus_nb);
+	bus_register_notifier(&pci_bus_type, &pci_bus_nb);
 
 	dma_ops = &noncoherent_swiotlb_dma_ops;
 
Signature · 1 line
1.9.3
#1 lore raw
Quoted 41 lines · Mark Salter

The default dma_ops for devices on arm64 systems are noncoherent in nature and rely upon special operations and bounce buffers to perform a device DMA operation to/from memory. Some drivers rely upon coherent operations involving suitably capable hardware. In this case, a "dma-coherent" property will exist on the corresponding Device Tree node for the bridge device, or one of its ancestors. This patch adds support for applying a DMA coherent dma_ops for PCI devices in the case of such a property.

[added search for device with of_node]
---
 arch/arm64/mm/dma-mapping.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/mm/dma-mapping.c +4 -2
index 4164c5a..0e26bd7 100644
@@ -23,10 +23,12 @@
 #include <linux/dma-mapping.h>
 #include <linux/dma-contiguous.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/platform_device.h>
 #include <linux/vmalloc.h>
 #include <linux/swiotlb.h>
 #include <linux/amba/bus.h>
+#include <linux/pci.h>
 
 #include <asm/cacheflush.h>
 
@@ -316,8 +318,18 @@ static int dma_bus_notifier(struct notifier_block *nb,
 	if (event != BUS_NOTIFY_ADD_DEVICE)
 		return NOTIFY_DONE;
 
-	if (of_property_read_bool(dev->of_node, "dma-coherent"))
-		set_dma_ops(dev, &coherent_swiotlb_dma_ops);
+	/*
+	 * Some devices won't have an of_node but a bus controller/bridge will.

arch/arm64/mm/dma-mapping.c static int dma_bus_notifier(struct notifier_block *nb, · line 332

Quoted 10 lines
arch/arm64/mm/dma-mapping.c +10 -0
+	 * Search up the device chain until we find an of_node to check.
+	 */
+	while (dev) {
+		if (dev->of_node) {
+			if (of_dma_is_coherent(dev->of_node))
+				set_dma_ops(_dev, &coherent_swiotlb_dma_ops);
+			break;
+		}
+		dev = dev->parent;
+	}

Hmm, how does this interact with of_dma_configure? I don't think the dma-coherent property is inherited there, so I'm uneasy about doing this for buses other than PCI (i.e. the platform bus).

In other words, we should move to using of_dma_configure for the platform and amba buses, and have a separate notifier for the PCI bus. Is it likely that people will have a mixture of coherent and non-coherent devices behind the same host controller?

Will

Quoted 25 lines
arch/arm64/mm/dma-mapping.c +5 -0
 	return NOTIFY_OK;
 }
@@ -330,6 +342,10 @@ static struct notifier_block amba_bus_nb = {
 	.notifier_call = dma_bus_notifier,
 };
 
+static struct notifier_block pci_bus_nb = {
+	.notifier_call = dma_bus_notifier,
+};
+
 extern int swiotlb_late_init_with_default_size(size_t default_size);
 
 static int __init swiotlb_late_init(void)
@@ -341,6 +357,7 @@ static int __init swiotlb_late_init(void)
 	 */
 	bus_register_notifier(&platform_bus_type, &platform_bus_nb);
 	bus_register_notifier(&amba_bustype, &amba_bus_nb);
+	bus_register_notifier(&pci_bus_type, &pci_bus_nb);
 
 	dma_ops = &noncoherent_swiotlb_dma_ops;
 
Signature · 1 line
1.9.3
#2 lore raw
Quoted 57 lines · Will Deacon
Quoted 41 lines · Mark Salter

The default dma_ops for devices on arm64 systems are noncoherent in nature and rely upon special operations and bounce buffers to perform a device DMA operation to/from memory. Some drivers rely upon coherent operations involving suitably capable hardware. In this case, a "dma-coherent" property will exist on the corresponding Device Tree node for the bridge device, or one of its ancestors. This patch adds support for applying a DMA coherent dma_ops for PCI devices in the case of such a property.

[added search for device with of_node]
---
 arch/arm64/mm/dma-mapping.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/mm/dma-mapping.c +4 -2
index 4164c5a..0e26bd7 100644
@@ -23,10 +23,12 @@
 #include <linux/dma-mapping.h>
 #include <linux/dma-contiguous.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/platform_device.h>
 #include <linux/vmalloc.h>
 #include <linux/swiotlb.h>
 #include <linux/amba/bus.h>
+#include <linux/pci.h>
 
 #include <asm/cacheflush.h>
 
@@ -316,8 +318,18 @@ static int dma_bus_notifier(struct notifier_block *nb,
 	if (event != BUS_NOTIFY_ADD_DEVICE)
 		return NOTIFY_DONE;
 
-	if (of_property_read_bool(dev->of_node, "dma-coherent"))
-		set_dma_ops(dev, &coherent_swiotlb_dma_ops);
+	/*
+	 * Some devices won't have an of_node but a bus controller/bridge will.

arch/arm64/mm/dma-mapping.c static int dma_bus_notifier(struct notifier_block *nb, · line 332

Quoted 10 lines
arch/arm64/mm/dma-mapping.c +10 -0
+	 * Search up the device chain until we find an of_node to check.
+	 */
+	while (dev) {
+		if (dev->of_node) {
+			if (of_dma_is_coherent(dev->of_node))
+				set_dma_ops(_dev, &coherent_swiotlb_dma_ops);
+			break;
+		}
+		dev = dev->parent;
+	}

Hmm, how does this interact with of_dma_configure? I don't think the dma-coherent property is inherited there, so I'm uneasy about doing this for buses other than PCI (i.e. the platform bus).

Quoted 4 lines

In other words, we should move to using of_dma_configure for the platform and amba buses, and have a separate notifier for the PCI bus. Is it likely that people will have a mixture of coherent and non-coherent devices behind the same host controller?

Right. My original internal patch had a separate method for the PCI bus rather than rolling these back into one...but I suggested to Mark that I thought Catalin and you wanted this rolled back into one. If you'd rather there be two, well that's really easy as we know that works.

Jon.

#3 lore raw
In reply to Will Deacon
Quoted 48 lines · Will Deacon
Quoted 47 lines · Mark Salter

The default dma_ops for devices on arm64 systems are noncoherent in nature and rely upon special operations and bounce buffers to perform a device DMA operation to/from memory. Some drivers rely upon coherent operations involving suitably capable hardware. In this case, a "dma-coherent" property will exist on the corresponding Device Tree node for the bridge device, or one of its ancestors. This patch adds support for applying a DMA coherent dma_ops for PCI devices in the case of such a property.

[added search for device with of_node]
---
 arch/arm64/mm/dma-mapping.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/mm/dma-mapping.c +10 -2
index 4164c5a..0e26bd7 100644
@@ -23,10 +23,12 @@
 #include <linux/dma-mapping.h>
 #include <linux/dma-contiguous.h>
 #include <linux/of.h>
+#include <linux/of_address.h>
 #include <linux/platform_device.h>
 #include <linux/vmalloc.h>
 #include <linux/swiotlb.h>
 #include <linux/amba/bus.h>
+#include <linux/pci.h>
 
 #include <asm/cacheflush.h>
 
@@ -316,8 +318,18 @@ static int dma_bus_notifier(struct notifier_block *nb,
 	if (event != BUS_NOTIFY_ADD_DEVICE)
 		return NOTIFY_DONE;
 
-	if (of_property_read_bool(dev->of_node, "dma-coherent"))
-		set_dma_ops(dev, &coherent_swiotlb_dma_ops);
+	/*
+	 * Some devices won't have an of_node but a bus controller/bridge will.
+	 * Search up the device chain until we find an of_node to check.
+	 */
+	while (dev) {
+		if (dev->of_node) {
+			if (of_dma_is_coherent(dev->of_node))
+				set_dma_ops(_dev, &coherent_swiotlb_dma_ops);
Quoted 8 lines

arch/arm64/mm/dma-mapping.c static int dma_bus_notifier(struct notifier_block *nb, · line 332

Quoted 4 lines
arch/arm64/mm/dma-mapping.c +4 -0
+			break;
+		}
+		dev = dev->parent;
+	}

Hmm, how does this interact with of_dma_configure? I don't think the dma-coherent property is inherited there, so I'm uneasy about doing this for buses other than PCI (i.e. the platform bus).

of_dma_configure() only gets called for platform bus and it uses of_dma_is_coherent() which searches device ancestors for dma-coherent property. But it uses set_arch_dma_coherent_ops() which is a no-op on arm64 now. Catalin mentions this in another
thread: https://lkml.org/lkml/2014/9/15/363

So maybe we should fix set_arch_dma_coherent_ops() and drop platform bus from the notifier handler.

Quoted 33 lines

In other words, we should move to using of_dma_configure for the platform and amba buses, and have a separate notifier for the PCI bus. Is it likely that people will have a mixture of coherent and non-coherent devices behind the same host controller?

Will

Quoted 25 lines
arch/arm64/mm/dma-mapping.c +5 -0
 	return NOTIFY_OK;
 }
@@ -330,6 +342,10 @@ static struct notifier_block amba_bus_nb = {
 	.notifier_call = dma_bus_notifier,
 };
 
+static struct notifier_block pci_bus_nb = {
+	.notifier_call = dma_bus_notifier,
+};
+
 extern int swiotlb_late_init_with_default_size(size_t default_size);
 
 static int __init swiotlb_late_init(void)
@@ -341,6 +357,7 @@ static int __init swiotlb_late_init(void)
 	 */
 	bus_register_notifier(&platform_bus_type, &platform_bus_nb);
 	bus_register_notifier(&amba_bustype, &amba_bus_nb);
+	bus_register_notifier(&pci_bus_type, &pci_bus_nb);
 
 	dma_ops = &noncoherent_swiotlb_dma_ops;
 
Signature · 1 line
1.9.3
#4 lore raw

Hi Mark,

Quoted 58 lines · Mark Salter
Quoted 48 lines · Will Deacon
Quoted 47 lines · Mark Salter

The default dma_ops for devices on arm64 systems are noncoherent in nature and rely upon special operations and bounce buffers to perform a device DMA operation to/from memory. Some drivers rely upon coherent operations involving suitably capable hardware. In this case, a "dma-coherent" property will exist on the corresponding Device Tree node for the bridge device, or one of its ancestors. This patch adds support for applying a DMA coherent dma_ops for PCI devices in the case of such a property.

[added search for device with of_node]
---
  arch/arm64/mm/dma-mapping.c | 21 +++++++++++++++++++--
  1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/mm/dma-mapping.c +10 -2
index 4164c5a..0e26bd7 100644
@@ -23,10 +23,12 @@
  #include <linux/dma-mapping.h>
  #include <linux/dma-contiguous.h>
  #include <linux/of.h>
+#include <linux/of_address.h>
  #include <linux/platform_device.h>
  #include <linux/vmalloc.h>
  #include <linux/swiotlb.h>
  #include <linux/amba/bus.h>
+#include <linux/pci.h>
 
  #include <asm/cacheflush.h>
 
@@ -316,8 +318,18 @@ static int dma_bus_notifier(struct notifier_block *nb,
  	if (event != BUS_NOTIFY_ADD_DEVICE)
  		return NOTIFY_DONE;
 
-	if (of_property_read_bool(dev->of_node, "dma-coherent"))
-		set_dma_ops(dev, &coherent_swiotlb_dma_ops);
+	/*
+	 * Some devices won't have an of_node but a bus controller/bridge will.
+	 * Search up the device chain until we find an of_node to check.
+	 */
+	while (dev) {
+		if (dev->of_node) {
+			if (of_dma_is_coherent(dev->of_node))
+				set_dma_ops(_dev, &coherent_swiotlb_dma_ops);
Quoted 8 lines

arch/arm64/mm/dma-mapping.c static int dma_bus_notifier(struct notifier_block *nb, · line 332

Quoted 4 lines
arch/arm64/mm/dma-mapping.c +4 -0
+			break;
+		}
+		dev = dev->parent;
+	}

Hmm, how does this interact with of_dma_configure? I don't think the dma-coherent property is inherited there, so I'm uneasy about doing this for buses other than PCI (i.e. the platform bus).

Quoted 1 line

of_dma_configure() only gets called for platform bus and it

It's going to be used for AMBA bus too - "[PATCH] of: amba: use of_dma_configure for AMBA devices"
http://www.spinics.net/lists/arm-kernel/msg363169.html

Quoted 2 lines

uses of_dma_is_coherent() which searches device ancestors for dma-coherent property. But it uses set_arch_dma_coherent_ops()

of_dma_is_coherent() will search from current Device node and all its ancestors.

Quoted 5 lines

which is a no-op on arm64 now. Catalin mentions this in another
thread: https://lkml.org/lkml/2014/9/15/363

So maybe we should fix set_arch_dma_coherent_ops() and drop platform bus from the notifier handler.

Yes. That will allow you to drop platform bus notifier handler. Actually, if patch "[PATCH] of: amba: use of_dma_configure for AMBA devices" will be merged then notifier for AMBA bus can be dropped too.

Quoted 7 lines
Quoted 7 lines

In other words, we should move to using of_dma_configure for the platform and amba buses, and have a separate notifier for the PCI bus. Is it likely that people will have a mixture of coherent and non-coherent devices behind the same host controller?

Also, there is a patch which updates DT documentation for DMA properties, but it's not been merged accidentally :(
"Re: [PATCH] dt/documentation: add specification of dma bus information"
https://lkml.org/lkml/2014/6/22/144

Regards,
-grygorii

maintainer
#5 lore raw
In reply to Mark Salter
Quoted 2 lines · Mark Salter
arch/arm64/mm/dma-mapping.c +0 -0
index 4164c5a..0e26bd7 100644

[...]

Quoted 11 lines
arch/arm64/mm/dma-mapping.c +5 -2
@@ -316,8 +318,18 @@ static int dma_bus_notifier(struct notifier_block *nb,
 	if (event != BUS_NOTIFY_ADD_DEVICE)
 		return NOTIFY_DONE;
 
-	if (of_property_read_bool(dev->of_node, "dma-coherent"))
-		set_dma_ops(dev, &coherent_swiotlb_dma_ops);
+	/*
+	 * Some devices won't have an of_node but a bus controller/bridge will.
+	 * Search up the device chain until we find an of_node to check.
+	 */
+	while (dev) {

arch/arm64/mm/dma-mapping.c static int dma_bus_notifier(struct notifier_block *nb, · line 335

Quoted 10 lines
arch/arm64/mm/dma-mapping.c +7 -0
+		if (dev->of_node) {
+			if (of_dma_is_coherent(dev->of_node))
+				set_dma_ops(_dev, &coherent_swiotlb_dma_ops);
+			break;
+		}
+		dev = dev->parent;
+	}
 
 	return NOTIFY_OK;
 }

For AMBA (with an additional patch) and platform devices, we can do the same as ARM and rely on set_arch_dma_coherent_ops(), so most of the notifier code removed. So we leave the above notifier for PCI only.

arch/arm64/mm/dma-mapping.c static int __init swiotlb_late_init(void) · line 360

Quoted 6 lines

static int __init swiotlb_late_init(void)

arch/arm64/mm/dma-mapping.c +1 -0
@@ -341,6 +357,7 @@ static int __init swiotlb_late_init(void)
 	 */
 	bus_register_notifier(&platform_bus_type, &platform_bus_nb);
 	bus_register_notifier(&amba_bustype, &amba_bus_nb);
+	bus_register_notifier(&pci_bus_type, &pci_bus_nb);

Does this compile when CONFIG_PCI is disabled?

Signature · 1 line
Catalin
#6 lore raw
Quoted 8 lines · Catalin Marinas

arch/arm64/mm/dma-mapping.c static int __init swiotlb_late_init(void) · line 360

Quoted 6 lines

static int __init swiotlb_late_init(void)

arch/arm64/mm/dma-mapping.c +1 -0
@@ -341,6 +357,7 @@ static int __init swiotlb_late_init(void)
       */
      bus_register_notifier(&platform_bus_type, &platform_bus_nb);
      bus_register_notifier(&amba_bustype, &amba_bus_nb);
+     bus_register_notifier(&pci_bus_type, &pci_bus_nb);

Does this compile when CONFIG_PCI is disabled?

No. Doh!