mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] Consolidate IO memcpy functions
@ 2024-09-06 11:41 Julian Vetter
  2024-09-06 11:41 ` [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib Julian Vetter
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Julian Vetter @ 2024-09-06 11:41 UTC (permalink / raw)
  To: Arnd Bergmann, Catalin Marinas, Will Deacon, Guo Ren,
	Huacai Chen, WANG Xuerui, Andrew Morton
  Cc: linux-arm-kernel, linux-kernel, linux-csky, loongarch,
	Yann Sionneau, Julian Vetter

Several architectures have almost the same implementation
for__memcpy_toio, __memcpy_fromio and memset_io. So, this patch series
consolidates them all into a single libs/io.c file.

Julian Vetter (4):
  Consolidate __memcpy_{to,from}io and __memset_io into a single lib
  Activate GENERIC_IO for the arm64 architecture
  Activate GENERIC_IO for the csky architecture
  Activate GENERIC_IO for the loongarch architecture

 arch/arm64/Kconfig             |   1 +
 arch/arm64/kernel/io.c         |  87 ----------------------------
 arch/csky/Kconfig              |   1 +
 arch/csky/kernel/Makefile      |   2 +-
 arch/csky/kernel/io.c          |  91 -----------------------------
 arch/loongarch/Kconfig         |   1 +
 arch/loongarch/kernel/Makefile |   2 +-
 arch/loongarch/kernel/io.c     |  94 ------------------------------
 lib/Kconfig                    |   3 +
 lib/Makefile                   |   2 +
 lib/io.c                       | 101 +++++++++++++++++++++++++++++++++
 11 files changed, 111 insertions(+), 274 deletions(-)
 delete mode 100644 arch/csky/kernel/io.c
 delete mode 100644 arch/loongarch/kernel/io.c
 create mode 100644 lib/io.c

-- 
2.34.1






^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib
  2024-09-06 11:41 [PATCH 0/4] Consolidate IO memcpy functions Julian Vetter
@ 2024-09-06 11:41 ` Julian Vetter
  2024-09-09 10:35   ` Arnd Bergmann
  2024-09-06 11:41 ` [PATCH 2/4] Activate GENERIC_IO for the arm64 architecture Julian Vetter
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Julian Vetter @ 2024-09-06 11:41 UTC (permalink / raw)
  To: Arnd Bergmann, Catalin Marinas, Will Deacon, Guo Ren,
	Huacai Chen, WANG Xuerui, Andrew Morton
  Cc: linux-arm-kernel, linux-kernel, linux-csky, loongarch,
	Yann Sionneau, Julian Vetter

Various architectures have almost the same implementations for
__memcpy_{to,from}io and __memset_io functions. So, consolidate them and
introduce a CONFIG_GENERIC_IO flag to build the given lib/io.c.

Reviewed-by: Yann Sionneau <ysionneau@kalrayinc.com>
Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>
---
 lib/Kconfig  |   3 ++
 lib/Makefile |   2 +
 lib/io.c     | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+)
 create mode 100644 lib/io.c

diff --git a/lib/Kconfig b/lib/Kconfig
index b38849af6f13..2ebdc41cf0a1 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -765,6 +765,9 @@ config GENERIC_LIB_UCMPDI2
 config GENERIC_LIB_DEVMEM_IS_ALLOWED
 	bool
 
+config GENERIC_IO
+	bool
+
 config PLDMFW
 	bool
 	default n
diff --git a/lib/Makefile b/lib/Makefile
index 322bb127b4dc..d231a1c49715 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -135,6 +135,8 @@ obj-$(CONFIG_HAS_IOMEM) += iomap_copy.o devres.o
 obj-$(CONFIG_CHECK_SIGNATURE) += check_signature.o
 obj-$(CONFIG_DEBUG_LOCKING_API_SELFTESTS) += locking-selftest.o
 
