From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.43.28 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796395780; b=d0B2uHEwUXElqWqVsO/uApKJW3KV8dYamswXgfJTz+UM+pViUkhJ/08HdNImTAZ+OHIy cqcAaN21QHBDl+N9b9jJ65XXOOAxB/XO/rbWoc2i6Rz7M4w3GX46zR0xr26fn5cIB55fT RSJU7xk2PVY8dwAF88TQm+d+WULUxlrziMK12r3hmueaMJsQPwAFqdnthnbMPoMWNOmYQ N/maNLLhe91D8+folS2QMiwYmw6fhC/rwJjhl/YNN4r/RAKVWkoHsXN11Oacyf0tQry61 n3ODUkgRQdOCaBCn82TuksdLnJBmw9ecEzIs89w3MvNjsIYg858wBaCiuqRaDvrMqbw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796395780; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: Received:From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:Content-Type: X-Originating-IP:X-ClientProxiedBy:X-EOPAttributedMessage: X-MS-PublicTrafficType:X-MS-TrafficTypeDiagnostic: X-MS-Office365-Filtering-Correlation-Id:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=SDs27VkVGpp01idTEE0snIItw3ZEZ7qWfrdBhs9kwY4=; b=SG4wCfiFDy+5s+xhLnVgMkGSM/xFj53o3kwB9vPDYOA1S4EoOrpq6a+iHZb4URWHM8lX p44URNjGg+7CXMezPi5j8ge2txSWl1LsHOZNH8nOFCrd7d9Yz2nA8FTeiyb6ggA6bZKxu XyCnYWsMqgtVIULbsuP1v5HqSWrGL6HyAWy7qVExbkp9hLfqxVIY0uE9oTExWToYKZmZ0 WwhXwbtS6qjPlMzlDIZk3nPYbGOvLrvZzHnajV44j37EMYe+QWz6iddXvcG+wiCqc0qDD KsZcqoTUnWJknci2D4T1RqsA3qFsCXeObpfudaVIPlt5GEVd0vLJpORUh1GPPSv9/nQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=CnHEqJ74; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.43.28 Received: from SJ2PR03CU001.outbound.protection.outlook.com (mail-westusazon11012028.outbound.protection.outlook.com [52.101.43.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E322606A322 for ; Fri, 4 Dec 2026 14:49:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0E322606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=CnHEqJ74 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eTC+ZsP84e1w/SJrpXXDUmFd2Wx7jUuWYpmbJMBJcTrxFkI7r5XXdhdSNZs9gnxhozukN2/V8auYFV0vCIRZu+jm4xsx8OmDtNZ6vLsVaD345CcPnRbzytPI6/ujgG59k/zJ9/RabBA760t+elijfGWKsP873N1Ykl+hW5zRh5Hn+Ab6lBAdjuxWUjjsyDqHpVQEwstq6uD83XkWLd0l9X/+2qKQQjjUKIMlJoyJQvR3A/xn84jMw3WBQihOeiwW3mSO3Vx/KFZGMChmvnWTAIauqTPpoRdLLYXSEYj28mnlSLs2oG8w60iqyv9l5tjI0ON4IlCzbTVl+t6Jg/7IQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SDs27VkVGpp01idTEE0snIItw3ZEZ7qWfrdBhs9kwY4=; b=GB9IqOp3+E/t4k7snvILppBhwxY0mGlZ9iRINc7oK+WfSWStLxqRqcyufw6v2iixthGEdmQd9Gm/7vY+REO97eV9ukCzYmRw6HCxdMAZd6w5bh9bb/reLQXVgyVqHFXhzeSGLj2XbrD3aVnOgXi5fHdj69/wclg6eNslMj3mmE/sgm3tSX2z5zkcKB/Eo1zaqKJoyBMfCFMxOQ90ypx06wqQb5iFCX0+iOESLwz+QZd087elmkoUcmf7EcTrIRCJXeGhipIqG3wKjbmTm0rT1oEBbMA6sY91J5WadkR68iS7iaFMi4Ab5Sq6hoF0D9hKDUOPWugDX2JowFG0ui0xlA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SDs27VkVGpp01idTEE0snIItw3ZEZ7qWfrdBhs9kwY4=; b=CnHEqJ743BS4YqDl0RCSeTSm6u7xU5aNn4ZWKKRfPB+yieYga9vWfAPZqYj50U0Yat1lqJTjzhdgt0TeoLGMBUPaymJMMjV7oj5DwYCb9T81Df0160bNnBeeKpIBRqJNbMj+8p9x4IBlibas/PJz9aQ+BuL7zydsmF7pnE4Hqp6xHgWyfYC2ix9kQqnUDhZssqB6uk4RDiBKsKSK1soWIyVPHv6h2KT69IL9Zv29Xd8W6myeHKFajVExe2+/1isq2mG/3TxEHJWwZMN3wvFNkFBUSq0k9vXkuGHMqds8snKKBE67Ca0RKcheJJ+LLGLbNjFgp+87h4DVNa/eboR/ZA== Received: from PH7PR02CA0028.namprd02.prod.outlook.com (2603:10b6:510:33d::32) by IA0PR12MB9046.namprd12.prod.outlook.com (2603:10b6:208:405::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.12; Fri, 4 Dec 2026 14:49:28 +0000 Received: from PH4PEPF0000B60D.namprd21.prod.outlook.com (2603:10b6:510:33d:cafe::19) by PH7PR02CA0028.outlook.office365.com (2603:10b6:510:33d::32) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.13 via Frontend Transport; Fri, 4 Dec 2026 14:49:28 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by PH4PEPF0000B60D.mail.protection.outlook.com (10.167.243.88) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.709.2 via Frontend Transport; Fri, 4 Dec 2026 14:49:28 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 06:49:15 -0800 Received: from xinmengs-dev-vm-linux.nvidia.com (10.126.231.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 06:49:13 -0800 From: Simon Song To: , , , , , CC: Zhi Wang , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , "Liam R. Howlett" , , Lorenzo Stoakes Subject: [PATCH v5 1/2] gpu: nova-core: publish typed SR-IOV PF APIs Date: Fri, 4 Dec 2026 06:48:54 -0800 Message-ID: <20261204144900.286240-2-xinmengs@nvidia.com> X-Mailer: git-send-email 2.43.7 In-Reply-To: <20261204144900.286240-1-xinmengs@nvidia.com> References: <20261204144900.286240-1-xinmengs@nvidia.com> X-Mailing-List: nova-gpu@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH4PEPF0000B60D:EE_|IA0PR12MB9046:EE_ X-MS-Office365-Filtering-Correlation-Id: b84dc280-e7ba-4016-fd0e-08df5215cc16 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|23010399003|1800799024|36860700016|376014|7416014|11063799006|6133799003|261009223027099003|22082099003|18002099003|261026140227099003|56012099006|261021123027099003|261022105027099003|261102123227099003|261028144727099003|261021121427099003|261026121527099003|261022113327099003|10067099003; X-Microsoft-Antispam-Message-Info: BDgSHZ9YZDQSEZDXE54V69ybI/zXrlXvsoQwRjigo3cGuNJUYs0y++VGE3bBerIT5/hkP/UKjYzvmjIo6ydk3RiyRqj/C7X4MMo4xHHZH5EIVZ4Wl7b5bJw+nms43Z/3rYTX33o1wNkJow/L4yQtJFz2jqhyiw5quaq0nkRpmFcIj8e+DVNjfkTHNPliRr/cusZotXUs4Vk21pVTL0PlTkB2rij626RG2awgvYD+2iJ9uRWVBfIsTQVSwvFKTCejK22fbtlGy50/MLXWwO4FCKFj+fxAEcGTPJquELkvW0utO9JMU42RXg0O+OWw4r6+xU3qrVae0e+w/KuGU/yecHjm3Y1lDjs1pPOb4S/THHjG/R02q8saCjFEqVzT0jBPVT6lhMLR5tAIlLoERyMyWwlqNupBzgurVjZ0G9T6zMeOFsd/xkJSND+6ZhESKAe9jbVf/6DVksovU77qK08OK2bcwLADBWUykxBgLapcEq8CuavzVdKOKRSGAgMjVdBZGKVgLvcI4aRV3lSuoJcSImJlPqGkbEGcF7hCZckw4+y4vYKQ8qxA76H6pumlRXyHhC8RsvDEDvBLOpgl2cDe0KXnjbn9Jz5OyrGWTYi6vOJyEu+mID+zP0Rf3w+/7R+CnmOjeYNlVrEZDOtxW5hw9BAIV4X23bc1E6KqQ8WPZ+rC4unAkvyRSTwot2IgGC5np3cLRqqu1QmtHboWjXFAGw== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(23010399003)(1800799024)(36860700016)(376014)(7416014)(11063799006)(6133799003)(261009223027099003)(22082099003)(18002099003)(261026140227099003)(56012099006)(261021123027099003)(261022105027099003)(261102123227099003)(261028144727099003)(261021121427099003)(261026121527099003)(261022113327099003)(10067099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pZwkFS8shnJ7N6NCl0fwJpWC8MOmU4Y0+rH+jHTdnhChdhhMoVSVZo4LOerN8Schhz87BABedEDUE6M44Y4RKBHo+URpjrlHg3u9u6HrweDlNl09vzAHMVwweNhSy0N1ocbzcHdHcUUP1/CfYiEL43EnQQ3szcgJkSMJi19QGI9F4xwiIOKn2SpibddvcveizZ2nNVag8heCW0Nxe5AYyiO8NqnCF+jsQqlXnrC7Kv/ogtjvZR2uQvnrmt+oHYCenZ20qwMyevvo3GwGfbhPPVpWyZ5yJDa8B0bAvQsOc/YzlF0ZIpUit+jqR5jj2Ue5NtHxf4LXg8zWGv7hTvaaNI8LIpfZF5Ar9Yyix+0vgNKpy/31ITQRXyCjhLpnwZdd2BrTgnZ1icoJTD/IUUNt0ReHqjiHUv82hX5rVKxdqIAM86NohHjYyHr8G0mfP7JG X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 14:49:28.1325 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b84dc280-e7ba-4016-fd0e-08df5215cc16 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: PH4PEPF0000B60D.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB9046 From: Zhi Wang 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 Signed-off-by: Simon Song --- 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 bea1b1b0844c9..34c35b5e648fc 100644 --- a/drivers/gpu/nova-core/driver.rs +++ b/drivers/gpu/nova-core/driver.rs @@ -24,7 +24,8 @@ use crate::{ api::NovaCoreApi, - gpu::Gpu, // + gpu::Gpu, + vgpu::vgpu_api::NovaCoreVfApi, // }; /// Counter for generating unique auxiliary device IDs. @@ -35,6 +36,10 @@ pub(crate) struct NovaCore<'bound> { #[allow(clippy::type_complexity)] #[uses('gpu: invariant)] _reg: auxiliary::Registration<'gpu, ForLt!(NovaCoreApi<'_>)>, + #[allow(clippy::type_complexity)] + #[pin] + #[uses('gpu: invariant)] + _vf_registration: pci::VfRegistration<'gpu, ForLt!(NovaCoreVfApi<'_>)>, #[pin] #[uses('bar1)] pub(crate) gpu: Gpu<'bar>, @@ -107,6 +112,10 @@ fn probe<'bound>( try_pin_init!(NovaCore { _: { + if pdev.is_virtfn() { + return Err(ENODEV); + } + pdev.enable_device_mem()?; pdev.set_master(); }, @@ -138,6 +147,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 { @@ -154,4 +169,21 @@ fn probe<'bound>( }, }) } + + fn sriov_enable<'a>( + _dev: &'a pci::Device>, + _this: Pin<&Self::Data<'a>>, + token: pci::SriovEnable<'a>, + ) -> Result> { + token.enable_all() + } + + fn sriov_disable<'a>( + _dev: &'a pci::Device>, + _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 4185717245438..fecd6341c997a 100644 --- a/drivers/gpu/nova-core/nova_core.rs +++ b/drivers/gpu/nova-core/nova_core.rs @@ -34,9 +34,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 f2dd5e73f8ee8..b792a446b2b66 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, - 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>, } impl<'gpu> NovaCoreVfApi<'gpu> { - #[expect(dead_code)] - pub(crate) fn new(gpu: &'gpu Gpu<'gpu>, pdev: &'gpu pci::Device) -> Self { - Self { + /// Creates the PF services that VF drivers access through the VF registration. + pub(crate) fn new<'res: 'gpu>( + pdev: &'gpu pci::Device, + gpu: Pin<&'gpu Gpu<'res>>, + ) -> impl PinInit + 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 { @@ -81,11 +93,29 @@ fn gfid(&self, gfid: u32) -> Result { } impl NovaCoreVfApi<'_> { + /// Obtains the enabled PF services for a bound VF. + pub fn handle(vf: &pci::Device) -> Result> { + 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 { + pub fn open_instance( + self: Pin<&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); @@ -97,29 +127,31 @@ pub fn open_instance(&self, gfid: u32, sbdf: u32, vm_pid: u32) -> Result 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, +} + +impl<'vf> NovaCoreVfApiHandle<'vf> { + /// Borrows the typed PF services for a single operation. + fn with( + &self, + f: impl for<'borrow, 'data> FnOnce(Pin<&'borrow NovaCoreVfApi<'data>>) -> Result, + ) -> Result { + self.vf + .vf_registration_data_with::), _>(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> { + 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