Skip to contentSkip to conversation

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

PATCH Applied Alexandre Courbot 3 participants 20 messages Oct 27 – Oct 30

Subsystems (7)
lore
Share & tools
lore

Alexandre Courbot

author maintainer
#0 lore raw

v6 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.

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 revision fixes the latest nits while addressing a couple more issues found by Sashiko.

This series is based on drm-rust-next.

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

---
Changes in v6:
- Add comment to consume_element's documentation to disambiguate it. (Eliot)
- Add a patch to avoid sleeping in drain when reaching the end of the queue. (Sashiko)
- Fix badly-formatted import block. (Sashiko)
- Link to v5: https://patch.msgid.link/[email protected]

Changes in v5:
- Drop MessageElement and instead use a closure to process messages in consume_element. This reduces the behavioral change of await_msg and harmonizes it with the behavior of drain to the point where the latter can also use consume_element. Eliot's R-b has
been dropped on that patch as the code has changed significantly.
- Avoid unneeded temporary result variable. (Eliot)
- Rename await_msg to await_rpc_msg. (Eliot)
- Add patch splitting looping behavior of await_msg from its RPC message handling.
- Add patch factoring out drain code.
- Add two doccomment fixes.
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Rebase on top of latest drm-rust-next, including the interrupt series.
- Move RPC parsing code to RpcMessage. (Eliot)
- Do not mention the command queue slots in the public documentation. (Eliot)
- Use different names for the length methods of the message and RPC headers. (Eliot)
- Fix bug where the RPC header length was used instead of the message one to compute the number of pages to advance by. (Eliot)
- Add a patch moving the message logging code to RpcMessage.
- Make the rpc module only visible from gsp. (Eliot)
- Link to v3: https://patch.msgid.link/[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.
- Move log_event into a method of RpcMessage.
- Make the rpc module only visible to gsp and its submodules.
- Fix doclinks pointing to old function names. (Sashiko)
- Link to v1: https://patch.msgid.link/[email protected]

---
Alexandre Courbot (15):
gpu: nova-core: gsp: cmdq: remove stale comment
gpu: nova-core: gsp: cmdq: simplify framing error description
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: move RPC message logging to RpcMessage
gpu: nova-core: gsp: cmdq: split the transport part of the receive path
gpu: nova-core: gsp: cmdq: extract await_msg_with from await_msg
gpu: nova-core: gsp: cmdq: do not sleep when waiting for a message with timeout 0
gpu: nova-core: gsp: cmdq: use consume_element to implement drain
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

 Documentation/gpu/nova/core/interrupts.rst         |   7 +-
 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                  | 486 +++++----------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 415 ++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              | 329 +-------------
 drivers/gpu/nova-core/gsp/commands/rpc.rs          | 333 ++++++++++++++
 drivers/gpu/nova-core/gsp/fw.rs                    | 123 +++---
 drivers/gpu/nova-core/gsp/sequencer.rs             |   6 +-
 12 files changed, 947 insertions(+), 772 deletions(-)

---
base-commit: 3497a403264b094ebd61b8b755165d56e7d9dc37
change-id: 20260927-cmdq-rpc-ed5f850b207e

Best regards,

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

PATCH v6 1/15 gpu: nova-core: gsp: cmdq: remove stale comment

The message constructor is no longer run by wait_for_msg.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 2 --
 1 file changed, 2 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +0 -2
index 1b2f346a237f..accfc1d20d28 100644
@@ -777,8 +777,6 @@ fn send_command<M>(&mut self, command: M) -> Result
     /// - `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.
-    ///
-    /// Error codes returned by the message constructor are propagated as-is.
     fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
         // Wait for a message to arrive from the GSP.
         let (slice_1, slice_2) = read_poll_timeout(
Signature · 1 line
2.56.0
author maintainer
#2 lore raw

PATCH v6 2/15 gpu: nova-core: gsp: cmdq: simplify framing error description

In reply to Alexandre Courbot

Use simpler and more precise language to explain the conditions in which EIO is returned.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +3 -5
index accfc1d20d28..1bd15fc0aef2 100644
@@ -775,8 +775,7 @@ fn send_command<M>(&mut self, command: M) -> Result
     /// # 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.
+    /// - `EIO` if a message fails framing or checksum validation.
     fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
         // Wait for a message to arrive from the GSP.
         let (slice_1, slice_2) = read_poll_timeout(
@@ -849,8 +848,7 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
     /// # 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.
+    /// - `EIO` if a message fails framing or checksum validation.
     /// - `ENOMSG` if the message was not the awaited reply.
     ///
     /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
@@ -902,7 +900,7 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
     ///
     /// - `ETIMEDOUT` if no message of type `M` arrives before the deadline, however many other
     ///   messages arrive while waiting.
-    /// - `EIO` if a message fails framing or checksum validation (see [`Self::wait_for_msg`]).
+    /// - `EIO` if a message fails framing or checksum validation.
     ///
     /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
     fn await_msg<M: MessageFromGsp>(&mut self) -> Result<M>
Signature · 1 line
2.56.0
author maintainer
#3 lore raw

PATCH v6 3/15 gpu: nova-core: gsp: cmdq: validate checksum earlier on receive

In reply to Alexandre Courbot

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 1bd15fc0aef2..0c18a586576c 100644
@@ -789,6 +789,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",
@@ -817,21 +826,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.56.0
author maintainer
#4 lore raw

PATCH v6 4/15 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 |  50 ++++++++++------
 drivers/gpu/nova-core/gsp/fw.rs   | 123 ++++++++++++++++++++++----------------
 2 files changed, 106 insertions(+), 67 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +33 -17
index 0c18a586576c..01c83a79da14 100644
@@ -54,6 +54,7 @@
             MsgFunction,
             MsgqRxHeader,
             MsgqTxHeader,
+            RpcMessageHeader,
             GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, //
         },
         PteArray,
@@ -688,11 +689,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())?;
         }
 
