Re: [PATCH v4 5/7] rust: io: add `register!` macro

From: Danilo Krummrich

Date: Fri Jan 30 2026 - 11:45:11 EST


On Fri Jan 30, 2026 at 5:14 PM CET, Gary Guo wrote:
> On Fri Jan 30, 2026 at 6:55 AM GMT, Alexandre Courbot wrote:
>> No, I'm starting to believe that the fundamental issue is that the
>> register interface does its I/O backwards, and that design issue is only
>> exacerbated by the recent I/O redesign. I.e. instead of doing
>>
>> regs::NV_PMC_BOOT_0::read(bar);
>>
>> We should really do
>>
>> bar.read_reg::<regs::NV_PMC_BOOT_0>();
>>
>> Because that way we can use deref coercion.
>>
>> That's quite a big redesign though, which means I don't believe
>> `register!` can make it this cycle... I'll give it a try though.
>
> Hmm, that's unfortunate, but I think this is indeed a big design change that we
> should iron out before merging...
>
> I think you're right that if we put the methods on `Io` then all of the deref
> issue would just went away.

I already discussed this with Alex offline and I think we should not take this
direction just because of the Deref issue, as we can easily overcome this with
using AsRef (or a custom trait).

Whereas the downside of bar.read_reg() is that you end up with a more
inconsistent and complicated API for drivers.

For instance, with the API as is you can do things like:

register!(NV_PFALCON_FALCON_ENGINE @ PFalconBase[0x000003c0] {
0:0 reset as bool;
});

impl NV_PFALCON_FALCON_ENGINE {
pub(crate) fn reset_engine<E: FalconEngine>(bar: &Bar0) {
Self::update(bar, &E::ID, |r| r.set_reset(true));

// TIMEOUT: falcon engine should not take more than 10us to reset.
time::delay::fsleep(time::Delta::from_micros(10));

Self::update(bar, &E::ID, |r| r.set_reset(false));
}
}

and then use this from the driver code like this:

impl<E: FalconEngine> FalconHal<E> for Tu102<E> {
fn do_stuff(&self, bar: &Bar0) {
//...

regs::NV_PFALCON_FALCON_ENGINE::reset_engine::<E>(bar);

//...
}
}

Having to implement AsRef (or a custom trait) for the corresponding I/O backend
implementations is a pretty minor inconvinience compared to the simplicity the
current API provides to drivers.