provide generic support for uncached segements in dma-direct
Subsystems (7)
Hi all,
can you take a look at this series? It lifts the support for mips-style uncached segements to the dma-direct layer, thus removing the need to have arch_dma_alloc/free routines for these architectures.
PATCH 1/7 MIPS: remove the _dma_cache_wback_inv export
This export is not used in modular code, which is a good thing as everyone should use the proper DMA API instead.
--- arch/mips/mm/cache.c | 2 -- 1 file changed, 2 deletions(-)
arch/mips/mm/cache.c +0 -2
@@ -62,8 +62,6 @@ void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);void (*_dma_cache_wback)(unsigned long start,unsigned long size);void (*_dma_cache_inv)(unsigned long start,unsigned long size); -EXPORT_SYMBOL (_dma_cache_wback_inv); -#endif /* CONFIG_DMA_NONCOHERENT */ /*
Signature · 1 line
2.20.1
PATCH 2/7 au1100fb: fix DMA API abuse
In reply to Christoph HellwigVirtual addresses return from dma(m)_alloc_coherent are opaque in what backs then, and drivers must not poke into them. Switch the driver to use the generic DMA API mmap helper to avoid these games.
--- drivers/video/fbdev/au1100fb.c | 24 ++++-------------------- drivers/video/fbdev/au1100fb.h | 1 + 2 files changed, 5 insertions(+), 20 deletions(-)
drivers/video/fbdev/au1100fb.c +3 -5
@@ -340,14 +340,12 @@ int au1100fb_fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fbi)*/ int au1100fb_fb_mmap (struct fb_info *fbi,struct vm_area_struct *vma) { -struct au1100fb_device *fbdev; - - fbdev =to_au1100fb_device (fbi); +struct au1100fb_device *fbdev =to_au1100fb_device (fbi); - vma->vm_page_prot =pgprot_noncached (vma->vm_page_prot);pgprot_val (vma->vm_page_prot) |= (6 <<9 );//CCA=6 -return vm_iomap_memory (vma, fbdev->fb_phys, fbdev->fb_len); +return dma_mmap_coherent (fbdev->dev, vma, fbdev->fb_mem, fbdev->fb_phys, + fbdev->fb_len); } static struct fb_ops au1100fb_ops @@ -412,7 +410,6 @@ static int au1100fb_drv_probe(struct platform_device *dev)
{
struct au1100fb_device *fbdev;
struct resource *regs_res;
- unsigned long page;
struct clk *c;
/* Allocate new device private */
@@ -424,6 +421,7 @@ static int au1100fb_drv_probe(struct platform_device *dev)
goto failed;
platform_set_drvdata(dev, (void *)fbdev);
+ fbdev->dev = &dev->dev;
/* Allocate region for our registers and map them */ regs_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
@@ -472,20 +470,6 @@ static int au1100fb_drv_probe(struct platform_device *dev)
au1100fb_fix.smem_start = fbdev->fb_phys; au1100fb_fix.smem_len = fbdev->fb_len;
- /*
- * Set page reserved so that mmap will work. This is necessary
- * since we'll be remapping normal memory.
- */
- for (page = (unsigned long)fbdev->fb_mem;
- page < PAGE_ALIGN((unsigned long)fbdev->fb_mem + fbdev->fb_len);
- page += PAGE_SIZE) {
-#ifdef CONFIG_DMA_NONCOHERENT
- SetPageReserved(virt_to_page(CAC_ADDR((void *)page)));
-#else
- SetPageReserved(virt_to_page(page));
-#endif
- }
-
print_dbg("Framebuffer memory map at %p", fbdev->fb_mem); print_dbg("phys=0x%08x, size=%dK", fbdev->fb_phys, fbdev->fb_len / 1024);
drivers/video/fbdev/au1100fb.h +1 -0
@@ -110,6 +110,7 @@ struct au1100fb_device {dma_addr_t fb_phys;int panel_idx;struct clk *lcdclk; +struct device *dev; };/********************************************************************/
Signature · 1 line
2.20.1
Re: [PATCH 2/7] au1100fb: fix DMA API abuse
Quoted 5 lines · Christoph Hellwig
Virtual addresses return from dma(m)_alloc_coherent are opaque in what backs then, and drivers must not poke into them. Switch the driver to use the generic DMA API mmap helper to avoid these games.
Signed-off-by Christoph Hellwig <[email protected]>
Best regards,
--
Bartlomiej Zolnierkiewicz
Samsung R&D Institute Poland
Samsung Electronics
Re: [PATCH 2/7] au1100fb: fix DMA API abuse
Quoted 9 lines · Bartlomiej Zolnierkiewicz
Quoted 5 lines · Christoph Hellwig
Virtual addresses return from dma(m)_alloc_coherent are opaque in what backs then, and drivers must not poke into them. Switch the driver to use the generic DMA API mmap helper to avoid these games.
Signed-off-by Christoph Hellwig <[email protected]>
Given this actually fixes issues with the current driver, what do you think of taking it for 5.2 through your tree? Also au1200fb has basically the same issues, just without abusing the CAC_ADDR helper, I could send you a patch for that one as well.
Re: [PATCH 2/7] au1100fb: fix DMA API abuse
Quoted 7 lines · Christoph Hellwig
Quoted 6 lines · Bartlomiej Zolnierkiewicz
Quoted 4 lines · Christoph Hellwig
Virtual addresses return from dma(m)_alloc_coherent are opaque in what backs then, and drivers must not poke into them. Switch the driver to use the generic DMA API mmap helper to avoid these games.
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Christoph Hellwig <[email protected]>Given this actually fixes issues with the current driver, what do you think of taking it for 5.2 through your tree? Also au1200fb has basically the same issues, just without abusing the
CAC_ADDRhelper, I could send you a patch for that one as well.
Please do, I have hardware to test if required.
Manuel
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH 2/7] au1100fb: fix DMA API abuse
In reply to Bartlomiej ZolnierkiewiczFYI, I've merged this patch into the dma-mapping tree to make progress with the generic uncached segment support for mips.
PATCH 3/7 dma-mapping: add a Kconfig symbol to indicate arch_dma_prep_coherent presence
In reply to Christoph HellwigAdd a Kconfig symbol that indicates an architecture provides a arch_dma_prep_coherent implementation, and provide a stub otherwise.
This will allow the generic dma-iommu code to use it while still allowing to be built for cache coherent architectures.
--- arch/arm64/Kconfig | 1 + arch/csky/Kconfig | 1 + include/linux/dma-noncoherent.h | 6 ++++++ kernel/dma/Kconfig | 3 +++ 4 files changed, 11 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -13,6 +13,7 @@ config ARM64 select ARCH_HAS_DEVMEM_IS_ALLOWED select ARCH_HAS_DMA_COHERENT_TO_PFN select ARCH_HAS_DMA_MMAP_PGPROT + select ARCH_HAS_DMA_PREP_COHERENT select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FAST_MULTIPLIER
arch/csky/Kconfig +1 -0
@@ -1,6 +1,7 @@ config CSKY def_bool y select ARCH_32BIT_OFF_T + select ARCH_HAS_DMA_PREP_COHERENT select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE select ARCH_USE_BUILTIN_BSWAP
include/linux/dma-noncoherent.h +6 -0
@@ -72,6 +72,12 @@ static inline void arch_sync_dma_for_cpu_all(struct device *dev) }#endif /* CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL */ +#ifdef CONFIG_ARCH_HAS_DMA_PREP_COHERENT void arch_dma_prep_coherent (struct page *page,size_t size); +#else +static inline void arch_dma_prep_coherent (struct page *page,size_t size) +{ +} +#endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */ #endif /* _LINUX_DMA_NONCOHERENT_H */
kernel/dma/Kconfig +3 -0
@@ -38,6 +38,9 @@ config ARCH_HAS_SYNC_DMA_FOR_CPU config ARCH_HAS_SYNC_DMA_FOR_CPU_ALL bool +config ARCH_HAS_DMA_PREP_COHERENT + bool + config ARCH_HAS_DMA_COHERENT_TO_PFN bool
Signature · 1 line
2.20.1
PATCH 4/7 dma-direct: provide generic support for uncached kernel segments
In reply to Christoph HellwigA few architectures support uncached kernel segments. In that case we get an uncached mapping for a given physica address by using an offset in the uncached segement. Implement support for this scheme in the generic dma-direct code instead of duplicating it in arch hooks.
--- arch/Kconfig | 8 ++++++++ include/linux/dma-noncoherent.h | 3 +++ kernel/dma/direct.c | 18 ++++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-)
arch/Kconfig +8 -0
@@ -249,6 +249,14 @@ config ARCH_HAS_FORTIFY_SOURCE config ARCH_HAS_SET_MEMORY bool +# +# Select if arch has an uncached kernel segment and provides the +# uncached_kernel_address / cached_kernel_address symbols to use it +# +config ARCH_HAS_UNCACHED_SEGMENT + select ARCH_HAS_DMA_PREP_COHERENT + bool + # Select if arch init_task must go in the __init_task_data section config ARCH_TASK_STRUCT_ON_STACK bool
include/linux/dma-noncoherent.h +3 -0
@@ -80,4 +80,7 @@ static inline void arch_dma_prep_coherent(struct page *page, size_t size) }#endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */ +void *uncached_kernel_address (void *addr); +void *cached_kernel_address (void *addr); +#endif /* _LINUX_DMA_NONCOHERENT_H */
kernel/dma/direct.c +16 -2
@@ -164,6 +164,13 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, } ret =page_address (page); + +if (IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) { +arch_dma_prep_coherent (page, size); + ret =uncached_kernel_address (ret); + } +if (force_dma_unencrypted ()) {set_memory_decrypted ((unsigned long )ret,1 <<get_order (size)); *dma_handle =__phys_to_dma (dev,page_to_phys (page));
@@ -171,6 +178,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, *dma_handle =phys_to_dma (dev,page_to_phys (page)); }memset (ret,0 , size); +return ret; }
@@ -189,13 +197,18 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,if (force_dma_unencrypted ())set_memory_encrypted ((unsigned long )cpu_addr,1 << page_order); + +if (IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) + cpu_addr =cached_kernel_address (cpu_addr);__dma_direct_free_pages (dev, size,virt_to_page (cpu_addr)); }void *dma_direct_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs) { -if (!dev_is_dma_coherent (dev)) +if (!IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev))return arch_dma_alloc (dev, size, dma_handle, gfp, attrs);return dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs); }
@@ -203,7 +216,8 @@ void *dma_direct_alloc(struct device *dev, size_t size,void dma_direct_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs) { -if (!dev_is_dma_coherent (dev)) +if (!IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev))arch_dma_free (dev, size, cpu_addr, dma_addr, attrs);else dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs);
Signature · 1 line
2.20.1
Re: [PATCH 4/7] dma-direct: provide generic support for uncached kernel segments
Hi Christoph,
Quoted 19 lines · Christoph Hellwig
kernel/dma/direct.c +7
-0@@ -164,6 +164,13 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, } ret =page_address (page); + +if (IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) { +arch_dma_prep_coherent (page, size); + ret =uncached_kernel_address (ret); + } +if (force_dma_unencrypted ()) {set_memory_decrypted ((unsigned long )ret,1 <<get_order (size)); *dma_handle =__phys_to_dma (dev,page_to_phys (page));
kernel/dma/direct.c void *dma_direct_alloc_pages(struct device *dev, size_t size, · line 183
Quoted 6 lines
kernel/dma/direct.c +1
-0@@ -171,6 +178,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, *dma_handle =phys_to_dma (dev,page_to_phys (page)); }memset (ret,0 , size); +return ret; }
I'm not so sure about this part though.
On MIPS we currently don't clear the allocated memory with memset. Is doing that really necessary?
If it is necessary then as-is this code will clear the allocated memory using uncached writes which will be pretty slow. It would be much more efficient to perform the memset before arch_dma_prep_coherent() & before converting ret to an uncached address.
Thanks,
Paul
Re: [PATCH 4/7] dma-direct: provide generic support for uncached kernel segments
Quoted 4 lines · Paul Burton
I'm not so sure about this part though.
On MIPS we currently don't clear the allocated memory with memset. Is doing that really necessary?
We are clearling it on mips, it is inside dma_direct_alloc_pages.
Quoted 4 lines
If it is necessary then as-is this code will clear the allocated memory using uncached writes which will be pretty slow. It would be much more efficient to perform the memset before
arch_dma_prep_coherent()& before converting ret to an uncached address.
Yes, we could do that.
From 247ca658ebeb7c8d04918747ec8a0da45c36bcb8 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Sun, 28 Apr 2019 13:23:26 -0500
Subject: dma-direct: provide generic support for uncached kernel segments
A few architectures support uncached kernel segments. In that case we get an uncached mapping for a given physica address by using an offset in the uncached segement. Implement support for this scheme in the generic dma-direct code instead of duplicating it in arch hooks.
--- arch/Kconfig | 8 ++++++++ include/linux/dma-noncoherent.h | 3 +++ kernel/dma/direct.c | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-)
arch/Kconfig +8 -0
@@ -249,6 +249,14 @@ config ARCH_HAS_FORTIFY_SOURCE config ARCH_HAS_SET_MEMORY bool +# +# Select if arch has an uncached kernel segment and provides the +# uncached_kernel_address / cached_kernel_address symbols to use it +# +config ARCH_HAS_UNCACHED_SEGMENT + select ARCH_HAS_DMA_PREP_COHERENT + bool + # Select if arch init_task must go in the __init_task_data section config ARCH_TASK_STRUCT_ON_STACK bool
include/linux/dma-noncoherent.h +3 -0
@@ -80,4 +80,7 @@ static inline void arch_dma_prep_coherent(struct page *page, size_t size) }#endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */ +void *uncached_kernel_address (void *addr); +void *cached_kernel_address (void *addr); +#endif /* _LINUX_DMA_NONCOHERENT_H */
kernel/dma/direct.c +15 -2
@@ -171,6 +171,13 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, *dma_handle =phys_to_dma (dev,page_to_phys (page)); }memset (ret,0 , size); + +if (IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) { +arch_dma_prep_coherent (page, size); + ret =uncached_kernel_address (ret); + } +return ret; }
@@ -189,13 +196,18 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,if (force_dma_unencrypted ())set_memory_encrypted ((unsigned long )cpu_addr,1 << page_order); + +if (IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) + cpu_addr =cached_kernel_address (cpu_addr);__dma_direct_free_pages (dev, size,virt_to_page (cpu_addr)); }void *dma_direct_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs) { -if (!dev_is_dma_coherent (dev)) +if (!IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev))return arch_dma_alloc (dev, size, dma_handle, gfp, attrs);return dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs); }
@@ -203,7 +215,8 @@ void *dma_direct_alloc(struct device *dev, size_t size,void dma_direct_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs) { -if (!dev_is_dma_coherent (dev)) +if (!IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev))arch_dma_free (dev, size, cpu_addr, dma_addr, attrs);else dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs);
Signature · 1 line
2.20.1
Re: [PATCH 4/7] dma-direct: provide generic support for uncached kernel segments
Hi Christoph,
Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Paul Burton
I'm not so sure about this part though.
On MIPS we currently don't clear the allocated memory with memset. Is doing that really necessary?
We are clearling it on mips, it is inside
dma_direct_alloc_pages.
Ah, of course, I clearly require more caffeine :)
Quoted 6 lines
Quoted 4 lines
If it is necessary then as-is this code will clear the allocated memory using uncached writes which will be pretty slow. It would be much more efficient to perform the memset before
arch_dma_prep_coherent()& before converting ret to an uncached address.Yes, we could do that.
Great; using cached writes would match the existing MIPS behavior.
Thanks,
Paul
Re: [PATCH 4/7] dma-direct: provide generic support for uncached kernel segments
Quoted 8 lines · Paul Burton
Quoted 6 lines
Quoted 4 lines
If it is necessary then as-is this code will clear the allocated memory using uncached writes which will be pretty slow. It would be much more efficient to perform the memset before
arch_dma_prep_coherent()& before converting ret to an uncached address.Yes, we could do that.
Great; using cached writes would match the existing MIPS behavior.
Can you test the stack with the two updated patches and ack them if they are fine? That would allow getting at least the infrastructure and mips in for this merge window.
Re: [PATCH 4/7] dma-direct: provide generic support for uncached kernel segments
Hi Christoph,
Quoted 6 lines · Christoph Hellwig
Quoted 5 lines · Paul Burton
Quoted 5 lines
Quoted 4 lines
If it is necessary then as-is this code will clear the allocated memory using uncached writes which will be pretty slow. It would be much more efficient to perform the memset before
arch_dma_prep_coherent()& before converting ret to an uncached address.
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Yes, we could do that.
Great; using cached writes would match the existing MIPS behavior.
Can you test the stack with the two updated patches and ack them if they are fine? That would allow getting at least the infrastructure and mips in for this merge window.
Did you send a v2 of this patch?
If so it hasn't showed up in my inbox, nor on the linux-mips archive on lore.kernel.org.
Thanks,
Paul
Re: [PATCH 4/7] dma-direct: provide generic support for uncached kernel segments
Quoted 8 lines · Paul Burton
Quoted 3 lines
Can you test the stack with the two updated patches and ack them if they are fine? That would allow getting at least the infrastructure and mips in for this merge window.
Did you send a v2 of this patch?
If so it hasn't showed up in my inbox, nor on the linux-mips archive on lore.kernel.org.
I did earlier in this thread. Here it is again:
---
From 247ca658ebeb7c8d04918747ec8a0da45c36bcb8 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Sun, 28 Apr 2019 13:23:26 -0500
Subject: dma-direct: provide generic support for uncached kernel segments
A few architectures support uncached kernel segments. In that case we get an uncached mapping for a given physica address by using an offset in the uncached segement. Implement support for this scheme in the generic dma-direct code instead of duplicating it in arch hooks.
--- arch/Kconfig | 8 ++++++++ include/linux/dma-noncoherent.h | 3 +++ kernel/dma/direct.c | 17 +++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-)
arch/Kconfig +8 -0
@@ -249,6 +249,14 @@ config ARCH_HAS_FORTIFY_SOURCE config ARCH_HAS_SET_MEMORY bool +# +# Select if arch has an uncached kernel segment and provides the +# uncached_kernel_address / cached_kernel_address symbols to use it +# +config ARCH_HAS_UNCACHED_SEGMENT + select ARCH_HAS_DMA_PREP_COHERENT + bool + # Select if arch init_task must go in the __init_task_data section config ARCH_TASK_STRUCT_ON_STACK bool
include/linux/dma-noncoherent.h +3 -0
@@ -80,4 +80,7 @@ static inline void arch_dma_prep_coherent(struct page *page, size_t size) }#endif /* CONFIG_ARCH_HAS_DMA_PREP_COHERENT */ +void *uncached_kernel_address (void *addr); +void *cached_kernel_address (void *addr); +#endif /* _LINUX_DMA_NONCOHERENT_H */
kernel/dma/direct.c +15 -2
@@ -171,6 +171,13 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, *dma_handle =phys_to_dma (dev,page_to_phys (page)); }memset (ret,0 , size); + +if (IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) { +arch_dma_prep_coherent (page, size); + ret =uncached_kernel_address (ret); + } +return ret; }
@@ -189,13 +196,18 @@ void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr,if (force_dma_unencrypted ())set_memory_encrypted ((unsigned long )cpu_addr,1 << page_order); + +if (IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev) && !(attrs &DMA_ATTR_NON_CONSISTENT )) + cpu_addr =cached_kernel_address (cpu_addr);__dma_direct_free_pages (dev, size,virt_to_page (cpu_addr)); }void *dma_direct_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs) { -if (!dev_is_dma_coherent (dev)) +if (!IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev))return arch_dma_alloc (dev, size, dma_handle, gfp, attrs);return dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs); }
@@ -203,7 +215,8 @@ void *dma_direct_alloc(struct device *dev, size_t size,void dma_direct_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs) { -if (!dev_is_dma_coherent (dev)) +if (!IS_ENABLED (CONFIG_ARCH_HAS_UNCACHED_SEGMENT ) && + !dev_is_dma_coherent (dev))arch_dma_free (dev, size, cpu_addr, dma_addr, attrs);else dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs);
Signature · 1 line
2.20.1
PATCH 5/7 MIPS: use the generic uncached segment support in dma-direct
In reply to Christoph HellwigStop providing our arch alloc/free hooks and just expose the segment offset instead.
--- arch/mips/Kconfig | 1 + arch/mips/include/asm/page.h | 3 --- arch/mips/jazz/jazzdma.c | 6 ------ arch/mips/mm/dma-noncoherent.c | 27 ++++++++++----------------- 4 files changed, 11 insertions(+), 26 deletions(-)
arch/mips/Kconfig +1 -0
@@ -9,6 +9,7 @@ config MIPS select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN_SANITIZE_ALL + select ARCH_HAS_UNCACHED_SEGMENT select ARCH_SUPPORTS_UPROBES select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_CMPXCHG_LOCKREF if 64BIT
arch/mips/include/asm/page.h +0 -3
@@ -258,9 +258,6 @@ extern int __virt_addr_valid(const volatile void *kaddr); ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0) | \ VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) -#define UNCAC_ADDR (addr) (UNCAC_BASE + __pa(addr)) -#define CAC_ADDR (addr) ((unsigned long)__va((addr) - UNCAC_BASE)) -#include <asm-generic/memory_model.h> #include <asm-generic/getorder.h>
arch/mips/jazz/jazzdma.c +0 -6
@@ -575,10 +575,6 @@ static void *jazz_dma_alloc(struct device *dev, size_t size,return NULL ; } -if (!(attrs &DMA_ATTR_NON_CONSISTENT )) { -dma_cache_wback_inv ((unsigned long )ret, size); - ret = (void *)UNCAC_ADDR (ret); - }return ret; }
@@ -586,8 +582,6 @@ static void jazz_dma_free(struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle, unsigned long attrs) {vdma_free (dma_handle ); -if (!(attrs &DMA_ATTR_NON_CONSISTENT )) - vaddr = (void *)CAC_ADDR ((unsigned long )vaddr);dma_direct_free_pages (dev, size, vaddr, dma_handle, attrs); }
arch/mips/mm/dma-noncoherent.c +10 -17
@@ -44,33 +44,26 @@ static inline bool cpu_needs_post_dma_flush(struct device *dev) } } -void *arch_dma_alloc (struct device *dev,size_t size, -dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs) +void arch_dma_prep_coherent (struct page *page,size_t size) { -void *ret; - - ret =dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs); -if (ret && !(attrs &DMA_ATTR_NON_CONSISTENT )) { -dma_cache_wback_inv ((unsigned long ) ret, size); - ret = (void *)UNCAC_ADDR (ret); - } +if (!PageHighMem (page)) +dma_cache_wback_inv ((unsigned long )page_address (page), size); +} -return ret; +void *uncached_kernel_address (void *addr) +{ +return (void *)(__pa (addr) +UNCAC_BASE ); } -void arch_dma_free (struct device *dev,size_t size,void *cpu_addr, -dma_addr_t dma_addr,unsigned long attrs) +void *cached_kernel_address (void *addr) { -if (!(attrs &DMA_ATTR_NON_CONSISTENT )) - cpu_addr = (void *)CAC_ADDR ((unsigned long )cpu_addr); -dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs); +return __va (addr) -UNCAC_BASE ; }long arch_dma_coherent_to_pfn (struct device *dev,void *cpu_addr,dma_addr_t dma_addr) { -unsigned long addr =CAC_ADDR ((unsigned long )cpu_addr); -return page_to_pfn (virt_to_page ((void *)addr)); +return page_to_pfn (virt_to_page (cached_kernel_address (cpu_addr))); } pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
Signature · 1 line
2.20.1
Re: [PATCH 5/7] MIPS: use the generic uncached segment support in dma-direct
Hi Christoph,
Quoted 12 lines · Christoph Hellwig
arch/mips/mm/dma-noncoherent.c +1
-2@@ -44,33 +44,26 @@ static inline bool cpu_needs_post_dma_flush(struct device *dev) } } -void *arch_dma_alloc (struct device *dev,size_t size, -dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs) +void arch_dma_prep_coherent (struct page *page,size_t size) {
arch/mips/mm/dma-noncoherent.c static inline bool cpu_needs_post_dma_flush(struct device *dev) · line 51
Quoted 10 lines
arch/mips/mm/dma-noncoherent.c +3
-7-void *ret; - - ret =dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs); -if (ret && !(attrs &DMA_ATTR_NON_CONSISTENT )) { -dma_cache_wback_inv ((unsigned long ) ret, size); - ret = (void *)UNCAC_ADDR (ret); - } +if (!PageHighMem (page)) +dma_cache_wback_inv ((unsigned long )page_address (page), size); +}
This series looks like a nice cleanup to me - the one thing that puzzles me is the !PageHighMem check above.
As far as I can see arch_dma_prep_coherent() should never be called with a highmem page, so would it make more sense to either drop this check or perhaps wrap it in a WARN_ON()?
Thanks,
Paul
Re: [PATCH 5/7] MIPS: use the generic uncached segment support in dma-direct
Quoted 6 lines · Paul Burton
This series looks like a nice cleanup to me - the one thing that puzzles me is the !PageHighMem check above.
As far as I can see
arch_dma_prep_coherent()should never be called with a highmem page, so would it make more sense to either drop this check or perhaps wrap it in aWARN_ON()?
dma_alloc_from_contigous can return highmem pages depending on where the CMA area is located. But given that these pages don't have a direct kernel mapping we also shouldn't have to flush the caches for them.
Re: [PATCH 5/7] MIPS: use the generic uncached segment support in dma-direct
Hi Christoph,
Quoted 4 lines · Christoph Hellwig
Quoted 3 lines · Paul Burton
This series looks like a nice cleanup to me - the one thing that puzzles me is the !PageHighMem check above.
Quoted 8 lines
Quoted 3 lines
As far as I can see
arch_dma_prep_coherent()should never be called with a highmem page, so would it make more sense to either drop this check or perhaps wrap it in aWARN_ON()?
dma_alloc_from_contigouscan return highmem pages depending on where the CMA area is located. But given that these pages don't have a direct kernel mapping we also shouldn't have to flush the caches for them.
Right but dma_direct_alloc_pages() already checks for the PageHighMem case & returns before ever calling arch_dma_prep_coherent(), no?
Thanks,
Paul
Re: [PATCH 5/7] MIPS: use the generic uncached segment support in dma-direct
Quoted 2 lines · Paul Burton
Right but
dma_direct_alloc_pages()already checks for the PageHighMem case & returns before ever callingarch_dma_prep_coherent(), no?
True. And of course it can't be remapped into the uncached segment anyway. So yes, we should drop it. Eventually I want to add generic support for DMA_ATTR_NO_KERNEL_MAPPING, but that'll involved auditing all instances anyway.
PATCH v2 5/7 MIPS: use the generic uncached segment support in dma-direct
In reply to Paul BurtonStop providing our arch alloc/free hooks and just expose the segment offset instead.
--- arch/mips/Kconfig | 1 + arch/mips/include/asm/page.h | 3 --- arch/mips/jazz/jazzdma.c | 6 ------ arch/mips/mm/dma-noncoherent.c | 26 +++++++++----------------- 4 files changed, 10 insertions(+), 26 deletions(-)
arch/mips/Kconfig +1 -0
@@ -9,6 +9,7 @@ config MIPS select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN_SANITIZE_ALL + select ARCH_HAS_UNCACHED_SEGMENT select ARCH_SUPPORTS_UPROBES select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_CMPXCHG_LOCKREF if 64BIT
arch/mips/include/asm/page.h +0 -3
@@ -258,9 +258,6 @@ extern int __virt_addr_valid(const volatile void *kaddr); ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0) | \ VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) -#define UNCAC_ADDR (addr) (UNCAC_BASE + __pa(addr)) -#define CAC_ADDR (addr) ((unsigned long)__va((addr) - UNCAC_BASE)) -#include <asm-generic/memory_model.h> #include <asm-generic/getorder.h>
arch/mips/jazz/jazzdma.c +0 -6
@@ -575,10 +575,6 @@ static void *jazz_dma_alloc(struct device *dev, size_t size,return NULL ; } -if (!(attrs &DMA_ATTR_NON_CONSISTENT )) { -dma_cache_wback_inv ((unsigned long )ret, size); - ret = (void *)UNCAC_ADDR (ret); - }return ret; }
@@ -586,8 +582,6 @@ static void jazz_dma_free(struct device *dev, size_t size, void *vaddr, dma_addr_t dma_handle, unsigned long attrs) {vdma_free (dma_handle ); -if (!(attrs &DMA_ATTR_NON_CONSISTENT )) - vaddr = (void *)CAC_ADDR ((unsigned long )vaddr);dma_direct_free_pages (dev, size, vaddr, dma_handle, attrs); }
arch/mips/mm/dma-noncoherent.c +9 -17
@@ -44,33 +44,25 @@ static inline bool cpu_needs_post_dma_flush(struct device *dev) } } -void *arch_dma_alloc (struct device *dev,size_t size, -dma_addr_t *dma_handle,gfp_t gfp,unsigned long attrs) +void arch_dma_prep_coherent (struct page *page,size_t size) { -void *ret; - - ret =dma_direct_alloc_pages (dev, size, dma_handle, gfp, attrs); -if (ret && !(attrs &DMA_ATTR_NON_CONSISTENT )) { -dma_cache_wback_inv ((unsigned long ) ret, size); - ret = (void *)UNCAC_ADDR (ret); - } +dma_cache_wback_inv ((unsigned long )page_address (page), size); +} -return ret; +void *uncached_kernel_address (void *addr) +{ +return (void *)(__pa (addr) +UNCAC_BASE ); } -void arch_dma_free (struct device *dev,size_t size,void *cpu_addr, -dma_addr_t dma_addr,unsigned long attrs) +void *cached_kernel_address (void *addr) { -if (!(attrs &DMA_ATTR_NON_CONSISTENT )) - cpu_addr = (void *)CAC_ADDR ((unsigned long )cpu_addr); -dma_direct_free_pages (dev, size, cpu_addr, dma_addr, attrs); +return __va (addr) -UNCAC_BASE ; }long arch_dma_coherent_to_pfn (struct device *dev,void *cpu_addr,dma_addr_t dma_addr) { -unsigned long addr =CAC_ADDR ((unsigned long )cpu_addr); -return page_to_pfn (virt_to_page ((void *)addr)); +return page_to_pfn (virt_to_page (cached_kernel_address (cpu_addr))); } pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
Signature · 1 line
2.20.1
Re: [PATCH 5/7 v2] MIPS: use the generic uncached segment support in dma-direct
Hi Christoph,
Quoted 10 lines · Christoph Hellwig
Stop providing our arch alloc/free hooks and just expose the segment offset instead.
Signed-off-by Christoph Hellwig <[email protected]>--- arch/mips/Kconfig | 1 + arch/mips/include/asm/page.h | 3 --- arch/mips/jazz/jazzdma.c | 6 ------ arch/mips/mm/dma-noncoherent.c | 26 +++++++++----------------- 4 files changed, 10 insertions(+), 26 deletions(-)
This one looks good to me now, for patches 1 & 5:
Thanks,
Paul
Re: [PATCH 5/7 v2] MIPS: use the generic uncached segment support in dma-direct
Quoted 14 lines · Paul Burton
Hi Christoph,
Quoted 10 lines · Christoph Hellwig
Stop providing our arch alloc/free hooks and just expose the segment offset instead.
Signed-off-by Christoph Hellwig <[email protected]>--- arch/mips/Kconfig | 1 + arch/mips/include/asm/page.h | 3 --- arch/mips/jazz/jazzdma.c | 6 ------ arch/mips/mm/dma-noncoherent.c | 26 +++++++++----------------- 4 files changed, 10 insertions(+), 26 deletions(-)
Quoted 3 lines
This one looks good to me now, for patches 1 & 5:
Acked-by Paul Burton <[email protected]>
Thanks, I've merged thos into the dma-mapping tree.
Re: [PATCH 5/7 v2] MIPS: use the generic uncached segment support in dma-direct
Quoted 16 lines · Christoph Hellwig
Quoted 14 lines · Paul Burton
Hi Christoph,
Quoted 10 lines · Christoph Hellwig
Stop providing our arch alloc/free hooks and just expose the segment offset instead.
Signed-off-by Christoph Hellwig <[email protected]>--- arch/mips/Kconfig | 1 + arch/mips/include/asm/page.h | 3 --- arch/mips/jazz/jazzdma.c | 6 ------ arch/mips/mm/dma-noncoherent.c | 26 +++++++++----------------- 4 files changed, 10 insertions(+), 26 deletions(-)
Quoted 5 lines
Quoted 3 lines
This one looks good to me now, for patches 1 & 5:
Acked-by Paul Burton <[email protected]>Thanks, I've merged thos into the dma-mapping tree.
I think this is the cause of some kernelci failures in current linux-next builds:
https://kernelci.org/build/next/branch/master/kernel/next-20190702/
bigsur_defconfig ‐ mips3 warnings — 1 error cavium_octeon_defconfig ‐ mips3 warnings — 1 error ip27_defconfig ‐ mips3 warnings — 1 error loongson3_defconfig ‐ mips3 warnings — 1 error mips_paravirt_defconfig ‐ mips3 warnings — 1 error nlm_xlp_defconfig ‐ mips3 warnings — 1 error nlm_xlr_defconfig ‐ mips1 warning — 1 error
/home/buildslave/workspace/workspace/kernel-build@8/linux/build/../kernel/dma/direct.c:144:
undefined reference to `arch_dma_prep_coherent' 2/home/buildslave/workspace/kernel-build/linux/build/../kernel/dma/direct.c:144:
undefined reference to `arch_dma_prep_coherent'
2(.text+0xafc): undefined reference to arch_dma_prep_coherent' 1direct.c:(.text+0x934): undefined reference to arch_dma_prep_coherent' 1(.text+0xb84): undefined reference to `arch_dma_prep_coherent'
I haven't looked into the details, but I suspect all machines with cache-coherent DMA are broken.
Arnd
Re: [PATCH 5/7 v2] MIPS: use the generic uncached segment support in dma-direct
Quoted 2 lines · Arnd Bergmann
I think this is the cause of some kernelci failures in current linux-next builds:
Yes, Guenther reported this already and I sent a fix. I've been waiting for an ACK from the mips maintaines, but given the breakage I might as well just pull it in without an ACK.
PATCH 6/7 nios2: use the generic uncached segment support in dma-direct
In reply to Christoph HellwigStop providing our own arch alloc/free hooks and just expose the segment offset and use the generic dma-direct allocator.
--- arch/nios2/Kconfig | 1 + arch/nios2/include/asm/page.h | 6 ------ arch/nios2/mm/dma-mapping.c | 34 +++++++++++++++------------------- 3 files changed, 16 insertions(+), 25 deletions(-)
arch/nios2/Kconfig +1 -0
@@ -4,6 +4,7 @@ config NIOS2 select ARCH_32BIT_OFF_T select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE + select ARCH_HAS_UNCACHED_SEGMENT select ARCH_NO_SWAP select TIMER_OF select GENERIC_ATOMIC64
arch/nios2/include/asm/page.h +0 -6
@@ -101,12 +101,6 @@ static inline bool pfn_valid(unsigned long pfn)# define VM_DATA_DEFAULT_FLAGS (VM_READ | VM_WRITE | \ VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) -# define UNCAC_ADDR (addr) \ - ((void *)((unsigned)(addr) | CONFIG_NIOS2_IO_REGION_BASE)) -# define CAC_ADDR (addr) \ - ((void *)(((unsigned)(addr) & ~CONFIG_NIOS2_IO_REGION_BASE) | \ - CONFIG_NIOS2_KERNEL_REGION_BASE)) -#include <asm-generic/memory_model.h> #include <asm-generic/getorder.h>
arch/nios2/mm/dma-mapping.c +15 -19
@@ -60,32 +60,28 @@ void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr, } } -void *arch_dma_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle, -gfp_t gfp,unsigned long attrs) +void arch_dma_prep_coherent (struct page *page,size_t size) { -void *ret; +unsigned long start = (unsigned long )page_address (page); -/* optimized page clearing */ - gfp |= __GFP_ZERO; +flush_dcache_range (start, start + size); +} -if (dev =NULL || (dev->coherent_dma_mask <0xffffffff )) - gfp |=GFP_DMA ; +void *uncached_kernel_address (void *ptr) +{ +unsigned long addr = (unsigned long )ptr; - ret = (void *)__get_free_pages (gfp,get_order (size)); -if (ret !=NULL ) { - *dma_handle =virt_to_phys (ret); -flush_dcache_range ((unsigned long ) ret, - (unsigned long ) ret + size); - ret =UNCAC_ADDR (ret); - } + addr |=CONFIG_NIOS2_IO_REGION_BASE ; -return ret; +return (void *)ptr; } -void arch_dma_free (struct device *dev,size_t size,void *vaddr, -dma_addr_t dma_handle,unsigned long attrs) +void *cached_kernel_address (void *ptr) { -unsigned long addr = (unsigned long )CAC_ADDR ((unsigned long ) vaddr); +unsigned long addr = (unsigned long )ptr; + + addr &= ~CONFIG_NIOS2_IO_REGION_BASE ; + addr |=CONFIG_NIOS2_KERNEL_REGION_BASE ; -free_pages (addr,get_order (size)); +return (void *)ptr; }
Signature · 1 line
2.20.1
PATCH 7/7 microblaze: use the generic uncached segment support in dma-direct
In reply to Christoph HellwigStop providing our own arch alloc/free hooks for nommu platforms and just expose the segment offset and use the generic dma-direct allocator.
--- arch/microblaze/Kconfig | 2 + arch/microblaze/mm/consistent.c | 97 +++++++++++++++------------------ 2 files changed, 47 insertions(+), 52 deletions(-)
arch/microblaze/Kconfig +2 -0
@@ -3,9 +3,11 @@ config MICROBLAZE select ARCH_32BIT_OFF_T select ARCH_NO_SWAP select ARCH_HAS_DMA_COHERENT_TO_PFN if MMU + select ARCH_HAS_DMA_PREP_COHERENT select ARCH_HAS_GCOV_PROFILE_ALL select ARCH_HAS_SYNC_DMA_FOR_CPU select ARCH_HAS_SYNC_DMA_FOR_DEVICE + select ARCH_HAS_UNCACHED_SEGMENT if !MMU select ARCH_MIGHT_HAVE_PC_PARPORT select ARCH_NO_COHERENT_DMA_MMAP if !MMU select ARCH_WANT_IPC_PARSE_VERSION
arch/microblaze/mm/consistent.c +45 -52
158 lines not on this page · raw message
Signature · 1 line
2.20.1