@@ -718,7 +730,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.
@@ -791,23 +803,25 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
 
         // 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()),
+            SBufferIter::new_reader([header.as_bytes(), slice_1, slice_2])
+                .take(header.msg_length()),
         ) != 0
         {
             dev_err!(&self.dev, "GSP receive: bad checksum\n");
             return Err(EIO);
         }
 
+        let rpc_header = header.rpc_header();
+        let payload_length = rpc_header.rpc_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);
@@ -852,8 +866,9 @@ 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();
-        let seq = message.header.sequence();
+        let rpc_header = message.header.rpc_header();
+        let function = rpc_header.function();
+        let seq = rpc_header.sequence();
 
         // An early return here would leave the read pointer on this message.
         let result = if matches!(function, Ok(f) if f == M::FUNCTION) {
@@ -879,7 +894,7 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
 
         // 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),
+            message.header.msg_length().div_ceil(GSP_PAGE_SIZE),
         )?);
 
         result
@@ -958,12 +973,13 @@ fn drain(&mut self) -> Result {
             let msg = self.wait_for_msg(Delta::ZERO)?;
 
             let pages =
-                u32::try_from(msg.header.length().div_ceil(GSP_PAGE_SIZE)).map_err(|_| {
+                u32::try_from(msg.header.msg_length().div_ceil(GSP_PAGE_SIZE)).map_err(|_| {
                     dev_err!(&self.dev, "GSP drain: message length overflow\n");
                     EIO
                 })?;
-            let function = msg.header.function();
-            let seq = msg.header.sequence();
+            let rpc_header = msg.header.rpc_header();
+            let function = rpc_header.function();
+            let seq = rpc_header.sequence();
 
             self.gsp_mem.advance_cpu_read_ptr(pages);
             self.log_event(function, seq);
drivers/gpu/nova-core/gsp/fw.rs +73 -50

162 lines not on this page · raw message

Signature · 1 line
2.56.0
author maintainer
#5 lore raw

PATCH v6 5/15 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 01c83a79da14..88d81a4e37ef 100644
@@ -683,18 +683,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 {
@@ -717,14 +717,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",
@@ -733,6 +725,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.56.0
author maintainer
#6 lore raw

PATCH v6 6/15 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 88d81a4e37ef..ef59f541f7e6 100644
@@ -39,10 +39,7 @@
     },
 };
 
-use continuation::{
-    ContinuationRecord,
-    SplitState, //
-};
+use continuation::SplitState;
 
 use pin_init::pin_init_scope;
 
@@ -71,6 +68,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
@@ -133,6 +143,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.
 ///
 /// A reply that [`Cmdq::send_command`] waits for, or an event that [`Cmdq::await_msg`] waits for.
@@ -662,24 +728,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 space on the command queue to store `command`, initialize it with
+    /// [`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)?;
 
@@ -690,40 +750,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
@@ -759,13 +787,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.56.0
author maintainer
#7 lore raw

PATCH v6 7/15 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 | 112 ++++++++++++++++++++++++--------------
 1 file changed, 70 insertions(+), 42 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +70 -42
index ef59f541f7e6..18861400c3be 100644
@@ -554,11 +554,69 @@ struct GspCommand<'a> {
 struct GspMessage<'a> {
     // Reference to the header of the message.
     header: &'a GspMsgElement,
-    // Slices to the contents of the message. The second slice is zero unless the message loops
+    // Slices to the contents of the message. The second slice is empty unless the message loops
     // over the message queue.
     contents: (&'a [u8], &'a [u8]),
 }
 
+/// A RPC message ready to be processed from the message queue.
+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 empty unless the message loops
+    // over the message queue.
+    contents: (&'a [u8], &'a [u8]),
+}
+
+impl<'a> RpcMessage<'a> {
+    /// 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(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.rpc_length();
+
+        dev_dbg!(
+            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),
+        })
+    }
+}
+
 /// GSP command queue.
 ///
 /// Provides the ability to send commands and receive messages from the GSP using a shared memory
@@ -838,35 +896,6 @@ fn wait_for_msg(&self, timeout: Delta) -> Result<GspMessage<'_>> {
             return Err(EIO);
         }
 
