rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexandre Courbot <acourbot@nvidia.com>
To: Joel Fernandes <joelagnelf@nvidia.com>,
	 Yury Norov <yury.norov@gmail.com>,
	Danilo Krummrich <dakr@kernel.org>,
	 Miguel Ojeda <ojeda@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Alexandre Courbot <acourbot@nvidia.com>
Subject: [PATCH RFC 2/2] gpu: nova-core: demonstrate use of BoundedInt
Date: Thu, 02 Oct 2025 00:03:14 +0900	[thread overview]
Message-ID: <20251002-bounded_ints-v1-2-dd60f5804ea4@nvidia.com> (raw)
In-Reply-To: <20251002-bounded_ints-v1-0-dd60f5804ea4@nvidia.com>

Augment the register macro with two new bounded setter and getter
methods, and showcase how they can be used in the driver.

Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
 drivers/gpu/nova-core/falcon.rs      | 14 +++++++++-----
 drivers/gpu/nova-core/regs/macros.rs | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon.rs
index 37e6298195e49a9a29e81226abe16cd410c9adbc..2ce2635c86d8cad6e61862d4bd1976d642090c5b 100644
--- a/drivers/gpu/nova-core/falcon.rs
+++ b/drivers/gpu/nova-core/falcon.rs
@@ -6,6 +6,7 @@
 use hal::FalconHal;
 use kernel::device;
 use kernel::dma::DmaAddress;
+use kernel::num::BoundedInt;
 use kernel::prelude::*;
 use kernel::sync::aref::ARef;
 use kernel::time::Delta;
@@ -488,24 +489,27 @@ fn dma_wr<F: FalconFirmware<Target = E>>(
         // Set up the base source DMA address.
 
         regs::NV_PFALCON_FALCON_DMATRFBASE::default()
-            .set_base((dma_start >> 8) as u32)
+            .set_base_bounded(BoundedInt::new((dma_start >> 8) as u32))
             .write(bar, &E::ID);
         regs::NV_PFALCON_FALCON_DMATRFBASE1::default()
-            .set_base((dma_start >> 40) as u16)
+            .set_base_bounded(BoundedInt::try_from((dma_start >> 40) as u32)?)
             .write(bar, &E::ID);
 
+        let r = regs::NV_PFALCON_FALCON_DMATRFBASE::read(bar, &E::ID).base_bounded();
+        pr_info!("BASE: {:x}\n", r);
+
         let cmd = regs::NV_PFALCON_FALCON_DMATRFCMD::default()
             .set_size(DmaTrfCmdSize::Size256B)
             .set_imem(target_mem == FalconMem::Imem)
-            .set_sec(if sec { 1 } else { 0 });
+            .set_sec_bounded(BoundedInt::new(if sec { 1 } else { 0 }));
 
         for pos in (0..num_transfers).map(|i| i * DMA_LEN) {
             // Perform a transfer of size `DMA_LEN`.
             regs::NV_PFALCON_FALCON_DMATRFMOFFS::default()
-                .set_offs(load_offsets.dst_start + pos)
+                .set_offs_bounded(BoundedInt::try_from(load_offsets.dst_start + pos)?)
                 .write(bar, &E::ID);
             regs::NV_PFALCON_FALCON_DMATRFFBOFFS::default()
-                .set_offs(src_start + pos)
+                .set_offs_bounded(BoundedInt::new(src_start + pos))
                 .write(bar, &E::ID);
             cmd.write(bar, &E::ID);
 
diff --git a/drivers/gpu/nova-core/regs/macros.rs b/drivers/gpu/nova-core/regs/macros.rs
index 754c14ee7f401688da51e138db71ccaa58445aa6..03a1830f492fdc1747767ed768ce2239e9ce41ec 100644
--- a/drivers/gpu/nova-core/regs/macros.rs
+++ b/drivers/gpu/nova-core/regs/macros.rs
@@ -565,6 +565,40 @@ pub(crate) fn [<set_ $field>](mut self, value: $to_type) -> Self {
             self
         }
         );
+
+        ::kernel::macros::paste!(
+        pub(crate) fn [<$field _bounded>](self) ->
+            ::kernel::num::BoundedInt<u32, { $hi + 1 - $lo }> {
+            const MASK: u32 = $name::[<$field:upper _MASK>];
+            const SHIFT: u32 = $name::[<$field:upper _SHIFT>];
+            // Ensure the returned type has the same width as the field.
+            ::kernel::static_assert!(
+                MASK >> SHIFT == ::kernel::num::BoundedInt::<u32, { $hi + 1 - $lo }>::MASK
+            );
+
+            let field = ((self.0 & MASK) >> SHIFT);
+
+            ::kernel::num::BoundedInt::<u32, { $hi + 1 - $lo }>::new(field)
+        }
+
+        pub(crate) fn [<set_ $field _bounded>](
+            mut self,
+            value: ::kernel::num::BoundedInt<u32, { $hi + 1 - $lo }>
+        ) -> Self {
+            const MASK: u32 = $name::[<$field:upper _MASK>];
+            const SHIFT: u32 = $name::[<$field:upper _SHIFT>];
+            // Ensure the returned type has the same width as the field.
+            ::kernel::static_assert!(
+                MASK >> SHIFT == ::kernel::num::BoundedInt::<u32, { $hi + 1 - $lo }>::MASK
+            );
+
+            let value = (value.get() << SHIFT) & MASK;
+            self.0 = (self.0 & !MASK) | value;
+
+            self
+        }
+        );
+
     };
 
     // Generates the `Debug` implementation for `$name`.

-- 
2.51.0


  parent reply	other threads:[~2025-10-01 15:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-01 15:03 [RFC PATCH 0/2] rust: bounded integer types and use in register macro Alexandre Courbot
2025-10-01 15:03 ` [PATCH RFC 1/2] rust: kernel: add bounded integer types Alexandre Courbot
2025-10-01 15:03 ` Alexandre Courbot [this message]
2025-10-01 22:07 ` [RFC PATCH 0/2] rust: bounded integer types and use in register macro Joel Fernandes
2025-10-02  3:03   ` Alexandre Courbot
2025-10-02 13:32     ` Joel Fernandes

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=20251002-bounded_ints-v1-2-dd60f5804ea4@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=joelagnelf@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=yury.norov@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).