Skip to contentSkip to conversation

ARM: Fix dma_alloc_coherent() and friends for NOMMU

RFC PATCH Superseded Vladimir Murzin 3 participants 17 messages Jan 10, 2017 – Jan 16, 2017

Subsystems (4)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c
Signature · 1 line
2.0.0
#1 lore raw

RFC PATCH v4 1/5 dma: Add simple dma_noop_mmap

This patch adds a simple implementation of mmap to dma_noop_ops.

Cc Christian Borntraeger <[email protected]>
---
 lib/dma-noop.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
lib/dma-noop.c +21 -0
index 3d766e7..96638a1 100644
@@ -64,6 +64,26 @@ static int dma_noop_supported(struct device *dev, u64 mask)
 	return 1;
 }
 
+static int dma_noop_mmap(struct device *dev, struct vm_area_struct *vma,
+			 void *cpu_addr, dma_addr_t dma_addr, size_t size,
+			 unsigned long attrs)
+{
+	unsigned long user_count = vma_pages(vma);
+	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
+	unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
+	unsigned long off = vma->vm_pgoff;
+	int ret = -ENXIO;
+
+	if (off < count && user_count <= (count - off)) {
+		ret = remap_pfn_range(vma, vma->vm_start,
+				      pfn + off,
+				      user_count << PAGE_SHIFT,
+				      vma->vm_page_prot);
+	}
+
+	return ret;
+}
+
 struct dma_map_ops dma_noop_ops = {
 	.alloc			= dma_noop_alloc,
 	.free			= dma_noop_free,
@@ -71,6 +91,7 @@ struct dma_map_ops dma_noop_ops = {
 	.map_sg			= dma_noop_map_sg,
 	.mapping_error		= dma_noop_mapping_error,
 	.dma_supported		= dma_noop_supported,
+	.mmap			= dma_noop_mmap,
 };
 
 EXPORT_SYMBOL(dma_noop_ops);
Signature · 1 line
2.0.0
#2 lore raw

RFC PATCH v4 2/5 drivers: dma-coherent: Introduce default DMA pool

In reply to Vladimir Murzin

This patch introduces default coherent DMA pool similar to default CMA area concept. To keep other users safe code kept under CONFIG_ARM.

---
 .../bindings/reserved-memory/reserved-memory.txt   |  3 ++
 drivers/base/dma-coherent.c                        | 59 +++++++++++++++++++---
 2 files changed, 55 insertions(+), 7 deletions(-)
Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +3 -0
index 3da0ebd..16291f2 100644
@@ -68,6 +68,9 @@ Linux implementation note:
 - If a "linux,cma-default" property is present, then Linux will use the
   region for the default pool of the contiguous memory allocator.
 
+- If a "linux,dma-default" property is present, then Linux will use the
+  region for the default pool of the consistent DMA allocator.
+
 Device node references to reserved memory
 -----------------------------------------
 Regions in the /reserved-memory node may be referenced by other device
drivers/base/dma-coherent.c +52 -7

117 lines not on this page · raw message

Signature · 1 line
2.0.0
#3 lore raw

RFC PATCH v4 3/5 ARM: NOMMU: Introduce dma operations for noMMU

In reply to Vladimir Murzin

R/M classes of cpus can have memory covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch factors out DMA support for NOMMU configuration into separate entity which provides dedicated dma_ops. We have to handle there several cases:
- configurations with MMU/MPU setup
- configurations without MMU/MPU setup
- special case for M-class, since caches and MPU there are optional

In general we rely on default DMA area for coherent allocations or/and per-device memory reserves suitable for coherent DMA, so if such regions are set coherent allocations go from there.

In case MPU/MPU was not setup we fallback to normal page allocator for DMA memory allocation.

In case we run M-class cpus, for configuration without cache support (like Cortex-M3/M4) dma operations are forced to be coherent and wired with dma-noop (such decision is made based on cacheid global variable); however, if caches are detected there and no DMA coherent region is given (either default or per-device), dma is disallowed even MPU is not set - it is because M-class implement system memory map which defines part of address space as Normal memory.

---
 arch/arm/include/asm/dma-mapping.h |   3 +-
 arch/arm/mm/Makefile               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c    | 252 +++++++++++++++++++++++++++++++++++++
 3 files changed, 256 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c
arch/arm/include/asm/dma-mapping.h +2 -1
index bf02dbd..559faad 100644
@@ -20,7 +20,8 @@ static inline struct dma_map_ops *__generic_dma_ops(struct device *dev)
 {
 	if (dev && dev->archdata.dma_ops)
 		return dev->archdata.dma_ops;
-	return &arm_dma_ops;
+
+	return IS_ENABLED(CONFIG_MMU) ? &arm_dma_ops : &dma_noop_ops;
 }
 
 static inline struct dma_map_ops *get_dma_ops(struct device *dev)
arch/arm/mm/Makefile +2 -3
index 2ac7988..5796357 100644
@@ -2,9 +2,8 @@
 # Makefile for the linux arm-specific parts of the memory manager.
 #
 
-obj-y				:= dma-mapping.o extable.o fault.o init.o \
-				   iomap.o
-
+obj-y				:= extable.o fault.o init.o iomap.o
+obj-y				+= dma-mapping$(MMUEXT).o
 obj-$(CONFIG_MMU)		+= fault-armv.o flush.o idmap.o ioremap.o \
 				   mmap.o pgd.o mmu.o pageattr.o
 
arch/arm/mm/dma-mapping-nommu.c new file +252 -0

252 lines not on this page · raw message

Signature · 1 line
2.0.0
#4 lore raw

RFC PATCH v4 4/5 ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus

In reply to Vladimir Murzin

Now, we have dedicated non-cacheable region for consistent DMA operations. However, that region can still be marked as bufferable by MPU, so it'd be safer to have barriers by default.

---
 arch/arm/mm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/mm/Kconfig +1 -1
index 0b79f12..64a1465c 100644
@@ -1029,7 +1029,7 @@ config ARM_L1_CACHE_SHIFT
 
 config ARM_DMA_MEM_BUFFERABLE
 	bool "Use non-cacheable memory for DMA" if (CPU_V6 || CPU_V6K) && !CPU_V7
-	default y if CPU_V6 || CPU_V6K || CPU_V7
+	default y if CPU_V6 || CPU_V6K || CPU_V7 || CPU_V7M
 	help
 	  Historically, the kernel has used strongly ordered mappings to
 	  provide DMA coherent memory.  With the advent of ARMv7, mapping
Signature · 1 line
2.0.0
#5 lore raw

RFC PATCH v4 5/5 ARM: dma-mapping: Remove traces of NOMMU code

In reply to Vladimir Murzin

DMA operations for NOMMU case have been just factored out into separate compilation unit, so don't keep dead code.

---
 arch/arm/mm/dma-mapping.c | 26 ++------------------------
 1 file changed, 2 insertions(+), 24 deletions(-)
arch/arm/mm/dma-mapping.c +2 -24
index ab77100..d8a755b 100644
@@ -344,8 +344,6 @@ static void __dma_free_buffer(struct page *page, size_t size)
 	}
 }
 
