Skip to contentSkip to conversation

gpu: nova-core: gsp: prepare the command queue for r000 dual-message types

PATCH Superseded Alexandre Courbot 3 participants 22 messages Sep 30 – Oct 9

Subsystems (5)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

v3 cover gpu: nova-core: gsp: prepare the command queue for r000 dual-message types

The upcoming r000 firmware support [1] introduces a new type of command named GMC alongside the existing and already supported RPC command type.

Since RPC was the only command type supported so far, it was embedded into the lowest command queue level: the message header actually includes the RPC header and the code considers them as one.

This makes the r000/GMC support difficult to land without a refactor, with the result of transport layer code being duplicated between the two message types, and no clear separation between the RPC and GMC code which are mixed together in the command queue. Merging the code that way would introduce quite some technical debt.

Thus, this series prepares a proper landing ground for a new message type by extracting all the RPC layer code and moving it into its own sub-modules, making the cmdq module completely agnostic of the type of message it transports. By the end of the series, the only mention of RPC in cmdq.rs is for the rpc sub-module.

With the transport and message layers properly separated, support for GMC can be added into sibling modules of rpc, and the transport layer can be converted once when doing the switch to r000.

This moves quite a bit of code around, but most of it is moved verbatim. No significant functional change is intended.

For the r000 series, this essentially means the following:

- Its patch 7 making allocate_command generic can be dropped as the transport layer makes no assumption about the message type.
- Patches adding GMC support will do it in dedicated gmc sub-modules, following the model set by RPC. These gmc sub-modules will start as dead_code as they are being built.
- The "switch to r000" patch will essentially operate on the transport layer level, i.e. cmdq.rs.
- There should be no need for extra post-switch RPC fixing, as the transport layer code will already be shared with GMC.

Overall, this should make the r000 series much easier to review and eventually land, while not creating new technical debt.

This series is based on drm-rust-next.

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

---
Changes in v3:
- Use dedicated RpcMessage type to distinguish parsed RPC message from the transport-level GspMessage.
- Introduce traits for sending and receiving elements on the command queue (thanks Eliot!).
- Split unsafe blocks, drop unused impls, and other fixes on patch 2. (thanks Eliot!)
- Squash the two move commits into a single one.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Fix checksum on receive incorrectly using the whole slots allocated to the message instead of its actual length. (Sashiko)
- Drop patch 1 to avoid having to validate the elemCount header member against edge cases. (Sashiko)
- Fix incorrect layer separation in receive path, where the CPU read pointer was advanced in the message layer.
- Fix doclinks pointing to old function names. (Sashiko)
- Link to v1: https://patch.msgid.link/[email protected]

---
Alexandre Courbot (9):
gpu: nova-core: gsp: cmdq: validate checksum earlier on receive
gpu: nova-core: gsp: introduce and use proper RpcMessageHeader type
gpu: nova-core: gsp: cmdq: group the RPC-specific part of send_single_command
gpu: nova-core: gsp: cmdq: split the transport part of the send path
gpu: nova-core: gsp: cmdq: split RPC parsing part of the receive path
gpu: nova-core: gsp: cmdq: split the transport part of the receive path
gpu: nova-core: gsp: cmdq: move the RPC code into a sub-module
gpu: nova-core: gsp: move the RPC commands into a sub-module
gpu: nova-core: gsp: add rpc to RPC message send/receive methods

 drivers/gpu/nova-core/api.rs                       |   2 +-
 drivers/gpu/nova-core/gpu.rs                       |   2 +-
 drivers/gpu/nova-core/gsp.rs                       |   4 +-
 drivers/gpu/nova-core/gsp/boot.rs                  |  12 +-
 drivers/gpu/nova-core/gsp/cmdq.rs                  | 351 +++----------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 380 +++++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              | 335 +-----------------
 drivers/gpu/nova-core/gsp/commands/rpc.rs          | 339 ++++++++++++++++++
 drivers/gpu/nova-core/gsp/fw.rs                    |  88 +++--
 drivers/gpu/nova-core/gsp/sequencer.rs             |   6 +-
 11 files changed, 834 insertions(+), 685 deletions(-)

---
base-commit: 10a6623a24a85708650efad7be15182289403cd7
change-id: 20260927-cmdq-rpc-ed5f850b207e

Best regards,

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

PATCH v3 1/9 gpu: nova-core: gsp: cmdq: validate checksum earlier on receive

The checksum validation error message of wait_for_msg prints the message's sequence number before validating the checksum.

But the checksum is part of the transport layer, and it not validating indicates a corruption that could very well be in the RPC message header, meaning the value of this field cannot be trusted.

Furthermore, the transport layer is not supposed to know the kind of message it transports, and it accessing the RPC header is a layering violation.

Thus, move the checksum validation before we start looking at the message header, and drop that information from the error message.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +9 -15
index a5595da23407..d293d28b0967 100644
@@ -768,6 +768,15 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
         // Extract the `GspMsgElement`.
         let (header, slice_1) = GspMsgElement::from_bytes_prefix(slice_1).ok_or(EIO)?;
 
+        // Validate checksum after truncating the message to its exact length.
+        if Cmdq::calculate_checksum(
+            SBufferIter::new_reader([header.as_bytes(), slice_1, slice_2]).take(header.length()),
+        ) != 0
+        {
+            dev_err!(&self.dev, "GSP receive: bad checksum\n");
+            return Err(EIO);
+        }
+
         dev_dbg!(
             &self.dev,
             "GSP RPC: receive: seq# {}, function={:?}, length=0x{:x}\n",
@@ -796,21 +805,6 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
             )
         };
 
-        // Validate checksum.
-        if Cmdq::calculate_checksum(SBufferIter::new_reader([
-            header.as_bytes(),
-            slice_1,
-            slice_2,
-        ])) != 0
-        {
-            dev_err!(
-                &self.dev,
-                "GSP RPC: receive: Call {} - bad checksum\n",
-                header.sequence()
-            );
-            return Err(EIO);
-        }
-
         Ok(GspMessage {
             header,
             contents: (slice_1, slice_2),
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v3 1/9] gpu: nova-core: gsp: cmdq: validate checksum earlier on receive

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] gpu: nova-core: TOCTOU race in GSP message checksum validation allows bypassing integrity checks --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] gpu: nova-core: TOCTOU race in GSP message checksum validation allows bypassing integrity checks --

commit 7f9f9f297ff07c9a2fc40d814e38057d895893f1
Author: Alexandre Courbot <[email protected]>

gpu: nova-core: gsp: cmdq: validate checksum earlier on receive

The checksum validation error message of wait_for_msg() prints the message's sequence number before validating the checksum. But the checksum is part of the transport layer, and it not validating indicates a corruption. Thus, this commit moves the checksum validation before we start looking at the message header, and drops the sequence number from the error message.

drivers/gpu/nova-core/gsp/cmdq.rs fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> { · line 773

Quoted 11 lines
drivers/gpu/nova-core/gsp/cmdq.rs +3 -0
index a5595da23407b..d293d28b09674 100644
@@ -768,6 +768,15 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
         // Extract the `GspMsgElement`.
         let (header, slice_1) = GspMsgElement::from_bytes_prefix(slice_1).ok_or(EIO)?;
 
+        // Validate checksum after truncating the message to its exact length.
+        if Cmdq::calculate_checksum(
+            SBufferIter::new_reader([header.as_bytes(), slice_1, slice_2]).take(header.length()),

[Severity: High]
Does this regression create a TOCTOU race condition in wait_for_msg()? header appears to map directly to DMA memory, and header.length() is read here for the first time to determine the boundaries for the checksum validation.

If the GSP (or a hardware glitch) modifies the length field in DMA to a larger value after this checksum passes, it is read again further down in wait_for_msg():

        let payload_length = header.payload_length();

        // Check that the driver read area is large enough for the message.
        if slice_1.len() + slice_2.len() < payload_length {
            return Err(EIO);
        }

Could this secondary read use the modified length, causing the slices to be truncated using the corrupted length and bypassing the integrity check for the unverified data passed to the RPC handler?

Quoted 7 lines
drivers/gpu/nova-core/gsp/cmdq.rs +6 -0
+        ) != 0
+        {
+            dev_err!(&self.dev, "GSP receive: bad checksum\n");
+            return Err(EIO);
+        }
+
         dev_dbg!(

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH v3 2/9 gpu: nova-core: gsp: introduce and use proper RpcMessageHeader type

In reply to Alexandre Courbot

So far, the GSP command queue transport and message layer code were intertwined, a design issue that goes as deep as the types themselves:
the generated bindings for GspMsgElement even include the RPC header at its end.

This makes it difficult to introduce the new GMC message type; thus this patch works around these limitations to make the RPC message header more explicit and allow it to be eventually handled by a different layer.

The RpcMessageHeader wrapping type is introduced following the same model as GspMsgElement, and can be obtained from the latter. The methods of GspMsgElement that actually query the RPC header are moved to RpcMessageHeader.

Regarding initialization, GspMsgElement leaves the RPC header zeroed, and the command queue code is now responsible for initializing it in a separate call.

The only functional change is that the RPC debug messages now display the size of the RPC payload instead of the whole message including its headers, as they are technically part of the message layer. This metric is arguably more useful as the headers have successfully been parsed by the time we can print these messages.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 35 +++++++++++-----
 drivers/gpu/nova-core/gsp/fw.rs   | 88 ++++++++++++++++++++++++---------------
 2 files changed, 78 insertions(+), 45 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +24 -11
index d293d28b0967..b40d45bf0a92 100644
@@ -50,6 +50,7 @@
             MsgFunction,
             MsgqRxHeader,
             MsgqTxHeader,
+            RpcMessageHeader,
             GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, //
         },
         PteArray,
@@ -664,11 +665,22 @@ fn send_single_command<M>(&mut self, command: M) -> Result
         let (cmd, payload_1) = M::Command::from_bytes_mut_prefix(dst.contents.0).ok_or(EIO)?;
 
         // Fill the header and command in-place.
-        let msg_element = GspMsgElement::init(self.seq, size_in_bytes, M::FUNCTION);
-        // SAFETY: `msg_header` and `cmd` are valid references, and not touched if the initializer
-        // fails.
+        let msg_element_init = GspMsgElement::init(self.seq, size_in_bytes);
+        let rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
+        // SAFETY: `dst.header` is a valid reference, and is not touched if the initializer fails.
+        unsafe {
+            pin_init::raw_try_init(core::ptr::from_mut(dst.header), msg_element_init)?;
+        }
+        // SAFETY: `dst.header.rpc_header_mut()` is a valid reference, and is not touched if the
+        // initializer fails.
+        unsafe {
+            pin_init::raw_try_init(
+                core::ptr::from_mut(dst.header.rpc_header_mut()),
+                rpc_header_init,
+            )?;
+        }
+        // SAFETY: `cmd` is a valid reference, and is not touched if the initializer fails.
         unsafe {
-            pin_init::raw_try_init(core::ptr::from_mut(dst.header), msg_element)?;
             pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
         }
 
@@ -694,7 +706,7 @@ fn send_single_command<M>(&mut self, command: M) -> Result
             "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
             self.seq,
             M::FUNCTION,
-            dst.header.length(),
+            size_in_bytes,
         );
 
         // All set - update the write pointer and inform the GSP of the new command.
@@ -777,16 +789,17 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
             return Err(EIO);
         }
 
+        let rpc_header = header.rpc_header();
+        let payload_length = rpc_header.length();
+
         dev_dbg!(
             &self.dev,
             "GSP RPC: receive: seq# {}, function={:?}, length=0x{:x}\n",
-            header.sequence(),
-            header.function(),
-            header.length(),
+            rpc_header.sequence(),
+            rpc_header.function(),
+            payload_length,
         );
 
