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

[RFC PATCH v5 08/10] lib/arm: Add I/O memory copy helpers


  • To: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Oleksii Moisieiev <Oleksii_Moisieiev@xxxxxxxx>
  • Date: Tue, 22 Jul 2025 11:41:41 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=epam.com; dmarc=pass action=none header.from=epam.com; dkim=pass header.d=epam.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=s78A2BGWOWtn2IanctdhKkqcCNJK9PnZSUd/GG10AOg=; b=Y+UOGyrASIzHDqC8VzzZ4ZeQZ5Qcduil3bggP8YoEfvQePnJBFwH4s7fmpK5qdroF2YrY6OdEeIC3ixDH2WmpMy9Eih/HxyjGtnaSXOJ65F4vr596sZmbxEpduFNhKd05ZOOF+IrR81Le9wA6SusJzpqy+g67j15AJq2ziz81Lnftw7Xoav6E+VtqAaLVZMzMXGuefmK1nmuc80ezXnSVOSNDfzUpd/GnkHihwB4kgjg9JDgTqntw00dKWGagp75xMvu5X5YKvTX+AQKziaXhCfEMbFJXaVX0eW6qKlZWyVtFm0j4AuNGpccaCKB29zFDaYPUIU83HyxmwsPqO61ug==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=srMseTY9wPYSN2xAPV3USh3y9aqIz4bS0fQnb9+H7U77043K/J0nN37HE7hfAy8sU+FNZXTZwS5xDEDKhCaee5KQblnqQtmQcpuWi0ixkPRv7F61MDeXCmd+KTNY24fYCZwhJLg0ETkROjTyMoOI1iCNaXgPGJl0ooKUjiLUC/T2EEgDjZuvC0ZTDmmbV3SjswSSqbdY+DU+rhVJTjBfCbUj9mO5Cf9a4UVXfdvhj69rLrCVMdCdkHjIPhCScznd8+Pt9pD7OhZ8U3oVEV0nfrVMgY2KTFXCz4Qb6sXQZNkRLIvrUniHy3mxOodIVixE3yG4ifBGZLvPOoVBoh3VDA==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=epam.com;
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Bertrand Marquis <bertrand.marquis@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Juergen Gross <jgross@xxxxxxxx>, Julien Grall <julien@xxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Oleksii Moisieiev <Oleksii_Moisieiev@xxxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Volodymyr Babchuk <Volodymyr_Babchuk@xxxxxxxx>, Grygorii Strashko <grygorii_strashko@xxxxxxxx>
  • Delivery-date: Tue, 22 Jul 2025 11:41:54 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHb+v2X+ZrZt2L7gUmcdC6X7zP5Og==
  • Thread-topic: [RFC PATCH v5 08/10] lib/arm: Add I/O memory copy helpers

This commit introduces two helper functions, `__memcpy_fromio` and
`__memcpy_toio`, to provide a robust mechanism for copying data between
standard memory and memory-mapped I/O (MMIO) space for the ARM
architecture.

These functions are designed to handle memory transfers safely,
accounting for potential address alignment issues to ensure correctness
and improve performance where possible. The implementation is specific
to ARM and uses relaxed I/O accessors.

__memcpy_fromio:
Copies a block of data from an I/O memory source to a destination in
standard ("real") memory. The implementation first handles any unaligned
bytes at the beginning of the source buffer individually using byte-wise
reads. It then copies the bulk of the data using 32-bit reads for
efficiency, and finally processes any remaining bytes at the end of the
buffer.

__memcpy_toio:
Copies a block of data from standard memory to a destination in I/O
memory space. It follows a similar strategy, handling any initial
unaligned portion of the destination buffer byte-by-byte before using
more efficient 32-bit writes for the main, aligned part of the transfer.
Any trailing bytes are also handled individually.
xen/include/xen/lib/arm/io.h

Signed-off-by: Oleksii Moisieiev <oleksii_moisieiev@xxxxxxxx>
---