-        let rpc_header = header.rpc_header();
-        let payload_length = rpc_header.rpc_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(GspMessage {
             header,
             contents: (slice_1, slice_2),
@@ -893,14 +922,15 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
         Error: From<M::InitError>,
     {
         let message = self.wait_for_msg(timeout)?;
-        let rpc_header = message.header.rpc_header();
-        let function = rpc_header.function();
-        let seq = rpc_header.sequence();
+        let elem_count = u32::try_from(message.header.msg_length().div_ceil(GSP_PAGE_SIZE))?;
+        let rpc_message = RpcMessage::parse(self.dev, message)?;
+        let function = rpc_message.header.function();
 
         // An early return here would leave the read pointer on this message.
         let result = if matches!(function, Ok(f) if f == 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]);
+            let (cmd, contents_1) =
+                M::Message::from_bytes_prefix(rpc_message.contents.0).ok_or(EIO)?;
+            let mut sbuffer = SBufferIter::new_reader([contents_1, rpc_message.contents.1]);
 
             M::read(cmd, &mut sbuffer)
                 .map_err(|e| e.into())
@@ -914,15 +944,13 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
                     }
                 })
         } else {
-            self.log_event(function, seq);
+            self.log_event(function, rpc_message.header.sequence());
 
             Err(ENOMSG)
         };
 
         // Advance the read pointer past this message.
-        self.gsp_mem.advance_cpu_read_ptr(u32::try_from(
-            message.header.msg_length().div_ceil(GSP_PAGE_SIZE),
-        )?);
+        self.gsp_mem.advance_cpu_read_ptr(elem_count);
 
         result
     }
@@ -1004,9 +1032,9 @@ fn drain(&mut self) -> Result {
                     dev_err!(&self.dev, "GSP drain: message length overflow\n");
                     EIO
                 })?;
-            let rpc_header = msg.header.rpc_header();
-            let function = rpc_header.function();
-            let seq = rpc_header.sequence();
+            let rpc_msg = RpcMessage::parse(self.dev, msg)?;
+            let function = rpc_msg.header.function();
+            let seq = rpc_msg.header.sequence();
 
             self.gsp_mem.advance_cpu_read_ptr(pages);
             self.log_event(function, seq);
Signature · 1 line
2.56.0
author maintainer
#8 lore raw

PATCH v6 8/15 gpu: nova-core: gsp: cmdq: move RPC message logging to RpcMessage

In reply to Alexandre Courbot

The RpcMessage type is the right container for the log_event method, since it contains all the data we passed to it. Move it there so callers don't need to extract the required data themselves.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 66 ++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 35 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +31 -35
index 18861400c3be..4efc96eba4dd 100644
@@ -615,6 +615,34 @@ fn parse(dev: &device::Device, element: GspMessage<'a>) -> Result<RpcMessage<'a>
             contents: (slice_1, slice_2),
         })
     }
+
+    /// Logs an event, meaning a message that no caller was waiting for.
+    ///
+    /// An OS error or robust-channel record is logged at error level and an unknown function code
+    /// at warning level. Other events are not logged.
+    fn log(&self, dev: &device::Device) {
+        let function = self.header.function();
+        let seq = self.header.sequence();
+
+        match function {
+            Ok(MsgFunction::OsErrorLog) => {
+                dev_err!(dev, "GSP reported an OS error (seq {})\n", seq);
+            }
+            Ok(MsgFunction::RcTriggered) => {
+                dev_err!(dev, "GSP triggered robust-channel recovery (seq {})\n", seq);
+            }
+            // Nothing to do for the remaining known function codes.
+            Ok(_) => {}
+            Err(raw) => {
+                dev_warn!(
+                    dev,
+                    "unknown GSP message function {:#x} (seq {})\n",
+                    raw,
+                    seq
+                );
+            }
+        }
+    }
 }
 
 /// GSP command queue.
@@ -944,7 +972,7 @@ fn receive_msg<M: MessageFromGsp>(&mut self, timeout: Delta) -> Result<M>
                     }
                 })
         } else {
-            self.log_event(function, rpc_message.header.sequence());
+            rpc_message.log(self.dev);
 
             Err(ENOMSG)
         };
@@ -986,36 +1014,6 @@ fn await_msg<M: MessageFromGsp>(&mut self) -> Result<M>
         }
     }
 
-    /// Logs an event, meaning a message that no caller was waiting for.
-    ///
-    /// An OS error or robust-channel record is logged at error level and an unknown function code
-    /// at warning level. Every other event is recorded only by the receive trace in
-    /// [`Self::wait_for_msg`].
-    fn log_event(&self, function: Result<MsgFunction, u32>, seq: u32) {
-        match function {
-            Ok(MsgFunction::OsErrorLog) => {
-                dev_err!(&self.dev, "GSP reported an OS error (seq {})\n", seq);
-            }
-            Ok(MsgFunction::RcTriggered) => {
-                dev_err!(
-                    &self.dev,
-                    "GSP triggered robust-channel recovery (seq {})\n",
-                    seq
-                );
-            }
-            // Nothing to do for the remaining known function codes.
-            Ok(_) => {}
-            Err(raw) => {
-                dev_warn!(
-                    &self.dev,
-                    "unknown GSP message function {:#x} (seq {})\n",
-                    raw,
-                    seq
-                );
-            }
-        }
-    }
-
     /// Logs and consumes every message the queue holds.
     ///
     /// # Errors
@@ -1032,12 +1030,10 @@ fn drain(&mut self) -> Result {
                     dev_err!(&self.dev, "GSP drain: message length overflow\n");
                     EIO
                 })?;
-            let rpc_msg = RpcMessage::parse(self.dev, msg)?;
-            let function = rpc_msg.header.function();
-            let seq = rpc_msg.header.sequence();
+
+            RpcMessage::parse(self.dev, msg)?.log(self.dev);
 
             self.gsp_mem.advance_cpu_read_ptr(pages);
-            self.log_event(function, seq);
         }
 
         Ok(())
