Skip to contentSkip to conversation

MIPS: dma: random fixes and improvements

PATCH Applied Denis Orlov 2 participants 6 messages Feb 10, 2023 – Feb 13, 2023 MIPS

lore
Share & tools
lore
#0 lore raw

cover MIPS: dma: random fixes and improvements

Denis Orlov (4):
MIPS: dma: fix nullptr handling in dma_free_coherent
MIPS: dma: remove unnecessary ifdefs
MIPS: dma: add arch-specific dma_alloc() implementation
MIPS: dma: simplify source structure

 arch/mips/include/asm/dma-mapping.h | 40 ---------------------------
 arch/mips/include/asm/dma.h         | 43 ++++++++++++++++++++++++++++-
 arch/mips/lib/dma-default.c         |  8 ------
 3 files changed, 42 insertions(+), 49 deletions(-)
 delete mode 100644 arch/mips/include/asm/dma-mapping.h
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/4 MIPS: dma: fix nullptr handling in dma_free_coherent

It is not an error to pass a null pointer to free() and as such it seems that dma_free_coherent() should be able to handle this situation too. Currently, if CONFIG_MMU option is enabled, we would convert this null pointer into a pointer to the beginning of CKSEG0 memory segment before passing it to free(), actually trying to deallocate stuff.

---
 arch/mips/include/asm/dma-mapping.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/mips/include/asm/dma-mapping.h +1 -1
index 8e6ea08168..9f6ec03e3b 100644
@@ -31,7 +31,7 @@ static inline void *dma_alloc_coherent(size_t size, dma_addr_t *dma_handle)
 static inline void dma_free_coherent(void *vaddr, dma_addr_t dma_handle,
 				     size_t size)
 {
-	if (IS_ENABLED(CONFIG_MMU))
+	if (IS_ENABLED(CONFIG_MMU) && vaddr)
 		free((void *)CKSEG0ADDR(vaddr));
 	else
 		free(vaddr);
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/4 MIPS: dma: remove unnecessary ifdefs

In reply to Denis Orlov

We do not support any MIPS CPUs that are not MIPS32/MIPS64, so there is no reason to check for those.

---
 arch/mips/lib/dma-default.c | 8 --------
 1 file changed, 8 deletions(-)
arch/mips/lib/dma-default.c +0 -8
index fbe627c24c..48176e5d28 100644
@@ -6,8 +6,6 @@
 #include <dma.h>
 #include <asm/io.h>
 
-#if defined(CONFIG_CPU_MIPS32) || \
-	defined(CONFIG_CPU_MIPS64)
 static inline void __dma_sync_mips(unsigned long addr, size_t size,
 				   enum dma_data_direction direction)
 {
@@ -28,12 +26,6 @@ static inline void __dma_sync_mips(unsigned long addr, size_t size,
 		BUG();
 	}
 }
-#else
-static inline void __dma_sync_mips(void *addr, size_t size,
-	enum dma_data_direction direction)
-{
-}
-#endif
 
 void dma_sync_single_for_cpu(dma_addr_t address, size_t size,
 			     enum dma_data_direction dir)
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/4 MIPS: dma: add arch-specific dma_alloc() implementation

In reply to Denis Orlov

DMA allocations should be aligned on a cache line size, at least on cache non-coherent MIPS systems. Instead of using some hardcoded value for an alignment from a generic implementation (which may be wrong for us), we can get cache info from 'current_cpu_data' variable, so use it.

---
 arch/mips/include/asm/dma.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/mips/include/asm/dma.h +12 -0
index e0b4689172..49eeaac1a2 100644
@@ -6,6 +6,18 @@
 #ifndef __ASM_DMA_H
 #define __ASM_DMA_H
 
+#include <common.h>
+#include <xfuncs.h>
+#include <asm/cpu-info.h>
+
+#define dma_alloc dma_alloc
+static inline void *dma_alloc(size_t size)
+{
+	unsigned long max_linesz = max(current_cpu_data.dcache.linesz,
+				       current_cpu_data.scache.linesz);
+	return xmemalign(max_linesz, ALIGN(size, max_linesz));
+}
+
 #include "asm/dma-mapping.h"
 
 #endif /* __ASM_DMA_H */
Signature · 1 line
2.30.2
#4 lore raw

PATCH 4/4 MIPS: dma: simplify source structure

In reply to Denis Orlov

There is no reason to keep code from 'dma-mapping.h' in a separate file, so merge it into 'dma.h'.

---
 arch/mips/include/asm/dma-mapping.h | 40 -----------------------------
 arch/mips/include/asm/dma.h         | 31 +++++++++++++++++++++-
 2 files changed, 30 insertions(+), 41 deletions(-)
 delete mode 100644 arch/mips/include/asm/dma-mapping.h
arch/mips/include/asm/dma-mapping.h deleted +0 -40
deleted file mode 100644
index 9f6ec03e3b..0000000000
@@ -1,40 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-
-#ifndef _ASM_DMA_MAPPING_H
-#define _ASM_DMA_MAPPING_H
-
-#include <common.h>
-#include <xfuncs.h>
-#include <asm/addrspace.h>
-#include <asm/types.h>
-#include <malloc.h>
-#include <asm/io.h>
-
-#define dma_alloc_coherent dma_alloc_coherent
-static inline void *dma_alloc_coherent(size_t size, dma_addr_t *dma_handle)
-{
-	void *ret;
-
-	ret = xmemalign(PAGE_SIZE, size);
-
-	memset(ret, 0, size);
-
-	if (dma_handle)
-		*dma_handle = CPHYSADDR(ret);
-
-	dma_flush_range((unsigned long)ret, (unsigned long)(ret + size));
-
-	return (void *)CKSEG1ADDR(ret);
-}
-
-#define dma_free_coherent dma_free_coherent
-static inline void dma_free_coherent(void *vaddr, dma_addr_t dma_handle,
-				     size_t size)
-{
-	if (IS_ENABLED(CONFIG_MMU) && vaddr)
-		free((void *)CKSEG0ADDR(vaddr));
-	else
-		free(vaddr);
-}
-
-#endif /* _ASM_DMA_MAPPING_H */
arch/mips/include/asm/dma.h +30 -1
index 49eeaac1a2..62d9c7c548 100644
@@ -7,8 +7,12 @@
 #define __ASM_DMA_H
 
 #include <common.h>
+#include <malloc.h>
 #include <xfuncs.h>
+#include <asm/addrspace.h>
 #include <asm/cpu-info.h>
+#include <asm/io.h>
+#include <asm/types.h>
 
 #define dma_alloc dma_alloc
 static inline void *dma_alloc(size_t size)
@@ -18,6 +22,31 @@ static inline void *dma_alloc(size_t size)
 	return xmemalign(max_linesz, ALIGN(size, max_linesz));
 }
 
