Skip to contentSkip to conversation

gpu: nova-core: use the TryFrom/Into derive macros

RFC PATCH In review Alexandre Courbot 2 participants 2 messages Sep 30, 2025

Subsystems (7)
lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

RFC PATCH 1/1 gpu: nova-core: use the TryFrom/Into derive macros

Use the new Tryfrom and Into derive macros to replace the boilerplate code used for conversion between register fields types and the primitive type they are represented in.

This removes a lot of boilerplate, with the only exception being fields encoded as booleans which still need manual implementations as there is no #[repr(bool)].

---
As requested on [1], this is how these macros are used in Nova-core. Note that it requires a tiny fix to the register! macro itself - I haven't included it because things are still moving and I want to base the fix on the state of -rc1. In any case, the content of this patch will be identical.

The only case not covered by the macros is the one of types that can be converted from/to booleans - but I think this one will be difficult to handle so we probably need to rely on manual implementations until Rust supports #[repr(bool)] or something.

[1] https://lore.kernel.org/rust-for-linux/CANiq72na_d6JQdyZ1S22mus3oom_jz93rpY+ubr4yOuvMY_fSA@mail.gmail.com/

---
 Documentation/gpu/nova/core/todo.rst |  21 -----
 drivers/gpu/nova-core/falcon.rs      | 151 ++++++-----------------------------
 drivers/gpu/nova-core/gpu.rs         |  33 ++------
 3 files changed, 31 insertions(+), 174 deletions(-)
Documentation/gpu/nova/core/todo.rst +0 -21
index 091a2fb78c6b53037e902f015f504b8819281860..5d6caede2cd5ab9e5e3923770a7cb974b5826670 100644
@@ -23,27 +23,6 @@ Enablement (Rust)
 Tasks that are not directly related to nova-core, but are preconditions in terms
 of required APIs.
 
-FromPrimitive API [FPRI]
-------------------------
-
-Sometimes the need arises to convert a number to a value of an enum or a
-structure.
-
-A good example from nova-core would be the ``Chipset`` enum type, which defines
-the value ``AD102``. When probing the GPU the value ``0x192`` can be read from a
-certain register indication the chipset AD102. Hence, the enum value ``AD102``
-should be derived from the number ``0x192``. Currently, nova-core uses a custom
-implementation (``Chipset::from_u32`` for this.
-
-Instead, it would be desirable to have something like the ``FromPrimitive``
-trait [1] from the num crate.
-
-Having this generalization also helps with implementing a generic macro that
-automatically generates the corresponding mappings between a value and a number.
-
-| Complexity: Beginner
-| Link: https://docs.rs/num/latest/num/trait.FromPrimitive.html
-
 Conversion from byte slices for types implementing FromBytes [TRSM]
 -------------------------------------------------------------------
 
drivers/gpu/nova-core/falcon.rs +26 -125

235 lines not on this page · raw message

drivers/gpu/nova-core/gpu.rs +5 -28
index 5da9ad72648340ed988184737219b14771f31b7a..4d90c821c5954fe4cf9b1562d853fddf5cedbce3 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 
-use kernel::{device, devres::Devres, error::code::*, pci, prelude::*, sync::Arc};
+use kernel::{device, devres::Devres, pci, prelude::*, sync::Arc};
 
 use crate::driver::Bar0;
 use crate::falcon::{gsp::Gsp as GspFalcon, sec2::Sec2 as Sec2Falcon, Falcon};
@@ -14,7 +14,8 @@ macro_rules! define_chipset {
     ({ $($variant:ident = $value:expr),* $(,)* }) =>
     {
         /// Enum representation of the GPU chipset.
-        #[derive(fmt::Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
+        #[derive(fmt::Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, kernel::macros::TryFrom)]
+        #[try_from(u32)]
         pub(crate) enum Chipset {
             $($variant = $value),*,
         }
@@ -42,18 +43,6 @@ pub(crate) const fn name(&self) -> &'static str {
             }
             );
         }
-
-        // TODO[FPRI]: replace with something like derive(FromPrimitive)
-        impl TryFrom<u32> for Chipset {
-            type Error = kernel::error::Error;
-
-            fn try_from(value: u32) -> Result<Self, Self::Error> {
-                match value {
-                    $( $value => Ok(Chipset::$variant), )*
-                    _ => Err(ENODEV),
-                }
-            }
-        }
     }
 }
 
@@ -110,26 +99,14 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 }
 
 /// Enum representation of the GPU generation.
-#[derive(fmt::Debug)]
+#[derive(fmt::Debug, kernel::macros::TryFrom)]
+#[try_from(u8)]
 pub(crate) enum Architecture {
     Turing = 0x16,
     Ampere = 0x17,
     Ada = 0x19,
 }
 
-impl TryFrom<u8> for Architecture {
-    type Error = Error;
-
-    fn try_from(value: u8) -> Result<Self> {
-        match value {
-            0x16 => Ok(Self::Turing),
-            0x17 => Ok(Self::Ampere),
-            0x19 => Ok(Self::Ada),
-            _ => Err(ENODEV),
-        }
-    }
-}
-
 pub(crate) struct Revision {
     major: u8,
     minor: u8,

---
base-commit: 299eb32863e584cfff7c6b667c3e92ae7d4d2bf9
change-id: 20250930-nova-tryfrom-a2cfa169dfd2

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
#1 lore raw
Quoted 2 lines · Alexandre Courbot

As requested on [1], this is how these macros are used in Nova-core.

Thanks!

Cheers,
Miguel