From: Zhi Wang 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 Signed-off-by: Alok Kumar --- 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 | 68 +++++++- drivers/gpu/nova-core/vgpu/vgpu_api.rs | 153 ++++++++++++++++++ drivers/gpu/nova-core/vgpu/vram.rs | 1 - 11 files changed, 253 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 efed67fc0d9e..a3ad6cebe90c 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, @@ -457,6 +460,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> { + 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>, bar: Bar0<'gpu>, diff --git a/drivers/gpu/nova-core/mm/bar_user.rs b/drivers/gpu/nova-core/mm/bar_user.rs index b0d073b4f074..64802ccec37b 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<'pool, 'map, 'gpu> { unmap_error: Option, } -#[expect(dead_code)] impl<'pool, 'map, 'gpu> BarMapping<'pool, '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 265d8269733e..be3076d065c6 100644 --- a/drivers/gpu/nova-core/mm/vram.rs +++ b/drivers/gpu/nova-core/mm/vram.rs @@ -75,7 +75,6 @@ pub(crate) struct VramRegion<'pool> { range: Range, } -#[expect(dead_code)] impl VramRegion<'_> { /// Returns the range covered by this region. pub(crate) const fn range(&self) -> &Range { 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 = ::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 fa4ff2505815..f2dd5e73f8ee 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 ad2b6e4d0a67..78ea76ecac0d 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 { } /// 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 { } /// 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, + 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 9164e28123f6..65e87e17a984 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 2e3415b9f8d9..282fb5f3673f 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::() == cv!(fw::VGPU_CPU_GSP_RESPONSE_BUFF_REGION_SIZE)); /// Physical VRAM regions containing the vGPU plugin logs. -#[expect(dead_code)] pub(super) struct PluginLogRegions<'pool> { pub(super) init: VramRegion<'pool>, pub(super) vgpu: VramRegion<'pool>, @@ -94,7 +93,6 @@ pub(super) struct CommBufferRegion<'pool, 'map, 'gpu> { map: BarMapping<'pool, 'map, 'gpu>, } -#[expect(dead_code)] impl<'pool, 'map, 'gpu> CommBufferRegion<'pool, '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 39bf7f4d5fb8..65e58e9418c1 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<'_, '_, '_>) 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) -> Result { let gfid = u16::try_from(gfid).map_err(|_| EINVAL)?; let gfid = NonZero::new(gfid).ok_or(EINVAL)?; @@ -160,6 +161,7 @@ struct VgpuInstance<'pool, 'gpu> { vram_slot: VgpuVramSlot<'pool>, chids: ChannelIdReservation<'gpu>, ceutils: Option, + active: bool, needs_teardown: bool, /// An uncertain or failed operation retains resources until device removal. failure: Option, @@ -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 { @@ -353,7 +356,6 @@ fn new(mm: &GpuMm<'_>, layout: VgpuVramLayout) -> Result VgpuInstances<'gpu> { pub(super) const fn new() -> Self { Self { pool: None } @@ -401,6 +403,14 @@ fn destroy_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, gfid: Gfid) -> Res .with_project(|this| this.registry.destroy_instance(vgpu, gfid)) } + fn reset_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, gfid: Gfid) -> Result { + self.pool + .as_mut() + .ok_or(ENOENT)? + .as_mut() + .with_project(|this| this.registry.reset_instance(vgpu, gfid)) + } + pub(super) fn release_all(&mut self, vgpu: &VgpuManagerInner<'gpu>) { if let Some(pool) = self.pool.as_mut() { pool.as_mut() @@ -480,6 +490,7 @@ fn allocate_instance( vram_slot, chids, ceutils: None, + active: false, needs_teardown: false, failure: None, }) @@ -553,11 +564,60 @@ fn create_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, info: InstanceInfo) result } + + fn reset_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, gfid: Gfid) -> Result { + let instance = self + .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(vgpu.dev, &mut instance.plugin_rpc)?; + instance.ceutils.as_ref().ok_or(EINVAL)?.scrub_guest_fb( + vgpu.dev, + vgpu.cmdq, + vgpu.bar_user, + vgpu.mm, + &instance.vram_slot.fbmem, + ) + })(); + if let Err(error) = result { + instance.failure = Some(error); + instance.active = false; + } + result + } +} + +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<'_> { - #[expect(dead_code)] fn create_instance(&self, info: InstanceInfo) -> Result { self.instances.lock().create_instance(self, info) } + + fn close_instance(&self, gfid: Gfid) -> Result { + self.instances.lock().destroy_instance(self, gfid) + } + + fn reset_instance(&self, gfid: Gfid) -> Result { + self.instances.lock().reset_instance(self, gfid) + } } 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, + cmdq: &'gpu Cmdq<'gpu>, + vgpu: Option<&'gpu VgpuManager<'gpu>>, + total_vfs: Option>, +} + +impl<'gpu> NovaCoreVfApi<'gpu> { + #[expect(dead_code)] + pub(crate) fn new(gpu: &'gpu Gpu<'gpu>, pdev: &'gpu pci::Device) -> 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 { + 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 { + 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 8668d22b3a88..4dd28579b9b5 100644 --- a/drivers/gpu/nova-core/vgpu/vram.rs +++ b/drivers/gpu/nova-core/vgpu/vram.rs @@ -35,7 +35,6 @@ pub(super) struct VgpuVramLayout { fb_align: u64, } -#[expect(dead_code)] impl VgpuVramLayout { pub(super) fn new( type_id: u32,