-        let payload_length = header.payload_length();
-
         // Check that the driver read area is large enough for the message.
         if slice_1.len() + slice_2.len() < payload_length {
             return Err(EIO);
@@ -833,7 +846,7 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
         Error: From<M::InitError>,
     {
         let message = self.wait_for_msg(timeout)?;
-        let function = message.header.function().map_err(|_| EINVAL)?;
+        let function = message.header.rpc_header().function().map_err(|_| EINVAL)?;
 
         // Extract the message. Store the result as we want to advance the read pointer even in
         // case of failure.
drivers/gpu/nova-core/gsp/fw.rs +54 -34

131 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#4 lore raw

PATCH v3 3/9 gpu: nova-core: gsp: cmdq: group the RPC-specific part of send_single_command

In reply to Alexandre Courbot

send_single_command handles both the transport and message layers of the command, intertwining the logic of the two.

Reorder the code so the message layer logic is within the same contiguous block of code, so it can easily be moved.

No functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +15 -15
index b40d45bf0a92..bda79f23626d 100644
@@ -659,18 +659,18 @@ fn send_single_command<M>(&mut self, command: M) -> Result
             .gsp_mem
             .allocate_command(size_in_bytes, Self::ALLOCATE_TIMEOUT)?;
 
-        // 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)?;
-
-        // Fill the header and command in-place.
+        // Fill the header.
         let msg_element_init = GspMsgElement::init(self.seq, size_in_bytes);
-        let rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
         // SAFETY: `dst.header` is a valid reference, and is not touched if the initializer fails.
         unsafe {
             pin_init::raw_try_init(core::ptr::from_mut(dst.header), msg_element_init)?;
         }
+
+        // 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 rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
         // SAFETY: `dst.header.rpc_header_mut()` is a valid reference, and is not touched if the
         // initializer fails.
         unsafe {
@@ -693,14 +693,6 @@ fn send_single_command<M>(&mut self, command: M) -> Result
         }
         drop(sbuffer);
 
-        // Compute checksum now that the whole message is ready.
-        dst.header
-            .set_checksum(Cmdq::calculate_checksum(SBufferIter::new_reader([
-                dst.header.as_bytes(),
-                dst.contents.0,
-                dst.contents.1,
-            ])));
-
         dev_dbg!(
             &self.dev,
             "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
@@ -709,6 +701,14 @@ fn send_single_command<M>(&mut self, command: M) -> Result
             size_in_bytes,
         );
 
+        // Compute checksum now that the whole message is ready.
+        dst.header
+            .set_checksum(Cmdq::calculate_checksum(SBufferIter::new_reader([
+                dst.header.as_bytes(),
+                dst.contents.0,
+                dst.contents.1,
+            ])));
+
         // All set - update the write pointer and inform the GSP of the new command.
         let elem_count = dst.header.element_count();
         self.seq += 1;
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH v3 4/9 gpu: nova-core: gsp: cmdq: split the transport part of the send path

In reply to Alexandre Courbot

Introduce the CommandElement trait and RpcCommandElement wrapper type to define how messages (independently of their type) are sent through the transport layer. send_single_command becomes send_command_element, which allocates the queue slots, writes the element header, and delegates the writing of the message itself to the implementation of CommandElement before computing the checksum, advancing the write pointer and ringing the doorbell.

The RPC part of send_single_command (writing the RPC header and the command payload) is now part of the CommandElement implementation. This sets things up for moving the RPC code into its own sub-module, leaving the transport agnostic of the message type.

No functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 133 +++++++++++++++++++++++---------------
 1 file changed, 80 insertions(+), 53 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +80 -53
index bda79f23626d..d8a7716fc500 100644
@@ -35,10 +35,7 @@
     },
 };
 
-use continuation::{
-    ContinuationRecord,
-    SplitState, //
-};
+use continuation::SplitState;
 
 use pin_init::pin_init_scope;
 
@@ -67,6 +64,19 @@
 /// reply type are sent using [`Cmdq::send_command_no_wait`].
 pub(crate) struct NoReply;
 
+/// Trait implemented by types that can be sent as a single command queue element.
+///
+/// The command queue allocates `size()` bytes after the `GspMsgElement` header and calls `write()`
+/// to fill them.
+trait CommandElement {
+    /// Size in bytes of the element, not including the `GspMsgElement` header.
+    fn size(&self) -> usize;
+
+    /// Writes the contents of the command into `dst`. `dev` is the queue's device (to be used for
+    /// logging), `seq` is the sequence number of the element.
+    fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result;
+}
+
 /// Trait implemented by types representing a command to send to the GSP.
 ///
 /// The main purpose of this trait is to provide [`Cmdq`] with the information it needs to send
@@ -129,6 +139,62 @@ fn size(&self) -> usize {
     }
 }
 
+/// Wrapper type for sending a RPC command as a command queue element.
+///
+/// [`CommandElement`] cannot be directly implemented for all [`CommandToGsp`] with a blanket
+/// implementation as it would conflict with other future command types.
+struct RpcCommandElement<M>(M);
+
+impl<M> CommandElement for RpcCommandElement<M>
+where
+    M: CommandToGsp,
+    Error: From<M::InitError>,
+{
+    fn size(&self) -> usize {
+        self.0.size()
+    }
+
+    fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result {
+        let command = &self.0;
+        let size_in_bytes = command.size();
+        // 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 rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
+        // SAFETY: `dst.header.rpc_header_mut()` is a valid reference, and is not touched if the
+        // initializer fails.
+        unsafe {
+            pin_init::raw_try_init(
+                core::ptr::from_mut(dst.header.rpc_header_mut()),
+                rpc_header_init,
+            )?;
+        }
+        // SAFETY: `cmd` is a valid reference, and is not touched if the initializer fails.
+        unsafe {
+            pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
+        }
+
+        // Fill the variable-length payload, which may be empty.
+        let mut sbuffer = SBufferIter::new_writer([&mut payload_1[..], &mut dst.contents.1[..]]);
+        command.init_variable_payload(&mut sbuffer)?;
+
+        if !sbuffer.is_empty() {
+            return Err(EIO);
+        }
+
+        dev_dbg!(
+            dev,
+            "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
+            seq,
+            M::FUNCTION,
+            size_in_bytes,
+        );
+
+        Ok(())
+    }
+}
+
 /// Trait representing messages received from the GSP.
 ///
 /// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.
@@ -638,24 +704,18 @@ impl CmdqInner<'_> {
     /// Timeout for waiting for space on the command queue.
     const ALLOCATE_TIMEOUT: Delta = Delta::from_secs(1);
 
-    /// Sends `command` to the GSP, without splitting it.
+    /// Allocate enough send slots to store `command`, initialize them using
+    /// [`CommandElement::write`], and send the command to the GSP.
     ///
     /// # Errors
     ///
     /// - `EMSGSIZE` if the command exceeds the maximum queue element size.
     /// - `ETIMEDOUT` if space does not become available within the timeout.
-    /// - `EIO` if the variable payload requested by the command has not been entirely
-    ///   written to by its [`CommandToGsp::init_variable_payload`] method.
     ///
-    /// Error codes returned by the command initializers are propagated as-is.
-    fn send_single_command<M>(&mut self, command: M) -> Result
-    where
-        M: CommandToGsp,
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
+    /// Error codes returned by [`CommandElement::write`] are returned as-is.
+    fn send_command_element<E: CommandElement>(&mut self, command: E) -> Result {
         let size_in_bytes = command.size();
-        let dst = self
+        let mut dst = self
             .gsp_mem
             .allocate_command(size_in_bytes, Self::ALLOCATE_TIMEOUT)?;
 
@@ -666,40 +726,8 @@ fn send_single_command<M>(&mut self, command: M) -> Result
             pin_init::raw_try_init(core::ptr::from_mut(dst.header), msg_element_init)?;
         }
 
-        // 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 rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
-        // SAFETY: `dst.header.rpc_header_mut()` is a valid reference, and is not touched if the
-        // initializer fails.
-        unsafe {
-            pin_init::raw_try_init(
-                core::ptr::from_mut(dst.header.rpc_header_mut()),
-                rpc_header_init,
-            )?;
-        }
-        // SAFETY: `cmd` is a valid reference, and is not touched if the initializer fails.
-        unsafe {
-            pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
-        }
-
-        // Fill the variable-length payload, which may be empty.
-        let mut sbuffer = SBufferIter::new_writer([&mut payload_1[..], &mut dst.contents.1[..]]);
-        command.init_variable_payload(&mut sbuffer)?;
-
-        if !sbuffer.is_empty() {
-            return Err(EIO);
-        }
-        drop(sbuffer);
-
-        dev_dbg!(
-            &self.dev,
-            "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
-            self.seq,
-            M::FUNCTION,
-            size_in_bytes,
-        );
+        // Write the message payload.
+        command.write(self.dev, self.seq, &mut dst)?;
 
         // Compute checksum now that the whole message is ready.
         dst.header
@@ -735,13 +763,12 @@ fn send_command<M>(&mut self, command: M) -> Result
         Error: From<M::InitError>,
     {
         match SplitState::new(command)? {
-            SplitState::Single(command) => self.send_single_command(command),
+            SplitState::Single(command) => self.send_command_element(RpcCommandElement(command)),
             SplitState::Split(command, mut continuations) => {
-                self.send_single_command(command)?;
+                self.send_command_element(RpcCommandElement(command))?;
 
                 while let Some(continuation) = continuations.next() {
-                    // Turbofish needed because the compiler cannot infer M here.
-                    self.send_single_command::<ContinuationRecord<'_>>(continuation)?;
+                    self.send_command_element(RpcCommandElement(continuation))?;
                 }
 
                 Ok(())
Signature · 1 line
2.55.0
Reviewed-by on v3 4/9
#6 lore raw

Re: [PATCH v3 4/9] gpu: nova-core: gsp: cmdq: split the transport part of the send path

Quoted 121 lines · Alexandre Courbot

Introduce the CommandElement trait and RpcCommandElement wrapper type to define how messages (independently of their type) are sent through the transport layer. send_single_command becomes send_command_element, which allocates the queue slots, writes the element header, and delegates the writing of the message itself to the implementation of CommandElement before computing the checksum, advancing the write pointer and ringing the doorbell.

The RPC part of send_single_command (writing the RPC header and the command payload) is now part of the CommandElement implementation. This sets things up for moving the RPC code into its own sub-module, leaving the transport agnostic of the message type.

No functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 133 +++++++++++++++++++++++---------------
 1 file changed, 80 insertions(+), 53 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +70 -4
index bda79f23626d..d8a7716fc500 100644
@@ -35,10 +35,7 @@
     },
 };
 
-use continuation::{
-    ContinuationRecord,
-    SplitState, //
-};
+use continuation::SplitState;
 
 use pin_init::pin_init_scope;
 
@@ -67,6 +64,19 @@
 /// reply type are sent using [`Cmdq::send_command_no_wait`].
 pub(crate) struct NoReply;
 
+/// Trait implemented by types that can be sent as a single command queue element.
+///
+/// The command queue allocates `size()` bytes after the `GspMsgElement` header and calls `write()`
+/// to fill them.
+trait CommandElement {
+    /// Size in bytes of the element, not including the `GspMsgElement` header.
+    fn size(&self) -> usize;
+
+    /// Writes the contents of the command into `dst`. `dev` is the queue's device (to be used for
+    /// logging), `seq` is the sequence number of the element.
+    fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result;
+}
+
 /// Trait implemented by types representing a command to send to the GSP.
 ///
 /// The main purpose of this trait is to provide [`Cmdq`] with the information it needs to send
@@ -129,6 +139,62 @@ fn size(&self) -> usize {
     }
 }
 
