* [PATCH 01/13] drm/tyr: convert SeatInfo::slot from u8 to usize
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
@ 2026-11-25 22:46 ` Daniel Almeida
2026-11-25 22:46 ` [PATCH 02/13] drm/tyr: convert VmAsData::va_bits from u8 to u32 Daniel Almeida
` (12 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, convert
`SeatInfo::slot` from `u8` to `usize`.
The slot index is stored as a `u8`, while the code that uses it takes a
`usize`. The casts follow from that choice. The index is cast with `as`
on the way into a `SeatInfo` and on the way out, and
`SlotManager::new()` rejects more than 256 slots so that the cast to
`u8` cannot truncate.
Store the index as `usize`, which removes the casts and the limit.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/mmu/address_space.rs | 15 +++------------
drivers/gpu/drm/tyr/slot.rs | 23 +++++++++--------------
2 files changed, 12 insertions(+), 26 deletions(-)
diff --git a/drivers/gpu/drm/tyr/mmu/address_space.rs b/drivers/gpu/drm/tyr/mmu/address_space.rs
index 76e3d2df6afd..dd6377bee989 100644
--- a/drivers/gpu/drm/tyr/mmu/address_space.rs
+++ b/drivers/gpu/drm/tyr/mmu/address_space.rs
@@ -467,10 +467,7 @@ pub(super) fn start_vm_update(
) -> Result {
let seat = vm_as_data.as_seat.access(self);
match seat.slot() {
- Some(slot) => {
- let as_nr = slot as usize;
- self.as_start_update(as_nr, region)
- }
+ Some(slot) => self.as_start_update(slot, region),
_ => Ok(()),
}
}
@@ -479,10 +476,7 @@ pub(super) fn start_vm_update(
pub(super) fn end_vm_update(&mut self, vm_as_data: &VmAsData<'drm>) -> Result {
let seat = vm_as_data.as_seat.access(self);
match seat.slot() {
- Some(slot) => {
- let as_nr = slot as usize;
- self.as_end_update(as_nr)
- }
+ Some(slot) => self.as_end_update(slot),
_ => Ok(()),
}
}
@@ -491,10 +485,7 @@ pub(super) fn end_vm_update(&mut self, vm_as_data: &VmAsData<'drm>) -> Result {
pub(super) fn flush_vm(&mut self, vm_as_data: &VmAsData<'drm>) -> Result {
let seat = vm_as_data.as_seat.access(self);
match seat.slot() {
- Some(slot) => {
- let as_nr = slot as usize;
- self.as_flush(as_nr)
- }
+ Some(slot) => self.as_flush(slot),
_ => Ok(()),
}
}
diff --git a/drivers/gpu/drm/tyr/slot.rs b/drivers/gpu/drm/tyr/slot.rs
index d194ead53f71..0e153dc025c5 100644
--- a/drivers/gpu/drm/tyr/slot.rs
+++ b/drivers/gpu/drm/tyr/slot.rs
@@ -47,7 +47,7 @@ pub(crate) struct SeatInfo {
/// it means the object has been evicted from the hardware slot,
/// and a new slot needs to be acquired to make this object
/// resident again.
- slot: u8,
+ slot: usize,
/// Sequence number encoding the last time this seat was active.
/// We also use it to check if a slot is still bound to a seat.
@@ -98,7 +98,7 @@ impl Seat {
/// If the seat is not `Seat::Active` we can't trust the
/// `SeatInfo`. In that case `None` is returned, otherwise
/// `Some(SeatInfo::slot)` is returned.
- pub(crate) fn slot(&self) -> Option<u8> {
+ pub(crate) fn slot(&self) -> Option<usize> {
match self {
Self::Active(info) => Some(info.slot),
_ => None,
@@ -174,10 +174,6 @@ pub(crate) fn new(manager: T, slot_count: usize) -> Result<Self> {
if slot_count > MAX_SLOTS {
return Err(EINVAL);
}
- // Since the slot index is stored in SeatInfo as a u8, the maximum number of slots is 256.
- if slot_count > u8::MAX as usize + 1 {
- return Err(EINVAL);
- }
Ok(Self {
manager,
@@ -193,7 +189,7 @@ fn record_active_slot(&mut self, slot_idx: usize, slot_data: T::SlotData) {
let cur_seqno = self.use_seqno;
*T::seat(&slot_data).access_mut(self) = Seat::Active(SeatInfo {
- slot: slot_idx as u8,
+ slot: slot_idx,
seqno: cur_seqno,
});
@@ -221,7 +217,7 @@ fn reactivate_slot(&mut self, slot_idx: usize, slot_data: &T::SlotData) -> Resul
};
*T::seat(slot_data).access_mut(self) = Seat::Active(SeatInfo {
- slot: slot_idx as u8,
+ slot: slot_idx,
seqno: cur_seqno,
});
@@ -325,8 +321,8 @@ fn evict_slot(&mut self, slot_idx: usize, locked_seat: &LockedSeat<T, MAX_SLOTS>
/// If they don't match, the seat is stale and is reset to `NoSeat`.
fn check_seat(&mut self, locked_seat: &LockedSeat<T, MAX_SLOTS>) {
let (slot_idx, seat_seqno, is_active) = match locked_seat.access(self) {
- Seat::Active(seat_info) => (seat_info.slot as usize, seat_info.seqno, true),
- Seat::Idle(seat_info) => (seat_info.slot as usize, seat_info.seqno, false),
+ Seat::Active(seat_info) => (seat_info.slot, seat_info.seqno, true),
+ Seat::Idle(seat_info) => (seat_info.slot, seat_info.seqno, false),
_ => return,
};
@@ -353,7 +349,7 @@ pub(crate) fn activate(&mut self, slot_data: T::SlotData) -> Result {
// Copy out only the slot index so the borrow of slot_data ends here.
let slot_idx = match T::seat(&slot_data).access(self) {
- Seat::Active(seat_info) | Seat::Idle(seat_info) => Some(seat_info.slot as usize),
+ Seat::Active(seat_info) | Seat::Idle(seat_info) => Some(seat_info.slot),
Seat::NoSeat => None,
};
@@ -368,7 +364,7 @@ pub(crate) fn activate(&mut self, slot_data: T::SlotData) -> Result {
pub(crate) fn idle(&mut self, locked_seat: &LockedSeat<T, MAX_SLOTS>) -> Result {
self.check_seat(locked_seat);
if let Seat::Active(seat_info) = locked_seat.access(self) {
- self.idle_slot(seat_info.slot as usize, locked_seat)?;
+ self.idle_slot(seat_info.slot, locked_seat)?;
}
Ok(())
}
@@ -379,8 +375,7 @@ pub(crate) fn evict(&mut self, locked_seat: &LockedSeat<T, MAX_SLOTS>) -> Result
match locked_seat.access(self) {
Seat::Active(seat_info) | Seat::Idle(seat_info) => {
- let slot_idx = seat_info.slot as usize;
- self.evict_slot(slot_idx, locked_seat)?;
+ self.evict_slot(seat_info.slot, locked_seat)?;
}
_ => (),
}
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 02/13] drm/tyr: convert VmAsData::va_bits from u8 to u32
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 ` Daniel Almeida
2026-11-25 22:46 ` [PATCH 03/13] drm/tyr: convert the lock region exponents " Daniel Almeida
` (11 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, convert
`VmAsData::va_bits` from `u8` to `u32`.
`VmAsData::new()` receives the number of virtual address bits as a
`u32` and narrows it with `as` to store it in a `u8` field. The only
reader of the field, `as_config()`, widens it again to `u64`.
Store it as `u32`, which removes the cast.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/mmu/address_space.rs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/gpu/drm/tyr/mmu/address_space.rs b/drivers/gpu/drm/tyr/mmu/address_space.rs
index dd6377bee989..a388c0087b20 100644
--- a/drivers/gpu/drm/tyr/mmu/address_space.rs
+++ b/drivers/gpu/drm/tyr/mmu/address_space.rs
@@ -78,7 +78,7 @@ pub(crate) struct VmAsData<'drm> {
as_seat: LockedSeat<AddressSpaceManager<'drm>, MAX_AS>,
/// Virtual address bits for this address space.
- va_bits: u8,
+ va_bits: u32,
/// The page table which maps GPU virtual addresses to physical addresses for this VM.
#[pin]
@@ -105,7 +105,7 @@ pub(crate) fn new<'a>(
try_pin_init!(Self {
as_seat: LockedBy::new(&mmu.as_manager, Seat::NoSeat),
- va_bits: va_bits as u8,
+ va_bits,
page_table <- page_table_init,
}? Error)
}
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 03/13] drm/tyr: convert the lock region exponents from u8 to u32
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 ` Daniel Almeida
2026-11-25 22:46 ` [PATCH 04/13] drm/tyr: convert EntryHeader to `bitfield!` Daniel Almeida
` (10 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, convert the
lock region exponents in `as_start_update()` from `u8` to `u32`.
`as_start_update()` works out the size of the region to lock as a power
of two. It takes the exponents from `trailing_zeros()` and
`leading_zeros()`, which return `u32`, and narrows them to `u8` with
`as`. The result is then widened back to `u32` for `checked_shl()`.
Keep the exponents as `u32`, which removes the casts.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/mmu/address_space.rs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/tyr/mmu/address_space.rs b/drivers/gpu/drm/tyr/mmu/address_space.rs
index a388c0087b20..f6ccb0d7e3ed 100644
--- a/drivers/gpu/drm/tyr/mmu/address_space.rs
+++ b/drivers/gpu/drm/tyr/mmu/address_space.rs
@@ -362,7 +362,7 @@ fn as_start_update(&mut self, as_nr: usize, region: &Range<u64>) -> Result {
// Count the number of trailing zero bits (zeros at the right/least-significant
// end of the binary representation). For a power-of-two value, this equals the
// base-2 exponent (e.g., 32 KiB = 2^15 → 15).
- let lock_region_min_size_log2 = lock_region_min_size.trailing_zeros() as u8;
+ let lock_region_min_size_log2 = lock_region_min_size.trailing_zeros();
// XOR the first and last addresses to identify which bits differ between them.
// The highest set bit in the result determines the exponent of the smallest
@@ -373,11 +373,11 @@ fn as_start_update(&mut self, as_nr: usize, region: &Range<u64>) -> Result {
// highest set bit in 0x3FFF is bit 13
// minimum region size = 2^(13 + 1) = 16 KiB
let addr_xor = region.start ^ (region.end - 1);
- let region_size_log2 = 64 - addr_xor.leading_zeros() as u8;
+ let region_size_log2 = 64 - addr_xor.leading_zeros();
let lock_region_log2 = core::cmp::max(region_size_log2, lock_region_min_size_log2);
- let lock_region_size = 1u64.checked_shl(lock_region_log2.into()).ok_or(EINVAL)?;
+ let lock_region_size = 1u64.checked_shl(lock_region_log2).ok_or(EINVAL)?;
// Align the LOCKADDR base address down to the lock region size (1 << lock_region_log2).
//
// The MMU ignores the low lock_region_log2 bits of LOCKADDR base, so ensure
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 04/13] drm/tyr: convert EntryHeader to `bitfield!`
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (2 preceding siblings ...)
2026-11-25 22:46 ` [PATCH 03/13] drm/tyr: convert the lock region exponents " Daniel Almeida
@ 2026-11-25 22:46 ` Daniel Almeida
2026-11-25 22:46 ` [PATCH 05/13] drm/tyr: use SizeConstants for the u32 and u64 size constants Daniel Almeida
` (9 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, 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
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 05/13] drm/tyr: use SizeConstants for the u32 and u64 size constants
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (3 preceding siblings ...)
2026-11-25 22:46 ` [PATCH 04/13] drm/tyr: convert EntryHeader to `bitfield!` Daniel Almeida
@ 2026-11-25 22:46 ` Daniel Almeida
2026-11-25 22:46 ` [PATCH 06/13] drm/tyr: replace lossless `as` casts with From and FromSafeCast Daniel Almeida
` (8 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, use the
`SizeConstants` trait for the size constants that are not used as
`usize`.
The `SZ_*` constants are `usize`, so an expression that mixes one with
a value of another integer type needs a cast on one side.
The `SizeConstants` trait provides the same constants as `u32` and
`u64`, so that neither side has to be cast:
va % SZ_4K as u64 -> va % u64::SZ_4K
va.start as usize % SZ_4K -> va.start % u32::SZ_4K
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/fw/parser.rs | 4 ++--
drivers/gpu/drm/tyr/vm.rs | 32 ++++++++++++++------------------
2 files changed, 16 insertions(+), 20 deletions(-)
diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index 89de560bf192..6ff681aa78d1 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -20,7 +20,7 @@
device::Device,
num::Bounded,
prelude::*,
- sizes::SZ_4K, //
+ sizes::SizeConstants, //
};
use crate::{
@@ -334,7 +334,7 @@ fn parse_section_entry(
return Err(EINVAL);
}
- if section_hdr.va.start as usize % SZ_4K != 0 || section_hdr.va.end as usize % SZ_4K != 0 {
+ if section_hdr.va.start % u32::SZ_4K != 0 || section_hdr.va.end % u32::SZ_4K != 0 {
dev_err!(
entry_cursor.dev,
"Firmware virtual address range {:#x}..{:#x} is not page aligned",
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 1dd11b2b1253..72c8593c489e 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -42,11 +42,7 @@
},
new_mutex,
prelude::*,
- sizes::{
- SZ_1G,
- SZ_2M,
- SZ_4K, //
- },
+ sizes::SizeConstants,
sync::{
aref::ARef,
Arc,
@@ -491,9 +487,9 @@ pub(crate) fn map_bo_range(
flags: VmMapFlags,
) -> Result {
if map_size == 0
- || va % SZ_4K as u64 != 0
- || bo_offset % SZ_4K as u64 != 0
- || map_size % SZ_4K as u64 != 0
+ || va % u64::SZ_4K != 0
+ || bo_offset % u64::SZ_4K != 0
+ || map_size % u64::SZ_4K != 0
{
return Err(EINVAL);
}
@@ -545,7 +541,7 @@ pub(crate) fn map_bo_range(
/// This removes any existing mappings in the specified range, freeing the
/// virtual address space for reuse.
pub(crate) fn unmap_range(&self, va: u64, size: u64) -> Result {
- if size == 0 || va % SZ_4K as u64 != 0 || size % SZ_4K as u64 != 0 {
+ if size == 0 || va % u64::SZ_4K != 0 || size % u64::SZ_4K != 0 {
return Err(EINVAL);
}
@@ -791,27 +787,27 @@ fn sm_step_remap<'op>(
/// Returns the page size (4KB or 2MB) and the number of pages that can be mapped at that size.
fn get_pgsize(addr: u64, size: u64) -> (u64, u64) {
// Get the distance to the next boundary of 2MB block
- let blk_offset_2m = addr.wrapping_neg() % (SZ_2M as u64);
+ let blk_offset_2m = addr.wrapping_neg() % u64::SZ_2M;
// Use 4K blocks if the address is not 2MB aligned, or we have less than 2MB to map
- if blk_offset_2m != 0 || size < SZ_2M as u64 {
+ if blk_offset_2m != 0 || size < u64::SZ_2M {
let pgcount = if blk_offset_2m == 0 {
- size / SZ_4K as u64
+ size / u64::SZ_4K
} else {
- u64::min(blk_offset_2m, size) / SZ_4K as u64
+ u64::min(blk_offset_2m, size) / u64::SZ_4K
};
- return (SZ_4K as u64, pgcount);
+ return (u64::SZ_4K, pgcount);
}
- let blk_offset_1g = addr.wrapping_neg() % (SZ_1G as u64);
+ let blk_offset_1g = addr.wrapping_neg() % u64::SZ_1G;
let blk_offset = if blk_offset_1g == 0 {
- SZ_1G as u64
+ u64::SZ_1G
} else {
blk_offset_1g
};
- let pgcount = u64::min(blk_offset, size) / SZ_2M as u64;
+ let pgcount = u64::min(blk_offset, size) / u64::SZ_2M;
- (SZ_2M as u64, pgcount)
+ (u64::SZ_2M, pgcount)
}
/// Maps a physical address range into the page table at the specified virtual address.
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 06/13] drm/tyr: replace lossless `as` casts with From and FromSafeCast
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (4 preceding siblings ...)
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 ` Daniel Almeida
2026-11-25 22:46 ` [PATCH 07/13] drm/tyr: use `cv!` for constant conversions Daniel Almeida
` (7 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, replace the
casts from `usize` to `u64`, from `u32` to `usize` and from a
`Bounded<u64, 8>` to `u8` with conversions that cannot lose bits:
mapped as u64 -> u64::from_safe_cast(mapped)
fw_header.size as usize -> usize::from_safe_cast(fw_header.size)
val.get() as u8 -> u8::from(val)
`FromSafeCast` is only implemented where no value is lost on any
architecture, and `From` for a `Bounded` only where all its bits fit.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/fw.rs | 9 ++++++---
drivers/gpu/drm/tyr/fw/parser.rs | 11 +++++++----
drivers/gpu/drm/tyr/regs.rs | 2 +-
drivers/gpu/drm/tyr/vm.rs | 9 +++++----
4 files changed, 19 insertions(+), 12 deletions(-)
diff --git a/drivers/gpu/drm/tyr/fw.rs b/drivers/gpu/drm/tyr/fw.rs
index aea151ff1f93..14cfe2884d5a 100644
--- a/drivers/gpu/drm/tyr/fw.rs
+++ b/drivers/gpu/drm/tyr/fw.rs
@@ -26,7 +26,10 @@
poll,
Io, //
},
- num::Bounded,
+ num::{
+ casts::FromSafeCast,
+ Bounded, //
+ },
prelude::*,
str::CString,
sync::{
@@ -238,8 +241,8 @@ pub(crate) fn new(
parsed.vm_map_flags,
)?;
- let section_start = parsed.data_range.start as usize;
- let section_end = parsed.data_range.end as usize;
+ let section_start = usize::from_safe_cast(parsed.data_range.start);
+ let section_end = usize::from_safe_cast(parsed.data_range.end);
let mut data = KVec::new();
// Ensure that the firmware slice is not out of bounds.
diff --git a/drivers/gpu/drm/tyr/fw/parser.rs b/drivers/gpu/drm/tyr/fw/parser.rs
index 6ff681aa78d1..6576bc870982 100644
--- a/drivers/gpu/drm/tyr/fw/parser.rs
+++ b/drivers/gpu/drm/tyr/fw/parser.rs
@@ -18,7 +18,10 @@
use kernel::{
bitfield,
device::Device,
- num::Bounded,
+ num::{
+ casts::FromSafeCast,
+ Bounded, //
+ },
prelude::*,
sizes::SizeConstants, //
};
@@ -182,7 +185,7 @@ pub(super) fn new(dev: &'a Device, data: &'a [u8]) -> Self {
/// in the binary, extracting section information needed for loading.
pub(super) fn parse(&mut self) -> Result<KVec<ParsedSection>> {
let fw_header = self.parse_fw_header()?;
- let header_end = fw_header.size as usize;
+ let header_end = usize::from_safe_cast(fw_header.size);
let mut parsed_sections = KVec::new();
while self.cursor.pos() < header_end {
@@ -210,7 +213,7 @@ fn parse_fw_header(&mut self) -> Result<FirmwareHeader> {
}
};
- if fw_header.size as usize > self.cursor.len() {
+ if usize::from_safe_cast(fw_header.size) > self.cursor.len() {
dev_err!(self.cursor.dev, "Firmware image is truncated");
return Err(EINVAL);
}
@@ -323,7 +326,7 @@ fn parse_section_entry(
return Err(EINVAL);
}
- if section_hdr.data.end as usize > firmware_size {
+ if usize::from_safe_cast(section_hdr.data.end) > firmware_size {
dev_err!(
entry_cursor.dev,
"Firmware data range {:#x}..{:#x} exceeds firmware size {:#x}",
diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 69c901afb28f..603a9052c439 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -1154,7 +1154,7 @@ impl TryFrom<Bounded<u64, 8>> for MMU_MEMATTR_STAGE1 {
type Error = Error;
fn try_from(val: Bounded<u64, 8>) -> Result<Self, Self::Error> {
- Ok(Self::from_raw(val.get() as u8))
+ Ok(Self::from_raw(u8::from(val)))
}
}
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 72c8593c489e..d15db120352b 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -41,6 +41,7 @@
ARM64LPAES1, //
},
new_mutex,
+ num::casts::FromSafeCast,
prelude::*,
sizes::SizeConstants,
sync::{
@@ -616,7 +617,7 @@ fn sm_step_map<'op>(
// end of the GEM buffer itself.
let gem_range_end = op.gem_offset().checked_add(op.length()).ok_or(EINVAL)?;
- if gem_range_end > op.obj().size() as u64 {
+ if gem_range_end > u64::from_safe_cast(op.obj().size()) {
dev_err!(
context.dev,
"Requested GEM range ends at {} which is beyond the GEM buffer size {}",
@@ -891,7 +892,7 @@ fn pt_map(
return Err(ENOMEM);
}
- segment_mapped += mapped as u64;
+ segment_mapped += u64::from_safe_cast(mapped);
}
Ok(segment_mapped)
@@ -937,8 +938,8 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>)
return Err(EINVAL);
}
- bytes_left_to_unmap -= unmapped as u64;
- iova += unmapped as u64;
+ bytes_left_to_unmap -= u64::from_safe_cast(unmapped);
+ iova += u64::from_safe_cast(unmapped);
}
Ok(())
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 07/13] drm/tyr: use `cv!` for constant conversions
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (5 preceding siblings ...)
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 ` Daniel Almeida
2026-11-25 22:46 ` [PATCH 08/13] drm/tyr: use VmMapFlags::all_bits() to validate map flags Daniel Almeida
` (6 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, convert
constants with `cv!`, which fails the build if the value does not fit
the target type.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/file.rs | 2 +-
drivers/gpu/drm/tyr/regs.rs | 6 +++---
drivers/gpu/drm/tyr/vm.rs | 8 +++++---
3 files changed, 9 insertions(+), 7 deletions(-)
diff --git a/drivers/gpu/drm/tyr/file.rs b/drivers/gpu/drm/tyr/file.rs
index 9030e535b7d0..78c2147328e1 100644
--- a/drivers/gpu/drm/tyr/file.rs
+++ b/drivers/gpu/drm/tyr/file.rs
@@ -79,7 +79,7 @@ pub(crate) fn dev_query(
if devquery.pointer == 0 {
match devquery.type_ {
uapi::drm_panthor_dev_query_type_DRM_PANTHOR_DEV_QUERY_GPU_INFO => {
- devquery.size = core::mem::size_of_val(®_data.gpu_info) as u32;
+ devquery.size = cv!(size_of::<uapi::drm_panthor_gpu_info>() => u32);
Ok(0)
}
_ => Err(EINVAL),
diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 603a9052c439..49e4adf2231d 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -751,11 +751,11 @@ fn from(access: AccessType) -> Self {
#[repr(u8)]
pub(crate) enum CoherencyMode {
/// ACE-Lite coherency protocol.
- AceLite = uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_ACE_LITE as u8,
+ AceLite = cv!(uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_ACE_LITE => u8),
/// ACE coherency protocol.
- Ace = uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_ACE as u8,
+ Ace = cv!(uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_ACE => u8),
/// No coherency protocol.
- None = uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_NONE as u8,
+ None = cv!(uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_NONE => u8),
}
impl TryFrom<Bounded<u32, 32>> for CoherencyMode {
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index d15db120352b..9b52f3562004 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -80,11 +80,13 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum VmFlag {
/// Map as read-only.
- Readonly = uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_MAP_READONLY as u32,
+ Readonly =
+ cv!(uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_MAP_READONLY => u32),
/// Map as non-executable.
- Noexec = uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC as u32,
+ Noexec = cv!(uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC => u32),
/// Map as uncached.
- Uncached = uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED as u32,
+ Uncached =
+ cv!(uapi::drm_panthor_vm_bind_op_flags_DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED => u32),
}
);
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 08/13] drm/tyr: use VmMapFlags::all_bits() to validate map flags
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (6 preceding siblings ...)
2026-11-25 22:46 ` [PATCH 07/13] drm/tyr: use `cv!` for constant conversions Daniel Almeida
@ 2026-11-25 22:46 ` Daniel Almeida
2026-11-25 22:47 ` [PATCH 09/13] drm/tyr: justify the casts of enums to their integer value Daniel Almeida
` (5 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:46 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, stop casting
each `VmFlag` variant to `u32` to build the mask of valid flags in
`VmMapFlags::try_from()`. `impl_flags!` already generates that mask as
`all_bits()`.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/vm.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 9b52f3562004..f06d5b02d8d7 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -144,7 +144,7 @@ impl TryFrom<u32> for VmMapFlags {
type Error = Error;
fn try_from(value: u32) -> Result<Self, Self::Error> {
- let valid = VmFlag::Readonly as u32 | VmFlag::Noexec as u32 | VmFlag::Uncached as u32;
+ let valid = Self::all_bits();
if value & !valid != 0 {
return Err(EINVAL);
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 09/13] drm/tyr: justify the casts of enums to their integer value
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (7 preceding siblings ...)
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 ` Daniel Almeida
2026-11-25 22:47 ` [PATCH 10/13] drm/tyr: add split_u64() for the 64-bit register values Daniel Almeida
` (4 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:47 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, justify the
casts that convert an enum to its integer value. These stay, since
`as` is how Rust reads the value of an enum.
Each of them gets a `// CAST:` comment and an
`#[expect(clippy::as_conversions)]`, so that the lint can be enabled
for the driver.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/fw.rs | 2 ++
drivers/gpu/drm/tyr/regs.rs | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/drivers/gpu/drm/tyr/fw.rs b/drivers/gpu/drm/tyr/fw.rs
index 14cfe2884d5a..2fa9852e0b83 100644
--- a/drivers/gpu/drm/tyr/fw.rs
+++ b/drivers/gpu/drm/tyr/fw.rs
@@ -98,7 +98,9 @@ fn from(value: Bounded<u32, 2>) -> Self {
}
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()
}
}
diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 49e4adf2231d..443e9a1e7a21 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -251,7 +251,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -279,7 +281,9 @@ fn try_from(val: Bounded<u32, 4>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -313,7 +317,9 @@ fn try_from(val: Bounded<u32, 4>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -434,7 +440,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -465,7 +473,9 @@ fn from(val: Bounded<u32, 2>) -> Self {
}
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()
}
}
@@ -772,7 +782,9 @@ fn try_from(val: Bounded<u32, 32>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -814,7 +826,9 @@ fn try_from(val: Bounded<u32, 2>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -857,7 +871,9 @@ fn from(val: Bounded<u32, 2>) -> Self {
}
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()
}
}
@@ -1050,7 +1066,9 @@ fn try_from(val: Bounded<u8, 2>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -1095,7 +1113,9 @@ fn try_from(val: Bounded<u8, 2>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -1126,7 +1146,9 @@ fn from(val: Bounded<u8, 2>) -> Self {
}
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()
}
}
@@ -1379,7 +1401,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -1476,7 +1500,9 @@ fn try_from(val: Bounded<u32, 8>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -1508,7 +1534,9 @@ fn from(val: Bounded<u32, 2>) -> Self {
}
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()
}
}
@@ -1575,7 +1603,9 @@ fn try_from(val: Bounded<u64, 4>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -1672,7 +1702,9 @@ fn try_from(val: Bounded<u64, 5>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -1703,7 +1735,9 @@ fn try_from(val: Bounded<u64, 2>) -> Result<Self, Self::Error> {
}
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()
}
}
@@ -1735,7 +1769,9 @@ fn try_from(val: Bounded<u64, 2>) -> Result<Self, Self::Error> {
}
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()
}
}
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 10/13] drm/tyr: add split_u64() for the 64-bit register values
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (8 preceding siblings ...)
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 ` Daniel Almeida
2026-11-25 22:47 ` [PATCH 11/13] drm/tyr: return usize from get_pgsize() Daniel Almeida
` (3 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:47 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, add
`split_u64()`, the counterpart of `join_u64()`, and use it for the
64-bit values that the MMU code writes as two 32-bit registers.
The two casts that the split needs are now in one place, each with a
`// CAST:` comment, where there were ten.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/mmu/address_space.rs | 29 ++++++++++++++++-------------
drivers/gpu/drm/tyr/regs.rs | 12 ++++++++++++
2 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/drivers/gpu/drm/tyr/mmu/address_space.rs b/drivers/gpu/drm/tyr/mmu/address_space.rs
index f6ccb0d7e3ed..dfa8499a8ebd 100644
--- a/drivers/gpu/drm/tyr/mmu/address_space.rs
+++ b/drivers/gpu/drm/tyr/mmu/address_space.rs
@@ -47,6 +47,7 @@
regs::{
mmu_control::mmu_as_control,
mmu_control::mmu_as_control::*,
+ split_u64,
MAX_AS, //
},
slot::{
@@ -255,34 +256,34 @@ fn as_enable(&mut self, as_nr: usize, as_config: &AddressSpaceConfig) -> Result
let io = &*self.iomem;
- let transtab = as_config.transtab;
+ let (transtab_lo, transtab_hi) = split_u64(as_config.transtab);
io.write(
TRANSTAB_LO::try_at(as_nr).ok_or(EINVAL)?,
- TRANSTAB_LO::from_raw(transtab as u32),
+ TRANSTAB_LO::from_raw(transtab_lo),
);
io.write(
TRANSTAB_HI::try_at(as_nr).ok_or(EINVAL)?,
- TRANSTAB_HI::from_raw((transtab >> 32) as u32),
+ TRANSTAB_HI::from_raw(transtab_hi),
);
- let transcfg = as_config.transcfg;
+ let (transcfg_lo, transcfg_hi) = split_u64(as_config.transcfg);
io.write(
TRANSCFG_LO::try_at(as_nr).ok_or(EINVAL)?,
- TRANSCFG_LO::from_raw(transcfg as u32),
+ TRANSCFG_LO::from_raw(transcfg_lo),
);
io.write(
TRANSCFG_HI::try_at(as_nr).ok_or(EINVAL)?,
- TRANSCFG_HI::from_raw((transcfg >> 32) as u32),
+ TRANSCFG_HI::from_raw(transcfg_hi),
);
- let memattr = as_config.memattr;
+ let (memattr_lo, memattr_hi) = split_u64(as_config.memattr);
io.write(
MEMATTR_LO::try_at(as_nr).ok_or(EINVAL)?,
- MEMATTR_LO::from_raw(memattr as u32),
+ MEMATTR_LO::from_raw(memattr_lo),
);
io.write(
MEMATTR_HI::try_at(as_nr).ok_or(EINVAL)?,
- MEMATTR_HI::from_raw((memattr >> 32) as u32),
+ MEMATTR_HI::from_raw(memattr_hi),
);
self.as_send_cmd_and_wait(as_nr, MmuCommand::Update)?;
@@ -322,14 +323,15 @@ fn as_disable(&mut self, as_nr: usize) -> Result {
let transcfg = TRANSCFG::zeroed()
.with_mode(AddressSpaceMode::Unmapped)
.into_raw();
+ let (transcfg_lo, transcfg_hi) = split_u64(transcfg);
io.write(
TRANSCFG_LO::try_at(as_nr).ok_or(EINVAL)?,
- TRANSCFG_LO::from_raw(transcfg as u32),
+ TRANSCFG_LO::from_raw(transcfg_lo),
);
io.write(
TRANSCFG_HI::try_at(as_nr).ok_or(EINVAL)?,
- TRANSCFG_HI::from_raw((transcfg >> 32) as u32),
+ TRANSCFG_HI::from_raw(transcfg_hi),
);
self.as_send_cmd_and_wait(as_nr, MmuCommand::Update)?;
@@ -405,14 +407,15 @@ fn as_start_update(&mut self, as_nr: usize, region: &Range<u64>) -> Result {
.try_with_size(lockaddr_size)?
.try_with_base(lockaddr_base_field)?
.into_raw();
+ let (lockaddr_lo, lockaddr_hi) = split_u64(lockaddr_val);
io.write(
LOCKADDR_LO::try_at(as_nr).ok_or(EINVAL)?,
- LOCKADDR_LO::from_raw(lockaddr_val as u32),
+ LOCKADDR_LO::from_raw(lockaddr_lo),
);
io.write(
LOCKADDR_HI::try_at(as_nr).ok_or(EINVAL)?,
- LOCKADDR_HI::from_raw((lockaddr_val >> 32) as u32),
+ LOCKADDR_HI::from_raw(lockaddr_hi),
);
self.as_send_cmd_and_wait(as_nr, MmuCommand::Lock)
diff --git a/drivers/gpu/drm/tyr/regs.rs b/drivers/gpu/drm/tyr/regs.rs
index 443e9a1e7a21..76892a911227 100644
--- a/drivers/gpu/drm/tyr/regs.rs
+++ b/drivers/gpu/drm/tyr/regs.rs
@@ -32,6 +32,18 @@ pub(crate) fn join_u64(lo: u32, hi: u32) -> u64 {
(u64::from(lo)) | ((u64::from(hi)) << 32)
}
+/// Split a 64-bit value into its low and high 32-bit halves.
+pub(crate) fn split_u64(value: u64) -> (u32, u32) {
+ // CAST: The upper 32 bits are dropped on purpose. They are returned in `hi`.
+ #[expect(clippy::as_conversions)]
+ let lo = value as u32;
+ // CAST: After the shift, the value fits in 32 bits.
+ #[expect(clippy::as_conversions)]
+ let hi = (value >> 32) as u32;
+
+ (lo, hi)
+}
+
/// Read a logical 64-bit value from split 32-bit registers without tearing.
pub(crate) fn read_u64_no_tearing(lo_read: impl Fn() -> u32, hi_read: impl Fn() -> u32) -> u64 {
loop {
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 11/13] drm/tyr: return usize from get_pgsize()
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (9 preceding siblings ...)
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 ` Daniel Almeida
2026-11-25 22:47 ` [PATCH 12/13] drm/tyr: justify the physical address cast in pt_map() Daniel Almeida
` (2 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:47 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, return the
page size and the page count from `get_pgsize()` as `usize`, the type
that `map_pages()` and `unmap_pages()` take. Its two callers cast both
values.
The page size now comes from the `usize` size constants. The page count
is computed from `u64` values, so it is still cast, now in
`get_pgsize()`, with a `// CAST:` comment that gives its bound.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/vm.rs | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index f06d5b02d8d7..673fc6819070 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -788,7 +788,7 @@ fn sm_step_remap<'op>(
/// can be mapped before we hit the 2MB boundary. Or, if mapping 2MB pages,
/// figure out how many pages can be mapped before hitting the 1GB boundary
/// Returns the page size (4KB or 2MB) and the number of pages that can be mapped at that size.
-fn get_pgsize(addr: u64, size: u64) -> (u64, u64) {
+fn get_pgsize(addr: u64, size: u64) -> (usize, usize) {
// Get the distance to the next boundary of 2MB block
let blk_offset_2m = addr.wrapping_neg() % u64::SZ_2M;
@@ -799,7 +799,11 @@ fn get_pgsize(addr: u64, size: u64) -> (u64, u64) {
} else {
u64::min(blk_offset_2m, size) / u64::SZ_4K
};
- return (u64::SZ_4K, pgcount);
+ // CAST: This path covers less than one 2MB block, so `pgcount` is below 512.
+ #[expect(clippy::as_conversions)]
+ let pgcount = pgcount as usize;
+
+ return (usize::SZ_4K, pgcount);
}
let blk_offset_1g = addr.wrapping_neg() % u64::SZ_1G;
@@ -809,8 +813,11 @@ fn get_pgsize(addr: u64, size: u64) -> (u64, u64) {
blk_offset_1g
};
let pgcount = u64::min(blk_offset, size) / u64::SZ_2M;
+ // CAST: `blk_offset` is at most 1GB, so `pgcount` is at most 512.
+ #[expect(clippy::as_conversions)]
+ let pgcount = pgcount as usize;
- (u64::SZ_2M, pgcount)
+ (usize::SZ_2M, pgcount)
}
/// Maps a physical address range into the page table at the specified virtual address.
@@ -869,8 +876,8 @@ fn pt_map(
pt.map_pages(
curr_iova,
curr_paddr as PhysAddr,
- pgsize as usize,
- pgcount as usize,
+ pgsize,
+ pgcount,
prot,
GFP_KERNEL,
)
@@ -933,7 +940,7 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>)
// We know that this page table has one or more consecutive mappings
// starting at `iova` with the total size of `pgcount * pgsize` because
// gpuvm callbacks provide exactly the range that was previously mapped.
- let unmapped = unsafe { pt.unmap_pages(iova_usize, pgsize as usize, pgcount as usize) };
+ let unmapped = unsafe { pt.unmap_pages(iova_usize, pgsize, pgcount) };
if unmapped == 0 {
dev_err!(dev, "Failed to unmap any bytes at iova {:#x}", iova_usize);
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 12/13] drm/tyr: justify the physical address cast in pt_map()
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (10 preceding siblings ...)
2026-11-25 22:47 ` [PATCH 11/13] drm/tyr: return usize from get_pgsize() Daniel Almeida
@ 2026-11-25 22:47 ` Daniel Almeida
2026-11-25 22:47 ` [PATCH 13/13] drm/tyr: deny `clippy::as_conversions` Daniel Almeida
2026-11-26 10:02 ` [PATCH 00/13] drm/tyr: reduce the number of `as` casts Alice Ryhl
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:47 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, justify the
cast of the DMA address to `PhysAddr` in `pt_map()`. It stays, with a
`// CAST:` comment and an `#[expect(clippy::as_conversions)]`.
No functional change intended.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/vm.rs | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 673fc6819070..3e2ed43f07a0 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -866,22 +866,18 @@ fn pt_map(
}
};
+ // CAST: `PhysAddr` is as wide as a DMA address on the platforms we care about.
+ #[expect(clippy::as_conversions)]
+ let curr_paddr = curr_paddr as PhysAddr;
+
// SAFETY:
// No other io-pgtable operation can currently access this range because Tyr holds
// the gpuvm_unique mutex for the entire sm_map() operation.
// The addresses being mapped won't overlap any existing mappings in this
// page table because drm_gpuvm_sm_map() checks each requested mapping and either unmaps
// or remaps any overlap before creating the new mapping.
- let (mapped, result) = unsafe {
- pt.map_pages(
- curr_iova,
- curr_paddr as PhysAddr,
- pgsize,
- pgcount,
- prot,
- GFP_KERNEL,
- )
- };
+ let (mapped, result) =
+ unsafe { pt.map_pages(curr_iova, curr_paddr, pgsize, pgcount, prot, GFP_KERNEL) };
if let Err(e) = result {
// If map_pages fails, mapped will be zero because the ARM LPAE backend
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* [PATCH 13/13] drm/tyr: deny `clippy::as_conversions`
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (11 preceding siblings ...)
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 ` Daniel Almeida
2026-11-26 10:02 ` [PATCH 00/13] drm/tyr: reduce the number of `as` casts Alice Ryhl
13 siblings, 0 replies; 15+ messages in thread
From: Daniel Almeida @ 2026-11-25 22:47 UTC (permalink / raw)
To: Daniel Almeida, Alice Ryhl, Danilo Krummrich, David Airlie,
Simona Vetter, Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan
Cc: 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, deny
`clippy::as_conversions` in the driver, so that Clippy rejects a new
cast unless it carries an `#[expect]`.
The casts that remain are all justified in a `// CAST:` comment and
marked that way.
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
drivers/gpu/drm/tyr/tyr.rs | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/gpu/drm/tyr/tyr.rs b/drivers/gpu/drm/tyr/tyr.rs
index e7ec450bdc9c..ced937af4c53 100644
--- a/drivers/gpu/drm/tyr/tyr.rs
+++ b/drivers/gpu/drm/tyr/tyr.rs
@@ -5,6 +5,11 @@
//! The name "Tyr" is inspired by Norse mythology, reflecting Arm's tradition of
//! naming their GPUs after Nordic mythological figures and places.
+// `as` conversions can silently truncate, so they are denied by default in
+// this driver. A conversion that cannot be avoided is justified in a
+// `// CAST:` comment and marked with `#[expect(clippy::as_conversions)]`.
+#![deny(clippy::as_conversions)]
+
use crate::driver::TyrPlatformDriver;
mod driver;
--
2.56.0
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 00/13] drm/tyr: reduce the number of `as` casts
2026-11-25 22:46 [PATCH 00/13] drm/tyr: reduce the number of `as` casts Daniel Almeida
` (12 preceding siblings ...)
2026-11-25 22:47 ` [PATCH 13/13] drm/tyr: deny `clippy::as_conversions` Daniel Almeida
@ 2026-11-26 10:02 ` Alice Ryhl
13 siblings, 0 replies; 15+ messages in thread
From: Alice Ryhl @ 2026-11-26 10:02 UTC (permalink / raw)
To: Daniel Almeida
Cc: Alice Ryhl, Danilo Krummrich, David Airlie, Simona Vetter,
Benno Lossin, Gary Guo, Miguel Ojeda, Boqun Feng,
Björn Roy Baron, Andreas Hindborg, Trevor Gross,
Tamir Duberstein, Alexandre Courbot, Onur Özkan, dri-devel,
rust-for-linux, linux-kernel
On Wed, 25 Nov 2026 19:46:51 -0300, Daniel Almeida wrote:
> An `as` cast silently drops the bits that do not fit the target type,
> and nothing at the cast shows whether that can happen. Tyr has 86 of
> them, as counted by `clippy::as_conversions`. This series removes 62.
Looks great overall. For the whole series:
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Alice
^ permalink raw reply [flat|nested] 15+ messages in thread