sparc32: Page align size in arch_dma_alloc
Andreas Larsson
maintainerPATCH 1/1 sparc32: Page align size in arch_dma_alloc
Commit 53b7670e5735 ("sparc: factor the dma coherent mapping into helper") lost the page align for the calls to dma_make_coherent and srmmu_unmapiorange. The latter cannot handle a non page aligned len argument.
--- arch/sparc/kernel/ioport.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/sparc/kernel/ioport.c +3 -1
@@ -356,7 +356,9 @@ void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,void arch_dma_free (struct device *dev,size_t size,void *cpu_addr,dma_addr_t dma_addr,unsigned long attrs) { -if (!sparc_dma_free_resource (cpu_addr,PAGE_ALIGN (size))) + size =PAGE_ALIGN (size); + +if (!sparc_dma_free_resource (cpu_addr, size))return ;dma_make_coherent (dma_addr, size);
Signature · 1 line
2.17.1
Quoted 6 lines · Andreas Larsson
Commit
53b7670e5735("sparc: factor the dma coherent mapping into helper") lost the page align for the calls todma_make_coherentandsrmmu_unmapiorange. The latter cannot handle a non page aligned len argument.Signed-off-by Andreas Larsson <[email protected]>
Looks good,
Andreas - while I've got your attention: I've been looking into fully converting sparc32 to the generic DMA code. Do you have any documentation for the Leon cache handling in dma_make_coherent, and more importantly how that applies to the dma coherent handling? I could see how a flush might be required for the streaming DMA mappings, that is mapping normal cached memory for I/O. But for the coherent allocations which can be accessed from the device and the cpu without another DMA mapping call this seems really strange.
Andreas Larsson
maintainerQuoted 8 lines · Christoph Hellwig
Andreas - while I've got your attention: I've been looking into fully converting sparc32 to the generic DMA code. Do you have any documentation for the Leon cache handling in
dma_make_coherent, and more importantly how that applies to the dma coherent handling? I could see how a flush might be required for the streaming DMA mappings, that is mapping normal cached memory for I/O. But for the coherent allocations which can be accessed from the device and the cpu without another DMA mapping call this seems really strange.
As long as the area passed to arch_dma_free is mapped by arch_dma_allocate, I don't see why the call to dma_make_coherent in arch_dma_free should be needed. I am not sure if there are any current (or historical paths) where we nevertheless have a cacheable mapping when we reach arch_dma_free (or the historical pci32_free_coherent).
The usual case for LEON systems is that cache snooping on the CPU side invalidates cache lines matching DMA that the CPU sees on the bus. Under the assumption that DMA accesses are seen on the processor bus, this is the reason for only flushing if snooping is not enabled in dma_make_coherent.
Signature · 1 line
Andreas Larsson
Quoted 9 lines · Andreas Larsson
Quoted 8 lines
Andreas - while I've got your attention: I've been looking into fully converting sparc32 to the generic DMA code. Do you have any documentation for the Leon cache handling in
dma_make_coherent, and more importantly how that applies to the dma coherent handling? I could see how a flush might be required for the streaming DMA mappings, that is mapping normal cached memory for I/O. But for the coherent allocations which can be accessed from the device and the cpu without another DMA mapping call this seems really strange.
Quoted 5 lines
As long as the area passed to
arch_dma_freeis mapped byarch_dma_allocate, I don't see why the call todma_make_coherentinarch_dma_freeshould be needed. I am not sure if there are any current (or historical paths) where we nevertheless have a cacheable mapping when we reacharch_dma_free(or the historicalpci32_free_coherent).
Note that the cacheable mapping in the kernel map still exists, but is is not used for any access.
Quoted 5 lines
The usual case for LEON systems is that cache snooping on the CPU side invalidates cache lines matching DMA that the CPU sees on the bus. Under the assumption that DMA accesses are seen on the processor bus, this is the reason for only flushing if snooping is not enabled in
dma_make_coherent.
Thanks. Can you take a look and test the two patches below on top of your fix? A git tree is also available here:
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/sparc32-generic-dma
From 832183d0409a941788e4c27682b2ad5164aec5d9 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Mon, 13 Sep 2021 18:39:38 +0200
Subject: sparc32: remove dma_make_coherent
LEON only needs snooping when DMA accesses are not seen on the processor bus. Given that coherent allocations are mapped uncached this can't happen for those, so open code the d-cache flushing logic in the only remaing place that needs it, arch_sync_dma_for_cpu.
--- arch/sparc/kernel/ioport.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-)
arch/sparc/kernel/ioport.c +10 -16
@@ -52,17 +52,6 @@#include <asm/io-unit.h> #include <asm/leon.h> -/* This function must make sure that caches and memory are coherent after DMA -* On LEON systems without cache snooping it flushes the entire D-CACHE. -*/ -static inline void dma_make_coherent (unsigned long pa,unsigned long len) -{ -if (sparc_cpu_model == sparc_leon) { -if (!sparc_leon3_snooping_enabled ()) -leon_flush_dcache_all (); - } -} -static void __iomem *_sparc_ioremap (struct resource *res,u32 bus,u32 pa,int sz);static void __iomem *_sparc_alloc_io (unsigned int busno,unsigned long phys,unsigned long size,char *name);
@@ -361,18 +350,23 @@ void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,if (!sparc_dma_free_resource (cpu_addr, size))return ; -dma_make_coherent (dma_addr, size);srmmu_unmapiorange ((unsigned long )cpu_addr, size);free_pages ((unsigned long )phys_to_virt (dma_addr),get_order (size)); } -/* IIep is write-through, not flushing on cpu to device transfer. */ - +/* +* IIep is write-through, not flushing on cpu to device transfer. +* +* On LEON systems without cache snooping, the entire D-CACHE must be flushed to +* make DMA to cacheable memory coherent. +*/ void arch_sync_dma_for_cpu (phys_addr_t paddr,size_t size,enum dma_data_direction dir) { -if (dir !=PCI_DMA_TODEVICE ) -dma_make_coherent (paddr,PAGE_ALIGN (size)); +if (dir !=PCI_DMA_TODEVICE && + sparc_cpu_model == sparc_leon && + !sparc_leon3_snooping_enabled ()) +leon_flush_dcache_all (); }#ifdef CONFIG_PROC_FS
Signature · 1 line
2.30.2
From 0f612347699290a8d2f604a7640b5568e3d9af57 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Mon, 13 Sep 2021 18:44:55 +0200
Subject: sparc32: use DMA_DIRECT_REMAP
Use the generic dma remapping allocator instead of open coding it. This also avoids setting up page tables from irq context which is generally dangerous and uses the atomic pool instead.
The only interesting part is the architecture specific pgprot_dmacoherent definition that sets the SRMMU_PRIV bit as done by the old implementation. (I have no idea what it is useful for, though).
--- arch/sparc/Kconfig | 3 +- arch/sparc/include/asm/pgtable_32.h | 8 +++++ arch/sparc/kernel/ioport.c | 54 ----------------------------- 3 files changed, 10 insertions(+), 55 deletions(-)
arch/sparc/Kconfig +2 -1
@@ -54,8 +54,9 @@ config SPARC32 def_bool !64BIT select ARCH_32BIT_OFF_T select ARCH_HAS_SYNC_DMA_FOR_CPU - select GENERIC_ATOMIC64 select CLZ_TAB + select DMA_DIRECT_REMAP + select GENERIC_ATOMIC64 select HAVE_UID16 select OLD_SIGACTION select ZONE_DMA
arch/sparc/include/asm/pgtable_32.h +8 -0
@@ -313,6 +313,14 @@ static inline pgprot_t pgprot_noncached(pgprot_t prot)return prot; } +#define pgprot_dmacoherent pgprot_dmacoherent +static inline pgprot_t pgprot_dmacoherent (pgprot_t prot) +{ +pgprot_val (prot) &= ~pgprot_val (__pgprot (SRMMU_CACHE )); +pgprot_val (prot) |=pgprot_val (__pgprot (SRMMU_PRIV )); +return prot; +} + static pte_t pte_modify(pte_t pte, pgprot_t newprot) __attribute_const__;static inline pte_t pte_modify (pte_t pte,pgprot_t newprot) {
arch/sparc/kernel/ioport.c +0 -54
60 lines not on this page · raw message
Signature · 1 line
2.30.2
Andreas Larsson
maintainerQuoted 5 lines · Christoph Hellwig
Thanks. Can you take a look and test the two patches below on top of your fix? A git tree is also available here:
http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/sparc32-generic-dma
In a quick test, this seems to work on LEON for code paths previously going to arch_dma_alloc and arch_dma_free. However, this makes setting up these DMA mappings to not go through sparc_dma_alloc_resource, and it seems important that they do that on Sun systems. Hopefully, someone with more knowledge about that could chime in here.
The added pgprot_dmacoherent is problematic as it sets SRMMU_PRIV, which sets up kernel access only. This was fine for arch_dma_alloc that sets up kernel accesses only, but for user space DMA mmap this would make them kernel accessable only. Having no sparc-specific pgprot_dmacoherent, keeping it to default to pgprot_noncached, is probably better.
Signature · 1 line
Andreas Larsson
Quoted 4 lines · Andreas Larsson
Quoted 3 lines · Christoph Hellwig
Thanks. Can you take a look and test the two patches below on top of your fix? A git tree is also available here:
Quoted 8 lines
In a quick test, this seems to work on LEON for code paths previously going to
arch_dma_allocandarch_dma_free. However, this makes setting up these DMA mappings to not go throughsparc_dma_alloc_resource, and it seems important that they do that on Sun systems. Hopefully, someone with more knowledge about that could chime in here.
Does the hardware actually care about it? The only thing it does is to force allocating from a specific virtual address range, but how would that have a special meaning?
Quoted 5 lines
The added
pgprot_dmacoherentis problematic as it setsSRMMU_PRIV, which sets up kernel access only. This was fine forarch_dma_allocthat sets up kernel accesses only, but for user space DMA mmap this would make them kernel accessable only. Having no sparc-specificpgprot_dmacoherent, keeping it to default topgprot_noncached, is probably better.
I've just tried to keep the existing attributes. If SRMMU_PRIV does indeed mean that the page can't also be mapped into userspace page tables it would be good to remove it in an incremental patch. If OTOH it only means that this PTE is a kernel mapping it should not affect a userspace mapping as that will always use separate PTEs.
Quoted 3 lines
Signature · 2 lines
-- Andreas Larsson
---end quoted text---
Andreas Larsson
maintainerPlease consider the environment before printing this email
Quoted 11 lines · Christoph Hellwig
Quoted 5 lines
The added
pgprot_dmacoherentis problematic as it setsSRMMU_PRIV, which sets up kernel access only. This was fine forarch_dma_allocthat sets up kernel accesses only, but for user space DMA mmap this would make them kernel accessable only. Having no sparc-specificpgprot_dmacoherent, keeping it to default topgprot_noncached, is probably better.I've just tried to keep the existing attributes. If
SRMMU_PRIVdoes indeed mean that the page can't also be mapped into userspace page tables it would be good to remove it in an incremental patch. If OTOH it only means that this PTE is a kernel mapping it should not affect a userspace mapping as that will always use separate PTEs.
Before the patch, arch_dma_alloc did via srmmu_mapiorange set up pages with SRMMU_PRIV, which is all fine as it sets up kernel buffers. With your patch we get PAGE_KERNEL as an argument to dma_pgprot in the corresponding call path that earlier lead to arch_dma_alloc. PAGE_KERNEL already includes SRMMU_PRIV so adding it again should not be necessary.
The problem I am pointing to is that adding a pgprot_dmacoherent that adds SRMMU_PRIV, changes the behaviour of other call paths that calls dma_pgprot but are not mapping in kernel pages.
Now this is not confirmed in execution from my side, but it seems that from following the code that e.g. this call path that is about mapping DMA pages accessible from user space:
dma_mmap_attrs -> dma_direct_mmap -> dma_pgprot -> pgprot_dmacoherent
goes from making it merely uncacheable with the default
#ifndef pgprot_dmacoherent
#define pgprot_dmacoherent(prot) pgprot_noncached(prot)
#endif
to also being non-user-accessible if we change to this pgprot_dmacoherent
#define pgprot_dmacoherent pgprot_dmacoherent static inline pgprot_t pgprot_dmacoherent(pgprot_t prot)
{
pgprot_val(prot) &= ~pgprot_val(__pgprot(SRMMU_CACHE));
pgprot_val(prot) |= pgprot_val(__pgprot(SRMMU_PRIV));
return prot;
}Signature · 1 line
Andreas Larsson
Quoted 5 lines · Andreas Larsson
Before the patch,
arch_dma_allocdid viasrmmu_mapiorangeset up pages withSRMMU_PRIV, which is all fine as it sets up kernel buffers. With your patch we getPAGE_KERNELas an argument todma_pgprotin the corresponding call path that earlier lead toarch_dma_alloc.PAGE_KERNELalready includesSRMMU_PRIVso adding it again should not be necessary.
You're right, I missed that PAGE_KERNEL already includes SRMMU_PRIV.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 8 lines · Sam Ravnborg
Hi Andreas,
Quoted 4 lines · Andreas Larsson
Commit
53b7670e5735("sparc: factor the dma coherent mapping into helper") lost the page align for the calls todma_make_coherentandsrmmu_unmapiorange. The latter cannot handle a non page aligned len argument.
Quoted 6 lines
I wonder how you managed to track this down - well done.
Quoted 2 lines
Signed-off-by Andreas Larsson <[email protected]>Reviewed-by Sam Ravnborg <[email protected]>I assume David or Christoph picks it up.
I'll happily pick it up if that is ok. Dave?
David Miller
maintainerFrom: Christoph Hellwig <[email protected]>
Date: Tue, 14 Sep 2021 08:12:22 +0200
Quoted 9 lines · Sam Ravnborg
Quoted 8 lines
Hi Andreas,
Quoted 4 lines · Andreas Larsson
Commit
53b7670e5735("sparc: factor the dma coherent mapping into helper") lost the page align for the calls todma_make_coherentandsrmmu_unmapiorange. The latter cannot handle a non page aligned len argument.
Quoted 8 lines
Quoted 6 lines
I wonder how you managed to track this down - well done.
Quoted 2 lines
Signed-off-by Andreas Larsson <[email protected]>Reviewed-by Sam Ravnborg <[email protected]>I assume David or Christoph picks it up.
I'll happily pick it up if that is ok. Dave?
Yep, it's ok.