Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796044070; b=TVsKhomQUCqYGxQsgWhGN9DEdSvaBmiOa5H2OiLhowExChdiHcALYv4ZjZULVeXYlzn5 iL+k+/JHpnBZu2BavO0xWxKaBiUOTBwFnXl3ivLe5n0as1pvxciLn6PKM/YwqVtmJ5oIq xhtiub1oYY5EOiBaz8BAzhW2/+A5S5/hEEmhgCw+wsrRiX/lzIZQFCOAOBD0UsuYSLi8C RNEz7exffZu3l9OeJNxn/WXsO9pH9Y6GuS8g1AHPmhXdWZ+tNbjyJLOTh1sW3EiDX2pqq 3yY2GaiNfybZoB/xZBL14pWalYG7VgiSOMgJuuZ3Ht7lXeY4ELxmYJpZCfzMe7hzixQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796044070; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=XZV2k2PG4VKrsd3lStO5Bm7jfklUOUwh/GNp1Pn5N9s=; b=edGVzFSwdeYvPbFsO/sbJWznkKWLdq9vNSx4zrhExmOnjL+CHhM44tzzHAXE+VpTP2PT lmWDId1LVi5aHZSLAmUAM/kt93hq0pSZhQk84P0E6IFG1HG0qKdkLcILAKtFlyWMLY09j 7SV5a4Nq5yCFKZW3XzJV1WiQsKxlZBsTdtAJc8bfaMUvYjxgB60pbIKzatL9/mZ/NA4RZ 38HUUR077Hnzc5jUvzEmOVDcwIyOAx3P40PmLKPuhC9IAEBRx8xdndU0gqpPu8sb9g7x6 7Y5FPzyR+Ys1P3/kK/gWdiJgstfSj/p52PfbffE+I99ocTFQJWwPs3c5YiU0qobX2tw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=A1VdIgGP; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F672606A320 for ; Mon, 30 Nov 2026 13:07:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=A1VdIgGP Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D4D51F0089C; Mon, 30 Nov 2026 13:07:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796044069; bh=XZV2k2PG4VKrsd3lStO5Bm7jfklUOUwh/GNp1Pn5N9s=; h=From:Date:Subject:To:Cc; b=A1VdIgGPqh3PrOZQb/csLe0NeRS9TNChvBtBat0qQwTouLq9ll+nFn8hwHPD3qDIE AxSMxwx9l2Qnqn5yXeXo7fAaqfOMxJ32RBywnCZeWyyfIZpAJhlDm45h1UxDCa3ufj TP9wPHhmol99FtFaTP8z69//o8rJdz8zvws0lWnamjQVHL7oJwV64vjkudbcSCoFHL rp7ALbo1QfljNTiVJXTsIfu5doo639ZKtDgt7Jp3znpHWj30Sf4vORg/eAwaR086Zh zGvQ3Z4sC8b3aLVsrV7JxcHariQIDeb8Fn8sVrYDkRW+icZzVi9yBcQHWg5OPCRrqH 5Qf+rMzGBVnww== From: Gary Guo Date: Mon, 30 Nov 2026 13:07:45 +0000 Subject: [PATCH] gpu: nova-core: migrate to `kernel::num::casts` X-Mailing-List: nova-gpu@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261130-dev-nova-num-v1-1-6f8e19907b9d@garyguo.net> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/yWMSw7CMAwFr1J5jUWTAguuglg4iaFB1EX5KVLVu 5PAcp7ezAaRg+cI12GDwMVHv0oDdRjAziRPRu8agx71RalpRMcFZS2EkhfUmiwZO53O5KApn8A PX3+52/3PMZsX29Qb/WEoMppAYuc+vb3kisI1HReKiQPs+xfS/5CclAAAAA== X-Change-ID: 20261130-dev-nova-num-22acabc345ad To: Danilo Krummrich , Alice Ryhl , Alexandre Courbot , David Airlie , Simona Vetter Cc: nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, Gary Guo X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796044067; l=31089; i=gary@garyguo.net; s=20221204; h=from:subject:message-id; bh=4TIknRuG4zgkZU1AYipM+3CkYYmyFTgpM4JEG64E0ng=; b=or/Oa6ULcjEFYhM9Z6JZsmzsUjXaExLEwzlWjpisN5FZ9LGZKeWCTxiiPXyyFeXjpSmw0e7rN 9IN+PpFb6tGCQb+BabW+NCtbipvm782ovSXCaA6qkl/aSHObm1kgOQO X-Developer-Key: i=gary@garyguo.net; a=ed25519; pk=vB3uIX95SM4eVrIqo1DWNWKDKD2xzB+yLLLr0yOPYMo= From: Gary Guo 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 --- 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> { 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 { 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 usize { // `length` includes the length of the RPC message header. - num::u32_as_usize(self.inner.length).saturating_sub(size_of::()) + usize::from_safe_cast(self.inner.length).saturating_sub(size_of::()) } /// 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 } 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 { 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::::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 { 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 { 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>) -> 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>) -> 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` 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 { - /// Create a `Self` from `value`. This operation is guaranteed to be lossless. - fn from_safe_cast(value: T) -> Self; -} - -impl FromSafeCast for u64 { - fn from_safe_cast(value: usize) -> Self { - usize_as_u64(value) - } -} - -#[cfg(CONFIG_32BIT)] -impl FromSafeCast for u32 { - fn from_safe_cast(value: usize) -> Self { - usize_as_u32(value) - } -} - -impl FromSafeCast for usize { - fn from_safe_cast(value: u32) -> Self { - u32_as_usize(value) - } -} - -#[cfg(CONFIG_64BIT)] -impl FromSafeCast 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 { - /// 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 IntoSafeCast for S -where - T: FromSafeCast, -{ - 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