+/// Wrapper type for sending a RPC command as a command queue element.
+///
+/// [`CommandElement`] cannot be directly implemented for all [`CommandToGsp`] with a blanket
+/// implementation as it would conflict with other future command types.
+struct RpcCommandElement<M>(M);
+
+impl<M> CommandElement for RpcCommandElement<M>
+where
+    M: CommandToGsp,
+    Error: From<M::InitError>,
+{
+    fn size(&self) -> usize {
+        self.0.size()
+    }
+
+    fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result {
+        let command = &self.0;
+        let size_in_bytes = command.size();
+        // 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 rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
+        // SAFETY: `dst.header.rpc_header_mut()` is a valid reference, and is not touched if the
+        // initializer fails.
+        unsafe {
+            pin_init::raw_try_init(
+                core::ptr::from_mut(dst.header.rpc_header_mut()),
+                rpc_header_init,
+            )?;
+        }
+        // SAFETY: `cmd` is a valid reference, and is not touched if the initializer fails.
+        unsafe {
+            pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
+        }
+
+        // Fill the variable-length payload, which may be empty.
+        let mut sbuffer = SBufferIter::new_writer([&mut payload_1[..], &mut dst.contents.1[..]]);
+        command.init_variable_payload(&mut sbuffer)?;
+
+        if !sbuffer.is_empty() {
+            return Err(EIO);
+        }
+
+        dev_dbg!(
+            dev,
+            "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
+            seq,
+            M::FUNCTION,
+            size_in_bytes,
+        );
+
+        Ok(())
+    }
+}
+
 /// Trait representing messages received from the GSP.
 ///
 /// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.

drivers/gpu/nova-core/gsp/cmdq.rs impl CmdqInner<'_> { · line 708

Quoted 6 lines
drivers/gpu/nova-core/gsp/cmdq.rs +2 -1
@@ -638,24 +704,18 @@ impl CmdqInner<'_> {
     /// Timeout for waiting for space on the command queue.
     const ALLOCATE_TIMEOUT: Delta = Delta::from_secs(1);
 
-    /// Sends `command` to the GSP, without splitting it.
+    /// Allocate enough send slots to store `command`, initialize them using
+    /// [`CommandElement::write`], and send the command to the GSP.

nit: not sure what send slots are / seems like a new term? - I think the caller doesn't need to understand that it's per page anyway

author maintainer
#7 lore raw

Re: [PATCH v3 4/9] gpu: nova-core: gsp: cmdq: split the transport part of the send path

Quoted 125 lines · Eliot Courtney
Quoted 124 lines · Alexandre Courbot

Introduce the CommandElement trait and RpcCommandElement wrapper type to define how messages (independently of their type) are sent through the transport layer. send_single_command becomes send_command_element, which allocates the queue slots, writes the element header, and delegates the writing of the message itself to the implementation of CommandElement before computing the checksum, advancing the write pointer and ringing the doorbell.

The RPC part of send_single_command (writing the RPC header and the command payload) is now part of the CommandElement implementation. This sets things up for moving the RPC code into its own sub-module, leaving the transport agnostic of the message type.

No functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 133 +++++++++++++++++++++++---------------
 1 file changed, 80 insertions(+), 53 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +70 -4
index bda79f23626d..d8a7716fc500 100644
@@ -35,10 +35,7 @@
     },
 };
 
-use continuation::{
-    ContinuationRecord,
-    SplitState, //
-};
+use continuation::SplitState;
 
 use pin_init::pin_init_scope;
 
@@ -67,6 +64,19 @@
 /// reply type are sent using [`Cmdq::send_command_no_wait`].
 pub(crate) struct NoReply;
 
+/// Trait implemented by types that can be sent as a single command queue element.
+///
+/// The command queue allocates `size()` bytes after the `GspMsgElement` header and calls `write()`
+/// to fill them.
+trait CommandElement {
+    /// Size in bytes of the element, not including the `GspMsgElement` header.
+    fn size(&self) -> usize;
+
+    /// Writes the contents of the command into `dst`. `dev` is the queue's device (to be used for
+    /// logging), `seq` is the sequence number of the element.
+    fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result;
+}
+
 /// Trait implemented by types representing a command to send to the GSP.
 ///
 /// The main purpose of this trait is to provide [`Cmdq`] with the information it needs to send
@@ -129,6 +139,62 @@ fn size(&self) -> usize {
     }
 }
 
+/// Wrapper type for sending a RPC command as a command queue element.
+///
+/// [`CommandElement`] cannot be directly implemented for all [`CommandToGsp`] with a blanket
+/// implementation as it would conflict with other future command types.
+struct RpcCommandElement<M>(M);
+
+impl<M> CommandElement for RpcCommandElement<M>
+where
+    M: CommandToGsp,
+    Error: From<M::InitError>,
+{
+    fn size(&self) -> usize {
+        self.0.size()
+    }
+
+    fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result {
+        let command = &self.0;
+        let size_in_bytes = command.size();
+        // 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 rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
+        // SAFETY: `dst.header.rpc_header_mut()` is a valid reference, and is not touched if the
+        // initializer fails.
+        unsafe {
+            pin_init::raw_try_init(
+                core::ptr::from_mut(dst.header.rpc_header_mut()),
+                rpc_header_init,
+            )?;
+        }
+        // SAFETY: `cmd` is a valid reference, and is not touched if the initializer fails.
+        unsafe {
+            pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
+        }
+
+        // Fill the variable-length payload, which may be empty.
+        let mut sbuffer = SBufferIter::new_writer([&mut payload_1[..], &mut dst.contents.1[..]]);
+        command.init_variable_payload(&mut sbuffer)?;
+
+        if !sbuffer.is_empty() {
+            return Err(EIO);
+        }
+
+        dev_dbg!(
+            dev,
+            "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
+            seq,
+            M::FUNCTION,
+            size_in_bytes,
+        );
+
+        Ok(())
+    }
+}
+
 /// Trait representing messages received from the GSP.
 ///
 /// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.
@@ -638,24 +704,18 @@ impl CmdqInner<'_> {
     /// Timeout for waiting for space on the command queue.
     const ALLOCATE_TIMEOUT: Delta = Delta::from_secs(1);
 
Quoted 6 lines

drivers/gpu/nova-core/gsp/cmdq.rs impl CmdqInner<'_> { · line 708

Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs +2 -1
-    /// Sends `command` to the GSP, without splitting it.
+    /// Allocate enough send slots to store `command`, initialize them using
+    /// [`CommandElement::write`], and send the command to the GSP.

nit: not sure what send slots are / seems like a new term? - I think the caller doesn't need to understand that it's per page anyway

Indeed, that detail is not necessary. Reworded, thanks!

author maintainer
#8 lore raw

PATCH v3 5/9 gpu: nova-core: gsp: cmdq: split RPC parsing part of the receive path

In reply to Alexandre Courbot

The RPC parsing should not be done in the same method as the transport header parsing. Split it out and introduce a RpcMessage type to indicate messages that have been successfully parsed as RPC. This makes the code easier to move around into its new destination.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +33 -4
index d8a7716fc500..b8a9e02b76fe 100644
@@ -553,6 +553,17 @@ struct GspMessage<'a> {
     contents: (&'a [u8], &'a [u8]),
 }
 
+/// A RPC message ready to be processed from the message queue.
+///
+/// This is the type returned by [`CmdqInner::parse_rpc_message`].
+struct RpcMessage<'a> {
+    // Reference to the RPC header of the message.
+    header: &'a RpcMessageHeader,
+    // Slices to the contents of the message. The second slice is zero unless the message loops
+    // over the message queue.
+    contents: (&'a [u8], &'a [u8]),
+}
+
 /// GSP command queue.
 ///
 /// Provides the ability to send commands and receive messages from the GSP using a shared memory
@@ -794,7 +805,7 @@ fn send_command<M>(&mut self, command: M) -> Result
     ///   message queue.
     ///
     /// Error codes returned by the message constructor are propagated as-is.
-    fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
+    fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {
         // Wait for a message to arrive from the GSP.
         let (slice_1, slice_2) = read_poll_timeout(
             || Ok(self.gsp_mem.driver_read_area()),
@@ -816,6 +827,24 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
             return Err(EIO);
         }
 
+        self.parse_rpc_message(GspMessage {
+            header,
+            contents: (slice_1, slice_2),
+        })
+    }
+
+    /// Validate the RPC layer of `element` and returns its RPC header and its contents trimmed down
+    /// to the RPC payload.
+    ///
+    /// # Errors
+    ///
+    /// - `EIO` if the element is shorter than the payload length advertised by the RPC header.
+    fn parse_rpc_message<'a>(&self, element: GspMessage<'a>) -> Result<RpcMessage<'a>> {
+        let GspMessage {
+            header,
+            contents: (slice_1, slice_2),
+        } = element;
+
         let rpc_header = header.rpc_header();
         let payload_length = rpc_header.length();
 
@@ -845,8 +874,8 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
             )
         };
 
-        Ok(GspMessage {
-            header,
+        Ok(RpcMessage {
+            header: rpc_header,
             contents: (slice_1, slice_2),
         })
     }
@@ -873,7 +902,7 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
         Error: From<M::InitError>,
     {
         let message = self.wait_for_msg(timeout)?;
-        let function = message.header.rpc_header().function().map_err(|_| EINVAL)?;
+        let function = message.header.function().map_err(|_| EINVAL)?;
 
         // Extract the message. Store the result as we want to advance the read pointer even in
         // case of failure.
Signature · 1 line
2.55.0
#9 lore raw

Re: [PATCH v3 5/9] gpu: nova-core: gsp: cmdq: split RPC parsing part of the receive path

Quoted 33 lines · Alexandre Courbot

The RPC parsing should not be done in the same method as the transport header parsing. Split it out and introduce a RpcMessage type to indicate messages that have been successfully parsed as RPC. This makes the code easier to move around into its new destination.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +11 -0
index d8a7716fc500..b8a9e02b76fe 100644
@@ -553,6 +553,17 @@ struct GspMessage<'a> {
     contents: (&'a [u8], &'a [u8]),
 }
 
+/// A RPC message ready to be processed from the message queue.
+///
+/// This is the type returned by [`CmdqInner::parse_rpc_message`].
+struct RpcMessage<'a> {
+    // Reference to the RPC header of the message.
+    header: &'a RpcMessageHeader,
+    // Slices to the contents of the message. The second slice is zero unless the message loops
+    // over the message queue.
+    contents: (&'a [u8], &'a [u8]),
+}
+
 /// GSP command queue.
 ///
 /// Provides the ability to send commands and receive messages from the GSP using a shared memory

drivers/gpu/nova-core/gsp/cmdq.rs fn send_command<M>(&mut self, command: M) -> Result · line 808

Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
@@ -794,7 +805,7 @@ fn send_command<M>(&mut self, command: M) -> Result
     ///   message queue.
     ///
     /// Error codes returned by the message constructor are propagated as-is.
-    fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
+    fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {

This gets changed back to GspMessage return value in the next patch, and also introduces a bug [1]. What about just keeping return GspMessage here? You can move RpcMessage to the next patch.

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

author maintainer
#10 lore raw

Re: [PATCH v3 5/9] gpu: nova-core: gsp: cmdq: split RPC parsing part of the receive path

Quoted 44 lines · Eliot Courtney
Quoted 33 lines · Alexandre Courbot

The RPC parsing should not be done in the same method as the transport header parsing. Split it out and introduce a RpcMessage type to indicate messages that have been successfully parsed as RPC. This makes the code easier to move around into its new destination.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +11 -0
index d8a7716fc500..b8a9e02b76fe 100644
@@ -553,6 +553,17 @@ struct GspMessage<'a> {
     contents: (&'a [u8], &'a [u8]),
 }
 
+/// A RPC message ready to be processed from the message queue.
+///
+/// This is the type returned by [`CmdqInner::parse_rpc_message`].
+struct RpcMessage<'a> {
+    // Reference to the RPC header of the message.
+    header: &'a RpcMessageHeader,
+    // Slices to the contents of the message. The second slice is zero unless the message loops
+    // over the message queue.
+    contents: (&'a [u8], &'a [u8]),
+}
+
 /// GSP command queue.
 ///
 /// Provides the ability to send commands and receive messages from the GSP using a shared memory

drivers/gpu/nova-core/gsp/cmdq.rs fn send_command<M>(&mut self, command: M) -> Result · line 808

Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
@@ -794,7 +805,7 @@ fn send_command<M>(&mut self, command: M) -> Result
     ///   message queue.
     ///
     /// Error codes returned by the message constructor are propagated as-is.