Signature · 1 line
2.56.0
author maintainer
#9 lore raw

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

In reply to Alexandre Courbot

receive_msg currently handles both the transport and RPC layers. Retire it and split its logic between the new consume_element, a transport-level method which passes the parsed message contents to a closure before advancing the CPU read pointer past it, and the already-existing await_msg, which calls consume_element with a closure matching the RPC message.

This introduces a small behavioral change: a matched RPC message that is too short to contain the message component itself no longer stalls the queue. This behavior is justified because it fixes an inconsistency:
unmatched RPC messages were always skipped, whereas matched RPC messages could block the queue if they were too short. While a misconstructed RPC message is a problem, it is not a fatal queue error as long as the transport framing and checksum are valid.

---
 Documentation/gpu/nova/core/interrupts.rst |  7 +--
 drivers/gpu/nova-core/gsp/cmdq.rs          | 96 +++++++++++++++++-------------
 2 files changed, 59 insertions(+), 44 deletions(-)
Documentation/gpu/nova/core/interrupts.rst +3 -4
index e42286915711..13a087b960c0 100644
@@ -583,13 +583,12 @@ the command's sequence number on every reply. On r570 the reply to
 ``UnloadingGuestDriver`` carries sequence 0.
 
 The read pointer advances past every message that passes framing and checksum
-validation, whether it matched or was an event. A matched message that is too
-short to decode is the exception, and it stays at the queue head.
+validation, whether it matched or was an event.
 
 A message's length is inside the region that the checksum covers, so once the
 framing or the checksum fails there is no trustworthy length with which to skip
-the message. Such a message also stays at the queue head, and every later
-receive fails on it with ``EIO`` until the device is reset.
+the message. Such a message stays at the queue head, and every later receive
+fails on it with ``EIO`` until the device is reset.
 
 The polling path and the IRQ thread both read the queue under the command-queue
 mutex. Replies and events share one queue and one read pointer, so one lock is
drivers/gpu/nova-core/gsp/cmdq.rs +56 -40

133 lines not on this page · raw message

Signature · 1 line
2.56.0
author maintainer
#10 lore raw

PATCH v6 10/15 gpu: nova-core: gsp: cmdq: extract await_msg_with from await_msg

In reply to Alexandre Courbot

The logic of await_msg is also usable for other kinds of messages that are not RPC; it will be used to wait for GMC messages as well. Extract that logic into a new await_msg_with method that uses a closure to decide whether a given message is the one we wait for, and use it in await_msg.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 44 ++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 12 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +32 -12
index 6075868638ec..9e678715c624 100644
@@ -974,6 +974,36 @@ fn consume_element<R>(
         Ok(res)
     }
 
+    /// Consumes messages from the queue until a message that satisfies `f` is encountered, and
+    /// returns the result of `f`.
+    ///
+    /// The message is waited for up to [`Cmdq::RECEIVE_TIMEOUT`] from the call.
+    ///
+    /// `f` is expected to return `Ok` with the result to return to the caller, and `Err(ENOMSG)` to
+    /// signal that the received message was not the one expected (such messages are dropped). Any
+    /// other error returned by `f` is propagated to the caller.
+    ///
+    /// # Errors
+    ///
+    /// - `ETIMEDOUT` if no message satisfying `f` arrives before the deadline, however many other
+    ///   messages arrive while waiting.
+    /// - `EIO` if a message fails framing or checksum validation.
+    /// - Any error returned by `f` with the exception of `ENOMSG`.
+    fn await_msg_with<R>(&mut self, f: impl Fn(GspParsedMessage<'_>) -> Result<R>) -> Result<R> {
+        let deadline = Instant::<Monotonic>::now() + Cmdq::RECEIVE_TIMEOUT;
+        loop {
+            let remaining = deadline - Instant::<Monotonic>::now();
+            if remaining.is_negative() {
+                break Err(ETIMEDOUT);
+            }
+            match self.consume_element(remaining, &f)? {
+                Ok(m) => break Ok(m),
+                Err(ENOMSG) => continue,
+                Err(e) => break Err(e),
+            }
+        }
+    }
+
     /// Receives a message of type `M`, waiting up to [`Cmdq::RECEIVE_TIMEOUT`] from the call.
     ///
     /// Any other message that arrives first is logged as an event and does not extend the
@@ -1016,18 +1046,8 @@ fn await_msg<M: MessageFromGsp>(&mut self) -> Result<M>
                 Err(ENOMSG)
             }
         };
-        let deadline = Instant::<Monotonic>::now() + Cmdq::RECEIVE_TIMEOUT;
-        loop {
-            let remaining = deadline - Instant::<Monotonic>::now();
-            if remaining.is_negative() {
-                break Err(ETIMEDOUT);
-            }
-            match self.consume_element(remaining, f)? {
-                Ok(m) => break Ok(m),
-                Err(ENOMSG) => continue,
-                Err(e) => break Err(e),
-            }
-        }
+
+        self.await_msg_with(f)
     }
 
     /// Logs and consumes every message the queue holds.
Signature · 1 line
2.56.0
author maintainer
#11 lore raw

PATCH v6 11/15 gpu: nova-core: gsp: cmdq: do not sleep when waiting for a message with timeout 0

In reply to Alexandre Courbot

