[PATCH v2 07/19] gpu: nova-core: register: move OFFSET declaration to I/O impl block

Alexandre Courbot posted 19 patches 6 months, 3 weeks ago
[PATCH v2 07/19] gpu: nova-core: register: move OFFSET declaration to I/O impl block
Posted by Alexandre Courbot 6 months, 3 weeks ago
The OFFSET const is an I/O property, and having to pass it to the
@common rule makes it impossible to make I/O optional, as we want to get
to eventually.

Thus, move OFFSET to the I/O impl block so it is not needed by the
@common rule anymore.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 drivers/gpu/nova-core/regs/macros.rs | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/regs/macros.rs
index 742afd3ae1a3c798817bbf815945889077ce10d0..4da897787c065e69657ce65327e3290af403a615 100644
--- a/drivers/gpu/nova-core/regs/macros.rs
+++ b/drivers/gpu/nova-core/regs/macros.rs
@@ -92,7 +92,7 @@ macro_rules! register {
             $($fields:tt)*
         }
     ) => {
-        register!(@common $name @ $offset $(, $comment)?);
+        register!(@common $name $(, $comment)?);
         register!(@field_accessors $name { $($fields)* });
         register!(@io $name @ $offset);
     };
@@ -103,7 +103,7 @@ macro_rules! register {
             $($fields:tt)*
         }
     ) => {
-        register!(@common $name @ $alias::OFFSET $(, $comment)?);
+        register!(@common $name $(, $comment)?);
         register!(@field_accessors $name { $($fields)* });
         register!(@io $name @ $alias::OFFSET);
     };
@@ -114,7 +114,7 @@ macro_rules! register {
             $($fields:tt)*
         }
     ) => {
-        register!(@common $name @ $offset $(, $comment)?);
+        register!(@common $name $(, $comment)?);
         register!(@field_accessors $name { $($fields)* });
         register!(@io $name @ + $offset);
     };
@@ -125,7 +125,7 @@ macro_rules! register {
             $($fields:tt)*
         }
     ) => {
-        register!(@common $name @ $alias::OFFSET $(, $comment)?);
+        register!(@common $name $(, $comment)?);
         register!(@field_accessors $name { $($fields)* });
         register!(@io $name @ + $alias::OFFSET);
     };