-    fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
+    fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {

This gets changed back to GspMessage return value in the next patch, and also introduces a bug [1]. What about just keeping return GspMessage here? You can move RpcMessage to the next patch.

Quoted 1 line

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

Indeed, we could organize the change a bit more smoothly. I've tried to reorder and hopefully the next revision will flow better.

And yes, it is undeniable that having both length methods called length is confusing and has caused this bug, so I've given them different names.

author maintainer
#11 lore raw

PATCH v3 6/9 gpu: nova-core: gsp: cmdq: split the transport part of the receive path

In reply to Alexandre Courbot

wait_for_msg mixes two layers: the transport layer which polls the queue, extracts the element header and validates the checksum, and the RPC layer which reads the RPC header and trims the payload slices to the length advertised by the RPC header.

Move the transport layer into wait_for_element, and introduce consume_element, a transport-level method which reads the message's contents using an implementation of the MessageElement trait before advancing the CPU read pointer past it, and parse_rpc_message, which validates the RPC layer. This sets things up for moving the RPC code into its own module, leaving the transport agnostic of the message type.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 219 +++++++++++++++++++++-----------------
 1 file changed, 119 insertions(+), 100 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +119 -100
index b8a9e02b76fe..07036972dbec 100644
@@ -195,6 +195,15 @@ fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Res
     }
 }
 
+/// Trait implemented by types that can be received as single command queue elements.
+///
+/// The command queue validates the element header before calling `read()` to interpret the
+/// contents.
+trait MessageElement: Sized {
+    /// Tries to read `Self` from `element`. `dev` is the queue's device, to be used for logging.
+    fn read(dev: &device::Device, element: GspMessage<'_>) -> Result<Self>;
+}
+
 /// Trait representing messages received from the GSP.
 ///
 /// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.
@@ -218,6 +227,94 @@ fn read(
     ) -> Result<Self, Self::InitError>;
 }
 
+/// Wrapper type for receiving a RPC message from a command queue element.
+///
+/// [`MessageElement`] cannot be directly implemented for all [`MessageFromGsp`] with a blanket
+/// implementation as it would conflict with other future message types.
+struct RpcMessageElement<M>(M);
+
+impl<M> RpcMessageElement<M>
+where
+    M: MessageFromGsp,
+{
+    /// Validate the RPC layer of `element` and returns its RPC header and its contents trimmed down
+    /// to the RPC payload.
+    ///
+    /// # Errors
+    ///
+    /// - `EIO` if the element is shorter than the payload length advertised by the RPC header.
+    fn parse_rpc_message<'a>(
+        dev: &device::Device,
+        element: GspMessage<'a>,
+    ) -> Result<RpcMessage<'a>> {
+        let GspMessage {
+            header,
+            contents: (slice_1, slice_2),
+        } = element;
+
+        let rpc_header = header.rpc_header();
+        let payload_length = rpc_header.length();
+
+        dev_dbg!(
+            dev,
+            "GSP RPC: receive: seq# {}, function={:?}, length=0x{:x}\n",
+            rpc_header.sequence(),
+            rpc_header.function(),
+            payload_length,
+        );
+
+        // Check that the element is large enough for the message.
+        if slice_1.len() + slice_2.len() < payload_length {
+            return Err(EIO);
+        }
+
+        // Cut the message slices down to the actual length of the message.
+        let (slice_1, slice_2) = if slice_1.len() > payload_length {
+            // PANIC: we checked above that `slice_1` is at least as long as `payload_length`.
+            (slice_1.split_at(payload_length).0, &slice_2[0..0])
+        } else {
+            (
+                slice_1,
+                // PANIC: we checked above that `slice_1.len() + slice_2.len()` is at least as
+                // large as `payload_length`.
+                slice_2.split_at(payload_length - slice_1.len()).0,
+            )
+        };
+
+        Ok(RpcMessage {
+            header: rpc_header,
+            contents: (slice_1, slice_2),
+        })
+    }
+}
+
+impl<M> MessageElement for RpcMessageElement<M>
+where
+    M: MessageFromGsp,
+    Error: From<M::InitError>,
+{
+    fn read(dev: &device::Device, element: GspMessage<'_>) -> Result<Self> {
+        let message = Self::parse_rpc_message(dev, element)?;
+        let function = message.header.function().map_err(|_| EINVAL)?;
+
+        if function != M::FUNCTION {
+            return Err(ERANGE);
+        }
+
+        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(Self)
+            .map_err(|e| e.into())
+            .inspect(|_| {
+                if !sbuffer.is_empty() {
+                    dev_warn!(dev, "GSP message {:?} has unprocessed data\n", function);
+                }
+            })
+    }
+}
+
 /// Number of GSP pages making the [`Msgq`].
 pub(crate) const MSGQ_NUM_PAGES: u32 = 0x3f;
 
@@ -544,7 +641,7 @@ struct GspCommand<'a> {
 
 /// A message ready to be processed from the message queue.
 ///
-/// This is the type returned by [`CmdqInner::wait_for_msg`].
+/// This is the type returned by [`CmdqInner::wait_for_element`].
 struct GspMessage<'a> {
     // Reference to the header of the message.
     header: &'a GspMsgElement,
@@ -662,8 +759,8 @@ pub(crate) fn send_command<M>(&self, command: M) -> Result<M::Reply>
         inner.send_command(command)?;
 
         loop {
-            match inner.receive_msg::<M::Reply>(Self::RECEIVE_TIMEOUT) {
-                Ok(reply) => break Ok(reply),
+            match inner.consume_element::<RpcMessageElement<M::Reply>>(Self::RECEIVE_TIMEOUT) {
+                Ok(reply) => break Ok(reply.0),
                 Err(ERANGE) => continue,
                 Err(e) => break Err(e),
             }
@@ -689,13 +786,16 @@ pub(crate) fn send_command_no_wait<M>(&self, command: M) -> Result
 
     /// Receive a message from the GSP.
     ///
-    /// See [`CmdqInner::receive_msg`] for details.
+    /// See [`CmdqInner::consume_element`] for details.
     pub(crate) fn receive_msg<M: MessageFromGsp>(&self, timeout: Delta) -> Result<M>
     where
         // This allows all error types, including `Infallible`, to be used for `M::InitError`.
         Error: From<M::InitError>,
     {
-        self.inner.lock().receive_msg(timeout)
+        self.inner
+            .lock()
+            .consume_element::<RpcMessageElement<M>>(timeout)
+            .map(|m| m.0)
     }
 }
 
@@ -787,7 +887,7 @@ fn send_command<M>(&mut self, command: M) -> Result
         }
     }
 
-    /// Wait for a message to become available on the message queue.
+    /// Wait for the next element to become available on the message queue.
     ///
     /// This works purely at the transport layer and does not interpret or validate the message
     /// beyond the advertised length in its [`GspMsgElement`].
@@ -805,7 +905,7 @@ fn send_command<M>(&mut self, command: M) -> Result
     ///   message queue.
     ///
     /// Error codes returned by the message constructor are propagated as-is.
-    fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {
+    fn wait_for_element(&self, timeout: Delta) -> Result<GspMessage<'_>> {
         // Wait for a message to arrive from the GSP.
         let (slice_1, slice_2) = read_poll_timeout(
             || Ok(self.gsp_mem.driver_read_area()),
@@ -827,108 +927,27 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {
             return Err(EIO);
         }
 
-        self.parse_rpc_message(GspMessage {
+        Ok(GspMessage {
             header,
             contents: (slice_1, slice_2),
         })
     }
 
-    /// Validate the RPC layer of `element` and returns its RPC header and its contents trimmed down
-    /// to the RPC payload.
+    /// Wait for the next element on the message queue, pass it to [`MessageElement::read`], and
+    /// advances the read pointer past it.
+    ///
+    /// The read pointer advances regardless of whether [`MessageElement::read`] succeeds or not.
     ///
     /// # Errors
     ///
-    /// - `EIO` if the element is shorter than the payload length advertised by the RPC header.
-    fn parse_rpc_message<'a>(&self, element: GspMessage<'a>) -> Result<RpcMessage<'a>> {
-        let GspMessage {
-            header,
-            contents: (slice_1, slice_2),
-        } = element;
+    /// Errors from [`Self::wait_for_element`] and from [`MessageElement::read`] are propagated
+    /// as-is.
+    fn consume_element<M: MessageElement>(&mut self, timeout: Delta) -> Result<M> {
+        let message = self.wait_for_element(timeout)?;
+        let elem_count = u32::try_from(message.header.length().div_ceil(GSP_PAGE_SIZE))?;
+        let result = M::read(self.dev, message);
 
-        let rpc_header = header.rpc_header();
-        let payload_length = rpc_header.length();
-
-        dev_dbg!(
-            &self.dev,
-            "GSP RPC: receive: seq# {}, function={:?}, length=0x{:x}\n",
-            rpc_header.sequence(),
-            rpc_header.function(),
-            payload_length,
-        );
-
-        // Check that the driver read area is large enough for the message.
-        if slice_1.len() + slice_2.len() < payload_length {
-            return Err(EIO);
-        }
-
-        // Cut the message slices down to the actual length of the message.
-        let (slice_1, slice_2) = if slice_1.len() > payload_length {
-            // PANIC: we checked above that `slice_1` is at least as long as `payload_length`.
-            (slice_1.split_at(payload_length).0, &slice_2[0..0])
-        } else {
-            (
-                slice_1,
-                // PANIC: we checked above that `slice_1.len() + slice_2.len()` is at least as
-                // large as `payload_length`.
-                slice_2.split_at(payload_length - slice_1.len()).0,
-            )
-        };
-
-        Ok(RpcMessage {
-            header: rpc_header,
-            contents: (slice_1, slice_2),
-        })
-    }
-
-    /// Receive a message from the GSP.
-    ///
-    /// The expected message type is specified using the `M` generic parameter. If the pending
-    /// message has a different function code, `ERANGE` is returned and the message is consumed.
-    ///
-    /// The read pointer is always advanced past the message, regardless of whether it matched.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if `timeout` has elapsed before any message becomes available.
-    /// - `EIO` if there was some inconsistency (e.g. message shorter than advertised) on the
-    ///   message queue.
-    /// - `EINVAL` if the function code of the message was not recognized.
-    /// - `ERANGE` if the message had a recognized but non-matching function code.
-    ///
-    /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
-    fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
-    where
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
-        let message = self.wait_for_msg(timeout)?;
-        let function = message.header.function().map_err(|_| EINVAL)?;
-
-        // 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 mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
-
-            M::read(cmd, &mut sbuffer)
-                .map_err(|e| e.into())
-                .inspect(|_| {
-                    if !sbuffer.is_empty() {
-                        dev_warn!(
-                            &self.dev,
-                            "GSP message {:?} has unprocessed data\n",
-                            function
-                        );
-                    }
-                })
-        } else {
-            Err(ERANGE)
-        };
-
-        // Advance the read pointer past this message.
-        self.gsp_mem.advance_cpu_read_ptr(u32::try_from(
-            message.header.length().div_ceil(GSP_PAGE_SIZE),
-        )?);
+        self.gsp_mem.advance_cpu_read_ptr(elem_count);
 
         result
     }
Signature · 1 line
2.55.0
#12 lore raw

Re: [PATCH v3 6/9] gpu: nova-core: gsp: cmdq: split the transport part of the receive path

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] nova-core: ring buffer desynchronization via unvalidated element count --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] nova-core: ring buffer desynchronization via unvalidated element count --

commit 1b11098df54520332b501e33775803bd797ce2f8
Author: Alexandre Courbot <[email protected]>

gpu: nova-core: gsp: cmdq: split the transport part of the receive path

wait_for_msg mixes two layers: the transport layer which polls the queue, extracts the element header and validates the checksum, and the RPC layer which reads the RPC header and trims the payload slices to the length advertised by the RPC header.

Move the transport layer into wait_for_element, and introduce consume_element, a transport-level method which reads the message's contents using an implementation of the MessageElement trait before advancing the CPU read pointer past it, and parse_rpc_message, which validates the RPC layer. This sets things up for moving the RPC code into its own module, leaving the transport agnostic of the message type.