Changes in v5:
- move memcpy_toio/fromio to the generic place

 xen/include/xen/lib/arm/io.h | 15 +++++++
 xen/lib/Makefile             |  1 +
 xen/lib/arm/Makefile         |  1 +
 xen/lib/arm/io.c             | 80 ++++++++++++++++++++++++++++++++++++
 4 files changed, 97 insertions(+)
 create mode 100644 xen/include/xen/lib/arm/io.h
 create mode 100644 xen/lib/arm/Makefile
 create mode 100644 xen/lib/arm/io.c

diff --git a/xen/include/xen/lib/arm/io.h b/xen/include/xen/lib/arm/io.h
new file mode 100644
index 0000000000..86973660ba
--- /dev/null
+++ b/xen/include/xen/lib/arm/io.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _XEN_LIB_ARM_IO_H
+#define _XEN_LIB_ARM_IO_H
+
+#include <xen/types.h>
+
+/*
+ * Prototypes for I/O memory access functions.
+ */
+extern void __memcpy_fromio(void *to, const volatile void __iomem *from,
+                     size_t count);
+extern void __memcpy_toio(volatile void __iomem *to, const void *from,
+                   size_t count);
+
+#endif /* _XEN_LIB_ARM_IO_H */
diff --git a/xen/lib/Makefile b/xen/lib/Makefile
index 5ccb1e5241..efa8157a72 100644
--- a/xen/lib/Makefile
+++ b/xen/lib/Makefile
@@ -1,4 +1,5 @@
 obj-$(CONFIG_X86) += x86/
+obj-$(CONFIG_ARM) += arm/
 
 lib-y += bsearch.o
 lib-y += ctors.o
diff --git a/xen/lib/arm/Makefile b/xen/lib/arm/Makefile
new file mode 100644
index 0000000000..87250b3822
--- /dev/null
+++ b/xen/lib/arm/Makefile
@@ -0,0 +1 @@
+obj-y += io.o
\ No newline at end of file
diff --git a/xen/lib/arm/io.c b/xen/lib/arm/io.c
new file mode 100644
index 0000000000..d267bd28e4
--- /dev/null
+++ b/xen/lib/arm/io.c
@@ -0,0 +1,80 @@
+#include <asm/io.h>
+#include <xen/lib/arm/io.h>
+
+/*
+ * memcpy_fromio - Copy data from IO memory space to "real" memory space.
+ * @to: Where to copy to
+ * @from: Where to copy from
+ * @count: The size of the area.
+ */
+void __memcpy_fromio(void *to, const volatile void __iomem *from,
+                     size_t count)
+{
+    while ( count && !IS_ALIGNED((unsigned long)from, 4) )
+    {
+        *(u8 *)to = readb_relaxed(from);
+        from++;
+        to++;
+        count--;
+    }
+
+    while ( count >= 4 )
+    {
+        *(u32 *)to = readl_relaxed(from);
+        from += 4;
+        to += 4;
+        count -= 4;
+    }
+
+    while ( count )
+    {
+        *(u8 *)to = readb_relaxed(from);
+        from++;
+        to++;
+        count--;
+    }
+}
+
+/*
+ * memcpy_toio - Copy data from "real" memory space to IO memory space.
+ * @to: Where to copy to
+ * @from: Where to copy from
+ * @count: The size of the area.
+ */
+void __memcpy_toio(volatile void __iomem *to, const void *from,
+                   size_t count)
+{
+    while ( count && !IS_ALIGNED((unsigned long)to, 4) )
+    {
+        writeb_relaxed(*(u8 *)from, to);
+        from++;
+        to++;
+        count--;
+    }
+
+    while ( count >= 4 )
+    {
+        writel_relaxed(*(u32 *)from, to);
+        from += 4;
+        to += 4;
+        count -= 4;
+    }
+
+    while ( count )
+    {
+        writeb_relaxed(*(u8 *)from, to);
+        from++;
+        to++;
+        count--;
+    }
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * End:
+ */
-- 
2.34.1



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.