+obj-$(CONFIG_GENERIC_IO) += io.o
+
 lib-y += logic_pio.o
 
 lib-$(CONFIG_INDIRECT_IOMEM) += logic_iomem.o
diff --git a/lib/io.c b/lib/io.c
new file mode 100644
index 000000000000..c3385566a3e1
--- /dev/null
+++ b/lib/io.c
@@ -0,0 +1,101 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * derived from arch/arm/kernel/io.c
+ *
+ * Copyright (C) 2024 Kalray Inc.
+ * Author(s): Julian Vetter
+ */
+
+#include <linux/export.h>
+#include <linux/types.h>
+#include <linux/io.h>
+
+#define NATIVE_STORE_SIZE	(BITS_PER_LONG/8)
+
+#if IS_ENABLED(CONFIG_64BIT)
+#define __raw_write_native(val, dst) __raw_writeq(val, dst)
+#define __raw_read_native(src) __raw_readq((src))
+#else
+#define __raw_write_native(val, dst) __raw_writel(val, dst)
+#define __raw_read_native(src) __raw_readl(src)
+#endif
+
+void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
+{
+	while (count && !IS_ALIGNED((unsigned long)from, NATIVE_STORE_SIZE)) {
+		*(u8 *)to = __raw_readb(from);
+		from++;
+		to++;
+		count--;
+	}
+
+	while (count >= NATIVE_STORE_SIZE) {
+		*(uintptr_t *)to = __raw_read_native(from);
+		from += NATIVE_STORE_SIZE;
+		to += NATIVE_STORE_SIZE;
+		count -= NATIVE_STORE_SIZE;
+	}
+
+	while (count) {
+		*(u8 *)to = __raw_readb(from);
+		from++;
+		to++;
+		count--;
+	}
+}
+EXPORT_SYMBOL(__memcpy_fromio);
+
+void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
+{
+	while (count && !IS_ALIGNED((unsigned long)to, NATIVE_STORE_SIZE)) {
+		__raw_writeb(*(u8 *)from, to);
+		from++;
+		to++;
+		count--;
+	}
+
+	while (count >= NATIVE_STORE_SIZE) {
+		__raw_write_native(*(uintptr_t *)from, to);
+		from += NATIVE_STORE_SIZE;
+		to += NATIVE_STORE_SIZE;
+		count -= NATIVE_STORE_SIZE;
+	}
+
+	while (count) {
+		__raw_writeb(*(u8 *)from, to);
+		from++;
+		to++;
+		count--;
+	}
+}
+EXPORT_SYMBOL(__memcpy_toio);
+
+void __memset_io(volatile void __iomem *dst, int c, size_t count)
+{
+	uintptr_t qc = (u8)c;
+
+	qc |= qc << 8;
+	qc |= qc << 16;
+#if IS_ENABLED(CONFIG_64BIT)
+	qc |= qc << 32;
+#endif
+
+	while (count && !IS_ALIGNED((unsigned long)dst, NATIVE_STORE_SIZE)) {
+		__raw_writeb(c, dst);
+		dst++;
+		count--;
+	}
+
+	while (count >= NATIVE_STORE_SIZE) {
+		__raw_write_native(qc, dst);
+		dst += NATIVE_STORE_SIZE;
+		count -= NATIVE_STORE_SIZE;
+	}
+
+	while (count) {
+		__raw_writeb(c, dst);
+		dst++;
+		count--;
+	}
+}
+EXPORT_SYMBOL(__memset_io);
-- 
2.34.1






^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/4] Activate GENERIC_IO for the arm64 architecture
  2024-09-06 11:41 [PATCH 0/4] Consolidate IO memcpy functions Julian Vetter
  2024-09-06 11:41 ` [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib Julian Vetter
@ 2024-09-06 11:41 ` Julian Vetter
  2024-09-06 11:41 ` [PATCH 3/4] Activate GENERIC_IO for the csky architecture Julian Vetter
  2024-09-06 11:41 ` [PATCH 4/4] Activate GENERIC_IO for the loongarch architecture Julian Vetter
  3 siblings, 0 replies; 7+ messages in thread
