ARM/dma-mapping: invalidate caches on arch_dma_prep_coherent
Subsystems (2)
PATCH 1/1 ARM/dma-mapping: invalidate caches on arch_dma_prep_coherent
since switching to dma-direct, memory using DMA_ATTR_NO_KERNEL_MAPPING is no longer allocated using the arch specific handlers and instead will use dma_direct_alloc_no_mapping. While the arm specific allocation handlers implicitly clear the allocated dma buffers and will flush any caches dma-direct relies on ARCH_HAS_DMA_PREP_COHERENT to flush the caches.
Without this flush video frame corruption can occur in drivers
like the coda v4l2 driver which explicitly sets the DMA_ATTR_NO_KERNEL_MAPPING flag.
--- arch/arm/Kconfig | 1 + arch/arm/mm/dma-mapping.c | 8 ++++++++ 2 files changed, 9 insertions(+)
arch/arm/Kconfig +1 -0
@@ -14,6 +14,7 @@ config ARM select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DMA_ALLOC if MMU select ARCH_HAS_DMA_OPS + select ARCH_HAS_DMA_PREP_COHERENT select ARCH_HAS_DMA_WRITE_COMBINE if !ARM_DMA_MEM_BUFFERABLE select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FORTIFY_SOURCE
arch/arm/mm/dma-mapping.c +8 -0
@@ -1821,3 +1821,11 @@ void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, {__arm_dma_free (dev, size, cpu_addr, dma_handle, attrs, false); } + +void arch_dma_prep_coherent (struct page *page,size_t size) +{ +void *ptr =page_address (page); + +dmac_flush_range (ptr, ptr + size); +outer_flush_range (__pa (ptr),__pa (ptr) + size); +}
Signature · 1 line
2.49.0
Quoted 11 lines · Christian Meissl
since switching to dma-direct, memory using
DMA_ATTR_NO_KERNEL_MAPPINGis no longer allocated using the arch specific handlers and instead will usedma_direct_alloc_no_mapping. While the arm specific allocation handlers implicitly clear the allocated dma buffers and will flush any caches dma-direct relies onARCH_HAS_DMA_PREP_COHERENTto flush the caches.Without this flush video frame corruption can occur in drivers
like the coda v4l2 driver which explicitly sets theDMA_ATTR_NO_KERNEL_MAPPINGflag.Fixes ae626eb97376 ("ARM/dma-mapping: use dma-direct unconditionally")Signed-off-by Christian Meissl <[email protected]>
[...]
Quoted 6 lines
arch/arm/mm/dma-mapping.c +0
-0@@ -1821,3 +1821,11 @@ void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, {
arch/arm/mm/dma-mapping.c void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, · line 1831
Quoted 10 lines
arch/arm/mm/dma-mapping.c +8
-0__arm_dma_free (dev, size, cpu_addr, dma_handle, attrs, false); } + +void arch_dma_prep_coherent (struct page *page,size_t size) +{ +void *ptr =page_address (page); + +dmac_flush_range (ptr, ptr + size); +outer_flush_range (__pa (ptr),__pa (ptr) + size); +}
It probably doesn't make any difference in practice, FWIW arm64 only does a clean rather than flush (clean+invalidate) here.
What I noticed is that arch_dma_prep_coherent() is only called for lowmem pages, so doing page_address() is safe. However, I don't think we have anything to flush the caches for highmem pages.
Signature · 1 line
Catalin