[PATCH v2 3/5] kvx: Implement dma handling primitives

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Yann Sionneau <ysionneau@xxxxxxxxx>

Signed-off-by: Jules Maselbas <jmaselbas@xxxxxxxxx>
---
changes v1 -> v2:
 - add BUILD_BUG_ON_MSG in dma_alloc_coherent
 - renamed kvx_dcache_invalidate_mem_area to invalidate_dcache_range
 - add comment on dcache beeing write-through
---
 arch/kvx/Kconfig                |  1 +
 arch/kvx/include/asm/dma.h      | 32 +++++++++++
 arch/kvx/include/asm/sys_arch.h |  3 ++
 arch/kvx/lib/Makefile           |  2 +-
 arch/kvx/lib/dma-default.c      | 94 +++++++++++++++++++++++++++++++++
 5 files changed, 131 insertions(+), 1 deletion(-)
 create mode 100644 arch/kvx/include/asm/dma.h
 create mode 100644 arch/kvx/lib/dma-default.c

diff --git a/arch/kvx/Kconfig b/arch/kvx/Kconfig
index 4e02613ec..093444088 100644
--- a/arch/kvx/Kconfig
+++ b/arch/kvx/Kconfig
@@ -13,6 +13,7 @@ config KVX
 	select GENERIC_FIND_NEXT_BIT
 	select HAS_ARCH_SJLJ
 	select HAS_CACHE
+	select HAS_DMA
 	select LIBFDT
 	select MFD_SYSCON
 	select OF_BAREBOX_DRIVERS
diff --git a/arch/kvx/include/asm/dma.h b/arch/kvx/include/asm/dma.h
new file mode 100644
index 000000000..a7ecf279a
--- /dev/null
+++ b/arch/kvx/include/asm/dma.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* SPDX-FileCopyrightText: 2021 Yann Sionneau <ysionneau@xxxxxxxxx>, Kalray Inc. */
+
+#ifndef __ASM_DMA_H
+#define __ASM_DMA_H
+
+#include <common.h>
+
+#define KVX_DDR_32BIT_RAM_WINDOW_BA	(0x80000000ULL)
+#define KVX_DDR_64BIT_RAM_WINDOW_BA	(0x100000000ULL)
+#define MAX_32BIT_ADDR			(0xffffffffULL)
+
+#define dma_alloc dma_alloc
+static inline void *dma_alloc(size_t size)
+{
+	return xmemalign(64, ALIGN(size, 64));
+}
+
+static inline void *dma_alloc_coherent(size_t size, dma_addr_t *dma_handle)
+{
+	BUILD_BUG_ON_MSG(1, "dma_alloc_coherent not supported: "
+			"MMU support is required to map uncached pages");
+	return NULL;
+}
+
+static inline void dma_free_coherent(void *mem, dma_addr_t dma_handle,
+				     size_t size)
+{
+	free(mem);
+}
+
+#endif /* __ASM_DMA_H */
diff --git a/arch/kvx/include/asm/sys_arch.h b/arch/kvx/include/asm/sys_arch.h
index 9df32c4e7..ce07a5598 100644
--- a/arch/kvx/include/asm/sys_arch.h
+++ b/arch/kvx/include/asm/sys_arch.h
@@ -11,6 +11,9 @@
 #define EXCEPTION_STRIDE	0x40
 #define EXCEPTION_ALIGNMENT	0x100
 
