* [PATCH] gpu: nova-core: migrate to `kernel::num::casts`
@ 2026-11-30 13:07 Gary Guo
2026-12-02 6:56 ` Alexandre Courbot
0 siblings, 1 reply; 2+ messages in thread
From: Gary Guo @ 2026-11-30 13:07 UTC (permalink / raw)
To: Danilo Krummrich, Alice Ryhl, Alexandre Courbot, David Airlie,
Simona Vetter
Cc: nova-gpu, dri-devel, Gary Guo
From: Gary Guo <gary@garyguo.net>
Migrate users of the Nova's internal `num` module over to the
`kernel::num::casts` instead. `FromSafeCast` and `IntoSafeCast` are
migrated to the same-named traits or their `Arch` variant; `uX_as_uY`
methods are migrated to `FromSafeCast`/`IntoSafeCast` outside const
contexts and migrated to `cv!` macro inside const context.
Signed-off-by: Gary Guo <gary@garyguo.net>
---
drivers/gpu/nova-core/falcon.rs | 2 +-
drivers/gpu/nova-core/falcon/fsp.rs | 4 +-
drivers/gpu/nova-core/fb.rs | 2 +-
drivers/gpu/nova-core/firmware.rs | 2 +-
drivers/gpu/nova-core/firmware/booter.rs | 2 +-
drivers/gpu/nova-core/firmware/fwsec.rs | 2 +-
drivers/gpu/nova-core/firmware/fwsec/bootloader.rs | 2 +-
drivers/gpu/nova-core/firmware/gen_bootloader.rs | 2 +-
drivers/gpu/nova-core/firmware/gsp.rs | 2 +-
drivers/gpu/nova-core/firmware/radix3.rs | 6 +-
drivers/gpu/nova-core/firmware/tlv.rs | 8 +-
drivers/gpu/nova-core/gsp.rs | 4 +-
drivers/gpu/nova-core/gsp/cmdq.rs | 3 +-
drivers/gpu/nova-core/gsp/fw.rs | 14 +-
drivers/gpu/nova-core/gsp/fw/commands.rs | 2 +-
drivers/gpu/nova-core/gsp/nvkv/decode.rs | 18 ++-
drivers/gpu/nova-core/gsp/sequencer.rs | 2 +-
drivers/gpu/nova-core/irq/interrupt_tree.rs | 20 +--
drivers/gpu/nova-core/mctp.rs | 7 +-
drivers/gpu/nova-core/mm/bar_user.rs | 20 ++-
drivers/gpu/nova-core/mm/pagetable/map.rs | 18 +--
drivers/gpu/nova-core/mm/pagetable/walk.rs | 24 ++--
drivers/gpu/nova-core/mm/pramin.rs | 7 +-
drivers/gpu/nova-core/mm/vmm.rs | 17 +--
drivers/gpu/nova-core/num.rs | 158 ---------------------
drivers/gpu/nova-core/vbios.rs | 2 +-
26 files changed, 94 insertions(+), 256 deletions(-)
diff --git a/drivers/gpu/nova-core/falcon.rs b/drivers/gpu/nova-core/falcon.rs
index 8bcc9240d1bd..09c8fed9f924 100644
--- a/drivers/gpu/nova-core/falcon.rs
+++ b/drivers/gpu/nova-core/falcon.rs
@@ -19,6 +19,7 @@
Mmio, //
},
num::{
+ casts::FromSafeCast,
Bounded,
TryIntoBounded, //
},
@@ -32,7 +33,6 @@
driver::Bar0,
falcon::hal::LoadMethod,
gpu::Chipset,
- num::FromSafeCast,
regs, //
};
diff --git a/drivers/gpu/nova-core/falcon/fsp.rs b/drivers/gpu/nova-core/falcon/fsp.rs
index d7c213848005..1296f8588eb3 100644
--- a/drivers/gpu/nova-core/falcon/fsp.rs
+++ b/drivers/gpu/nova-core/falcon/fsp.rs
@@ -15,6 +15,7 @@
Io,
Mmio, //
},
+ num::casts::FromSafeCast,
prelude::*,
sizes::SZ_1K,
time::Delta,
@@ -29,7 +30,6 @@
Falcon,
FalconEngine, //
},
- num,
regs, //
};
@@ -166,7 +166,7 @@ pub(crate) fn recv_msg(&mut self) -> Result<KVec<u8>> {
Delta::from_millis(10),
Delta::from_millis(FSP_MSG_TIMEOUT_MS),
)
- .map(num::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/fb.rs b/drivers/gpu/nova-core/fb.rs
index 5ceb7760c78e..e87bacdcb317 100644
--- a/drivers/gpu/nova-core/fb.rs
+++ b/drivers/gpu/nova-core/fb.rs
@@ -10,6 +10,7 @@
dma::CoherentHandle,
fmt,
io::Io,
+ num::casts::FromSafeCast,
prelude::*,
ptr::{
Alignable,
@@ -23,7 +24,6 @@
firmware::gsp::GspFirmware,
gpu::Chipset,
gsp,
- num::FromSafeCast,
vgpu::VgpuState, //
};
diff --git a/drivers/gpu/nova-core/firmware.rs b/drivers/gpu/nova-core/firmware.rs
index 358c9b8db0b8..030bb35dfded 100644
--- a/drivers/gpu/nova-core/firmware.rs
+++ b/drivers/gpu/nova-core/firmware.rs
@@ -9,6 +9,7 @@
use kernel::{
firmware,
+ num::casts::IntoSafeCast,
prelude::*, //
};
@@ -19,7 +20,6 @@
},
gpu,
gsp::boot_firmware_files,
- num::IntoSafeCast, //
};
pub(crate) mod booter;
diff --git a/drivers/gpu/nova-core/firmware/booter.rs b/drivers/gpu/nova-core/firmware/booter.rs
index aa4458bb3312..460a0fd2ff21 100644
--- a/drivers/gpu/nova-core/firmware/booter.rs
+++ b/drivers/gpu/nova-core/firmware/booter.rs
@@ -10,6 +10,7 @@
use kernel::{
device,
dma::Coherent,
+ num::casts::IntoSafeCast,
prelude::*, //
};
@@ -33,7 +34,6 @@
Unsigned, //
},
gpu::Chipset,
- num::IntoSafeCast,
};
/// Signature for Booter firmware. Their size is encoded into the header and not known a compile
diff --git a/drivers/gpu/nova-core/firmware/fwsec.rs b/drivers/gpu/nova-core/firmware/fwsec.rs
index 7a931f22f629..7f7ca3ba2298 100644
--- a/drivers/gpu/nova-core/firmware/fwsec.rs
+++ b/drivers/gpu/nova-core/firmware/fwsec.rs
@@ -19,6 +19,7 @@
self,
Device, //
},
+ num::casts::FromSafeCast,
prelude::*,
transmute::{
AsBytes,
@@ -42,7 +43,6 @@
Signed,
Unsigned, //
},
- num::FromSafeCast,
vbios::Vbios,
};
diff --git a/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs b/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs
index 461e5e6f7899..384a9f025a4b 100644
--- a/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs
+++ b/drivers/gpu/nova-core/firmware/fwsec/bootloader.rs
@@ -12,6 +12,7 @@
Device, //
},
dma::Coherent,
+ num::casts::FromSafeCast,
prelude::*, //
};
@@ -30,7 +31,6 @@
},
},
gpu::Chipset,
- num::FromSafeCast, //
};
/// Wrapper for [`FwsecFirmware`] that includes the bootloader performing the actual load
diff --git a/drivers/gpu/nova-core/firmware/gen_bootloader.rs b/drivers/gpu/nova-core/firmware/gen_bootloader.rs
index 751335c97e35..266f1f6354ec 100644
--- a/drivers/gpu/nova-core/firmware/gen_bootloader.rs
+++ b/drivers/gpu/nova-core/firmware/gen_bootloader.rs
@@ -8,6 +8,7 @@
use kernel::{
device,
num::{
+ casts::FromSafeCast,
Bounded,
TryIntoBounded, //
},
@@ -35,7 +36,6 @@
Tlv,
},
gpu::Chipset,
- num::FromSafeCast, //
};
/// Structure used by the boot-loader to load the rest of the code.
diff --git a/drivers/gpu/nova-core/firmware/gsp.rs b/drivers/gpu/nova-core/firmware/gsp.rs
index 1cf17d0fb36b..f608a8cce141 100644
--- a/drivers/gpu/nova-core/firmware/gsp.rs
+++ b/drivers/gpu/nova-core/firmware/gsp.rs
@@ -7,6 +7,7 @@
DmaAddress, //
},
firmware,
+ num::casts::FromSafeCast,
prelude::*,
str::CString,
};
@@ -21,7 +22,6 @@
},
},
gpu::Chipset,
- num::FromSafeCast,
};
/// The GSP firmware image, its signatures, and the GSP bootloader.
diff --git a/drivers/gpu/nova-core/firmware/radix3.rs b/drivers/gpu/nova-core/firmware/radix3.rs
index b17d1363e48f..f90b359ef90f 100644
--- a/drivers/gpu/nova-core/firmware/radix3.rs
+++ b/drivers/gpu/nova-core/firmware/radix3.rs
@@ -19,6 +19,7 @@
DataDirection,
DmaAddress, //
},
+ num::casts::FromSafeCast,
prelude::*,
scatterlist::{
Owned,
@@ -26,10 +27,7 @@
},
};
-use crate::{
- gsp::GSP_PAGE_SIZE,
- num::FromSafeCast, //
-};
+use crate::gsp::GSP_PAGE_SIZE;
/// A radix3 page table and the data it maps.
pub(crate) struct Radix3<'a> {
diff --git a/drivers/gpu/nova-core/firmware/tlv.rs b/drivers/gpu/nova-core/firmware/tlv.rs
index 7b879f13a61e..ac376bd35f84 100644
--- a/drivers/gpu/nova-core/firmware/tlv.rs
+++ b/drivers/gpu/nova-core/firmware/tlv.rs
@@ -4,14 +4,12 @@
use kernel::{
device,
firmware,
+ num::casts::IntoSafeCast,
prelude::*,
str::CString, //
};
-use crate::{
- gpu,
- num::*, //
-};
+use crate::gpu;
/// Requests the GPU firmware TLV `name` suitable for `chipset`.
pub(crate) fn request_tlv(
@@ -51,7 +49,7 @@ fn parse(hdr: &[u8]) -> Option<Self> {
return None;
}
let len_arr = <[u8; 4]>::try_from(hdr.get(4..Self::SIZE)?).ok()?;
- let length = u32_as_usize(u32::from_le_bytes(len_arr));
+ let length = u32::from_le_bytes(len_arr).into_safe_cast();
Some(Self { tag, length })
}
}
diff --git a/drivers/gpu/nova-core/gsp.rs b/drivers/gpu/nova-core/gsp.rs
index 2f1cdf8b7bec..bc77a491d233 100644
--- a/drivers/gpu/nova-core/gsp.rs
+++ b/drivers/gpu/nova-core/gsp.rs
@@ -13,6 +13,7 @@
DmaAddress, //
},
io::io_write,
+ num::casts::FromSafeCast,
pci,
prelude::*, //
};
@@ -49,7 +50,6 @@
cmdq::Cmdq,
fw::GspArgumentsPadded, //
},
- num,
vgpu::VgpuManager, //
};
@@ -89,7 +89,7 @@ fn init(view: CoherentView<'_, Self>, start: DmaAddress) -> Result<()> {
for i in 0..NUM_PAGES {
io_write!(view, .0[build: i],
start
- .checked_add(num::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 e2b9a5383a66..c95a2d2aacea 100644
--- a/drivers/gpu/nova-core/gsp/cmdq.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq.rs
@@ -60,7 +60,6 @@
GSP_PAGE_SHIFT,
GSP_PAGE_SIZE, //
},
- num,
sbuffer::SBufferIter, //
};
@@ -239,7 +238,7 @@ fn cpuq_free_slots(&self) -> u32 {
/// Returns the size of the region of the CPU message queue that the driver is currently allowed
/// to write to, in bytes.
fn driver_write_area_size(&self) -> usize {
- num::u32_as_usize(self.cpuq_free_slots()) * GSP_PAGE_SIZE
+ usize::from_safe_cast(self.cpuq_free_slots()) * GSP_PAGE_SIZE
}
/// Returns the number of slots of the GSP message queue that the driver is currently allowed
diff --git a/drivers/gpu/nova-core/gsp/fw.rs b/drivers/gpu/nova-core/gsp/fw.rs
index 5c216d777dff..66a777d16898 100644
--- a/drivers/gpu/nova-core/gsp/fw.rs
+++ b/drivers/gpu/nova-core/gsp/fw.rs
@@ -20,6 +20,10 @@
io_read,
io_write, //
},
+ num::casts::{
+ FromSafeCast,
+ IntoSafeCast, //
+ },
prelude::*,
ptr::{
Alignable,
@@ -48,10 +52,6 @@
cmdq::Cmdq, //
GSP_PAGE_SIZE,
},
- num::{
- self,
- FromSafeCast, //
- },
};
/// Maximum size of a single GSP message queue element in bytes.
@@ -675,7 +675,7 @@ fn id8(name: &str) -> u64 {
let init_inner = init!(bindings::LibosMemoryRegionInitArgument {
id8: id8(name),
pa: obj.dma_address(),
- size: num::usize_as_u64(obj.size()),
+ size: obj.size().into_safe_cast(),
kind: cv!(bindings::LibosMemoryRegionKind_LIBOS_MEMORY_REGION_CONTIGUOUS),
loc: cv!(bindings::LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM),
..Zeroable::init_zeroed()
@@ -883,7 +883,7 @@ pub(crate) fn init(cmd_size: usize, function: MsgFunction) -> impl Init<Self, Er
/// Returns the length of the RPC's payload, not including the message and RPC headers.
pub(crate) fn rpc_length(&self) -> usize {
// `length` includes the length of the RPC message header.
- num::u32_as_usize(self.inner.length).saturating_sub(size_of::<Self>())
+ usize::from_safe_cast(self.inner.length).saturating_sub(size_of::<Self>())
}
/// Returns the sequence number of the message.
@@ -980,7 +980,7 @@ pub(crate) fn sequence(&self) -> u64 {
/// Returns the size in bytes of the payload following the header.
pub(crate) fn payload_size(&self) -> usize {
- num::u32_as_usize(self.inner.size)
+ self.inner.size.into_safe_cast()
}
/// Returns the `NV_STATUS` carried by a response to a command.
diff --git a/drivers/gpu/nova-core/gsp/fw/commands.rs b/drivers/gpu/nova-core/gsp/fw/commands.rs
index b4bdbddad79f..e0d8edd73017 100644
--- a/drivers/gpu/nova-core/gsp/fw/commands.rs
+++ b/drivers/gpu/nova-core/gsp/fw/commands.rs
@@ -7,6 +7,7 @@
alloc::ArrayVec,
bitfield,
device,
+ num::casts::IntoSafeCast,
pci,
prelude::*,
transmute::{
@@ -30,7 +31,6 @@
},
GSP_PAGE_SIZE, //
},
- num::IntoSafeCast,
vgpu::VgpuState, //
};
diff --git a/drivers/gpu/nova-core/gsp/nvkv/decode.rs b/drivers/gpu/nova-core/gsp/nvkv/decode.rs
index 84c0c998b2c9..8d227fc26ba0 100644
--- a/drivers/gpu/nova-core/gsp/nvkv/decode.rs
+++ b/drivers/gpu/nova-core/gsp/nvkv/decode.rs
@@ -7,6 +7,7 @@
use kernel::{
alloc::ArrayVec,
+ num::casts::IntoSafeCast,
prelude::*, //
};
use pin_init::{
@@ -14,16 +15,13 @@
stack_try_pin_init, //
};
-use crate::{
- gsp::nvkv::{
- Array,
- Index,
- Key,
- KeyId,
- Op,
- Opcode, //
- },
- num::IntoSafeCast, //
+use crate::gsp::nvkv::{
+ Array,
+ Index,
+ Key,
+ KeyId,
+ Op,
+ Opcode, //
};
/// Defines a schema struct together with its [`Schema`] and [`Visit`] implementations that decode
diff --git a/drivers/gpu/nova-core/gsp/sequencer.rs b/drivers/gpu/nova-core/gsp/sequencer.rs
index 23b09fb875e9..3217cde4e9c9 100644
--- a/drivers/gpu/nova-core/gsp/sequencer.rs
+++ b/drivers/gpu/nova-core/gsp/sequencer.rs
@@ -11,6 +11,7 @@
poll::read_poll_timeout,
Io, //
},
+ num::casts::FromSafeCast,
prelude::*,
time::{
delay::fsleep,
@@ -35,7 +36,6 @@
GspBootContext,
LibosMemoryRegionInitArgument, //
},
- num::FromSafeCast,
sbuffer::SBufferIter,
};
diff --git a/drivers/gpu/nova-core/irq/interrupt_tree.rs b/drivers/gpu/nova-core/irq/interrupt_tree.rs
index 590f921894b6..bd52b77da688 100644
--- a/drivers/gpu/nova-core/irq/interrupt_tree.rs
+++ b/drivers/gpu/nova-core/irq/interrupt_tree.rs
@@ -20,7 +20,10 @@
Io, //
},
irq,
- num::Bounded,
+ num::{
+ casts::IntoSafeCast,
+ Bounded, //
+ },
pci::{
self,
IrqType, //
@@ -30,8 +33,7 @@
use crate::{
driver::Bar0,
- gpu::Chipset,
- num, //
+ gpu::Chipset, //
};
use super::{
@@ -59,10 +61,10 @@
const VECTOR_BITS: u32 = (MAX_NUM_LEAVES * VECTORS_PER_LEAF).ilog2();
/// Width of the vector field in the leaf trigger register.
-const TRIGGER_VECTOR_BITS: u32 = {
+const TRIGGER_VECTOR_BITS: u32 = cv!({
let range = NV_VIRTUAL_FUNCTION_PRIV_CPU_INTR_LEAF_TRIGGER::VECTOR_RANGE;
- num::u8_as_u32(*range.end() - *range.start() + 1)
-};
+ *range.end() - *range.start() + 1
+});
/// Index of a leaf register within the widest supported tree. An 8-leaf tree implements only the
/// lower half of the range.
@@ -85,8 +87,8 @@ pub(super) const fn into_u32(self) -> u32 {
self as u32
}
- pub(super) const fn into_raw(self) -> usize {
- num::u32_as_usize(self.into_u32())
+ pub(super) fn into_raw(self) -> usize {
+ self.into_u32().into_safe_cast()
}
/// Returns the number of subtrees a tree of this size implements.
@@ -368,7 +370,7 @@ pub(super) fn request_for(&self, subtree: Subtree) -> Result<irq::IrqRequest<'_>
}
let entry = match self.msi_type {
- MsiType::MsiX => num::u32_as_usize(subtree.index()),
+ MsiType::MsiX => subtree.index().into_safe_cast(),
MsiType::Msi => 0,
};
diff --git a/drivers/gpu/nova-core/mctp.rs b/drivers/gpu/nova-core/mctp.rs
index f62e79f56b05..12536ec82060 100644
--- a/drivers/gpu/nova-core/mctp.rs
+++ b/drivers/gpu/nova-core/mctp.rs
@@ -13,10 +13,7 @@
prelude::*, //
};
-use crate::{
- bounded_enum,
- num, //
-};
+use crate::bounded_enum;
bounded_enum! {
/// NVDM message type identifiers carried over MCTP.
@@ -88,7 +85,7 @@ impl NvdmHeader {
/// Builds an NVDM header for the given message type.
pub(crate) fn new(nvdm_type: NvdmType) -> Self {
Self::zeroed()
- .with_const_msg_type::<{ num::u8_as_u32(MSG_TYPE_VENDOR_PCI) }>()
+ .with_const_msg_type::<{ cv!(MSG_TYPE_VENDOR_PCI) }>()
.with_vendor_id(Vendor::NVIDIA.as_raw())
.with_nvdm_type(nvdm_type)
}
diff --git a/drivers/gpu/nova-core/mm/bar_user.rs b/drivers/gpu/nova-core/mm/bar_user.rs
index 2e8bd2a8ed91..eb46bd311335 100644
--- a/drivers/gpu/nova-core/mm/bar_user.rs
+++ b/drivers/gpu/nova-core/mm/bar_user.rs
@@ -6,6 +6,7 @@
use kernel::{
io::Io,
new_mutex,
+ num::casts::arch::IntoSafeCastArch,
prelude::*,
sync::Mutex, //
};
@@ -25,11 +26,16 @@
VramAddress,
PAGE_SIZE, //
},
- num::IntoSafeCast,
};
#[cfg(CONFIG_NOVA_CORE_SELFTESTS)]
-use kernel::device;
+use kernel::{
+ device,
+ num::casts::{
+ arch::FromSafeCastArch,
+ IntoSafeCast, //
+ }, //
+};
/// BAR1 user interface for virtual memory mappings.
///
@@ -131,7 +137,7 @@ fn bar_offset(&self, offset: usize) -> Result<usize> {
return Err(EINVAL);
}
- let base_vfn: usize = self.mapped().vfn_start.raw().into_safe_cast();
+ let base_vfn: usize = self.mapped().vfn_start.raw().into_safe_cast_arch();
let base = base_vfn.checked_mul(PAGE_SIZE).ok_or(EOVERFLOW)?;
base.checked_add(offset).ok_or(EOVERFLOW)
}
@@ -240,7 +246,7 @@ pub(crate) fn run_self_test(
let test_vfn = mapped.vfn_start;
// Pre-compute test addresses for the PRAMIN to BAR1 read test.
- let vfn_offset: usize = test_vfn.raw().into_safe_cast();
+ let vfn_offset: usize = test_vfn.raw().into_safe_cast_arch();
let bar1_base_offset = vfn_offset.checked_mul(PAGE_SIZE).ok_or(EOVERFLOW)?;
let bar1_read_offset: usize = bar1_base_offset + 0x100;
let vram_read_addr = test_vram + 0x100;
@@ -322,11 +328,11 @@ pub(crate) fn run_self_test(
let mut total_size = 0usize;
for block in blocks.iter() {
- total_size += IntoSafeCast::<usize>::into_safe_cast(block.size());
+ total_size += usize::from_safe_cast_arch(block.size());
// Map all pages of this block.
let page_size: u64 = PAGE_SIZE.into_safe_cast();
- let num_pages: usize = (block.size() / page_size).into_safe_cast();
+ let num_pages: usize = (block.size() / page_size).into_safe_cast_arch();
let mut pfns = KVec::new();
for j in 0..num_pages {
@@ -340,7 +346,7 @@ pub(crate) fn run_self_test(
}
let mapped = vmm.map_pages(mm, &pfns, None, true)?;
- let bar1_base_vfn: usize = mapped.vfn_start.raw().into_safe_cast();
+ let bar1_base_vfn: usize = mapped.vfn_start.raw().into_safe_cast_arch();
let bar1_base = bar1_base_vfn.checked_mul(PAGE_SIZE).ok_or(EOVERFLOW)?;
for j in 0..num_pages {
diff --git a/drivers/gpu/nova-core/mm/pagetable/map.rs b/drivers/gpu/nova-core/mm/pagetable/map.rs
index f7774b9cc972..0307541463f3 100644
--- a/drivers/gpu/nova-core/mm/pagetable/map.rs
+++ b/drivers/gpu/nova-core/mm/pagetable/map.rs
@@ -11,6 +11,7 @@
GpuBuddyAllocMode, //
},
io::io_write,
+ num::casts::IntoSafeCast,
prelude::*,
ptr::Alignment,
rbtree::{
@@ -40,17 +41,12 @@
PdeOps,
PteOps, //
};
-use crate::{
- mm::{
- GpuMm,
- Pfn,
- Vfn,
- VramAddress,
- PAGE_SIZE, //
- },
- num::{
- IntoSafeCast, //
- },
+use crate::mm::{
+ GpuMm,
+ Pfn,
+ Vfn,
+ VramAddress,
+ PAGE_SIZE, //
};
/// A pre-allocated and zeroed page table page.
diff --git a/drivers/gpu/nova-core/mm/pagetable/walk.rs b/drivers/gpu/nova-core/mm/pagetable/walk.rs
index 76c1729971f5..80e29d9babba 100644
--- a/drivers/gpu/nova-core/mm/pagetable/walk.rs
+++ b/drivers/gpu/nova-core/mm/pagetable/walk.rs
@@ -36,7 +36,10 @@
use core::marker::PhantomData;
-use kernel::prelude::*;
+use kernel::{
+ num::casts::IntoSafeCast,
+ prelude::*, //
+};
use super::{
DualPdeOps,
@@ -48,18 +51,13 @@
PdeOps,
PteOps, //
};
-use crate::{
- mm::{
- pramin,
- GpuMm,
- Pfn,
- Vfn,
- VirtualAddress,
- VramAddress, //
- },
- num::{
- IntoSafeCast, //
- },
+use crate::mm::{
+ pramin,
+ GpuMm,
+ Pfn,
+ Vfn,
+ VirtualAddress,
+ VramAddress, //
};
/// Result of walking to a PTE.
diff --git a/drivers/gpu/nova-core/mm/pramin.rs b/drivers/gpu/nova-core/mm/pramin.rs
index c83b1681d241..d712dbdb82f5 100644
--- a/drivers/gpu/nova-core/mm/pramin.rs
+++ b/drivers/gpu/nova-core/mm/pramin.rs
@@ -13,6 +13,10 @@
Io,
Mmio, //
},
+ num::casts::{
+ arch::IntoSafeCastArch,
+ IntoSafeCast, //
+ },
prelude::*,
ptr::{
Alignable,
@@ -37,7 +41,6 @@
},
VramAddress, //
},
- num::IntoSafeCast, //
};
/// Size of the PRAMIN window (1 MiB).
@@ -156,7 +159,7 @@ fn window_offset(&mut self, addr: VramAddress, len: usize) -> Result<usize> {
self.window_range = window_range;
}
- Ok((addr - self.window_range.start).into_safe_cast())
+ Ok((addr - self.window_range.start).into_safe_cast_arch())
}
/// Return a typed MMIO view of a `T` at `vram_addr`.
diff --git a/drivers/gpu/nova-core/mm/vmm.rs b/drivers/gpu/nova-core/mm/vmm.rs
index 51b500a27233..585f747058c5 100644
--- a/drivers/gpu/nova-core/mm/vmm.rs
+++ b/drivers/gpu/nova-core/mm/vmm.rs
@@ -8,6 +8,10 @@
use kernel::{
gpu::buddy::AllocatedBlocks,
maple_tree::MapleTreeAlloc,
+ num::casts::{
+ arch::IntoSafeCastArch,
+ IntoSafeCast, //
+ },
prelude::*,
rbtree::RBTree, //
};
@@ -34,10 +38,7 @@
Vfn,
VramAddress,
PAGE_SIZE, //
- },
- num::{
- IntoSafeCast, //
- },
+ }, //
};
/// Multi-page prepared mapping -- VA range allocated, ready for execute.
@@ -155,7 +156,7 @@ pub(crate) fn new(
va_size: u64,
) -> Result<Self> {
let page_size: u64 = PAGE_SIZE.into_safe_cast();
- let va_pages: usize = (va_size / page_size).into_safe_cast();
+ let va_pages: usize = (va_size / page_size).into_safe_cast_arch();
let virt_alloc = KBox::pin_init(MapleTreeAlloc::<()>::new(), GFP_KERNEL)?;
Ok(Self {
@@ -181,8 +182,8 @@ fn alloc_vfn_range(&self, num_pages: usize, va_range: Option<Range<u64>>) -> Res
if range_size != size {
return Err(EINVAL);
}
- let start_vfn: usize = (r.start / page_size).into_safe_cast();
- let end_vfn: usize = (r.end / page_size).into_safe_cast();
+ let start_vfn: usize = (r.start / page_size).into_safe_cast_arch();
+ let end_vfn: usize = (r.end / page_size).into_safe_cast_arch();
self.virt_alloc
.insert_range(start_vfn..end_vfn, (), GFP_KERNEL)?;
start_vfn
@@ -197,7 +198,7 @@ fn alloc_vfn_range(&self, num_pages: usize, va_range: Option<Range<u64>>) -> Res
/// Free a virtual frame number range back to the maple tree.
fn free_vfn(&self, vfn: Vfn) {
- let vfn_index: usize = vfn.raw().into_safe_cast();
+ let vfn_index: usize = vfn.raw().into_safe_cast_arch();
if self.virt_alloc.erase(vfn_index).is_none() {
kernel::pr_warn!("free_vfn: VFN {} not found in maple tree\n", vfn_index);
}
diff --git a/drivers/gpu/nova-core/num.rs b/drivers/gpu/nova-core/num.rs
index 3b3f1ed2dc75..2df66eccb172 100644
--- a/drivers/gpu/nova-core/num.rs
+++ b/drivers/gpu/nova-core/num.rs
@@ -5,164 +5,6 @@
//! This is essentially a staging module for code to mature until it can be moved to the `kernel`
//! crate.
-use kernel::{
- macros::paste,
- prelude::*, //
-};
-
-/// Implements safe `as` conversion functions from a given type into a series of target types.
-///
-/// These functions can be used in place of `as`, with the guarantee that they will be lossless.
-macro_rules! impl_safe_as {
- ($from:ty as { $($into:ty),* }) => {
- $(
- paste! {
- #[doc = ::core::concat!(
- "Losslessly converts a [`",
- ::core::stringify!($from),
- "`] into a [`",
- ::core::stringify!($into),
- "`].")]
- ///
- /// This conversion is allowed as it is always lossless. Prefer this over the `as`
- /// keyword to ensure no lossy casts are performed.
- ///
- /// This is for use from a `const` context. For non `const` use, prefer the
- /// [`FromSafeCast`] and [`IntoSafeCast`] traits.
- ///
- /// # Examples
- ///
- /// ```
- /// use crate::num;
- ///
- #[doc = ::core::concat!(
- "assert_eq!(num::",
- ::core::stringify!($from),
- "_as_",
- ::core::stringify!($into),
- "(1",
- ::core::stringify!($from),
- "), 1",
- ::core::stringify!($into),
- ");")]
- /// ```
- #[allow(unused)]
- #[inline(always)]
- pub(crate) const fn [<$from _as_ $into>](value: $from) -> $into {
- ::kernel::build_assert::static_assert!(size_of::<$into>() >= size_of::<$from>());
-
- value as $into
- }
- }
- )*
- };
-}
-
-impl_safe_as!(u8 as { u16, u32, u64, usize });
-impl_safe_as!(u16 as { u32, u64, usize });
-impl_safe_as!(u32 as { u64, usize } );
-// `u64` and `usize` have the same size on 64-bit platforms.
-#[cfg(CONFIG_64BIT)]
-impl_safe_as!(u64 as { usize } );
-
-// A `usize` fits into a `u64` on 32 and 64-bit platforms.
-#[cfg(any(CONFIG_32BIT, CONFIG_64BIT))]
-impl_safe_as!(usize as { u64 });
-
-// A `usize` fits into a `u32` on 32-bit platforms.
-#[cfg(CONFIG_32BIT)]
-impl_safe_as!(usize as { u32 });
-
-/// Extension trait providing guaranteed lossless cast to `Self` from `T`.
-///
-/// The standard library's `From` implementations do not cover conversions that are not portable or
-/// future-proof. For instance, even though it is safe today, `From<usize>` is not implemented for
-/// [`u64`] because of the possibility to support larger-than-64bit architectures in the future.
-///
-/// The workaround is to either deal with the error handling of [`TryFrom`] for an operation that
-/// technically cannot fail, or to use the `as` keyword, which can silently strip data if the
-/// destination type is smaller than the source.
-///
-/// Both options are hardly acceptable for the kernel. It is also a much more architecture
-/// dependent environment, supporting only 32 and 64 bit architectures, with some modules
-/// explicitly depending on a specific bus width that could greatly benefit from infallible
-/// conversion operations.
-///
-/// Thus this extension trait that provides, for the architecture the kernel is built for, safe
-/// conversion between types for which such cast is lossless.
-///
-/// In other words, this trait is implemented if, for the current build target and with `t: T`, the
-/// `t as Self` operation is completely lossless.
-///
-/// Prefer this over the `as` keyword to ensure no lossy casts are performed.
-///
-/// If you need to perform a conversion in `const` context, use [`cv!`](kernel::num::cv).
-///
-/// # Examples
-///
-/// ```
-/// use crate::num::FromSafeCast;
-///
-/// assert_eq!(usize::from_safe_cast(0xf00u32), 0xf00u32 as usize);
-/// ```
-pub(crate) trait FromSafeCast<T> {
- /// Create a `Self` from `value`. This operation is guaranteed to be lossless.
- fn from_safe_cast(value: T) -> Self;
-}
-
-impl FromSafeCast<usize> for u64 {
- fn from_safe_cast(value: usize) -> Self {
- usize_as_u64(value)
- }
-}
-
-#[cfg(CONFIG_32BIT)]
-impl FromSafeCast<usize> for u32 {
- fn from_safe_cast(value: usize) -> Self {
- usize_as_u32(value)
- }
-}
-
-impl FromSafeCast<u32> for usize {
- fn from_safe_cast(value: u32) -> Self {
- u32_as_usize(value)
- }
-}
-
-#[cfg(CONFIG_64BIT)]
-impl FromSafeCast<u64> for usize {
- fn from_safe_cast(value: u64) -> Self {
- u64_as_usize(value)
- }
-}
-
-/// Counterpart to the [`FromSafeCast`] trait, i.e. this trait is to [`FromSafeCast`] what [`Into`]
-/// is to [`From`].
-///
-/// See the documentation of [`FromSafeCast`] for the motivation.
-///
-/// # Examples
-///
-/// ```
-/// use crate::num::IntoSafeCast;
-///
-/// assert_eq!(0xf00u32.into_safe_cast(), 0xf00u32 as usize);
-/// ```
-pub(crate) trait IntoSafeCast<T> {
- /// Convert `self` into a `T`. This operation is guaranteed to be lossless.
- fn into_safe_cast(self) -> T;
-}
-
-/// Reverse operation for types implementing [`FromSafeCast`].
-impl<S, T> IntoSafeCast<T> for S
-where
- T: FromSafeCast<S>,
-{
- fn into_safe_cast(self) -> T {
- T::from_safe_cast(self)
- }
-}
-
/// Creates an enum type associated to a [`Bounded`](kernel::num::Bounded), with a [`From`]
/// conversion to the associated `Bounded` and either a [`TryFrom`] or `From` conversion from the
/// associated `Bounded`.
diff --git a/drivers/gpu/nova-core/vbios.rs b/drivers/gpu/nova-core/vbios.rs
index a2d2d170b501..a32ef135f879 100644
--- a/drivers/gpu/nova-core/vbios.rs
+++ b/drivers/gpu/nova-core/vbios.rs
@@ -5,6 +5,7 @@
use kernel::{
device,
io::Io,
+ num::casts::FromSafeCast,
prelude::*,
ptr::{
Alignable,
@@ -29,7 +30,6 @@
FalconUCodeDescV2,
FalconUCodeDescV3, //
},
- num::FromSafeCast,
};
/// BIOS Image Type from PCI Data Structure code_type field.
---
base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d
change-id: 20261130-dev-nova-num-22acabc345ad
Best regards,
--
Gary Guo <gary@garyguo.net>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] gpu: nova-core: migrate to `kernel::num::casts`
2026-11-30 13:07 [PATCH] gpu: nova-core: migrate to `kernel::num::casts` Gary Guo
@ 2026-12-02 6:56 ` Alexandre Courbot
0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Courbot @ 2026-12-02 6:56 UTC (permalink / raw)
To: Gary Guo
Cc: Danilo Krummrich, Alice Ryhl, David Airlie, Simona Vetter,
nova-gpu, dri-devel, Gary Guo
On Mon Nov 30, 2026 at 10:07 PM JST, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
>
> Migrate users of the Nova's internal `num` module over to the
> `kernel::num::casts` instead. `FromSafeCast` and `IntoSafeCast` are
> migrated to the same-named traits or their `Arch` variant; `uX_as_uY`
> methods are migrated to `FromSafeCast`/`IntoSafeCast` outside const
> contexts and migrated to `cv!` macro inside const context.
>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> ---
> drivers/gpu/nova-core/falcon.rs | 2 +-
> drivers/gpu/nova-core/falcon/fsp.rs | 4 +-
> drivers/gpu/nova-core/fb.rs | 2 +-
> drivers/gpu/nova-core/firmware.rs | 2 +-
> drivers/gpu/nova-core/firmware/booter.rs | 2 +-
> drivers/gpu/nova-core/firmware/fwsec.rs | 2 +-
> drivers/gpu/nova-core/firmware/fwsec/bootloader.rs | 2 +-
> drivers/gpu/nova-core/firmware/gen_bootloader.rs | 2 +-
> drivers/gpu/nova-core/firmware/gsp.rs | 2 +-
> drivers/gpu/nova-core/firmware/radix3.rs | 6 +-
> drivers/gpu/nova-core/firmware/tlv.rs | 8 +-
> drivers/gpu/nova-core/gsp.rs | 4 +-
> drivers/gpu/nova-core/gsp/cmdq.rs | 3 +-
> drivers/gpu/nova-core/gsp/fw.rs | 14 +-
> drivers/gpu/nova-core/gsp/fw/commands.rs | 2 +-
> drivers/gpu/nova-core/gsp/nvkv/decode.rs | 18 ++-
> drivers/gpu/nova-core/gsp/sequencer.rs | 2 +-
> drivers/gpu/nova-core/irq/interrupt_tree.rs | 20 +--
> drivers/gpu/nova-core/mctp.rs | 7 +-
> drivers/gpu/nova-core/mm/bar_user.rs | 20 ++-
> drivers/gpu/nova-core/mm/pagetable/map.rs | 18 +--
> drivers/gpu/nova-core/mm/pagetable/walk.rs | 24 ++--
> drivers/gpu/nova-core/mm/pramin.rs | 7 +-
> drivers/gpu/nova-core/mm/vmm.rs | 17 +--
> drivers/gpu/nova-core/num.rs | 158 ---------------------
> drivers/gpu/nova-core/vbios.rs | 2 +-
> 26 files changed, 94 insertions(+), 256 deletions(-)
There have been a couple of efforts to do that already (most recent
being [1] IIRC), but landing it has been tricky and pending on the
availability of `cv!`.
And with all the code landing in Nova at the moment this is going to be
difficult to apply, actually this already results in lots of conflicts
on today's drm-rust-next.
If we want to get this in soon, I think a good point to land this would
be after the r000 switch and before we start landing the vGPU patches. I
can point you to the tree once I have a better idea of what it will look
like, or I can revive my own patch if you prefer.
[1] https://lore.kernel.org/all/20260828-nova_num-v1-0-e21f17ba4127@nvidia.com/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 6:56 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 13:07 [PATCH] gpu: nova-core: migrate to `kernel::num::casts` Gary Guo
2026-12-02 6:56 ` Alexandre Courbot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox