From: Julian Vetter <jvetter@kalrayinc.com>
To: Arnd Bergmann <arnd@arndb.de>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>, Guo Ren <guoren@kernel.org>,
Huacai Chen <chenhuacai@kernel.org>,
WANG Xuerui <kernel@xen0n.name>,
Andrew Morton <akpm@linux-foundation.org>
Cc: linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-csky@vger.kernel.org,
loongarch@lists.linux.dev,
Yann Sionneau <ysionneau@kalrayinc.com>,
Julian Vetter <jvetter@kalrayinc.com>
Subject: [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib
Date: Fri, 6 Sep 2024 13:41:48 +0200 [thread overview]
Message-ID: <20240906114151.519028-2-jvetter@kalrayinc.com> (raw)
In-Reply-To: <20240906114151.519028-1-jvetter@kalrayinc.com>
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
next prev parent reply other threads:[~2024-09-06 11:50 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-06 11:41 [PATCH 0/4] Consolidate IO memcpy functions Julian Vetter
2024-09-06 11:41 ` Julian Vetter [this message]
2024-09-09 10:35 ` [PATCH 1/4] Consolidate __memcpy_{to,from}io and __memset_io into a single lib 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240906114151.519028-2-jvetter@kalrayinc.com \
--to=jvetter@kalrayinc.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=catalin.marinas@arm.com \
--cc=chenhuacai@kernel.org \
--cc=guoren@kernel.org \
--cc=kernel@xen0n.name \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-csky@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=loongarch@lists.linux.dev \
--cc=will@kernel.org \
--cc=ysionneau@kalrayinc.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®