+#define kvx_cluster_id() ((int) \
+	((kvx_sfr_get(PCR) & KVX_SFR_PCR_CID_MASK) \
+					>> KVX_SFR_PCR_CID_SHIFT))
 #define KVX_SFR_START(__sfr_reg) \
 	(KVX_SFR_## __sfr_reg ## _SHIFT)
 
diff --git a/arch/kvx/lib/Makefile b/arch/kvx/lib/Makefile
index d271ebccf..c730e1c23 100644
--- a/arch/kvx/lib/Makefile
+++ b/arch/kvx/lib/Makefile
@@ -3,4 +3,4 @@
 # Copyright (C) 2019 Kalray Inc.
 #
 
-obj-y	+= cpuinfo.o board.o dtb.o poweroff.o bootm.o setjmp.o cache.o
+obj-y	+= cpuinfo.o board.o dtb.o poweroff.o bootm.o setjmp.o cache.o dma-default.o
diff --git a/arch/kvx/lib/dma-default.c b/arch/kvx/lib/dma-default.c
new file mode 100644
index 000000000..2a4144696
--- /dev/null
+++ b/arch/kvx/lib/dma-default.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: 2021 Yann Sionneau <ysionneau@xxxxxxxxx>, Kalray Inc.
+
+#include <dma.h>
+#include <asm/barrier.h>
+#include <asm/io.h>
+#include <asm/cache.h>
+#include <asm/sfr.h>
+#include <asm/sys_arch.h>
+
+/*
+ * The implementation of arch should follow the following rules:
+ *		map		for_cpu		for_device	unmap
+ * TO_DEV	writeback	none		writeback	none
+ * FROM_DEV	invalidate	invalidate(*)	invalidate	invalidate(*)
+ * BIDIR	writeback	invalidate	writeback	invalidate
+ *
+ * (*) - only necessary if the CPU speculatively prefetches.
+ *
+ * (see https://lkml.org/lkml/2018/5/18/979)
+ */
+
+void dma_sync_single_for_device(dma_addr_t addr, size_t size,
+				enum dma_data_direction dir)
+{
+	/* dcache is Write-Through: no need to flush to force writeback */
+	switch (dir) {
+	case DMA_FROM_DEVICE:
+		invalidate_dcache_range(addr, addr + size);
+		break;
+	case DMA_TO_DEVICE:
+	case DMA_BIDIRECTIONAL:
+		/* allow device to read buffer written by CPU */
+		wmb();
+		break;
+	default:
+		BUG();
+	}
+}
+
+void dma_sync_single_for_cpu(dma_addr_t addr, size_t size,
+				enum dma_data_direction dir)
+{
+	/* CPU does not speculatively prefetches */
+	switch (dir) {
+	case DMA_FROM_DEVICE:
+		/* invalidate has been done during map/for_device */
+	case DMA_TO_DEVICE:
+		break;
+	case DMA_BIDIRECTIONAL:
+		invalidate_dcache_range(addr, addr + size);
+		break;
+	default:
+		BUG();
+	}
+}
+
+#define KVX_DDR_ALIAS_OFFSET \
+	(KVX_DDR_64BIT_RAM_WINDOW_BA - KVX_DDR_32BIT_RAM_WINDOW_BA)
+#define KVX_DDR_ALIAS_WINDOW \
+	(KVX_DDR_64BIT_RAM_WINDOW_BA + KVX_DDR_ALIAS_OFFSET)
+
+/* Local smem is aliased between 0 and 16MB */
+#define KVX_SMEM_LOCAL_ALIAS 0x1000000ULL
+
+dma_addr_t dma_map_single(struct device_d *dev, void *ptr, size_t size,
+			  enum dma_data_direction dir)
+{
+	uintptr_t addr = (uintptr_t) ptr;
+
+	dma_sync_single_for_device(addr, size, dir);
+
+	/* Local smem alias should never be used for dma */
+	if (addr < KVX_SMEM_LOCAL_ALIAS)
+		return addr + (1 + kvx_cluster_id()) * KVX_SMEM_LOCAL_ALIAS;
+
+	if (dev->dma_mask && addr <= dev->dma_mask)
+		return addr;
+
+	if (addr >= KVX_DDR_ALIAS_WINDOW)
+		return DMA_ERROR_CODE;
+
+	addr -= KVX_DDR_ALIAS_OFFSET;
+	if (dev->dma_mask && addr > dev->dma_mask)
+		return DMA_ERROR_CODE;
+
+	return addr;
+}
+
+void dma_unmap_single(struct device_d *dev, dma_addr_t addr, size_t size,
+		      enum dma_data_direction dir)
+{
+	dma_sync_single_for_cpu(addr, size, dir);
+}
-- 
2.17.1



_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux