Skip to contentSkip to conversation

Use 'arm_nommu_dma_ops' to handle dma memroy if device offer consistent dma memory region

PATCH In review [email protected] 4 participants 16 messages Jun 8, 2020 – Jun 12, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Use 'arm_nommu_dma_ops' to handle dma memroy if device offer consistent dma memory region

From: dillon min <[email protected]>

when do mmap on /dev/fb0, we will get -6 error on cortex-m3/m4 or armv7m platform without cache support, this is caused by following reason:

on armv7m platform, if no cache support, we will use dma direct mapping, but, this is not support on !MMU hardware, just return '-ENXIO' error

so, add use_reserved_mem() for these armv7m hardware but no-cache support. eg, stm32f429/stm32f469.

verified on stm32f469-disco board, mmap frambuffer to userspace.

dillon min (2):
ARM: dts: stm32: Setup 4M bytes reserved memory for mmap
arm-nommu: Add use_reserved_mem() to check if device support reserved
memory

 arch/arm/boot/dts/stm32f469-disco.dts | 14 ++++++++++++++
 arch/arm/mm/dma-mapping-nommu.c       | 28 +++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/2 ARM: dts: stm32: Setup 4M bytes reserved memory for mmap

From: dillon min <[email protected]>

To mmap a framebuffer or v4l2 buffer from kernel to userspace on no-mmu platform, we need rely on 'arm_nommu_dma_ops' from arch/arm/mm/dma-mapping-nommu.c , so setup 4M bytes memory reserved for this purpose.

---
 arch/arm/boot/dts/stm32f469-disco.dts | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/arm/boot/dts/stm32f469-disco.dts +14 -0
index 9397db0c43de..082b24ee81f7 100644
@@ -65,6 +65,20 @@
 		reg = <0x00000000 0x1000000>;
 	};
 
+	reserved-memory {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges;
+
+		linux,dma {
+			compatible = "shared-dma-pool";
+			size = <0x400000>;
+			no-map;
+			linux,dma-default;
+		};
+
+	};
+
 	aliases {
 		serial0 = &usart3;
 	};
Signature · 1 line
2.7.4
author
#2 lore raw

Re: [PATCH 1/2] ARM: dts: stm32: Setup 4M bytes reserved memory for mmap

Hi Christoph Hellwig,

This is the patchset. for your reference.

thanks,

Quoted 41 lines · On Mon, Jun 8, 2020 at 4:30 PM <[email protected]> wrote:

From: dillon min <[email protected]>

To mmap a framebuffer or v4l2 buffer from kernel to userspace on no-mmu platform, we need rely on 'arm_nommu_dma_ops' from arch/arm/mm/dma-mapping-nommu.c , so setup 4M bytes memory reserved for this purpose.

---
 arch/arm/boot/dts/stm32f469-disco.dts | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/arm/boot/dts/stm32f469-disco.dts +14 -1
index 9397db0c43de..082b24ee81f7 100644
@@ -65,6 +65,20 @@
                reg = <0x00000000 0x1000000>;
        };
 
+       reserved-memory {
+               #address-cells = <1>;
+               #size-cells = <1>;
+               ranges;
+
+               linux,dma {
+                       compatible = "shared-dma-pool";
+                       size = <0x400000>;
+                       no-map;
+                       linux,dma-default;
+               };
+
+       };
+
        aliases {
                serial0 = &usart3;
        };
--

2.7.4

#3 lore raw

PATCH 2/2 arm-nommu: Add use_reserved_mem() to check if device support reserved memory

In reply to [email protected]

From: dillon min <[email protected]>

Currently, we use dma direct to request coherent memory for driver on armv7m platform if 'cacheid' is zero, but dma_direct_can_mmap() is return false, dma_direct_mmap() return -ENXIO for CONFIG_MMU undefined platform.

so we have to back to use 'arm_nommu_dma_ops', add use_reserved_mem() to check if device support global or device corherent memory. if yes, then call set_dma_ops()

---
 arch/arm/mm/dma-mapping-nommu.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
