From: Alice Ryhl <aliceryhl@google.com>
To: Zhi Wang <zhiw@nvidia.com>
Cc: rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org,
linux-kernel@vger.kernel.org, dakr@kernel.org,
bhelgaas@google.com, kwilczynski@kernel.org, ojeda@kernel.org,
alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, tmgross@umich.edu,
markus.probst@posteo.de, helgaas@kernel.org, cjia@nvidia.com,
smitra@nvidia.com, ankita@nvidia.com, aniketa@nvidia.com,
kwankhede@nvidia.com, targupta@nvidia.com, acourbot@nvidia.com,
joelagnelf@nvidia.com, jhubbard@nvidia.com, zhiwang@kernel.org,
daniel.almeida@collabora.com
Subject: Re: [PATCH v10 2/5] rust: io: separate generic I/O helpers from MMIO implementation
Date: Tue, 20 Jan 2026 08:04:45 +0000 [thread overview]
Message-ID: <aW83HV4lVR5MQlDd@google.com> (raw)
In-Reply-To: <20260119202250.870588-3-zhiw@nvidia.com>
On Mon, Jan 19, 2026 at 10:22:44PM +0200, Zhi Wang wrote:
> The previous Io<SIZE> type combined both the generic I/O access helpers
> and MMIO implementation details in a single struct. This coupling prevented
> reusing the I/O helpers for other backends, such as PCI configuration
> space.
>
> Establish a clean separation between the I/O interface and concrete backends
> by separating generic I/O helpers from MMIO implementation.
>
> Introduce two traits to handle different access capabilities:
> - IoCapable<T> trait provides infallible I/O operations (read/write)
> with compile-time bounds checking.
> - IoTryCapable<T> trait provides fallible I/O operations
> (try_read/try_write) with runtime bounds checking.
> - The Io trait defines convenience accessors (read8/write8, try_read8/
> try_write8, etc.) that forward to the corresponding IoCapable<T> or
> IoTryCapable<T> implementations.
>
> This separation allows backends to selectively implement only the operations
> they support. For example, PCI configuration space can implement IoCapable<T>
> for infallible operations while MMIO regions can implement both IoCapable<T>
> and IoTryCapable<T>.
>
> Move the MMIO-specific logic into a dedicated Mmio<SIZE> type that
> implements Io and the corresponding `IoCapable<T>` and `IoTryCapable<T>` traits.
> Rename IoRaw to MmioRaw and update consumers to use the new types.
>
> Cc: Alexandre Courbot <acourbot@nvidia.com>
> Cc: Alice Ryhl <aliceryhl@google.com>
> Cc: Bjorn Helgaas <helgaas@kernel.org>
> Cc: Gary Guo <gary@garyguo.net>
> Cc: Danilo Krummrich <dakr@kernel.org>
> Cc: John Hubbard <jhubbard@nvidia.com>
> Signed-off-by: Zhi Wang <zhiw@nvidia.com>
Overall looks good to me. Some comments below:
> +/// Trait representing infallible I/O operations of a certain type.
> +///
> +/// This trait is used to provide compile-time bounds-checked I/O operations.
> +/// Different I/O backends can implement this trait to expose only the operations they support.
> +///
> +/// For example, a PCI configuration space may implement `IoCapable<u8>`, `IoCapable<u16>`,
> +/// and `IoCapable<u32>`, but not `IoCapable<u64>`, while an MMIO region on a 64-bit
> +/// system might implement all four.
> +pub trait IoCapable<T> {
> + /// Infallible read with compile-time bounds check.
> + fn read(&self, offset: usize) -> T;
> +
> + /// Infallible write with compile-time bounds check.
> + fn write(&self, value: T, offset: usize);
> +}
> +
> +/// Trait representing fallible I/O operations of a certain type.
> +///
> +/// This trait is used to provide runtime bounds-checked I/O operations.
> +/// Backends that do not support fallible operations (e.g., PCI configuration space)
> +/// do not need to implement this trait.
> +pub trait IoTryCapable<T> {
> + /// Fallible read with runtime bounds check.
> + fn try_read(&self, offset: usize) -> Result<T>;
> +
> + /// Fallible write with runtime bounds check.
> + fn try_write(&self, value: T, offset: usize) -> Result;
> +}
I still think it would make sense to have `IoCapable<T>: IoTryCapable<T>`,
but it's not a big deal.
> + /// Infallible 64-bit read with compile-time bounds check.
> + #[cfg(CONFIG_64BIT)]
> + fn read64(&self, offset: usize) -> u64
> + #[cfg(CONFIG_64BIT)]
> + fn try_read64(&self, offset: usize) -> Result<u64>
These don't really need cfg(CONFIG_64BIT). You can place that cfg on
impl blocks of IoCapable<u64>.
e.g., remove above but keep here:
> +// MMIO regions on 64-bit systems also support 64-bit accesses.
> +#[cfg(CONFIG_64BIT)]
> +impl<const SIZE: usize> IoCapable<u64> for Mmio<SIZE> {
> + define_read!(infallible, read, readq -> u64);
> + define_write!(infallible, write, writeq <- u64);
> +}
> +#[cfg(CONFIG_64BIT)]
> +impl<const SIZE: usize> IoTryCapable<u64> for Mmio<SIZE> {
> + define_read!(fallible, try_read, readq -> u64);
> + define_write!(fallible, try_write, writeq <- u64);
> +}
Alice
next prev parent reply other threads:[~2026-01-20 8:11 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-19 20:22 [PATCH v10 0/5] rust: pci: add config space read/write support Zhi Wang
2026-01-19 20:22 ` [PATCH v10 1/5] rust: devres: style for imports Zhi Wang
2026-01-20 15:26 ` Gary Guo
2026-01-20 16:11 ` Miguel Ojeda
2026-01-21 9:29 ` Zhi Wang
2026-01-22 1:55 ` Miguel Ojeda
2026-01-22 2:24 ` John Hubbard
2026-01-22 11:43 ` Danilo Krummrich
2026-01-19 20:22 ` [PATCH v10 2/5] rust: io: separate generic I/O helpers from MMIO implementation Zhi Wang
2026-01-20 8:04 ` Alice Ryhl [this message]
2026-01-20 10:12 ` Danilo Krummrich
2026-01-20 11:26 ` Alice Ryhl
2026-01-20 12:38 ` Alice Ryhl
2026-01-20 15:59 ` Gary Guo
2026-01-20 16:09 ` Danilo Krummrich
2026-01-20 17:26 ` Zhi Wang
2026-01-20 17:29 ` Danilo Krummrich
2026-01-20 17:35 ` Gary Guo
2026-01-20 13:37 ` kernel test robot
2026-01-20 15:54 ` Gary Guo
2026-01-19 20:22 ` [PATCH v10 3/5] rust: io: factor out MMIO read/write macros Zhi Wang
2026-01-19 20:22 ` [PATCH v10 4/5] rust: pci: add config space read/write support Zhi Wang
2026-01-19 20:22 ` [PATCH v10 5/5] sample: rust: pci: add tests for config space routines Zhi Wang
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=aW83HV4lVR5MQlDd@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=aniketa@nvidia.com \
--cc=ankita@nvidia.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=cjia@nvidia.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=helgaas@kernel.org \
--cc=jhubbard@nvidia.com \
--cc=joelagnelf@nvidia.com \
--cc=kwankhede@nvidia.com \
--cc=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=markus.probst@posteo.de \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=smitra@nvidia.com \
--cc=targupta@nvidia.com \
--cc=tmgross@umich.edu \
--cc=zhiw@nvidia.com \
--cc=zhiwang@kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.