Re: [PATCH v9 08/10] rust: io: introduce `write_reg` and `LocatedRegister`

From: Alexandre Courbot

Date: Sun Mar 15 2026 - 01:11:19 EST


On Sat Mar 14, 2026 at 10:56 PM JST, Gary Guo wrote:
> On Sat Mar 14, 2026 at 1:06 AM GMT, Alexandre Courbot wrote:
>> Some I/O types, like fixed address registers, carry their location
>> alongside their values. For these types, the regular `Io::write` method
>> can lead into repeating the location information twice: once to provide
>> the location itself, another time to build the value.
>>
>> We are also considering supporting making all register values carry
>> their full location information for convenience and safety.
>>
>> Add a new `Io::write_reg` method that takes a single argument
>> implementing `LocatedRegister`, a trait that decomposes implementors
>> into a `(location, value)` tuple. This allows write operations on fixed
>> offset registers to be done while specifying their name only once.
>>
>> Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
>> ---
>> rust/kernel/io.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++
>> rust/kernel/io/register.rs | 35 +++++++++++++++++++++--
>> 2 files changed, 103 insertions(+), 2 deletions(-)
>>
>> diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs
>> index bfea30a9acdf..24e6b48b6582 100644
>> --- a/rust/kernel/io.rs
>> +++ b/rust/kernel/io.rs
>> @@ -17,6 +17,8 @@
>> pub use crate::register;
>> pub use resource::Resource;
>>
>> +use register::LocatedRegister;
>> +
>> /// Physical address type.
>> ///
>> /// This is a type alias to either `u32` or `u64` depending on the config option
>> @@ -473,6 +475,40 @@ fn try_write<T, L>(&self, location: L, value: T) -> Result
>> Ok(())
>> }
>>
>> + /// Generic fallible write of a fully-located register value.
>> + ///
>> + /// # Examples
>> + ///
>> + /// Tuples carrying a location and a value can be used with this method:
>> + ///
>> + /// ```no_run
>> + /// use kernel::io::{
>> + /// register,
>> + /// Io,
>> + /// Mmio,
>> + /// };
>> + ///
>> + /// register! {
>> + /// FIFO_OUT(u32) @ 0x100 {}
>> + /// }
>> + ///
>> + /// fn do_write_reg(io: &Mmio) -> Result {
>> + /// // `FIFO_OUT` provides us the location of the write operation.
>> + /// io.try_write_reg(FIFO_OUT::from(10))
>
> This is not a good example, as I want to make FIFO not generate bitfields in the
> future and so people write
>
> io.write(FIFO_OUT, 10)
>
> Perhaps replace with any other register example that has bitfields..

What is wrong with the example? It demonstrates how we can do a FIFO
register with the current macro.

I was thinking that we can update this example once we have the right
support, but in the meantime this looks useful to me.