-#ifdef CONFIG_MMU
-
 static void *__alloc_from_contiguous(struct device *dev, size_t size,
 				     pgprot_t prot, struct page **ret_page,
 				     const void *caller, bool want_vaddr,
@@ -646,22 +644,6 @@ static inline pgprot_t __get_dma_pgprot(unsigned long attrs, pgprot_t prot)
 	return prot;
 }
 
-#define nommu() 0
-
-#else	/* !CONFIG_MMU */
-
-#define nommu() 1
-
-#define __get_dma_pgprot(attrs, prot)				__pgprot(0)
-#define __alloc_remap_buffer(dev, size, gfp, prot, ret, c, wv)	NULL
-#define __alloc_from_pool(size, ret_page)			NULL
-#define __alloc_from_contiguous(dev, size, prot, ret, c, wv, coherent_flag)	NULL
-#define __free_from_pool(cpu_addr, size)			do { } while (0)
-#define __free_from_contiguous(dev, page, cpu_addr, size, wv)	do { } while (0)
-#define __dma_free_remap(cpu_addr, size)			do { } while (0)
-
-#endif	/* CONFIG_MMU */
-
 static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp,
 				   struct page **ret_page)
 {
@@ -803,7 +785,7 @@ static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
 
 	if (cma)
 		buf->allocator = &cma_allocator;
-	else if (nommu() || is_coherent)
+	else if (is_coherent)
 		buf->allocator = &simple_allocator;
 	else if (allowblock)
 		buf->allocator = &remap_allocator;
@@ -852,8 +834,7 @@ static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
 		 void *cpu_addr, dma_addr_t dma_addr, size_t size,
 		 unsigned long attrs)
 {
-	int ret = -ENXIO;
-#ifdef CONFIG_MMU
+	int ret;
 	unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
 	unsigned long pfn = dma_to_pfn(dev, dma_addr);
@@ -868,7 +849,6 @@ static int __arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
 				      vma->vm_end - vma->vm_start,
 				      vma->vm_page_prot);
 	}
-#endif	/* CONFIG_MMU */
 
 	return ret;
 }
@@ -887,9 +867,7 @@ int arm_dma_mmap(struct device *dev, struct vm_area_struct *vma,
 		 void *cpu_addr, dma_addr_t dma_addr, size_t size,
 		 unsigned long attrs)
 {
-#ifdef CONFIG_MMU
 	vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot);
-#endif	/* CONFIG_MMU */
 	return __arm_dma_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
 }
 
Signature · 1 line
2.0.0
#6 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

In reply to Vladimir Murzin

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 27 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

Quoted 8 lines

To make life easier NOMMU dma operations are kept in separate compilation unit.

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.
I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Since my hardware doesn't have cache or MPU (and so use dma-noop) I haven't reserved specific memory region.
Buffer addresses and vma parameters look correct... What could I have miss here ?

Benjamin

Quoted 24 lines

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c

--
2.0.0

Signature · 7 lines
Benjamin Gaignard

Graphic Study Group

