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

From: Gary Guo

Date: Fri Jan 30 2026 - 12:06:39 EST


On Fri Jan 30, 2026 at 4:44 PM GMT, Danilo Krummrich wrote:
> 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).

`AsRef` won't work for `Io` which is a trait, unlike `Device` which is a type. A
custom trait might work but that's more complex than what Alex purposed.

I'm quite fond of the `io.read_reg(REGISTER_REF)` API suggested. It looks quite
uniform with other I/O acceessor methods, except typed and with constant
offsets.

>
> 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));
> }
> }

The only benefit of this compared to the newly proposed API is that you can use
`Self`. It's not too bad to have (hypothetical API)

pub(crate) fn reset_engine<E: FalconEngine>(bar: &Bar0) {
bar.update_reg(NV_PFALCON_FALCON_ENGINE(&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));

bar.update_reg(NV_PFALCON_FALCON_ENGINE(&E::ID), |r| r.set_reset(true));
}

I also think in most cases these helper function won't be implemented on the
register, so you're not saving much by having the ability to refer to `Self`.
For example, in this case you could implement it on `FalconEngine`.

Best,
Gary


>
> 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.