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>,
"Benno Lossin" <lossin@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 04/13] drm/tyr: convert EntryHeader to `bitfield!`
Date: Wed, 25 Nov 2026 19:46:55 -0300 [thread overview]
Message-ID: <20261125-b4-tyr-as-casts-v1-4-2b2f53164d86@collabora.com> (raw)
In-Reply-To: <20261125-b4-tyr-as-casts-v1-0-2b2f53164d86@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, convert
`EntryHeader` to `bitfield!`.
`EntryHeader` wraps the `u32` that starts a firmware entry and extracts
its fields with hand-written masks and shifts. The entry type is
truncated to `u8` with `as` after masking, and the size is returned as
a `u32` that the parser casts to `usize`.
With `bitfield!`, which `SectionFlags` already uses, the fields are
`Bounded` values that carry their width. `EntryType` converts from the
8-bit field, and the size converts to `usize` with `From`, so both
casts go.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/fw/parser.rs | 49 ++++++++++++++++------------------------
1 file changed, 20 insertions(+), 29 deletions(-)
diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index c4d0ad1d7899..89de560bf192 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -16,8 +16,9 @@
};
use kernel::{
- bits::bit_u32,
+ bitfield,
device::Device,
+ num::Bounded,
prelude::*,
sizes::SZ_4K, //
};
@@ -234,12 +235,12 @@ fn parse_entry(&mut self, header_end: usize) -> Result<EntrySection> {
}
let entry_section = EntrySection {
- entry_hdr: EntryHeader(self.cursor.read_u32()?),
+ entry_hdr: EntryHeader::from_raw(self.cursor.read_u32()?),
inner: None,
};
let firmware_size = self.cursor.len();
- let entry_size = entry_section.entry_hdr.size() as usize;
+ let entry_size = usize::from(entry_section.entry_hdr.size());
if self.cursor.pos() % size_of::<u32>() != 0
|| entry_size % size_of::<u32>() != 0
@@ -291,7 +292,7 @@ fn parse_entry(&mut self, header_end: usize) -> Result<EntrySection> {
dev_err!(
self.cursor.dev,
"Failed to handle firmware entry type: {}",
- entry_section.entry_hdr.entry_type_raw()
+ entry_section.entry_hdr.entry_type_raw().get()
);
Err(EINVAL)
}
@@ -526,31 +527,21 @@ struct EntrySection {
inner: Option<ParsedSection>,
}
-/// Header for a firmware entry, packed into a single u32.
-///
-/// The entry header encodes the entry type, size, and optional flag in a
-/// 32-bit value with the following layout:
-/// - Bits 0-7: Entry type
-/// - Bits 8-15: Size in bytes
-/// - Bit 31: Optional flag
-struct EntryHeader(u32);
-
-impl EntryHeader {
- fn entry_type_raw(&self) -> u8 {
- (self.0 & 0xff) as u8
+bitfield! {
+ /// Header for a firmware entry, packed into a single u32.
+ struct EntryHeader(u32) {
+ /// Entry type.
+ 7:0 entry_type_raw;
+ /// Size in bytes.
+ 15:8 size;
+ /// Optional flag.
+ 31:31 optional => bool;
}
+}
+impl EntryHeader {
fn entry_type(&self) -> Result<EntryType> {
- let v = self.entry_type_raw();
- EntryType::try_from(v)
- }
-
- fn optional(&self) -> bool {
- self.0 & bit_u32(31) != 0
- }
-
- fn size(&self) -> u32 {
- self.0 >> 8 & 0xff
+ EntryType::try_from(self.entry_type_raw())
}
}
@@ -571,11 +562,11 @@ enum EntryType {
BuildInfoMetadata = 6,
}
-impl TryFrom<u8> for EntryType {
+impl TryFrom<Bounded<u32, 8>> for EntryType {
type Error = Error;
- fn try_from(value: u8) -> Result<Self, Self::Error> {
- match value {
+ 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),
--
2.56.0
next prev parent reply other threads:[~2026-11-25 22:48 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
2026-11-25 22:46 ` [PATCH 01/13] drm/tyr: convert SeatInfo::slot from u8 to usize Daniel Almeida
2026-11-25 22:46 ` [PATCH 02/13] drm/tyr: convert VmAsData::va_bits from u8 to u32 Daniel Almeida
2026-11-25 22:46 ` [PATCH 03/13] drm/tyr: convert the lock region exponents " Daniel Almeida
2026-11-25 22:46 ` Daniel Almeida [this message]
2026-11-25 22:46 ` [PATCH 05/13] drm/tyr: use SizeConstants for the u32 and u64 size constants Daniel Almeida
2026-11-25 22:46 ` [PATCH 06/13] drm/tyr: replace lossless `as` casts with From and FromSafeCast Daniel Almeida
2026-11-25 22:46 ` [PATCH 07/13] drm/tyr: use `cv!` for constant conversions Daniel Almeida
2026-11-25 22:46 ` [PATCH 08/13] drm/tyr: use VmMapFlags::all_bits() to validate map flags Daniel Almeida
2026-11-25 22:47 ` [PATCH 09/13] drm/tyr: justify the casts of enums to their integer value Daniel Almeida
2026-11-25 22:47 ` [PATCH 10/13] drm/tyr: add split_u64() for the 64-bit register values Daniel Almeida
2026-11-25 22:47 ` [PATCH 11/13] drm/tyr: return usize from get_pgsize() Daniel Almeida
2026-11-25 22:47 ` [PATCH 12/13] drm/tyr: justify the physical address cast in pt_map() Daniel Almeida
2026-11-25 22:47 ` [PATCH 13/13] drm/tyr: deny `clippy::as_conversions` 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=20261125-b4-tyr-as-casts-v1-4-2b2f53164d86@collabora.com \
--to=daniel.almeida@collabora.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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®