From: Julian Vetter @ 2024-09-06 11:41 UTC (permalink / raw)
  To: Arnd Bergmann, Catalin Marinas, Will Deacon, Guo Ren,
	Huacai Chen, WANG Xuerui, Andrew Morton
  Cc: linux-arm-kernel, linux-kernel, linux-csky, loongarch,
	Yann Sionneau, Julian Vetter

Use the generic __memcpy_{from,to}io and __memset_io for the arm64
processor architecture.

Reviewed-by: Yann Sionneau <ysionneau@kalrayinc.com>
Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>
---
 arch/arm64/Kconfig     |  1 +
 arch/arm64/kernel/io.c | 87 ------------------------------------------
 2 files changed, 1 insertion(+), 87 deletions(-)

diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index a2f8ff354ca6..3e5647597a03 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -140,6 +140,7 @@ config ARM64
 	select GENERIC_CPU_VULNERABILITIES
 	select GENERIC_EARLY_IOREMAP
 	select GENERIC_IDLE_POLL_SETUP
+	select GENERIC_IO
 	select GENERIC_IOREMAP
 	select GENERIC_IRQ_IPI
 	select GENERIC_IRQ_PROBE
diff --git a/arch/arm64/kernel/io.c b/arch/arm64/kernel/io.c
index ef48089fbfe1..fe86ada23c7d 100644
--- a/arch/arm64/kernel/io.c
+++ b/arch/arm64/kernel/io.c
@@ -9,34 +9,6 @@
 #include <linux/types.h>
 #include <linux/io.h>
 