Quoted 14 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
index b8a9e02b76fe5..07036972dbecf 100644
@@ -805,7 +905,7 @@ fn send_command<M>(&mut self, command: M) -> Result
     ///   message queue.
     ///
     /// Error codes returned by the message constructor are propagated as-is.
-    fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {
+    fn wait_for_element(&self, timeout: Delta) -> Result<GspMessage<'_>> {
         // Wait for a message to arrive from the GSP.
         let (slice_1, slice_2) = read_poll_timeout(
             || Ok(self.gsp_mem.driver_read_area()),

drivers/gpu/nova-core/gsp/cmdq.rs fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> { · line 934

Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
@@ -827,108 +927,27 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {
             return Err(EIO);
         }
 
-        self.parse_rpc_message(GspMessage {
+        Ok(GspMessage {
             header,
             contents: (slice_1, slice_2),
         })
     }

[Severity: High]
Does this code allow a message with a maliciously large length to pass validation?

Looking at wait_for_element() which validates the checksum using .take():

nova-core/gsp/cmdq.rs:wait_for_element() {
    ...
    // Validate checksum after truncating the message to its exact length.
    if Cmdq::calculate_checksum(
        SBufferIter::new_reader([header.as_bytes(), slice_1, slice_2]).take(header.length()),
    ) != 0
    ...
}

Because .take() silently stops when the iterator is exhausted, this allows a message with a maliciously large length to pass validation if the truncated checksum matches.

Since the RPC bounds check has been moved out of wait_for_element() and into M::read(), the length check is deferred until it is too late to prevent pointer advancement in consume_element().

[ ... ]

Quoted 30 lines
drivers/gpu/nova-core/gsp/cmdq.rs +2 -28
-    fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
-    where
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
-        let message = self.wait_for_msg(timeout)?;
-        let function = message.header.function().map_err(|_| EINVAL)?;
-
-        // 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 mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
-
-            M::read(cmd, &mut sbuffer)
-                .map_err(|e| e.into())
-                .inspect(|_| {
-                    if !sbuffer.is_empty() {
-                        dev_warn!(
-                            &self.dev,
-                            "GSP message {:?} has unprocessed data\n",
-                            function
-                        );
-                    }
-                })
-        } else {
-            Err(ERANGE)
-        };
+    /// Errors from [`Self::wait_for_element`] and from [`MessageElement::read`] are propagated
+    /// as-is.

drivers/gpu/nova-core/gsp/cmdq.rs fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> {

Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +5 -4
+    fn consume_element<M: MessageElement>(&mut self, timeout: Delta) -> Result<M> {
+        let message = self.wait_for_element(timeout)?;
+        let elem_count = u32::try_from(message.header.length().div_ceil(GSP_PAGE_SIZE))?;
+        let result = M::read(self.dev, message);
 
-        // Advance the read pointer past this message.
-        self.gsp_mem.advance_cpu_read_ptr(u32::try_from(
-            message.header.length().div_ceil(GSP_PAGE_SIZE),
-        )?);
+        self.gsp_mem.advance_cpu_read_ptr(elem_count);

[Severity: High]
Can this corrupt the ring buffer state by advancing the read pointer even if M::read() fails bounds checking?

In consume_element(), the driver computes the queue advancement elem_count using message.header.length() (which retrieves the length from the unvalidated RPC header).

If the GSP firmware sends a massive or padded length, M::read() correctly bounds-checks and rejects it, but consume_element() temporarily ignores this failure and unconditionally advances the CPU read pointer by the unvalidated, potentially massive elem_count.

Recomputing elem_count via div_ceil instead of using the firmware-provided transport field message.header.element_count() guarantees desynchronization if the GSP ever pads messages.

Can this be exploited by a malfunctioning or compromised GSP firmware to irrevocably corrupt the shared queue?

Quoted 3 lines
drivers/gpu/nova-core/gsp/cmdq.rs +0 -0
         result
     }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#13 lore raw

Re: [PATCH v3 6/9] gpu: nova-core: gsp: cmdq: split the transport part of the receive path

In reply to Alexandre Courbot
Quoted 51 lines · Alexandre Courbot

wait_for_msg mixes two layers: the transport layer which polls the queue, extracts the element header and validates the checksum, and the RPC layer which reads the RPC header and trims the payload slices to the length advertised by the RPC header.

Move the transport layer into wait_for_element, and introduce consume_element, a transport-level method which reads the message's contents using an implementation of the MessageElement trait before advancing the CPU read pointer past it, and parse_rpc_message, which validates the RPC layer. This sets things up for moving the RPC code into its own module, leaving the transport agnostic of the message type.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 219 +++++++++++++++++++++-----------------
 1 file changed, 119 insertions(+), 100 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +19 -0
index b8a9e02b76fe..07036972dbec 100644
@@ -195,6 +195,15 @@ fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Res
     }
 }
 
+/// Trait implemented by types that can be received as single command queue elements.
+///
+/// The command queue validates the element header before calling `read()` to interpret the
+/// contents.
+trait MessageElement: Sized {
+    /// Tries to read `Self` from `element`. `dev` is the queue's device, to be used for logging.
+    fn read(dev: &device::Device, element: GspMessage<'_>) -> Result<Self>;
+}
+
 /// Trait representing messages received from the GSP.
 ///
 /// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.
@@ -218,6 +227,94 @@ fn read(
     ) -> Result<Self, Self::InitError>;
 }
 
+/// Wrapper type for receiving a RPC message from a command queue element.
+///
+/// [`MessageElement`] cannot be directly implemented for all [`MessageFromGsp`] with a blanket
+/// implementation as it would conflict with other future message types.
+struct RpcMessageElement<M>(M);
+
+impl<M> RpcMessageElement<M>
+where
+    M: MessageFromGsp,
+{

drivers/gpu/nova-core/gsp/cmdq.rs fn read( · line 249

Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +10 -0
+    /// Validate the RPC layer of `element` and returns its RPC header and its contents trimmed down
+    /// to the RPC payload.
+    ///
+    /// # Errors
+    ///
+    /// - `EIO` if the element is shorter than the payload length advertised by the RPC header.
+    fn parse_rpc_message<'a>(
+        dev: &device::Device,
+        element: GspMessage<'a>,
+    ) -> Result<RpcMessage<'a>> {

This doesn't depend on the type M, so it could go on RpcMessage instead.

Also, moving this here breaks some doclinks from other locations (e.g. This is the type returned by [CmdqInner::parse_rpc_message].). Can you fix please?

[...]

Quoted 42 lines
drivers/gpu/nova-core/gsp/cmdq.rs +0 -42
-    /// Receive a message from the GSP.
-    ///
-    /// The expected message type is specified using the `M` generic parameter. If the pending
-    /// message has a different function code, `ERANGE` is returned and the message is consumed.
-    ///
-    /// The read pointer is always advanced past the message, regardless of whether it matched.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if `timeout` has elapsed before any message becomes available.
-    /// - `EIO` if there was some inconsistency (e.g. message shorter than advertised) on the
-    ///   message queue.
-    /// - `EINVAL` if the function code of the message was not recognized.
-    /// - `ERANGE` if the message had a recognized but non-matching function code.
-    ///
-    /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
-    fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
-    where
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
-        let message = self.wait_for_msg(timeout)?;
-        let function = message.header.function().map_err(|_| EINVAL)?;
-
-        // 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 mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
-
-            M::read(cmd, &mut sbuffer)
-                .map_err(|e| e.into())
-                .inspect(|_| {
-                    if !sbuffer.is_empty() {
-                        dev_warn!(
-                            &self.dev,
-                            "GSP message {:?} has unprocessed data\n",
-                            function
-                        );
-                    }
-                })
-        } else {

drivers/gpu/nova-core/gsp/cmdq.rs fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> { · line 952

Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -7
-            Err(ERANGE)
-        };
-
-        // Advance the read pointer past this message.
-        self.gsp_mem.advance_cpu_read_ptr(u32::try_from(
-            message.header.length().div_ceil(GSP_PAGE_SIZE),
-        )?);
+        self.gsp_mem.advance_cpu_read_ptr(elem_count);
 
         result

Previously, if we got an unknown function code or the message was too short for MessageFromGsp::Message or the payload length is too big for the remaining read area, it wouldn't consume the element, but now it does. If we had corrupted data that happened to pass checksum, it could mess up the queue (e.g. wrap the read pointer around in front of the write pointer).

Since this nests transport, message layer (RPC here), and content layer, it might be worth saying how each should be handled. Here's the previous
+ semantics with this patch:

Transport:
- Timeout, ETIMEDOUT -> no change
- Bad checksum, EIO, not consumed -> no change

Message:
- Unknown function code, EINVAL: message consumed in this patch

If we get an unknown function code, we can't know if things are still in a valid state, so I think we should not consume the message and return an error here.

- Known but unexpected function code, ERANGE, consumed -> no change

Think we have this since we don't have async GSP message handling implemented yet so we use this to drain the cmdq of misc messages. So all good here. N.B. we are implicitly relying on the discriminants in MsgFunction essentially being an allowlist for events we can drain, otherwise we hit the case above (in the code previous to this patch, at least).

- slice_1.len() + slice_2.len() < payload_length hits, EIO, consumed in this patch

This will mess up the read pointer.

Content:
- Payload shorter than MessageFromGsp::Message, consumed in this patch

This is another weird scenario that shouldn't happen. Arguably we shouldn't consume the message here, but this patch changes that behaviour.

- MessageFromGsp::read fails, consumed -> no change

Not sure, but seems a bit weird to consume this here.

- Payload not fully read, warning+Ok -> no change

We could solve this with a custom error type for MessageElement, or return Result<Result<Self>> -> the Result<Result<Self>> is arguable since we are returning the result of the content layer.

Send path semantics look unaffected by this series to me.

Quoted 1 line
     }
author maintainer
#14 lore raw

Re: [PATCH v3 6/9] gpu: nova-core: gsp: cmdq: split the transport part of the receive path

On Thu Oct 1, 2026 at 1:48 PM JST, Eliot Courtney wrote:
<...>

Quoted 24 lines
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +10 -0
+/// Wrapper type for receiving a RPC message from a command queue element.
+///
+/// [`MessageElement`] cannot be directly implemented for all [`MessageFromGsp`] with a blanket
+/// implementation as it would conflict with other future message types.
+struct RpcMessageElement<M>(M);
+
+impl<M> RpcMessageElement<M>
+where
+    M: MessageFromGsp,
+{

drivers/gpu/nova-core/gsp/cmdq.rs fn read( · line 249

Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +10 -0
+    /// Validate the RPC layer of `element` and returns its RPC header and its contents trimmed down
+    /// to the RPC payload.
+    ///
+    /// # Errors
+    ///
+    /// - `EIO` if the element is shorter than the payload length advertised by the RPC header.
+    fn parse_rpc_message<'a>(
+        dev: &device::Device,
+        element: GspMessage<'a>,
+    ) -> Result<RpcMessage<'a>> {

This doesn't depend on the type M, so it could go on RpcMessage instead.

Quoted 3 lines

Also, moving this here breaks some doclinks from other locations (e.g. This is the type returned by [CmdqInner::parse_rpc_message].). Can you fix please?

Done and fixed, thanks!

Quoted 102 lines

[...]

Quoted 42 lines
drivers/gpu/nova-core/gsp/cmdq.rs +0 -42
-    /// Receive a message from the GSP.
-    ///
-    /// The expected message type is specified using the `M` generic parameter. If the pending
-    /// message has a different function code, `ERANGE` is returned and the message is consumed.
-    ///
-    /// The read pointer is always advanced past the message, regardless of whether it matched.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if `timeout` has elapsed before any message becomes available.
-    /// - `EIO` if there was some inconsistency (e.g. message shorter than advertised) on the
-    ///   message queue.
-    /// - `EINVAL` if the function code of the message was not recognized.
-    /// - `ERANGE` if the message had a recognized but non-matching function code.
-    ///
-    /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
-    fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
-    where
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
-        let message = self.wait_for_msg(timeout)?;
-        let function = message.header.function().map_err(|_| EINVAL)?;
-
-        // 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 mut sbuffer = SBufferIter::new_reader([contents_1, message.contents.1]);
-
-            M::read(cmd, &mut sbuffer)
-                .map_err(|e| e.into())
-                .inspect(|_| {
-                    if !sbuffer.is_empty() {
-                        dev_warn!(
-                            &self.dev,
-                            "GSP message {:?} has unprocessed data\n",
-                            function
-                        );
-                    }
-                })
-        } else {

drivers/gpu/nova-core/gsp/cmdq.rs fn wait_for_msg(&self, timeout: Delta) -> Result<RpcMessage<'_>> { · line 952

Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -7
-            Err(ERANGE)
-        };
-
-        // Advance the read pointer past this message.
-        self.gsp_mem.advance_cpu_read_ptr(u32::try_from(
-            message.header.length().div_ceil(GSP_PAGE_SIZE),
-        )?);
+        self.gsp_mem.advance_cpu_read_ptr(elem_count);
 
         result

Previously, if we got an unknown function code or the message was too short for MessageFromGsp::Message or the payload length is too big for the remaining read area, it wouldn't consume the element, but now it does. If we had corrupted data that happened to pass checksum, it could mess up the queue (e.g. wrap the read pointer around in front of the write pointer).

Since this nests transport, message layer (RPC here), and content layer, it might be worth saying how each should be handled. Here's the previous
+ semantics with this patch:

Transport:
- Timeout, ETIMEDOUT -> no change
- Bad checksum, EIO, not consumed -> no change

Message:
- Unknown function code, EINVAL: message consumed in this patch

If we get an unknown function code, we can't know if things are still in a valid state, so I think we should not consume the message and return an error here.

- Known but unexpected function code, ERANGE, consumed -> no change

Think we have this since we don't have async GSP message handling implemented yet so we use this to drain the cmdq of misc messages. So all good here. N.B. we are implicitly relying on the discriminants in MsgFunction essentially being an allowlist for events we can drain, otherwise we hit the case above (in the code previous to this patch, at least).

- slice_1.len() + slice_2.len() < payload_length hits, EIO, consumed in this patch

This will mess up the read pointer.

Content:
- Payload shorter than MessageFromGsp::Message, consumed in this patch

This is another weird scenario that shouldn't happen. Arguably we shouldn't consume the message here, but this patch changes that behaviour.

- MessageFromGsp::read fails, consumed -> no change

Not sure, but seems a bit weird to consume this here.

Quoted 7 lines

- Payload not fully read, warning+Ok -> no change

We could solve this with a custom error type for MessageElement, or return Result<Result<Self>> -> the Result<Result<Self>> is arguable since we are returning the result of the content layer.

Send path semantics look unaffected by this series to me.

The rebase on top of the interrupt series (which changed the semantics on the receive end) should make the next revision less impactful on that front, although messages are consumed even if they have an unexpected size because the RpcMessage::parse call is now performed inside the MessageElement::read implementation, so the transport layer cannot tell the difference between an invalid message length and other kinds of errors.

I would not worry about this detail though, because 1. r000 will put the message size into the transport layer, removing that issue entirely, and
2. the correct thing to do (which John tackled in his r000 series) is to mark the command queue as invalid by e.g. setting a poisoned flag and making further uses of the queue return an error, as such mismatch would be a firmware bug and thus not a recoverable situation.

author maintainer
#15 lore raw

PATCH v3 7/9 gpu: nova-core: gsp: cmdq: move the RPC code into a sub-module

In reply to Alexandre Courbot

Move the types and code related to sending and receiving RPC commands and messages into the rpc sub-module. The methods are still declared as members of CmdqInner and Cmdq; only they are now in their own sub-module.

This is a pure move commit, with no functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs                  | 343 +------------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 380 +++++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              |   6 +-
 drivers/gpu/nova-core/gsp/sequencer.rs             |   4 +-
 5 files changed, 388 insertions(+), 345 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +2 -341
index 07036972dbec..06386d1ab192 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 
-mod continuation;
+pub(crate) mod rpc;
 
 use core::mem;
 
@@ -35,8 +36,6 @@
     },
 };
 