arch/arm/mm/dma-mapping-nommu.c +27 -1
index 287ef898a55e..e1c213fec152 100644
@@ -14,6 +14,7 @@
 #include <asm/cacheflush.h>
 #include <asm/outercache.h>
 #include <asm/cp15.h>
+#include <linux/of.h>
 
 #include "dma.h"
 
@@ -188,6 +189,31 @@ const struct dma_map_ops arm_nommu_dma_ops = {
 };
 EXPORT_SYMBOL(arm_nommu_dma_ops);
 
+static bool use_reserved_mem(struct device *dev)
+{
+	struct device_node *np;
+
+	np = of_find_node_by_path("/reserved-memory/linux,dma");
+
+	if (np &&
+		of_device_is_compatible(np, "shared-dma-pool") &&
+		of_property_read_bool(np, "no-map") &&
+		of_property_read_bool(np, "linux,dma-default")) {
+		/* has global corherent mem support */
+		of_node_put(np);
+		return true;
+	}
+
+	np = of_parse_phandle(dev->of_node, "memory-region", 0);
+	if (np) {
+		/* has dev corherent mem support */
+		of_node_put(np);
+		return true;
+	}
+
+	return false;
+}
+
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 			const struct iommu_ops *iommu, bool coherent)
 {
@@ -206,6 +232,6 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 		dev->archdata.dma_coherent = (get_cr() & CR_M) ? coherent : true;
 	}
 
-	if (!dev->archdata.dma_coherent)
+	if (!dev->archdata.dma_coherent || use_reserved_mem(dev))
 		set_dma_ops(dev, &arm_nommu_dma_ops);
 }
Signature · 1 line
2.7.4
#4 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 61 lines · On 6/8/20 9:30 AM, [email protected] wrote:

From: dillon min <[email protected]>

Currently, we use dma direct to request coherent memory for driver on armv7m platform if 'cacheid' is zero, but dma_direct_can_mmap() is return false, dma_direct_mmap() return -ENXIO for CONFIG_MMU undefined platform.

so we have to back to use 'arm_nommu_dma_ops', add use_reserved_mem() to check if device support global or device corherent memory. if yes, then call set_dma_ops()

---
 arch/arm/mm/dma-mapping-nommu.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
arch/arm/mm/dma-mapping-nommu.c +26 -0
index 287ef898a55e..e1c213fec152 100644
@@ -14,6 +14,7 @@
 #include <asm/cacheflush.h>
 #include <asm/outercache.h>
 #include <asm/cp15.h>
+#include <linux/of.h>
 
 #include "dma.h"
 
@@ -188,6 +189,31 @@ const struct dma_map_ops arm_nommu_dma_ops = {
 };
 EXPORT_SYMBOL(arm_nommu_dma_ops);
 
+static bool use_reserved_mem(struct device *dev)
+{
+	struct device_node *np;
+
+	np = of_find_node_by_path("/reserved-memory/linux,dma");
+
+	if (np &&
+		of_device_is_compatible(np, "shared-dma-pool") &&
+		of_property_read_bool(np, "no-map") &&
+		of_property_read_bool(np, "linux,dma-default")) {
+		/* has global corherent mem support */
+		of_node_put(np);
+		return true;
+	}
+
+	np = of_parse_phandle(dev->of_node, "memory-region", 0);
+	if (np) {
+		/* has dev corherent mem support */
+		of_node_put(np);
+		return true;
+	}
+
+	return false;
+}
+
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 			const struct iommu_ops *iommu, bool coherent)
 {

arch/arm/mm/dma-mapping-nommu.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 237

Quoted 7 lines
arch/arm/mm/dma-mapping-nommu.c +1 -1
@@ -206,6 +232,6 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 		dev->archdata.dma_coherent = (get_cr() & CR_M) ? coherent : true;
 	}
 
-	if (!dev->archdata.dma_coherent)
+	if (!dev->archdata.dma_coherent || use_reserved_mem(dev))
 		set_dma_ops(dev, &arm_nommu_dma_ops);
 }