read_poll_timeout can potentially sleep for the specified sleep_delta if timeout_delta is Delta::ZERO and the system clock is coarse enough to not register a tick between the time the function has been called and the time the loop test is done. wait_for_msg has a short test condition and is particularly vulnerable to this condition.

Add a dedicated branch when timeout is Delta::ZERO to perform the test without calling read_poll_timeout and avoid potentially sleeping for 1ms.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +18 -6
index 9e678715c624..9c2c8fbad045 100644
@@ -909,12 +909,24 @@ fn send_command<M>(&mut self, command: M) -> Result
     /// - `EIO` if a message fails framing or checksum validation.
     fn wait_for_msg(&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()),
-            |driver_area| !driver_area.0.is_empty(),
-            Delta::from_millis(1),
-            timeout,
-        )
+        let (slice_1, slice_2) = if timeout == Delta::ZERO {
+            // If `timeout == Delta::ZERO`, check the message queue once as `read_poll_timeout` can
+            // potentially sleep if the clock hasn't moved forward while the condition was being
+            // checked.
+            let slices = self.gsp_mem.driver_read_area();
+            if !slices.0.is_empty() {
+                Ok(slices)
+            } else {
+                Err(ETIMEDOUT)
+            }
+        } else {
+            read_poll_timeout(
+                || Ok(self.gsp_mem.driver_read_area()),
+                |driver_area| !driver_area.0.is_empty(),
+                Delta::from_millis(1),
+                timeout,
+            )
+        }
         .map(|(slice_1, slice_2)| (slice_1.as_flattened(), slice_2.as_flattened()))?;
 
         // Extract the `GspMsgElement`.
Signature · 1 line
2.56.0
Reviewed-by on v6 11/15
#12 lore raw

Re: [PATCH v6 11/15] gpu: nova-core: gsp: cmdq: do not sleep when waiting for a message with timeout 0

Quoted 6 lines · Alexandre Courbot

read_poll_timeout can potentially sleep for the specified sleep_delta if timeout_delta is Delta::ZERO and the system clock is coarse enough to not register a tick between the time the function has been called and the time the loop test is done. wait_for_msg has a short test condition and is particularly vulnerable to this condition.

Quoted 6 lines

Add a dedicated branch when timeout is Delta::ZERO to perform the test without calling read_poll_timeout and avoid potentially sleeping for 1ms.

---

author maintainer
#13 lore raw

PATCH v6 12/15 gpu: nova-core: gsp: cmdq: use consume_element to implement drain

In reply to Alexandre Courbot

consume_element can be used to factor out the code of drain that advances the read pointer.

No functional change intended.

---
 drivers/gpu/nova-core/gsp/cmdq.rs | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +17 -15
index 9c2c8fbad045..cbdd05385e83 100644
@@ -651,6 +651,15 @@ enum GspParsedMessage<'a> {
     Rpc(RpcMessage<'a>),
 }
 
+impl GspParsedMessage<'_> {
+    /// Log the message on `dev`.
+    fn log(&self, dev: &device::Device) {
+        match self {
+            GspParsedMessage::Rpc(rpc) => rpc.log(dev),
+        }
+    }
+}
+
 /// GSP command queue.
 ///
 /// Provides the ability to send commands and receive messages from the GSP using a shared memory
@@ -1069,21 +1078,14 @@ fn await_msg<M: MessageFromGsp>(&mut self) -> Result<M>
     /// `EIO` if a message fails framing or checksum validation, or if a message's page count
     /// overflows a `u32`.
     fn drain(&mut self) -> Result {
-        while !self.gsp_mem.driver_read_area().0.is_empty() {
-            // A message is available, so this returns without waiting.
-            let msg = self.wait_for_msg(Delta::ZERO)?;
-
-            let pages =
-                u32::try_from(msg.header.msg_length().div_ceil(GSP_PAGE_SIZE)).map_err(|_| {
-                    dev_err!(&self.dev, "GSP drain: message length overflow\n");
-                    EIO
-                })?;
-
-            RpcMessage::parse(self.dev, msg)?.log(self.dev);
-
-            self.gsp_mem.advance_cpu_read_ptr(pages);
+        loop {
+            match self.consume_element(Delta::ZERO, |message| message.log(self.dev)) {
+                Ok(()) => (),
+                // No more message pending on the queue, drain is complete.
+                Err(ETIMEDOUT) => return Ok(()),
+                // Other errors are fatal, and stall the queue.
+                Err(e) => return Err(e),
+            }
         }
-
-        Ok(())
     }
 }
Signature · 1 line
2.56.0
author maintainer
#14 lore raw

PATCH v6 13/15 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; they just live in their own sub-module now.

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

---
 drivers/gpu/nova-core/gsp/cmdq.rs                  | 385 +------------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 415 +++++++++++++++++++++
 .../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, 426 insertions(+), 384 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +5 -380
index cbdd05385e83..ddf3f6256222 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(super) mod rpc;
 
 use core::mem;
 
@@ -39,8 +40,6 @@
     },
 };
 
