Skip to contentSkip to conversation

gpu: nova-core: miscellaneous improvements

PATCH Superseded Alexandre Courbot 4 participants 23 messages Dec 16, 2025 – Jan 19

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

v2 cover gpu: nova-core: miscellaneous improvements

A few simple, loosely-related small improvements for nova-core, including reporting unprocessed data in GSP messages, removal of unnecessary code in GSP and the sequencer, and leveraging the Zeroable derive macro and core library's CStr. Probably nothing too controversial.

This series is based on the fixup patch series for this cycle [1].

[1] https://lore.kernel.org/all/[email protected]/

---
Changes in v2:
- Rebase on drm-rust-next.
- Add a patch to reuse previously acquired reference to define in GSP boot sequence.
- Link to v1: https://patch.msgid.link/[email protected]

---
Alexandre Courbot (10):
gpu: nova-core: gsp: warn if data remains after processing a message
gpu: nova-core: gsp: remove unnecessary Display impls
gpu: nova-core: gsp: simplify sequencer opcode parsing
gpu: nova-core: gsp: remove unneeded sequencer trait
gpu: nova-core: gsp: derive Debug on more sequencer types
gpu: nova-core: gsp: derive Zeroable for GspStaticConfigInfo
gpu: nova-core: firmware: fwsec: do not require bound device when unneeded
gpu: nova-core: use core library's CStr instead of kernel one
gpu: nova-core: simplify str_from_null_terminated
gpu: nova-core: gsp: use available device reference

 drivers/gpu/nova-core/firmware.rs        |   2 +-
 drivers/gpu/nova-core/firmware/fwsec.rs  |   7 +--
 drivers/gpu/nova-core/firmware/gsp.rs    |   6 +-
 drivers/gpu/nova-core/gsp/boot.rs        |  32 +++-------
 drivers/gpu/nova-core/gsp/cmdq.rs        |  14 ++++-
 drivers/gpu/nova-core/gsp/fw.rs          | 104 +++----------------------------
 drivers/gpu/nova-core/gsp/fw/commands.rs |  11 ++--
 drivers/gpu/nova-core/gsp/sequencer.rs   |  18 +++---
 drivers/gpu/nova-core/nova_core.rs       |   2 +-
 drivers/gpu/nova-core/util.rs            |  11 +---
 10 files changed, 51 insertions(+), 156 deletions(-)

---
base-commit: 859c55803ab4bfe867be2a5bb116134b7bf50720
change-id: 20251208-nova-misc-1d797b5d64f2

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
author maintainer
#1 lore raw

PATCH v2 1/10 gpu: nova-core: gsp: warn if data remains after processing a message

Not processing the whole data from a received message is a strong indicator of a bug - emit a warning when such cases are detected.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gsp/cmdq.rs +11 -1
index 7985a0b3f769..f0b7ac1ee759 100644
@@ -661,7 +661,17 @@ pub(crate) fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Resul
             let (cmd, contents_1) = M::Message::from_bytes_prefix(message.contents.0).ok_or(EIO)?;
             let mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
 
-            M::read(cmd, &mut sbuffer).map_err(|e| e.into())
+            let res = M::read(cmd, &mut sbuffer).map_err(|e| e.into());
+
+            if res.is_ok() && !sbuffer.is_empty() {
+                dev_warn!(
+                    &self.dev,
+                    "GSP message {:?} has unprocessed data\n",
+                    function
+                );
+            }
+
+            res
         } else {
             Err(ERANGE)
         };
Signature · 1 line
2.52.0
reviewer
#2 lore raw

Re: [PATCH v2 01/10] gpu: nova-core: gsp: warn if data remains after processing a message

Quoted 17 lines · Alexandre Courbot

Not processing the whole data from a received message is a strong indicator of a bug - emit a warning when such cases are detected.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gsp/cmdq.rs +0 -0
index 7985a0b3f769..f0b7ac1ee759 100644
@@ -661,7 +661,17 @@ pub(crate) fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Resul
             let (cmd, contents_1) = M::Message::from_bytes_prefix(message.contents.0).ok_or(EIO)?;
             let mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
 

drivers/gpu/nova-core/gsp/cmdq.rs pub(crate) fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Resul · line 672

Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +9 -1
-            M::read(cmd, &mut sbuffer).map_err(|e| e.into())
+            let res = M::read(cmd, &mut sbuffer).map_err(|e| e.into());
+
+            if res.is_ok() && !sbuffer.is_empty() {
+                dev_warn!(
+                    &self.dev,
+                    "GSP message {:?} has unprocessed data\n",
+                    function
+                );
+            }

You can also do

    M::read(cmd, &mut sbuffer).map_err(|e| e.info).inspect(|_| {
        if !sbuffer.is_empty() {
            // ...
        }
    })