Sorry I have to NAK this hack :(

Digging git history reveled 79964a1c2972 ("ARM: 8633/1: nommu: allow mmap when !CONFIG_MMU") which make me wonder if diff below does the trick for you

kernel/dma/direct.c +2 -2
index 8f4bbda..8623b9e 100644
@@ -456,14 +456,14 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 #else /* CONFIG_MMU */
 bool dma_direct_can_mmap(struct device *dev)
 {
-	return false;
+	return true;
 }
 
 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
 		unsigned long attrs)
 {
-	return -ENXIO;
+	return vm_iomap_memory(vma, vma->vm_start, (vma->vm_end - vma->vm_start));;
 }
 #endif /* CONFIG_MMU */
 

Cheers
Vladimir

author
#5 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Hi Vladimir,

Thanks for reviewing.

Hi Christoph Hellwig,

I just want to know if kernel dma mapping/direct is focused on platforms with MMU.
leave arch code to handle dma coherent memory management themself for no-MMU platform.

so, you just return error code in kernel/dma/mapping.c,direct.c without CONFIG_MMU defined ?
which means dma-direct mapping doesn't support !CONFIG_MMU is not a bug, but design as it's.
or, just return error code currently, will add dma direct mapping support for !CONFIG_MMU in the
future?

As Vladimir Murzin's suggestion has changes in kernel code, I need your input to get
the design goal about dma-direct mapping, thanks.

Quoted 86 lines · Vladimir Murzin
Quoted 61 lines · On 6/8/20 9:30 AM, [email protected] wrote:

From: dillon min <[email protected]>

Currently, we use dma direct to request coherent memory for driver on armv7m platform if 'cacheid' is zero, but dma_direct_can_mmap() is return false, dma_direct_mmap() return -ENXIO for CONFIG_MMU undefined platform.

so we have to back to use 'arm_nommu_dma_ops', add use_reserved_mem() to check if device support global or device corherent memory. if yes, then call set_dma_ops()

---
 arch/arm/mm/dma-mapping-nommu.c | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)
arch/arm/mm/dma-mapping-nommu.c +26 -0
index 287ef898a55e..e1c213fec152 100644
@@ -14,6 +14,7 @@
 #include <asm/cacheflush.h>
 #include <asm/outercache.h>
 #include <asm/cp15.h>
+#include <linux/of.h>
 
 #include "dma.h"
 
@@ -188,6 +189,31 @@ const struct dma_map_ops arm_nommu_dma_ops = {
 };
 EXPORT_SYMBOL(arm_nommu_dma_ops);
 
+static bool use_reserved_mem(struct device *dev)
+{
+     struct device_node *np;
+
+     np = of_find_node_by_path("/reserved-memory/linux,dma");
+
+     if (np &&
+             of_device_is_compatible(np, "shared-dma-pool") &&
+             of_property_read_bool(np, "no-map") &&
+             of_property_read_bool(np, "linux,dma-default")) {
+             /* has global corherent mem support */
+             of_node_put(np);
+             return true;
+     }
+
+     np = of_parse_phandle(dev->of_node, "memory-region", 0);
+     if (np) {
+             /* has dev corherent mem support */
+             of_node_put(np);
+             return true;
+     }
+
+     return false;
+}
+
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
                      const struct iommu_ops *iommu, bool coherent)
 {

arch/arm/mm/dma-mapping-nommu.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 237

Quoted 7 lines
arch/arm/mm/dma-mapping-nommu.c +1 -1
@@ -206,6 +232,6 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
              dev->archdata.dma_coherent = (get_cr() & CR_M) ? coherent : true;
      }
 
-     if (!dev->archdata.dma_coherent)
+     if (!dev->archdata.dma_coherent || use_reserved_mem(dev))
              set_dma_ops(dev, &arm_nommu_dma_ops);
 }

