mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: "Danilo Krummrich" <dakr@kernel.org>,
	"Abdiel Janulgue" <abdiel.janulgue@gmail.com>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>
Cc: driver-core@lists.linux.dev, rust-for-linux@vger.kernel.org,
	 linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
	 dri-devel@lists.freedesktop.org,
	 Daniel Almeida <daniel.almeida@collabora.com>
Subject: [PATCH 2/7] rust: io: split IoCopyable into IoCopyReadable and IoCopyWritable
Date: Thu, 15 Oct 2026 09:39:06 -0300	[thread overview]
Message-ID: <20261015-io-access-modes-v1-2-93611b84b5e3@collabora.com> (raw)
In-Reply-To: <20261015-io-access-modes-v1-0-93611b84b5e3@collabora.com>

`IoCopyable` provides both `copy_from_io` and `copy_to_io`, so a backend
that supports copies supports them in both directions, and all four copy
methods of `Io` require both.

Thus, as done for `IoCapable` in the previous patch, replace `IoCopyable`
with an `IoCopyReadable` trait holding `copy_from_io` and `copy_read`, and
an `IoCopyWritable` trait holding `copy_to_io` and `copy_write`, and
implement both for every backend that implemented `IoCopyable`.
`copy_read` and `copy_to_slice`, which read from I/O memory, now require
`IoCopyReadable`, and `copy_write` and `copy_from_slice`, which write to
it, require `IoCopyWritable`.

No functional change intended.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 rust/kernel/dma.rs | 17 +++++++-----
 rust/kernel/io.rs  | 77 ++++++++++++++++++++++++++++++------------------------
 2 files changed, 53 insertions(+), 41 deletions(-)

diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 7216a78d38c9..c57026368ab8 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -17,7 +17,8 @@
     io::{
         IoBackend,
         IoBase,
-        IoCopyable,
+        IoCopyReadable,
+        IoCopyWritable,
         IoReadable,
         IoWritable,
         SysMem,
@@ -1201,7 +1202,7 @@ fn io_write<'a>(view: Self::View<'a, T>, value: T) {
     }
 }
 
-impl IoCopyable for CoherentIoBackend {
+impl IoCopyReadable for CoherentIoBackend {
     #[inline]
     unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
         // SAFETY: Per safety requirement.
@@ -1209,14 +1210,16 @@ unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
     }
 
     #[inline]
-    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
-        // SAFETY: Per safety requirement.
-        unsafe { SysMemBackend::copy_to_io(view.cpu_addr, buffer) }
+    fn copy_read<T: zerocopy::FromBytes>(view: Self::View<'_, T>) -> T {
+        SysMemBackend::copy_read(view.cpu_addr)
     }
+}
 
+impl IoCopyWritable for CoherentIoBackend {
     #[inline]
-    fn copy_read<T: zerocopy::FromBytes>(view: Self::View<'_, T>) -> T {
-        SysMemBackend::copy_read(view.cpu_addr)
+    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
+        // SAFETY: Per safety requirement.
+        unsafe { SysMemBackend::copy_to_io(view.cpu_addr, buffer) }
     }
 
     #[inline]
diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
index fd5dd5f6bda4..ae60099fd4c3 100644
--- a/rust/kernel/io.rs
+++ b/rust/kernel/io.rs
@@ -298,8 +298,8 @@ pub trait IoWritable<T>: IoBackend {
     fn io_write<'a>(view: Self::View<'a, T>, value: T);
 }
 