-use continuation::SplitState;
-
 use pin_init::pin_init_scope;
 
 use crate::{
@@ -48,10 +47,8 @@
     gsp::{
         fw::{
             GspMsgElement,
-            MsgFunction,
             MsgqRxHeader,
             MsgqTxHeader,
-            RpcMessageHeader,
             GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, //
         },
         PteArray,
@@ -81,149 +78,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 representing messages received from the GSP.
-///
-/// A reply that [`Cmdq::send_command`] waits for, or an event that [`Cmdq::await_msg`] waits for.
-/// The receiver matches a message's function code against [`Self::FUNCTION`] and decodes the
-/// message with [`Self::read`].
-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>;
-}
-
 /// Number of GSP pages making the [`Msgq`].
 pub(crate) const MSGQ_NUM_PAGES: u32 = 0x3f;
 
@@ -559,96 +413,10 @@ struct GspMessage<'a> {
     contents: (&'a [u8], &'a [u8]),
 }
 
-/// A RPC message ready to be processed from the message queue.
-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 empty unless the message loops
-    // over the message queue.
-    contents: (&'a [u8], &'a [u8]),
-}
-
-impl<'a> RpcMessage<'a> {
-    /// 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(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.rpc_length();
-
-        dev_dbg!(
-            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),
-        })
-    }
-
-    /// Logs an event, meaning a message that no caller was waiting for.
-    ///
-    /// An OS error or robust-channel record is logged at error level and an unknown function code
-    /// at warning level. Other events are not logged.
-    fn log(&self, dev: &device::Device) {
-        let function = self.header.function();
-        let seq = self.header.sequence();
-
-        match function {
-            Ok(MsgFunction::OsErrorLog) => {
-                dev_err!(dev, "GSP reported an OS error (seq {})\n", seq);
-            }
-            Ok(MsgFunction::RcTriggered) => {
-                dev_err!(dev, "GSP triggered robust-channel recovery (seq {})\n", seq);
-            }
-            // Nothing to do for the remaining known function codes.
-            Ok(_) => {}
-            Err(raw) => {
-                dev_warn!(
-                    dev,
-                    "unknown GSP message function {:#x} (seq {})\n",
-                    raw,
-                    seq
-                );
-            }
-        }
-    }
-}
-
 /// A message whose protocol layer has been parsed and validated.
 enum GspParsedMessage<'a> {
-    /// A [RPC message](RpcMessage).
-    Rpc(RpcMessage<'a>),
+    /// A [RPC message](rpc::RpcMessage).
+    Rpc(rpc::RpcMessage<'a>),
 }
 
 impl GspParsedMessage<'_> {
@@ -731,73 +499,6 @@ 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.
-    ///
-    /// Events that arrive before the reply are logged and consumed.
-    ///
-    /// 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 does
-    ///   not arrive within [`Self::RECEIVE_TIMEOUT`] of the send, however many events arrive
-    ///   while waiting.
-    /// - `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)?;
-
-        inner.await_msg()
-    }
-
-    /// 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)
-    }
-
-    /// Waits for an unsolicited GSP event of type `M`. Events that arrive before it are logged and
-    /// consumed.
-    ///
-    /// The queue mutex is held for the whole wait, up to [`Self::RECEIVE_TIMEOUT`], so no other
-    /// caller can send a command or consume an event meanwhile.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if the event does not arrive within [`Self::RECEIVE_TIMEOUT`] of the call,
-    ///   however many other events arrive while waiting.
-    /// - `EIO` if a message fails framing or checksum validation.
-    ///
-    /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
-    pub(crate) fn await_msg<M: MessageFromGsp>(&self) -> Result<M>
-    where
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
-        self.inner.lock().await_msg()
-    }
-
     /// Logs and consumes every message the GSP has already posted, and returns without waiting for
     /// more.
     ///
@@ -871,36 +572,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 a message to become available on the message queue.
     ///
     /// This works purely at the transport layer and does not interpret or validate the message
@@ -985,7 +656,7 @@ fn consume_element<R>(
             })?;
 
         // RPC is the only type of message supported for now.
-        let message = GspParsedMessage::Rpc(RpcMessage::parse(self.dev, message)?);
+        let message = GspParsedMessage::Rpc(rpc::RpcMessage::parse(self.dev, message)?);
 
         let res = f(message);
 
@@ -1025,52 +696,6 @@ fn await_msg_with<R>(&mut self, f: impl Fn(GspParsedMessage<'_>) -> Result<R>) -
         }
     }
 
-    /// Receives a message of type `M`, waiting up to [`Cmdq::RECEIVE_TIMEOUT`] from the call.
-    ///
-    /// Any other message that arrives first is logged as an event and does not extend the
-    /// deadline.
-    ///
-    /// # Errors
-    ///
-    /// - `ETIMEDOUT` if no message of type `M` arrives before the deadline, however many other
-    ///   messages arrive while waiting.
-    /// - `EIO` if a message fails framing or checksum validation.
-    ///
-    /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
-    fn await_msg<M: MessageFromGsp>(&mut self) -> Result<M>
-    where
-        // This allows all error types, including `Infallible`, to be used for `M::InitError`.
-        Error: From<M::InitError>,
-    {
-        let f = |message: GspParsedMessage<'_>| {
-            let GspParsedMessage::Rpc(rpc_message) = message;
-            let function = rpc_message.header.function();
-            if matches!(function, Ok(f) if f == M::FUNCTION) {
-                let (cmd, contents_1) =
-                    M::Message::from_bytes_prefix(rpc_message.contents.0).ok_or(EIO)?;
-                let mut sbuffer = SBufferIter::new_reader([contents_1, rpc_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",
-                                M::FUNCTION
-                            );
-                        }
-                    })
-            } else {
-                rpc_message.log(self.dev);
-
-                Err(ENOMSG)
-            }
-        };
-
-        self.await_msg_with(f)
-    }
-
     /// Logs and consumes every message the queue holds.
     ///
     /// # Errors