Sorry I have to NAK this hack :(

Digging git history reveled 79964a1c2972 ("ARM: 8633/1: nommu: allow mmap when !CONFIG_MMU") which make me wonder if diff below does the trick for you

kernel/dma/direct.c +1 -1
index 8f4bbda..8623b9e 100644
@@ -456,14 +456,14 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 #else /* CONFIG_MMU */
 bool dma_direct_can_mmap(struct device *dev)
 {
-       return false;
+       return true;

kernel/dma/direct.c int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 468

Quoted 10 lines
kernel/dma/direct.c +1 -1
 }
 
 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
                void *cpu_addr, dma_addr_t dma_addr, size_t size,
                unsigned long attrs)
 {
-       return -ENXIO;
+       return vm_iomap_memory(vma, vma->vm_start, (vma->vm_end - vma->vm_start));;
 }
 #endif /* CONFIG_MMU */

Yes, this is a quite nice way to support !CONFIG_MMU without cache on platforms. I will try your suggestion.
thanks

Quoted 3 lines

Cheers
Vladimir

#6 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 10 lines · dillon min

Hi Vladimir,

Thanks for reviewing.

Hi Christoph Hellwig,

I just want to know if kernel dma mapping/direct is focused on platforms with MMU.
leave arch code to handle dma coherent memory management themself for no-MMU platform.

No, I'd really like to consolidate everything that isn't overly arch specific eventually.

Quoted 9 lines

so, you just return error code in kernel/dma/mapping.c,direct.c without CONFIG_MMU defined ?
which means dma-direct mapping doesn't support !CONFIG_MMU is not a bug, but design as it's.
or, just return error code currently, will add dma direct mapping support for !CONFIG_MMU in the
future?

Quoted 3 lines

As Vladimir Murzin's suggestion has changes in kernel code, I need your input to get
the design goal about dma-direct mapping, thanks.

Can someone repost the whole patch?

#7 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 24 lines · Christoph Hellwig
Quoted 10 lines · dillon min

Hi Vladimir,

Thanks for reviewing.

Hi Christoph Hellwig,

I just want to know if kernel dma mapping/direct is focused on platforms with MMU.
leave arch code to handle dma coherent memory management themself for no-MMU platform.

No, I'd really like to consolidate everything that isn't overly arch specific eventually.

Quoted 9 lines

so, you just return error code in kernel/dma/mapping.c,direct.c without CONFIG_MMU defined ?
which means dma-direct mapping doesn't support !CONFIG_MMU is not a bug, but design as it's.
or, just return error code currently, will add dma direct mapping support for !CONFIG_MMU in the
future?

Quoted 6 lines
Quoted 3 lines

As Vladimir Murzin's suggestion has changes in kernel code, I need your input to get
the design goal about dma-direct mapping, thanks.

Can someone repost the whole patch?

Happy to repost as separate patch once dillon confirms it actually works.

Meanwhile, I'm trying to understand at which point we lost this functionality for NOMMU... maybe it will become different patch :)

Cheers
Vladimir

#8 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 29 lines · Vladimir Murzin
Quoted 28 lines · Christoph Hellwig
Quoted 10 lines · dillon min

Hi Vladimir,

Thanks for reviewing.

Hi Christoph Hellwig,

I just want to know if kernel dma mapping/direct is focused on platforms with MMU.
leave arch code to handle dma coherent memory management themself for no-MMU platform.

No, I'd really like to consolidate everything that isn't overly arch specific eventually.

Quoted 12 lines

so, you just return error code in kernel/dma/mapping.c,direct.c without CONFIG_MMU defined ?
which means dma-direct mapping doesn't support !CONFIG_MMU is not a bug, but design as it's.
or, just return error code currently, will add dma direct mapping support for !CONFIG_MMU in the
future?

As Vladimir Murzin's suggestion has changes in kernel code, I need your input to get
the design goal about dma-direct mapping, thanks.

Quoted 8 lines
Quoted 2 lines

Can someone repost the whole patch?

Happy to repost as separate patch once dillon confirms it actually works.

Meanwhile, I'm trying to understand at which point we lost this functionality for NOMMU... maybe it will become different patch :)

mmap operation for dma-noop (ancestor of dma-direct) was proposed in [1]. It was suggested to change dma_common_map() instead which was implemented in

07c75d7a6b9e ("drivers: dma-mapping: allow dma_common_mmap() for NOMMU")