Linaro.org ? Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog
#7 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 31 lines · Benjamin Gaignard

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

Quoted 5 lines

Since my hardware doesn't have cache or MPU (and so use dma-noop) I haven't reserved specific memory region.
Buffer addresses and vma parameters look correct... What could I have miss here ?

No ideas, sorry...

Cheers
Vladimir

Quoted 30 lines

Benjamin

Quoted 24 lines

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c

--
2.0.0

#8 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

Benjamin

Quoted 43 lines
Quoted 6 lines

Since my hardware doesn't have cache or MPU (and so use dma-noop) I haven't reserved specific memory region.
Buffer addresses and vma parameters look correct... What could I have miss here ?

No ideas, sorry...

Cheers
Vladimir

Quoted 30 lines

Benjamin

Quoted 24 lines

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c

--
2.0.0

Signature · 7 lines
Benjamin Gaignard

Graphic Study Group

Linaro.org ? Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog
#9 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 73 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

Quoted 7 lines

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Quoted 57 lines

Benjamin

Quoted 7 lines
Quoted 6 lines

Since my hardware doesn't have cache or MPU (and so use dma-noop) I haven't reserved specific memory region.
Buffer addresses and vma parameters look correct... What could I have miss here ?

Quoted 36 lines

No ideas, sorry...

Cheers
Vladimir

Quoted 30 lines

Benjamin

Quoted 24 lines

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c

--
2.0.0

--
Benjamin Gaignard

Graphic Study Group

Linaro.org ? Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog

Signature · 7 lines
Benjamin Gaignard

Graphic Study Group

Linaro.org ? Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog
#10 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 78 lines · Benjamin Gaignard

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 77 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

Quoted 7 lines
Quoted 3 lines

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Thanks for narrowing it down! I did not noticed that stm32f4 remap its memory, so dma-ranges property is in use.

It looks like virt_to_dma is ARM specific, so I probably have to discard idea of reusing dma-noop-ops and switch logic into dma-mapping-nommu.c based on is_device_dma_coherent(dev) check.

Meanwhile, I'm quite puzzled on how such memory remaping should work together with reserved memory. It seem it doesn't account dma-ranges while reserving memory (it is too early) nor while allocating/mapping/etc.

Cheers
Vladimir

Quoted 60 lines
Quoted 57 lines

Benjamin

Quoted 7 lines
Quoted 6 lines

Since my hardware doesn't have cache or MPU (and so use dma-noop) I haven't reserved specific memory region.
Buffer addresses and vma parameters look correct... What could I have miss here ?

Quoted 36 lines

No ideas, sorry...

Cheers
Vladimir

Quoted 30 lines

Benjamin

Quoted 24 lines

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c

--
2.0.0

--
Benjamin Gaignard

Graphic Study Group

Linaro.org ? Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog

#11 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 87 lines · Vladimir Murzin
Quoted 78 lines · Benjamin Gaignard

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 77 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

Quoted 7 lines
Quoted 3 lines

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Quoted 6 lines

Thanks for narrowing it down! I did not noticed that stm32f4 remap its memory, so dma-ranges property is in use.

It looks like virt_to_dma is ARM specific, so I probably have to discard idea of reusing dma-noop-ops and switch logic into dma-mapping-nommu.c based on is_device_dma_coherent(dev) check.