drivers/gpu/nova-core/gsp/cmdq/rpc.rs new file +415 -0

415 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 cdbb13674f08..ceac0b0f9423 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 250adc9fe74f..9f5ab08def2c 100644
@@ -28,8 +28,8 @@
     },
     gsp::{
         cmdq::{
-            Cmdq,
-            MessageFromGsp, //
+            rpc::MessageFromGsp,
+            Cmdq, //
         },
         fw,
         GspBootContext,
Signature · 1 line
2.56.0
author maintainer
#15 lore raw

PATCH v6 14/15 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     | 331 +----------------------------
 drivers/gpu/nova-core/gsp/commands/rpc.rs | 333 ++++++++++++++++++++++++++++++
 6 files changed, 344 insertions(+), 340 deletions(-)
drivers/gpu/nova-core/api.rs +1 -1
index 29207c4bb743..eec69aace3b2 100644
@@ -18,7 +18,7 @@
 use crate::{
     driver,
     gpu::Gpu,
-    gsp::commands::GetGspStaticInfoReply, //
+    gsp::commands::rpc::GetGspStaticInfoReply, //
 };
 
 /// API handle for the auxiliary bus child drivers to interact with nova-core.
drivers/gpu/nova-core/gpu.rs +1 -1
index 4e59072cab50..ac5305d7ff5e 100644
@@ -35,7 +35,7 @@
     gsp::{
         self,
         cmdq::Cmdq,
-        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 -330

334 lines not on this page · raw message

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

333 lines not on this page · raw message

Signature · 1 line
2.56.0
author maintainer
#16 lore raw

PATCH v6 15/15 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     | 24 ++++++++++++------------
 drivers/gpu/nova-core/gsp/commands/rpc.rs |  2 +-
 drivers/gpu/nova-core/gsp/sequencer.rs    |  2 +-
 6 files changed, 19 insertions(+), 19 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 ddf3f6256222..e6d9a97130ea 100644
@@ -62,7 +62,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 +12 -12
index 13baf4398632..2c52ec78871b 100644
@@ -158,9 +158,9 @@ fn write(&self, dev: &device::Device, seq: u32, dst: &mut GspCommand<'_>) -> Res
 
 /// Trait representing messages received from the GSP.
 ///
-/// A reply that [`Cmdq::send_command`] waits for, or an event that [`Cmdq::await_msg`] waits for.
-/// The receiver matches a message's function code against [`Self::FUNCTION`] and decodes the
-/// message with [`Self::read`].
+/// A reply that [`Cmdq::send_rpc_command`] waits for, or an event that [`Cmdq::await_rpc_msg`]
+/// waits for. The receiver matches a message's function code against [`Self::FUNCTION`] and decodes
+/// the message with [`Self::read`].
 pub(crate) trait MessageFromGsp: Sized {
     /// Function identifying this message from the GSP.
     const FUNCTION: MsgFunction;
@@ -284,7 +284,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,
@@ -292,9 +292,9 @@ 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)?;
 
-        inner.await_msg()
+        inner.await_rpc_msg()
     }
 
     /// Sends `command` to the GSP without waiting for a reply.
@@ -306,12 +306,12 @@ 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)
     }
 
     /// Waits for an unsolicited GSP event of type `M`. Events that arrive before it are logged and
@@ -327,12 +327,12 @@ pub(crate) fn send_command_no_wait<M>(&self, command: M) -> Result
     /// - `EIO` if a message fails framing or checksum validation.
     ///
     /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
-    pub(crate) fn await_msg<M: MessageFromGsp>(&self) -> Result<M>
+    pub(crate) fn await_rpc_msg<M: MessageFromGsp>(&self) -> Result<M>
     where
         // This allows all error types, including `Infallible`, to be used for `M::InitError`.
         Error: From<M::InitError>,
     {
-        self.inner.lock().await_msg()
+        self.inner.lock().await_rpc_msg()
     }
 }
 
@@ -348,7 +348,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>,
@@ -379,7 +379,7 @@ fn send_command<M>(&mut self, command: M) -> Result
     /// - `EIO` if a message fails framing or checksum validation.
     ///
     /// Error codes returned by [`MessageFromGsp::read`] are propagated as-is.