that removed CONFIG_MMU drom dma_common_mmap(). Later

62fcee9a3bd7 ("dma-mapping: remove CONFIG_ARCH_NO_COHERENT_DMA_MMAP")

reintroduced CONFIG_MMU in dma_common_mmap().

Even though commit mentions ARM, I do not see how mmap would continue to work for NOMMU with dma-direct. ARM NOMMU needs it's own DMA operations only in cases where caches are implemented or active, in other cases it fully relies on dma-direct.

It looks to me that we either should provide NOMMU variant for mmap in dma/direct or (carefully) fix dma/mapping.

Thoughts?

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2017-January/480600.html

Vladimir

Quoted 3 lines

Cheers
Vladimir

#9 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 7 lines · Vladimir Murzin

Even though commit mentions ARM, I do not see how mmap would continue to work for NOMMU with dma-direct. ARM NOMMU needs it's own DMA operations only in cases where caches are implemented or active, in other cases it fully relies on dma-direct.

It looks to me that we either should provide NOMMU variant for mmap in dma/direct or (carefully) fix dma/mapping.

I think dma-direct is the right place, the common helpers in dma/mapping.c are basically the red headed stepchilds for misc IOMMU drivers not covered by dma-iommu only.

author
#10 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Hi Vladimir,

I tested your changes, it's working fine on stm32f429-disco(armv7m, without cache) board.
you can submit a separate patch for dma-direct support on non-mmu platform, i will drop mine.

thanks.

best regards.

Dillon,

Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Vladimir Murzin

Even though commit mentions ARM, I do not see how mmap would continue to work for NOMMU with dma-direct. ARM NOMMU needs it's own DMA operations only in cases where caches are implemented or active, in other cases it fully relies on dma-direct.

Quoted 6 lines
Quoted 2 lines

It looks to me that we either should provide NOMMU variant for mmap in dma/direct or (carefully) fix dma/mapping.

I think dma-direct is the right place, the common helpers in dma/mapping.c are basically the red headed stepchilds for misc IOMMU drivers not covered by dma-iommu only.

Yes, thanks Christoph's input.

#11 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

In reply to Vladimir Murzin

Ok, I finally found the original patch from Vladimir. Comments below:

Quoted 5 lines

+++ b/kernel/dma/direct.c

(quoted diff) +0 -0
@@ -456,14 +456,14 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 #else /* CONFIG_MMU */
 bool dma_direct_can_mmap(struct device *dev)
 {

Review int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 466

Quoted 10 lines
(quoted diff) +2 -2
-	return false;
+	return true;
 }
 
 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
 		unsigned long attrs)
 {
-	return -ENXIO;
+	return vm_iomap_memory(vma, vma->vm_start, (vma->vm_end - vma->vm_start));;

I think we should try to reuse the mmu dma_direct_mmap implementation, which does about the same. This version has been compile tested on arm-nommu only, let me know what you think: (btw, a nommu_defconfig of some kind for arm would be nice..)

kernel/dma/Kconfig +1 -0
index d006668c0027d2..e0dae570a51530 100644
@@ -71,6 +71,7 @@ config SWIOTLB
 # in the pagetables
 #
 config DMA_NONCOHERENT_MMAP
+	default y if !MMU
 	bool
 
 config DMA_REMAP
kernel/dma/direct.c +0 -14
index 0a4881e59aa7d6..9ec6a5c3fc578c 100644
@@ -459,7 +459,6 @@ int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
 	return ret;
 }
 
-#ifdef CONFIG_MMU
 bool dma_direct_can_mmap(struct device *dev)
 {
 	return dev_is_dma_coherent(dev) ||
@@ -485,19 +484,6 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 	return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
 			user_count << PAGE_SHIFT, vma->vm_page_prot);
 }
