From: Alexandre Courbot <acourbot@nvidia.com>
To: Danilo Krummrich <dakr@kernel.org>,
Alice Ryhl <aliceryhl@google.com>,
David Airlie <airlied@gmail.com>,
Simona Vetter <simona@ffwll.ch>
Cc: John Hubbard <jhubbard@nvidia.com>,
Alistair Popple <apopple@nvidia.com>,
Timur Tabi <ttabi@nvidia.com>,
Eliot Courtney <ecourtney@nvidia.com>,
Zhi Wang <zhiw@nvidia.com>,
nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Alexandre Courbot <acourbot@nvidia.com>
Subject: [PATCH 2/2] gpu: nova-core: use FromSafeCast wherever possible
Date: Fri, 28 Aug 2026 14:33:14 +0900 [thread overview]
Message-ID: <20260828-nova_num-v1-2-e21f17ba4127@nvidia.com> (raw)
In-Reply-To: <20260828-nova_num-v1-0-e21f17ba4127@nvidia.com>
`FromSafeCast` should be preferred to the `*_as_*` family of functions
when it can be used (i.e. in non-const contexts), as it requires a
single import to perform all valid conversions.
After this patch, the only remaining references to the `casts` module
are those done in const context, which means the `casts` import can be
removed once these are converted to use the `cv!` macro.
Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
---
drivers/gpu/nova-core/falcon/fsp.rs | 4 ++--
drivers/gpu/nova-core/firmware/tlv.rs | 4 ++--
drivers/gpu/nova-core/gsp.rs | 4 ++--
drivers/gpu/nova-core/gsp/cmdq.rs | 19 +++++++++++--------
drivers/gpu/nova-core/gsp/fw.rs | 8 ++++----
5 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/drivers/gpu/nova-core/falcon/fsp.rs b/drivers/gpu/nova-core/falcon/fsp.rs
index 2470ac511c98..ece95b1ea8cb 100644
--- a/drivers/gpu/nova-core/falcon/fsp.rs
+++ b/drivers/gpu/nova-core/falcon/fsp.rs
@@ -16,7 +16,7 @@
},
Io, //
},
- num::casts,
+ num::casts::FromSafeCast,
prelude::*,
sizes::SZ_1K,
time::Delta,
@@ -165,7 +165,7 @@ pub(crate) fn recv_msg(&mut self) -> Result<KVec<u8>> {
Delta::from_millis(10),
Delta::from_millis(FSP_MSG_TIMEOUT_MS),
)
- .map(casts::u32_as_usize)?;
+ .map(usize::from_safe_cast)?;
// Don't blindly allocate more than the maximum we expect from FSP.
if msg_size > FSP_EMEM_CHANNEL_0_SIZE {
diff --git a/drivers/gpu/nova-core/firmware/tlv.rs b/drivers/gpu/nova-core/firmware/tlv.rs
index 6653c10e3e0a..7dff8871e338 100644
--- a/drivers/gpu/nova-core/firmware/tlv.rs
+++ b/drivers/gpu/nova-core/firmware/tlv.rs
@@ -5,7 +5,7 @@
device,
firmware,
num::casts::{
- self,
+ FromSafeCast,
IntoSafeCast, //
},
prelude::*,
@@ -52,7 +52,7 @@ fn parse(hdr: &[u8]) -> Option<Self> {
return None;
}
let len_arr = <[u8; 4]>::try_from(hdr.get(4..Self::SIZE)?).ok()?;
- let length = casts::u32_as_usize(u32::from_le_bytes(len_arr));
+ let length = usize::from_safe_cast(u32::from_le_bytes(len_arr));
Some(Self { tag, length })
}
}
diff --git a/drivers/gpu/nova-core/gsp.rs b/drivers/gpu/nova-core/gsp.rs
index fc8648de84c2..0f5275dcb41c 100644
--- a/drivers/gpu/nova-core/gsp.rs
+++ b/drivers/gpu/nova-core/gsp.rs
@@ -17,7 +17,7 @@
io_write,
Io, //
},
- num::casts,
+ num::casts::FromSafeCast,
pci,
prelude::*, //
};
@@ -92,7 +92,7 @@ fn init(view: CoherentView<'_, Self>, start: DmaAddress) -> Result<()> {
for i in 0..NUM_PAGES {
io_write!(view, .0[build: i],
start
- .checked_add(casts::usize_as_u64(i) << GSP_PAGE_SHIFT)
+ .checked_add(u64::from_safe_cast(i) << GSP_PAGE_SHIFT)
.ok_or(EOVERFLOW)?
);
}
diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs b/drivers/gpu/nova-core/gsp/cmdq.rs
index f85fde09aa6e..658d0a9b2cfb 100644
--- a/drivers/gpu/nova-core/gsp/cmdq.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq.rs
@@ -23,7 +23,10 @@
Io, //
},
new_mutex,
- num::casts,
+ num::casts::{
+ self,
+ FromSafeCast, //
+ },
prelude::*,
ptr,
sync::{
@@ -289,10 +292,10 @@ fn new(dev: &device::Device<device::Bound>) -> Result<Self> {
unsafe {
(
core::slice::from_raw_parts_mut(
- data.add(casts::u32_as_usize(tx)),
- casts::u32_as_usize(tail_end - tx),
+ data.add(usize::from_safe_cast(tx)),
+ usize::from_safe_cast(tail_end - tx),
),
- core::slice::from_raw_parts_mut(data, casts::u32_as_usize(wrap_end)),
+ core::slice::from_raw_parts_mut(data, usize::from_safe_cast(wrap_end)),
)
}
}
@@ -307,7 +310,7 @@ fn driver_write_area_size(&self) -> usize {
// `cpu_write_ptr`. The minimum value case is where `rx == 0` and `tx == MSGQ_NUM_PAGES -
// 1`, which gives `0 + MSGQ_NUM_PAGES - (MSGQ_NUM_PAGES - 1) - 1 == 0`.
let slots = (rx + MSGQ_NUM_PAGES - tx - 1) % MSGQ_NUM_PAGES;
- casts::u32_as_usize(slots) * GSP_PAGE_SIZE
+ usize::from_safe_cast(slots) * GSP_PAGE_SIZE
}
/// Returns the region of the GSP message queue that the driver is currently allowed to read
@@ -343,10 +346,10 @@ fn driver_write_area_size(&self) -> usize {
unsafe {
(
core::slice::from_raw_parts(
- data.add(casts::u32_as_usize(rx)),
- casts::u32_as_usize(tail_end - rx),
+ data.add(usize::from_safe_cast(rx)),
+ usize::from_safe_cast(tail_end - rx),
),
- core::slice::from_raw_parts(data, casts::u32_as_usize(wrap_end)),
+ core::slice::from_raw_parts(data, usize::from_safe_cast(wrap_end)),
)
}
}
diff --git a/drivers/gpu/nova-core/gsp/fw.rs b/drivers/gpu/nova-core/gsp/fw.rs
index 88f083ff1f13..61064b6a251b 100644
--- a/drivers/gpu/nova-core/gsp/fw.rs
+++ b/drivers/gpu/nova-core/gsp/fw.rs
@@ -681,7 +681,7 @@ fn id8(name: &str) -> u64 {
let init_inner = init!(bindings::LibosMemoryRegionInitArgument {
id8: id8(name),
pa: obj.dma_address(),
- size: casts::usize_as_u64(obj.size()),
+ size: u64::from_safe_cast(obj.size()),
kind: casts::u32_into_u8::<
{ bindings::LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS },
>(),
@@ -851,7 +851,7 @@ pub(crate) fn set_checksum(&mut self, checksum: u32) {
/// Returns the length of the message's payload.
pub(crate) fn payload_length(&self) -> usize {
// `rpc.length` includes the length of the RPC message header.
- casts::u32_as_usize(self.inner.rpc.length)
+ usize::from_safe_cast(self.inner.rpc.length)
.saturating_sub(size_of::<bindings::rpc_message_header_v>())
}
@@ -948,8 +948,8 @@ fn new(cmdq: &Cmdq) -> impl Init<Self> + '_ {
init!(MessageQueueInitArguments {
sharedMemPhysAddr: cmdq.dma_addr,
pageTableEntryCount: casts::usize_into_u32::<{ Cmdq::NUM_PTES }>(),
- cmdQueueOffset: casts::usize_as_u64(Cmdq::CMDQ_OFFSET),
- statQueueOffset: casts::usize_as_u64(Cmdq::STATQ_OFFSET),
+ cmdQueueOffset: u64::from_safe_cast(Cmdq::CMDQ_OFFSET),
+ statQueueOffset: u64::from_safe_cast(Cmdq::STATQ_OFFSET),
..Zeroable::init_zeroed()
})
}
--
2.55.0
next prev parent reply other threads:[~2026-08-28 5:33 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-28 5:33 [PATCH 0/2] gpu: nova-core: complete conversion to kernel `num` module Alexandre Courbot
2026-08-28 5:33 ` [PATCH 1/2] gpu: nova-core: use kernel lossless integer conversion module Alexandre Courbot
2026-08-28 12:09 ` Danilo Krummrich
2026-08-28 12:16 ` Miguel Ojeda
2026-08-28 5:33 ` Alexandre Courbot [this message]
2026-08-28 5:42 ` [PATCH 2/2] gpu: nova-core: use FromSafeCast wherever possible Eliot Courtney
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=20260828-nova_num-v1-2-e21f17ba4127@nvidia.com \
--to=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=apopple@nvidia.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=ecourtney@nvidia.com \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=nova-gpu@lists.linux.dev \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=ttabi@nvidia.com \
--cc=zhiw@nvidia.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
all inboxes | Powered by JetHome®