This does feel like something that would look more elegant with try blocks... I hope it'll be stable some day.

Best,
Gary

Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs +2 -0
+
+            res
         } else {
             Err(ERANGE)
         };
author maintainer
#3 lore raw

PATCH v2 2/10 gpu: nova-core: gsp: remove unnecessary Display impls

In reply to Alexandre Courbot

We only ever display these in debug context, for which the automatically derived Debug impls work just fine - so use them and remove these boilerplate-looking implementations.

---
 drivers/gpu/nova-core/gsp/cmdq.rs |  2 +-
 drivers/gpu/nova-core/gsp/fw.rs   | 54 ---------------------------------------
 2 files changed, 1 insertion(+), 55 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
index f0b7ac1ee759..4dde9cc4e3c7 100644
@@ -531,7 +531,7 @@ pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result
 
         dev_dbg!(
             &self.dev,
-            "GSP RPC: send: seq# {}, function={}, length=0x{:x}\n",
+            "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
             self.seq,
             M::FUNCTION,
             dst.header.length(),
drivers/gpu/nova-core/gsp/fw.rs +0 -54

72 lines not on this page · raw message

Signature · 1 line
2.52.0
reviewer Reviewed-by on v2 2/10
#4 lore raw

Re: [PATCH v2 02/10] gpu: nova-core: gsp: remove unnecessary Display impls

Quoted 7 lines · Alexandre Courbot

We only ever display these in debug context, for which the automatically derived Debug impls work just fine - so use them and remove these boilerplate-looking implementations.

Quoted 4 lines
---
 drivers/gpu/nova-core/gsp/cmdq.rs |  2 +-
 drivers/gpu/nova-core/gsp/fw.rs   | 54 ---------------------------------------
 2 files changed, 1 insertion(+), 55 deletions(-)
author maintainer
#5 lore raw

PATCH v2 3/10 gpu: nova-core: gsp: simplify sequencer opcode parsing

In reply to Alexandre Courbot

The opcodes are already the right type in the C union, so we can use them directly instead of converting them to a byte stream and back again using FromBytes.

---
 drivers/gpu/nova-core/gsp/fw.rs | 40 +++++-----------------------------------
 1 file changed, 5 insertions(+), 35 deletions(-)
drivers/gpu/nova-core/gsp/fw.rs +5 -35
index de251a143f7b..1b0fcbdc77ba 100644
@@ -472,13 +472,7 @@ pub(crate) fn reg_write_payload(&self) -> Result<RegWritePayload> {
             return Err(EINVAL);
         }
         // SAFETY: Opcode is verified to be `RegWrite`, so union contains valid `RegWritePayload`.
-        let payload_bytes = unsafe {
-            core::slice::from_raw_parts(
-                core::ptr::addr_of!(self.0.payload.regWrite).cast::<u8>(),
-                core::mem::size_of::<RegWritePayload>(),
-            )
-        };
-        Ok(*RegWritePayload::from_bytes(payload_bytes).ok_or(EINVAL)?)
+        Ok(RegWritePayload(unsafe { self.0.payload.regWrite }))
     }
 
     /// Returns the register modify payload by value.
@@ -489,13 +483,7 @@ pub(crate) fn reg_modify_payload(&self) -> Result<RegModifyPayload> {
             return Err(EINVAL);
         }
         // SAFETY: Opcode is verified to be `RegModify`, so union contains valid `RegModifyPayload`.
-        let payload_bytes = unsafe {
-            core::slice::from_raw_parts(
-                core::ptr::addr_of!(self.0.payload.regModify).cast::<u8>(),
-                core::mem::size_of::<RegModifyPayload>(),
-            )
-        };
-        Ok(*RegModifyPayload::from_bytes(payload_bytes).ok_or(EINVAL)?)
+        Ok(RegModifyPayload(unsafe { self.0.payload.regModify }))
     }
 
     /// Returns the register poll payload by value.
@@ -506,13 +494,7 @@ pub(crate) fn reg_poll_payload(&self) -> Result<RegPollPayload> {
             return Err(EINVAL);
         }
         // SAFETY: Opcode is verified to be `RegPoll`, so union contains valid `RegPollPayload`.
-        let payload_bytes = unsafe {
-            core::slice::from_raw_parts(
-                core::ptr::addr_of!(self.0.payload.regPoll).cast::<u8>(),
-                core::mem::size_of::<RegPollPayload>(),
-            )
-        };
-        Ok(*RegPollPayload::from_bytes(payload_bytes).ok_or(EINVAL)?)
+        Ok(RegPollPayload(unsafe { self.0.payload.regPoll }))
     }
 
     /// Returns the delay payload by value.
@@ -523,13 +505,7 @@ pub(crate) fn delay_us_payload(&self) -> Result<DelayUsPayload> {
             return Err(EINVAL);
         }
         // SAFETY: Opcode is verified to be `DelayUs`, so union contains valid `DelayUsPayload`.
-        let payload_bytes = unsafe {
-            core::slice::from_raw_parts(
-                core::ptr::addr_of!(self.0.payload.delayUs).cast::<u8>(),
-                core::mem::size_of::<DelayUsPayload>(),
-            )
-        };
-        Ok(*DelayUsPayload::from_bytes(payload_bytes).ok_or(EINVAL)?)
+        Ok(DelayUsPayload(unsafe { self.0.payload.delayUs }))
     }
 
     /// Returns the register store payload by value.
@@ -540,13 +516,7 @@ pub(crate) fn reg_store_payload(&self) -> Result<RegStorePayload> {
             return Err(EINVAL);
         }
         // SAFETY: Opcode is verified to be `RegStore`, so union contains valid `RegStorePayload`.
-        let payload_bytes = unsafe {
-            core::slice::from_raw_parts(
-                core::ptr::addr_of!(self.0.payload.regStore).cast::<u8>(),
-                core::mem::size_of::<RegStorePayload>(),
-            )
-        };
-        Ok(*RegStorePayload::from_bytes(payload_bytes).ok_or(EINVAL)?)
+        Ok(RegStorePayload(unsafe { self.0.payload.regStore }))
     }
 }
 
Signature · 1 line
2.52.0
reviewer Reviewed-by on v2 3/10
#6 lore raw

Re: [PATCH v2 03/10] gpu: nova-core: gsp: simplify sequencer opcode parsing

Quoted 6 lines · Alexandre Courbot

The opcodes are already the right type in the C union, so we can use them directly instead of converting them to a byte stream and back again using FromBytes.

Quoted 3 lines
---
 drivers/gpu/nova-core/gsp/fw.rs | 40 +++++-----------------------------------
 1 file changed, 5 insertions(+), 35 deletions(-)
author maintainer
#7 lore raw

PATCH v2 4/10 gpu: nova-core: gsp: remove unneeded sequencer trait

In reply to Alexandre Courbot

The GspSeqCmdRunner trait is never used as we never call the run methods from generic code. Remove it.

---
 drivers/gpu/nova-core/gsp/sequencer.rs | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/gpu/nova-core/gsp/sequencer.rs +6 -11
index 2d0369c49092..4efa048b9d93 100644
@@ -147,12 +147,7 @@ pub(crate) struct GspSequencer<'a> {
     dev: ARef<device::Device>,
 }
 