-#else /* CONFIG_MMU */
-bool dma_direct_can_mmap(struct device *dev)
-{
-	return false;
-}
-
-int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
-		void *cpu_addr, dma_addr_t dma_addr, size_t size,
-		unsigned long attrs)
-{
-	return -ENXIO;
-}
-#endif /* CONFIG_MMU */
 
 int dma_direct_supported(struct device *dev, u64 mask)
 {

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Reviewed-by
#12 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 11 lines · Christoph Hellwig

Ok, I finally found the original patch from Vladimir. Comments below:

Quoted 9 lines

+++ b/kernel/dma/direct.c

(quoted diff) +1 -1
@@ -456,14 +456,14 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 #else /* CONFIG_MMU */
 bool dma_direct_can_mmap(struct device *dev)
 {
-	return false;
+	return true;
 }
 
Quoted 11 lines

Review int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 466

Quoted 6 lines
(quoted diff) +1 -1
 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
 		unsigned long attrs)
 {
-	return -ENXIO;
+	return vm_iomap_memory(vma, vma->vm_start, (vma->vm_end - vma->vm_start));;

I think we should try to reuse the mmu dma_direct_mmap implementation, which does about the same. This version has been compile tested on arm-nommu only, let me know what you think: (btw, a nommu_defconfig of some kind for arm would be nice..)

Catch-all nommu_defconfig is not easy for ARM, AFAIK folk carry few hacks for randconfig...

Meanwhile, known working NOMMU configs

$ git grep "# CONFIG_MMU is not set" arch/arm/configs/
arch/arm/configs/efm32_defconfig:# CONFIG_MMU is not set
arch/arm/configs/lpc18xx_defconfig:# CONFIG_MMU is not set
arch/arm/configs/mps2_defconfig:# CONFIG_MMU is not set
arch/arm/configs/stm32_defconfig:# CONFIG_MMU is not set
arch/arm/configs/vf610m4_defconfig:# CONFIG_MMU is not set

kernel/dma/Kconfig config SWIOTLB · line 75

Quoted 11 lines
kernel/dma/Kconfig +1 -0
index d006668c0027d2..e0dae570a51530 100644
@@ -71,6 +71,7 @@ config SWIOTLB
 # in the pagetables
 #
 config DMA_NONCOHERENT_MMAP
+	default y if !MMU
 	bool

Nit: def_bool !MMU

Quoted 25 lines
kernel/dma/Kconfig +0 -0
 config DMA_REMAP
kernel/dma/direct.c +0 -7
index 0a4881e59aa7d6..9ec6a5c3fc578c 100644
@@ -459,7 +459,6 @@ int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
 	return ret;
 }
 
-#ifdef CONFIG_MMU
 bool dma_direct_can_mmap(struct device *dev)
 {
 	return dev_is_dma_coherent(dev) ||
@@ -485,19 +484,6 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 	return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
 			user_count << PAGE_SHIFT, vma->vm_page_prot);
 }
-#else /* CONFIG_MMU */
-bool dma_direct_can_mmap(struct device *dev)
-{
-	return false;
-}
-

kernel/dma/direct.c int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 489

