GSP_INIT reports the FIFO engine topology used to build the channel map for vGPU instances. Decode the count, GMC IDs and flags into GspStaticInfo and expose host-driven engines in firmware FIFO order, retaining repeated IDs. Reject counts and indexed entries beyond the driver's 64-entry table capacity rather than silently accepting a prefix. An omitted count remains zero. Cc: Alexandre Courbot Co-developed-by: Zhi Wang Signed-off-by: Zhi Wang Signed-off-by: Alok Kumar --- drivers/gpu/nova-core/gsp/commands.rs | 10 +++ drivers/gpu/nova-core/gsp/commands/gmc.rs | 4 +- drivers/gpu/nova-core/gsp/fw/commands.rs | 77 +++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/nova-core/gsp/commands.rs b/drivers/gpu/nova-core/gsp/commands.rs index 8b047744c36d..f5ec27995627 100644 --- a/drivers/gpu/nova-core/gsp/commands.rs +++ b/drivers/gpu/nova-core/gsp/commands.rs @@ -11,6 +11,8 @@ use kernel::prelude::*; +use super::fw::commands::FifoEngineList; + /// The static GPU configuration, which GSP-RM reports in reply to [`GspInit`](gmc::GspInit). pub struct GspStaticInfo { gpu_name: [u8; Self::MAX_GPU_NAME_LEN], @@ -23,6 +25,8 @@ pub struct GspStaticInfo { pub(crate) usable_fb_regions: KVec>, /// Exclusive end of the FB physical address space. pub(crate) total_fb_end: u64, + /// Host-driven engines in firmware FIFO order. + fifo_engine_list: FifoEngineList, } /// Error type for [`GspStaticInfo::gpu_name`]. @@ -63,6 +67,12 @@ pub fn gpu_short_name(&self) -> core::result::Result<&str, GpuNameError> { .map_err(GpuNameError::InvalidUtf8) } + /// Returns the host-driven engines in their firmware FIFO order. + #[expect(dead_code)] + pub(crate) fn fifo_engine_list(&self) -> FifoEngineList { + self.fifo_engine_list + } + /// Returns the total usable VRAM size in bytes, i.e. the summed lengths of all usable FB /// regions. pub fn vram_size(&self) -> u64 { diff --git a/drivers/gpu/nova-core/gsp/commands/gmc.rs b/drivers/gpu/nova-core/gsp/commands/gmc.rs index 27bd67fbe93e..4f813a780d1b 100644 --- a/drivers/gpu/nova-core/gsp/commands/gmc.rs +++ b/drivers/gpu/nova-core/gsp/commands/gmc.rs @@ -144,7 +144,8 @@ impl MessageFromGsp for GspStaticInfo { /// # Errors /// /// - `EINVAL` if the payload is not a whole number of NVKV words, if the stream is malformed - /// or omits a required key, or if GSP-RM reported no framebuffer region. + /// or omits a required key, if GSP-RM reported no framebuffer region, or if the FIFO engine + /// count or indexed entries exceed the supported table capacity. /// - `ENOMEM` if the words or the decoded regions cannot be allocated. fn read(payload: &mut SBufferIter>) -> Result { let bytes = payload.flush_into_kvec(GFP_KERNEL)?; @@ -183,6 +184,7 @@ fn read(payload: &mut SBufferIter>) -> Result) -> Result { // Decode: +const MAX_FIFO_ENGINES: usize = 64; + +/// Bit mask for `NVGMC_SC_ENGINE_FLAGS_IS_HOST_DRIVEN`. +const ENGINE_FLAGS_IS_HOST_DRIVEN: u32 = 1 << 0; + +/// Host-driven GMC engine IDs in hardware FIFO order, including any repeated IDs. +/// +/// # Invariants +/// +/// `count` is at most [`MAX_FIFO_ENGINES`]. The first `count` slots are the retained engine IDs. +#[derive(Copy, Clone)] +pub(crate) struct FifoEngineList { + gmc_ids: [u32; MAX_FIFO_ENGINES], + count: usize, +} + +impl FifoEngineList { + #[expect(dead_code)] + pub(crate) fn gmc_ids(&self) -> &[u32] { + // PANIC: The type invariant bounds `count` by the array capacity. + &self.gmc_ids[..self.count] + } +} + +/// A FIFO engine count that fits the supported tables. +/// +/// # Invariants +/// +/// The count is at most [`MAX_FIFO_ENGINES`]. +#[derive(Default)] +struct FifoEngineCount(usize); + +impl TryFrom> for FifoEngineCount { + type Error = Error; + + fn try_from(value: DecoderValue<'_>) -> Result { + let count = crate::num::u32_as_usize(u32::try_from(value)?); + if count > MAX_FIFO_ENGINES { + Err(EINVAL) + } else { + // INVARIANT: The count was checked against the table capacity above. + Ok(Self(count)) + } + } +} + // Should decode with UnknownKeyPolicy::Ignore. nvkv_decode! { /// Schema for the `GSP_INIT` response. @@ -326,6 +374,9 @@ pub(crate) struct GspInitResponseSchema => GspInitResponse { fb_regions: Accumulated, bar1_pde_base: Required>, vmmu_segment_size: Key, + fifo_engine_count: Key, + fifo_engine_gmc_ids: Indexed, + fifo_engine_flags: Indexed, } } @@ -337,6 +388,9 @@ pub(crate) struct GspInitResponse { fb_regions: KVVec, bar1_pde_base: u64, vmmu_segment_size: u64, + fifo_engine_count: FifoEngineCount, + fifo_engine_gmc_ids: [u32; MAX_FIFO_ENGINES], + fifo_engine_flags: [u32; MAX_FIFO_ENGINES], } impl GspInitResponse { @@ -390,6 +444,29 @@ pub(crate) fn total_fb_end(&self) -> Option { .checked_add(1) } + /// Returns the host-driven engines in their firmware FIFO order. + pub(crate) fn fifo_engine_list(&self) -> FifoEngineList { + // INVARIANT: The list starts empty and appends at most one ID per supported input slot. + let mut fifo_engine_list = FifoEngineList { + gmc_ids: [0; MAX_FIFO_ENGINES], + count: 0, + }; + for (&gmc_id, &flags) in self + .fifo_engine_gmc_ids + .iter() + .zip(&self.fifo_engine_flags) + .take(self.fifo_engine_count.0) + { + if flags & ENGINE_FLAGS_IS_HOST_DRIVEN != 0 { + // PANIC: At most one slot is filled per input, and the input has at most + // MAX_FIFO_ENGINES entries, so the next retained ID always fits. + fifo_engine_list.gmc_ids[fifo_engine_list.count] = gmc_id; + fifo_engine_list.count += 1; + } + } + fifo_engine_list + } + /// Returns the BAR1 page directory entry base address. pub(crate) fn bar1_pde_base(&self) -> u64 { self.bar1_pde_base -- 2.53.0