dma_pfn_offset is a member of struct device, so it should be OK for dma_noop_ops to also make reference to it (and assume it's zero if not explicitly set).

Quoted 3 lines

Meanwhile, I'm quite puzzled on how such memory remaping should work together with reserved memory. It seem it doesn't account dma-ranges while reserving memory (it is too early) nor while allocating/mapping/etc.

The reserved memory is described in terms of CPU physical addresses, so a device offset shouldn't matter from that perspective. It only comes into play at the point you generate the dma_addr_t to hand off to the device - only then do you need to transform the CPU physical address of the allocated/mapped page into the device's view of that page (i.e. subtract the offset).

Robin.

Quoted 65 lines

Cheers
Vladimir

Quoted 60 lines
Quoted 57 lines

Benjamin

Quoted 7 lines
Quoted 6 lines

Since my hardware doesn't have cache or MPU (and so use dma-noop) I haven't reserved specific memory region.
Buffer addresses and vma parameters look correct... What could I have miss here ?

Quoted 36 lines

No ideas, sorry...

Cheers
Vladimir

Quoted 30 lines

Benjamin

Quoted 24 lines

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c

--
2.0.0

--
Benjamin Gaignard

Graphic Study Group

Linaro.org ? Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog

#12 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 103 lines · Robin Murphy
Quoted 87 lines · Vladimir Murzin
Quoted 78 lines · Benjamin Gaignard

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 77 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

Quoted 7 lines
Quoted 3 lines

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Quoted 6 lines

Thanks for narrowing it down! I did not noticed that stm32f4 remap its memory, so dma-ranges property is in use.

It looks like virt_to_dma is ARM specific, so I probably have to discard idea of reusing dma-noop-ops and switch logic into dma-mapping-nommu.c based on is_device_dma_coherent(dev) check.

dma_pfn_offset is a member of struct device, so it should be OK for dma_noop_ops to also make reference to it (and assume it's zero if not explicitly set).

Quoted 3 lines

Meanwhile, I'm quite puzzled on how such memory remaping should work together with reserved memory. It seem it doesn't account dma-ranges while reserving memory (it is too early) nor while allocating/mapping/etc.

Quoted 6 lines

The reserved memory is described in terms of CPU physical addresses, so a device offset shouldn't matter from that perspective. It only comes into play at the point you generate the dma_addr_t to hand off to the device - only then do you need to transform the CPU physical address of the allocated/mapped page into the device's view of that page (i.e. subtract the offset).

Thanks for explanation! So dma-coherent.c should be modified, right? I see that some architectures provide phys_to_dma/dma_to_phys helpers primary for swiotlb, is it safe to reuse them given that default implementation is provided? Nothing under Documentation explains how they supposed to be used, sorry if asking stupid question.

Cheers
Vladimir

Quoted 70 lines

Robin.

Quoted 65 lines

Cheers
Vladimir

Quoted 60 lines
Quoted 57 lines

Benjamin

Quoted 7 lines
Quoted 6 lines

Since my hardware doesn't have cache or MPU (and so use dma-noop) I haven't reserved specific memory region.
Buffer addresses and vma parameters look correct... What could I have miss here ?

Quoted 36 lines

No ideas, sorry...

Cheers
Vladimir

Quoted 30 lines

Benjamin

Quoted 24 lines

[1] http://www.armlinux.org.uk/developer/patches/viewpatch.php?id=8633/1

Vladimir Murzin (5):
dma: Add simple dma_noop_mmap
drivers: dma-coherent: Introduce default DMA pool
ARM: NOMMU: Introduce dma operations for noMMU
ARM: NOMMU: Set ARM_DMA_MEM_BUFFERABLE for M-class cpus
ARM: dma-mapping: Remove traces of NOMMU code

 .../bindings/reserved-memory/reserved-memory.txt   |   3 +
 arch/arm/include/asm/dma-mapping.h                 |   3 +-
 arch/arm/mm/Kconfig                                |   2 +-
 arch/arm/mm/Makefile                               |   5 +-
 arch/arm/mm/dma-mapping-nommu.c                    | 252 +++++++++++++++++++++
 arch/arm/mm/dma-mapping.c                          |  26 +--
 drivers/base/dma-coherent.c                        |  59 ++++-
 lib/dma-noop.c                                     |  21 ++
 8 files changed, 335 insertions(+), 36 deletions(-)
 create mode 100644 arch/arm/mm/dma-mapping-nommu.c

--
2.0.0

--
Benjamin Gaignard

Graphic Study Group

Linaro.org ? Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog

#13 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 111 lines · Vladimir Murzin
Quoted 103 lines · Robin Murphy
Quoted 87 lines · Vladimir Murzin
Quoted 78 lines · Benjamin Gaignard

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 77 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

Quoted 7 lines
Quoted 3 lines

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Quoted 6 lines

Thanks for narrowing it down! I did not noticed that stm32f4 remap its memory, so dma-ranges property is in use.

It looks like virt_to_dma is ARM specific, so I probably have to discard idea of reusing dma-noop-ops and switch logic into dma-mapping-nommu.c based on is_device_dma_coherent(dev) check.

dma_pfn_offset is a member of struct device, so it should be OK for dma_noop_ops to also make reference to it (and assume it's zero if not explicitly set).

Quoted 3 lines

Meanwhile, I'm quite puzzled on how such memory remaping should work together with reserved memory. It seem it doesn't account dma-ranges while reserving memory (it is too early) nor while allocating/mapping/etc.

Quoted 6 lines

The reserved memory is described in terms of CPU physical addresses, so a device offset shouldn't matter from that perspective. It only comes into play at the point you generate the dma_addr_t to hand off to the device - only then do you need to transform the CPU physical address of the allocated/mapped page into the device's view of that page (i.e. subtract the offset).

Quoted 5 lines

Thanks for explanation! So dma-coherent.c should be modified, right? I see that some architectures provide phys_to_dma/dma_to_phys helpers primary for swiotlb, is it safe to reuse them given that default implementation is provided? Nothing under Documentation explains how they supposed to be used, sorry if asking stupid question.

Those are essentially SWIOTLB-specific, so can't be universally relied upon. I think something like this ought to suffice:

---8<---

lib/dma-noop.c +5 -0
index 3d766e78fbe2..fbb1b37750d5 100644
@@ -8,6 +8,11 @@
 #include <linux/dma-mapping.h>
 #include <linux/scatterlist.h>
 
+static dma_addr_t dma_noop_dev_offset(struct device *dev)
+{
+       return (dma_addr_t)dev->dma_pfn_offset << PAGE_SHIFT;
+}
+
 static void *dma_noop_alloc(struct device *dev, size_t size,
                            dma_addr_t *dma_handle, gfp_t gfp,
                            unsigned long attrs)
@@ -16,7 +21,7 @@ static void *dma_noop_alloc(struct device *dev, size_t

size,

        ret = (void *)__get_free_pages(gfp, get_order(size));
        if (ret)
-               *dma_handle = virt_to_phys(ret);
+               *dma_handle = virt_to_phys(ret) - dma_noop_dev_offset(dev);
        return ret;

}

@@ -32,7 +37,7 @@ static dma_addr_t dma_noop_map_page(struct device *dev, struct page *page,
enum dma_data_direction dir, unsigned long attrs)
{
- return page_to_phys(page) + offset;
+ return page_to_phys(page) + offset - dma_noop_dev_offset(dev);
}

static int dma_noop_map_sg(struct device *dev, struct scatterlist *sgl,
int nents,
@@ -47,7 +52,8 @@ static int dma_noop_map_sg(struct device *dev, struct scatterlist *sgl, int nent

                BUG_ON(!sg_page(sg));
                va = sg_virt(sg);
-               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va);
+               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va) -
+                                       dma_noop_dev_offset(dev);
                sg_dma_len(sg) = sg->length;
        }

--->8---

intentionally whitespace-damaged by copy-pasting off my terminal to emphasise how utterly untested it is ;)

Robin.

#14 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 112 lines · Robin Murphy
Quoted 111 lines · Vladimir Murzin
Quoted 109 lines · Robin Murphy
Quoted 87 lines · Vladimir Murzin
Quoted 78 lines · Benjamin Gaignard

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 77 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

Quoted 7 lines
Quoted 3 lines

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Quoted 6 lines

Thanks for narrowing it down! I did not noticed that stm32f4 remap its memory, so dma-ranges property is in use.

It looks like virt_to_dma is ARM specific, so I probably have to discard idea of reusing dma-noop-ops and switch logic into dma-mapping-nommu.c based on is_device_dma_coherent(dev) check.

dma_pfn_offset is a member of struct device, so it should be OK for dma_noop_ops to also make reference to it (and assume it's zero if not explicitly set).

Quoted 3 lines

Meanwhile, I'm quite puzzled on how such memory remaping should work together with reserved memory. It seem it doesn't account dma-ranges while reserving memory (it is too early) nor while allocating/mapping/etc.

The reserved memory is described in terms of CPU physical addresses, so a device offset shouldn't matter from that perspective. It only comes into play at the point you generate the dma_addr_t to hand off to the device - only then do you need to transform the CPU physical address of the allocated/mapped page into the device's view of that page (i.e. subtract the offset).

Quoted 8 lines
Quoted 5 lines

Thanks for explanation! So dma-coherent.c should be modified, right? I see that some architectures provide phys_to_dma/dma_to_phys helpers primary for swiotlb, is it safe to reuse them given that default implementation is provided? Nothing under Documentation explains how they supposed to be used, sorry if asking stupid question.

Those are essentially SWIOTLB-specific, so can't be universally relied upon. I think something like this ought to suffice:

Yup, but what about dma-coherent.c? Currently it has

int dma_alloc_from_coherent(struct device *dev, ssize_t size,
dma_addr_t *dma_handle, void **ret)
{
...

	*dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
	*ret = mem->virt_base + (pageno << PAGE_SHIFT);

...
}

In case reserved memory is described in terms of CPU phys addresses, would not we need to take into account dma_pfn_offset? What I'm missing?

Thanks
Vladimir

Quoted 56 lines

---8<---

lib/dma-noop.c +7 -2
index 3d766e78fbe2..fbb1b37750d5 100644
@@ -8,6 +8,11 @@
 #include <linux/dma-mapping.h>
 #include <linux/scatterlist.h>
 
+static dma_addr_t dma_noop_dev_offset(struct device *dev)
+{
+       return (dma_addr_t)dev->dma_pfn_offset << PAGE_SHIFT;
+}
+
 static void *dma_noop_alloc(struct device *dev, size_t size,
                            dma_addr_t *dma_handle, gfp_t gfp,
                            unsigned long attrs)
@@ -16,7 +21,7 @@ static void *dma_noop_alloc(struct device *dev, size_t
 size,
 
        ret = (void *)__get_free_pages(gfp, get_order(size));
        if (ret)
-               *dma_handle = virt_to_phys(ret);
+               *dma_handle = virt_to_phys(ret) - dma_noop_dev_offset(dev);
        return ret;
 }
@@ -32,7 +37,7 @@ static dma_addr_t dma_noop_map_page(struct device
 *dev, struct page *page,
                                      enum dma_data_direction dir,
                                      unsigned long attrs)
 {
-       return page_to_phys(page) + offset;
+       return page_to_phys(page) + offset - dma_noop_dev_offset(dev);
 }
 

static int dma_noop_map_sg(struct device *dev, struct scatterlist *sgl,
int nents,

lib/dma-noop.c +0 -0
@@ -47,7 +52,8 @@ static int dma_noop_map_sg(struct device *dev, struct

scatterlist *sgl, int nent

                BUG_ON(!sg_page(sg));
                va = sg_virt(sg);
lib/dma-noop.c +2 -2
-               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va);
+               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va) -
+                                       dma_noop_dev_offset(dev);
                sg_dma_len(sg) = sg->length;
        }
--->8---

intentionally whitespace-damaged by copy-pasting off my terminal to emphasise how utterly untested it is ;)

