From: Zhi Wang Add Cmdq operations to allocate and free a CeUtils channel and submit guest VRAM scrub requests. Decode the allocation and scrub replies, and validate the returned semaphore address and aperture. Distinguish an explicit allocation rejection from failures that may leave firmware owning the channel, so callers can decide whether to retain its host resources. Signed-off-by: Zhi Wang Co-developed-by: Alexandre Courbot Signed-off-by: Alexandre Courbot Signed-off-by: Alok Kumar --- drivers/gpu/nova-core/vgpu/commands.rs | 153 ++++++++++++++++++ drivers/gpu/nova-core/vgpu/fw.rs | 2 + drivers/gpu/nova-core/vgpu/fw/commands.rs | 48 +++++- .../gpu/nova-core/vgpu/fw/r000_00/bindings.rs | 1 + 4 files changed, 202 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/nova-core/vgpu/commands.rs b/drivers/gpu/nova-core/vgpu/commands.rs index d11f9af27ee0..a71379165eaf 100644 --- a/drivers/gpu/nova-core/vgpu/commands.rs +++ b/drivers/gpu/nova-core/vgpu/commands.rs @@ -10,6 +10,7 @@ use kernel::{ device, + num::casts::usize_as_u64, prelude::*, time::Delta, transmute::AsBytes, // @@ -36,16 +37,22 @@ GmcCommandId, GSP_PAGE_SIZE, // }, + mm::PAGE_SIZE, sbuffer::SBufferIter, vgpu::{ fw::{ commands::{ encode_plugin_set_bme, + AllocCeutilsRequest, + AllocCeutilsResponse, AssignedVfVgpuType, + ScrubGuestFbRequest, + ScrubGuestFbResponse, VgpuProperties, VgpuPropertiesSchema, // }, RpcMessage, + NV_ADDR_FBMEM, // }, gsp_plugin_rpc::PluginRpc, instance::Gfid, @@ -255,3 +262,149 @@ pub(super) fn set_plugin_bme( let bme = encode_plugin_set_bme(enable)?; rpc.rpc_call_nvkv(dev, RpcMessage::UpdateBmeState, &bme) } + +impl CommandToGsp for AllocCeutilsRequest { + const COMMAND: GmcCommandId = GmcCommandId::VgpuMgrAllocGspCeutils; + type Command = Self; + type Reply = AllocCeutilsResponse; + const MAX_REPLY_SIZE: u32 = cv!(size_of::()); + type InitError = Infallible; + + fn init(&self) -> impl Init { + *self + } +} + +impl MessageFromGsp for AllocCeutilsResponse { + type InitError = Error; + + fn read(payload: &mut MessagePayload<'_>) -> Result { + payload.read() + } +} + +/// The `VGPU_MGR_FREE_GSP_CEUTILS` command. +struct FreeCeutils(Gfid); + +impl CommandToGsp for FreeCeutils { + const COMMAND: GmcCommandId = GmcCommandId::VgpuMgrFreeGspCeutils; + type Command = u32; + type Reply = (); + const MAX_REPLY_SIZE: u32 = 0; + type InitError = Infallible; + + fn init(&self) -> impl Init { + u32::from(self.0.get()).to_le() + } +} + +impl CommandToGsp for ScrubGuestFbRequest { + const COMMAND: GmcCommandId = GmcCommandId::VgpuMgrScrubGuestFb; + type Command = Self; + type Reply = ScrubGuestFbResponse; + const MAX_REPLY_SIZE: u32 = cv!(size_of::()); + type InitError = Infallible; + + fn init(&self) -> impl Init { + *self + } +} + +impl MessageFromGsp for ScrubGuestFbResponse { + type InitError = Error; + + fn read(payload: &mut MessagePayload<'_>) -> Result { + payload.read() + } +} + +/// Whether a failed allocation may still have transferred CHID ownership to firmware. +#[expect(dead_code)] +pub(super) enum CeUtilsAllocError { + /// A matching firmware response explicitly rejected the allocation. + NotOwned(Error), + /// The request may have completed despite a transport or response-validation error. + MayOwn(Error), +} + +/// Allocate a CeUtils channel and validate its semaphore description. +#[expect(dead_code)] +pub(super) fn alloc_ceutils( + dev: &device::Device, + cmdq: &Cmdq<'_>, + gfid: Gfid, + chid: u32, +) -> core::result::Result { + let request = AllocCeutilsRequest { + gfid: u32::from(gfid.get()).to_le(), + fixed_chid: chid.to_le(), + force_ceid: u32::MAX.to_le(), + swizz_id: 0, + }; + + dev_dbg!(dev, "alloc CeUtils: gfid={} chid={}\n", gfid.get(), chid,); + + let response = cmdq + .send_gmc_command(request, |_| Ok(())) + .map_err(CeUtilsAllocError::MayOwn)? + .map_err(|_| CeUtilsAllocError::NotOwned(EIO))?; + + let semaphore_address = u64::from_le(response.semaphore_address); + let semaphore_aperture = u32::from_le(response.semaphore_aperture); + let page_size = usize_as_u64(PAGE_SIZE); + + if semaphore_address == 0 + || !semaphore_address.is_multiple_of(page_size) + || semaphore_aperture != NV_ADDR_FBMEM + { + return Err(CeUtilsAllocError::MayOwn(EINVAL)); + } + + dev_dbg!( + dev, + "alloc CeUtils: gfid={} semaphore={:#x}\n", + gfid.get(), + semaphore_address, + ); + Ok(semaphore_address) +} + +/// Release a CeUtils allocation, including one whose allocation reply was lost. +#[expect(dead_code)] +pub(super) fn free_ceutils(dev: &device::Device, cmdq: &Cmdq<'_>, gfid: Gfid) -> Result { + dev_dbg!(dev, "free CeUtils: gfid={}\n", gfid.get()); + + cmdq.send_gmc_command(FreeCeutils(gfid), |_| Ok(()))? + .map_err(|_| EIO) +} + +/// Submit an asynchronous guest FB scrub and return its work identifier. +#[expect(dead_code)] +pub(super) fn submit_ceutils_scrub( + dev: &device::Device, + cmdq: &Cmdq<'_>, + gfid: Gfid, + fb_offset: u64, + fb_size: u64, +) -> Result { + let request = ScrubGuestFbRequest { + gfid: u32::from(gfid.get()).to_le(), + reserved: 0, + fb_offset: fb_offset.to_le(), + fb_size: fb_size.to_le(), + }; + + dev_dbg!( + dev, + "submit scrub: gfid={} offset={:#x} size={:#x}\n", + gfid.get(), + fb_offset, + fb_size, + ); + + let response = cmdq + .send_gmc_command(request, |_| Ok(()))? + .map_err(|_| EIO)?; + + u32::try_from(u64::from_le(response.work_id)).map_err(|_| EOVERFLOW) +} diff --git a/drivers/gpu/nova-core/vgpu/fw.rs b/drivers/gpu/nova-core/vgpu/fw.rs index 76bd2b7e4e52..02f51bb8cf7f 100644 --- a/drivers/gpu/nova-core/vgpu/fw.rs +++ b/drivers/gpu/nova-core/vgpu/fw.rs @@ -31,6 +31,8 @@ VGPU_CPU_GSP_VGPU_TASK_LOG_BUFF_REGION_SIZE, // }; +pub(super) use bindings::NV_ADDR_FBMEM; + /// State observed in the response buffer for an expected RPC sequence. pub(super) enum RpcResponse { Pending { diff --git a/drivers/gpu/nova-core/vgpu/fw/commands.rs b/drivers/gpu/nova-core/vgpu/fw/commands.rs index 10f1427bf355..c2e2ffad2b2d 100644 --- a/drivers/gpu/nova-core/vgpu/fw/commands.rs +++ b/drivers/gpu/nova-core/vgpu/fw/commands.rs @@ -20,10 +20,10 @@ Array, Encodable, Encoder, - Index, // + Index, Key, KeyId, - Required, + Required, // }, mm::vram::VramRegion, vgpu::Dbdf, // @@ -330,6 +330,50 @@ pub(crate) fn encode_plugin_config_params( Ok(encoder.finish()) } +#[repr(C)] +#[derive(Clone, Copy)] +pub(crate) struct AllocCeutilsRequest { + pub(crate) gfid: u32, + pub(crate) fixed_chid: u32, + pub(crate) force_ceid: u32, + pub(crate) swizz_id: u32, +} + +// SAFETY: All fields are integers, and the struct has no padding. +unsafe impl kernel::transmute::AsBytes for AllocCeutilsRequest {} + +// SAFETY: All fields are integers, for which all bit patterns are valid. +unsafe impl kernel::transmute::FromBytes for AllocCeutilsRequest {} + +#[repr(C)] +#[derive(FromBytes, IntoBytes)] +pub(crate) struct AllocCeutilsResponse { + pub(crate) semaphore_address: u64, + pub(crate) semaphore_aperture: u32, + _reserved: u32, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub(crate) struct ScrubGuestFbRequest { + pub(crate) gfid: u32, + pub(crate) reserved: u32, + pub(crate) fb_offset: u64, + pub(crate) fb_size: u64, +} + +// SAFETY: All fields are integers, and the struct has no padding. +unsafe impl kernel::transmute::AsBytes for ScrubGuestFbRequest {} + +// SAFETY: All fields are integers, for which all bit patterns are valid. +unsafe impl kernel::transmute::FromBytes for ScrubGuestFbRequest {} + +#[repr(C)] +#[derive(FromBytes, IntoBytes)] +pub(crate) struct ScrubGuestFbResponse { + pub(crate) work_id: u64, +} + nvkv_encode! { struct PluginSetBmeRequest { bme_enable: Key, diff --git a/drivers/gpu/nova-core/vgpu/fw/r000_00/bindings.rs b/drivers/gpu/nova-core/vgpu/fw/r000_00/bindings.rs index 85f965335b64..e46f7339544a 100644 --- a/drivers/gpu/nova-core/vgpu/fw/r000_00/bindings.rs +++ b/drivers/gpu/nova-core/vgpu/fw/r000_00/bindings.rs @@ -8,6 +8,7 @@ pub type u16_ = __u16; pub type u32_ = __u32; pub type u64_ = __u64; +pub const NV_ADDR_FBMEM: u32 = 2; pub const GSP_PLUGIN_BOOTLOADED: u32 = 1315261039; pub const VGPU_CPU_GSP_CTRL_BUFF_VERSION: u32 = 2; pub const VGPU_CPU_GSP_CTRL_BUFF_REGION_SIZE: u32 = 4096; -- 2.53.0