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 <[email protected]>
---
 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

Reply via email to