[PATCH v5 34/35] gpu: nova-core: vgpu: introduce SR-IOV PF APIs
From: Alok Kumar
Date: Tue Nov 24 2026 - 09:12:45 EST
From: Zhi Wang <zhiw@xxxxxxxxxx>
Provide PF operations for querying vGPU availability and creating,
closing and resetting VF instances.
Validate VF identifiers against the PF's VF count and route lifecycle
operations through the manager. Retain the GPU context needed to create
plugin RPC channels and return guest PCI properties after activation.
Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
Signed-off-by: Alok Kumar <alkumar@xxxxxxxxxx>
---
drivers/gpu/nova-core/gpu.rs | 20 ++-
drivers/gpu/nova-core/mm/bar_user.rs | 1 -
drivers/gpu/nova-core/mm/vram.rs | 1 -
drivers/gpu/nova-core/nova_core.rs | 6 +
drivers/gpu/nova-core/vgpu.rs | 2 +
drivers/gpu/nova-core/vgpu/commands.rs | 10 +-
drivers/gpu/nova-core/vgpu/fw/commands.rs | 1 +
drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs | 2 -
drivers/gpu/nova-core/vgpu/instance.rs | 57 ++++++-
drivers/gpu/nova-core/vgpu/vgpu_api.rs | 153 ++++++++++++++++++
drivers/gpu/nova-core/vgpu/vram.rs | 1 -
11 files changed, 242 insertions(+), 12 deletions(-)
create mode 100644 drivers/gpu/nova-core/vgpu/vgpu_api.rs
diff --git a/drivers/gpu/nova-core/gpu.rs b/drivers/gpu/nova-core/gpu.rs
index 0de451fc3d7b..a2f78dfb1121 100644
--- a/drivers/gpu/nova-core/gpu.rs
+++ b/drivers/gpu/nova-core/gpu.rs
@@ -1,6 +1,9 @@
// SPDX-License-Identifier: GPL-2.0
-use core::ops::Range;
+use core::{
+ num::NonZero,
+ ops::Range, //
+};
use kernel::{
device,
@@ -468,6 +471,21 @@ fn drop(self: Pin<&mut Self>) {
}
impl<'gpu> Gpu<'gpu> {
+ pub(crate) fn cmdq(&self) -> &Cmdq<'gpu> {
+ &self.gsp_resources.gsp.cmdq
+ }
+
+ pub(crate) fn vgpu_manager(&self) -> Option<&VgpuManager<'_>> {
+ self.vgpu().as_ref().map(|vgpu| vgpu.as_ref().get_ref())
+ }
+
+ pub(crate) fn vgpu_total_vfs(&self) -> Option<NonZero<u16>> {
+ match self.gsp_resources.vgpu_state {
+ VgpuState::Disabled => None,
+ VgpuState::Enabled { total_vfs } => Some(total_vfs),
+ }
+ }
+
pub(crate) fn new<'a>(
pdev: &'gpu pci::Device<device::Core<'a>>,
bar: Bar0<'gpu>,
diff --git a/drivers/gpu/nova-core/mm/bar_user.rs b/drivers/gpu/nova-core/mm/bar_user.rs
index 75970cb0b5cb..e0fc62630fbe 100644
--- a/drivers/gpu/nova-core/mm/bar_user.rs
+++ b/drivers/gpu/nova-core/mm/bar_user.rs
@@ -185,7 +185,6 @@ pub(crate) struct BarMapping<'map, 'gpu> {
unmap_error: Option<Error>,
}
-#[expect(dead_code)]
impl<'map, 'gpu> BarMapping<'map, 'gpu> {
/// Maps `region`, which must start and end on a page boundary.
pub(crate) fn new(
diff --git a/drivers/gpu/nova-core/mm/vram.rs b/drivers/gpu/nova-core/mm/vram.rs
index 7774b15e4c89..6054eed9aeb0 100644
--- a/drivers/gpu/nova-core/mm/vram.rs
+++ b/drivers/gpu/nova-core/mm/vram.rs
@@ -74,7 +74,6 @@ pub(crate) struct VramRegion {
range: Range<u64>,
}
-#[expect(dead_code)]
impl VramRegion {
/// Returns the range covered by this region.
pub(crate) const fn range(&self) -> &Range<u64> {
diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nova_core.rs
index 84f777844592..418571724543 100644
--- a/drivers/gpu/nova-core/nova_core.rs
+++ b/drivers/gpu/nova-core/nova_core.rs
@@ -34,6 +34,12 @@
use debugfs::DebugfsData;
+#[cfg(CONFIG_PCI_IOV)]
+pub use vgpu::vgpu_api::{
+ NovaCoreVfApi,
+ VgpuTypeInfo, //
+};
+
pub(crate) const MODULE_NAME: &core::ffi::CStr = <LocalModule as kernel::ModuleMetadata>::NAME;
/// Pointer to the [`DebugfsData`] of the module.
diff --git a/drivers/gpu/nova-core/vgpu.rs b/drivers/gpu/nova-core/vgpu.rs
index e1e2d6bcfd31..1aee21dbe740 100644
--- a/drivers/gpu/nova-core/vgpu.rs
+++ b/drivers/gpu/nova-core/vgpu.rs
@@ -42,6 +42,8 @@
mod hal;
mod instance;
mod scrubber;
+#[cfg_attr(not(CONFIG_PCI_IOV), expect(dead_code, unreachable_pub))]
+pub(crate) mod vgpu_api;
mod vram;
bitfield! {
diff --git a/drivers/gpu/nova-core/vgpu/commands.rs b/drivers/gpu/nova-core/vgpu/commands.rs
index 07a8e071d34d..ae2ae3b69a1c 100644
--- a/drivers/gpu/nova-core/vgpu/commands.rs
+++ b/drivers/gpu/nova-core/vgpu/commands.rs
@@ -83,7 +83,6 @@ fn read(payload: &mut MessagePayload<'_>) -> Result<Self, Self::InitError> {
}
/// 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<'_>,
@@ -126,7 +125,6 @@ fn read(payload: &mut MessagePayload<'_>) -> Result<Self, Self::InitError> {
}
/// Query and decode the firmware properties of one vGPU type.
-#[expect(dead_code)]
pub(super) fn query_vgpu_properties(
_dev: &device::Device,
cmdq: &Cmdq<'_>,
@@ -263,6 +261,14 @@ pub(super) fn set_plugin_bme(
rpc.rpc_call_nvkv(dev, RpcMessage::UpdateBmeState, &bme)
}
+/// Reset an active GSP plugin.
+pub(super) fn reset_plugin(
+ dev: &device::Device<device::Bound>,
+ rpc: &mut PluginRpc<'_, '_>,
+) -> Result {
+ rpc.rpc_call(dev, RpcMessage::Reset, &[])
+}
+
impl CommandToGsp for AllocCeutilsRequest {
const COMMAND: GmcCommandId = GmcCommandId::VgpuMgrAllocGspCeutils;
type Command = Self;
diff --git a/drivers/gpu/nova-core/vgpu/fw/commands.rs b/drivers/gpu/nova-core/vgpu/fw/commands.rs
index c2e2ffad2b2d..7cb2db63c3a0 100644
--- a/drivers/gpu/nova-core/vgpu/fw/commands.rs
+++ b/drivers/gpu/nova-core/vgpu/fw/commands.rs
@@ -37,6 +37,7 @@
pub(crate) enum RpcMessage {
VersionNegotiation = bindings::MESSAGE_NV_VGPU_CPU_RPC_MSG_VERSION_NEGOTIATION,
SetupConfigParamsAndInit = bindings::MESSAGE_NV_VGPU_CPU_RPC_MSG_SETUP_CONFIG_PARAMS_AND_INIT,
+ Reset = bindings::MESSAGE_NV_VGPU_CPU_RPC_MSG_RESET,
UpdateBmeState = bindings::MESSAGE_NV_VGPU_CPU_RPC_MSG_UPDATE_BME_STATE,
}
diff --git a/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs b/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs
index 49d85df70018..f9fd8c2a5da0 100644
--- a/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs
+++ b/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs
@@ -80,7 +80,6 @@ struct CommBuffer {
static_assert!(size_of::<RawResponseRegion>() == cv!(fw::VGPU_CPU_GSP_RESPONSE_BUFF_REGION_SIZE));
/// Physical VRAM regions containing the vGPU plugin logs.
-#[expect(dead_code)]
pub(super) struct PluginLogRegions {
pub(super) init: VramRegion,
pub(super) vgpu: VramRegion,
@@ -94,7 +93,6 @@ pub(super) struct CommBufferRegion<'map, 'gpu> {
map: BarMapping<'map, 'gpu>,
}
-#[expect(dead_code)]
impl<'map, 'gpu> CommBufferRegion<'map, 'gpu> {
/// Map the communication portion of a plugin management heap.
pub(super) fn new(
diff --git a/drivers/gpu/nova-core/vgpu/instance.rs b/drivers/gpu/nova-core/vgpu/instance.rs
index 2f6c2a8f3a02..5c6353f8c09a 100644
--- a/drivers/gpu/nova-core/vgpu/instance.rs
+++ b/drivers/gpu/nova-core/vgpu/instance.rs
@@ -33,6 +33,7 @@
commands::{
free_ceutils,
negotiate_plugin_version,
+ reset_plugin,
send_bootload,
send_cleanup,
send_plugin_config,
@@ -56,6 +57,7 @@
VgpuVramSlotPool, //
},
Dbdf,
+ VgpuManager,
VgpuManagerInner, //
};
@@ -112,7 +114,6 @@ fn wait_plugin_ready(dev: &device::Device, comm: &CommBufferRegion<'_, '_>) -> R
impl Gfid {
/// Validates an external GFID for a device supporting `total_vfs` VFs.
- #[expect(dead_code)]
pub(super) fn new(gfid: u32, total_vfs: NonZero<u16>) -> Result<Self> {
let gfid = u16::try_from(gfid).map_err(|_| EINVAL)?;
let gfid = NonZero::new(gfid).ok_or(EINVAL)?;
@@ -160,6 +161,7 @@ struct VgpuInstance<'gpu> {
vram_slot: VgpuVramSlot,
chids: ChannelIdReservation<'gpu>,
ceutils: Option<CeUtils>,
+ active: bool,
needs_teardown: bool,
/// An uncertain or failed operation retains resources until device removal.
failure: Option<Error>,
@@ -202,6 +204,7 @@ fn activate(&mut self, vgpu: &VgpuManagerInner<'gpu>) -> Result {
self.configure_plugin(dev)?;
set_plugin_bme(dev, &mut self.plugin_rpc, true)?;
+ self.active = true;
Ok(())
}
@@ -301,6 +304,7 @@ fn shutdown(&mut self, dev: &device::Device, cmdq: &Cmdq<'_>) -> Result {
send_shutdown(dev, cmdq, self.gfid)?;
dev_dbg!(dev, "shutdown: gfid={} stopped\n", self.gfid.get());
}
+ self.active = false;
Ok(())
}
}
@@ -313,7 +317,6 @@ pub(super) struct InstanceInfo {
vm_pid: u32,
}
-#[expect(dead_code)]
impl InstanceInfo {
pub(super) fn new(gfid: Gfid, dbdf: Dbdf, properties: &VgpuProperties, vm_pid: u32) -> Self {
Self {
@@ -331,7 +334,6 @@ pub(super) struct VgpuInstances<'gpu> {
vram_slots: Option<VgpuVramSlotPool>,
}
-#[expect(dead_code)]
impl<'gpu> VgpuInstances<'gpu> {
pub(super) const fn new() -> Self {
Self {
@@ -428,6 +430,7 @@ fn allocate_instance(
vram_slot,
chids,
ceutils: None,
+ active: false,
needs_teardown: false,
failure: None,
})
@@ -468,13 +471,27 @@ pub(super) fn release_all(&mut self, vgpu: &VgpuManagerInner<'gpu>) {
}
}
+impl VgpuManager<'_> {
+ /// Allocate, register, and activate a vGPU instance.
+ pub(super) fn create_instance(&self, info: InstanceInfo) -> Result {
+ self.with_inner(|inner| inner.create_instance(info))
+ }
+
+ pub(super) fn close_instance(&self, gfid: Gfid) -> Result {
+ self.with_inner(|inner| inner.close_instance(gfid))
+ }
+
+ pub(super) fn reset_instance(&self, gfid: Gfid) -> Result {
+ self.with_inner(|inner| inner.reset_instance(gfid))
+ }
+}
+
impl VgpuManagerInner<'_> {
/// Allocate, register, and activate a vGPU instance.
///
/// Keep the registry locked through creation or rollback so duplicate checks and
/// type limits remain stable. MM and BAR-user locks are acquired only within
/// individual memory operations, in that order.
- #[expect(dead_code)]
fn create_instance(&self, info: InstanceInfo) -> Result {
let mut instances = self.instances.lock();
@@ -507,4 +524,36 @@ fn create_instance(&self, info: InstanceInfo) -> Result {
result
}
+
+ fn close_instance(&self, gfid: Gfid) -> Result {
+ self.instances.lock().destroy_instance(self, gfid)
+ }
+
+ fn reset_instance(&self, gfid: Gfid) -> Result {
+ let mut instances = self.instances.lock();
+ let instance = instances
+ .instances
+ .iter_mut()
+ .find(|instance| instance.gfid == gfid)
+ .ok_or(ENOENT)?;
+ if instance.failure.is_some() || !instance.active {
+ return Err(EBUSY);
+ }
+
+ let result = (|| {
+ reset_plugin(self.dev, &mut instance.plugin_rpc)?;
+ instance.ceutils.as_ref().ok_or(EINVAL)?.scrub_guest_fb(
+ self.dev,
+ self.cmdq,
+ self.bar_user,
+ self.mm,
+ &instance.vram_slot.fbmem,
+ )
+ })();
+ if let Err(error) = result {
+ instance.failure = Some(error);
+ instance.active = false;
+ }
+ result
+ }
}
diff --git a/drivers/gpu/nova-core/vgpu/vgpu_api.rs b/drivers/gpu/nova-core/vgpu/vgpu_api.rs
new file mode 100644
index 000000000000..b263e96098a8
--- /dev/null
+++ b/drivers/gpu/nova-core/vgpu/vgpu_api.rs
@@ -0,0 +1,153 @@
+// SPDX-License-Identifier: GPL-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+
+//! PF-owned lifecycle operations for Nova VF drivers.
+
+use core::num::NonZero;
+
+use kernel::{
+ device,
+ pci,
+ prelude::*, //
+};
+
+use crate::{
+ gpu::Gpu,
+ gsp::cmdq::Cmdq,
+ vgpu::{
+ commands::query_vgpu_properties,
+ fw::commands::VgpuProperties,
+ instance::{
+ Gfid,
+ InstanceInfo, //
+ },
+ },
+};
+
+use super::{
+ commands::query_assigned_vf_type,
+ Dbdf,
+ VgpuManager, //
+};
+
+/// Guest PCI properties of an activated vGPU instance.
+pub struct VgpuTypeInfo {
+ /// PCI device ID to present to the guest.
+ pub pci_dev_id: u32,
+ /// PCI subsystem ID to present to the guest.
+ pub pci_subsys_id: u32,
+ /// BAR1 aperture size in MiB.
+ pub bar1_length: u64,
+}
+
+impl From<&VgpuProperties> for VgpuTypeInfo {
+ fn from(vgpu_properties: &VgpuProperties) -> Self {
+ Self {
+ pci_dev_id: vgpu_properties.dev_id,
+ pci_subsys_id: vgpu_properties.subsystem_id,
+ bar1_length: vgpu_properties.bar1_length,
+ }
+ }
+}
+
+/// PF-owned operations available while a VF driver is bound.
+pub struct NovaCoreVfApi<'gpu> {
+ pdev: &'gpu pci::Device<device::Bound>,
+ cmdq: &'gpu Cmdq<'gpu>,
+ vgpu: Option<&'gpu VgpuManager<'gpu>>,
+ total_vfs: Option<NonZero<u16>>,
+}
+
+impl<'gpu> NovaCoreVfApi<'gpu> {
+ #[expect(dead_code)]
+ pub(crate) fn new(gpu: &'gpu Gpu<'gpu>, pdev: &'gpu pci::Device<device::Bound>) -> Self {
+ Self {
+ pdev,
+ cmdq: gpu.cmdq(),
+ vgpu: gpu.vgpu_manager(),
+ total_vfs: gpu.vgpu_total_vfs(),
+ }
+ }
+
+ /// Returns whether the PF booted with vGPU support enabled.
+ pub fn is_available(&self) -> bool {
+ self.vgpu.is_some()
+ }
+
+ fn gfid(&self, gfid: u32) -> Result<Gfid> {
+ let total_vfs = self.total_vfs.ok_or(ENODEV)?;
+ Gfid::new(gfid, total_vfs)
+ }
+}
+
+impl NovaCoreVfApi<'_> {
+ /// Creates and boots an instance for a one-based VF ID.
+ ///
+ /// `sbdf` encodes the VF address as `(segment << 16) | (bus << 8) | devfn`.
+ /// `vm_pid` identifies the VM process's thread group. This call may sleep.
+ pub fn open_instance(&self, gfid: u32, sbdf: u32, vm_pid: u32) -> Result<VgpuTypeInfo> {
+ let dev = self.pdev.as_ref();
+ let gfid = self.gfid(gfid)?;
+ let dbdf = Dbdf::from_raw(sbdf);
+
+ dev_dbg!(
+ dev,
+ "vgpu_open: gfid={} sbdf={:#x}\n",
+ gfid.get(),
+ dbdf.into_raw()
+ );
+
+ let cmdq = self.cmdq;
+ let vgpu = self.vgpu.ok_or(ENODEV)?;
+
+ let type_id = query_assigned_vf_type(dev, cmdq, dbdf)?;
+ dev_dbg!(
+ dev,
+ "vgpu_open: gfid={} assigned type_id={}\n",
+ gfid.get(),
+ type_id
+ );
+
+ let vgpu_properties = query_vgpu_properties(dev, cmdq, type_id)?;
+ dev_dbg!(
+ dev,
+ "vgpu_open: gfid={} vgpu_type={} fb_length={:#x}\n",
+ gfid.get(),
+ vgpu_properties.type_id,
+ vgpu_properties.fb_length
+ );
+
+ vgpu.create_instance(InstanceInfo::new(gfid, dbdf, &vgpu_properties, vm_pid))?;
+
+ Ok(VgpuTypeInfo::from(&*vgpu_properties))
+ }
+
+ /// Tears down an instance, retaining resources if firmware cleanup fails.
+ ///
+ /// This call may sleep.
+ pub fn close_instance(&self, gfid: u32) -> Result {
+ let dev = self.pdev.as_ref();
+ let gfid = self.gfid(gfid)?;
+
+ dev_dbg!(dev, "vgpu_close: gfid={}\n", gfid.get());
+
+ let result = self.vgpu.ok_or(ENODEV)?.close_instance(gfid);
+ if let Err(error) = result {
+ dev_err!(dev, "vgpu_close: gfid={} failed: {:?}\n", gfid.get(), error);
+ }
+ result
+ }
+
+ /// Resets an instance and scrubs its guest VRAM. This call may sleep.
+ pub fn reset_instance(&self, gfid: u32) -> Result {
+ let dev = self.pdev.as_ref();
+ let gfid = self.gfid(gfid)?;
+
+ dev_dbg!(dev, "vgpu_reset: gfid={}\n", gfid.get());
+
+ self.vgpu.ok_or(ENODEV)?.reset_instance(gfid)?;
+
+ dev_dbg!(dev, "vgpu_reset: gfid={} done\n", gfid.get());
+ Ok(())
+ }
+}
diff --git a/drivers/gpu/nova-core/vgpu/vram.rs b/drivers/gpu/nova-core/vgpu/vram.rs
index fb7c460b9d97..e3edf25549af 100644
--- a/drivers/gpu/nova-core/vgpu/vram.rs
+++ b/drivers/gpu/nova-core/vgpu/vram.rs
@@ -36,7 +36,6 @@ pub(super) struct VgpuVramLayout {
fb_align: u64,
}
-#[expect(dead_code)]
impl VgpuVramLayout {
pub(super) fn new(
type_id: u32,
--
2.53.0