gpu: nova-core: gsp: add continuation record support
Subsystems (4)
v3 cover gpu: nova-core: gsp: add continuation record support
GSP commands over 16 pages need to be sent using "continuation records" which essentially means splitting the payload over multiple commands.
This series adds a command type ContinuationRecord which just writes its header and whatever payload it is given. It also adds types SplitState and SplitCommand which support splitting a large RPC into smaller ones while transparently letting regular sized RPCs be sent without extra copies.
The send pathway uses SplitState to send all commands, but if the command fits into 16 pages, it still writes directly into the command queue. If it is larger than 16 pages and needs continuation records, it writes into a staging buffer, so there is one copy.
---
Changes in v3:
- Rebased onto drm-rust-next
- Added some doc comments
- Reworded command_size doc comment
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Added doccoments
- Renamed driver_bytes_available_to_write to driver_write_area_size
- allocate_command_with_timeout merged allocate_command with timeout parameter
- Replaced hardcoded GSP_PAGE_SIZE * 16 with bindings
- Changed oversized command error from EIO to EMSGSIZE
- Added EMSGSIZE to kernel/error.rs
- Split WrappingCommand functionality into SplitState + SplitCommand enum
- Made max_size a const (MAX_CMD_SIZE)
- Removed send_continuation_record + added comment for type inference
- send_single_command now consumes the command
- Extracted command_size + used in SplitState
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Eliot Courtney (9):
gpu: nova-core: gsp: sort MsgFunction variants alphabetically
gpu: nova-core: gsp: add mechanism to wait for space on command queue
rust: add EMSGSIZE error code
gpu: nova-core: gsp: add checking oversized commands
gpu: nova-core: gsp: clarify invariant on command queue
gpu: nova-core: gsp: unconditionally call variable payload handling
gpu: nova-core: gsp: add command_size helper
gpu: nova-core: gsp: support large RPCs via continuation record
gpu: nova-core: gsp: add tests for SplitState
drivers/gpu/nova-core/gsp/cmdq.rs | 116 +++++++--- drivers/gpu/nova-core/gsp/commands.rs | 258 +++++++++++++++++++++- drivers/gpu/nova-core/gsp/fw.rs | 75 ++++--- drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs | 1 + rust/kernel/error.rs | 1 + 5 files changed, 393 insertions(+), 58 deletions(-)
---
base-commit: 4a49fe23e357b48845e31fe9c28a802c05458198
change-id: 20260203-cmdq-continuation-b99f3d5966c3
Best regards,
Signature · 1 line
Eliot Courtney <[email protected]>
PATCH v3 1/9 gpu: nova-core: gsp: sort MsgFunction variants alphabetically
There is no particular order required here and keeping them alphabetical will help preventing future mistakes.
--- drivers/gpu/nova-core/gsp/fw.rs | 67 +++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 32 deletions(-)
drivers/gpu/nova-core/gsp/fw.rs +35 -32
@@ -191,34 +191,34 @@ pub(crate) fn new(gsp_firmware: &GspFirmware, fb_layout: &FbLayout) -> Self {#[repr( u32 )] pub (crate )enum MsgFunction {// Common function codes -Nop = bindings::NV_VGPU_MSG_FUNCTION_NOP , -SetGuestSystemInfo = bindings::NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO , -AllocRoot = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_ROOT , +AllocChannelDma = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA , +AllocCtxDma = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA ,AllocDevice = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_DEVICE ,AllocMemory = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY , -AllocCtxDma = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA , -AllocChannelDma = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA , -MapMemory = bindings::NV_VGPU_MSG_FUNCTION_MAP_MEMORY , -BindCtxDma = bindings::NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA ,AllocObject = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT , +AllocRoot = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_ROOT , +BindCtxDma = bindings::NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA ,Free = bindings::NV_VGPU_MSG_FUNCTION_FREE , -Log = bindings::NV_VGPU_MSG_FUNCTION_LOG ,GetGspStaticInfo = bindings::NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO , -SetRegistry = bindings::NV_VGPU_MSG_FUNCTION_SET_REGISTRY , -GspSetSystemInfo = bindings::NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO , +GetStaticInfo = bindings::NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO ,GspInitPostObjGpu = bindings::NV_VGPU_MSG_FUNCTION_GSP_INIT_POST_OBJGPU ,GspRmControl = bindings::NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL , -GetStaticInfo = bindings::NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO , +GspSetSystemInfo = bindings::NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO , +Log = bindings::NV_VGPU_MSG_FUNCTION_LOG , +MapMemory = bindings::NV_VGPU_MSG_FUNCTION_MAP_MEMORY , +Nop = bindings::NV_VGPU_MSG_FUNCTION_NOP , +SetGuestSystemInfo = bindings::NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO , +SetRegistry = bindings::NV_VGPU_MSG_FUNCTION_SET_REGISTRY ,// Event codes GspInitDone = bindings::NV_VGPU_MSG_EVENT_GSP_INIT_DONE , +GspLockdownNotice = bindings::NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE , +GspPostNoCat = bindings::NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD ,GspRunCpuSequencer = bindings::NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER , -PostEvent = bindings::NV_VGPU_MSG_EVENT_POST_EVENT , -RcTriggered = bindings::NV_VGPU_MSG_EVENT_RC_TRIGGERED ,MmuFaultQueued = bindings::NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED ,OsErrorLog = bindings::NV_VGPU_MSG_EVENT_OS_ERROR_LOG , -GspPostNoCat = bindings::NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD , -GspLockdownNotice = bindings::NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE , +PostEvent = bindings::NV_VGPU_MSG_EVENT_POST_EVENT , +RcTriggered = bindings::NV_VGPU_MSG_EVENT_RC_TRIGGERED ,UcodeLibOsPrint = bindings::NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT , }
@@ -227,38 +227,41 @@ impl TryFrom<u32> for MsgFunction {fn try_from (value:u32 ) ->Result <MsgFunction > {match value { - bindings::NV_VGPU_MSG_FUNCTION_NOP =>Ok (MsgFunction ::Nop ), - bindings::NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO => { -Ok (MsgFunction ::SetGuestSystemInfo ) - } - bindings::NV_VGPU_MSG_FUNCTION_ALLOC_ROOT =>Ok (MsgFunction ::AllocRoot ), +// Common function codes + bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA =>Ok (MsgFunction ::AllocChannelDma ), + bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA =>Ok (MsgFunction ::AllocCtxDma ), bindings::NV_VGPU_MSG_FUNCTION_ALLOC_DEVICE =>Ok (MsgFunction ::AllocDevice ), bindings::NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY =>Ok (MsgFunction ::AllocMemory ), - bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CTX_DMA =>Ok (MsgFunction ::AllocCtxDma ), - bindings::NV_VGPU_MSG_FUNCTION_ALLOC_CHANNEL_DMA =>Ok (MsgFunction ::AllocChannelDma ), - bindings::NV_VGPU_MSG_FUNCTION_MAP_MEMORY =>Ok (MsgFunction ::MapMemory ), - bindings::NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA =>Ok (MsgFunction ::BindCtxDma ), bindings::NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT =>Ok (MsgFunction ::AllocObject ), + bindings::NV_VGPU_MSG_FUNCTION_ALLOC_ROOT =>Ok (MsgFunction ::AllocRoot ), + bindings::NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA =>Ok (MsgFunction ::BindCtxDma ), bindings::NV_VGPU_MSG_FUNCTION_FREE =>Ok (MsgFunction ::Free ), - bindings::NV_VGPU_MSG_FUNCTION_LOG =>Ok (MsgFunction ::Log ), bindings::NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO =>Ok (MsgFunction ::GetGspStaticInfo ), - bindings::NV_VGPU_MSG_FUNCTION_SET_REGISTRY =>Ok (MsgFunction ::SetRegistry ), - bindings::NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO =>Ok (MsgFunction ::GspSetSystemInfo ), + bindings::NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO =>Ok (MsgFunction ::GetStaticInfo ), bindings::NV_VGPU_MSG_FUNCTION_GSP_INIT_POST_OBJGPU => {Ok (MsgFunction ::GspInitPostObjGpu ) } bindings::NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL =>Ok (MsgFunction ::GspRmControl ), - bindings::NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO =>Ok (MsgFunction ::GetStaticInfo ), + bindings::NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO =>Ok (MsgFunction ::GspSetSystemInfo ), + bindings::NV_VGPU_MSG_FUNCTION_LOG =>Ok (MsgFunction ::Log ), + bindings::NV_VGPU_MSG_FUNCTION_MAP_MEMORY =>Ok (MsgFunction ::MapMemory ), + bindings::NV_VGPU_MSG_FUNCTION_NOP =>Ok (MsgFunction ::Nop ), + bindings::NV_VGPU_MSG_FUNCTION_SET_GUEST_SYSTEM_INFO => { +Ok (MsgFunction ::SetGuestSystemInfo ) + } + bindings::NV_VGPU_MSG_FUNCTION_SET_REGISTRY =>Ok (MsgFunction ::SetRegistry ), + +// Event codes bindings::NV_VGPU_MSG_EVENT_GSP_INIT_DONE =>Ok (MsgFunction ::GspInitDone ), + bindings::NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE =>Ok (MsgFunction ::GspLockdownNotice ), + bindings::NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD =>Ok (MsgFunction ::GspPostNoCat ), bindings::NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER => {Ok (MsgFunction ::GspRunCpuSequencer ) } - bindings::NV_VGPU_MSG_EVENT_POST_EVENT =>Ok (MsgFunction ::PostEvent ), - bindings::NV_VGPU_MSG_EVENT_RC_TRIGGERED =>Ok (MsgFunction ::RcTriggered ), bindings::NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED =>Ok (MsgFunction ::MmuFaultQueued ), bindings::NV_VGPU_MSG_EVENT_OS_ERROR_LOG =>Ok (MsgFunction ::OsErrorLog ), - bindings::NV_VGPU_MSG_EVENT_GSP_POST_NOCAT_RECORD =>Ok (MsgFunction ::GspPostNoCat ), - bindings::NV_VGPU_MSG_EVENT_GSP_LOCKDOWN_NOTICE =>Ok (MsgFunction ::GspLockdownNotice ), + bindings::NV_VGPU_MSG_EVENT_POST_EVENT =>Ok (MsgFunction ::PostEvent ), + bindings::NV_VGPU_MSG_EVENT_RC_TRIGGERED =>Ok (MsgFunction ::RcTriggered ), bindings::NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT =>Ok (MsgFunction ::UcodeLibOsPrint ), _ =>Err (EINVAL ), }
Signature · 1 line
2.53.0
PATCH v3 2/9 gpu: nova-core: gsp: add mechanism to wait for space on command queue
In reply to Eliot CourtneyAdd a timeout to allocate_command which waits for space on the GSP command queue. It uses a similar timeout to nouveau.
This lets send_command wait for space to free up in the command queue. This is required to support continuation records which can fill up the queue.
--- drivers/gpu/nova-core/gsp/cmdq.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +27 -12
@@ -250,6 +250,19 @@ fn new(dev: &device::Device<device::Bound>) -> Result<Self> { } } +/// Returns the size of the region of the CPU message queue that the driver is currently allowed +/// to write to, in bytes. +fn driver_write_area_size (&self) ->usize { +let tx = self.cpu_write_ptr (); +let rx = self.gsp_read_ptr (); + +// `rx` and `tx` are both in `0..MSGQ_NUM_PAGES` per the invariants of `gsp_read_ptr` and +// `cpu_write_ptr`. The minimum value case is where `rx == 0` and `tx == MSGQ_NUM_PAGES - +// 1`, which gives `0 + MSGQ_NUM_PAGES - (MSGQ_NUM_PAGES - 1) - 1 == 0`. +let slots = (rx +MSGQ_NUM_PAGES - tx -1 ) %MSGQ_NUM_PAGES ; + num::u32_as_usize (slots) *GSP_PAGE_SIZE + } +/// Returns the region of the GSP message queue that the driver is currently allowed to read /// from. ///
@@ -281,15 +294,22 @@ fn new(dev: &device::Device<device::Bound>) -> Result<Self> { }/// Allocates a region on the command queue that is large enough to send a command of `size` -/// bytes. +/// bytes, waiting for space to become available based on the provided timeout. /// /// This returns a [`GspCommand`] ready to be written to by the caller. /// /// # Errors /// -/// - `EAGAIN` if the driver area is too small to hold the requested command. +/// - `ETIMEDOUT` if space does not become available within the timeout. /// - `EIO` if the command header is not properly aligned. -fn allocate_command (&mut self, size:usize ) ->Result <GspCommand <'_>> { +fn allocate_command (&mut self, size:usize , timeout:Delta ) ->Result <GspCommand <'_>> { +read_poll_timeout ( + ||Ok (self.driver_write_area_size ()), + |available_bytes| *available_bytes >=size_of ::<GspMsgElement >() + size, +Delta ::ZERO , + timeout, + )?; +// Get the current writable area as an array of bytes. let (slice_1, slice_2) = {let (slice_1, slice_2) = self.driver_write_area ();
@@ -298,13 +318,6 @@ fn allocate_command(&mut self, size: usize) -> Result<GspCommand<'_>> { (slice_1.as_flattened_mut (), slice_2.as_flattened_mut ()) }; -// If the GSP is still processing previous messages the shared region -// may be full in which case we will have to retry once the GSP has -// processed the existing commands. -if size_of ::<GspMsgElement >() + size > slice_1.len () + slice_2.len () { -return Err (EAGAIN ); - } -// Extract area for the `GspMsgElement`. let (header, slice_1) =GspMsgElement ::from_bytes_mut_prefix (slice_1).ok_or (EIO )?;
@@ -497,7 +510,7 @@ fn notify_gsp(bar: &Bar0) {/// /// # Errors /// -/// - `EAGAIN` if there was not enough space in the command queue to send the command. +/// - `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. ///
@@ -509,7 +522,9 @@ pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result Error: From<M::InitError>, {let command_size =size_of ::<M ::Command >() + command.variable_payload_len (); -let dst = self.gsp_mem.allocate_command (command_size)?; +let dst = self + .gsp_mem + .allocate_command (command_size,Delta ::from_secs (1 ))?;// Extract area for the command itself. let (cmd, payload_1) =M ::Command ::from_bytes_mut_prefix (dst.contents.0 ).ok_or (EIO )?;
Signature · 1 line
2.53.0
PATCH v3 3/9 rust: add EMSGSIZE error code
In reply to Eliot CourtneyAdd the EMSGSIZE error code, which indicates that a message is too long.
--- rust/kernel/error.rs | 1 + 1 file changed, 1 insertion(+)
rust/kernel/error.rs +1 -0
@@ -67,6 +67,7 @@ macro_rules! declare_err {declare_err! (EDOM ,"Math argument out of domain of func." );declare_err! (ERANGE ,"Math result not representable." );declare_err! (EOVERFLOW ,"Value too large for defined data type." ); +declare_err! (EMSGSIZE ,"Message too long." );declare_err! (ETIMEDOUT ,"Connection timed out." );declare_err! (ERESTARTSYS ,"Restart the system call." );declare_err! (ERESTARTNOINTR ,"System call was interrupted by a signal and will be restarted." );
Signature · 1 line
2.53.0
Re: [PATCH v3 3/9] rust: add EMSGSIZE error code
Quoted 6 lines · Eliot Courtney
Add the EMSGSIZE error code, which indicates that a message is too long.
Tested-by Zhi Wang <[email protected]>Signed-off-by Eliot Courtney <[email protected]>
I checked that it is placed in the right spot in the list :)
Thanks!
Cheers,
Miguel
PATCH v3 4/9 gpu: nova-core: gsp: add checking oversized commands
In reply to Eliot CourtneyThe limit is 16 pages for a single command sent to the GSP. Return an error if allocate_command is called with a too large size.
--- drivers/gpu/nova-core/gsp/cmdq.rs | 7 ++++++- drivers/gpu/nova-core/gsp/fw.rs | 4 ++++ drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs | 1 + 3 files changed, 11 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gsp/cmdq.rs +6 -1
@@ -32,7 +32,8 @@ GspMsgElement, MsgFunction, MsgqRxHeader, - MsgqTxHeader, // + MsgqTxHeader, + GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, // }, PteArray, GSP_PAGE_SHIFT,
@@ -300,9 +301,13 @@ fn driver_write_area_size(&self) -> usize {/// /// # Errors /// +/// - `EMSGSIZE` if the command is larger than [`GSP_MSG_QUEUE_ELEMENT_SIZE_MAX`]. /// - `ETIMEDOUT` if space does not become available within the timeout. /// - `EIO` if the command header is not properly aligned. fn allocate_command (&mut self, size:usize , timeout:Delta ) ->Result <GspCommand <'_>> { +if size_of ::<GspMsgElement >() + size >GSP_MSG_QUEUE_ELEMENT_SIZE_MAX { +return Err (EMSGSIZE ); + }read_poll_timeout ( ||Ok (self.driver_write_area_size ()), |available_bytes| *available_bytes >= size_of::<GspMsgElement>() + size,
drivers/gpu/nova-core/gsp/fw.rs +4 -0
@@ -39,6 +39,10 @@ }, }; +/// Maximum size of a single GSP message queue element in bytes. +pub (crate )const GSP_MSG_QUEUE_ELEMENT_SIZE_MAX :usize = + num::u32_as_usize (bindings::GSP_MSG_QUEUE_ELEMENT_SIZE_MAX ); +/// Empty type to group methods related to heap parameters for running the GSP firmware. enum GspFwHeapParams {}
drivers/gpu/nova-core/gsp/fw/r570_144/bindings.rs +1 -0
@@ -43,6 +43,7 @@ fn fmt(&self, fmt: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {pub const GSP_FW_WPR_META_REVISION :u32 =1 ;pub const GSP_FW_WPR_META_MAGIC :i64 = -2577556379034558285 ;pub const REGISTRY_TABLE_ENTRY_TYPE_DWORD :u32 =1 ; +pub const GSP_MSG_QUEUE_ELEMENT_SIZE_MAX :u32 =65536 ;pub type __u8 = ffi::c_uchar ;pub type __u16 = ffi::c_ushort ;pub type __u32 = ffi::c_uint ;
Signature · 1 line
2.53.0
PATCH v3 5/9 gpu: nova-core: gsp: clarify invariant on command queue
In reply to Eliot CourtneyClarify why using only the first returned slice from allocate_command for the message headers is okay.
--- drivers/gpu/nova-core/gsp/cmdq.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gsp/cmdq.rs +3 -1
@@ -531,7 +531,9 @@ pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result .gsp_mem .allocate_command (command_size,Delta ::from_secs (1 ))?; -// Extract area for the command itself. +// 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.
Signature · 1 line
2.53.0
PATCH v3 6/9 gpu: nova-core: gsp: unconditionally call variable payload handling
In reply to Eliot CourtneyUnconditionally call the variable length payload code, which is a no-op if there is no such payload but could defensively catch some coding errors by e.g. checking that the allocated size is completely filled.
--- drivers/gpu/nova-core/gsp/cmdq.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +6 -8
@@ -545,16 +545,14 @@ pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result command.init ().__init (core::ptr::from_mut (cmd))?; } -// Fill the variable-length payload. -if command_size >size_of ::<M ::Command >() { -let mut sbuffer = -SBufferIter ::new_writer ([&mut payload_1[..], &mut dst.contents.1 [..]]); - command.init_variable_payload (&mut sbuffer)?; +// 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 ); - } +if !sbuffer.is_empty () { +return Err (EIO ); } +drop (sbuffer);// Compute checksum now that the whole message is ready. dst.header
Signature · 1 line
2.53.0
PATCH v3 7/9 gpu: nova-core: gsp: add command_size helper
In reply to Eliot CourtneyAdd a helper function which computes the size of a command.
--- drivers/gpu/nova-core/gsp/cmdq.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/gpu/nova-core/gsp/cmdq.rs +10 -1
@@ -450,6 +450,15 @@ struct GspMessage<'a> { contents: (&'a [u8], &'a [u8]), } +/// Computes the total size of the command (including its variable-length payload) without the +/// [`GspMsgElement`] header. +fn command_size <M >(command: &M ) ->usize +where +M :CommandToGsp , +{ +size_of ::<M ::Command >() + command.variable_payload_len () +} +/// GSP command queue. /// /// Provides the ability to send commands and receive messages from the GSP using a shared memory
@@ -526,7 +535,7 @@ pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result// This allows all error types, including `Infallible`, to be used for `M::InitError`. Error: From<M::InitError>, { -let command_size =size_of ::<M ::Command >() + command.variable_payload_len (); +let command_size =command_size (&command);let dst = self .gsp_mem .allocate_command (command_size,Delta ::from_secs (1 ))?;
Signature · 1 line
2.53.0
PATCH v3 8/9 gpu: nova-core: gsp: support large RPCs via continuation record
In reply to Eliot CourtneySplits large RPCs if necessary and sends the remaining parts using continuation records. RPCs that do not need continuation records continue to write directly into the command buffer. Ones that do write into a staging buffer first, so there is one copy.
Continuation record for receive is not necessary to support at the moment because those replies do not need to be read and are currently drained by retrying receive_msg on ERANGE.
--- drivers/gpu/nova-core/gsp/cmdq.rs | 43 +++++++++- drivers/gpu/nova-core/gsp/commands.rs | 144 +++++++++++++++++++++++++++++++++- drivers/gpu/nova-core/gsp/fw.rs | 4 + 3 files changed, 187 insertions(+), 4 deletions(-)
drivers/gpu/nova-core/gsp/cmdq.rs +40 -3
@@ -28,6 +28,10 @@use crate ::{ driver::Bar0 , gsp::{ + commands::{ +ContinuationRecord , +SplitState ,// + }, fw::{GspMsgElement ,MsgFunction ,
@@ -452,7 +456,7 @@ struct GspMessage<'a> {/// Computes the total size of the command (including its variable-length payload) without the /// [`GspMsgElement`] header. -fn command_size <M >(command: &M ) ->usize +pub (crate )fn command_size <M >(command: &M ) ->usize where M :CommandToGsp , {
@@ -520,7 +524,7 @@ fn notify_gsp(bar: &Bar0) { .write(bar); } -/// Sends `command` to the GSP. +/// Sends `command` to the GSP, without splitting it. /// /// # Errors ///
@@ -529,7 +533,7 @@ fn notify_gsp(bar: &Bar0) {/// 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 <M >(&mut self, bar: &Bar0 , command:M ) ->Result +fn send_single_command <M >(&mut self, bar: &Bar0 , command:M ) ->Result where M :CommandToGsp ,// This allows all error types, including `Infallible`, to be used for `M::InitError`.
@@ -588,6 +592,39 @@ pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> ResultOk (()) } +/// 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. +pub (crate )fn send_command <M >(&mut self, bar: &Bar0 , command:M ) ->Result +where +M :CommandToGsp , +Error :From <M ::InitError >, + { +let mut state =SplitState ::new (&command)?; + + self.send_single_command (bar, state.command (command))?; + +while let Some (continuation) = state.next_continuation_record () { +dev_dbg! ( + &self.dev, +"GSP RPC: send continuation: size=0x{:x}\n" , +command_size (&continuation), + ); +// Turbofish needed because the compiler cannot infer M here. + self.send_single_command ::<ContinuationRecord <'_>>(bar, 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
drivers/gpu/nova-core/gsp/commands.rs +143 -1
165 lines not on this page · raw message
drivers/gpu/nova-core/gsp/fw.rs +4 -0
@@ -202,6 +202,7 @@ pub(crate) enum MsgFunction { AllocObject = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT, AllocRoot = bindings::NV_VGPU_MSG_FUNCTION_ALLOC_ROOT, BindCtxDma = bindings::NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA, + ContinuationRecord = bindings::NV_VGPU_MSG_FUNCTION_CONTINUATION_RECORD, Free = bindings::NV_VGPU_MSG_FUNCTION_FREE, GetGspStaticInfo = bindings::NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO, GetStaticInfo = bindings::NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO,
@@ -239,6 +240,9 @@ fn try_from(value: u32) -> Result<MsgFunction> { bindings::NV_VGPU_MSG_FUNCTION_ALLOC_OBJECT => Ok(MsgFunction::AllocObject), bindings::NV_VGPU_MSG_FUNCTION_ALLOC_ROOT => Ok(MsgFunction::AllocRoot), bindings::NV_VGPU_MSG_FUNCTION_BIND_CTX_DMA => Ok(MsgFunction::BindCtxDma), + bindings::NV_VGPU_MSG_FUNCTION_CONTINUATION_RECORD => { + Ok(MsgFunction::ContinuationRecord) + } bindings::NV_VGPU_MSG_FUNCTION_FREE => Ok(MsgFunction::Free), bindings::NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO => Ok(MsgFunction::GetGspStaticInfo), bindings::NV_VGPU_MSG_FUNCTION_GET_STATIC_INFO => Ok(MsgFunction::GetStaticInfo),
Signature · 1 line
2.53.0
Alexandre Courbot
maintainerRe: [PATCH v3 8/9] gpu: nova-core: gsp: support large RPCs via continuation record
Hi Eliot,
On Thu Feb 26, 2026 at 8:45 PM JST, Eliot Courtney wrote:
<snip>
Quoted 13 lines
drivers/gpu/nova-core/gsp/cmdq.rs +9
-0@@ -588,6 +592,39 @@ pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> ResultOk (()) } +/// 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.
drivers/gpu/nova-core/gsp/cmdq.rs pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result · line 613
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +10
-0+/// +/// Error codes returned by the command initializers are propagated as-is. +pub (crate )fn send_command <M >(&mut self, bar: &Bar0 , command:M ) ->Result +where +M :CommandToGsp , +Error :From <M ::InitError >, + { +let mut state =SplitState ::new (&command)?; + + self.send_single_command (bar, state.command (command))?;
As we discussed offline these two lines were bothering me a bit, because we pass command twice and there is a possibility (although purely hypothetical) of API misuse. I think I found a way around it: we turn SplitState into an enum with a Single variant, that contains the original command, and a Split one, which contains the truncated initial command and its continuation records.
This simple change cascades into more simplifications in the new types introduced by this patch: SplitCommand is not an enum anymore, but the original command with a shorter payload. And the continuation records are their own self-contained type. This is achieved by allocating two KVVecs when we split, and taking advantage of the SBuffer we conveniently have at hand to fill them both.
I have pushed a branch with this idea implemented on top of yours in [1]
- please check it, but I think it brings a nice (if small) incremental improvement. It removes the API misuse potential, makes every type purely single-purpose, and doesn't need PhantomData or lifetimes, making it simpler overall IMHO.
[1] https://github.com/Gnurou/linux/tree/review/continuations
drivers/gpu/nova-core/gsp/cmdq.rs pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result · line 620
Quoted 7 lines
drivers/gpu/nova-core/gsp/cmdq.rs +7
-0+ +while let Some (continuation) = state.next_continuation_record () { +dev_dbg! ( + &self.dev, +"GSP RPC: send continuation: size=0x{:x}\n" , +command_size (&continuation), + );
send_single_command should already print the command, so I don't think we need this additional debug print?
Quoted 41 lines
drivers/gpu/nova-core/gsp/cmdq.rs +7
-0+// Turbofish needed because the compiler cannot infer M here. + self.send_single_command ::<ContinuationRecord <'_>>(bar, 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 drivers/gpu/nova-core/gsp/commands.rs +5
-1@@ -4,6 +4,7 @@ array, convert::Infallible, ffi::FromBytesUntilNulError, + marker::PhantomData, str::Utf8Error, // };@@ -22,13 +23,16 @@ driver::Bar0, gsp::{ cmdq::{ + command_size, Cmdq, CommandToGsp, MessageFromGsp, // }, fw::{ commands::*, - MsgFunction, // + GspMsgElement, + MsgFunction, + GSP_MSG_QUEUE_ELEMENT_SIZE_MAX, // }, }, sbuffer::SBufferIter,
drivers/gpu/nova-core/gsp/commands.rs pub(crate) fn get_gsp_info(cmdq: &mut Cmdq, bar: &Bar0) -> Result<GetGspStaticIn · line 251
Quoted 6 lines
drivers/gpu/nova-core/gsp/commands.rs +3
-0@@ -242,3 +246,141 @@ pub(crate) fn get_gsp_info(cmdq: &mut Cmdq, bar: &Bar0) -> Result<GetGspStaticIn } } } + +/// The `ContinuationRecord` command. +pub (crate )struct ContinuationRecord <'a> {
These new types are to be used by the command queue, and we don't want to use them elsewhere, so pub(super) seems more appropriate to me.
And actually, since they are more command queue infrastructure than pure "commands" that fulfill a given purpose, I'd suggest moving them into their own sub-module of gsp (named continuation or split maybe?). That way they don't get in the way of readers who just want to learn about GSP commands.
Re: [PATCH v3 8/9] gpu: nova-core: gsp: support large RPCs via continuation record
Quoted 34 lines · Alexandre Courbot
Quoted 9 lines
drivers/gpu/nova-core/gsp/cmdq.rs +9
-0+/// 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. drivers/gpu/nova-core/gsp/cmdq.rs pub(crate) fn send_command<M>(&mut self, bar: &Bar0, command: M) -> Result · line 613
Quoted 10 lines
drivers/gpu/nova-core/gsp/cmdq.rs +10
-0+/// +/// Error codes returned by the command initializers are propagated as-is. +pub (crate )fn send_command <M >(&mut self, bar: &Bar0 , command:M ) ->Result +where +M :CommandToGsp , +Error :From <M ::InitError >, + { +let mut state =SplitState ::new (&command)?; + + self.send_single_command (bar, state.command (command))?;As we discussed offline these two lines were bothering me a bit, because we pass
commandtwice and there is a possibility (although purely hypothetical) of API misuse. I think I found a way around it: we turnSplitStateinto an enum with aSinglevariant, that contains the original command, and aSplitone, which contains the truncated initial command and its continuation records.This simple change cascades into more simplifications in the new types introduced by this patch:
SplitCommandis not an enum anymore, but the original command with a shorter payload. And the continuation records are their own self-contained type. This is achieved by allocating twoKVVecs when we split, and taking advantage of theSBufferwe conveniently have at hand to fill them both.
Quoted 7 lines
I have pushed a branch with this idea implemented on top of yours in [1]
- please check it, but I think it brings a nice (if small) incremental improvement. It removes the API misuse potential, makes every type purely single-purpose, and doesn't needPhantomDataor lifetimes, making it simpler overall IMHO.[1] https://github.com/Gnurou/linux/tree/review/continuations
Thank you for looking into this and I agree this new design is better. We pay an extra allocation but it's rare that that path will be hit, so I agree it's worth it.
Added and thanks for your work on improving this API.
Quoted 7 lines
drivers/gpu/nova-core/gsp/commands.rs pub(crate) fn get_gsp_info(cmdq: &mut Cmdq, bar: &Bar0) -> Result<GetGspStaticIn · line 251
Quoted 3 lines
drivers/gpu/nova-core/gsp/commands.rs +3
-0+ +/// The `ContinuationRecord` command. +pub (crate )struct ContinuationRecord <'a> {These new types are to be used by the command queue, and we don't want to use them elsewhere, so
pub(super)seems more appropriate to me.
Quoted 5 lines
And actually, since they are more command queue infrastructure than pure "commands" that fulfill a given purpose, I'd suggest moving them into their own sub-module of
gsp(namedcontinuationorsplitmaybe?). That way they don't get in the way of readers who just want to learn about GSP commands.
Done.
PATCH v3 9/9 gpu: nova-core: gsp: add tests for SplitState
In reply to Eliot CourtneyAdd tests for SplitState. They cover boundary conditions at the split points to make sure the right number of continuation records are made. They also check that the data concatenated is correct.
--- drivers/gpu/nova-core/gsp/commands.rs | 114 ++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+)
drivers/gpu/nova-core/gsp/commands.rs +114 -0
@@ -384,3 +384,117 @@ fn init_variable_payload( } } } + +#[kunit_tests(nova_core_gsp_commands)] +mod tests { +use super ::*; + +struct TestPayload { + data:KVVec <u8 >, + } + +impl TestPayload { +fn generate_pattern (len:usize ) ->Result <KVVec <u8 >> { +let mut data =KVVec ::with_capacity (len,GFP_KERNEL )?; +for iin 0 ..len { + data.push (ias u8 ,GFP_KERNEL )?; + } +Ok (data) + } + +fn new (len:usize ) ->Result <Self > { +Ok (Self { + data:Self ::generate_pattern (len)?, + }) + } + } + +impl CommandToGsp for TestPayload { +const FUNCTION :MsgFunction =MsgFunction ::Nop ; +type Command = (); +type InitError =Infallible ; + +fn init (&self) ->impl Init <Self ::Command ,Self ::InitError > { + <()>::init_zeroed () + } + +fn variable_payload_len (&self) ->usize { + self.data.len () + } + +fn init_variable_payload ( + &self, + dst: &mut SBufferIter <core::array::IntoIter <&mut [u8 ],2 >>, + ) ->Result { + dst.write_all (self.data.as_slice ()) + } + } + +const MAX_CMD_SIZE :usize =SplitState ::<TestPayload >::MAX_CMD_SIZE ; + +fn read_payload (cmd: &impl CommandToGsp ) ->Result <KVVec <u8 >> { +let len = cmd.variable_payload_len (); +let mut buf =KVVec ::from_elem (0u8 , len,GFP_KERNEL )?; +let mut sbuf =SBufferIter ::new_writer ([buf.as_mut_slice (), &mut []]); + cmd.init_variable_payload (&mut sbuf)?; +drop (sbuf); +Ok (buf) + } + +struct SplitTest { + payload_size:usize , + num_continuations:usize , + } + +fn check_split (t:SplitTest ) ->Result { +let payload =TestPayload ::new (t.payload_size)?; +let mut state =SplitState ::new (&payload)?; + +let mut buf =read_payload (&state.command (payload))?; +assert! (buf.len () <=MAX_CMD_SIZE ); + +let mut num_continuations =0 ; +while let Some (cont) = state.next_continuation_record () { +let payload =read_payload (&cont)?; +assert! (payload.len () <=MAX_CMD_SIZE ); + buf.extend_from_slice (&payload,GFP_KERNEL )?; + num_continuations +=1 ; + } + +assert_eq! (num_continuations, t.num_continuations); +assert_eq! ( + buf.as_slice (), +TestPayload ::generate_pattern (t.payload_size)?.as_slice () + ); +Ok (()) + } + +#[test] +fn split_command () ->Result { +check_split (SplitTest { + payload_size:0 , + num_continuations:0 , + })?; +check_split (SplitTest { + payload_size:MAX_CMD_SIZE , + num_continuations:0 , + })?; +check_split (SplitTest { + payload_size:MAX_CMD_SIZE +1 , + num_continuations:1 , + })?; +check_split (SplitTest { + payload_size:MAX_CMD_SIZE *2 , + num_continuations:1 , + })?; +check_split (SplitTest { + payload_size:MAX_CMD_SIZE *2 +1 , + num_continuations:2 , + })?; +check_split (SplitTest { + payload_size:MAX_CMD_SIZE *3 +MAX_CMD_SIZE /2 , + num_continuations:3 , + })?; +Ok (()) + } +}
Signature · 1 line
2.53.0
Alexandre Courbot
maintainerRe: [PATCH v3 9/9] gpu: nova-core: gsp: add tests for SplitState
Quoted 36 lines · Eliot Courtney
Add tests for SplitState. They cover boundary conditions at the split points to make sure the right number of continuation records are made. They also check that the data concatenated is correct.
Tested-by Zhi Wang <[email protected]>Signed-off-by Eliot Courtney <[email protected]>--- drivers/gpu/nova-core/gsp/commands.rs | 114 ++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+)drivers/gpu/nova-core/gsp/commands.rs +18
-0@@ -384,3 +384,117 @@ fn init_variable_payload( } } } + +#[kunit_tests(nova_core_gsp_commands)] +mod tests { +use super ::*; + +struct TestPayload { + data:KVVec <u8 >, + } + +impl TestPayload { +fn generate_pattern (len:usize ) ->Result <KVVec <u8 >> { +let mut data =KVVec ::with_capacity (len,GFP_KERNEL )?; +for iin 0 ..len { + data.push (ias u8 ,GFP_KERNEL )?; + } +Ok (data) + } +
drivers/gpu/nova-core/gsp/commands.rs fn init_variable_payload( · line 414
Quoted 10 lines
drivers/gpu/nova-core/gsp/commands.rs +10
-0+fn new (len:usize ) ->Result <Self > { +Ok (Self { + data:Self ::generate_pattern (len)?, + }) + } + } + +impl CommandToGsp for TestPayload { +const FUNCTION :MsgFunction =MsgFunction ::Nop ; +type Command = ();
Since we are testing the size of the data written on the command queue, can you make the command itself larger than 0 bytes? Otherwise there is a potential for missing errors.
I'm saying that because it actually happened to me, I used MAX_CMD_SIZE instead of MAX_CMD_SIZE - size_of::<C::Command>() somewhere, and despite it being broken the tests were passing.
Re: [PATCH v3 9/9] gpu: nova-core: gsp: add tests for SplitState
Quoted 32 lines · Alexandre Courbot
Quoted 17 lines
drivers/gpu/nova-core/gsp/commands.rs +17
-0+#[kunit_tests(nova_core_gsp_commands)] +mod tests { +use super ::*; + +struct TestPayload { + data:KVVec <u8 >, + } + +impl TestPayload { +fn generate_pattern (len:usize ) ->Result <KVVec <u8 >> { +let mut data =KVVec ::with_capacity (len,GFP_KERNEL )?; +for iin 0 ..len { + data.push (ias u8 ,GFP_KERNEL )?; + } +Ok (data) + } +drivers/gpu/nova-core/gsp/commands.rs fn init_variable_payload( · line 414
Quoted 10 lines
drivers/gpu/nova-core/gsp/commands.rs +10
-0+fn new (len:usize ) ->Result <Self > { +Ok (Self { + data:Self ::generate_pattern (len)?, + }) + } + } + +impl CommandToGsp for TestPayload { +const FUNCTION :MsgFunction =MsgFunction ::Nop ; +type Command = ();Since we are testing the size of the data written on the command queue, can you make the command itself larger than 0 bytes? Otherwise there is a potential for missing errors.
Quoted 3 lines
I'm saying that because it actually happened to me, I used
MAX_CMD_SIZEinstead ofMAX_CMD_SIZE - size_of::<C::Command>()somewhere, and despite it being broken the tests were passing.
Yeah good idea, particularly since you ran into it too. Added.
Alexandre Courbot
maintainerRe: [PATCH v3 9/9] gpu: nova-core: gsp: add tests for SplitState
In reply to Eliot CourtneyQuoted 6 lines · Eliot Courtney
Add tests for SplitState. They cover boundary conditions at the split points to make sure the right number of continuation records are made. They also check that the data concatenated is correct.
Tested-by Zhi Wang <[email protected]>Signed-off-by Eliot Courtney <[email protected]>
Also I'd call this patch "add tests for continuation records". That's another reason to move the continuation record stuff to its own module, that way these tests can be contained to it and named accordingly instead of being mixed with potentially future command-related tests.