From: Daniel Almeida <daniel.almeida@collabora.com>
To: Daniel Almeida <daniel.almeida@collabora.com>,
Alice Ryhl <aliceryhl@google.com>,
Danilo Krummrich <dakr@kernel.org>,
David Airlie <airlied@gmail.com>,
Simona Vetter <simona@ffwll.ch>
Cc: Kaiqi Guo <chaoji_xinren@163.com>,
Alexandre Courbot <acourbot@nvidia.com>,
dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 1/4] drm/tyr: use the conversion derives for the GPU_CONTROL enums
Date: Thu, 26 Nov 2026 17:10:43 -0300 [thread overview]
Message-ID: <20261126-tyr-enum-derives-v1-1-a487b0e1bee3@collabora.com> (raw)
In-Reply-To: <20261126-tyr-enum-derives-v1-0-a487b0e1bee3@collabora.com>
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
next prev parent reply other threads:[~2026-11-26 20:11 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261126-tyr-enum-derives-v1-1-a487b0e1bee3@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=chaoji_xinren@163.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®