Robin.

#15 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 128 lines · Vladimir Murzin
Quoted 112 lines · Robin Murphy
Quoted 111 lines · Vladimir Murzin
Quoted 109 lines · Robin Murphy
Quoted 87 lines · Vladimir Murzin
Quoted 78 lines · Benjamin Gaignard

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 77 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

Quoted 7 lines
Quoted 3 lines

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Quoted 6 lines

Thanks for narrowing it down! I did not noticed that stm32f4 remap its memory, so dma-ranges property is in use.

It looks like virt_to_dma is ARM specific, so I probably have to discard idea of reusing dma-noop-ops and switch logic into dma-mapping-nommu.c based on is_device_dma_coherent(dev) check.

dma_pfn_offset is a member of struct device, so it should be OK for dma_noop_ops to also make reference to it (and assume it's zero if not explicitly set).

Quoted 3 lines

Meanwhile, I'm quite puzzled on how such memory remaping should work together with reserved memory. It seem it doesn't account dma-ranges while reserving memory (it is too early) nor while allocating/mapping/etc.

The reserved memory is described in terms of CPU physical addresses, so a device offset shouldn't matter from that perspective. It only comes into play at the point you generate the dma_addr_t to hand off to the device - only then do you need to transform the CPU physical address of the allocated/mapped page into the device's view of that page (i.e. subtract the offset).

Quoted 8 lines
Quoted 5 lines

Thanks for explanation! So dma-coherent.c should be modified, right? I see that some architectures provide phys_to_dma/dma_to_phys helpers primary for swiotlb, is it safe to reuse them given that default implementation is provided? Nothing under Documentation explains how they supposed to be used, sorry if asking stupid question.

Those are essentially SWIOTLB-specific, so can't be universally relied upon. I think something like this ought to suffice:

Yup, but what about dma-coherent.c? Currently it has

int dma_alloc_from_coherent(struct device *dev, ssize_t size,
dma_addr_t *dma_handle, void **ret)
{
...

Quoted 7 lines
	*dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
	*ret = mem->virt_base + (pageno << PAGE_SHIFT);

...
}

In case reserved memory is described in terms of CPU phys addresses, would not we need to take into account dma_pfn_offset? What I'm missing?

Ah yes, I overlooked that one. AFAICS, that's intended to be accounted for when calling dma_init_coherent_memory (i.e. phys_addr vs. device_addr), but that's a bit awkward for a global pool.

How utterly disgusting do you think this (or some variant thereof) looks?

	/* Apply device-specific offset for the global pool */
	if (mem == dma_coherent_default_memory)
		*handle += dev->dma_pfn_offset << PAGE_SHIFT;

Robin.

Quoted 60 lines

Thanks
Vladimir

Quoted 56 lines

---8<---

lib/dma-noop.c +7 -2
index 3d766e78fbe2..fbb1b37750d5 100644
@@ -8,6 +8,11 @@
 #include <linux/dma-mapping.h>
 #include <linux/scatterlist.h>
 
+static dma_addr_t dma_noop_dev_offset(struct device *dev)
+{
+       return (dma_addr_t)dev->dma_pfn_offset << PAGE_SHIFT;
+}
+
 static void *dma_noop_alloc(struct device *dev, size_t size,
                            dma_addr_t *dma_handle, gfp_t gfp,
                            unsigned long attrs)
@@ -16,7 +21,7 @@ static void *dma_noop_alloc(struct device *dev, size_t
 size,
 
        ret = (void *)__get_free_pages(gfp, get_order(size));
        if (ret)
-               *dma_handle = virt_to_phys(ret);
+               *dma_handle = virt_to_phys(ret) - dma_noop_dev_offset(dev);
        return ret;
 }
@@ -32,7 +37,7 @@ static dma_addr_t dma_noop_map_page(struct device
 *dev, struct page *page,
                                      enum dma_data_direction dir,
                                      unsigned long attrs)
 {
-       return page_to_phys(page) + offset;
+       return page_to_phys(page) + offset - dma_noop_dev_offset(dev);
 }
 

static int dma_noop_map_sg(struct device *dev, struct scatterlist *sgl,
int nents,

lib/dma-noop.c +0 -0
@@ -47,7 +52,8 @@ static int dma_noop_map_sg(struct device *dev, struct

scatterlist *sgl, int nent

                BUG_ON(!sg_page(sg));
                va = sg_virt(sg);
lib/dma-noop.c +2 -2
-               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va);
+               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va) -
+                                       dma_noop_dev_offset(dev);
                sg_dma_len(sg) = sg->length;
        }
