mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] drm/tyr: use the conversion derives for the enums
@ 2026-11-26 20:10 Daniel Almeida
  2026-11-26 20:10 ` [PATCH 1/4] drm/tyr: use the conversion derives for the GPU_CONTROL enums Daniel Almeida
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-26 20:10 UTC (permalink / raw)
  To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
	Simona Vetter
  Cc: Kaiqi Guo, Alexandre Courbot, dri-devel, rust-for-linux, linux-kernel

The `as` cast series [1] leaves 19 casts that convert an enum to its
integer value, each inside a hand-written conversion between the enum
and `Bounded`. This series derives these conversions with the `TryFrom`,
`From` and `Into` derives of Kaiqi's series "rust: add conversion
derives and exhaustive From support" [2], which removes the 19 casts.
Patch 4 also converts `EntryType`, the last enum with a hand-written
conversion, which had no cast.

This is based on drm-rust-next and depends on [1] and on patches 1 to 9
of [2]. I will follow up with a review of [2] soon.

[1] https://lore.kernel.org/all/20261125-b4-tyr-as-casts-v1-0-2b2f53164d86@collabora.com/
[2] https://lore.kernel.org/all/20260930002436.21395-1-chaoji_xinren@163.com/

---
Daniel Almeida (4):
      drm/tyr: use the conversion derives for the GPU_CONTROL enums
      drm/tyr: use the conversion derives for the MMU_AS_CONTROL enums
      drm/tyr: use the conversion derives for `CacheMode`
      drm/tyr: use the `TryFrom` derive for `EntryType`

 drivers/gpu/drm/tyr/fw.rs        |  30 +--
 drivers/gpu/drm/tyr/fw/parser.rs |  25 +-
 drivers/gpu/drm/tyr/regs.rs      | 486 ++++-----------------------------------
 3 files changed, 56 insertions(+), 485 deletions(-)
---
base-commit: 7402dd230f4497954b465145a22cff7a5a488629
change-id: 20261126-tyr-enum-derives-cbf729d01e5c
prerequisite-message-id: <20261125-b4-tyr-as-casts-v1-0-2b2f53164d86@collabora.com>
prerequisite-patch-id: 1d54819d2dac9c113331baef19e763cedb0ca517
prerequisite-patch-id: 016e94c56c10e511f6ac4c045f620207211b9f02
prerequisite-patch-id: 1899e0445dbd58f2a72b6f9bdcffba70b7e65ac4
prerequisite-patch-id: 4da30d4ec1abd60c6cbc5f5f108437abb4d5e3f3
prerequisite-patch-id: 04ac471e820674330ff6ed854c7691be3f4c7cef
prerequisite-patch-id: b62e387da808817a10df1a2977da60ecc630aeef
prerequisite-patch-id: 17733676a0b8814fabdec83ab20b749729149c4f
prerequisite-patch-id: 7a6862e6c23c43bdf11dff1f84032cd4209afc33
prerequisite-patch-id: f10354893c358db15456a84dc6113d24f03af0c0
prerequisite-patch-id: faee0bac4883bd31a407fb7fc66a40d5c2400c49
prerequisite-patch-id: 8c67c4dd9fc45169d882a349d0b27cf056197c6f
prerequisite-patch-id: b6ddee237c4a24b5c0242097aace0102820ac1db
prerequisite-patch-id: d1a6ccba37c85b7f62a7a06d73ce09813a5c6530
prerequisite-patch-id: c4eb8ca570fef755b66db1d2f5372cb562b8c1ec
prerequisite-patch-id: c997628e4e15a91a1db84772cb92a878d0f624ce
prerequisite-patch-id: d52e3ddfdb2de753538ec5c70acbf9ef5f276301
prerequisite-patch-id: 4000d922c41278ed2f1df0a4873942d21e3c49c0
prerequisite-patch-id: 65006ecf8ae3a3aa4d2439f6d0bf239d4672f208
prerequisite-patch-id: 565696b33359e46ffcba363b3e72c1e8df5584ba
prerequisite-patch-id: 36fd0e809e61e4c4f0d34027dfcabd4434030883
prerequisite-patch-id: e8bf99685c380a6cc232256b160699bf91cbf812
prerequisite-patch-id: 6cca0def1c915ef8a34c21913eee636a57ff6aa6

Best regards,
--  
Daniel Almeida <daniel.almeida@collabora.com>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] drm/tyr: use the conversion derives for the GPU_CONTROL enums
  2026-11-26 20:10 [PATCH 0/4] drm/tyr: use the conversion derives for the enums Daniel Almeida
@ 2026-11-26 20:10 ` Daniel Almeida
  2026-11-26 20:10 ` [PATCH 2/4] drm/tyr: use the conversion derives for the MMU_AS_CONTROL enums Daniel Almeida
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-26 20:10 UTC (permalink / raw)
  To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
	Simona Vetter
  Cc: Kaiqi Guo, Alexandre Courbot, dri-devel, rust-for-linux, linux-kernel

An `as` cast silently drops the bits that do not fit the target type,
and nothing at the cast shows whether that can happen. As part of a
larger effort to reduce the number of `as` casts in Tyr, derive the
conversions between the GPU_CONTROL enums and `Bounded` instead of
writing them by hand. This removes the 8 casts that convert one of
these enums to its integer value.

No functional change intended.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/regs.rs | 199 +++++---------------------------------------
 1 file changed, 21 insertions(+), 178 deletions(-)

diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 76892a911227..35656a81aae9 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -63,7 +63,11 @@ pub(crate) fn read_u64_no_tearing(lo_read: impl Fn() -> u32, hi_read: impl Fn()
 /// They are involved in GPU configuration and control.
 pub(crate) mod gpu_control {
     use kernel::{
-        num::Bounded,
+        macros::{
+            From,
+            Into,
+            TryFrom, //
+        },
         prelude::*,
         register,
         uapi, //
@@ -235,7 +239,8 @@ pub(crate) mod gpu_control {
     }
 
     /// Helpers for GPU_COMMAND Register
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+    #[convert(Bounded<u32, 8>)]
     #[repr(u8)]
     pub(crate) enum GpuCommand {
         /// No operation. This is the default value.
@@ -248,30 +253,9 @@ pub(crate) enum GpuCommand {
         ClearFault = 7,
     }
 
-    impl TryFrom<Bounded<u32, 8>> for GpuCommand {
-        type Error = Error;
-
-        fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
-            match val.get() {
-                0 => Ok(GpuCommand::Nop),
-                1 => Ok(GpuCommand::Reset),
-                4 => Ok(GpuCommand::FlushCaches),
-                7 => Ok(GpuCommand::ClearFault),
-                _ => Err(EINVAL),
-            }
-        }
-    }
-
-    impl From<GpuCommand> for Bounded<u32, 8> {
-        #[expect(clippy::as_conversions)]
-        fn from(cmd: GpuCommand) -> Self {
-            // CAST: `GpuCommand` is `#[repr(u8)]`, so the cast is lossless.
-            (cmd as u8).into()
-        }
-    }
-
     /// Reset mode for [`GPU_COMMAND::reset()`].
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+    #[convert(Bounded<u32, 4>)]
     #[repr(u8)]
     pub(crate) enum ResetMode {
         /// Stop all external bus interfaces, then reset the entire GPU.
@@ -280,28 +264,9 @@ pub(crate) enum ResetMode {
         HardReset = 2,
     }
 
-    impl TryFrom<Bounded<u32, 4>> for ResetMode {
-        type Error = Error;
-
-        fn try_from(val: Bounded<u32, 4>) -> Result<Self, Self::Error> {
-            match val.get() {
-                1 => Ok(ResetMode::SoftReset),
-                2 => Ok(ResetMode::HardReset),
-                _ => Err(EINVAL),
-            }
-        }
-    }
-
-    impl From<ResetMode> for Bounded<u32, 4> {
-        #[expect(clippy::as_conversions)]
-        fn from(mode: ResetMode) -> Self {
-            // CAST: `ResetMode` is `#[repr(u8)]`, so the cast is lossless.
-            Bounded::try_new(mode as u32).unwrap()
-        }
-    }
-
     /// Cache flush mode for [`GPU_COMMAND::flush_caches()`].
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+    #[convert(Bounded<u32, 4>)]
     #[repr(u8)]
     pub(crate) enum FlushMode {
         /// No flush.
@@ -314,28 +279,6 @@ pub(crate) enum FlushMode {
         CleanInvalidate = 3,
     }
 
-    impl TryFrom<Bounded<u32, 4>> for FlushMode {
-        type Error = Error;
-
-        fn try_from(val: Bounded<u32, 4>) -> Result<Self, Self::Error> {
-            match val.get() {
-                0 => Ok(FlushMode::None),
-                1 => Ok(FlushMode::Clean),
-                2 => Ok(FlushMode::Invalidate),
-                3 => Ok(FlushMode::CleanInvalidate),
-                _ => Err(EINVAL),
-            }
-        }
-    }
-
-    impl From<FlushMode> for Bounded<u32, 4> {
-        #[expect(clippy::as_conversions)]
-        fn from(mode: FlushMode) -> Self {
-            // CAST: `FlushMode` is `#[repr(u8)]`, so the cast is lossless.
-            Bounded::try_new(mode as u32).unwrap()
-        }
-    }
-
     register! {
         base: TyrRegisters;
 
@@ -421,7 +364,8 @@ pub(crate) fn clear_fault() -> Self {
         }
     }
 
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+    #[convert(Bounded<u32, 8>)]
     #[repr(u8)]
     pub(crate) enum ExceptionType {
         /// Exception type: No error.
@@ -436,30 +380,8 @@ pub(crate) enum ExceptionType {
         GpuCacheabilityFault = 0x8A,
     }
 
-    impl TryFrom<Bounded<u32, 8>> for ExceptionType {
-        type Error = Error;
-
-        fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
-            match val.get() {
-                0x00 => Ok(ExceptionType::Ok),
-                0x80 => Ok(ExceptionType::GpuBusFault),
-                0x88 => Ok(ExceptionType::GpuShareabilityFault),
-                0x89 => Ok(ExceptionType::SystemShareabilityFault),
-                0x8A => Ok(ExceptionType::GpuCacheabilityFault),
-                _ => Err(EINVAL),
-            }
-        }
-    }
-
-    impl From<ExceptionType> for Bounded<u32, 8> {
-        #[expect(clippy::as_conversions)]
-        fn from(exc: ExceptionType) -> Self {
-            // CAST: `ExceptionType` is `#[repr(u8)]`, so the cast is lossless.
-            (exc as u8).into()
-        }
-    }
-
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, From, Into)]
+    #[convert(Bounded<u32, 2>)]
     #[repr(u8)]
     pub(crate) enum AccessType {
         /// Access type: An atomic (read/write) transaction.
@@ -472,26 +394,6 @@ pub(crate) enum AccessType {
         Write = 3,
     }
 
-    impl From<Bounded<u32, 2>> for AccessType {
-        fn from(val: Bounded<u32, 2>) -> Self {
-            match val.get() {
-                0 => AccessType::Atomic,
-                1 => AccessType::Execute,
-                2 => AccessType::Read,
-                3 => AccessType::Write,
-                _ => unreachable!(),
-            }
-        }
-    }
-
-    impl From<AccessType> for Bounded<u32, 2> {
-        #[expect(clippy::as_conversions)]
-        fn from(access: AccessType) -> Self {
-            // CAST: `AccessType` is `#[repr(u8)]`, so the cast is lossless.
-            Bounded::try_new(access as u32).unwrap()
-        }
-    }
-
     register! {
         base: TyrRegisters;
 
@@ -769,7 +671,8 @@ fn from(access: AccessType) -> Self {
         }
     }
 
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+    #[convert(Bounded<u32, 32>)]
     #[repr(u8)]
     pub(crate) enum CoherencyMode {
         /// ACE-Lite coherency protocol.
@@ -780,27 +683,6 @@ pub(crate) enum CoherencyMode {
         None = cv!(uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_NONE => u8),
     }
 
-    impl TryFrom<Bounded<u32, 32>> for CoherencyMode {
-        type Error = Error;
-
-        fn try_from(val: Bounded<u32, 32>) -> Result<Self, Self::Error> {
-            match val.get() {
-                0 => Ok(CoherencyMode::AceLite),
-                1 => Ok(CoherencyMode::Ace),
-                31 => Ok(CoherencyMode::None),
-                _ => Err(EINVAL),
-            }
-        }
-    }
-
-    impl From<CoherencyMode> for Bounded<u32, 32> {
-        #[expect(clippy::as_conversions)]
-        fn from(mode: CoherencyMode) -> Self {
-            // CAST: `CoherencyMode` is `#[repr(u8)]`, so the cast is lossless.
-            (mode as u8).into()
-        }
-    }
-
     register! {
         base: TyrRegisters;
 
@@ -813,7 +695,8 @@ fn from(mode: CoherencyMode) -> Self {
     }
 
     /// Helpers for MCU_CONTROL register
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+    #[convert(Bounded<u32, 2>)]
     #[repr(u8)]
     pub(crate) enum McuControlMode {
         /// Disable the MCU.
@@ -824,27 +707,6 @@ pub(crate) enum McuControlMode {
         Auto = 2,
     }
 
-    impl TryFrom<Bounded<u32, 2>> for McuControlMode {
-        type Error = Error;
-
-        fn try_from(val: Bounded<u32, 2>) -> Result<Self, Self::Error> {
-            match val.get() {
-                0 => Ok(McuControlMode::Disable),
-                1 => Ok(McuControlMode::Enable),
-                2 => Ok(McuControlMode::Auto),
-                _ => Err(EINVAL),
-            }
-        }
-    }
-
-    impl From<McuControlMode> for Bounded<u32, 2> {
-        #[expect(clippy::as_conversions)]
-        fn from(mode: McuControlMode) -> Self {
-            // CAST: `McuControlMode` is `#[repr(u8)]`, so the cast is lossless.
-            Bounded::try_new(mode as u32).unwrap()
-        }
-    }
-
     register! {
         base: TyrRegisters;
 
@@ -856,7 +718,8 @@ fn from(mode: McuControlMode) -> Self {
     }
 
     /// Helpers for MCU_STATUS register
-    #[derive(Copy, Clone, Debug, PartialEq)]
+    #[derive(Copy, Clone, Debug, PartialEq, From, Into)]
+    #[convert(Bounded<u32, 2>)]
     #[repr(u8)]
     pub(crate) enum McuStatus {
         /// MCU is disabled.
@@ -870,26 +733,6 @@ pub(crate) enum McuStatus {
         Fatal = 3,
     }
 
-    impl From<Bounded<u32, 2>> for McuStatus {
-        fn from(val: Bounded<u32, 2>) -> Self {
-            match val.get() {
-                0 => McuStatus::Disabled,
-                1 => McuStatus::Enabled,
-                2 => McuStatus::Halt,
-                3 => McuStatus::Fatal,
-                _ => unreachable!(),
-            }
-        }
-    }
-
-    impl From<McuStatus> for Bounded<u32, 2> {
-        #[expect(clippy::as_conversions)]
-        fn from(status: McuStatus) -> Self {
-            // CAST: `McuStatus` is `#[repr(u8)]`, so the cast is lossless.
-            Bounded::try_new(status as u32).unwrap()
-        }
-    }
-
     register! {
         base: TyrRegisters;
 

-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] drm/tyr: use the conversion derives for the MMU_AS_CONTROL enums
  2026-11-26 20:10 [PATCH 0/4] drm/tyr: use the conversion derives for the enums Daniel Almeida
  2026-11-26 20:10 ` [PATCH 1/4] drm/tyr: use the conversion derives for the GPU_CONTROL enums Daniel Almeida
@ 2026-11-26 20:10 ` Daniel Almeida
  2026-11-26 20:10 ` [PATCH 3/4] drm/tyr: use the conversion derives for `CacheMode` Daniel Almeida
  2026-11-26 20:10 ` [PATCH 4/4] drm/tyr: use the `TryFrom` derive for `EntryType` Daniel Almeida
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-26 20:10 UTC (permalink / raw)
  To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
	Simona Vetter
  Cc: Kaiqi Guo, Alexandre Courbot, dri-devel, rust-for-linux, linux-kernel

An `as` cast silently drops the bits that do not fit the target type,
and nothing at the cast shows whether that can happen. As part of a
larger effort to reduce the number of `as` casts in Tyr, derive the
conversions between the MMU_AS_CONTROL enums and `Bounded` instead of
writing them by hand. This removes the 10 casts that convert one of
these enums to its integer value.

No functional change intended.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/regs.rs | 287 ++++----------------------------------------
 1 file changed, 25 insertions(+), 262 deletions(-)

diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 35656a81aae9..799384c8fde4 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -858,6 +858,11 @@ pub(crate) mod mmu_control {
     pub(crate) mod mmu_as_control {
         use kernel::{
             bitfield,
+            macros::{
+                From,
+                Into,
+                TryFrom, //
+            },
             num::Bounded,
             prelude::*,
             register, //
@@ -899,7 +904,8 @@ pub(crate) mod mmu_as_control {
 
         /// Helpers for MEMATTR Register.
 
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u8, 2>)]
         #[repr(u8)]
         pub(crate) enum AllocPolicySelect {
             /// Ignore ALLOC_R/ALLOC_W fields.
@@ -908,33 +914,14 @@ pub(crate) enum AllocPolicySelect {
             Alloc = 3,
         }
 
-        impl TryFrom<Bounded<u8, 2>> for AllocPolicySelect {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u8, 2>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    2 => Ok(Self::Impl),
-                    3 => Ok(Self::Alloc),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<AllocPolicySelect> for Bounded<u8, 2> {
-            #[expect(clippy::as_conversions)]
-            fn from(val: AllocPolicySelect) -> Self {
-                // CAST: `AllocPolicySelect` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(val as u8).unwrap()
-            }
-        }
-
         /// Coherency policy for memory attributes. Indicates the shareability of cached accesses.
         ///
         /// The hardware spec defines different interpretations of these values depending on
         /// whether TRANSCFG.MODE is set to IDENTITY or not. IDENTITY mode does not use translation
         /// tables (all input addresses map to the same output address); it is deprecated and not
         /// used by the driver. This enum assumes that TRANSCFG.MODE is not set to IDENTITY.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u8, 2>)]
         #[repr(u8)]
         pub(crate) enum Coherency {
             /// Midgard inner domain coherency.
@@ -954,28 +941,8 @@ pub(crate) enum Coherency {
             CpuInnerDomainShaderCoh = 2,
         }
 
-        impl TryFrom<Bounded<u8, 2>> for Coherency {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u8, 2>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    0 => Ok(Self::MidgardInnerDomain),
-                    1 => Ok(Self::CpuInnerDomain),
-                    2 => Ok(Self::CpuInnerDomainShaderCoh),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<Coherency> for Bounded<u8, 2> {
-            #[expect(clippy::as_conversions)]
-            fn from(val: Coherency) -> Self {
-                // CAST: `Coherency` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(val as u8).unwrap()
-            }
-        }
-
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, From, Into)]
+        #[convert(Bounded<u8, 2>)]
         #[repr(u8)]
         pub(crate) enum MemoryType {
             /// Normal memory (shared).
@@ -988,26 +955,6 @@ pub(crate) enum MemoryType {
             Fault = 3,
         }
 
-        impl From<Bounded<u8, 2>> for MemoryType {
-            fn from(val: Bounded<u8, 2>) -> Self {
-                match val.get() {
-                    0 => Self::Shared,
-                    1 => Self::NonCacheable,
-                    2 => Self::WriteBack,
-                    3 => Self::Fault,
-                    _ => unreachable!(),
-                }
-            }
-        }
-
-        impl From<MemoryType> for Bounded<u8, 2> {
-            #[expect(clippy::as_conversions)]
-            fn from(val: MemoryType) -> Self {
-                // CAST: `MemoryType` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(val as u8).unwrap()
-            }
-        }
-
         bitfield! {
             /// Stage 1 memory attributes (8-bit bitfield).
             ///
@@ -1222,7 +1169,8 @@ pub(crate) fn from_mair(mair: u64) -> Self {
         }
 
         /// Helpers for MMU COMMAND register.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u32, 8>)]
         #[repr(u8)]
         pub(crate) enum MmuCommand {
             /// No operation, nothing happens.
@@ -1239,30 +1187,6 @@ pub(crate) enum MmuCommand {
             FlushMem = 5,
         }
 
-        impl TryFrom<Bounded<u32, 8>> for MmuCommand {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    0 => Ok(MmuCommand::Nop),
-                    1 => Ok(MmuCommand::Update),
-                    2 => Ok(MmuCommand::Lock),
-                    3 => Ok(MmuCommand::Unlock),
-                    4 => Ok(MmuCommand::FlushPt),
-                    5 => Ok(MmuCommand::FlushMem),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<MmuCommand> for Bounded<u32, 8> {
-            #[expect(clippy::as_conversions)]
-            fn from(cmd: MmuCommand) -> Self {
-                // CAST: `MmuCommand` is `#[repr(u8)]`, so the cast is lossless.
-                (cmd as u8).into()
-            }
-        }
-
         register! {
             base: TyrRegisters;
 
@@ -1273,7 +1197,8 @@ fn from(cmd: MmuCommand) -> Self {
         }
 
         /// MMU exception types for FAULTSTATUS register.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u32, 8>)]
         #[repr(u8)]
         pub(crate) enum MmuExceptionType {
             /// No error.
@@ -1322,48 +1247,9 @@ pub(crate) enum MmuExceptionType {
             MemoryAttributeFault3 = 0xEB,
         }
 
-        impl TryFrom<Bounded<u32, 8>> for MmuExceptionType {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    0x00 => Ok(MmuExceptionType::Ok),
-                    0xC0 => Ok(MmuExceptionType::TranslationFault0),
-                    0xC1 => Ok(MmuExceptionType::TranslationFault1),
-                    0xC2 => Ok(MmuExceptionType::TranslationFault2),
-                    0xC3 => Ok(MmuExceptionType::TranslationFault3),
-                    0xC4 => Ok(MmuExceptionType::TranslationFault4),
-                    0xC8 => Ok(MmuExceptionType::PermissionFault0),
-                    0xC9 => Ok(MmuExceptionType::PermissionFault1),
-                    0xCA => Ok(MmuExceptionType::PermissionFault2),
-                    0xCB => Ok(MmuExceptionType::PermissionFault3),
-                    0xD9 => Ok(MmuExceptionType::AccessFlag1),
-                    0xDA => Ok(MmuExceptionType::AccessFlag2),
-                    0xDB => Ok(MmuExceptionType::AccessFlag3),
-                    0xE0 => Ok(MmuExceptionType::AddressSizeFaultIn),
-                    0xE4 => Ok(MmuExceptionType::AddressSizeFaultOut0),
-                    0xE5 => Ok(MmuExceptionType::AddressSizeFaultOut1),
-                    0xE6 => Ok(MmuExceptionType::AddressSizeFaultOut2),
-                    0xE7 => Ok(MmuExceptionType::AddressSizeFaultOut3),
-                    0xE8 => Ok(MmuExceptionType::MemoryAttributeFault0),
-                    0xE9 => Ok(MmuExceptionType::MemoryAttributeFault1),
-                    0xEA => Ok(MmuExceptionType::MemoryAttributeFault2),
-                    0xEB => Ok(MmuExceptionType::MemoryAttributeFault3),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<MmuExceptionType> for Bounded<u32, 8> {
-            #[expect(clippy::as_conversions)]
-            fn from(exc: MmuExceptionType) -> Self {
-                // CAST: `MmuExceptionType` is `#[repr(u8)]`, so the cast is lossless.
-                (exc as u8).into()
-            }
-        }
-
         /// Access type for MMU faults.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, From, Into)]
+        #[convert(Bounded<u32, 2>)]
         #[repr(u8)]
         pub(crate) enum MmuAccessType {
             /// An atomic (read/write) transaction.
@@ -1376,26 +1262,6 @@ pub(crate) enum MmuAccessType {
             Write = 3,
         }
 
-        impl From<Bounded<u32, 2>> for MmuAccessType {
-            fn from(val: Bounded<u32, 2>) -> Self {
-                match val.get() {
-                    0 => MmuAccessType::Atomic,
-                    1 => MmuAccessType::Execute,
-                    2 => MmuAccessType::Read,
-                    3 => MmuAccessType::Write,
-                    _ => unreachable!(),
-                }
-            }
-        }
-
-        impl From<MmuAccessType> for Bounded<u32, 2> {
-            #[expect(clippy::as_conversions)]
-            fn from(access: MmuAccessType) -> Self {
-                // CAST: `MmuAccessType` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(access as u32).unwrap()
-            }
-        }
-
         register! {
             base: TyrRegisters;
 
@@ -1430,7 +1296,8 @@ fn from(access: MmuAccessType) -> Self {
         /// Helpers for TRANSCFG register.
         ///
         /// Address space mode for TRANSCFG register.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u64, 4>)]
         #[repr(u8)]
         pub(crate) enum AddressSpaceMode {
             /// The MMU forces all memory access to fail with a decode fault.
@@ -1443,30 +1310,9 @@ pub(crate) enum AddressSpaceMode {
             Aarch64_64K = 8,
         }
 
-        impl TryFrom<Bounded<u64, 4>> for AddressSpaceMode {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u64, 4>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    1 => Ok(AddressSpaceMode::Unmapped),
-                    2 => Ok(AddressSpaceMode::Identity),
-                    6 => Ok(AddressSpaceMode::Aarch64_4K),
-                    8 => Ok(AddressSpaceMode::Aarch64_64K),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<AddressSpaceMode> for Bounded<u64, 4> {
-            #[expect(clippy::as_conversions)]
-            fn from(mode: AddressSpaceMode) -> Self {
-                // CAST: `AddressSpaceMode` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(mode as u64).unwrap()
-            }
-        }
-
         /// Input address range restriction for TRANSCFG register.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u64, 5>)]
         #[repr(u8)]
         pub(crate) enum InaBits {
             /// Invalid VA range (reset value).
@@ -1521,51 +1367,9 @@ pub(crate) enum InaBits {
             Bits25 = 30,
         }
 
-        impl TryFrom<Bounded<u64, 5>> for InaBits {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u64, 5>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    0 => Ok(InaBits::Reset),
-                    7 => Ok(InaBits::Bits48),
-                    8 => Ok(InaBits::Bits47),
-                    9 => Ok(InaBits::Bits46),
-                    10 => Ok(InaBits::Bits45),
-                    11 => Ok(InaBits::Bits44),
-                    12 => Ok(InaBits::Bits43),
-                    13 => Ok(InaBits::Bits42),
-                    14 => Ok(InaBits::Bits41),
-                    15 => Ok(InaBits::Bits40),
-                    16 => Ok(InaBits::Bits39),
-                    17 => Ok(InaBits::Bits38),
-                    18 => Ok(InaBits::Bits37),
-                    19 => Ok(InaBits::Bits36),
-                    20 => Ok(InaBits::Bits35),
-                    21 => Ok(InaBits::Bits34),
-                    22 => Ok(InaBits::Bits33),
-                    23 => Ok(InaBits::Bits32),
-                    24 => Ok(InaBits::Bits31),
-                    25 => Ok(InaBits::Bits30),
-                    26 => Ok(InaBits::Bits29),
-                    27 => Ok(InaBits::Bits28),
-                    28 => Ok(InaBits::Bits27),
-                    29 => Ok(InaBits::Bits26),
-                    30 => Ok(InaBits::Bits25),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<InaBits> for Bounded<u64, 5> {
-            #[expect(clippy::as_conversions)]
-            fn from(bits: InaBits) -> Self {
-                // CAST: `InaBits` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(bits as u64).unwrap()
-            }
-        }
-
         /// Translation table memory attributes for TRANSCFG register.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u64, 2>)]
         #[repr(u8)]
         pub(crate) enum PtwMemattr {
             /// Invalid (reset value, not valid for enabled address space).
@@ -1576,29 +1380,9 @@ pub(crate) enum PtwMemattr {
             WriteBack = 2,
         }
 
-        impl TryFrom<Bounded<u64, 2>> for PtwMemattr {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u64, 2>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    0 => Ok(PtwMemattr::Invalid),
-                    1 => Ok(PtwMemattr::NonCacheable),
-                    2 => Ok(PtwMemattr::WriteBack),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<PtwMemattr> for Bounded<u64, 2> {
-            #[expect(clippy::as_conversions)]
-            fn from(attr: PtwMemattr) -> Self {
-                // CAST: `PtwMemattr` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(attr as u64).unwrap()
-            }
-        }
-
         /// Translation table memory shareability for TRANSCFG register.
-        #[derive(Copy, Clone, Debug, PartialEq)]
+        #[derive(Copy, Clone, Debug, PartialEq, TryFrom, Into)]
+        #[convert(Bounded<u64, 2>)]
         #[repr(u8)]
         #[allow(clippy::enum_variant_names)]
         pub(crate) enum PtwShareability {
@@ -1610,27 +1394,6 @@ pub(crate) enum PtwShareability {
             InnerShareable = 3,
         }
 
-        impl TryFrom<Bounded<u64, 2>> for PtwShareability {
-            type Error = Error;
-
-            fn try_from(val: Bounded<u64, 2>) -> Result<Self, Self::Error> {
-                match val.get() {
-                    0 => Ok(PtwShareability::NonShareable),
-                    2 => Ok(PtwShareability::OuterShareable),
-                    3 => Ok(PtwShareability::InnerShareable),
-                    _ => Err(EINVAL),
-                }
-            }
-        }
-
-        impl From<PtwShareability> for Bounded<u64, 2> {
-            #[expect(clippy::as_conversions)]
-            fn from(sh: PtwShareability) -> Self {
-                // CAST: `PtwShareability` is `#[repr(u8)]`, so the cast is lossless.
-                Bounded::try_new(sh as u64).unwrap()
-            }
-        }
-
         register! {
             base: TyrRegisters;
 

-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] drm/tyr: use the conversion derives for `CacheMode`
  2026-11-26 20:10 [PATCH 0/4] drm/tyr: use the conversion derives for the enums Daniel Almeida
  2026-11-26 20:10 ` [PATCH 1/4] drm/tyr: use the conversion derives for the GPU_CONTROL enums Daniel Almeida
  2026-11-26 20:10 ` [PATCH 2/4] drm/tyr: use the conversion derives for the MMU_AS_CONTROL enums Daniel Almeida
@ 2026-11-26 20:10 ` Daniel Almeida
  2026-11-26 20:10 ` [PATCH 4/4] drm/tyr: use the `TryFrom` derive for `EntryType` Daniel Almeida
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-26 20:10 UTC (permalink / raw)
  To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
	Simona Vetter
  Cc: Kaiqi Guo, Alexandre Courbot, dri-devel, rust-for-linux, linux-kernel

An `as` cast silently drops the bits that do not fit the target type,
and nothing at the cast shows whether that can happen. As part of a
larger effort to reduce the number of `as` casts in Tyr, derive the
conversions between `CacheMode` and `Bounded` instead of writing them
by hand. This removes the cast that converts `CacheMode` to its integer
value.

No functional change intended.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/fw.rs | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)

diff --git a/drivers/gpu/drm/tyr/fw.rs b/drivers/gpu/drm/tyr/fw.rs
index 2fa9852e0b83..cf880f685922 100644
--- a/drivers/gpu/drm/tyr/fw.rs
+++ b/drivers/gpu/drm/tyr/fw.rs
@@ -26,10 +26,11 @@
         poll,
         Io, //
     },
-    num::{
-        casts::FromSafeCast,
-        Bounded, //
+    macros::{
+        From,
+        Into, //
     },
+    num::casts::FromSafeCast,
     prelude::*,
     str::CString,
     sync::{
@@ -76,7 +77,8 @@
 
 pub(super) const CSF_MCU_SHARED_REGION_START: u32 = 0x04000000;
 
-#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq, From, Into)]
+#[convert(Bounded<u32, 2>)]
 #[repr(u8)]
 pub(super) enum CacheMode {
     None = 0,
@@ -85,26 +87,6 @@ pub(super) enum CacheMode {
     CachedCoherent = 3,
 }
 
-impl From<Bounded<u32, 2>> for CacheMode {
-    fn from(value: Bounded<u32, 2>) -> Self {
-        match value.get() {
-            0 => Self::None,
-            1 => Self::Cached,
-            2 => Self::UncachedCoherent,
-            3 => Self::CachedCoherent,
-            _ => unreachable!(),
-        }
-    }
-}
-
-impl From<CacheMode> for Bounded<u32, 2> {
-    #[expect(clippy::as_conversions)]
-    fn from(value: CacheMode) -> Self {
-        // CAST: `CacheMode` is `#[repr(u8)]`, so the cast is lossless.
-        Bounded::try_new(value as u32).unwrap()
-    }
-}
-
 bitfield! {
     pub(super) struct SectionFlags(u32) {
         0:0 read => bool;

-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] drm/tyr: use the `TryFrom` derive for `EntryType`
  2026-11-26 20:10 [PATCH 0/4] drm/tyr: use the conversion derives for the enums Daniel Almeida
                   ` (2 preceding siblings ...)
  2026-11-26 20:10 ` [PATCH 3/4] drm/tyr: use the conversion derives for `CacheMode` Daniel Almeida
@ 2026-11-26 20:10 ` Daniel Almeida
  3 siblings, 0 replies; 5+ messages in thread
From: Daniel Almeida @ 2026-11-26 20:10 UTC (permalink / raw)
  To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
	Simona Vetter
  Cc: Kaiqi Guo, Alexandre Courbot, dri-devel, rust-for-linux, linux-kernel

`EntryType` is the last enum in Tyr whose conversion from `Bounded` is
written by hand. Derive it as well, so that all of them use the
conversion derives.

No functional change intended.

Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 drivers/gpu/drm/tyr/fw/parser.rs | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)

diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index 6576bc870982..cf8a815e8ab8 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -18,10 +18,8 @@
 use kernel::{
     bitfield,
     device::Device,
-    num::{
-        casts::FromSafeCast,
-        Bounded, //
-    },
+    macros::TryFrom,
+    num::casts::FromSafeCast,
     prelude::*,
     sizes::SizeConstants, //
 };
@@ -548,7 +546,8 @@ fn entry_type(&self) -> Result<EntryType> {
     }
 }
 
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Copy, Debug, TryFrom)]
+#[try_from(Bounded<u32, 8>)]
 #[repr(u8)]
 enum EntryType {
     /// Host <-> FW interface.
@@ -564,19 +563,3 @@ enum EntryType {
     /// Metadata about how the FW binary was built.
     BuildInfoMetadata = 6,
 }
-
-impl TryFrom<Bounded<u32, 8>> for EntryType {
-    type Error = Error;
-
-    fn try_from(value: Bounded<u32, 8>) -> Result<Self, Self::Error> {
-        match value.get() {
-            0 => Ok(EntryType::Iface),
-            1 => Ok(EntryType::Config),
-            2 => Ok(EntryType::FutfTest),
-            3 => Ok(EntryType::TraceBuffer),
-            4 => Ok(EntryType::TimelineMetadata),
-            6 => Ok(EntryType::BuildInfoMetadata),
-            _ => Err(EINVAL),
-        }
-    }
-}

-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-26 20:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 20:10 [PATCH 0/4] drm/tyr: use the conversion derives for the enums Daniel Almeida
2026-11-26 20:10 ` [PATCH 1/4] drm/tyr: use the conversion derives for the GPU_CONTROL enums Daniel Almeida
2026-11-26 20:10 ` [PATCH 2/4] drm/tyr: use the conversion derives for the MMU_AS_CONTROL enums Daniel Almeida
2026-11-26 20:10 ` [PATCH 3/4] drm/tyr: use the conversion derives for `CacheMode` Daniel Almeida
2026-11-26 20:10 ` [PATCH 4/4] drm/tyr: use the `TryFrom` derive for `EntryType` Daniel Almeida

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®