-/// Trait indicating that an I/O backend supports memory copy operations.
-pub trait IoCopyable: IoBackend {
+/// Trait indicating that an I/O backend supports memory copy operations from I/O.
+pub trait IoCopyReadable: IoBackend {
     /// Copy contents of `view` to `buffer`.
     ///
     /// # Safety
@@ -308,14 +308,6 @@ pub trait IoCopyable: IoBackend {
     /// - `buffer` should not overlap with `view`.
     unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8);
 
-    /// Copy contents from `buffer` to `view`.
-    ///
-    /// # Safety
-    ///
-    /// - `buffer` is valid for volatile read for `view.size()` bytes.
-    /// - `buffer` should not overlap with `view`.
-    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8);
-
     /// Copy from `view` and return the value.
     #[inline]
     fn copy_read<T: FromBytes>(view: Self::View<'_, T>) -> T {
@@ -337,6 +329,17 @@ fn copy_read<T: FromBytes>(view: Self::View<'_, T>) -> T {
         // SAFETY: `T: FromBytes` guarantee that all bit patterns are valid.
         unsafe { buf.assume_init() }
     }
+}
+
+/// Trait indicating that an I/O backend supports memory copy operations to I/O.
+pub trait IoCopyWritable: IoBackend {
+    /// Copy contents from `buffer` to `view`.
+    ///
+    /// # Safety
+    ///
+    /// - `buffer` is valid for volatile read for `view.size()` bytes.
+    /// - `buffer` should not overlap with `view`.
+    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8);
 
     /// Copy `value` to `view`.
     ///
@@ -602,7 +605,7 @@ fn write_val(self, value: Self::Target)
     #[inline]
     fn copy_read(self) -> Self::Target
     where
-        Self::Backend: IoCopyable,
+        Self::Backend: IoCopyReadable,
         Self::Target: Sized + FromBytes,
     {
         Self::Backend::copy_read(self.as_view())
@@ -629,7 +632,7 @@ fn copy_read(self) -> Self::Target
     #[inline]
     fn copy_write(self, value: Self::Target)
     where
-        Self::Backend: IoCopyable,
+        Self::Backend: IoCopyWritable,
         Self::Target: Sized + IntoBytes,
     {
         Self::Backend::copy_write(self.as_view(), value);
@@ -654,7 +657,7 @@ fn copy_write(self, value: Self::Target)
     #[inline]
     fn copy_from_slice(self, data: &[u8])
     where
-        Self::Backend: IoCopyable,
+        Self::Backend: IoCopyWritable,
         Self: Io<'a, Target = [u8]>,
     {
         assert_eq!(self.len(), data.len());
@@ -685,7 +688,7 @@ fn copy_from_slice(self, data: &[u8])
     #[inline]
     fn copy_to_slice(self, data: &mut [u8])
     where
-        Self::Backend: IoCopyable,
+        Self::Backend: IoCopyReadable,
         Self: Io<'a, Target = [u8]>,
     {
         assert_eq!(self.len(), data.len());
@@ -1275,7 +1278,7 @@ fn io_write(view: <$backend as IoBackend>::View<'_, $ty>, value: $ty) {
 #[cfg(CONFIG_64BIT)]
 impl_mmio_io_capable!(MmioBackend, u64, readq, writeq);
 
-impl IoCopyable for MmioBackend {
+impl IoCopyReadable for MmioBackend {
     #[inline]
     unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
         // SAFETY:
@@ -1285,7 +1288,9 @@ unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
             bindings::memcpy_fromio(buffer.cast(), view.ptr.cast(), view.size());
         }
     }
+}
 
+impl IoCopyWritable for MmioBackend {
     #[inline]
     unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
         // SAFETY:
@@ -1442,7 +1447,7 @@ fn io_write(view: SysMem<'_, $ty>, value: $ty) {
 #[cfg(CONFIG_64BIT)]
 impl_sysmem_io_capable!(u64);
 
-impl IoCopyable for SysMemBackend {
+impl IoCopyReadable for SysMemBackend {
     #[inline]
     unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
         // Use `bindings::memcpy` instead of `copy_nonoverlapping` for volatile.
@@ -1452,15 +1457,6 @@ unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
         unsafe { bindings::memcpy(buffer.cast(), view.ptr.cast(), view.ptr.len()) };
     }
 
-    #[inline]
-    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
-        // Use `bindings::memcpy` instead of `copy_nonoverlapping` for volatile.
-        // SAFETY:
-        // - `view.ptr` is in CPU address space and valid for write.
-        // - `buffer` is valid for read for `view.size()` bytes which is equal to `view.ptr.len()`.
-        unsafe { bindings::memcpy(view.ptr.cast(), buffer.cast(), view.ptr.len()) };
-    }
-
     #[inline]
     fn copy_read<T: FromBytes>(view: Self::View<'_, T>) -> T {
         // SAFETY:
@@ -1471,6 +1467,17 @@ fn copy_read<T: FromBytes>(view: Self::View<'_, T>) -> T {
         // - `T: FromBytes` so all bit patterns are valid.
         unsafe { view.ptr.read_volatile() }
     }
+}
+
+impl IoCopyWritable for SysMemBackend {
+    #[inline]
+    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
+        // Use `bindings::memcpy` instead of `copy_nonoverlapping` for volatile.
+        // SAFETY:
+        // - `view.ptr` is in CPU address space and valid for write.
+        // - `buffer` is valid for read for `view.size()` bytes which is equal to `view.ptr.len()`.
+        unsafe { bindings::memcpy(view.ptr.cast(), buffer.cast(), view.ptr.len()) };
+    }
 
     #[inline]
     fn copy_write<T: IntoBytes>(view: Self::View<'_, T>, value: T) {
@@ -1640,7 +1647,7 @@ fn io_write<'a>(view: Self::View<'a, T>, value: T) {
     }
 }
 
-impl IoCopyable for IoSysMapBackend {
+impl IoCopyReadable for IoSysMapBackend {
     #[inline]
     unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
         match view {
@@ -1652,20 +1659,22 @@ unsafe fn copy_from_io(view: Self::View<'_, [u8]>, buffer: *mut u8) {
     }
 
     #[inline]
-    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
+    fn copy_read<T: FromBytes>(view: Self::View<'_, T>) -> T {
         match view {
-            // SAFETY: Per safety requirement.
-            IoSysMap::Io(l) => unsafe { MmioBackend::copy_to_io(l, buffer) },
-            // SAFETY: Per safety requirement.
-            IoSysMap::Sys(r) => unsafe { SysMemBackend::copy_to_io(r, buffer) },
+            IoSysMap::Io(l) => MmioBackend::copy_read(l),
+            IoSysMap::Sys(r) => SysMemBackend::copy_read(r),
         }
     }
+}
 
+impl IoCopyWritable for IoSysMapBackend {
     #[inline]
-    fn copy_read<T: FromBytes>(view: Self::View<'_, T>) -> T {
+    unsafe fn copy_to_io(view: Self::View<'_, [u8]>, buffer: *const u8) {
         match view {
-            IoSysMap::Io(l) => MmioBackend::copy_read(l),
-            IoSysMap::Sys(r) => SysMemBackend::copy_read(r),
+            // SAFETY: Per safety requirement.
+            IoSysMap::Io(l) => unsafe { MmioBackend::copy_to_io(l, buffer) },
+            // SAFETY: Per safety requirement.
+            IoSysMap::Sys(r) => unsafe { SysMemBackend::copy_to_io(r, buffer) },
         }
     }
 

-- 
2.56.0


  parent reply	other threads:[~2026-10-15 12:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-15 12:39 [PATCH 0/7] rust: io: add read-only and write-only views and registers Daniel Almeida
2026-10-15 12:39 ` [PATCH 1/7] rust: io: split IoCapable into IoReadable and IoWritable Daniel Almeida
2026-10-15 12:39 ` Daniel Almeida [this message]
2026-10-15 12:39 ` [PATCH 3/7] rust: io: add read-only and write-only views Daniel Almeida
2026-10-15 12:39 ` [PATCH 4/7] rust: io: project primitive accessors directly Daniel Almeida
2026-10-15 12:39 ` [PATCH 5/7] rust: io: let I/O locations carry an access mode Daniel Almeida
2026-10-15 12:39 ` [PATCH 6/7] rust: io: register: add `#[ro]` and `#[wo]` attributes Daniel Almeida
2026-10-15 12:39 ` [PATCH 7/7] drm/tyr: mark read-only and write-only registers Daniel Almeida
2026-10-20  8:01 ` [PATCH 0/7] rust: io: add read-only and write-only views and registers Alice Ryhl

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=20261015-io-access-modes-v1-2-93611b84b5e3@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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®