-#include "asm/dma-mapping.h"
+#define dma_alloc_coherent dma_alloc_coherent
+static inline void *dma_alloc_coherent(size_t size, dma_addr_t *dma_handle)
+{
+	void *ret;
+
+	ret = xmemalign(PAGE_SIZE, size);
+
+	memset(ret, 0, size);
+
+	if (dma_handle)
+		*dma_handle = CPHYSADDR(ret);
+
+	dma_flush_range((unsigned long)ret, (unsigned long)(ret + size));
+
+	return (void *)CKSEG1ADDR(ret);
+}
+
+#define dma_free_coherent dma_free_coherent
+static inline void dma_free_coherent(void *vaddr, dma_addr_t dma_handle,
+				     size_t size)
+{
+	if (IS_ENABLED(CONFIG_MMU) && vaddr)
+		free((void *)CKSEG0ADDR(vaddr));
+	else
+		free(vaddr);
+}
 
 #endif /* __ASM_DMA_H */
Signature · 1 line
2.30.2
Applied
#5 lore raw
In reply to Denis Orlov
Quoted 11 lines · Denis Orlov

Denis Orlov (4):
MIPS: dma: fix nullptr handling in dma_free_coherent
MIPS: dma: remove unnecessary ifdefs
MIPS: dma: add arch-specific dma_alloc() implementation
MIPS: dma: simplify source structure

 arch/mips/include/asm/dma-mapping.h | 40 ---------------------------
 arch/mips/include/asm/dma.h         | 43 ++++++++++++++++++++++++++++-
 arch/mips/lib/dma-default.c         |  8 ------
 3 files changed, 42 insertions(+), 49 deletions(-)
 delete mode 100644 arch/mips/include/asm/dma-mapping.h

Applied, thanks

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |