]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
gpu: nova-core: register: generate correct `Default` implementation
authorAlexandre Courbot <acourbot@nvidia.com>
Fri, 18 Jul 2025 07:26:17 +0000 (16:26 +0900)
committerAlexandre Courbot <acourbot@nvidia.com>
Fri, 15 Aug 2025 03:02:56 +0000 (12:02 +0900)
The `Default` implementation of a register should be the aggregate of
the default values of all its fields, and not simply be zeroed.

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Reviewed-by: Lyude Paul <lyude@redhat.com>
Link: https://lore.kernel.org/r/20250718-nova-regs-v2-12-7b6a762aa1cd@nvidia.com
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
drivers/gpu/nova-core/regs/macros.rs

index c966e58b775da2c5637952b6e6b6fd7f2548a300..e43e1ab7ae2e2196c9feca6fc6b4bea9081ee32f 100644 (file)
@@ -112,14 +112,14 @@ macro_rules! register {
 
     // All rules below are helpers.
 
-    // Defines the wrapper `$name` type, as well as its relevant implementations (`Debug`, `BitOr`,
-    // and conversion to the value type) and field accessor methods.
+    // Defines the wrapper `$name` type, as well as its relevant implementations (`Debug`,
+    // `Default`, `BitOr`, and conversion to the value type) and field accessor methods.
     (@core $name:ident $(, $comment:literal)? { $($fields:tt)* }) => {
         $(
         #[doc=$comment]
         )?
         #[repr(transparent)]
-        #[derive(Clone, Copy, Default)]
+        #[derive(Clone, Copy)]
         pub(crate) struct $name(u32);
 
         impl ::core::ops::BitOr for $name {
@@ -162,6 +162,7 @@ macro_rules! register {
             )*
         });
         register!(@debug $name { $($field;)* });
+        register!(@default $name { $($field;)* });
     };
 
     // Defines all the field getter/methods methods for `$name`.
@@ -321,6 +322,25 @@ macro_rules! register {
         }
     };
 
+    // Generates the `Default` implementation for `$name`.
+    (@default $name:ident { $($field:ident;)* }) => {
+        /// Returns a value for the register where all fields are set to their default value.
+        impl ::core::default::Default for $name {
+            fn default() -> Self {
+                #[allow(unused_mut)]
+                let mut value = Self(Default::default());
+
+                ::kernel::macros::paste!(
+                $(
+                value.[<set_ $field>](Default::default());
+                )*
+                );
+
+                value
+            }
+        }
+    };
+
     // Generates the IO accessors for a fixed offset register.
     (@io $name:ident @ $offset:expr) => {
         #[allow(dead_code)]