fix the non-coherent coldfire dma_alloc_coherent v4
Subsystems (7)
Hi all,
this is the next attempt to not return memory that is not DMA coherent on coldfire/m68knommu. The last one needed more fixups in the fec driver, which this versions includes. On top of that I've also added a few more cleanups to the core DMA allocation code.
Note: without the separately submitted
fix a few RISC-V / renesas Kconfig dependencies
series this will cause Kconfig warnings on riscv. These warnings are
due to real dependency issues, so I do not plan to hold the series for those patches to be applied.
Changes since v2:
- drop the separatly submitted riscv/soc patches and rebase on top of the soc tree
- rename CONFIG_ARCH_DMA_ALLOC to ARCH_HAS_DMA_ALLOC
- fix stray CONFIG_ prefixes in a Kconfig file
Changes since v1:
- sort out the dependency mess in RISCV
- don't even built non-coherent DMA support for coldfire cores without data caches
- apply the fec workarounds to all coldfire platforms with data caches
- add a trivial cleanup for m68k dma.c
PATCH 1/8 dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL
CONFIG_DMA_GLOBAL_POOL can't be combined with other DMA coherent allocators. Add dependencies to Kconfig to document this, and make kconfig complain about unment dependencies if someone tries.
--- kernel/dma/Kconfig | 2 ++ 1 file changed, 2 insertions(+)
kernel/dma/Kconfig +2 -0
@@ -135,6 +135,8 @@ config DMA_COHERENT_POOL config DMA_GLOBAL_POOL select DMA_DECLARE_COHERENT + depends on !ARCH_HAS_DMA_SET_UNCACHED + depends on !DMA_DIRECT_REMAP bool config DMA_DIRECT_REMAP
Signature · 1 line
2.39.2
Geert Uytterhoeven
maintainerRe: [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL
s/depdenencies/dependencies/
Quoted 3 lines · Christoph Hellwig
CONFIG_DMA_GLOBAL_POOLcan't be combined with other DMA coherent allocators. Add dependencies toKconfigto document this, and make kconfig complain about unment dependencies if someone tries.
unmet
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Re: [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL
Quoted 8 lines · Geert Uytterhoeven
s/depdenencies/dependencies/
Quoted 3 lines · Christoph Hellwig
CONFIG_DMA_GLOBAL_POOLcan't be combined with other DMA coherent allocators. Add dependencies toKconfigto document this, and make kconfig complain about unment dependencies if someone tries.unmet
Thanks, I've fixed this up, and also removed the line selecting ARCH_USE_DMA_ALLOC which snuk back into the second to last patch after the last rebase and applied the whole series to the dma-mapping tree now.
The RISC-V dependency fixes still haven't made it to the SOC tree, so you will see dependency warnings in linux-next until that gets fixed.
PATCH 2/8 dma-direct: add a CONFIG_ARCH_HAS_DMA_ALLOC symbol
In reply to Christoph HellwigInstead of using arch_dma_alloc if none of the generic coherent allocators are used, require the architectures to explicitly opt into providing it. This will used to deal with the case of m68knommu and coldfire where we can't do any coherent allocations whatsoever, and also makes it clear that arch_dma_alloc is a last resort.
--- arch/arm/Kconfig | 1 + arch/m68k/Kconfig | 1 + arch/parisc/Kconfig | 1 + kernel/dma/Kconfig | 9 +++++++++ kernel/dma/direct.c | 12 ++---------- 5 files changed, 14 insertions(+), 10 deletions(-)
arch/arm/Kconfig +1 -0
@@ -8,6 +8,7 @@ config ARM select ARCH_HAS_CPU_FINALIZE_INIT if MMU select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL if MMU + select ARCH_HAS_DMA_ALLOC if MMU select ARCH_HAS_DMA_WRITE_COMBINE if !ARM_DMA_MEM_BUFFERABLE select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FORTIFY_SOURCE
arch/m68k/Kconfig +1 -0
@@ -6,6 +6,7 @@ config M68K select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_CPU_FINALIZE_INIT if MMU select ARCH_HAS_CURRENT_STACK_POINTER + select ARCH_HAS_DMA_ALLOC if !MMU || COLDFIRE select ARCH_HAS_DMA_PREP_COHERENT if HAS_DMA && MMU && !COLDFIRE select ARCH_HAS_SYNC_DMA_FOR_DEVICE if HAS_DMA select ARCH_HAVE_NMI_SAFE_CMPXCHG if RMW_INSNS
arch/parisc/Kconfig +1 -0
@@ -8,6 +8,7 @@ config PARISC select HAVE_FUNCTION_GRAPH_TRACER select HAVE_SYSCALL_TRACEPOINTS select ARCH_WANT_FRAME_POINTERS + select ARCH_HAS_DMA_ALLOC if PA11 select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_STRICT_KERNEL_RWX select ARCH_HAS_STRICT_MODULE_RWX
kernel/dma/Kconfig +9 -0
@@ -144,6 +144,15 @@ config DMA_DIRECT_REMAP select DMA_COHERENT_POOL select DMA_NONCOHERENT_MMAP +# +# Fallback to arch code for DMA allocations. This should eventually go away. +# +config ARCH_HAS_DMA_ALLOC + depends on !ARCH_HAS_DMA_SET_UNCACHED + depends on !DMA_DIRECT_REMAP + depends on !DMA_GLOBAL_POOL + bool + config DMA_CMA bool "DMA Contiguous Memory Allocator" depends on HAVE_DMA_CONTIGUOUS && CMA
kernel/dma/direct.c +2 -10
@@ -220,13 +220,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,return dma_direct_alloc_no_mapping (dev, size, dma_handle, gfp);if (!dev_is_dma_coherent (dev)) { -/* -* Fallback to the arch handler if it exists. This should -* eventually go away. -*/ -if (!IS_ENABLED (CONFIG_ARCH_HAS_DMA_SET_UNCACHED ) && - !IS_ENABLED (CONFIG_DMA_DIRECT_REMAP ) && - !IS_ENABLED (CONFIG_DMA_GLOBAL_POOL ) && +if (IS_ENABLED (CONFIG_ARCH_HAS_DMA_ALLOC ) && !is_swiotlb_for_alloc (dev))return arch_dma_alloc (dev, size, dma_handle, gfp, attrs);
@@ -330,9 +324,7 @@ void dma_direct_free(struct device *dev, size_t size,return ; } -if (!IS_ENABLED (CONFIG_ARCH_HAS_DMA_SET_UNCACHED ) && - !IS_ENABLED (CONFIG_DMA_DIRECT_REMAP ) && - !IS_ENABLED (CONFIG_DMA_GLOBAL_POOL ) && +if (IS_ENABLED (CONFIG_ARCH_HAS_DMA_ALLOC ) && !dev_is_dma_coherent (dev) && !is_swiotlb_for_alloc (dev)) {arch_dma_free (dev, size, cpu_addr, dma_addr, attrs);
Signature · 1 line
2.39.2
PATCH 3/8 dma-direct: simplify the use atomic pool logic in dma_direct_alloc
In reply to Christoph HellwigThe logic in dma_direct_alloc when to use the atomic pool vs remapping grew a bit unreadable. Consolidate it into a single check, and clean up the set_uncached vs remap logic a bit as well.
--- kernel/dma/direct.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-)
kernel/dma/direct.c +10 -15
@@ -234,27 +234,22 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_handle);/* -* Otherwise remap if the architecture is asking for it. But -* given that remapping memory is a blocking operation we'll -* instead have to dip into the atomic pools. +* Otherwise we require the architecture to either be able to +* mark arbitrary parts of the kernel direct mapping uncached, +* or remapped it uncached. */ + set_uncached =IS_ENABLED (CONFIG_ARCH_HAS_DMA_SET_UNCACHED ); remap =IS_ENABLED (CONFIG_DMA_DIRECT_REMAP ); -if (remap) { -if (dma_direct_use_pool (dev, gfp)) -return dma_direct_alloc_from_pool (dev, size, - dma_handle, gfp); - }else { -if (!IS_ENABLED (CONFIG_ARCH_HAS_DMA_SET_UNCACHED )) -return NULL ; - set_uncached = true; - } +if (!set_uncached && !remap) +return NULL ; }/* -* Decrypting memory may block, so allocate the memory from the atomic -* pools if we can't block. +* Remapping or decrypting memory may block, allocate the memory from +* the atomic pools instead if we aren't allowed block. */ -if (force_dma_unencrypted (dev) &&dma_direct_use_pool (dev, gfp)) +if ((remap ||force_dma_unencrypted (dev)) && +dma_direct_use_pool (dev, gfp))return dma_direct_alloc_from_pool (dev, size, dma_handle, gfp);/* we always manually zero the memory once we are done */
Signature · 1 line
2.39.2
PATCH 4/8 dma-direct: warn when coherent allocations aren't supported
In reply to Christoph HellwigLog a warning once when dma_alloc_coherent fails because the platform does not support coherent allocations at all.
--- kernel/dma/direct.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/dma/direct.c +3 -1
@@ -240,8 +240,10 @@ void *dma_direct_alloc(struct device *dev, size_t size,*/ set_uncached =IS_ENABLED (CONFIG_ARCH_HAS_DMA_SET_UNCACHED ); remap =IS_ENABLED (CONFIG_DMA_DIRECT_REMAP ); -if (!set_uncached && !remap) +if (!set_uncached && !remap) { +pr_warn_once ("coherent DMA allocations not supported on this platform.\n" );return NULL ; + } }/*
Signature · 1 line
2.39.2
PATCH 5/8 m68k: use the coherent DMA code for coldfire without data cache
In reply to Christoph HellwigColdfire cores configured without a data cache are DMA coherent and should thus simply use the simple coherent version of dma-direct.
Introduce a new COLDFIRE_COHERENT_DMA Kconfig symbol as a convenient short hand for such configurations, and a M68K_NONCOHERENT_DMA symbol for all cases where we need to build non-coherent DMA infrastructure to simplify the Kconfig and code conditionals.
Not building the non-coherent DMA code slightly reduces the code size for such configurations.
Numers for m5249evb_defconfig below:
text data bss dec hex filename
2896158 401052 65392 3362602 334f2a vmlinux.before
2895166 400988 65392 3361546 334b0a vmlinux.after
--- arch/m68k/Kconfig | 8 ++++---- arch/m68k/Kconfig.cpu | 12 ++++++++++++ arch/m68k/kernel/Makefile | 2 +- arch/m68k/kernel/dma.c | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-)
arch/m68k/Kconfig +4 -4
@@ -6,16 +6,16 @@ config M68K select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_CPU_FINALIZE_INIT if MMU select ARCH_HAS_CURRENT_STACK_POINTER - select ARCH_HAS_DMA_ALLOC if !MMU || COLDFIRE - select ARCH_HAS_DMA_PREP_COHERENT if HAS_DMA && MMU && !COLDFIRE - select ARCH_HAS_SYNC_DMA_FOR_DEVICE if HAS_DMA + select ARCH_HAS_DMA_ALLOC if M68K_NONCOHERENT_DMA && COLDFIRE + select ARCH_HAS_DMA_PREP_COHERENT if M68K_NONCOHERENT_DMA && !COLDFIRE + select ARCH_HAS_SYNC_DMA_FOR_DEVICE if M68K_NONCOHERENT_DMA select ARCH_HAVE_NMI_SAFE_CMPXCHG if RMW_INSNS select ARCH_MIGHT_HAVE_PC_PARPORT if ISA select ARCH_NO_PREEMPT if !COLDFIRE select ARCH_USE_MEMTEST if MMU_MOTOROLA select ARCH_WANT_IPC_PARSE_VERSION select BINFMT_FLAT_ARGVP_ENVP_ON_STACK - select DMA_DIRECT_REMAP if HAS_DMA && MMU && !COLDFIRE + select DMA_DIRECT_REMAP if M68K_NONCOHERENT_DMA && !COLDFIRE select GENERIC_ATOMIC64 select GENERIC_CPU_DEVICES select GENERIC_IOMAP
arch/m68k/Kconfig.cpu +12 -0
@@ -535,3 +535,15 @@ config CACHE_COPYBACK The ColdFire CPU cache is set into Copy-back mode. endchoice endif # HAVE_CACHE_CB + +# Coldfire cores that do not have a data cache configured can do coherent DMA. +config COLDFIRE_COHERENT_DMA + bool + default y + depends on COLDFIRE + depends on !HAVE_CACHE_CB && !CACHE_D && !CACHE_BOTH + +config M68K_NONCOHERENT_DMA + bool + default y + depends on HAS_DMA && !COLDFIRE_COHERENT_DMA
arch/m68k/kernel/Makefile +1 -1
@@ -23,7 +23,7 @@ obj-$(CONFIG_MMU_MOTOROLA) += ints.o vectors.o obj-$(CONFIG_MMU_SUN3) += ints.o vectors.o obj-$(CONFIG_PCI) += pcibios.o -obj-$(CONFIG_HAS_DMA) += dma.o +obj-$(CONFIG_M68K_NONCOHERENT_DMA) += dma.o obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o obj-$(CONFIG_BOOTINFO_PROC) += bootinfo_proc.o
arch/m68k/kernel/dma.c +1 -1
@@ -17,7 +17,7 @@#include <asm/cacheflush.h> -#if defined(CONFIG_MMU ) && !defined(CONFIG_COLDFIRE ) +#ifndef CONFIG_COLDFIRE void arch_dma_prep_coherent (struct page *page,size_t size) {cache_push (page_to_phys (page), size);
Signature · 1 line
2.39.2
PATCH 6/8 net: fec: use dma_alloc_noncoherent for data cache enabled coldfire
In reply to Christoph HellwigColdfire platforms with data caches can't properly implement dma_alloc_coherent and currently just return noncoherent memory from dma_alloc_coherent.
The fec driver than works around this with a flush of all caches in the receive path. Make this hack a little less bad by using the explicit dma_alloc_noncoherent API and documenting the hacky cache flushes so that the DMA API level hack can be removed.
Also replace the check for CONFIG_M532x for said hack with a check for COLDFIRE && !COLDFIRE_COHERENT_DMA. While m532x is the only such platform with a fec module, this makes the code more consistent and easier to follow.
--- drivers/net/ethernet/freescale/fec_main.c | 86 ++++++++++++++++++++--- 1 file changed, 76 insertions(+), 10 deletions(-)
drivers/net/ethernet/freescale/fec_main.c +76 -10
@@ -406,6 +406,70 @@ static void fec_dump(struct net_device *ndev) }while (bdp != txq->bd.base); } +/* +* Coldfire does not support DMA coherent allocations, and has historically used +* a band-aid with a manual flush in fec_enet_rx_queue. +*/ +#if defined(CONFIG_COLDFIRE ) && !defined(CONFIG_COLDFIRE_COHERENT_DMA ) +static void *fec_dma_alloc (struct device *dev,size_t size,dma_addr_t *handle, +gfp_t gfp) +{ +return dma_alloc_noncoherent (dev, size, handle,DMA_BIDIRECTIONAL , gfp); +} + +static void fec_dma_free (struct device *dev,size_t size,void *cpu_addr, +dma_addr_t handle) +{ +dma_free_noncoherent (dev, size, cpu_addr, handle,DMA_BIDIRECTIONAL ); +} +#else /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */ +static void *fec_dma_alloc (struct device *dev,size_t size,dma_addr_t *handle, +gfp_t gfp) +{ +return dma_alloc_coherent (dev, size, handle, gfp); +} + +static void fec_dma_free (struct device *dev,size_t size,void *cpu_addr, +dma_addr_t handle) +{ +dma_free_coherent (dev, size, cpu_addr, handle); +} +#endif /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */ + +struct fec_dma_devres { +size_t size; +void *vaddr; +dma_addr_t dma_handle; +}; + +static void fec_dmam_release (struct device *dev,void *res) +{ +struct fec_dma_devres *this = res; + +fec_dma_free (dev, this->size, this->vaddr, this->dma_handle); +} + +static void *fec_dmam_alloc (struct device *dev,size_t size,dma_addr_t *handle, +gfp_t gfp) +{ +struct fec_dma_devres *dr; +void *vaddr; + + dr =devres_alloc (fec_dmam_release,sizeof (*dr), gfp); +if (!dr) +return NULL ; + vaddr =fec_dma_alloc (dev, size, handle, gfp); +if (!vaddr) { +devres_free (dr); +return NULL ; + } + dr->vaddr = vaddr; + dr->dma_handle = *handle; + dr->size = size; +devres_add (dev, dr); +return vaddr; +} +static inline bool is_ipv4_pkt (struct sk_buff *skb) {return skb->protocol ==htons (ETH_P_IP ) &&ip_hdr (skb)->version ==4 ;
@@ -1660,7 +1724,11 @@ fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) }#endif -#ifdef CONFIG_M532x +#if defined(CONFIG_COLDFIRE ) && !defined(CONFIG_COLDFIRE_COHERENT_DMA ) +/* +* Hacky flush of all caches instead of using the DMA API for the TSO +* headers. +*/ flush_cache_all ();#endif rxq = fep->rx_queue[queue_id];
@@ -3288,10 +3356,9 @@ static void fec_enet_free_queue(struct net_device *ndev)for (i =0 ; i < fep->num_tx_queues; i++)if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) { txq = fep->tx_queue[i]; -dma_free_coherent (&fep->pdev->dev, - txq->bd.ring_size *TSO_HEADER_SIZE , - txq->tso_hdrs, - txq->tso_hdrs_dma); +fec_dma_free (&fep->pdev->dev, + txq->bd.ring_size *TSO_HEADER_SIZE , + txq->tso_hdrs, txq->tso_hdrs_dma); }for (i =0 ; i < fep->num_rx_queues; i++)
@@ -3321,10 +3388,9 @@ static int fec_enet_alloc_queue(struct net_device *ndev) txq->tx_stop_threshold =FEC_MAX_SKB_DESCS ; txq->tx_wake_threshold =FEC_MAX_SKB_DESCS +2 *MAX_SKB_FRAGS ; - txq->tso_hdrs =dma_alloc_coherent (&fep->pdev->dev, + txq->tso_hdrs =fec_dma_alloc (&fep->pdev->dev, txq->bd.ring_size *TSO_HEADER_SIZE , - &txq->tso_hdrs_dma, -GFP_KERNEL ); + &txq->tso_hdrs_dma,GFP_KERNEL );if (!txq->tso_hdrs) { ret = -ENOMEM ; goto alloc_failed;
@@ -4043,8 +4109,8 @@ static int fec_enet_init(struct net_device *ndev) bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;/* Allocate memory for buffer descriptors. */ - cbd_base =dmam_alloc_coherent (&fep->pdev->dev, bd_size, &bd_dma, -GFP_KERNEL ); + cbd_base =fec_dmam_alloc (&fep->pdev->dev, bd_size, &bd_dma, +GFP_KERNEL );if (!cbd_base) { ret = -ENOMEM ; goto free_queue_mem;
Signature · 1 line
2.39.2
PATCH 7/8 m68k: don't provide arch_dma_alloc for nommu/coldfire
In reply to Christoph HellwigColdfire cores configured with a data cache can't provide coherent DMA allocations at all.
Instead of returning non-coherent kernel memory in this case, return NULL and fail the allocation.
The only driver that used to rely on the previous behavior (fec) has been switched to use non-coherent allocations for this case recently.
--- arch/m68k/kernel/dma.c | 23 ----------------------- 1 file changed, 23 deletions(-)
arch/m68k/kernel/dma.c +0 -23
@@ -33,29 +33,6 @@ pgprot_t pgprot_dmacoherent(pgprot_t prot) }return prot; } -#else -void *arch_dma_alloc (struct device *dev,size_t size,dma_addr_t *dma_handle, -gfp_t gfp,unsigned long attrs) -{ -void *ret; - -if (dev ==NULL || (*dev->dma_mask <0xffffffff )) - gfp |=GFP_DMA ; - ret = (void *)__get_free_pages (gfp,get_order (size)); - -if (ret !=NULL ) { -memset (ret,0 , size); - *dma_handle =virt_to_phys (ret); - } -return ret; -} - -void arch_dma_free (struct device *dev,size_t size,void *vaddr, -dma_addr_t dma_handle,unsigned long attrs) -{ -free_pages ((unsigned long )vaddr,get_order (size)); -} -#endif /* CONFIG_MMU && !CONFIG_COLDFIRE */ void arch_sync_dma_for_device(phys_addr_t handle, size_t size,
Signature · 1 line
2.39.2
PATCH 8/8 m68k: remove unused includes from dma.c
In reply to Christoph Hellwigdma.c doesn't need most of the headers it includes. Also there is no point in undefining the DEBUG symbol given that it isn't used anywhere in this small file.
--- arch/m68k/kernel/dma.c | 9 --------- 1 file changed, 9 deletions(-)
arch/m68k/kernel/dma.c +0 -9
@@ -4,17 +4,8 @@* for more details. */ -#undef DEBUG -#include <linux/dma-map-ops.h> -#include <linux/device.h> #include <linux/kernel.h> -#include <linux/platform_device.h> -#include <linux/scatterlist.h> -#include <linux/slab.h> -#include <linux/vmalloc.h> -#include <linux/export.h> -#include <asm/cacheflush.h> #ifndef CONFIG_COLDFIRE
Signature · 1 line
2.39.2