-/// Trait for running sequencer commands.
-pub(crate) trait GspSeqCmdRunner {
-    fn run(&self, sequencer: &GspSequencer<'_>) -> Result;
-}
-
-impl GspSeqCmdRunner for fw::RegWritePayload {
+impl fw::RegWritePayload {
     fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
         let addr = usize::from_safe_cast(self.addr());
 
@@ -160,7 +155,7 @@ fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
     }
 }
 
-impl GspSeqCmdRunner for fw::RegModifyPayload {
+impl fw::RegModifyPayload {
     fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
         let addr = usize::from_safe_cast(self.addr());
 
@@ -172,7 +167,7 @@ fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
     }
 }
 
-impl GspSeqCmdRunner for fw::RegPollPayload {
+impl fw::RegPollPayload {
     fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
         let addr = usize::from_safe_cast(self.addr());
 
@@ -197,14 +192,14 @@ fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
     }
 }
 
-impl GspSeqCmdRunner for fw::DelayUsPayload {
+impl fw::DelayUsPayload {
     fn run(&self, _sequencer: &GspSequencer<'_>) -> Result {
         fsleep(Delta::from_micros(i64::from(self.val())));
         Ok(())
     }
 }
 
-impl GspSeqCmdRunner for fw::RegStorePayload {
+impl fw::RegStorePayload {
     fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
         let addr = usize::from_safe_cast(self.addr());
 
@@ -212,7 +207,7 @@ fn run(&self, sequencer: &GspSequencer<'_>) -> Result {
     }
 }
 
-impl GspSeqCmdRunner for GspSeqCmd {
+impl GspSeqCmd {
     fn run(&self, seq: &GspSequencer<'_>) -> Result {
         match self {
             GspSeqCmd::RegWrite(cmd) => cmd.run(seq),
Signature · 1 line
2.52.0
reviewer Reviewed-by on v2 4/10
#8 lore raw

Re: [PATCH v2 04/10] gpu: nova-core: gsp: remove unneeded sequencer trait

Quoted 5 lines · Alexandre Courbot

The GspSeqCmdRunner trait is never used as we never call the run methods from generic code. Remove it.

Quoted 3 lines
---
 drivers/gpu/nova-core/gsp/sequencer.rs | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
author maintainer
#9 lore raw

PATCH v2 5/10 gpu: nova-core: gsp: derive `Debug` on more sequencer types

In reply to Alexandre Courbot

Being able to print these is useful when debugging the sequencer.

---
 drivers/gpu/nova-core/gsp/fw.rs        | 10 +++++-----
 drivers/gpu/nova-core/gsp/sequencer.rs |  1 +
 2 files changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/gsp/fw.rs +5 -5
index 1b0fcbdc77ba..09549f7db52d 100644
@@ -330,7 +330,7 @@ fn from(value: SeqBufOpcode) -> Self {
 
 /// Wrapper for GSP sequencer register write payload.
 #[repr(transparent)]
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub(crate) struct RegWritePayload(bindings::GSP_SEQ_BUF_PAYLOAD_REG_WRITE);
 
 impl RegWritePayload {
@@ -353,7 +353,7 @@ unsafe impl AsBytes for RegWritePayload {}
 
 /// Wrapper for GSP sequencer register modify payload.
 #[repr(transparent)]
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub(crate) struct RegModifyPayload(bindings::GSP_SEQ_BUF_PAYLOAD_REG_MODIFY);
 
 impl RegModifyPayload {
@@ -381,7 +381,7 @@ unsafe impl AsBytes for RegModifyPayload {}
 
 /// Wrapper for GSP sequencer register poll payload.
 #[repr(transparent)]
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub(crate) struct RegPollPayload(bindings::GSP_SEQ_BUF_PAYLOAD_REG_POLL);
 
 impl RegPollPayload {
@@ -414,7 +414,7 @@ unsafe impl AsBytes for RegPollPayload {}
 
 /// Wrapper for GSP sequencer delay payload.
 #[repr(transparent)]
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub(crate) struct DelayUsPayload(bindings::GSP_SEQ_BUF_PAYLOAD_DELAY_US);
 
 impl DelayUsPayload {
@@ -432,7 +432,7 @@ unsafe impl AsBytes for DelayUsPayload {}
 
 /// Wrapper for GSP sequencer register store payload.
 #[repr(transparent)]
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub(crate) struct RegStorePayload(bindings::GSP_SEQ_BUF_PAYLOAD_REG_STORE);
 
 impl RegStorePayload {
drivers/gpu/nova-core/gsp/sequencer.rs +1 -0
index 4efa048b9d93..5eead7ad4cbd 100644
@@ -70,6 +70,7 @@ fn read(
 /// GSP Sequencer Command types with payload data.
 /// Commands have an opcode and an opcode-dependent struct.
 #[allow(clippy::enum_variant_names)]
+#[derive(Debug)]
 pub(crate) enum GspSeqCmd {
     RegWrite(fw::RegWritePayload),
     RegModify(fw::RegModifyPayload),
Signature · 1 line
2.52.0
reviewer Reviewed-by on v2 5/10
#10 lore raw

Re: [PATCH v2 05/10] gpu: nova-core: gsp: derive `Debug` on more sequencer types

Quoted 4 lines · Alexandre Courbot

Being able to print these is useful when debugging the sequencer.

Quoted 4 lines
---
 drivers/gpu/nova-core/gsp/fw.rs        | 10 +++++-----
 drivers/gpu/nova-core/gsp/sequencer.rs |  1 +
 2 files changed, 6 insertions(+), 5 deletions(-)
author maintainer
#11 lore raw

PATCH v2 6/10 gpu: nova-core: gsp: derive Zeroable for GspStaticConfigInfo

In reply to Alexandre Courbot

We can derive Zeroable automatically instead of implementing it ourselves if we convert it from a tuple struct into a regular one. This removes an unsafe statement.

---
 drivers/gpu/nova-core/gsp/fw/commands.rs | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/gpu/nova-core/gsp/fw/commands.rs +5 -6
index 21be44199693..85465521de32 100644
@@ -107,12 +107,15 @@ unsafe impl FromBytes for PackedRegistryTable {}
 
 /// Payload of the `GetGspStaticInfo` command and message.
 #[repr(transparent)]
-pub(crate) struct GspStaticConfigInfo(bindings::GspStaticConfigInfo_t);
+#[derive(Zeroable)]
+pub(crate) struct GspStaticConfigInfo {
+    inner: bindings::GspStaticConfigInfo_t,
+}
 
 impl GspStaticConfigInfo {
     /// Returns a bytes array containing the (hopefully) zero-terminated name of this GPU.
     pub(crate) fn gpu_name_str(&self) -> [u8; 64] {
-        self.0.gpuNameString
+        self.inner.gpuNameString
     }
 }
 
@@ -122,7 +125,3 @@ unsafe impl AsBytes for GspStaticConfigInfo {}
 // SAFETY: This struct only contains integer types for which all bit patterns
 // are valid.
 unsafe impl FromBytes for GspStaticConfigInfo {}
-
-// SAFETY: This struct only contains integer types and fixed-size arrays for which
-// all bit patterns are valid.
-unsafe impl Zeroable for GspStaticConfigInfo {}
Signature · 1 line
2.52.0
reviewer
#12 lore raw

Re: [PATCH v2 06/10] gpu: nova-core: gsp: derive Zeroable for GspStaticConfigInfo

Quoted 3 lines · Alexandre Courbot

We can derive Zeroable automatically instead of implementing it ourselves if we convert it from a tuple struct into a regular one. This removes an unsafe statement.

With latest pin-init changes you should be able to derive Zeroable on tuple structs now, no need to convert.

Best,
Gary

Quoted 37 lines
---
 drivers/gpu/nova-core/gsp/fw/commands.rs | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/gpu/nova-core/gsp/fw/commands.rs +5 -6
index 21be44199693..85465521de32 100644
@@ -107,12 +107,15 @@ unsafe impl FromBytes for PackedRegistryTable {}
 
 /// Payload of the `GetGspStaticInfo` command and message.
 #[repr(transparent)]
-pub(crate) struct GspStaticConfigInfo(bindings::GspStaticConfigInfo_t);
+#[derive(Zeroable)]
+pub(crate) struct GspStaticConfigInfo {
+    inner: bindings::GspStaticConfigInfo_t,
+}
 
 impl GspStaticConfigInfo {
     /// Returns a bytes array containing the (hopefully) zero-terminated name of this GPU.
     pub(crate) fn gpu_name_str(&self) -> [u8; 64] {
-        self.0.gpuNameString
+        self.inner.gpuNameString
     }
 }
 
@@ -122,7 +125,3 @@ unsafe impl AsBytes for GspStaticConfigInfo {}
 // SAFETY: This struct only contains integer types for which all bit patterns
 // are valid.
 unsafe impl FromBytes for GspStaticConfigInfo {}
-
-// SAFETY: This struct only contains integer types and fixed-size arrays for which
-// all bit patterns are valid.
-unsafe impl Zeroable for GspStaticConfigInfo {}
author maintainer
#13 lore raw

PATCH v2 7/10 gpu: nova-core: firmware: fwsec: do not require bound device when unneeded

In reply to Alexandre Courbot

run doesn't require a bound device as its argument.

---
 drivers/gpu/nova-core/firmware/fwsec.rs | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/gpu/nova-core/firmware/fwsec.rs +1 -6
index b28e34d279f4..b98b1286dc94 100644
@@ -412,12 +412,7 @@ pub(crate) fn new(
     }
 
     /// Loads the FWSEC firmware into `falcon` and execute it.
-    pub(crate) fn run(
-        &self,
-        dev: &Device<device::Bound>,
-        falcon: &Falcon<Gsp>,
-        bar: &Bar0,
-    ) -> Result<()> {
+    pub(crate) fn run(&self, dev: &Device, falcon: &Falcon<Gsp>, bar: &Bar0) -> Result<()> {
         // Reset falcon, load the firmware, and run it.
         falcon
             .reset(bar)
Signature · 1 line
2.52.0
maintainer
#14 lore raw

Re: [PATCH v2 07/10] gpu: nova-core: firmware: fwsec: do not require bound device when unneeded

drivers/gpu/nova-core/firmware/fwsec.rs pub(crate) fn new( · line 419

Quoted 6 lines · Alexandre Courbot
     /// Loads the FWSEC firmware into `falcon` and execute it.
drivers/gpu/nova-core/firmware/fwsec.rs +0 -5
-    pub(crate) fn run(
-        &self,
-        dev: &Device<device::Bound>,
-        falcon: &Falcon<Gsp>,
-        bar: &Bar0,

Hm..the method also takes a &pci::Bar, so its either called from a bound context or within a Revocable critical section.

Leaving the other argument as &Device<Bound> makes it obvious which one it is (and should be).

Quoted 8 lines
drivers/gpu/nova-core/firmware/fwsec.rs +1 -1
-    ) -> Result<()> {
+    pub(crate) fn run(&self, dev: &Device, falcon: &Falcon<Gsp>, bar: &Bar0) -> Result<()> {
         // Reset falcon, load the firmware, and run it.
         falcon
             .reset(bar)
Signature · 2 lines
-- 
2.52.0
author maintainer
#15 lore raw

PATCH v2 8/10 gpu: nova-core: use core library's CStr instead of kernel one

In reply to Alexandre Courbot

The kernel's own CStr type has been replaced by the one in the core library, and is now an alias to the latter. Change our imports to directly reference the actual type.

---
 drivers/gpu/nova-core/firmware.rs     | 2 +-
 drivers/gpu/nova-core/firmware/gsp.rs | 6 ++++--
 drivers/gpu/nova-core/nova_core.rs    | 2 +-
 drivers/gpu/nova-core/util.rs         | 4 ++--
 4 files changed, 8 insertions(+), 6 deletions(-)
drivers/gpu/nova-core/firmware.rs +1 -1
index 2d2008b33fb4..672f6cd24d4b 100644
@@ -229,7 +229,7 @@ const fn make_entry_chipset(self, chipset: &str) -> Self {
     }
 
     pub(crate) const fn create(
-        module_name: &'static kernel::str::CStr,
+        module_name: &'static core::ffi::CStr,
     ) -> firmware::ModInfoBuilder<N> {
         let mut this = Self(firmware::ModInfoBuilder::new(module_name));
         let mut i = 0;
drivers/gpu/nova-core/firmware/gsp.rs +4 -2
index 0549805282ab..53fdbf1de27e 100644
@@ -34,10 +34,12 @@
 /// that scheme before nova-core becomes stable, which means this module will eventually be
 /// removed.
 mod elf {
-    use core::mem::size_of;
+    use core::{
+        ffi::CStr,
+        mem::size_of, //
+    };
 
     use kernel::bindings;
-    use kernel::str::CStr;
     use kernel::transmute::FromBytes;
 
     /// Newtype to provide a [`FromBytes`] implementation.
drivers/gpu/nova-core/nova_core.rs +1 -1
index b98a1c03f13d..3c26cf0b7c6e 100644
@@ -19,7 +19,7 @@
 mod util;
 mod vbios;
 
-pub(crate) const MODULE_NAME: &kernel::str::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
+pub(crate) const MODULE_NAME: &core::ffi::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
 
 kernel::module_pci_driver! {
     type: driver::NovaCore,
drivers/gpu/nova-core/util.rs +2 -2
index 4b503249a3ef..8b2a4b99c55b 100644
@@ -3,10 +3,10 @@
 /// Converts a null-terminated byte slice to a string, or `None` if the array does not
 /// contains any null byte or contains invalid characters.
 ///
-/// Contrary to [`kernel::str::CStr::from_bytes_with_nul`], the null byte can be anywhere in the
+/// Contrary to [`core::ffi::CStr::from_bytes_with_nul`], the null byte can be anywhere in the
 /// slice, and not only in the last position.
 pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
-    use kernel::str::CStr;
+    use core::ffi::CStr;
 
     bytes
         .iter()
Signature · 1 line
2.52.0
reviewer Reviewed-by on v2 8/10
#16 lore raw

Re: [PATCH v2 08/10] gpu: nova-core: use core library's CStr instead of kernel one

Quoted 6 lines · Alexandre Courbot

The kernel's own CStr type has been replaced by the one in the core library, and is now an alias to the latter. Change our imports to directly reference the actual type.

Quoted 6 lines
---
 drivers/gpu/nova-core/firmware.rs     | 2 +-
 drivers/gpu/nova-core/firmware/gsp.rs | 6 ++++--
 drivers/gpu/nova-core/nova_core.rs    | 2 +-
 drivers/gpu/nova-core/util.rs         | 4 ++--
 4 files changed, 8 insertions(+), 6 deletions(-)
author maintainer
#17 lore raw

PATCH v2 9/10 gpu: nova-core: simplify str_from_null_terminated

In reply to Alexandre Courbot

The core library's CStr has a from_bytes_until_nul method that we can leverage to simplify this function.

---
 drivers/gpu/nova-core/util.rs | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/nova-core/util.rs +2 -7
index 8b2a4b99c55b..2cccbce78c14 100644
@@ -2,15 +2,10 @@
 
 /// Converts a null-terminated byte slice to a string, or `None` if the array does not
 /// contains any null byte or contains invalid characters.
-///
-/// Contrary to [`core::ffi::CStr::from_bytes_with_nul`], the null byte can be anywhere in the
-/// slice, and not only in the last position.
 pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
     use core::ffi::CStr;
 
-    bytes
-        .iter()
-        .position(|&b| b == 0)
-        .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok())
+    CStr::from_bytes_until_nul(bytes)
+        .ok()
         .and_then(|cstr| cstr.to_str().ok())
 }
Signature · 1 line
2.52.0
#18 lore raw

Re: [PATCH v2 09/10] gpu: nova-core: simplify str_from_null_terminated

Quoted 19 lines · Alexandre Courbot

The core library's CStr has a from_bytes_until_nul method that we can leverage to simplify this function.

---
 drivers/gpu/nova-core/util.rs | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/nova-core/util.rs +0 -2
index 8b2a4b99c55b..2cccbce78c14 100644
@@ -2,15 +2,10 @@
 
 /// Converts a null-terminated byte slice to a string, or `None` if the array does not
 /// contains any null byte or contains invalid characters.
-///
-/// Contrary to [`core::ffi::CStr::from_bytes_with_nul`], the null byte can be anywhere in the

drivers/gpu/nova-core/util.rs line 9

Quoted 10 lines
drivers/gpu/nova-core/util.rs +2 -5
-/// slice, and not only in the last position.
 pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
     use core::ffi::CStr;
 
-    bytes
-        .iter()
-        .position(|&b| b == 0)
-        .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok())
+    CStr::from_bytes_until_nul(bytes)
+        .ok()

I guess I should have reviewed this patch, before creating my version of this. I went so far as to delete this file entirely, see if you prefer that, it's otherwise the same core idea, but with more cleanup. [1]

[1] https://lore.kernel.org/[email protected]

drivers/gpu/nova-core/util.rs line 11

Quoted 3 lines
drivers/gpu/nova-core/util.rs +0 -0
         .and_then(|cstr| cstr.to_str().ok())
 }

thanks,

Signature · 1 line
John Hubbard
maintainer
#19 lore raw

Re: [PATCH v2 09/10] gpu: nova-core: simplify str_from_null_terminated

Quoted 35 lines · John Hubbard
Quoted 19 lines · Alexandre Courbot

The core library's CStr has a from_bytes_until_nul method that we can leverage to simplify this function.

---
 drivers/gpu/nova-core/util.rs | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/nova-core/util.rs +0 -2
index 8b2a4b99c55b..2cccbce78c14 100644
@@ -2,15 +2,10 @@
 
 /// Converts a null-terminated byte slice to a string, or `None` if the array does not
 /// contains any null byte or contains invalid characters.
-///
-/// Contrary to [`core::ffi::CStr::from_bytes_with_nul`], the null byte can be anywhere in the

drivers/gpu/nova-core/util.rs line 9

Quoted 10 lines
drivers/gpu/nova-core/util.rs +2 -5
-/// slice, and not only in the last position.
 pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
     use core::ffi::CStr;
 
-    bytes
-        .iter()
-        .position(|&b| b == 0)
-        .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok())
+    CStr::from_bytes_until_nul(bytes)
+        .ok()

I guess I should have reviewed this patch, before creating my version of this. I went so far as to delete this file entirely, see if you prefer that, it's otherwise the same core idea, but with more cleanup. [1]

Quoted 1 line

[1] https://lore.kernel.org/[email protected]

Yes, let's remove str_from_null_terminated() entirely.

Quoted 1 line

drivers/gpu/nova-core/util.rs line 10

Quoted 1 line
drivers/gpu/nova-core/util.rs +0 -0
         .and_then(|cstr| cstr.to_str().ok())

Additionally, why do we return an Option here? While an error can only ever happen if the given slice does not contain any NULL byte, I don't see why we discard the error code.

author maintainer
#20 lore raw

Re: [PATCH v2 09/10] gpu: nova-core: simplify str_from_null_terminated

Quoted 36 lines · Danilo Krummrich
Quoted 35 lines · John Hubbard
Quoted 19 lines · Alexandre Courbot

The core library's CStr has a from_bytes_until_nul method that we can leverage to simplify this function.

---
 drivers/gpu/nova-core/util.rs | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/gpu/nova-core/util.rs +0 -2
index 8b2a4b99c55b..2cccbce78c14 100644
@@ -2,15 +2,10 @@
 
 /// Converts a null-terminated byte slice to a string, or `None` if the array does not
 /// contains any null byte or contains invalid characters.
-///
-/// Contrary to [`core::ffi::CStr::from_bytes_with_nul`], the null byte can be anywhere in the

drivers/gpu/nova-core/util.rs line 9

Quoted 10 lines
drivers/gpu/nova-core/util.rs +2 -5
-/// slice, and not only in the last position.
 pub(crate) fn str_from_null_terminated(bytes: &[u8]) -> Option<&str> {
     use core::ffi::CStr;
 
-    bytes
-        .iter()
-        .position(|&b| b == 0)
-        .and_then(|null_pos| CStr::from_bytes_with_nul(&bytes[..=null_pos]).ok())
+    CStr::from_bytes_until_nul(bytes)
+        .ok()

I guess I should have reviewed this patch, before creating my version of this. I went so far as to delete this file entirely, see if you prefer that, it's otherwise the same core idea, but with more cleanup. [1]

Quoted 3 lines
Quoted 1 line

[1] https://lore.kernel.org/[email protected]

Yes, let's remove str_from_null_terminated() entirely.

Removing that method is perfectly fine IMHO, it was only using in a couple of places and is easily emulated.

Quoted 6 lines
Quoted 2 lines

drivers/gpu/nova-core/util.rs line 10

Quoted 2 lines
drivers/gpu/nova-core/util.rs +0 -0
         .and_then(|cstr| cstr.to_str().ok())

Additionally, why do we return an Option here? While an error can only ever happen if the given slice does not contain any NULL byte, I don't see why we discard the error code.

I guess I didn't want to change the function's prototype, but yeah this is sloppy and another good reason to get rid of it.

I'll respin the series once the patches in -fixes are visible in -next.

author maintainer
#21 lore raw

PATCH v2 10/10 gpu: nova-core: gsp: use available device reference

In reply to Alexandre Courbot

We already have a regular reference to the Device, so we don't need to repeatedly acquire one in this method.

---
 drivers/gpu/nova-core/gsp/boot.rs | 32 +++++++-------------------------
 1 file changed, 7 insertions(+), 25 deletions(-)
drivers/gpu/nova-core/gsp/boot.rs +7 -25
index 54937606b5b0..ca7efdaa753b 100644
@@ -173,15 +173,10 @@ pub(crate) fn boot(
             Some(libos_handle as u32),
             Some((libos_handle >> 32) as u32),
         )?;
-        dev_dbg!(
-            pdev.as_ref(),
-            "GSP MBOX0: {:#x}, MBOX1: {:#x}\n",
-            mbox0,
-            mbox1
-        );
+        dev_dbg!(dev, "GSP MBOX0: {:#x}, MBOX1: {:#x}\n", mbox0, mbox1);
 
         dev_dbg!(
-            pdev.as_ref(),
+            dev,
             "Using SEC2 to load and run the booter_load firmware...\n"
         );
 
@@ -193,19 +188,10 @@ pub(crate) fn boot(
             Some(wpr_handle as u32),
             Some((wpr_handle >> 32) as u32),
         )?;
-        dev_dbg!(
-            pdev.as_ref(),
-            "SEC2 MBOX0: {:#x}, MBOX1{:#x}\n",
-            mbox0,
-            mbox1
-        );
+        dev_dbg!(dev, "SEC2 MBOX0: {:#x}, MBOX1{:#x}\n", mbox0, mbox1);
 
         if mbox0 != 0 {
-            dev_err!(
-                pdev.as_ref(),
-                "Booter-load failed with error {:#x}\n",
-                mbox0
-            );
+            dev_err!(dev, "Booter-load failed with error {:#x}\n", mbox0);
             return Err(ENODEV);
         }
 
@@ -219,11 +205,7 @@ pub(crate) fn boot(
             Delta::from_secs(5),
         )?;
 
-        dev_dbg!(
-            pdev.as_ref(),
-            "RISC-V active? {}\n",
-            gsp_falcon.is_riscv_active(bar),
-        );
+        dev_dbg!(dev, "RISC-V active? {}\n", gsp_falcon.is_riscv_active(bar),);
 
         // Create and run the GSP sequencer.
         let seq_params = GspSequencerParams {
@@ -231,7 +213,7 @@ pub(crate) fn boot(
             libos_dma_handle: libos_handle,
             gsp_falcon,
             sec2_falcon,
-            dev: pdev.as_ref().into(),
+            dev: dev.into(),
             bar,
         };
         GspSequencer::run(&mut self.cmdq, seq_params)?;
@@ -242,7 +224,7 @@ pub(crate) fn boot(
         // Obtain and display basic GPU information.
         let info = commands::get_gsp_info(&mut self.cmdq, bar)?;
         dev_info!(
-            pdev.as_ref(),
+            dev,
             "GPU name: {}\n",
             info.gpu_name().unwrap_or("invalid GPU name")
         );
Signature · 1 line
2.52.0
reviewer Reviewed-by on v2 10/10
#22 lore raw

Re: [PATCH v2 10/10] gpu: nova-core: gsp: use available device reference

Quoted 4 lines · Alexandre Courbot

We already have a regular reference to the Device, so we don't need to repeatedly acquire one in this method.

Quoted 3 lines
---
 drivers/gpu/nova-core/gsp/boot.rs | 32 +++++++-------------------------
 1 file changed, 7 insertions(+), 25 deletions(-)