From: Zhi Wang <[email protected]> Register NovaCoreVfApi as typed SR-IOV PF data once the GPU is initialized.
Enable and disable VFs through the sriov_enable() and sriov_disable() callbacks. The VF registration removes VFs before dropping its PF API data. Declare the registration before the GPU so its borrowed services remain valid through VF teardown. Expose a VF-side handle that borrows these services for each operation. Return an owned instance on open and close it on drop while the VF remains bound. Signed-off-by: Zhi Wang <[email protected]> Signed-off-by: Simon Song <[email protected]> --- drivers/gpu/nova-core/driver.rs | 34 ++++- drivers/gpu/nova-core/nova_core.rs | 3 +- drivers/gpu/nova-core/vgpu.rs | 1 - drivers/gpu/nova-core/vgpu/vgpu_api.rs | 171 +++++++++++++++++++------ 4 files changed, 167 insertions(+), 42 deletions(-) diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver.rs index 86f47016dcff8..6b99e88e2090e 100644 --- a/drivers/gpu/nova-core/driver.rs +++ b/drivers/gpu/nova-core/driver.rs @@ -29,7 +29,8 @@ NovaCoreFwCtl, NovaCoreFwCtlData, // }, - gpu::Gpu, // + gpu::Gpu, + vgpu::vgpu_api::NovaCoreVfApi, // }; /// Counter for generating unique auxiliary device IDs. @@ -43,6 +44,10 @@ pub(crate) struct NovaCore<'bound> { /// Unregisters fwctl and drains its callbacks before GPU resources are dropped. #[uses('gpu: invariant)] _fwctl: fwctl::Registration<'gpu, NovaCoreFwCtl>, + #[allow(clippy::type_complexity)] + #[pin] + #[uses('gpu: invariant)] + _vf_registration: pci::VfRegistration<'gpu, ForLt!(NovaCoreVfApi<'_>)>, #[pin] #[uses('bar1)] pub(crate) gpu: Gpu<'bar>, @@ -115,6 +120,10 @@ fn probe<'bound>( try_pin_init!(NovaCore { _: { + if pdev.is_virtfn() { + return Err(ENODEV); + } + pdev.enable_device_mem()?; pdev.set_master(); }, @@ -146,6 +155,12 @@ fn probe<'bound>( Ok(()) }), + // SAFETY: The registration is stored in `NovaCore`, this driver's bus device + // private data. + _vf_registration <- unsafe { + pci::VfRegistration::new(pdev, NovaCoreVfApi::new(pdev, gpu)) + }, + // SAFETY: `NovaCore` is dropped when the device is unbound; i.e. `mem::forget()` is // never called on it. _reg: unsafe { @@ -177,4 +192,21 @@ fn probe<'bound>( }, }) } + + fn sriov_enable<'a>( + _dev: &'a pci::Device<Core<'_>>, + _this: Pin<&Self::Data<'a>>, + token: pci::SriovEnable<'a>, + ) -> Result<pci::SriovEnabled<'a>> { + token.enable_all() + } + + fn sriov_disable<'a>( + _dev: &'a pci::Device<Core<'_>>, + _this: Pin<&Self::Data<'a>>, + token: pci::SriovDisable<'a>, + ) -> Result { + token.disable(); + Ok(()) + } } diff --git a/drivers/gpu/nova-core/nova_core.rs b/drivers/gpu/nova-core/nova_core.rs index b2cb11c0a4d20..03e01003674df 100644 --- a/drivers/gpu/nova-core/nova_core.rs +++ b/drivers/gpu/nova-core/nova_core.rs @@ -35,9 +35,10 @@ use debugfs::DebugfsData; -#[cfg(CONFIG_PCI_IOV)] pub use vgpu::vgpu_api::{ NovaCoreVfApi, + NovaCoreVfApiHandle, + VgpuInstance, VgpuTypeInfo, // }; diff --git a/drivers/gpu/nova-core/vgpu.rs b/drivers/gpu/nova-core/vgpu.rs index 1aee21dbe740d..3353faf737903 100644 --- a/drivers/gpu/nova-core/vgpu.rs +++ b/drivers/gpu/nova-core/vgpu.rs @@ -42,7 +42,6 @@ mod hal; mod instance; mod scrubber; -#[cfg_attr(not(CONFIG_PCI_IOV), expect(dead_code, unreachable_pub))] pub(crate) mod vgpu_api; mod vram; diff --git a/drivers/gpu/nova-core/vgpu/vgpu_api.rs b/drivers/gpu/nova-core/vgpu/vgpu_api.rs index b263e96098a80..1064e461cd269 100644 --- a/drivers/gpu/nova-core/vgpu/vgpu_api.rs +++ b/drivers/gpu/nova-core/vgpu/vgpu_api.rs @@ -8,7 +8,8 @@ use kernel::{ device, pci, - prelude::*, // + prelude::*, + types::ForLt, // }; use crate::{ @@ -51,27 +52,38 @@ fn from(vgpu_properties: &VgpuProperties) -> Self { } /// PF-owned operations available while a VF driver is bound. -pub struct NovaCoreVfApi<'gpu> { +#[pin_data] +pub struct NovaCoreVfApi<'gpu> +where + // The GPU resources are invariant in their lifetime, which outlives this API borrow. + exists<'gpu_resources>: 'gpu, +{ pdev: &'gpu pci::Device<device::Bound>, - cmdq: &'gpu Cmdq<'gpu>, - vgpu: Option<&'gpu VgpuManager<'gpu>>, + #[uses('gpu_resources: invariant)] + cmdq: &'gpu Cmdq<'gpu_resources>, + #[uses('gpu_resources: invariant)] + vgpu: Option<&'gpu VgpuManager<'gpu_resources>>, 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 { + /// Creates the PF services that VF drivers access through the VF registration. + pub(crate) fn new<'res: 'gpu>( + pdev: &'gpu pci::Device<device::Bound>, + gpu: Pin<&'gpu Gpu<'res>>, + ) -> impl PinInit<Self, Error> + use<'gpu, 'res> { + let gpu = gpu.get_ref(); + try_pin_init!(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() + self.with_vgpu(|vgpu| vgpu.is_some()) } fn gfid(&self, gfid: u32) -> Result<Gfid> { @@ -81,11 +93,29 @@ fn gfid(&self, gfid: u32) -> Result<Gfid> { } impl NovaCoreVfApi<'_> { + /// Obtains the enabled PF services for a bound VF. + pub fn handle(vf: &pci::Device<device::Bound>) -> Result<NovaCoreVfApiHandle<'_>> { + let handle = NovaCoreVfApiHandle { vf }; + handle.with(|api| { + if api.is_available() { + Ok(()) + } else { + Err(ENODEV) + } + })?; + Ok(handle) + } + /// 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> { + pub fn open_instance( + self: Pin<&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); @@ -97,29 +127,31 @@ pub fn open_instance(&self, gfid: u32, sbdf: u32, vm_pid: u32) -> Result<VgpuTyp 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)) + self.with_project_ref(|this| { + let cmdq = *this.cmdq; + let vgpu = this.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. @@ -131,11 +163,13 @@ pub fn close_instance(&self, gfid: u32) -> Result { 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 + self.with_vgpu(|vgpu| { + let result = 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. @@ -145,9 +179,68 @@ pub fn reset_instance(&self, gfid: u32) -> Result { dev_dbg!(dev, "vgpu_reset: gfid={}\n", gfid.get()); - self.vgpu.ok_or(ENODEV)?.reset_instance(gfid)?; + self.with_vgpu(|vgpu| vgpu.ok_or(ENODEV)?.reset_instance(gfid))?; dev_dbg!(dev, "vgpu_reset: gfid={} done\n", gfid.get()); Ok(()) } } + +/// Access to PF services for the lifetime of a VF driver binding. +/// +/// The PF's registration keeps these services available until VF removal +/// completes and is dropped before the GPU resources they borrow. +pub struct NovaCoreVfApiHandle<'vf> { + vf: &'vf pci::Device<device::Bound>, +} + +impl<'vf> NovaCoreVfApiHandle<'vf> { + /// Borrows the typed PF services for a single operation. + fn with<R>( + &self, + f: impl for<'borrow, 'data> FnOnce(Pin<&'borrow NovaCoreVfApi<'data>>) -> Result<R>, + ) -> Result<R> { + self.vf + .vf_registration_data_with::<ForLt!(NovaCoreVfApi<'_>), _>(f)? + } + + /// Creates and boots an instance that closes when dropped. + /// + /// The arguments have the same meaning as in [`NovaCoreVfApi::open_instance`]. + pub fn open(&self, gfid: u32, dbdf: u32, vm_pid: u32) -> Result<VgpuInstance<'vf>> { + let type_info = self.with(|api| api.open_instance(gfid, dbdf, vm_pid))?; + Ok(VgpuInstance { + api: Self { vf: self.vf }, + gfid, + type_info, + }) + } + + /// Resets an active instance and scrubs its guest VRAM. + pub fn reset(&self, gfid: u32) -> Result { + self.with(|api| api.reset_instance(gfid)) + } +} + +/// An active instance whose teardown runs while its VF driver remains bound. +/// +/// Dropping this guard may sleep while firmware teardown completes. +pub struct VgpuInstance<'vf> { + api: NovaCoreVfApiHandle<'vf>, + gfid: u32, + type_info: VgpuTypeInfo, +} + +impl VgpuInstance<'_> { + /// Returns the assigned PCI IDs and BAR1 aperture size. + #[inline] + pub fn type_info(&self) -> &VgpuTypeInfo { + &self.type_info + } +} + +impl Drop for VgpuInstance<'_> { + fn drop(&mut self) { + let _ = self.api.with(|api| api.close_instance(self.gfid)); + } +} -- 2.43.7