-use continuation::SplitState;
-
 use pin_init::pin_init_scope;
 
 use crate::{
@@ -44,10 +43,8 @@
     gsp::{
         fw::{
             GspMsgElement,
-            MsgFunction,
             MsgqRxHeader,
             MsgqTxHeader,
-            RpcMessageHeader,
             GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, //
         },
         PteArray,
@@ -77,124 +74,6 @@ trait CommandElement {
     fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result;
 }
 
-/// Trait implemented by types representing a command to send to the GSP.
-///
-/// The main purpose of this trait is to provide [`Cmdq`] with the information it needs to send
-/// a given command.
-///
-/// [`CommandToGsp::init`] in particular is responsible for initializing the command directly
-/// into the space reserved for it in the command queue buffer.
-///
-/// Some commands may be followed by a variable-length payload. For these, the
-/// [`CommandToGsp::variable_payload_len`] and [`CommandToGsp::init_variable_payload`] need to be
-/// defined as well.
-pub(crate) trait CommandToGsp {
-    /// Function identifying this command to the GSP.
-    const FUNCTION: MsgFunction;
-
-    /// Type generated by [`CommandToGsp::init`], to be written into the command queue buffer.
-    type Command: FromBytes + AsBytes;
-
-    /// Type of the reply expected from the GSP, or [`NoReply`] for commands that don't
-    /// have a reply.
-    type Reply;
-
-    /// Error type returned by [`CommandToGsp::init`].
-    type InitError;
-
-    /// In-place command initializer responsible for filling the command in the command queue
-    /// buffer.
-    fn init(&self) -> impl Init<Self::Command, Self::InitError>;
-
-    /// Size of the variable-length payload following the command structure generated by
-    /// [`CommandToGsp::init`].
-    ///
-    /// Most commands don't have a variable-length payload, so this is zero by default.
-    fn variable_payload_len(&self) -> usize {
-        0
-    }
-
-    /// Method initializing the variable-length payload.
-    ///
-    /// The command buffer is circular, which means that we may need to jump back to its beginning
-    /// while in the middle of a command. For this reason, the variable-length payload is
-    /// initialized using a [`SBufferIter`].
-    ///
-    /// This method will receive a buffer of the length returned by
-    /// [`CommandToGsp::variable_payload_len`], and must write every single byte of it. Leaving
-    /// unwritten space will lead to an error.
-    ///
-    /// Most commands don't have a variable-length payload, so this does nothing by default.
-    fn init_variable_payload(
-        &self,
-        _dst: &mut SBufferIter<core::array::IntoIter<&mut [u8], 2>>,
-    ) -> Result {
-        Ok(())
-    }
-
-    /// Total size of the command (including its variable-length payload) without the
-    /// [`GspMsgElement`] header.
-    fn size(&self) -> usize {
-        size_of::<Self::Command>() + self.variable_payload_len()
-    }
-}
-
-/// Wrapper type for sending a RPC command as a command queue element.
-///
-/// [`CommandElement`] cannot be directly implemented for all [`CommandToGsp`] with a blanket
-/// implementation as it would conflict with other future command types.
-struct RpcCommandElement<M>(M);
-
-impl<M> CommandElement for RpcCommandElement<M>
-where
-    M: CommandToGsp,
-    Error: From<M::InitError>,
-{
-    fn size(&self) -> usize {
-        self.0.size()
-    }
-
-    fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Result {
-        let command = &self.0;
-        let size_in_bytes = command.size();
-        // 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 rpc_header_init = RpcMessageHeader::init(size_in_bytes, M::FUNCTION);
-        // SAFETY: `dst.header.rpc_header_mut()` is a valid reference, and is not touched if the
-        // initializer fails.
-        unsafe {
-            pin_init::raw_try_init(
-                core::ptr::from_mut(dst.header.rpc_header_mut()),
-                rpc_header_init,
-            )?;
-        }
-        // SAFETY: `cmd` is a valid reference, and is not touched if the initializer fails.
-        unsafe {
-            pin_init::raw_try_init(core::ptr::from_mut(cmd), command.init())?;
-        }
-
-        // Fill the variable-length payload, which may be empty.
-        let mut sbuffer = SBufferIter::new_writer([&mut payload_1[..], &mut dst.contents.1[..]]);
-        command.init_variable_payload(&mut sbuffer)?;
-
-        if !sbuffer.is_empty() {
-            return Err(EIO);
-        }
-
-        dev_dbg!(
-            dev,
-            "GSP RPC: send: seq# {}, function={:?}, length=0x{:x}\n",
-            seq,
-            M::FUNCTION,
-            size_in_bytes,
-        );
-
-        Ok(())
-    }
-}
-
 /// Trait implemented by types that can be received as single command queue elements.
 ///
 /// The command queue validates the element header before calling `read()` to interpret the
@@ -204,117 +83,6 @@ trait MessageElement: Sized {
     fn read(dev: &device::Device, element: GspMessage<'_>) -> Result<Self>;
 }
 
-/// Trait representing messages received from the GSP.
-///
-/// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.
-pub(crate) trait MessageFromGsp: Sized {
-    /// Function identifying this message from the GSP.
-    const FUNCTION: MsgFunction;
-
-    /// Error type returned by [`MessageFromGsp::read`].
-    type InitError;
-
-    /// Type containing the raw message to be read from the message queue.
-    type Message: FromBytes;
-
-    /// Method reading the message from the message queue and returning it.
-    ///
-    /// From a `Self::Message` and a [`SBufferIter`], constructs an instance of `Self` and returns
-    /// it.
-    fn read(
-        msg: &Self::Message,
-        sbuffer: &mut SBufferIter<core::array::IntoIter<&[u8], 2>>,
-    ) -> Result<Self, Self::InitError>;
-}
-
-/// Wrapper type for receiving a RPC message from a command queue element.
-///
-/// [`MessageElement`] cannot be directly implemented for all [`MessageFromGsp`] with a blanket
-/// implementation as it would conflict with other future message types.
-struct RpcMessageElement<M>(M);
-
-impl<M> RpcMessageElement<M>
-where
-    M: MessageFromGsp,
-{
-    /// Validate the RPC layer of `element` and returns its RPC header and its contents trimmed down
-    /// to the RPC payload.
-    ///
-    /// # Errors
-    ///
-    /// - `EIO` if the element is shorter than the payload length advertised by the RPC header.
-    fn parse_rpc_message<'a>(
-        dev: &device::Device,
-        element: GspMessage<'a>,
-    ) -> Result<RpcMessage<'a>> {
-        let GspMessage {
-            header,
-            contents: (slice_1, slice_2),
-        } = element;
-
-        let rpc_header = header.rpc_header();
-        let payload_length = rpc_header.length();
-
-        dev_dbg!(
-            dev,
-            "GSP RPC: receive: seq# {}, function={:?}, length=0x{:x}\n",
-            rpc_header.sequence(),
-            rpc_header.function(),
-            payload_length,
-        );
-
-        // Check that the element is large enough for the message.
-        if slice_1.len() + slice_2.len() < payload_length {
-            return Err(EIO);
-        }
-
-        // Cut the message slices down to the actual length of the message.
-        let (slice_1, slice_2) = if slice_1.len() > payload_length {
-            // PANIC: we checked above that `slice_1` is at least as long as `payload_length`.
-            (slice_1.split_at(payload_length).0, &slice_2[0..0])
-        } else {
-            (
-                slice_1,
-                // PANIC: we checked above that `slice_1.len() + slice_2.len()` is at least as
-                // large as `payload_length`.
-                slice_2.split_at(payload_length - slice_1.len()).0,
-            )
-        };
-
-        Ok(RpcMessage {
-            header: rpc_header,
-            contents: (slice_1, slice_2),
-        })
-    }
-}
-
-impl<M> MessageElement for RpcMessageElement<M>
-where
-    M: MessageFromGsp,
-    Error: From<M::InitError>,
-{
-    fn read(dev: &device::Device, element: GspMessage<'_>) -> Result<Self> {
-        let message = Self::parse_rpc_message(dev, element)?;
-        let function = message.header.function().map_err(|_| EINVAL)?;
-
-        if function != M::FUNCTION {
-            return Err(ERANGE);
-        }
-
-        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(Self)
-            .map_err(|e| e.into())
-            .inspect(|_| {
-                if !sbuffer.is_empty() {
-                    dev_warn!(dev, "GSP message {:?} has unprocessed data\n", function);
-                }
-            })
-    }
-}
-
 /// Number of GSP pages making the [`Msgq`].
 pub(crate) const MSGQ_NUM_PAGES: u32 = 0x3f;
 
@@ -650,17 +418,6 @@ struct GspMessage<'a> {
     contents: (&'a [u8], &'a [u8]),
 }
 
