* [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
2023-10-20 7:18 ` Geert Uytterhoeven
2023-10-20 5:40 ` [PATCH 2/8] dma-direct: add a CONFIG_ARCH_HAS_DMA_ALLOC symbol Christoph Hellwig
` (6 subsequent siblings)
7 siblings, 1 reply; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev,
Lad Prabhakar
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.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
kernel/dma/Kconfig | 2 ++
1 file changed, 2 insertions(+)
diff --git a/kernel/dma/Kconfig b/kernel/dma/Kconfig
index f488997b071712..4524db877eba36 100644
--- a/kernel/dma/Kconfig
+++ b/kernel/dma/Kconfig
@@ -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
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL
2023-10-20 5:40 ` [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL Christoph Hellwig
@ 2023-10-20 7:18 ` Geert Uytterhoeven
2023-10-23 5:50 ` Christoph Hellwig
0 siblings, 1 reply; 11+ messages in thread
From: Geert Uytterhoeven @ 2023-10-20 7:18 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Greg Ungerer, iommu, Robin Murphy, Marek Szyprowski, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev,
Lad Prabhakar
s/depdenencies/dependencies/
On Fri, Oct 20, 2023 at 7:40 AM Christoph Hellwig <hch@lst.de> wrote:
> 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.
unmet
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
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
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL
2023-10-20 7:18 ` Geert Uytterhoeven
@ 2023-10-23 5:50 ` Christoph Hellwig
0 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-23 5:50 UTC (permalink / raw)
To: Geert Uytterhoeven
Cc: Christoph Hellwig, Greg Ungerer, iommu, Robin Murphy,
Marek Szyprowski, Wei Fang, Shenwei Wang, Clark Wang,
NXP Linux Team, linux-m68k, netdev, Lad Prabhakar
On Fri, Oct 20, 2023 at 09:18:32AM +0200, Geert Uytterhoeven wrote:
> s/depdenencies/dependencies/
>
> On Fri, Oct 20, 2023 at 7:40 AM Christoph Hellwig <hch@lst.de> wrote:
> > 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.
>
> 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.
^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH 2/8] dma-direct: add a CONFIG_ARCH_HAS_DMA_ALLOC symbol
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
2023-10-20 5:40 ` [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
2023-10-20 5:40 ` [PATCH 3/8] dma-direct: simplify the use atomic pool logic in dma_direct_alloc Christoph Hellwig
` (5 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev
Instead 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.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
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(-)
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 9557808e8937b1..f8567e95f98bef 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -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
diff --git a/arch/m68k/Kconfig b/arch/m68k/Kconfig
index 3e318bf9504c5b..4f3e7dec2171c8 100644
--- a/arch/m68k/Kconfig
+++ b/arch/m68k/Kconfig
@@ -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
diff --git a/arch/parisc/Kconfig b/arch/parisc/Kconfig
index a15ab147af2e07..46e8e4ea7a57de 100644
--- a/arch/parisc/Kconfig
+++ b/arch/parisc/Kconfig
@@ -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
diff --git a/kernel/dma/Kconfig b/kernel/dma/Kconfig
index 4524db877eba36..d62f5957f36be7 100644
--- a/kernel/dma/Kconfig
+++ b/kernel/dma/Kconfig
@@ -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
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 9596ae1aa0dacf..c078090cd38ecc 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -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);
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 3/8] dma-direct: simplify the use atomic pool logic in dma_direct_alloc
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
2023-10-20 5:40 ` [PATCH 1/8] dma-direct: add depdenencies to CONFIG_DMA_GLOBAL_POOL Christoph Hellwig
2023-10-20 5:40 ` [PATCH 2/8] dma-direct: add a CONFIG_ARCH_HAS_DMA_ALLOC symbol Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
2023-10-20 5:40 ` [PATCH 4/8] dma-direct: warn when coherent allocations aren't supported Christoph Hellwig
` (4 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev
The 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.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
kernel/dma/direct.c | 25 ++++++++++---------------
1 file changed, 10 insertions(+), 15 deletions(-)
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index c078090cd38ecc..9657ef7c055eaa 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -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 */
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 4/8] dma-direct: warn when coherent allocations aren't supported
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
` (2 preceding siblings ...)
2023-10-20 5:40 ` [PATCH 3/8] dma-direct: simplify the use atomic pool logic in dma_direct_alloc Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
2023-10-20 5:40 ` [PATCH 5/8] m68k: use the coherent DMA code for coldfire without data cache Christoph Hellwig
` (3 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev
Log a warning once when dma_alloc_coherent fails because the platform
does not support coherent allocations at all.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
kernel/dma/direct.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 9657ef7c055eaa..ed3056eb20b8bd 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -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;
+ }
}
/*
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 5/8] m68k: use the coherent DMA code for coldfire without data cache
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
` (3 preceding siblings ...)
2023-10-20 5:40 ` [PATCH 4/8] dma-direct: warn when coherent allocations aren't supported Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
2023-10-20 5:40 ` [PATCH 6/8] net: fec: use dma_alloc_noncoherent for data cache enabled coldfire Christoph Hellwig
` (2 subsequent siblings)
7 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev
Coldfire 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
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
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(-)
diff --git a/arch/m68k/Kconfig b/arch/m68k/Kconfig
index 4f3e7dec2171c8..50ada24dfbbadf 100644
--- a/arch/m68k/Kconfig
+++ b/arch/m68k/Kconfig
@@ -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
diff --git a/arch/m68k/Kconfig.cpu b/arch/m68k/Kconfig.cpu
index b826e9c677b2ae..ad69b466a08bd1 100644
--- a/arch/m68k/Kconfig.cpu
+++ b/arch/m68k/Kconfig.cpu
@@ -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
diff --git a/arch/m68k/kernel/Makefile b/arch/m68k/kernel/Makefile
index af015447dfb4c1..01fb69a5095f43 100644
--- a/arch/m68k/kernel/Makefile
+++ b/arch/m68k/kernel/Makefile
@@ -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
diff --git a/arch/m68k/kernel/dma.c b/arch/m68k/kernel/dma.c
index 2e192a5df949bb..f83870cfa79b37 100644
--- a/arch/m68k/kernel/dma.c
+++ b/arch/m68k/kernel/dma.c
@@ -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);
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 6/8] net: fec: use dma_alloc_noncoherent for data cache enabled coldfire
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
` (4 preceding siblings ...)
2023-10-20 5:40 ` [PATCH 5/8] m68k: use the coherent DMA code for coldfire without data cache Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
2023-10-20 5:40 ` [PATCH 7/8] m68k: don't provide arch_dma_alloc for nommu/coldfire Christoph Hellwig
2023-10-20 5:40 ` [PATCH 8/8] m68k: remove unused includes from dma.c Christoph Hellwig
7 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev
Coldfire 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.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
drivers/net/ethernet/freescale/fec_main.c | 86 ++++++++++++++++++++---
1 file changed, 76 insertions(+), 10 deletions(-)
diff --git a/drivers/net/ethernet/freescale/fec_main.c b/drivers/net/ethernet/freescale/fec_main.c
index 77c8e9cfb44562..c5e370570f6ec6 100644
--- a/drivers/net/ethernet/freescale/fec_main.c
+++ b/drivers/net/ethernet/freescale/fec_main.c
@@ -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;
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 7/8] m68k: don't provide arch_dma_alloc for nommu/coldfire
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
` (5 preceding siblings ...)
2023-10-20 5:40 ` [PATCH 6/8] net: fec: use dma_alloc_noncoherent for data cache enabled coldfire Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
2023-10-20 5:40 ` [PATCH 8/8] m68k: remove unused includes from dma.c Christoph Hellwig
7 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev
Coldfire 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.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
arch/m68k/kernel/dma.c | 23 -----------------------
1 file changed, 23 deletions(-)
diff --git a/arch/m68k/kernel/dma.c b/arch/m68k/kernel/dma.c
index f83870cfa79b37..eef63d032abb53 100644
--- a/arch/m68k/kernel/dma.c
+++ b/arch/m68k/kernel/dma.c
@@ -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,
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread* [PATCH 8/8] m68k: remove unused includes from dma.c
2023-10-20 5:40 fix the non-coherent coldfire dma_alloc_coherent v4 Christoph Hellwig
` (6 preceding siblings ...)
2023-10-20 5:40 ` [PATCH 7/8] m68k: don't provide arch_dma_alloc for nommu/coldfire Christoph Hellwig
@ 2023-10-20 5:40 ` Christoph Hellwig
7 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2023-10-20 5:40 UTC (permalink / raw)
To: Greg Ungerer, iommu
Cc: Robin Murphy, Marek Szyprowski, Geert Uytterhoeven, Wei Fang,
Shenwei Wang, Clark Wang, NXP Linux Team, linux-m68k, netdev
dma.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.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Geert Uytterhoeven <geert@linux-m68k.org>
Reviewed-by: Greg Ungerer <gerg@linux-m68k.org>
Acked-by: Geert Uytterhoeven <geert@linux-m68k.org>
Tested-by: Greg Ungerer <gerg@linux-m68k.org>
---
arch/m68k/kernel/dma.c | 9 ---------
1 file changed, 9 deletions(-)
diff --git a/arch/m68k/kernel/dma.c b/arch/m68k/kernel/dma.c
index eef63d032abb53..16063783aa80c6 100644
--- a/arch/m68k/kernel/dma.c
+++ b/arch/m68k/kernel/dma.c
@@ -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
--
2.39.2
^ permalink raw reply related [flat|nested] 11+ messages in thread