Move the types and code related to sending a RPC command into the `rpc` sub-module. The methods are still declared as members of `CmdqInner` and `Cmdq`; only they are now in their own sub-module.
This is a pure move commit, with no functional change intended. Signed-off-by: Alexandre Courbot <[email protected]> --- drivers/gpu/nova-core/gsp/cmdq.rs | 214 +----------------- drivers/gpu/nova-core/gsp/cmdq/rpc.rs | 248 +++++++++++++++++++++ .../nova-core/gsp/cmdq/{ => rpc}/continuation.rs | 0 drivers/gpu/nova-core/gsp/commands.rs | 2 +- 4 files changed, 251 insertions(+), 213 deletions(-) diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs b/drivers/gpu/nova-core/gsp/cmdq.rs index 602824236f64..2d22ae45e990 100644 --- a/drivers/gpu/nova-core/gsp/cmdq.rs +++ b/drivers/gpu/nova-core/gsp/cmdq.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -mod continuation; +pub(crate) mod rpc; use core::mem; @@ -35,11 +36,6 @@ }, }; -use continuation::{ - ContinuationRecord, - SplitState, // -}; - use pin_init::pin_init_scope; use crate::{ @@ -50,7 +46,6 @@ MsgFunction, MsgqRxHeader, MsgqTxHeader, - RpcMessageHeader, GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, // }, PteArray, @@ -67,68 +62,6 @@ /// reply type are sent using [`Cmdq::send_command_no_wait`]. pub(crate) struct NoReply; -/// 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() - } -} - /// Trait representing messages received from the GSP. /// /// This trait tells [`Cmdq::receive_msg`] how it can receive a given type of message. @@ -558,58 +491,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. - /// - /// Messages with non-matching function codes are silently consumed until the expected reply - /// arrives. - /// - /// The queue is locked for the entire send+receive cycle to ensure that no other command can - /// be interleaved. - /// - /// # Errors - /// - /// - `ETIMEDOUT` if space does not become available to send the command, or if the reply is - /// not received within the timeout. - /// - `EIO` if the variable payload requested by the command has not been entirely - /// written to by its [`CommandToGsp::init_variable_payload`] method. - /// - /// Error codes returned by the command and reply initializers are propagated as-is. - pub(crate) fn send_command<M>(&self, command: M) -> Result<M::Reply> - where - M: CommandToGsp, - M::Reply: MessageFromGsp, - Error: From<M::InitError>, - Error: From<<M::Reply as MessageFromGsp>::InitError>, - { - let mut inner = self.inner.lock(); - inner.send_command(command)?; - - loop { - match inner.receive_msg::<M::Reply>(Self::RECEIVE_TIMEOUT) { - Ok(reply) => break Ok(reply), - Err(ERANGE) => continue, - Err(e) => break Err(e), - } - } - } - - /// Sends `command` to the GSP without waiting for a reply. - /// - /// # Errors - /// - /// - `ETIMEDOUT` if space does not become available within the timeout. - /// - `EIO` if the variable payload requested by the command has not been entirely - /// written to by its [`CommandToGsp::init_variable_payload`] method. - /// - /// Error codes returned by the command initializers are propagated as-is. - pub(crate) fn send_command_no_wait<M>(&self, command: M) -> Result - where - M: CommandToGsp<Reply = NoReply>, - Error: From<M::InitError>, - { - self.inner.lock().send_command(command) - } - /// Receive a message from the GSP. /// /// See [`CmdqInner::receive_msg`] for details. @@ -685,97 +566,6 @@ fn send_command_element( Ok(()) } - /// Sends `command` to the GSP, without splitting it. - /// - /// # 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>, - { - let dev = self.dev; - let seq = self.seq; - let size_in_bytes = command.size(); - - let init = |dst: &mut GspCommand<'_>| { - // 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: `rpc_header_mut()` and `cmd` are valid references, and not touched if the - // initializer fails. - unsafe { - pin_init::raw_try_init( - core::ptr::from_mut(dst.header.rpc_header_mut()), - rpc_header_init, - )?; - 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(()) - }; - - self.send_command_element(size_in_bytes, init) - } - - /// 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_single_command(command), - SplitState::Split(command, mut continuations) => { - self.send_single_command(command)?; - - while let Some(continuation) = continuations.next() { - // Turbofish needed because the compiler cannot infer M here. - self.send_single_command::<ContinuationRecord<'_>>(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 diff --git a/drivers/gpu/nova-core/gsp/cmdq/rpc.rs b/drivers/gpu/nova-core/gsp/cmdq/rpc.rs new file mode 100644 index 000000000000..3bafe456efd6 --- /dev/null +++ b/drivers/gpu/nova-core/gsp/cmdq/rpc.rs @@ -0,0 +1,248 @@ +// SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + +//! Support for the RPC message type on the GSP command queue. + +mod continuation; + +use kernel::{ + prelude::*, + transmute::{ + AsBytes, + FromBytes, // + }, +}; + +use continuation::{ + ContinuationRecord, + SplitState, // +}; + +use crate::{ + gsp::{ + cmdq::{ + GspCommand, + NoReply, // + }, + fw::{ + MsgFunction, + RpcMessageHeader, // + }, + }, + sbuffer::SBufferIter, +}; + +use super::{ + Cmdq, + CmdqInner, + MessageFromGsp, // +}; + +/// 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() + } +} + +impl CmdqInner<'_> { + /// Sends `command` to the GSP, without splitting it. + /// + /// # 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>, + { + let dev = self.dev; + let seq = self.seq; + let size_in_bytes = command.size(); + + let init = |dst: &mut GspCommand<'_>| { + // 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: `rpc_header_mut()` and `cmd` are valid references, and not touched if the + // initializer fails. + unsafe { + pin_init::raw_try_init( + core::ptr::from_mut(dst.header.rpc_header_mut()), + rpc_header_init, + )?; + 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(()) + }; + + self.send_command_element(size_in_bytes, init) + } + + /// 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_single_command(command), + SplitState::Split(command, mut continuations) => { + self.send_single_command(command)?; + + while let Some(continuation) = continuations.next() { + // Turbofish needed because the compiler cannot infer M here. + self.send_single_command::<ContinuationRecord<'_>>(continuation)?; + } + + Ok(()) + } + } + } +} + +impl Cmdq<'_> { + /// Sends `command` to the GSP and waits for the reply. + /// + /// Messages with non-matching function codes are silently consumed until the expected reply + /// arrives. + /// + /// The queue is locked for the entire send+receive cycle to ensure that no other command can + /// be interleaved. + /// + /// # Errors + /// + /// - `ETIMEDOUT` if space does not become available to send the command, or if the reply is + /// not received within the timeout. + /// - `EIO` if the variable payload requested by the command has not been entirely + /// written to by its [`CommandToGsp::init_variable_payload`] method. + /// + /// Error codes returned by the command and reply initializers are propagated as-is. + pub(crate) fn send_command<M>(&self, command: M) -> Result<M::Reply> + where + M: CommandToGsp, + M::Reply: MessageFromGsp, + Error: From<M::InitError>, + Error: From<<M::Reply as MessageFromGsp>::InitError>, + { + let mut inner = self.inner.lock(); + inner.send_command(command)?; + + loop { + match inner.receive_msg::<M::Reply>(Self::RECEIVE_TIMEOUT) { + Ok(reply) => break Ok(reply), + Err(ERANGE) => continue, + Err(e) => break Err(e), + } + } + } + + /// Sends `command` to the GSP without waiting for a reply. + /// + /// # Errors + /// + /// - `ETIMEDOUT` if space does not become available within the timeout. + /// - `EIO` if the variable payload requested by the command has not been entirely + /// written to by its [`CommandToGsp::init_variable_payload`] method. + /// + /// Error codes returned by the command initializers are propagated as-is. + pub(crate) fn send_command_no_wait<M>(&self, command: M) -> Result + where + M: CommandToGsp<Reply = NoReply>, + Error: From<M::InitError>, + { + self.inner.lock().send_command(command) + } +} diff --git a/drivers/gpu/nova-core/gsp/cmdq/continuation.rs b/drivers/gpu/nova-core/gsp/cmdq/rpc/continuation.rs similarity index 100% rename from drivers/gpu/nova-core/gsp/cmdq/continuation.rs rename to drivers/gpu/nova-core/gsp/cmdq/rpc/continuation.rs diff --git a/drivers/gpu/nova-core/gsp/commands.rs b/drivers/gpu/nova-core/gsp/commands.rs index e087c9e8c35c..4322a4d554f9 100644 --- a/drivers/gpu/nova-core/gsp/commands.rs +++ b/drivers/gpu/nova-core/gsp/commands.rs @@ -23,8 +23,8 @@ gpu::Chipset, gsp::{ cmdq::{ + rpc::CommandToGsp, Cmdq, - CommandToGsp, MessageFromGsp, NoReply, // }, -- 2.55.0