--->8---

intentionally whitespace-damaged by copy-pasting off my terminal to emphasise how utterly untested it is ;)

Robin.

#16 lore raw

v4 cover ARM: Fix dma_alloc_coherent() and friends for NOMMU

Quoted 141 lines · Robin Murphy
Quoted 128 lines · Vladimir Murzin
Quoted 112 lines · Robin Murphy
Quoted 111 lines · Vladimir Murzin
Quoted 109 lines · Robin Murphy
Quoted 87 lines · Vladimir Murzin
Quoted 78 lines · Benjamin Gaignard

2017-01-12 11:35 GMT+01:00 Benjamin Gaignard <[email protected]>:

Quoted 77 lines

2017-01-11 15:34 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 67 lines · Benjamin Gaignard
Quoted 31 lines

2017-01-10 15:18 GMT+01:00 Vladimir Murzin <[email protected]>:

Quoted 30 lines

Hi,

It seem that addition of cache support for M-class cpus uncovered latent bug in DMA usage. NOMMU memory model has been treated as being always consistent; however, for R/M classes of cpu memory can be covered by MPU which in turn might configure RAM as Normal i.e. bufferable and cacheable. It breaks dma_alloc_coherent() and friends, since data can stuck in caches now or be buffered.

This patch set is trying to address the issue by providing region of memory suitable for consistent DMA operations. It is supposed that such region is marked by MPU as non-cacheable. Robin suggested to advertise such memory as reserved shared-dma-pool, rather then using homebrew command line option, and extend dma-coherent to provide default DMA area in the similar way as it is done for CMA (PATCH 2/5). It allows us to offload all bookkeeping on generic coherent DMA framework, and it is seems that it might be reused by other architectures like c6x and blackfin.