-    fn await_msg<M: MessageFromGsp>(&mut self) -> Result<M>
+    fn await_rpc_msg<M: MessageFromGsp>(&mut self) -> 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 54c40f29e367..156357dd48df 100644
@@ -190,7 +190,7 @@ fn read(
 
 /// Waits for GSP initialization to complete.
 pub(crate) fn wait_gsp_init_done(cmdq: &Cmdq<'_>) -> Result {
-    cmdq.await_msg::<GspInitDone>().map(|_| ())
+    cmdq.await_rpc_msg::<GspInitDone>().map(|_| ())
 }
 
 /// The `GetGspStaticInfo` command.
drivers/gpu/nova-core/gsp/sequencer.rs +1 -1
index 9f5ab08def2c..23b09fb875e9 100644
@@ -343,7 +343,7 @@ pub(crate) fn run(
         libos: &'a Coherent<'a, [LibosMemoryRegionInitArgument]>,
         bootloader_app_version: u32,
     ) -> Result {
-        let seq_info = cmdq.await_msg::<GspSequence>()?;
+        let seq_info = cmdq.await_rpc_msg::<GspSequence>()?;
 
         let sequencer = GspSequencer {
             bar: ctx.bar,
Signature · 1 line
2.56.0
#17 lore raw
In reply to Alexandre Courbot
Quoted 48 lines · Alexandre Courbot

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.

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 revision fixes the latest nits while addressing a couple more issues found by Sashiko.

This series is based on drm-rust-next.

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

Quoted 8 lines

---
Changes in v6:
- Add comment to consume_element's documentation to disambiguate it. (Eliot)
- Add a patch to avoid sleeping in drain when reaching the end of the queue. (Sashiko)
- Fix badly-formatted import block. (Sashiko)
- Link to v5: https://patch.msgid.link/[email protected]

Hi Alex, looks like it is converging, are you planning to merge soon? Or do you need more reviewers to weigh in?

thanks,

Signature · 1 line
John Hubbard
Quoted 87 lines

Changes in v5:
- Drop MessageElement and instead use a closure to process messages in consume_element. This reduces the behavioral change of await_msg and harmonizes it with the behavior of drain to the point where the latter can also use consume_element. Eliot's R-b has
been dropped on that patch as the code has changed significantly.
- Avoid unneeded temporary result variable. (Eliot)
- Rename await_msg to await_rpc_msg. (Eliot)
- Add patch splitting looping behavior of await_msg from its RPC message handling.
- Add patch factoring out drain code.
- Add two doccomment fixes.
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Rebase on top of latest drm-rust-next, including the interrupt series.
- Move RPC parsing code to RpcMessage. (Eliot)
- Do not mention the command queue slots in the public documentation. (Eliot)
- Use different names for the length methods of the message and RPC headers. (Eliot)
- Fix bug where the RPC header length was used instead of the message one to compute the number of pages to advance by. (Eliot)
- Add a patch moving the message logging code to RpcMessage.
- Make the rpc module only visible from gsp. (Eliot)
- Link to v3: https://patch.msgid.link/[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.
- Move log_event into a method of RpcMessage.
- Make the rpc module only visible to gsp and its submodules.
- Fix doclinks pointing to old function names. (Sashiko)
- Link to v1: https://patch.msgid.link/[email protected]

---
Alexandre Courbot (15):
gpu: nova-core: gsp: cmdq: remove stale comment
gpu: nova-core: gsp: cmdq: simplify framing error description
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: move RPC message logging to RpcMessage
gpu: nova-core: gsp: cmdq: split the transport part of the receive path
gpu: nova-core: gsp: cmdq: extract await_msg_with from await_msg
gpu: nova-core: gsp: cmdq: do not sleep when waiting for a message with timeout 0
gpu: nova-core: gsp: cmdq: use consume_element to implement drain
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

 Documentation/gpu/nova/core/interrupts.rst         |   7 +-
 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                  | 486 +++++----------------
 drivers/gpu/nova-core/gsp/cmdq/rpc.rs              | 415 ++++++++++++++++++
 .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs   |   0
 drivers/gpu/nova-core/gsp/commands.rs              | 329 +-------------
 drivers/gpu/nova-core/gsp/commands/rpc.rs          | 333 ++++++++++++++
 drivers/gpu/nova-core/gsp/fw.rs                    | 123 +++---
 drivers/gpu/nova-core/gsp/sequencer.rs             |   6 +-
 12 files changed, 947 insertions(+), 772 deletions(-)

---
base-commit: 3497a403264b094ebd61b8b755165d56e7d9dc37
change-id: 20260927-cmdq-rpc-ed5f850b207e

Best regards,

Signature · 1 line
Alexandre Courbot <[email protected]>
author maintainer
#18 lore raw
Quoted 58 lines · John Hubbard
Quoted 48 lines · Alexandre Courbot

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.

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 revision fixes the latest nits while addressing a couple more issues found by Sashiko.

This series is based on drm-rust-next.

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

Quoted 8 lines

---
Changes in v6:
- Add comment to consume_element's documentation to disambiguate it. (Eliot)
- Add a patch to avoid sleeping in drain when reaching the end of the queue. (Sashiko)
- Fix badly-formatted import block. (Sashiko)
- Link to v5: https://patch.msgid.link/[email protected]

Quoted 2 lines

Hi Alex, looks like it is converging, are you planning to merge soon? Or do you need more reviewers to weigh in?

I was planning to give this (hopefully final) a couple days on the list to check that the r000 rebase on it is viable (which it seems to be!), so unless someone screams I will merge it very soon.

author maintainer Applied
#19 lore raw
In reply to Alexandre Courbot
Quoted 8 lines · Alexandre Courbot

Alexandre Courbot (15):
gpu: nova-core: gsp: cmdq: remove stale comment
gpu: nova-core: gsp: cmdq: simplify framing error description
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

Quoted 8 lines

gpu: nova-core: gsp: cmdq: move RPC message logging to RpcMessage
gpu: nova-core: gsp: cmdq: split the transport part of the receive path
gpu: nova-core: gsp: cmdq: extract await_msg_with from await_msg
gpu: nova-core: gsp: cmdq: do not sleep when waiting for a message with timeout 0
gpu: nova-core: gsp: cmdq: use consume_element to implement drain
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

Merged into drm-rust-next, thanks!