-/// A RPC message ready to be processed from the message queue.
-///
-/// This is the type returned by [`CmdqInner::parse_rpc_message`].
-struct RpcMessage<'a> {
-    // Reference to the RPC header of the message.
-    header: &'a RpcMessageHeader,
-    // Slices to the contents of the message. The second slice is zero unless the message loops
-    // over the message queue.
-    contents: (&'a [u8], &'a [u8]),
-}
-
 /// GSP command queue.
 ///
 /// Provides the ability to send commands and receive messages from the GSP using a shared memory
@@ -731,72 +488,6 @@ fn calculate_checksum<T: Iterator<Item = u8>>(it: T) -> u32 {
     fn notify_gsp(bar: Bar0<'_>) {
         bar.write_reg(regs::NV_PGSP_QUEUE_HEAD::zeroed().with_address(0u32));
     }
-
-    /// Sends `command` to the GSP and waits for the reply.
-    ///
-    /// Messages with non-matching function codes are silently consumed until the expected reply
-    /// arrives.
-    ///
-    /// The queue is locked for the entire send+receive cycle to ensure that no other command can
-    /// be interleaved.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if space does not become available to send the command, or if the reply is
-    ///   not received within the timeout.
-    /// - `EIO` if the variable payload requested by the command has not been entirely
-    ///   written to by its [`CommandToGsp::init_variable_payload`] method.
-    ///
-    /// Error codes returned by the command and reply initializers are propagated as-is.
-    pub(crate) fn send_command<M>(&self, command: M) -> Result<M::Reply>
-    where
-        M: CommandToGsp,
-        M::Reply: MessageFromGsp,
-        Error: From<M::InitError>,
-        Error: From<<M::Reply as MessageFromGsp>::InitError>,
-    {
-        let mut inner = self.inner.lock();
-        inner.send_command(command)?;
-
-        loop {
-            match inner.consume_element::<RpcMessageElement<M::Reply>>(Self::RECEIVE_TIMEOUT) {
-                Ok(reply) => break Ok(reply.0),
-                Err(ERANGE) => continue,
-                Err(e) => break Err(e),
-            }
-        }
-    }
-
-    /// Sends `command` to the GSP without waiting for a reply.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if space does not become available within the timeout.
-    /// - `EIO` if the variable payload requested by the command has not been entirely
-    ///   written to by its [`CommandToGsp::init_variable_payload`] method.
-    ///
-    /// Error codes returned by the command initializers are propagated as-is.
-    pub(crate) fn send_command_no_wait<M>(&self, command: M) -> Result
-    where
-        M: CommandToGsp<Reply = NoReply>,
-        Error: From<M::InitError>,
-    {
-        self.inner.lock().send_command(command)
-    }
-
-    /// Receive a message from the GSP.
-    ///
-    /// See [`CmdqInner::consume_element`] for details.
-    pub(crate) fn receive_msg<M: MessageFromGsp>(&self, timeout: Delta) -> Result<M>
-    where
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
-        self.inner
-            .lock()
-            .consume_element::<RpcMessageElement<M>>(timeout)
-            .map(|m| m.0)
-    }
 }
 
 /// Inner mutex protected state of [`Cmdq`].
@@ -857,36 +548,6 @@ fn send_command_element<E: CommandElement>(&mut self, command: E) -> Result {
         Ok(())
     }
 
-    /// Sends `command` to the GSP.
-    ///
-    /// The command may be split into multiple messages if it is large.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if space does not become available within the timeout.
-    /// - `EIO` if the variable payload requested by the command has not been entirely
-    ///   written to by its [`CommandToGsp::init_variable_payload`] method.
-    ///
-    /// Error codes returned by the command initializers are propagated as-is.
-    fn send_command<M>(&mut self, command: M) -> Result
-    where
-        M: CommandToGsp,
-        Error: From<M::InitError>,
-    {
-        match SplitState::new(command)? {
-            SplitState::Single(command) => self.send_command_element(RpcCommandElement(command)),
-            SplitState::Split(command, mut continuations) => {
-                self.send_command_element(RpcCommandElement(command))?;
-
-                while let Some(continuation) = continuations.next() {
-                    self.send_command_element(RpcCommandElement(continuation))?;
-                }
-
-                Ok(())
-            }
-        }
-    }
-
     /// Wait for the next element to become available on the message queue.
     ///
     /// This works purely at the transport layer and does not interpret or validate the message
drivers/gpu/nova-core/gsp/cmdq/rpc.rs new file +380 -0

380 lines not on this page · raw message