Dedicated DMA region is required for cases other than:
- MMU/MPU is off
- cpu is v7m w/o cache support
- device is coherent

In case one of the above conditions is true dma operations are forced to be coherent and wired with dma_noop_ops.

To make life easier NOMMU dma operations are kept in separate compilation unit.

Quoted 8 lines
Quoted 5 lines

Since the issue was reported in the same time as Benjamin sent his patch [1] to allow mmap for NOMMU, his case is also addressed in this series (PATCH 1/5 and PATCH 3/5).

Thanks!

I have tested this v4 on my setup (stm32f4, no cache, no MPU) and unfortunately it doesn't work with my drm/kms driver.

I guess the same is for fbmem, but would be better to have confirmation since amba-clcd I use has not been ported to drm/kms (yet), so I can't test.

Quoted 6 lines

I haven't any errors but nothing is displayed unlike what I have when using current dma-mapping
code.
I guess the issue is coming from dma-noop where __get_free_pages() is used instead of alloc_pages()
in dma-mapping.

Unless I've missed something bellow is a call stack for both

#1
__alloc_simple_buffer
__dma_alloc_buffer
alloc_pages
split_page
__dma_clear_buffer
memset
page_address

#2
__get_free_pages
alloc_pages
page_address

Quoted 4 lines

So the difference is that nommu case in dma-mapping.c memzeros memory, handles DMA_ATTR_NO_KERNEL_MAPPING and does optimisation of memory usage.

Is something from above critical for your driver?

I have removed all the diff (split_page, __dma_clear_buffer, memset) from #1 and it is still working.
DMA_ATTR_NO_KERNEL_MAPPING flag is not set when allocating the buffer.

Quoted 7 lines
Quoted 3 lines

I have investigated more and found that dma-noop doesn't take care of "dma-ranges" property which is set in DT.
I believed that is the root cause of my problem with your patches.

After testing changing virt_to_phys to virt_to_dma in dma-noop.c fix the issue modetest and fbdemo are now still functional.

Quoted 6 lines

Thanks for narrowing it down! I did not noticed that stm32f4 remap its memory, so dma-ranges property is in use.

It looks like virt_to_dma is ARM specific, so I probably have to discard idea of reusing dma-noop-ops and switch logic into dma-mapping-nommu.c based on is_device_dma_coherent(dev) check.