Quoted 10 lines
kernel/dma/direct.c +0 -7
-int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
-		void *cpu_addr, dma_addr_t dma_addr, size_t size,
-		unsigned long attrs)
-{
-	return -ENXIO;
-}
-#endif /* CONFIG_MMU */
 
 int dma_direct_supported(struct device *dev, u64 mask)
 {

LGTM. FWIW:

#13 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 82 lines · Vladimir Murzin
Quoted 11 lines · Christoph Hellwig

Ok, I finally found the original patch from Vladimir. Comments below:

Quoted 9 lines

+++ b/kernel/dma/direct.c

(quoted diff) +1 -1
@@ -456,14 +456,14 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 #else /* CONFIG_MMU */
 bool dma_direct_can_mmap(struct device *dev)
 {
-	return false;
+	return true;
 }
 
Quoted 11 lines

Review int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 466

Quoted 6 lines
(quoted diff) +1 -1
 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
 		unsigned long attrs)
 {
-	return -ENXIO;
+	return vm_iomap_memory(vma, vma->vm_start, (vma->vm_end - vma->vm_start));;

I think we should try to reuse the mmu dma_direct_mmap implementation, which does about the same. This version has been compile tested on arm-nommu only, let me know what you think: (btw, a nommu_defconfig of some kind for arm would be nice..)

Catch-all nommu_defconfig is not easy for ARM, AFAIK folk carry few hacks for randconfig...

Meanwhile, known working NOMMU configs

$ git grep "# CONFIG_MMU is not set" arch/arm/configs/
arch/arm/configs/efm32_defconfig:# CONFIG_MMU is not set
arch/arm/configs/lpc18xx_defconfig:# CONFIG_MMU is not set
arch/arm/configs/mps2_defconfig:# CONFIG_MMU is not set
arch/arm/configs/stm32_defconfig:# CONFIG_MMU is not set
arch/arm/configs/vf610m4_defconfig:# CONFIG_MMU is not set

kernel/dma/Kconfig config SWIOTLB · line 75

Quoted 11 lines
kernel/dma/Kconfig +1 -0
index d006668c0027d2..e0dae570a51530 100644
@@ -71,6 +71,7 @@ config SWIOTLB
 # in the pagetables
 #
 config DMA_NONCOHERENT_MMAP
+	default y if !MMU
 	bool

Nit: def_bool !MMU

Quoted 32 lines
kernel/dma/Kconfig +0 -0
 config DMA_REMAP
kernel/dma/direct.c +0 -14
index 0a4881e59aa7d6..9ec6a5c3fc578c 100644
@@ -459,7 +459,6 @@ int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
 	return ret;
 }
 
-#ifdef CONFIG_MMU
 bool dma_direct_can_mmap(struct device *dev)
 {
 	return dev_is_dma_coherent(dev) ||
@@ -485,19 +484,6 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 	return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
 			user_count << PAGE_SHIFT, vma->vm_page_prot);
 }
-#else /* CONFIG_MMU */
-bool dma_direct_can_mmap(struct device *dev)
-{
-	return false;
-}
-
-int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
-		void *cpu_addr, dma_addr_t dma_addr, size_t size,
-		unsigned long attrs)
-{
-	return -ENXIO;
-}
-#endif /* CONFIG_MMU */
 
Quoted 9 lines

kernel/dma/direct.c int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 489

Quoted 3 lines
kernel/dma/direct.c +0 -0
 int dma_direct_supported(struct device *dev, u64 mask)
 {

LGTM. FWIW:

@dillon, can you give it a try?

I think Christoph would appreciate your Tested-by and that might speed up getting fix mainline.

Cheers
Vladimir

Quoted 5 lines

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author Tested-by
#14 lore raw

Re: [PATCH 2/2] arm-nommu: Add use_reserved_mem() to check if device support reserved memory

Quoted 90 lines · Vladimir Murzin
Quoted 88 lines · Vladimir Murzin
Quoted 11 lines · Christoph Hellwig

Ok, I finally found the original patch from Vladimir. Comments below:

Quoted 9 lines

+++ b/kernel/dma/direct.c

(quoted diff) +1 -1
@@ -456,14 +456,14 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
 #else /* CONFIG_MMU */
 bool dma_direct_can_mmap(struct device *dev)
 {
-   return false;
+   return true;
 }
 
Quoted 11 lines

Review int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 466

Quoted 6 lines
(quoted diff) +1 -1
 int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
            void *cpu_addr, dma_addr_t dma_addr, size_t size,
            unsigned long attrs)
 {
-   return -ENXIO;
+   return vm_iomap_memory(vma, vma->vm_start, (vma->vm_end - vma->vm_start));;

I think we should try to reuse the mmu dma_direct_mmap implementation, which does about the same. This version has been compile tested on arm-nommu only, let me know what you think: (btw, a nommu_defconfig of some kind for arm would be nice..)

Catch-all nommu_defconfig is not easy for ARM, AFAIK folk carry few hacks for randconfig...

Meanwhile, known working NOMMU configs

$ git grep "# CONFIG_MMU is not set" arch/arm/configs/
arch/arm/configs/efm32_defconfig:# CONFIG_MMU is not set
arch/arm/configs/lpc18xx_defconfig:# CONFIG_MMU is not set
arch/arm/configs/mps2_defconfig:# CONFIG_MMU is not set
arch/arm/configs/stm32_defconfig:# CONFIG_MMU is not set
arch/arm/configs/vf610m4_defconfig:# CONFIG_MMU is not set

kernel/dma/Kconfig config SWIOTLB · line 75

Quoted 11 lines
kernel/dma/Kconfig +1 -0
index d006668c0027d2..e0dae570a51530 100644
@@ -71,6 +71,7 @@ config SWIOTLB
 # in the pagetables
 #
 config DMA_NONCOHERENT_MMAP
+    default y if !MMU
     bool

Nit: def_bool !MMU

Quoted 25 lines
kernel/dma/Kconfig +0 -0
 config DMA_REMAP
kernel/dma/direct.c +0 -7
index 0a4881e59aa7d6..9ec6a5c3fc578c 100644
@@ -459,7 +459,6 @@ int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
     return ret;
 }
 
