nova-core: Convert bindings to use zerocopy
Subsystems (5)
cover nova-core: Convert bindings to use zerocopy
This patch converts all the bindings to use the new zerocopy library. Bindings are currently generated using a fairly simple bindgen script[1]. This is a temporary solution until the bindings have been stablisied but for now upstream should be kept in sync with the resulting output.
Note that this does not completely remove all users of the transmute From/AsBytes traits from nova-core, leaving some ambiguity with when referring to FromBytes. However others have recently posted patches[2][3][4] to remove the majority of other uses which should allow us to remove transmute::FromBytes entirely from nova-core.
Given the inevitable conflicts that will arise from trying to merge several different series doing bits of the same thing I'm happy if someone wants to take this patch and the others and roll them into a single series. Alternatively I am happy to take that on - hopefully Alex can provide some guidance here for what would be preferred.
[1] - https://github.com/apopple-nvidia/nova-gsp-binding-generator/tree/zerocopy
[2] - https://lore.kernel.org/rust-for-linux/[email protected]/
[3] - https://lore.kernel.org/rust-for-linux/[email protected]/
[4] - https://lore.kernel.org/rust-for-linux/[email protected]/
Alistair Popple (1):
nova-core: Update firmware bindings to use zerocopy traits
drivers/gpu/nova-core/Makefile | 4 + drivers/gpu/nova-core/gsp/cmdq.rs | 21 +- .../gpu/nova-core/gsp/cmdq/continuation.rs | 16 +- drivers/gpu/nova-core/gsp/commands.rs | 19 +- drivers/gpu/nova-core/gsp/fw.rs | 54 +---- drivers/gpu/nova-core/gsp/fw/commands.rs | 50 ++--- drivers/gpu/nova-core/gsp/fw/r570_144.rs | 41 ++++ .../gpu/nova-core/gsp/fw/r570_144/bindings.rs | 185 ++++++++++++------ drivers/gpu/nova-core/gsp/sequencer.rs | 5 +- scripts/Makefile.build | 4 +- 10 files changed, 223 insertions(+), 176 deletions(-)
Signature · 1 line
2.54.0
PATCH 1/1 nova-core: Update firmware bindings to use zerocopy traits
Currently most of nova-core uses unsafe implementations of AsBytes and FromBytes in order to read and write GSP commands using the generated bindings. Whilst this is generally safe in practice there is nothing that actually guarantees or checks this.
This is exactly what the zerocopy library was introduced to do - provide compile-time checks ensuring From/AsBytes is safe. Make use of these checks by converting all our generated bindings to derive the required traits for the zerocopy checks, removing many unsafe implementations in the process.
Note this does require the use of an unstable feature - trivial_bounds
- as some bindings end up needing to derive zerocopy::Unaligned. A work-around is also required in the bindings to make some of the __IncompleteArrayField<T> ZSTs monomorphic as the check macro can not prove a generic type is aligned and thus forces T to derive zerocopy::Unaligned, which is not satisfied by all types.
--- drivers/gpu/nova-core/Makefile | 4 + drivers/gpu/nova-core/gsp/cmdq.rs | 21 +- .../gpu/nova-core/gsp/cmdq/continuation.rs | 16 +- drivers/gpu/nova-core/gsp/commands.rs | 19 +- drivers/gpu/nova-core/gsp/fw.rs | 54 +---- drivers/gpu/nova-core/gsp/fw/commands.rs | 50 ++--- drivers/gpu/nova-core/gsp/fw/r570_144.rs | 41 ++++ .../gpu/nova-core/gsp/fw/r570_144/bindings.rs | 185 ++++++++++++------ drivers/gpu/nova-core/gsp/sequencer.rs | 5 +- scripts/Makefile.build | 4 +- 10 files changed, 223 insertions(+), 176 deletions(-)
drivers/gpu/nova-core/Makefile +4 -0
@@ -1,4 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 +# The GSP firmware bindings derive zerocopy's `IntoBytes` on `#[repr(C)]` +# unions, which is gated behind the `zerocopy_derive_union_into_bytes` cfg. +rustflags-y += --cfg zerocopy_derive_union_into_bytes + obj-$(CONFIG_NOVA_CORE) += nova-core.o nova-core-y := nova_core.o
drivers/gpu/nova-core/gsp/cmdq.rs +15 -6
@@ -29,6 +29,14 @@ }, }; +// TODO: Remove `as` once FromBytes is removed completely +use zerocopy::{ +FromBytes as ZCFromBytes , +Immutable , +IntoBytes , +KnownLayout ,// +}; +use continuation::{ContinuationRecord ,SplitState ,//
@@ -76,7 +84,7 @@ pub(crate) trait CommandToGsp {const FUNCTION :MsgFunction ;/// Type generated by [`CommandToGsp::init`], to be written into the command queue buffer. -type Command :FromBytes +AsBytes ; +type Command :ZCFromBytes +KnownLayout +Immutable +IntoBytes ;/// Type of the reply expected from the GSP, or [`NoReply`] for commands that don't /// have a reply.
@@ -133,7 +141,7 @@ pub(crate) trait MessageFromGsp: Sized {type InitError ;/// Type containing the raw message to be read from the message queue. -type Message :FromBytes ; +type Message :ZCFromBytes +KnownLayout +Immutable ;/// Method reading the message from the message queue and returning it. ///
@@ -383,7 +391,7 @@ fn allocate_command(&mut self, size: usize, timeout: Delta) -> Result<GspCommand };// Extract area for the `GspMsgElement`. -let (header, slice_1) =GspMsgElement ::from_bytes_mut_prefix (slice_1).ok_or (EIO )?; +let (header, slice_1) =GspMsgElement ::mut_from_prefix (slice_1).map_err (|_|EIO )?;// Create the contents area. let (slice_1, slice_2) =if slice_1.len () > size {
@@ -639,7 +647,7 @@ fn send_single_command<M>(&mut self, bar: Bar0<'_>, command: M) -> Result// Extract area for the command itself. The GSP message header and the command header // together are guaranteed to fit entirely into a single page, so it's ok to only look // at `dst.contents.0` here. -let (cmd, payload_1) =M ::Command ::from_bytes_mut_prefix (dst.contents.0 ).ok_or (EIO )?; +let (cmd, payload_1) =M ::Command ::mut_from_prefix (dst.contents.0 ).map_err (|_|EIO )?;// Fill the header and command in-place. let msg_element =GspMsgElement ::init (self.seq, size_in_bytes,M ::FUNCTION );
@@ -744,7 +752,7 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> { .map(|(slice_1, slice_2)| (slice_1.as_flattened(), slice_2.as_flattened()))?;// Extract the `GspMsgElement`. -let (header, slice_1) =GspMsgElement ::from_bytes_prefix (slice_1).ok_or (EIO )?; +let (header, slice_1) =GspMsgElement ::ref_from_prefix (slice_1).map_err (|_|EIO )?;dev_dbg! ( &self.dev,
@@ -822,7 +830,8 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>// Extract the message. Store the result as we want to advance the read pointer even in // case of failure. let result =if function ==M ::FUNCTION { -let (cmd, contents_1) =M ::Message ::from_bytes_prefix (message.contents.0 ).ok_or (EIO )?; +let (cmd, contents_1) = +M ::Message ::ref_from_prefix (message.contents.0 ).map_err (|_|EIO )?;let mut sbuffer =SBufferIter ::new_reader ([contents_1, message.contents.1 ]);M ::read (cmd, &mut sbuffer)
drivers/gpu/nova-core/gsp/cmdq/continuation.rs +6 -10
@@ -171,22 +171,18 @@ fn init_variable_payload(mod tests {use super ::*; -use kernel::transmute::{ -AsBytes , -FromBytes ,// +use zerocopy_derive::{ +FromBytes , +Immutable , +IntoBytes , +KnownLayout ,// };/// Non-zero-sized command header for testing. #[repr( C )] -#[derive( Clone , Copy , Zeroable )] +#[derive( Clone , Copy , FromBytes , Immutable , IntoBytes , KnownLayout , Zeroable )] struct TestHeader ([u8 ;64 ]); -// SAFETY: `TestHeader` is a plain array of bytes for which all bit patterns are valid. -unsafe impl FromBytes for TestHeader {} - -// SAFETY: `TestHeader` is a plain array of bytes for which all bit patterns are valid. -unsafe impl AsBytes for TestHeader {} -struct TestPayload { data:KVVec <u8 >, }
drivers/gpu/nova-core/gsp/commands.rs +5 -14
@@ -12,11 +12,7 @@use kernel::{ device, pci, - prelude::*, - transmute::{ -AsBytes , -FromBytes ,// - },// + prelude::*,// };use crate ::{
@@ -130,13 +126,12 @@ fn init_variable_payload(let mut string_data =KVec ::new ();for entryin self.entries.iter ().take (Self ::NUM_ENTRIES ) { - dst.write_all ( - fw::commands::PackedRegistryEntry ::new ( + dst.write_all (zerocopy::IntoBytes ::as_bytes ( + &fw::commands::PackedRegistryEntry ::new ( (string_data_start_offset + string_data.len ())as u32 , entry.value, - ) - .as_bytes (), - )?; + ), + ))?;let key_bytes = entry.key.as_bytes (); string_data.extend_from_slice (key_bytes,GFP_KERNEL )?;
@@ -150,10 +145,6 @@ fn init_variable_payload(/// Message type for GSP initialization done notification. struct GspInitDone ; -// SAFETY: `GspInitDone` is a zero-sized type with no bytes, therefore it -// trivially has no uninitialized bytes. -unsafe impl FromBytes for GspInitDone {} -impl MessageFromGsp for GspInitDone {const FUNCTION :MsgFunction =MsgFunction ::GspInitDone ;type InitError =Infallible ;
drivers/gpu/nova-core/gsp/fw.rs +5 -49
116 lines not on this page · raw message
drivers/gpu/nova-core/gsp/fw/commands.rs +11 -39
104 lines not on this page · raw message
drivers/gpu/nova-core/gsp/fw/r570_144.rs +41 -0
50 lines not on this page · raw message
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +130 -55
492 lines not on this page · raw message
drivers/gpu/nova-core/gsp/sequencer.rs +3 -2
@@ -15,9 +15,10 @@ delay::fsleep, Delta, // }, - transmute::FromBytes, // }; +use zerocopy::FromBytes ; +use crate ::{ driver::Bar0 , falcon::{
@@ -82,7 +83,7 @@ pub(crate) enum GspSeqCmd {impl GspSeqCmd {/// Creates a new `GspSeqCmd` from raw data returning the command and its size in bytes. pub (crate )fn new (data: &[u8 ], dev: &device::Device ) ->Result <(Self ,usize )> { -let fw_cmd = fw::SequencerBufferCmd ::from_bytes (data).ok_or (EINVAL )?; +let fw_cmd = fw::SequencerBufferCmd ::ref_from_bytes (data).map_err (|_|EINVAL )?;let opcode_size = core::mem::size_of ::<u32 >(); let (cmd, size) = match fw_cmd.opcode()? {
scripts/Makefile.build +3 -1
@@ -314,10 +314,12 @@ $(obj)/%.lst: $(obj)/%.c FORCE # - Stable since Rust 1.89.0: `feature(generic_arg_infer)`. # - Expected to become stable: `feature(arbitrary_self_types)`. # - To be determined: `feature(used_with_arg)`. +# - Required by nova-core's zerocopy firmware bindings, whose derives emit +# trivial `where` bounds: `feature(trivial_bounds)`. # # Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on # the unstable features in use. -rust_allowed_features := arbitrary_self_types,asm_goto,generic_arg_infer,used_with_arg +rust_allowed_features := arbitrary_self_types,asm_goto,generic_arg_infer,used_with_arg,trivial_bounds # `--out-dir` is required to avoid temporaries being created by `rustc` in the # current working directory, which may be not accessible in the out-of-tree
Signature · 1 line
2.54.0
Alexandre Courbot
maintainerRe: [PATCH 1/1] nova-core: Update firmware bindings to use zerocopy traits
Quoted 11 lines · Alistair Popple
Currently most of nova-core uses unsafe implementations of AsBytes and FromBytes in order to read and write GSP commands using the generated bindings. Whilst this is generally safe in practice there is nothing that actually guarantees or checks this.
This is exactly what the zerocopy library was introduced to do - provide compile-time checks ensuring From/AsBytes is safe. Make use of these checks by converting all our generated bindings to derive the required traits for the zerocopy checks, removing many unsafe implementations in the process.
Quoted 8 lines
Note this does require the use of an unstable feature -
trivial_bounds
- as some bindings end up needing to derivezerocopy::Unaligned. A work-around is also required in the bindings to make some of the __IncompleteArrayField<T> ZSTs monomorphic as the check macro can not prove a generic type is aligned and thus forces T to derivezerocopy::Unaligned, which is not satisfied by all types.Signed-off-by Alistair Popple <[email protected]>
I have counted and this removes 28 unsafe statements. :) (it also adds 2 tbf)
Quoted 31 lines
--- drivers/gpu/nova-core/Makefile | 4 + drivers/gpu/nova-core/gsp/cmdq.rs | 21 +- .../gpu/nova-core/gsp/cmdq/continuation.rs | 16 +- drivers/gpu/nova-core/gsp/commands.rs | 19 +- drivers/gpu/nova-core/gsp/fw.rs | 54 +---- drivers/gpu/nova-core/gsp/fw/commands.rs | 50 ++--- drivers/gpu/nova-core/gsp/fw/r570_144.rs | 41 ++++ .../gpu/nova-core/gsp/fw/r570_144/bindings.rs | 185 ++++++++++++------ drivers/gpu/nova-core/gsp/sequencer.rs | 5 +- scripts/Makefile.build | 4 +- 10 files changed, 223 insertions(+), 176 deletions(-)drivers/gpu/nova-core/Makefile +4
-0@@ -1,4 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 +# The GSP firmware bindings derive zerocopy's `IntoBytes` on `#[repr(C)]` +# unions, which is gated behind the `zerocopy_derive_union_into_bytes` cfg. +rustflags-y += --cfg zerocopy_derive_union_into_bytes + obj-$(CONFIG_NOVA_CORE) += nova-core.o nova-core-y := nova_core.odrivers/gpu/nova-core/gsp/cmdq.rs +0
-0
drivers/gpu/nova-core/gsp/cmdq.rs line 34
Quoted 6 lines
drivers/gpu/nova-core/gsp/cmdq.rs +3
-0@@ -29,6 +29,14 @@ }, }; +// TODO: Remove `as` once FromBytes is removed completely +use zerocopy::{ +FromBytes as ZCFromBytes ,
Since zerocopy's FromBytes is the one we will keep long-term, should we rather rename the one from transmute?
<...>
Quoted 43 lines
drivers/gpu/nova-core/gsp/fw/r570_144.rs +31
-0@@ -23,9 +23,50 @@ )]use kernel::ffi;use pin_init::MaybeZeroable ; +use zerocopy_derive::{ +FromBytes , +Immutable , +IntoBytes , +KnownLayout ,// +};include! ("r570_144/bindings.rs" );// SAFETY: This type has a size of zero, so its inclusion into another type should not affect their // ability to implement `Zeroable`. unsafe impl <T > kernel::prelude::Zeroable for __IncompleteArrayField <T > {} + +/// Monomorphic version of [`__IncompleteArrayField<PACKED_REGISTRY_ENTRY>`]. +/// +/// zerocopy's `IntoBytes` derive can only run its compile-time no-padding check +/// on a concrete type; for the generic `__IncompleteArrayField<T>` it falls back +/// to requiring every field be `Unaligned`, which `PACKED_REGISTRY_ENTRY` +/// does not satisfy. Specializing to a concrete type lets the derive succeed. +#[repr( C )] +#[derive( Debug , Default , FromBytes , Immutable , IntoBytes , KnownLayout , MaybeZeroable )] +pub struct __IncompletePackedRegistryEntry ( + ::core::marker::PhantomData <PACKED_REGISTRY_ENTRY >, + [PACKED_REGISTRY_ENTRY ;0 ], +); +impl __IncompletePackedRegistryEntry { +#[inline] +pub const fn new () ->Self { +__IncompletePackedRegistryEntry (::core::marker::PhantomData , []) + } +#[inline] +pub fn as_ptr (&self) -> *const PACKED_REGISTRY_ENTRY { + selfas *const _ as *const PACKED_REGISTRY_ENTRY + } +#[inline] +pub fn as_mut_ptr (&mut self) -> *mut PACKED_REGISTRY_ENTRY { + selfas *mut _ as *mut PACKED_REGISTRY_ENTRY
drivers/gpu/nova-core/gsp/fw/r570_144.rs line 72
Quoted 10 lines
drivers/gpu/nova-core/gsp/fw/r570_144.rs +10
-0+ } +#[inline] +pub unsafe fn as_slice (&self, len:usize ) -> &[PACKED_REGISTRY_ENTRY ] { + ::core::slice::from_raw_parts (self.as_ptr (), len) + } +#[inline] +pub unsafe fn as_mut_slice (&mut self, len:usize ) -> &mut [PACKED_REGISTRY_ENTRY ] { + ::core::slice::from_raw_parts_mut (self.as_mut_ptr (), len) + } +}
Yikes, this is a one-shot so I guess we can live with that if neeeded.
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs line 4
Quoted 10 lines
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +1
-1@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #[repr( C )] -#[derive( Default )] +#[derive( Default , FromBytes , IntoBytes , Immutable , KnownLayout )]
This is the part my other email [1] was about: are we going, long-term, to replace this with a #[derive(zerocopy_derive::most_traits)]? If the Nova's bindings generator program can take care of producing the correct traits for each generated type then maybe that's even better, but at the same time I am wondering whether the kernel's bindgen invocation isn't going to automatically derive most_traits on all generated types. In this case, we could end up with duplicate implementations.
I think this point needs to be clarified before we can decide when to merge this patch.
[1] https://lore.kernel.org/all/[email protected]/
<...>
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs pub struct PACKED_REGISTRY_ENTRY { · line 903
Quoted 8 lines
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +1
-1#[repr( C )] -#[derive( Debug , Default , MaybeZeroable )] +#[derive( Debug , Default , MaybeZeroable , FromBytes , Immutable , KnownLayout , IntoBytes )] pub struct PACKED_REGISTRY_TABLE {pub size:
u32_,
pub numEntries:u32_,drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +1
-1- pub entries: __IncompleteArrayField<PACKED_REGISTRY_ENTRY>, + pub entries: __IncompletePackedRegistryEntry,
This is part of the generated bindings, right? How does the generator program know it needs to use __IncompletePackedRegistryEntry?
<...>
scripts/Makefile.build $(obj)/%.lst: $(obj)/%.c FORCE · line 322
Quoted 13 lines
scripts/Makefile.build +3
-1@@ -314,10 +314,12 @@ $(obj)/%.lst: $(obj)/%.c FORCE # - Stable since Rust 1.89.0: `feature(generic_arg_infer)`. # - Expected to become stable: `feature(arbitrary_self_types)`. # - To be determined: `feature(used_with_arg)`. +# - Required by nova-core's zerocopy firmware bindings, whose derives emit +# trivial `where` bounds: `feature(trivial_bounds)`. # # Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on # the unstable features in use. -rust_allowed_features := arbitrary_self_types,asm_goto,generic_arg_infer,used_with_arg +rust_allowed_features := arbitrary_self_types,asm_goto,generic_arg_infer,used_with_arg,trivial_bounds
This also might be something that will land soon through the Rust tree; Miguel, do you know what the plan is by any chance?
Re: [PATCH 1/1] nova-core: Update firmware bindings to use zerocopy traits
On 2026-06-29 at 17:36 +1000, Alexandre Courbot <[email protected]> wrote...
Quoted 58 lines · Alistair Popple
Quoted 11 lines
Currently most of nova-core uses unsafe implementations of AsBytes and FromBytes in order to read and write GSP commands using the generated bindings. Whilst this is generally safe in practice there is nothing that actually guarantees or checks this.
This is exactly what the zerocopy library was introduced to do - provide compile-time checks ensuring From/AsBytes is safe. Make use of these checks by converting all our generated bindings to derive the required traits for the zerocopy checks, removing many unsafe implementations in the process.
Quoted 8 lines
Note this does require the use of an unstable feature -
trivial_bounds
- as some bindings end up needing to derivezerocopy::Unaligned. A work-around is also required in the bindings to make some of the __IncompleteArrayField<T> ZSTs monomorphic as the check macro can not prove a generic type is aligned and thus forces T to derivezerocopy::Unaligned, which is not satisfied by all types.Signed-off-by Alistair Popple <[email protected]>I have counted and this removes 28 unsafe statements. :) (it also adds 2 tbf)
Quoted 34 lines
--- drivers/gpu/nova-core/Makefile | 4 + drivers/gpu/nova-core/gsp/cmdq.rs | 21 +- .../gpu/nova-core/gsp/cmdq/continuation.rs | 16 +- drivers/gpu/nova-core/gsp/commands.rs | 19 +- drivers/gpu/nova-core/gsp/fw.rs | 54 +---- drivers/gpu/nova-core/gsp/fw/commands.rs | 50 ++--- drivers/gpu/nova-core/gsp/fw/r570_144.rs | 41 ++++ .../gpu/nova-core/gsp/fw/r570_144/bindings.rs | 185 ++++++++++++------ drivers/gpu/nova-core/gsp/sequencer.rs | 5 +- scripts/Makefile.build | 4 +- 10 files changed, 223 insertions(+), 176 deletions(-)drivers/gpu/nova-core/Makefile +4
-0@@ -1,4 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 +# The GSP firmware bindings derive zerocopy's `IntoBytes` on `#[repr(C)]` +# unions, which is gated behind the `zerocopy_derive_union_into_bytes` cfg. +rustflags-y += --cfg zerocopy_derive_union_into_bytes + obj-$(CONFIG_NOVA_CORE) += nova-core.o nova-core-y := nova_core.odrivers/gpu/nova-core/gsp/cmdq.rs +0
-0@@ -29,6 +29,14 @@ }, };
Quoted 6 lines
drivers/gpu/nova-core/gsp/cmdq.rs line 34
Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs +3
-0+// TODO: Remove `as` once FromBytes is removed completely +use zerocopy::{ +FromBytes as ZCFromBytes ,Since zerocopy's
FromBytesis the one we will keep long-term, should we rather rename the one fromtransmute?
Good idea - it ended up this way because I was going to split the series up to convert one type at a time but ended up squashing it into one big change given the mechanical nature of the conversions.
Quoted 48 lines
<...>
Quoted 47 lines
drivers/gpu/nova-core/gsp/fw/r570_144.rs +35
-0@@ -23,9 +23,50 @@ )]use kernel::ffi;use pin_init::MaybeZeroable ; +use zerocopy_derive::{ +FromBytes , +Immutable , +IntoBytes , +KnownLayout ,// +};include! ("r570_144/bindings.rs" );// SAFETY: This type has a size of zero, so its inclusion into another type should not affect their // ability to implement `Zeroable`. unsafe impl <T > kernel::prelude::Zeroable for __IncompleteArrayField <T > {} + +/// Monomorphic version of [`__IncompleteArrayField<PACKED_REGISTRY_ENTRY>`]. +/// +/// zerocopy's `IntoBytes` derive can only run its compile-time no-padding check +/// on a concrete type; for the generic `__IncompleteArrayField<T>` it falls back +/// to requiring every field be `Unaligned`, which `PACKED_REGISTRY_ENTRY` +/// does not satisfy. Specializing to a concrete type lets the derive succeed. +#[repr( C )] +#[derive( Debug , Default , FromBytes , Immutable , IntoBytes , KnownLayout , MaybeZeroable )] +pub struct __IncompletePackedRegistryEntry ( + ::core::marker::PhantomData <PACKED_REGISTRY_ENTRY >, + [PACKED_REGISTRY_ENTRY ;0 ], +); +impl __IncompletePackedRegistryEntry { +#[inline] +pub const fn new () ->Self { +__IncompletePackedRegistryEntry (::core::marker::PhantomData , []) + } +#[inline] +pub fn as_ptr (&self) -> *const PACKED_REGISTRY_ENTRY { + selfas *const _ as *const PACKED_REGISTRY_ENTRY + } +#[inline] +pub fn as_mut_ptr (&mut self) -> *mut PACKED_REGISTRY_ENTRY { + selfas *mut _ as *mut PACKED_REGISTRY_ENTRY + } +#[inline] +pub unsafe fn as_slice (&self, len:usize ) -> &[PACKED_REGISTRY_ENTRY ] { + ::core::slice::from_raw_parts (self.as_ptr (), len)
Quoted 8 lines
drivers/gpu/nova-core/gsp/fw/r570_144.rs line 72
Quoted 6 lines
drivers/gpu/nova-core/gsp/fw/r570_144.rs +6
-0+ } +#[inline] +pub unsafe fn as_mut_slice (&mut self, len:usize ) -> &mut [PACKED_REGISTRY_ENTRY ] { + ::core::slice::from_raw_parts_mut (self.as_mut_ptr (), len) + } +}Yikes, this is a one-shot so I guess we can live with that if neeeded.
Yes, this is a total hack but I couldn't figure out a better alternative. Given this is the only type that has this problem I figured it was a bearable trade-off to make, but am open to better suggestions if there are any.
Quoted 7 lines
Quoted 7 lines
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +0
-0@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0
Quoted 11 lines
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs line 4
Quoted 3 lines
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +1
-1#[repr( C )] -#[derive( Default )] +#[derive( Default , FromBytes , IntoBytes , Immutable , KnownLayout )] This is the part my other email [1] was about: are we going, long-term, to replace this with a
#[derive(zerocopy_derive::most_traits)]? If the Nova's bindings generator program can take care of producing the correct traits for each generated type then maybe that's even better, but at the same time I am wondering whether the kernel's bindgen invocation isn't going to automatically derivemost_traitson all generated types. In this case, we could end up with duplicate implementations.
If that's coming soon I think it makes sense to wait simply to avoid churn on the generated bindings. I didn't do anything too smart for Nova's binding generator - it basically just derives all these traits for all our bindings. Being able to have it derive most_traits would be cleaner. So happy to rebase once that is merged.
Quoted 6 lines
I think this point needs to be clarified before we can decide when to merge this patch.
[1] https://lore.kernel.org/all/[email protected]/
<...>
Quoted 11 lines
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs pub struct PACKED_REGISTRY_ENTRY { · line 903
Quoted 8 lines
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +1
-1#[repr( C )] -#[derive( Debug , Default , MaybeZeroable )] +#[derive( Debug , Default , MaybeZeroable , FromBytes , Immutable , KnownLayout , IntoBytes )] pub struct PACKED_REGISTRY_TABLE {pub size:
u32_,
pub numEntries:u32_,drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +1
-1- pub entries: __IncompleteArrayField<PACKED_REGISTRY_ENTRY>, + pub entries: __IncompletePackedRegistryEntry,This is part of the generated bindings, right? How does the generator program know it needs to use
__IncompletePackedRegistryEntry?
Maigc[1] ... otherwise known as sed :-)
[1] - https://github.com/apopple-nvidia/nova-gsp-binding-generator/blob/zerocopy/Kbuild#L153
Quoted 17 lines
<...>
scripts/Makefile.build $(obj)/%.lst: $(obj)/%.c FORCE · line 322
Quoted 13 lines
scripts/Makefile.build +3
-1@@ -314,10 +314,12 @@ $(obj)/%.lst: $(obj)/%.c FORCE # - Stable since Rust 1.89.0: `feature(generic_arg_infer)`. # - Expected to become stable: `feature(arbitrary_self_types)`. # - To be determined: `feature(used_with_arg)`. +# - Required by nova-core's zerocopy firmware bindings, whose derives emit +# trivial `where` bounds: `feature(trivial_bounds)`. # # Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on # the unstable features in use. -rust_allowed_features := arbitrary_self_types,asm_goto,generic_arg_infer,used_with_arg +rust_allowed_features := arbitrary_self_types,asm_goto,generic_arg_infer,used_with_arg,trivial_boundsThis also might be something that will land soon through the Rust tree; Miguel, do you know what the plan is by any chance?
Re: [PATCH 1/1] nova-core: Update firmware bindings to use zerocopy traits
In reply to Alexandre CourbotQuoted 6 lines · Alexandre Courbot
This is the part my other email [1] was about: are we going, long-term, to replace this with a
#[derive(zerocopy_derive::most_traits)]? If theThis also might be something that will land soon through the Rust tree; Miguel, do you know what the plan is by any chance?
If you mean:
https://lore.kernel.org/rust-for-linux/[email protected]/
Then yeah :)
Cheers,
Miguel
Alexandre Courbot
maintainerRe: [PATCH 1/1] nova-core: Update firmware bindings to use zerocopy traits
Quoted 5 lines · Miguel Ojeda
Quoted 3 lines · Alexandre Courbot
This is the part my other email [1] was about: are we going, long-term, to replace this with a
#[derive(zerocopy_derive::most_traits)]? If the
Quoted 8 lines
Quoted 2 lines
This also might be something that will land soon through the Rust tree; Miguel, do you know what the plan is by any chance?
If you mean:
https://lore.kernel.org/rust-for-linux/[email protected]/
Then yeah :)
Thanks - also, is there a plan to automatically derive zerocopy_derive::most_traits for generated bindings types, or is that something that Nova should do by itself?
Quoted 5 lines · Alistair Popple
Given the inevitable conflicts that will arise from trying to merge several different series doing bits of the same thing I'm happy if someone wants to take this patch and the others and roll them into a single series. Alternatively I am happy to take that on - hopefully Alex can provide some guidance here for what would be preferred.
Agreed it should be one series. Happy to help or fold in my part -- whatever Alex prefers.
Best regards,
SeungJong
Alexandre Courbot
maintainerQuoted 9 lines · SeungJong Ha
Quoted 5 lines · Alistair Popple
Given the inevitable conflicts that will arise from trying to merge several different series doing bits of the same thing I'm happy if someone wants to take this patch and the others and roll them into a single series. Alternatively I am happy to take that on - hopefully Alex can provide some guidance here for what would be preferred.
Agreed it should be one series. Happy to help or fold in my part -- whatever Alex prefers.
Since there is no particular urgency to switch from transmute to zerocopy, I think it makes sense to merge things in an order that limits churn as much as possible.
This means the conversion should probably be done bottom-to-top, starting with the bindings, then the types that embed them. For the bindings, #[derive(zerocopy_derive::most_traits)] should be landing soon [1] and I would like us to understand first whether that is something we want to use with the bindings; if so, let's wait for it to be available (unless there is a good reason to convert quickly) as each change to bindings.rs is almost 200 LoCs.
Since Alistair maintains the tool generating the bindings [2], I think it would make sense if he takes care of that part (bindings and anything embedding them) as well.
There are also types in nova-core that do not depend on bindings at all; for these I think it is fine to take the patches that have been already submitted if they apply cleanly and build, as these are good opportunities for first contributions. Nicolás was first to send something for vbios/firmware [3] so I'd like to consider his v2 first now that -rc1 is tagged.
For the rest, I'm happy to merge anything that applies - small patches limited to a single module should be easier to process and less likely to conflict.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/apopple-nvidia/nova-gsp-binding-generator/
[3] https://lore.kernel.org/all/[email protected]/
On 2026-06-29 at 17:09 +1000, Alexandre Courbot <[email protected]> wrote...
Quoted 15 lines · SeungJong Ha
Quoted 9 lines · Alistair Popple
Quoted 5 lines
Given the inevitable conflicts that will arise from trying to merge several different series doing bits of the same thing I'm happy if someone wants to take this patch and the others and roll them into a single series. Alternatively I am happy to take that on - hopefully Alex can provide some guidance here for what would be preferred.
Agreed it should be one series. Happy to help or fold in my part -- whatever Alex prefers.
Since there is no particular urgency to switch from transmute to zerocopy, I think it makes sense to merge things in an order that limits churn as much as possible.
Quoted 7 lines
This means the conversion should probably be done bottom-to-top, starting with the bindings, then the types that embed them. For the bindings,
#[derive(zerocopy_derive::most_traits)]should be landing soon [1] and I would like us to understand first whether that is something we want to use with the bindings; if so, let's wait for it to be available (unless there is a good reason to convert quickly) as each change tobindings.rsis almost 200 LoCs.
I think it makes sense to wait, if only to avoid churn on the generated bindings.
Quoted 3 lines
Since Alistair maintains the tool generating the bindings [2], I think it would make sense if he takes care of that part (bindings and anything embedding them) as well.
Agreed.
Quoted 6 lines
There are also types in nova-core that do not depend on bindings at all; for these I think it is fine to take the patches that have been already submitted if they apply cleanly and build, as these are good opportunities for first contributions. Nicolás was first to send something for vbios/firmware [3] so I'd like to consider his v2 first now that
-rc1is tagged.
Yep - given the interest from other contributors I'm going to leave all the non-binding related conversions alone. In other words anything that does not require changes to drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs would be good for others to look at, and thanks for the contributions!
- Alistair
Quoted 7 lines
For the rest, I'm happy to merge anything that applies - small patches limited to a single module should be easier to process and less likely to conflict.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/apopple-nvidia/nova-gsp-binding-generator/
[3] https://lore.kernel.org/all/[email protected]/
Danilo Krummrich
maintainerQuoted 11 lines · Alexandre Courbot
Since there is no particular urgency to switch from transmute to zerocopy, I think it makes sense to merge things in an order that limits churn as much as possible.
This means the conversion should probably be done bottom-to-top, starting with the bindings, then the types that embed them. For the bindings,
#[derive(zerocopy_derive::most_traits)]should be landing soon [1] and I would like us to understand first whether that is something we want to use with the bindings; if so, let's wait for it to be available (unless there is a good reason to convert quickly) as each change tobindings.rsis almost 200 LoCs.
Doing another subsequent conversion to most_traits seems trivial. The upside would be that we could remove the transmute::* types from nova entirely, which also allows us to switch the DMA code to zerocopy.
No need to go for it with might and main, but a minor inconvinience like the above might be worth taking, as switching to zerocopy in core infrastructure can simplify backports.