dma_pfn_offset is a member of struct device, so it should be OK for dma_noop_ops to also make reference to it (and assume it's zero if not explicitly set).

Quoted 3 lines

Meanwhile, I'm quite puzzled on how such memory remaping should work together with reserved memory. It seem it doesn't account dma-ranges while reserving memory (it is too early) nor while allocating/mapping/etc.

The reserved memory is described in terms of CPU physical addresses, so a device offset shouldn't matter from that perspective. It only comes into play at the point you generate the dma_addr_t to hand off to the device - only then do you need to transform the CPU physical address of the allocated/mapped page into the device's view of that page (i.e. subtract the offset).

Quoted 8 lines
Quoted 5 lines

Thanks for explanation! So dma-coherent.c should be modified, right? I see that some architectures provide phys_to_dma/dma_to_phys helpers primary for swiotlb, is it safe to reuse them given that default implementation is provided? Nothing under Documentation explains how they supposed to be used, sorry if asking stupid question.

Those are essentially SWIOTLB-specific, so can't be universally relied upon. I think something like this ought to suffice:

Yup, but what about dma-coherent.c? Currently it has

int dma_alloc_from_coherent(struct device *dev, ssize_t size,
dma_addr_t *dma_handle, void **ret)
{
...

Quoted 7 lines
	*dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
	*ret = mem->virt_base + (pageno << PAGE_SHIFT);

...
}

In case reserved memory is described in terms of CPU phys addresses, would not we need to take into account dma_pfn_offset? What I'm missing?

Ah yes, I overlooked that one. AFAICS, that's intended to be accounted for when calling dma_init_coherent_memory (i.e. phys_addr vs. device_addr), but that's a bit awkward for a global pool.

Quoted 5 lines

How utterly disgusting do you think this (or some variant thereof) looks?

	/* Apply device-specific offset for the global pool */
	if (mem == dma_coherent_default_memory)
		*handle += dev->dma_pfn_offset << PAGE_SHIFT;

It'd work for default dma region, but IMO the issue is wider here... does following look better?

drivers/base/dma-coherent.c +18 -2
index b52ba27..22daa4c 100644
@@ -27,6 +27,15 @@ static inline struct dma_coherent_mem *dev_get_coherent_memory(struct device *de
 	return dma_coherent_default_memory;
 }
 
+static inline dma_addr_t dma_get_device_base(struct device *dev,
+					     struct dma_coherent_mem * mem)
+{
+	if (!dev)
+		return mem->pfn_base << PAGE_SHIFT;
+
+	return (mem->pfn_base + dev->dma_pfn_offset) << PAGE_SHIFT;
+}
+
 static bool dma_init_coherent_memory(
 	phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags,
 	struct dma_coherent_mem **mem)
@@ -92,13 +101,19 @@ static void dma_release_coherent_memory(struct dma_coherent_mem *mem)
 static int dma_assign_coherent_memory(struct device *dev,
 				      struct dma_coherent_mem *mem)
 {
+	unsigned long dma_pfn_offset = mem->pfn_base - PFN_DOWN(mem->device_base);
+
 	if (!dev)
 		return -ENODEV;
 
 	if (dev->dma_mem)
 		return -EBUSY;
 
+	if (dev->dma_pfn_offset)
+		WARN_ON(dev->dma_pfn_offset != dma_pfn_offset);
+
 	dev->dma_mem = mem;
+	dev->dma_pfn_offset = dma_pfn_offset;
 	/* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
 
 	return 0;
@@ -145,7 +160,7 @@ void *dma_mark_declared_memory_occupied(struct device *dev,
 		return ERR_PTR(-EINVAL);
 
 	spin_lock_irqsave(&mem->spinlock, flags);
-	pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
+	pos = PFN_DOWN(device_addr - dma_get_device_base(dev, mem));
 	err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
 	spin_unlock_irqrestore(&mem->spinlock, flags);
 
@@ -195,8 +210,9 @@ int dma_alloc_from_coherent(struct device *dev, ssize_t size,
 	/*
 	 * Memory was found in the per-device area.
 	 */
-	*dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
+	*dma_handle = dma_get_device_base(dev, mem) + (pageno << PAGE_SHIFT);
 	*ret = mem->virt_base + (pageno << PAGE_SHIFT);
+
 	dma_memory_map = (mem->flags & DMA_MEMORY_MAP);
 	spin_unlock_irqrestore(&mem->spinlock, flags);
 	if (dma_memory_map)

Cheers
Vladimir

Quoted 65 lines

Robin.

Quoted 60 lines

Thanks
Vladimir

Quoted 56 lines

---8<---

lib/dma-noop.c +7 -2
index 3d766e78fbe2..fbb1b37750d5 100644
@@ -8,6 +8,11 @@
 #include <linux/dma-mapping.h>
 #include <linux/scatterlist.h>
 
+static dma_addr_t dma_noop_dev_offset(struct device *dev)
+{
+       return (dma_addr_t)dev->dma_pfn_offset << PAGE_SHIFT;
+}
+
 static void *dma_noop_alloc(struct device *dev, size_t size,
                            dma_addr_t *dma_handle, gfp_t gfp,
                            unsigned long attrs)
@@ -16,7 +21,7 @@ static void *dma_noop_alloc(struct device *dev, size_t
 size,
 
        ret = (void *)__get_free_pages(gfp, get_order(size));
        if (ret)
-               *dma_handle = virt_to_phys(ret);
+               *dma_handle = virt_to_phys(ret) - dma_noop_dev_offset(dev);
        return ret;
 }
@@ -32,7 +37,7 @@ static dma_addr_t dma_noop_map_page(struct device
 *dev, struct page *page,
                                      enum dma_data_direction dir,
                                      unsigned long attrs)
 {
-       return page_to_phys(page) + offset;
+       return page_to_phys(page) + offset - dma_noop_dev_offset(dev);
 }
 

static int dma_noop_map_sg(struct device *dev, struct scatterlist *sgl,
int nents,

lib/dma-noop.c +0 -0
@@ -47,7 +52,8 @@ static int dma_noop_map_sg(struct device *dev, struct

scatterlist *sgl, int nent

                BUG_ON(!sg_page(sg));
                va = sg_virt(sg);
lib/dma-noop.c +2 -2
-               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va);
+               sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va) -
+                                       dma_noop_dev_offset(dev);
                sg_dma_len(sg) = sg->length;
        }
--->8---

intentionally whitespace-damaged by copy-pasting off my terminal to emphasise how utterly untested it is ;)

Robin.