-#ifdef CONFIG_MMU
 bool dma_direct_can_mmap(struct device *dev)
 {
     return dev_is_dma_coherent(dev) ||
@@ -485,19 +484,6 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
     return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
                     user_count << PAGE_SHIFT, vma->vm_page_prot);
 }
-#else /* CONFIG_MMU */
-bool dma_direct_can_mmap(struct device *dev)
-{
-    return false;
-}
-

kernel/dma/direct.c int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma, · line 489

Quoted 10 lines
kernel/dma/direct.c +0 -7
-int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
-            void *cpu_addr, dma_addr_t dma_addr, size_t size,
-            unsigned long attrs)
-{
-    return -ENXIO;
-}
-#endif /* CONFIG_MMU */
 
 int dma_direct_supported(struct device *dev, u64 mask)
 {

LGTM. FWIW:

Quoted 9 lines
Quoted 3 lines

@dillon, can you give it a try?

I think Christoph would appreciate your Tested-by and that might speed up getting fix mainline.

sorry for the late response. Yes, it's working

Thanks Christoph

index 8f4bbdaf965e..3e0ecf0b5fb3 100644

kernel/dma/direct.c +0 -0
@@ -427,7 +427,6 @@ int dma_direct_get_sgtable(struct device *dev,

struct sg_table *sgt,

        return ret;

}

-#ifdef CONFIG_MMU
bool dma_direct_can_mmap(struct device *dev)

 {
        return dev_is_dma_coherent(dev) ||
@@ -453,19 +452,6 @@ int dma_direct_mmap(struct device *dev, struct
vm_area_struct *vma,
        return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
                        user_count << PAGE_SHIFT, vma->vm_page_prot);

}
-#else /* CONFIG_MMU */
-bool dma_direct_can_mmap(struct device *dev)
-{
- return false;
-}
-
-int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
- void *cpu_addr, dma_addr_t dma_addr, size_t size,
- unsigned long attrs)
-{
- return -ENXIO;
-}
-#endif /* CONFIG_MMU */

Quoted 10 lines

Cheers
Vladimir

Quoted 5 lines

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author
#15 lore raw
In reply to [email protected]

Hi Christoph Hellwig,

This is the patchset. for your reference.

thanks,

Quoted 26 lines · On Mon, Jun 8, 2020 at 4:30 PM <[email protected]> wrote:

From: dillon min <[email protected]>

when do mmap on /dev/fb0, we will get -6 error on cortex-m3/m4 or armv7m platform without cache support, this is caused by following reason:

on armv7m platform, if no cache support, we will use dma direct mapping, but, this is not support on !MMU hardware, just return '-ENXIO' error

so, add use_reserved_mem() for these armv7m hardware but no-cache support. eg, stm32f429/stm32f469.

verified on stm32f469-disco board, mmap frambuffer to userspace.

dillon min (2):
ARM: dts: stm32: Setup 4M bytes reserved memory for mmap
arm-nommu: Add use_reserved_mem() to check if device support reserved
memory

 arch/arm/boot/dts/stm32f469-disco.dts | 14 ++++++++++++++
 arch/arm/mm/dma-mapping-nommu.c       | 28 +++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 1 deletion(-)

--
2.7.4