-/*
- * Copy data from IO memory space to "real" memory space.
- */
-void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
-{
-	while (count && !IS_ALIGNED((unsigned long)from, 8)) {
-		*(u8 *)to = __raw_readb(from);
-		from++;
-		to++;
-		count--;
-	}
-
-	while (count >= 8) {
-		*(u64 *)to = __raw_readq(from);
-		from += 8;
-		to += 8;
-		count -= 8;
-	}
-
-	while (count) {
-		*(u8 *)to = __raw_readb(from);
-		from++;
-		to++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memcpy_fromio);
-
 /*
  * This generates a memcpy that works on a from/to address which is aligned to
  * bits. Count is in terms of the number of bits sized quantities to copy. It
@@ -78,62 +50,3 @@ void __iowrite32_copy_full(void __iomem *to, const void *from, size_t count)
 	dgh();
 }
 EXPORT_SYMBOL(__iowrite32_copy_full);
-
-/*
- * Copy data from "real" memory space to IO memory space.
- */
-void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
-{
-	while (count && !IS_ALIGNED((unsigned long)to, 8)) {
-		__raw_writeb(*(u8 *)from, to);
-		from++;
-		to++;
-		count--;
-	}
-
-	while (count >= 8) {
-		__raw_writeq(*(u64 *)from, to);
-		from += 8;
-		to += 8;
-		count -= 8;
-	}
-
-	while (count) {
-		__raw_writeb(*(u8 *)from, to);
-		from++;
-		to++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memcpy_toio);
-
-/*
- * "memset" on IO memory space.
- */
-void __memset_io(volatile void __iomem *dst, int c, size_t count)
-{
-	u64 qc = (u8)c;
-
-	qc |= qc << 8;
-	qc |= qc << 16;
-	qc |= qc << 32;
-
-	while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
-		__raw_writeb(c, dst);
-		dst++;
-		count--;
-	}
-
-	while (count >= 8) {
-		__raw_writeq(qc, dst);
-		dst += 8;
-		count -= 8;
-	}
-
-	while (count) {
-		__raw_writeb(c, dst);
-		dst++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memset_io);
-- 
2.34.1






^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/4] Activate GENERIC_IO for the csky architecture
  2024-09-06 11:41 [PATCH 0/4] Consolidate IO memcpy functions Julian Vetter
  2024-09-06 11:41 ` [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib Julian Vetter
  2024-09-06 11:41 ` [PATCH 2/4] Activate GENERIC_IO for the arm64 architecture Julian Vetter
@ 2024-09-06 11:41 ` Julian Vetter
  2024-09-09  5:38   ` Guo Ren
  2024-09-06 11:41 ` [PATCH 4/4] Activate GENERIC_IO for the loongarch architecture Julian Vetter
  3 siblings, 1 reply; 7+ messages in thread
From: Julian Vetter @ 2024-09-06 11:41 UTC (permalink / raw)
  To: Arnd Bergmann, Catalin Marinas, Will Deacon, Guo Ren,
	Huacai Chen, WANG Xuerui, Andrew Morton
  Cc: linux-arm-kernel, linux-kernel, linux-csky, loongarch,
	Yann Sionneau, Julian Vetter

Use the generic __memcpy_{from,to}io and __memset_io for the csky
processor architecture.

Reviewed-by: Yann Sionneau <ysionneau@kalrayinc.com>
Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>
---
 arch/csky/Kconfig         |  1 +
 arch/csky/kernel/Makefile |  2 +-
 arch/csky/kernel/io.c     | 91 ---------------------------------------
 3 files changed, 2 insertions(+), 92 deletions(-)
 delete mode 100644 arch/csky/kernel/io.c

diff --git a/arch/csky/Kconfig b/arch/csky/Kconfig
index 5479707eb5d1..d64329b691f7 100644
--- a/arch/csky/Kconfig
+++ b/arch/csky/Kconfig
@@ -48,6 +48,7 @@ config CSKY
 	select DMA_DIRECT_REMAP
 	select IRQ_DOMAIN
 	select DW_APB_TIMER_OF
+	select GENERIC_IO
 	select GENERIC_IOREMAP
 	select GENERIC_LIB_ASHLDI3
 	select GENERIC_LIB_ASHRDI3
diff --git a/arch/csky/kernel/Makefile b/arch/csky/kernel/Makefile
index 8a868316b912..de1c3472e8f0 100644
--- a/arch/csky/kernel/Makefile
+++ b/arch/csky/kernel/Makefile
@@ -2,7 +2,7 @@
 extra-y := vmlinux.lds
 
 obj-y += head.o entry.o atomic.o signal.o traps.o irq.o time.o vdso.o vdso/
-obj-y += power.o syscall.o syscall_table.o setup.o io.o
+obj-y += power.o syscall.o syscall_table.o setup.o
 obj-y += process.o cpu-probe.o ptrace.o stacktrace.o
 obj-y += probes/
 
diff --git a/arch/csky/kernel/io.c b/arch/csky/kernel/io.c
deleted file mode 100644
index 5883f13fa2b1..000000000000
--- a/arch/csky/kernel/io.c
+++ /dev/null
@@ -1,91 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-#include <linux/export.h>
-#include <linux/types.h>
-#include <linux/io.h>
-
-/*
- * Copy data from IO memory space to "real" memory space.
- */
-void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
-{
-	while (count && !IS_ALIGNED((unsigned long)from, 4)) {
-		*(u8 *)to = __raw_readb(from);
-		from++;
-		to++;
-		count--;
-	}
-
-	while (count >= 4) {
-		*(u32 *)to = __raw_readl(from);
-		from += 4;
-		to += 4;
-		count -= 4;
-	}
-
-	while (count) {
-		*(u8 *)to = __raw_readb(from);
-		from++;
-		to++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memcpy_fromio);
-
-/*
- * Copy data from "real" memory space to IO memory space.
- */
-void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
-{
-	while (count && !IS_ALIGNED((unsigned long)to, 4)) {
-		__raw_writeb(*(u8 *)from, to);
-		from++;
-		to++;
-		count--;
-	}
-
-	while (count >= 4) {
-		__raw_writel(*(u32 *)from, to);
-		from += 4;
-		to += 4;
-		count -= 4;
-	}
-
-	while (count) {
-		__raw_writeb(*(u8 *)from, to);
-		from++;
-		to++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memcpy_toio);
-
-/*
- * "memset" on IO memory space.
- */
-void __memset_io(volatile void __iomem *dst, int c, size_t count)
-{
-	u32 qc = (u8)c;
-
-	qc |= qc << 8;
-	qc |= qc << 16;
-
-	while (count && !IS_ALIGNED((unsigned long)dst, 4)) {
-		__raw_writeb(c, dst);
-		dst++;
-		count--;
-	}
-
-	while (count >= 4) {
-		__raw_writel(qc, dst);
-		dst += 4;
-		count -= 4;
-	}
-
-	while (count) {
-		__raw_writeb(c, dst);
-		dst++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memset_io);
-- 
2.34.1






^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/4] Activate GENERIC_IO for the loongarch architecture
  2024-09-06 11:41 [PATCH 0/4] Consolidate IO memcpy functions Julian Vetter
                   ` (2 preceding siblings ...)
  2024-09-06 11:41 ` [PATCH 3/4] Activate GENERIC_IO for the csky architecture Julian Vetter
@ 2024-09-06 11:41 ` Julian Vetter
  3 siblings, 0 replies; 7+ messages in thread
From: Julian Vetter @ 2024-09-06 11:41 UTC (permalink / raw)
  To: Arnd Bergmann, Catalin Marinas, Will Deacon, Guo Ren,
	Huacai Chen, WANG Xuerui, Andrew Morton
  Cc: linux-arm-kernel, linux-kernel, linux-csky, loongarch,
	Yann Sionneau, Julian Vetter

Use the generic __memcpy_{from,to}io and __memset_io for the loongarch
processor architecture.

Reviewed-by: Yann Sionneau <ysionneau@kalrayinc.com>
Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>
---
 arch/loongarch/Kconfig         |  1 +
 arch/loongarch/kernel/Makefile |  2 +-
 arch/loongarch/kernel/io.c     | 94 ----------------------------------
 3 files changed, 2 insertions(+), 95 deletions(-)
 delete mode 100644 arch/loongarch/kernel/io.c

diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
index 70f169210b52..088137d75113 100644
--- a/arch/loongarch/Kconfig
+++ b/arch/loongarch/Kconfig
@@ -84,6 +84,7 @@ config LOONGARCH
 	select GENERIC_CPU_DEVICES
 	select GENERIC_ENTRY
 	select GENERIC_GETTIMEOFDAY
+	select GENERIC_IO
 	select GENERIC_IOREMAP if !ARCH_IOREMAP
 	select GENERIC_IRQ_MULTI_HANDLER
 	select GENERIC_IRQ_PROBE
diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile
index c9bfeda89e40..9497968ee158 100644
--- a/arch/loongarch/kernel/Makefile
+++ b/arch/loongarch/kernel/Makefile
@@ -8,7 +8,7 @@ OBJECT_FILES_NON_STANDARD_head.o := y
 extra-y		:= vmlinux.lds
 
 obj-y		+= head.o cpu-probe.o cacheinfo.o env.o setup.o entry.o genex.o \
-		   traps.o irq.o idle.o process.o dma.o mem.o io.o reset.o switch.o \
+		   traps.o irq.o idle.o process.o dma.o mem.o reset.o switch.o \
 		   elf.o syscall.o signal.o time.o topology.o inst.o ptrace.o vdso.o \
 		   alternative.o unwind.o
 
diff --git a/arch/loongarch/kernel/io.c b/arch/loongarch/kernel/io.c
deleted file mode 100644
index cb85bda5a6ad..000000000000
--- a/arch/loongarch/kernel/io.c
+++ /dev/null
@@ -1,94 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
- */
-#include <linux/export.h>
-#include <linux/types.h>
-#include <linux/io.h>
-
-/*
- * Copy data from IO memory space to "real" memory space.
- */
-void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
-{
-	while (count && !IS_ALIGNED((unsigned long)from, 8)) {
-		*(u8 *)to = __raw_readb(from);
-		from++;
-		to++;
-		count--;
-	}
-
-	while (count >= 8) {
-		*(u64 *)to = __raw_readq(from);
-		from += 8;
-		to += 8;
-		count -= 8;
-	}
-
-	while (count) {
-		*(u8 *)to = __raw_readb(from);
-		from++;
-		to++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memcpy_fromio);
-
-/*
- * Copy data from "real" memory space to IO memory space.
- */
-void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
-{
-	while (count && !IS_ALIGNED((unsigned long)to, 8)) {
-		__raw_writeb(*(u8 *)from, to);
-		from++;
-		to++;
-		count--;
-	}
-
-	while (count >= 8) {
-		__raw_writeq(*(u64 *)from, to);
-		from += 8;
-		to += 8;
-		count -= 8;
-	}
-
-	while (count) {
-		__raw_writeb(*(u8 *)from, to);
-		from++;
-		to++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memcpy_toio);
-
-/*
- * "memset" on IO memory space.
- */
-void __memset_io(volatile void __iomem *dst, int c, size_t count)
-{
-	u64 qc = (u8)c;
-
-	qc |= qc << 8;
-	qc |= qc << 16;
-	qc |= qc << 32;
-
-	while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
-		__raw_writeb(c, dst);
-		dst++;
-		count--;
-	}
-
-	while (count >= 8) {
-		__raw_writeq(qc, dst);
-		dst += 8;
-		count -= 8;
-	}
-
-	while (count) {
-		__raw_writeb(c, dst);
-		dst++;
-		count--;
-	}
-}
-EXPORT_SYMBOL(__memset_io);
-- 
2.34.1






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/4] Activate GENERIC_IO for the csky architecture
  2024-09-06 11:41 ` [PATCH 3/4] Activate GENERIC_IO for the csky architecture Julian Vetter
@ 2024-09-09  5:38   ` Guo Ren
  0 siblings, 0 replies; 7+ messages in thread
From: Guo Ren @ 2024-09-09  5:38 UTC (permalink / raw)
  To: Julian Vetter
  Cc: Arnd Bergmann, Catalin Marinas, Will Deacon, Huacai Chen,
	WANG Xuerui, Andrew Morton, linux-arm-kernel, linux-kernel,
	linux-csky, loongarch, Yann Sionneau

On Fri, Sep 6, 2024 at 7:47 PM Julian Vetter <jvetter@kalrayinc.com> wrote:
>
> Use the generic __memcpy_{from,to}io and __memset_io for the csky
> processor architecture.
>
> Reviewed by: Yann Sionneau <ysionneau@kalrayinc.com>
> Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>
I'm okay with moving it into GENERIC_IO

Acked-by: Guo Ren <guoren@kernel.org>

> ---
>  arch/csky/Kconfig         |  1 +
>  arch/csky/kernel/Makefile |  2 +-
>  arch/csky/kernel/io.c     | 91 ---------------------------------------
>  3 files changed, 2 insertions(+), 92 deletions(-)
>  delete mode 100644 arch/csky/kernel/io.c
>
> diff --git a/arch/csky/Kconfig b/arch/csky/Kconfig
> index 5479707eb5d1..d64329b691f7 100644
> --- a/arch/csky/Kconfig
> +++ b/arch/csky/Kconfig
> @@ -48,6 +48,7 @@ config CSKY
>         select DMA_DIRECT_REMAP
>         select IRQ_DOMAIN
>         select DW_APB_TIMER_OF
> +       select GENERIC_IO
>         select GENERIC_IOREMAP
>         select GENERIC_LIB_ASHLDI3
>         select GENERIC_LIB_ASHRDI3
> diff --git a/arch/csky/kernel/Makefile b/arch/csky/kernel/Makefile
> index 8a868316b912..de1c3472e8f0 100644
> --- a/arch/csky/kernel/Makefile
> +++ b/arch/csky/kernel/Makefile
> @@ -2,7 +2,7 @@
>  extra-y := vmlinux.lds
>
>  obj-y += head.o entry.o atomic.o signal.o traps.o irq.o time.o vdso.o vdso/
> -obj-y += power.o syscall.o syscall_table.o setup.o io.o
> +obj-y += power.o syscall.o syscall_table.o setup.o
>  obj-y += process.o cpu-probe.o ptrace.o stacktrace.o
>  obj-y += probes/
>
> diff --git a/arch/csky/kernel/io.c b/arch/csky/kernel/io.c
> deleted file mode 100644
> index 5883f13fa2b1..000000000000
> --- a/arch/csky/kernel/io.c
> +++ /dev/null
> @@ -1,91 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0
> -
> -#include <linux/export.h>
> -#include <linux/types.h>
> -#include <linux/io.h>
> -
> -/*
> - * Copy data from IO memory space to "real" memory space.
> - */
> -void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
> -{
> -       while (count && !IS_ALIGNED((unsigned long)from, 4)) {
> -               *(u8 *)to = __raw_readb(from);
> -               from++;
> -               to++;
> -               count--;
> -       }
> -
> -       while (count >= 4) {
> -               *(u32 *)to = __raw_readl(from);
> -               from += 4;
> -               to += 4;
> -               count -= 4;
> -       }
> -
> -       while (count) {
> -               *(u8 *)to = __raw_readb(from);
> -               from++;
> -               to++;
> -               count--;
> -       }
> -}
> -EXPORT_SYMBOL(__memcpy_fromio);
> -
> -/*
> - * Copy data from "real" memory space to IO memory space.
> - */
> -void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
> -{
> -       while (count && !IS_ALIGNED((unsigned long)to, 4)) {
> -               __raw_writeb(*(u8 *)from, to);
> -               from++;
> -               to++;
> -               count--;
> -       }
> -
> -       while (count >= 4) {
> -               __raw_writel(*(u32 *)from, to);
> -               from += 4;
> -               to += 4;
> -               count -= 4;
> -       }
> -
> -       while (count) {
> -               __raw_writeb(*(u8 *)from, to);
> -               from++;
> -               to++;
> -               count--;
> -       }
> -}
> -EXPORT_SYMBOL(__memcpy_toio);
> -
> -/*
> - * "memset" on IO memory space.
> - */
> -void __memset_io(volatile void __iomem *dst, int c, size_t count)
> -{
> -       u32 qc = (u8)c;
> -
> -       qc |= qc << 8;
> -       qc |= qc << 16;
> -
> -       while (count && !IS_ALIGNED((unsigned long)dst, 4)) {
> -               __raw_writeb(c, dst);
> -               dst++;
> -               count--;
> -       }
> -
> -       while (count >= 4) {
> -               __raw_writel(qc, dst);
> -               dst += 4;
> -               count -= 4;
> -       }
> -
> -       while (count) {
> -               __raw_writeb(c, dst);
> -               dst++;
> -               count--;
> -       }
> -}
> -EXPORT_SYMBOL(__memset_io);
> --
> 2.34.1
>
>
>
>
>


-- 
Best Regards
 Guo Ren

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib
  2024-09-06 11:41 ` [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib Julian Vetter
@ 2024-09-09 10:35   ` Arnd Bergmann
  0 siblings, 0 replies; 7+ messages in thread
From: Arnd Bergmann @ 2024-09-09 10:35 UTC (permalink / raw)
  To: Julian Vetter, Catalin Marinas, Will Deacon, guoren, Huacai Chen,
	WANG Xuerui, Andrew Morton
  Cc: linux-arm-kernel, linux-kernel, linux-csky, loongarch, Yann Sionneau

On Fri, Sep 6, 2024, at 11:41, Julian Vetter wrote:
> Various architectures have almost the same implementations for
> __memcpy_{to,from}io and __memset_io functions. So, consolidate them and
> introduce a CONFIG_GENERIC_IO flag to build the given lib/io.c.
>
> Reviewed-by: Yann Sionneau <ysionneau@kalrayinc.com>
> Signed-off-by: Julian Vetter <jvetter@kalrayinc.com>

Thanks for working on this. The implementation looks correct
to me and we should be able to use this on most architectures,
but since this is a shared file, it would be good to make it
as polished as possible.

>  lib/Kconfig  |   3 ++
>  lib/Makefile |   2 +
>  lib/io.c     | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 106 insertions(+)
>  create mode 100644 lib/io.c

I feel the name is a little too geneal, maybe use io_copy.c and
CONFIG_GENERIC_IO_COPY for the name?

Alternatively, this could be part of lib/iomap_copy.c,
which already has some related helper functions. In that
case, I think we would rely on the per-architecture "#define
__memcpy_fromio __memcpy_fromio" etc macros instead of a
Kconfig symbol.

> +void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
> +{
> +	while (count && !IS_ALIGNED((unsigned long)from, NATIVE_STORE_SIZE)) {
> +		*(u8 *)to = __raw_readb(from);
> +		from++;
> +		to++;
> +		count--;
> +	}

There is a corner case that this bit actually misses (same in the
existing implementation): If the __iomem buffer is aligned, but
the 'to' buffer is not, this may cause an alignment fault on
architectures without native unaligned stores. At the moment
I think both csky and some configurations of loongarch64 can
run into this. I think the best way to deal with this is to
use get_unaligned()/put_unaligned() in place of the pointer
dereference. This has no effect on architectures that have
native unaligned access but otherwise uses byte access on the
kernel buffer, which I think is a good tradeoff.

> +	while (count >= NATIVE_STORE_SIZE) {
> +		*(uintptr_t *)to = __raw_read_native(from);
> +		from += NATIVE_STORE_SIZE;
> +		to += NATIVE_STORE_SIZE;
> +		count -= NATIVE_STORE_SIZE;
> +	}

The use of __raw_read_native() somehow bothers me, it seems
a little more complicated than open-coding the two
possibles paths, or even using the aligned-access
helpers like

      if (IS_ENABLED(CONFIG_64BIT))
             __iowrite64_copy(to, from, count & WORD_MASK)
      else
             __iowrite32_copy(to, from, count & WORD_MASK)

Those helpers do require the kernel buffer to be naturally
aligned though.

> +void __memset_io(volatile void __iomem *dst, int c, size_t count)
> +{
> +	uintptr_t qc = (u8)c;
> +
> +	qc |= qc << 8;
> +	qc |= qc << 16;
> +#if IS_ENABLED(CONFIG_64BIT)
> +	qc |= qc << 32;
> +#endif

If you use IS_ENABLED() here, please do it like

       if (IS_ENABLED(CONFIG_64BIT)
              qc |= qc << 32;

      Arnd

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-09-09  8:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-06 11:41 [PATCH 0/4] Consolidate IO memcpy functions Julian Vetter
2024-09-06 11:41 ` [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib Julian Vetter
2024-09-09 10:35   ` Arnd Bergmann
2024-09-06 11:41 ` [PATCH 2/4] Activate GENERIC_IO for the arm64 architecture Julian Vetter
2024-09-06 11:41 ` [PATCH 3/4] Activate GENERIC_IO for the csky architecture Julian Vetter
2024-09-09  5:38   ` Guo Ren
2024-09-06 11:41 ` [PATCH 4/4] Activate GENERIC_IO for the loongarch architecture Julian Vetter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®