[PATCH 2/7] gpu: nova-core: Add support to convert bitfield to underlying type

Joel Fernandes posted 7 patches 3 months, 3 weeks ago
[PATCH 2/7] gpu: nova-core: Add support to convert bitfield to underlying type
Posted by Joel Fernandes 3 months, 3 weeks ago
To support the usecase where we read a register and write to another
with identical bit layout, add support to convert bitfield to underlying type.

Another way to do this, is to read individual fields, on the caller
side, and write to the destination fields, but that is both cumbersome
and error-prone as new bits added in hardware may be missed.

Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
---
 drivers/gpu/nova-core/bitfield.rs | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitfield.rs
index 0994505393dd..2266abc3f7ab 100644
--- a/drivers/gpu/nova-core/bitfield.rs
+++ b/drivers/gpu/nova-core/bitfield.rs
@@ -72,6 +72,7 @@
 /// - Field setters: `set_mode()`, `set_state()`, etc. (supports chaining with builder pattern).
 ///   Note that the compiler will error out if the size of the setter's arg exceeds the
 ///   struct's storage size.
+/// - Conversion from the underlying storage type (e.g., `From<u32>`).
 /// - Debug and Default implementations.
 ///
 /// Note: Field accessors and setters inherit the same visibility as the struct itself.
@@ -117,6 +118,12 @@ fn from(val: $name) -> $storage {
             }
         }
 
+        impl ::core::convert::From<$storage> for $name {
+            fn from(val: $storage) -> $name {
+                $name(val)
+            }
+        }
+
         bitfield!(@fields_dispatcher $vis $name $storage { $($fields)* });
     };
 
-- 
2.34.1
Re: [PATCH 2/7] gpu: nova-core: Add support to convert bitfield to underlying type
Posted by Alexandre Courbot 3 months, 2 weeks ago
On Tue Oct 21, 2025 at 3:55 AM JST, Joel Fernandes wrote:
> To support the usecase where we read a register and write to another
> with identical bit layout, add support to convert bitfield to underlying type.
>
> Another way to do this, is to read individual fields, on the caller
> side, and write to the destination fields, but that is both cumbersome
> and error-prone as new bits added in hardware may be missed.

Shouldn't the title be "Add support to convert bitfield FROM underlying
type"? Since this adds a `From<storage> for Bitfield`.
Re: [PATCH 2/7] gpu: nova-core: Add support to convert bitfield to underlying type
Posted by Joel Fernandes 3 months, 2 weeks ago

On 10/22/2025 2:25 AM, Alexandre Courbot wrote:
> On Tue Oct 21, 2025 at 3:55 AM JST, Joel Fernandes wrote:
>> To support the usecase where we read a register and write to another
>> with identical bit layout, add support to convert bitfield to underlying type.
>>
>> Another way to do this, is to read individual fields, on the caller
>> side, and write to the destination fields, but that is both cumbersome
>> and error-prone as new bits added in hardware may be missed.
> 
> Shouldn't the title be "Add support to convert bitfield FROM underlying
> type"? Since this adds a `From<storage> for Bitfield`.
> 

Good catch, I will fix the title for next posting.

thanks,

 - Joel
Re: [PATCH 2/7] gpu: nova-core: Add support to convert bitfield to underlying type
Posted by John Hubbard 3 months, 3 weeks ago
On 10/20/25 11:55 AM, Joel Fernandes wrote:
> To support the usecase where we read a register and write to another
> with identical bit layout, add support to convert bitfield to underlying type.
> 
> Another way to do this, is to read individual fields, on the caller
> side, and write to the destination fields, but that is both cumbersome
> and error-prone as new bits added in hardware may be missed.
> 
> Signed-off-by: Joel Fernandes <joelagnelf@nvidia.com>
> ---
>  drivers/gpu/nova-core/bitfield.rs | 7 +++++++
>  1 file changed, 7 insertions(+)

Reviewed-by: John Hubbard <jhubbard@nvidia.com>


thanks,
-- 
John Hubbard

> 
> diff --git a/drivers/gpu/nova-core/bitfield.rs b/drivers/gpu/nova-core/bitfield.rs
> index 0994505393dd..2266abc3f7ab 100644
> --- a/drivers/gpu/nova-core/bitfield.rs
> +++ b/drivers/gpu/nova-core/bitfield.rs
> @@ -72,6 +72,7 @@
>  /// - Field setters: `set_mode()`, `set_state()`, etc. (supports chaining with builder pattern).
>  ///   Note that the compiler will error out if the size of the setter's arg exceeds the
>  ///   struct's storage size.
> +/// - Conversion from the underlying storage type (e.g., `From<u32>`).
>  /// - Debug and Default implementations.
>  ///
>  /// Note: Field accessors and setters inherit the same visibility as the struct itself.
> @@ -117,6 +118,12 @@ fn from(val: $name) -> $storage {
>              }
>          }
>  
> +        impl ::core::convert::From<$storage> for $name {
> +            fn from(val: $storage) -> $name {
> +                $name(val)
> +            }
> +        }
> +
>          bitfield!(@fields_dispatcher $vis $name $storage { $($fields)* });
>      };
>