mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 05/13] drm/tyr: use SizeConstants for the u32 and u64 size constants
Date: Wed, 25 Nov 2026 19:46:56 -0300	[thread overview]
Message-ID: <20261125-b4-tyr-as-casts-v1-5-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, 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


  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 ` [PATCH 04/13] drm/tyr: convert EntryHeader to `bitfield!` Daniel Almeida
2026-11-25 22:46 ` Daniel Almeida [this message]
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-5-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®