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 <[email protected]> Co-developed-by: Zhi Wang <[email protected]> Signed-off-by: Zhi Wang <[email protected]> Signed-off-by: Alok Kumar <[email protected]> --- 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<Range<u64>>, /// 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 890462beb11f..c9258ee9e059 100644 --- a/drivers/gpu/nova-core/gsp/commands/gmc.rs +++ b/drivers/gpu/nova-core/gsp/commands/gmc.rs @@ -145,7 +145,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<core::array::IntoIter<&[u8], 2>>) -> Result<Self> { let bytes = payload.flush_into_kvec(GFP_KERNEL)?; @@ -184,6 +185,7 @@ fn read(payload: &mut SBufferIter<core::array::IntoIter<&[u8], 2>>) -> Result<Se bar1_pde_base: reply.bar1_pde_base(), usable_fb_regions, total_fb_end: reply.total_fb_end().ok_or(EINVAL)?, + fifo_engine_list: reply.fifo_engine_list(), }) } } diff --git a/drivers/gpu/nova-core/gsp/fw/commands.rs b/drivers/gpu/nova-core/gsp/fw/commands.rs index bda6a7ef5ef7..07115ccf3e95 100644 --- a/drivers/gpu/nova-core/gsp/fw/commands.rs +++ b/drivers/gpu/nova-core/gsp/fw/commands.rs @@ -21,8 +21,10 @@ nvkv_decode, nvkv_encode, Accumulated, + DecoderValue, Encodable, Encoder, + Indexed, Key, Required, TruncatingArray, // @@ -307,6 +309,52 @@ pub(crate) fn new(ctx: &GspBootContext<'_, '_>) -> Result<Self> { // 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<DecoderValue<'_>> for FifoEngineCount { + type Error = Error; + + fn try_from(value: DecoderValue<'_>) -> Result<Self> { + 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. @@ -317,6 +365,9 @@ pub(crate) struct GspInitResponseSchema => GspInitResponse { fb_regions: Accumulated<FbRegionSchema>, bar1_pde_base: Required<Key<u64, 0x1020>>, vmmu_segment_size: Key<u64, 0x1050>, + fifo_engine_count: Key<FifoEngineCount, 0x0500>, + fifo_engine_gmc_ids: Indexed<u32, MAX_FIFO_ENGINES, 0x0501>, + fifo_engine_flags: Indexed<u32, MAX_FIFO_ENGINES, 0x0502>, } } @@ -328,6 +379,9 @@ pub(crate) struct GspInitResponse { fb_regions: KVVec<FbRegion>, 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 { @@ -381,6 +435,29 @@ pub(crate) fn total_fb_end(&self) -> Option<u64> { .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