@@ -134,7 +134,7 @@ macro_rules! register {
 
     // Defines the wrapper `$name` type, as well as its relevant implementations (`Debug`, `BitOr`,
     // and conversion to regular `u32`).
-    (@common $name:ident @ $offset:expr $(, $comment:literal)?) => {
+    (@common $name:ident $(, $comment:literal)?) => {
         $(
         #[doc=$comment]
         )?
@@ -142,11 +142,6 @@ macro_rules! register {
         #[derive(Clone, Copy, Default)]
         pub(crate) struct $name(u32);
 
-        #[allow(dead_code)]
-        impl $name {
-            pub(crate) const OFFSET: usize = $offset;
-        }
-
         // TODO[REGA]: display the raw hex value, then the value of all the fields. This requires
         // matching the fields, which will complexify the syntax considerably...
         impl ::core::fmt::Debug for $name {
@@ -319,6 +314,8 @@ pub(crate) fn [<set_ $field>](mut self, value: $to_type) -> Self {
     (@io $name:ident @ $offset:expr) => {
         #[allow(dead_code)]
         impl $name {
+            pub(crate) const OFFSET: usize = $offset;
+
             #[inline]
             pub(crate) fn read<const SIZE: usize, T>(io: &T) -> Self where
                 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>,
@@ -351,6 +348,8 @@ pub(crate) fn alter<const SIZE: usize, T, F>(
     (@io $name:ident @ + $offset:literal) => {
         #[allow(dead_code)]
         impl $name {
+            pub(crate) const OFFSET: usize = $offset;
+
             #[inline]
             pub(crate) fn read<const SIZE: usize, T>(
                 io: &T,

-- 
2.50.1
Re: [PATCH v2 07/19] gpu: nova-core: register: move OFFSET declaration to I/O impl block
Posted by Daniel Almeida 6 months, 2 weeks ago

> On 18 Jul 2025, at 04:26, Alexandre Courbot <acourbot@nvidia.com> wrote:
> 
> The OFFSET const is an I/O property, and having to pass it to the
> @common rule makes it impossible to make I/O optional, as we want to get
> to eventually.
> 
> Thus, move OFFSET to the I/O impl block so it is not needed by the
> @common rule anymore.
> 
> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
> ---
> drivers/gpu/nova-core/regs/macros.rs | 19 +++++++++----------
> 1 file changed, 9 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/regs/macros.rs
> index 742afd3ae1a3c798817bbf815945889077ce10d0..4da897787c065e69657ce65327e3290af403a615 100644
> --- a/drivers/gpu/nova-core/regs/macros.rs
> +++ b/drivers/gpu/nova-core/regs/macros.rs
> @@ -92,7 +92,7 @@ macro_rules! register {
>             $($fields:tt)*
>         }
>     ) => {
> -        register!(@common $name @ $offset $(, $comment)?);
> +        register!(@common $name $(, $comment)?);
>         register!(@field_accessors $name { $($fields)* });
>         register!(@io $name @ $offset);
>     };
> @@ -103,7 +103,7 @@ macro_rules! register {
>             $($fields:tt)*
>         }
>     ) => {
> -        register!(@common $name @ $alias::OFFSET $(, $comment)?);
> +        register!(@common $name $(, $comment)?);
>         register!(@field_accessors $name { $($fields)* });
>         register!(@io $name @ $alias::OFFSET);
>     };
> @@ -114,7 +114,7 @@ macro_rules! register {
>             $($fields:tt)*
>         }
>     ) => {
> -        register!(@common $name @ $offset $(, $comment)?);
> +        register!(@common $name $(, $comment)?);
>         register!(@field_accessors $name { $($fields)* });
>         register!(@io $name @ + $offset);
>     };
> @@ -125,7 +125,7 @@ macro_rules! register {
>             $($fields:tt)*
>         }
>     ) => {
> -        register!(@common $name @ $alias::OFFSET $(, $comment)?);
> +        register!(@common $name $(, $comment)?);
>         register!(@field_accessors $name { $($fields)* });
>         register!(@io $name @ + $alias::OFFSET);
>     };
> @@ -134,7 +134,7 @@ macro_rules! register {
> 
>     // Defines the wrapper `$name` type, as well as its relevant implementations (`Debug`, `BitOr`,
>     // and conversion to regular `u32`).
> -    (@common $name:ident @ $offset:expr $(, $comment:literal)?) => {
> +    (@common $name:ident $(, $comment:literal)?) => {
>         $(
>         #[doc=$comment]
>         )?
> @@ -142,11 +142,6 @@ macro_rules! register {
>         #[derive(Clone, Copy, Default)]
>         pub(crate) struct $name(u32);
> 
> -        #[allow(dead_code)]
> -        impl $name {
> -            pub(crate) const OFFSET: usize = $offset;
> -        }
> -
>         // TODO[REGA]: display the raw hex value, then the value of all the fields. This requires
>         // matching the fields, which will complexify the syntax considerably...
>         impl ::core::fmt::Debug for $name {
> @@ -319,6 +314,8 @@ pub(crate) fn [<set_ $field>](mut self, value: $to_type) -> Self {
>     (@io $name:ident @ $offset:expr) => {
>         #[allow(dead_code)]
>         impl $name {
> +            pub(crate) const OFFSET: usize = $offset;
> +

Minor suggestion, have you ever though about somehow making this a const
generic? This saves the space needed to store the actual constant in the
binary.

Again, not sure whether this is feasible.


>             #[inline]
>             pub(crate) fn read<const SIZE: usize, T>(io: &T) -> Self where
>                 T: ::core::ops::Deref<Target = ::kernel::io::Io<SIZE>>,
> @@ -351,6 +348,8 @@ pub(crate) fn alter<const SIZE: usize, T, F>(
>     (@io $name:ident @ + $offset:literal) => {
>         #[allow(dead_code)]
>         impl $name {
> +            pub(crate) const OFFSET: usize = $offset;
> +
>             #[inline]
>             pub(crate) fn read<const SIZE: usize, T>(
>                 io: &T,
> 
> -- 
> 2.50.1
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Re: [PATCH v2 07/19] gpu: nova-core: register: move OFFSET declaration to I/O impl block
Posted by Alexandre Courbot 6 months, 2 weeks ago
On Sat Jul 26, 2025 at 2:03 AM JST, Daniel Almeida wrote:
>
>
>> On 18 Jul 2025, at 04:26, Alexandre Courbot <acourbot@nvidia.com> wrote:
>> 
>> The OFFSET const is an I/O property, and having to pass it to the
>> @common rule makes it impossible to make I/O optional, as we want to get
>> to eventually.
>> 
>> Thus, move OFFSET to the I/O impl block so it is not needed by the
>> @common rule anymore.
>> 
>> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
>> ---
>> drivers/gpu/nova-core/regs/macros.rs | 19 +++++++++----------
>> 1 file changed, 9 insertions(+), 10 deletions(-)
>> 
>> diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/regs/macros.rs
>> index 742afd3ae1a3c798817bbf815945889077ce10d0..4da897787c065e69657ce65327e3290af403a615 100644
>> --- a/drivers/gpu/nova-core/regs/macros.rs
>> +++ b/drivers/gpu/nova-core/regs/macros.rs
>> @@ -92,7 +92,7 @@ macro_rules! register {
>>             $($fields:tt)*
>>         }
>>     ) => {
>> -        register!(@common $name @ $offset $(, $comment)?);
>> +        register!(@common $name $(, $comment)?);
>>         register!(@field_accessors $name { $($fields)* });
>>         register!(@io $name @ $offset);
>>     };
>> @@ -103,7 +103,7 @@ macro_rules! register {
>>             $($fields:tt)*
>>         }
>>     ) => {
>> -        register!(@common $name @ $alias::OFFSET $(, $comment)?);
>> +        register!(@common $name $(, $comment)?);
>>         register!(@field_accessors $name { $($fields)* });
>>         register!(@io $name @ $alias::OFFSET);
>>     };
>> @@ -114,7 +114,7 @@ macro_rules! register {
>>             $($fields:tt)*
>>         }
>>     ) => {
>> -        register!(@common $name @ $offset $(, $comment)?);
>> +        register!(@common $name $(, $comment)?);
>>         register!(@field_accessors $name { $($fields)* });
>>         register!(@io $name @ + $offset);
>>     };
>> @@ -125,7 +125,7 @@ macro_rules! register {
>>             $($fields:tt)*
>>         }
>>     ) => {
>> -        register!(@common $name @ $alias::OFFSET $(, $comment)?);
>> +        register!(@common $name $(, $comment)?);
>>         register!(@field_accessors $name { $($fields)* });
>>         register!(@io $name @ + $alias::OFFSET);
>>     };
>> @@ -134,7 +134,7 @@ macro_rules! register {
>> 
>>     // Defines the wrapper `$name` type, as well as its relevant implementations (`Debug`, `BitOr`,
>>     // and conversion to regular `u32`).
>> -    (@common $name:ident @ $offset:expr $(, $comment:literal)?) => {
>> +    (@common $name:ident $(, $comment:literal)?) => {
>>         $(
>>         #[doc=$comment]
>>         )?
>> @@ -142,11 +142,6 @@ macro_rules! register {
>>         #[derive(Clone, Copy, Default)]
>>         pub(crate) struct $name(u32);
>> 
>> -        #[allow(dead_code)]
>> -        impl $name {
>> -            pub(crate) const OFFSET: usize = $offset;
>> -        }
>> -
>>         // TODO[REGA]: display the raw hex value, then the value of all the fields. This requires
>>         // matching the fields, which will complexify the syntax considerably...
>>         impl ::core::fmt::Debug for $name {
>> @@ -319,6 +314,8 @@ pub(crate) fn [<set_ $field>](mut self, value: $to_type) -> Self {
>>     (@io $name:ident @ $offset:expr) => {
>>         #[allow(dead_code)]
>>         impl $name {
>> +            pub(crate) const OFFSET: usize = $offset;
>> +
>
> Minor suggestion, have you ever though about somehow making this a const
> generic? This saves the space needed to store the actual constant in the
> binary.
>
> Again, not sure whether this is feasible.

A const generic would require a trait to hold it, and would make that
trait implementable several times for the same register, so I'm not
quite sure how that would work...

Besides, we want `OFFSET` to be available for external users to read (in
case they need the register's actual address for whatever reason), and I
don't think this is doable with a const generic.