drivers/gpu/nova-core/gsp/cmdq/rpc/continuation.rs renamed from drivers/gpu/nova-core/gsp/cmdq/continuation.rs +0 -0
similarity index 100%
rename from drivers/gpu/nova-core/gsp/cmdq/continuation.rs
rename to drivers/gpu/nova-core/gsp/cmdq/rpc/continuation.rs
drivers/gpu/nova-core/gsp/commands.rs +4 -2
index 59d7d7fb15e8..25a5a8d33d64 100644
@@ -23,9 +23,11 @@
     gpu::Chipset,
     gsp::{
         cmdq::{
+            rpc::{
+                CommandToGsp,
+                MessageFromGsp, //
+            },
             Cmdq,
-            CommandToGsp,
-            MessageFromGsp,
             NoReply, //
         },
         fw::{
drivers/gpu/nova-core/gsp/sequencer.rs +2 -2
index dae34c11eb05..ebf13867746f 100644
@@ -28,8 +28,8 @@
     },
     gsp::{
         cmdq::{
-            Cmdq,
-            MessageFromGsp, //
+            rpc::MessageFromGsp,
+            Cmdq, //
         },
         fw,
         GspBootContext,
Signature · 1 line
2.55.0
Reviewed-by on v3 7/9
#16 lore raw

Re: [PATCH v3 7/9] gpu: nova-core: gsp: cmdq: move the RPC code into a sub-module

Quoted 21 lines · Alexandre Courbot

Move the types and code related to sending and receiving RPC commands and messages into the rpc sub-module. The methods are still declared as members of CmdqInner and Cmdq; only they are now in their own sub-module.

This is a pure move commit, with no functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs                  | 343 +------------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 380 +++++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              |   6 +-
 drivers/gpu/nova-core/gsp/sequencer.rs             |   4 +-
 5 files changed, 388 insertions(+), 345 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +0 -0
index 07036972dbec..06386d1ab192 100644

drivers/gpu/nova-core/gsp/cmdq.rs line 4

Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs +2 -1
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 
-mod continuation;
+pub(crate) mod rpc;

Should this be pub(super)?

I tried to think of a way we can have a only generic (over RPC and GMC) receive/send path on cmdq. I think it's possible but I don't think we need to do it right now. It'll require some care and maybe information from the future w.r.t. how we handle async events etc, so maybe let's revisit later.

author maintainer
#17 lore raw

Re: [PATCH v3 7/9] gpu: nova-core: gsp: cmdq: move the RPC code into a sub-module

Quoted 30 lines · Eliot Courtney
Quoted 21 lines · Alexandre Courbot

Move the types and code related to sending and receiving RPC commands and messages into the rpc sub-module. The methods are still declared as members of CmdqInner and Cmdq; only they are now in their own sub-module.

This is a pure move commit, with no functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs                  | 343 +------------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 380 +++++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              |   6 +-
 drivers/gpu/nova-core/gsp/sequencer.rs             |   4 +-
 5 files changed, 388 insertions(+), 345 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +0 -0
index 07036972dbec..06386d1ab192 100644

drivers/gpu/nova-core/gsp/cmdq.rs line 4

Quoted 5 lines
drivers/gpu/nova-core/gsp/cmdq.rs +2 -1
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 
-mod continuation;
+pub(crate) mod rpc;

Should this be pub(super)?

Quoted 5 lines

I tried to think of a way we can have a only generic (over RPC and GMC) receive/send path on cmdq. I think it's possible but I don't think we need to do it right now. It'll require some care and maybe information from the future w.r.t. how we handle async events etc, so maybe let's revisit later.

Yeah, that's also something I was considering as I wrote this, but I also don't want to paint ourselves into a corner before r000 support is merged. Since you seem to agree let's defer this to the point where we have more visibility on how we receive messages.

author maintainer
#18 lore raw

Re: [PATCH v3 7/9] gpu: nova-core: gsp: cmdq: move the RPC code into a sub-module

In reply to Eliot Courtney
Quoted 25 lines · Eliot Courtney
Quoted 24 lines · Alexandre Courbot

Move the types and code related to sending and receiving RPC commands and messages into the rpc sub-module. The methods are still declared as members of CmdqInner and Cmdq; only they are now in their own sub-module.

This is a pure move commit, with no functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs                  | 343 +------------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 380 +++++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              |   6 +-
 drivers/gpu/nova-core/gsp/sequencer.rs             |   4 +-
 5 files changed, 388 insertions(+), 345 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +1 -0
index 07036972dbec..06386d1ab192 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 
Quoted 4 lines

drivers/gpu/nova-core/gsp/cmdq.rs line 4

Quoted 2 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
-mod continuation;
+pub(crate) mod rpc;

Should this be pub(super)?

This cannot be unfortunately, as commands (which are in a sibling module of cmdq) are protocol-dependent and need to view CommandToGsp and MessageFromGsp.

author maintainer
#19 lore raw

Re: [PATCH v3 7/9] gpu: nova-core: gsp: cmdq: move the RPC code into a sub-module

Quoted 26 lines · Alexandre Courbot
Quoted 25 lines · Eliot Courtney
Quoted 24 lines · Alexandre Courbot

Move the types and code related to sending and receiving RPC commands and messages into the rpc sub-module. The methods are still declared as members of CmdqInner and Cmdq; only they are now in their own sub-module.

This is a pure move commit, with no functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs                  | 343 +------------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 380 +++++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              |   6 +-
 drivers/gpu/nova-core/gsp/sequencer.rs             |   4 +-
 5 files changed, 388 insertions(+), 345 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +1 -0
index 07036972dbec..06386d1ab192 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 
Quoted 8 lines
Quoted 4 lines

drivers/gpu/nova-core/gsp/cmdq.rs line 4

Quoted 2 lines
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
-mod continuation;
+pub(crate) mod rpc;

Should this be pub(super)?

This cannot be unfortunately, as commands (which are in a sibling module of cmdq) are protocol-dependent and need to view CommandToGsp and MessageFromGsp.

Err sorry, my reply was completely wrong - pub(super) here means visibility in all sub-modules of gsp, which is exactly what we need. Sorry for the noise, and I'll fix.

author maintainer
#20 lore raw

PATCH v3 8/9 gpu: nova-core: gsp: move the RPC commands into a sub-module

In reply to Alexandre Courbot

Move the types and code related to RPC commands into the rpc sub-module, and update their users to reference them from their new location.

This is a pure move commit, with no functional change intended.

---
 drivers/gpu/nova-core/api.rs              |   2 +-
 drivers/gpu/nova-core/gpu.rs              |   2 +-
 drivers/gpu/nova-core/gsp.rs              |   4 +-
 drivers/gpu/nova-core/gsp/boot.rs         |  12 +-
 drivers/gpu/nova-core/gsp/commands.rs     | 337 +----------------------------
 drivers/gpu/nova-core/gsp/commands/rpc.rs | 339 ++++++++++++++++++++++++++++++
 6 files changed, 350 insertions(+), 346 deletions(-)
drivers/gpu/nova-core/api.rs +1 -1
index f02c6c7c7e51..84a15265e849 100644
@@ -16,7 +16,7 @@
 pub use crate::gpu::Spec;
 
 use crate::gpu::Gpu;
-use crate::gsp::commands::GetGspStaticInfoReply;
+use crate::gsp::commands::rpc::GetGspStaticInfoReply;
 
 /// API handle for the auxiliary bus child drivers to interact with nova-core.
 pub struct NovaCoreApi<'bound> {
drivers/gpu/nova-core/gpu.rs +1 -1
index fb6f8a86a503..2a3bd619acf6 100644
@@ -34,7 +34,7 @@
     fsp::Fsp,
     gsp::{
         self,
-        commands::GetGspStaticInfoReply,
+        commands::rpc::GetGspStaticInfoReply,
         Gsp,
         GspBootContext, //
     },
drivers/gpu/nova-core/gsp.rs +2 -2
index dda58095f40b..75a3ba7d50f4 100644
@@ -220,8 +220,8 @@ pub(crate) fn new(
     }
 
     /// Query the GSP for the static GPU information.
-    pub(crate) fn get_static_info(&self) -> Result<commands::GetGspStaticInfoReply> {
-        self.cmdq.send_command(commands::GetGspStaticInfo)
+    pub(crate) fn get_static_info(&self) -> Result<commands::rpc::GetGspStaticInfoReply> {
+        self.cmdq.send_command(commands::rpc::GetGspStaticInfo)
     }
 }
 
drivers/gpu/nova-core/gsp/boot.rs +6 -6
index 4fb1b69ac9d5..8e446ad1eac2 100644
@@ -43,9 +43,9 @@ pub(crate) fn boot(
         let gsp_fw = KBox::pin_init(GspFirmware::new(dev, chipset), GFP_KERNEL)?;
 
         self.cmdq
-            .send_command_no_wait(commands::SetSystemInfo::new(pdev, chipset))?;
+            .send_command_no_wait(commands::rpc::SetSystemInfo::new(pdev, chipset))?;
         self.cmdq
-            .send_command_no_wait(commands::SetRegistry::new(ctx.vgpu.state())?)?;
+            .send_command_no_wait(commands::rpc::SetRegistry::new(ctx.vgpu.state())?)?;
 
         // Perform the chipset-specific boot sequence, and retrieve the unload bundle.
         let unload_bundle = hal.boot(&self, &mut ctx, &gsp_fw)?.or_else(|| {
@@ -79,7 +79,7 @@ pub(crate) fn boot(
         hal.post_boot(&self, ctx, &gsp_fw)?;
 
         // Wait until GSP is fully initialized.
-        commands::wait_gsp_init_done(&self.cmdq)?;
+        commands::rpc::wait_gsp_init_done(&self.cmdq)?;
 
         Ok(unload_guard.dismiss().1)
     }
@@ -88,10 +88,10 @@ pub(crate) fn boot(
     fn shutdown_gsp(
         cmdq: &Cmdq<'_>,
         gsp_falcon: &Falcon<'_, Gsp>,
-        mode: commands::PowerStateLevel,
+        mode: commands::rpc::PowerStateLevel,
     ) -> Result {
         // Command to shut the GSP down.
-        cmdq.send_command(commands::UnloadingGuestDriver::new(mode))?;
+        cmdq.send_command(commands::rpc::UnloadingGuestDriver::new(mode))?;
 
         // Wait until GSP signals it is suspended.
         const LIBOS_INTERRUPT_PROCESSOR_SUSPENDED: u32 = bits::bit_u32(31);
@@ -118,7 +118,7 @@ pub(crate) fn unload(
         let mut res = Self::shutdown_gsp(
             &self.cmdq,
             ctx.gsp_falcon,
-            commands::PowerStateLevel::Level0,
+            commands::rpc::PowerStateLevel::Level0,
         )
         .inspect_err(|e| dev_err!(dev, "GSP shutdown failed: {:?}\n", e));
 
drivers/gpu/nova-core/gsp/commands.rs +1 -336

340 lines not on this page · raw message

drivers/gpu/nova-core/gsp/commands/rpc.rs new file +339 -0

339 lines not on this page · raw message

Signature · 1 line
2.55.0
author maintainer
#21 lore raw

PATCH v3 9/9 gpu: nova-core: gsp: add `rpc` to RPC message send/receive methods

In reply to Alexandre Courbot

Since we are going to introduce a second type of message, add rpc to the names of the relevant command queue methods so we can distinguish between the two.

No functional change intended.

---
 drivers/gpu/nova-core/gsp.rs              |  2 +-
 drivers/gpu/nova-core/gsp/boot.rs         |  6 +++---
 drivers/gpu/nova-core/gsp/cmdq.rs         |  2 +-
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs     | 14 +++++++-------
 drivers/gpu/nova-core/gsp/commands/rpc.rs |  2 +-
 drivers/gpu/nova-core/gsp/sequencer.rs    |  2 +-
 6 files changed, 14 insertions(+), 14 deletions(-)
drivers/gpu/nova-core/gsp.rs +1 -1
index 75a3ba7d50f4..ed7eef21e8f9 100644
@@ -221,7 +221,7 @@ pub(crate) fn new(
 
     /// Query the GSP for the static GPU information.
     pub(crate) fn get_static_info(&self) -> Result<commands::rpc::GetGspStaticInfoReply> {
-        self.cmdq.send_command(commands::rpc::GetGspStaticInfo)
+        self.cmdq.send_rpc_command(commands::rpc::GetGspStaticInfo)
     }
 }
 
drivers/gpu/nova-core/gsp/boot.rs +3 -3
index 8e446ad1eac2..0d0b07fafd4f 100644
@@ -43,9 +43,9 @@ pub(crate) fn boot(
         let gsp_fw = KBox::pin_init(GspFirmware::new(dev, chipset), GFP_KERNEL)?;
 
         self.cmdq
-            .send_command_no_wait(commands::rpc::SetSystemInfo::new(pdev, chipset))?;
+            .send_rpc_command_no_wait(commands::rpc::SetSystemInfo::new(pdev, chipset))?;
         self.cmdq
-            .send_command_no_wait(commands::rpc::SetRegistry::new(ctx.vgpu.state())?)?;
+            .send_rpc_command_no_wait(commands::rpc::SetRegistry::new(ctx.vgpu.state())?)?;
 
         // Perform the chipset-specific boot sequence, and retrieve the unload bundle.
         let unload_bundle = hal.boot(&self, &mut ctx, &gsp_fw)?.or_else(|| {
@@ -91,7 +91,7 @@ fn shutdown_gsp(
         mode: commands::rpc::PowerStateLevel,
     ) -> Result {
         // Command to shut the GSP down.
-        cmdq.send_command(commands::rpc::UnloadingGuestDriver::new(mode))?;
+        cmdq.send_rpc_command(commands::rpc::UnloadingGuestDriver::new(mode))?;
 
         // Wait until GSP signals it is suspended.
         const LIBOS_INTERRUPT_PROCESSOR_SUSPENDED: u32 = bits::bit_u32(31);
drivers/gpu/nova-core/gsp/cmdq.rs +1 -1
index 06386d1ab192..17cbed8277f3 100644
@@ -58,7 +58,7 @@
 use super::regs;
 
 /// Marker type representing the absence of a reply for a command. Commands using this as their
-/// reply type are sent using [`Cmdq::send_command_no_wait`].
+/// reply type are sent using [`Cmdq::send_rpc_command_no_wait`].
 pub(crate) struct NoReply;
 
 /// Trait implemented by types that can be sent as a single command queue element.
drivers/gpu/nova-core/gsp/cmdq/rpc.rs +7 -7
index 7ec0fed76637..627616140162 100644
@@ -159,7 +159,7 @@ fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Res
 
 /// Trait representing messages received from the GSP.
 ///
-/// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message.
+/// This trait tells [`Cmdq::receive_rpc_msg`] how it can receive a given type of message.
 pub(crate) trait MessageFromGsp: Sized {
     /// Function identifying this message from the GSP.
     const FUNCTION: MsgFunction;
@@ -291,7 +291,7 @@ impl CmdqInner<'_> {
     ///   written to by its [`CommandToGsp::init_variable_payload`] method.
     ///
     /// Error codes returned by the command initializers are propagated as-is.
-    fn send_command<M>(&mut self, command: M) -> Result
+    fn send_rpc_command<M>(&mut self, command: M) -> Result
     where
         M: CommandToGsp,
         Error: From<M::InitError>,
@@ -328,7 +328,7 @@ impl Cmdq<'_> {
     ///   written to by its [`CommandToGsp::init_variable_payload`] method.
     ///
     /// Error codes returned by the command and reply initializers are propagated as-is.
-    pub(crate) fn send_command<M>(&self, command: M) -> Result<M::Reply>
+    pub(crate) fn send_rpc_command<M>(&self, command: M) -> Result<M::Reply>
     where
         M: CommandToGsp,
         M::Reply: MessageFromGsp,
@@ -336,7 +336,7 @@ pub(crate) fn send_command<M>(&self, command: M) -> Result<M::Reply>
         Error: From<<M::Reply as MessageFromGsp>::InitError>,
     {
         let mut inner = self.inner.lock();
-        inner.send_command(command)?;
+        inner.send_rpc_command(command)?;
 
         loop {
             match inner.consume_element::<RpcMessageElement<M::Reply>>(Self::RECEIVE_TIMEOUT) {
@@ -356,18 +356,18 @@ pub(crate) fn send_command<M>(&self, command: M) -> Result<M::Reply>
     ///   written to by its [`CommandToGsp::init_variable_payload`] method.
     ///
     /// Error codes returned by the command initializers are propagated as-is.
-    pub(crate) fn send_command_no_wait<M>(&self, command: M) -> Result
+    pub(crate) fn send_rpc_command_no_wait<M>(&self, command: M) -> Result
     where
         M: CommandToGsp<Reply = NoReply>,
         Error: From<M::InitError>,
     {
-        self.inner.lock().send_command(command)
+        self.inner.lock().send_rpc_command(command)
     }
 
     /// Receive a message from the GSP.
     ///
     /// See [`CmdqInner::consume_element`] for details.
-    pub(crate) fn receive_msg<M: MessageFromGsp>(&self, timeout: Delta) -> Result<M>
+    pub(crate) fn receive_rpc_msg<M: MessageFromGsp>(&self, timeout: Delta) -> Result<M>
     where
         // This allows all error types, including `Infallible`, to be used for `M::InitError`.
         Error: From<M::InitError>,
drivers/gpu/nova-core/gsp/commands/rpc.rs +1 -1
index 0176ac79fb09..a218453d6bef 100644
@@ -191,7 +191,7 @@ fn read(
 /// Waits for GSP initialization to complete.
 pub(crate) fn wait_gsp_init_done(cmdq: &Cmdq<'_>) -> Result {
     loop {
-        match cmdq.receive_msg::<GspInitDone>(Cmdq::RECEIVE_TIMEOUT) {
+        match cmdq.receive_rpc_msg::<GspInitDone>(Cmdq::RECEIVE_TIMEOUT) {
             Ok(_) => break Ok(()),
             Err(ERANGE) => continue,
             Err(e) => break Err(e),
drivers/gpu/nova-core/gsp/sequencer.rs +1 -1
index ebf13867746f..ab3237ed64d6 100644
@@ -344,7 +344,7 @@ pub(crate) fn run(
         bootloader_app_version: u32,
     ) -> Result {
         let seq_info = loop {
-            match cmdq.receive_msg::<GspSequence>(Cmdq::RECEIVE_TIMEOUT) {
+            match cmdq.receive_rpc_msg::<GspSequence>(Cmdq::RECEIVE_TIMEOUT) {
                 Ok(seq_info) => break seq_info,
                 Err(ERANGE) => continue,
                 Err(e) => return Err(e),
Signature · 1 line
2.55.0