From: Zhi Wang Creating an instance requires its VF assignment and the corresponding vGPU properties before reserving resources. Decode the VF assignment and NVKV property replies at their query entry points. Validate the returned type ID and nonzero instance limit, and initialize the decoded properties on the heap. Co-developed-by: Eliot Courtney Signed-off-by: Eliot Courtney Co-developed-by: Alexandre Courbot Signed-off-by: Alexandre Courbot Signed-off-by: Zhi Wang Signed-off-by: Alok Kumar --- drivers/gpu/nova-core/gsp.rs | 1 + drivers/gpu/nova-core/gsp/fw.rs | 2 +- drivers/gpu/nova-core/sbuffer.rs | 7 ++ drivers/gpu/nova-core/vgpu.rs | 11 ++ drivers/gpu/nova-core/vgpu/commands.rs | 126 ++++++++++++++++++++++ drivers/gpu/nova-core/vgpu/fw.rs | 6 ++ drivers/gpu/nova-core/vgpu/fw/commands.rs | 75 +++++++++++++ 7 files changed, 227 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/nova-core/vgpu/commands.rs create mode 100644 drivers/gpu/nova-core/vgpu/fw/commands.rs diff --git a/drivers/gpu/nova-core/gsp.rs b/drivers/gpu/nova-core/gsp.rs index 4bce365c96e5..125dfd06aab1 100644 --- a/drivers/gpu/nova-core/gsp.rs +++ b/drivers/gpu/nova-core/gsp.rs @@ -29,6 +29,7 @@ pub(crate) use logbuffer::RetainedLogs; pub(crate) use fw::{ + GmcCommandId, GspFmcBootParams, GspFwWprMeta, LibosMemoryRegionInitArgument, diff --git a/drivers/gpu/nova-core/gsp/fw.rs b/drivers/gpu/nova-core/gsp/fw.rs index 6daabb6aea78..5158f0e7c12c 100644 --- a/drivers/gpu/nova-core/gsp/fw.rs +++ b/drivers/gpu/nova-core/gsp/fw.rs @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. pub(crate) mod commands; -mod r000_00; +pub(crate) mod r000_00; // Alias to avoid repeating the version number with every use. use r000_00 as bindings; diff --git a/drivers/gpu/nova-core/sbuffer.rs b/drivers/gpu/nova-core/sbuffer.rs index b8c01104c255..fd410aef2a1f 100644 --- a/drivers/gpu/nova-core/sbuffer.rs +++ b/drivers/gpu/nova-core/sbuffer.rs @@ -161,6 +161,13 @@ pub(crate) fn read_exact(&mut self, mut dst: &mut [u8]) -> Result { Ok(()) } + pub(crate) fn read(&mut self) -> Result { + let mut ret = T::new_zeroed(); + self.read_exact(ret.as_mut_bytes())?; + + Ok(ret) + } + /// Read all the remaining data into a [`KVec`]. /// /// `self` will be empty after this operation. diff --git a/drivers/gpu/nova-core/vgpu.rs b/drivers/gpu/nova-core/vgpu.rs index 5fd9641db579..0934932e051d 100644 --- a/drivers/gpu/nova-core/vgpu.rs +++ b/drivers/gpu/nova-core/vgpu.rs @@ -4,6 +4,7 @@ use core::num::NonZero; use kernel::{ + bitfield, device, pci, prelude::*, // @@ -21,10 +22,20 @@ gsp::commands::FifoEngineList, // }; +mod commands; mod fw; mod hal; mod vram; +bitfield! { + struct Dbdf(u32) { + 2:0 function; + 7:3 device; + 15:8 bus; + 31:16 domain; + } +} + /// vGPU state detected during GPU construction. #[derive(Debug, Clone, Copy)] pub(crate) enum VgpuState { diff --git a/drivers/gpu/nova-core/vgpu/commands.rs b/drivers/gpu/nova-core/vgpu/commands.rs new file mode 100644 index 000000000000..66669948230a --- /dev/null +++ b/drivers/gpu/nova-core/vgpu/commands.rs @@ -0,0 +1,126 @@ +// SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + +//! vGPU firmware command operations. +//! +//! Sends requests, checks responses and coordinates the firmware command +//! sequences used by instance lifecycle operations. + +use core::convert::Infallible; + +use kernel::{ + device, + prelude::*, // +}; + +use crate::{ + gsp::{ + cmdq::{ + gmc::{ + CommandToGsp, + MessageFromGsp, + MessagePayload, // + }, + Cmdq, + }, + nvkv::{ + nvkv_words, + Decoder, + Schema, + UnknownKeyPolicy, // + }, + GmcCommandId, + GSP_PAGE_SIZE, // + }, + vgpu::{ + fw::commands::{ + AssignedVfVgpuType, + VgpuProperties, + VgpuPropertiesSchema, // + }, + Dbdf, + }, +}; + +struct QueryAssignedVfVgpuType(Dbdf); + +impl CommandToGsp for QueryAssignedVfVgpuType { + const COMMAND: GmcCommandId = GmcCommandId::QueryAssignedVfVgpuType; + type Command = u64; + type Reply = AssignedVfVgpuType; + const MAX_REPLY_SIZE: u32 = cv!(size_of::()); + type InitError = Infallible; + + fn init(&self) -> impl Init { + u64::from(self.0.into_raw()).to_le() + } +} + +impl MessageFromGsp for AssignedVfVgpuType { + type InitError = Error; + + fn read(payload: &mut MessagePayload<'_>) -> Result { + payload.read() + } +} + +/// Query the vGPU type assigned to a VF by its DBDF. +#[expect(dead_code)] +pub(super) fn query_assigned_vf_type( + _dev: &device::Device, + cmdq: &Cmdq<'_>, + dbdf: Dbdf, +) -> Result { + let reply = cmdq + .send_gmc_command(QueryAssignedVfVgpuType(dbdf), |_| Ok(()))? + .map_err(|_| EIO)?; + + Ok(reply.vgpu_type_id) +} + +struct QueryVgpuProperties(u32); + +impl CommandToGsp for QueryVgpuProperties { + const COMMAND: GmcCommandId = GmcCommandId::QueryVgpuProperties; + type Command = u32; + type Reply = KBox; + /// The reply is NVKV-encoded and varies in length. + const MAX_REPLY_SIZE: u32 = cv!(GSP_PAGE_SIZE); + type InitError = Infallible; + + fn init(&self) -> impl Init { + self.0.to_le() + } +} + +impl MessageFromGsp for KBox { + type InitError = Error; + + fn read(payload: &mut MessagePayload<'_>) -> Result { + let bytes = payload.flush_into_kvec(GFP_KERNEL)?; + let words = nvkv_words(&bytes, &[])?; + let decoder = Decoder::new(&words, UnknownKeyPolicy::Ignore); + let mut schema = KBox::init(VgpuPropertiesSchema::init(), GFP_KERNEL)?; + let properties = KBox::try_init(decoder.decode(&mut *schema)?, GFP_KERNEL)?; + + Ok(properties) + } +} + +/// Query and decode the firmware properties of one vGPU type. +#[expect(dead_code)] +pub(super) fn query_vgpu_properties( + _dev: &device::Device, + cmdq: &Cmdq<'_>, + type_id: u32, +) -> Result> { + let properties = cmdq + .send_gmc_command(QueryVgpuProperties(type_id), |_| Ok(()))? + .map_err(|_| EIO)?; + + if properties.type_id != type_id || properties.max_instance == 0 { + Err(EINVAL) + } else { + Ok(properties) + } +} diff --git a/drivers/gpu/nova-core/vgpu/fw.rs b/drivers/gpu/nova-core/vgpu/fw.rs index e4571cfe11a3..2d32e196a771 100644 --- a/drivers/gpu/nova-core/vgpu/fw.rs +++ b/drivers/gpu/nova-core/vgpu/fw.rs @@ -1,4 +1,10 @@ // SPDX-License-Identifier: GPL-2.0 // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +//! vGPU firmware interface. +//! +//! Exposes command constants and communication buffer layout types from +//! raw firmware bindings. + +pub(super) mod commands; mod r000_00; diff --git a/drivers/gpu/nova-core/vgpu/fw/commands.rs b/drivers/gpu/nova-core/vgpu/fw/commands.rs new file mode 100644 index 000000000000..d1864d7ab530 --- /dev/null +++ b/drivers/gpu/nova-core/vgpu/fw/commands.rs @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + +//! Wire types and codecs for vGPU commands. +//! +//! Defines request encoders and response schemas independently of command +//! submission and instance lifecycle operations. + +use kernel::{ + alloc::ArrayVec, + prelude::*, // +}; + +use crate::{ + gsp::nvkv::{ + nvkv_decode, + Array, + Key, + Required, // + }, // +}; + +nvkv_decode! { + pub(crate) struct VgpuPropertiesSchema => VgpuProperties { + // TODO: `name`/`class` required? + name: Array, + class: Array, + type_id: Required>, + bar1_length: Required>, + max_instance: Required>, + ecc: Key, + profile_size: Required>, + max_fps: Key, + num_heads: Key, + max_res_x: Key, + max_res_y: Key, + dev_id: Required>, + subsystem_id: Required>, + fb_length: Required>, + gsp_heap_size: Required>, + fb_reservation: Required>, + } +} + +pub(crate) struct VgpuProperties { + name: ArrayVec, + class: ArrayVec, + pub(crate) type_id: u32, + pub(crate) bar1_length: u64, + pub(crate) max_instance: u32, + ecc: u32, + profile_size: u64, + max_fps: u32, + num_heads: u32, + max_res_x: u32, + max_res_y: u32, + pub(crate) dev_id: u32, + pub(crate) subsystem_id: u32, + pub(crate) fb_length: u64, + pub(crate) gsp_heap_size: u64, + fb_reservation: u64, +} + +impl VgpuProperties { + const STRING_LEN: usize = 64; +} + +// TODO: extract from bindings +#[repr(C)] +#[derive(FromBytes, IntoBytes)] +pub(crate) struct AssignedVfVgpuType { + pub(crate) vgpu_type_id: u32, + _swizz_id: u32, + _placement_id: u32, +} -- 2.53.0