From patchwork Sat Apr 13 03:47:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13628622 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BC198C4345F for ; Sat, 13 Apr 2024 03:48:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=hBplTIdAY4+7gYhQP8eVpg7oNQ73RbYa7SMRChwGiK8=; b=YyYuElxUoLGi85 BtfrZDNYLvXkxUr13esZkk0YK+Vz3baEwPy0t4KOwn1IfqMm2+Scw4MTvkYdoHQ2IZfHJ93H/XvMl O8OnqRwaZVTI72tGxF1IEFq7rVfX/K4ZiTIzWCTfulgHhL5gRl9zqySoiHWEpNTjt+DQHFFTvuk/D WlMYdPeqWiilFFVBvaQVcCcKuOGd6fsDSFjJjbtZCEYiwS5cURNZHdsdI5W+enlUmNcN9zVj/wm67 g9RqIrLauPZ4nigVT+uPDb+H5nDNo3fYA6L9r7jUuQR+CuwpY4py/S/go3EpSCxnh78i66NYJmVPJ sgZ4J+IA12jsM6fXYWTg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rvUNA-00000002FMX-0P6x; Sat, 13 Apr 2024 03:48:12 +0000 Received: from mail-dm6nam04on20600.outbound.protection.outlook.com ([2a01:111:f403:2409::600] helo=NAM04-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rvUMw-00000002FA2-1yJF for linux-arm-kernel@lists.infradead.org; Sat, 13 Apr 2024 03:47:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jjccV0SPS5+fHeAF99+NAT1ET3bpEi8hGblvnSya79+mctUBZEKknX6bHp2ygt1VYeb/csFGPj7FhvVWpN5lvL+lmW/gxvrs8NnI0nL0a4NfcQmVVMImLHUlaRX17XnJsjo5YsnPl970MzdtBZA1QyHzl2EcxM3WLZoSITJ1cToF3iaK9oXzO6kBQ/PWI/srTssETJakOGNyCjbikXkHQY1oEXp02TWmvbJDnT2I+h9PzHruDcQOlFq1Z6Efa/tJoiEak/jWOoQ0psNCawX4vpEAgt3ULdwJ1uOw2nWpYgBpU+XW7Bp+opKNr0k/LLhaH0Y57/1NoifqnnfkfnySRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=xM4B8FG5GEwfuAIkKYqRf3sLAHIcdh4j9sDUAtG7p5E=; b=cKMcGgcml+gmWWZsFPnDdJYLTrgOpL8Bj5lkskdnDhyzHOavWKZH9W78Vg90jhcOV8SmH+ypCqJ36KpWkh/L9Vv39WXVv9413UhjxpS0wl+enPbjUuuoyU2JFPXHk0CH4UifY4pYYG/hge2hJJk79pnj6U4KliqWN6PBXCM0Txwtqa9tyG67cwCA/CHj10+0rJ3MJFf7Po7l9o+cpqQvQAuTEfUEM+SxSa7lQA2RcJCKsi+Hh9ClQJPNthmIKgyobOZgsCqol2kLnskYYj42hZbMc6BOhIHk7vT4Qc9vKIX6qbTvg04Q98rCmqoHzBEo2cpOEj0ZzLoxxbfyNo7lDw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=xM4B8FG5GEwfuAIkKYqRf3sLAHIcdh4j9sDUAtG7p5E=; b=hSUcfK1wXbRSObzA9ENHfp7AgiEJ/B0L2OBE4NzISnTJyOU6FNuaMYBUJDZ+tDyuYagE+Gi9TpEj7tVMICf32bJhCNmKIPevMU2WKNwJQ9ZxJC6FIrPSz/pYqEV/cb+FnQqsJL3tx9lxU0ezHymMGVNeFXLpXeRmxqBmrBEaOXM2s+TknkFKerooSPtt/dfHvJYMj0GKhsFVtEvdt3tRZ9XNyOI4+s70vx4joLHN3WWEZWfG351qcfB8d+MNRIvg8GLiF37BHqyBfvywykS876jr2/aNV6ytwMq6q9rv2xmvt7JPn5QDO/Eul4HSNgq7FrNtKl3XTv/f4XOgO+Pawg== Received: from BL1P221CA0010.NAMP221.PROD.OUTLOOK.COM (2603:10b6:208:2c5::14) by PH7PR12MB7259.namprd12.prod.outlook.com (2603:10b6:510:207::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.53; Sat, 13 Apr 2024 03:47:52 +0000 Received: from BN3PEPF0000B06B.namprd21.prod.outlook.com (2603:10b6:208:2c5:cafe::5c) by BL1P221CA0010.outlook.office365.com (2603:10b6:208:2c5::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.19 via Frontend Transport; Sat, 13 Apr 2024 03:47:52 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) 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.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BN3PEPF0000B06B.mail.protection.outlook.com (10.167.243.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7519.0 via Frontend Transport; Sat, 13 Apr 2024 03:47:52 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Fri, 12 Apr 2024 20:47:43 -0700 Received: from drhqmail202.nvidia.com (10.126.190.181) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Fri, 12 Apr 2024 20:47:43 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.126.190.181) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Fri, 12 Apr 2024 20:47:42 -0700 From: Nicolin Chen To: , , , , CC: , , , , , , , , , , , Subject: [PATCH RFCv1 04/14] iommufd: Add struct iommufd_viommu and iommufd_viommu_ops Date: Fri, 12 Apr 2024 20:47:01 -0700 Message-ID: <8610498e3fc00000e78bb9cef6fac9f6a54978a4.1712978212.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN3PEPF0000B06B:EE_|PH7PR12MB7259:EE_ X-MS-Office365-Filtering-Correlation-Id: ea556029-34d3-48fa-f2a1-08dc5b6c7ee5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +SQ8uJse3AIFFlGVAYr1iZzqgFeJW06iFYfpB1YPmt18llefA0J33Djr6d42wX+1SsPIodQc+57AWgyJ9zNQTY7qvI+AH8UZHfmyol5k5srgRxSBl/cG+xo9U4E6kMAvPNQ4dJjsiuXAlTmR8oSdbgq0aemndgr5P/Bbbmc36/vn/+gjMSvX4yq++IY2zMZo8RZB92JrIJy3h6bXRw/f9R/8iQZSGsswk8Dpm2AsQz8nC4HHLmxGCVgduUuS24+Rz8MKcPKKQ7vllrkaI6VBnLY0/CBHu5pf5fOq9Q4pTBIQMwQj2b/kEhSqTtqAfClvnrkukV8PCBN0FfJKSxR3s1Kh6kkkF9qDoCTIHw4XQJMinKFecn1SbZ4+kNwlbmRV8R5eFlA8l0tBtdG5AmzGR9bDvRFudo5xWsGcGEOBNfQrv/Rk16z3s5NTvJp2NOJ078Hg1zx2Mdg9b6WrfVZTTPeGfsu8XZynoYmNfc/vSIYPcpHyLQ35HdtaXqnTx37qnK97MUhzwql3GPcLZsRNDYpqlvEFOdmmGqJso9NmSHe8/iE4AzlImxEDiza0KoWgxjLCoE/PBp72qFBGPM/KuocfqfCLn1Wx5zGZPqaiOK0z/VYv7oFSu0aRtIHcgpYvm+N8bgyqjuUX0HXvKAHNZToM/DoTaQc+K6qBQUJGX+TM6Uzr9lNBAMNdAXscxJpay55o6hDD0M5+4BzsH9HVcQzIrhfRMxbfEDA8WBZ2kJwkr2A2Poyx2VCewFYxCcPp X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230031)(82310400014)(7416005)(1800799015)(376005)(36860700004);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Apr 2024 03:47:52.1487 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ea556029-34d3-48fa-f2a1-08dc5b6c7ee5 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN3PEPF0000B06B.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7259 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240412_204758_595607_748398A9 X-CRM114-Status: GOOD ( 19.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a new iommufd_viommu core structure to represent a vIOMMU instance in the user space, typically backed by a HW-accelerated feature of an IOMMU, e.g. NVIDIA CMDQ-Virtualization (an ARM SMMUv3 extension) and AMD Hardware Accelerated Virtualized IOMMU (vIOMMU). Define a new iommufd_viommu_ops to hold the free op and any future op. A driver should embed this core structure in its driver viommu structure and call the new iommufd_viommu_alloc() helper to allocate a core/driver structure bundle and fill its core viommu->ops: struct my_driver_viommu { struct iommufd_viommu core; .... }; static const struct iommufd_viommu_ops my_driver_viommu_ops = { .free = my_driver_viommu_free, }; struct my_driver_viommu *my_viommu = iommufd_viommu_alloc(my_driver_viommu, core); my_viommu->core.ops = &my_driver_viommu_ops; Lastly, add viommu_alloc to iommu_ops so iommufd can link to the driver to allocate a viommu object. Any future viommu op will be added to the new struct iommufd_viommu_ops. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/viommu.c | 2 ++ include/linux/iommu.h | 15 ++++++++++++++ include/linux/iommufd.h | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c index f77d6972d552..3886b1dd1f13 100644 --- a/drivers/iommu/iommufd/viommu.c +++ b/drivers/iommu/iommufd/viommu.c @@ -17,3 +17,5 @@ return NULL; \ return container_of(obj, struct iommufd_##name, obj); \ } + +viommu_struct_alloc(viommu); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 2e925b5eba53..4d4461146288 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -145,6 +145,8 @@ struct iopf_queue { struct mutex lock; }; +struct iommufd_viommu; + /* iommu fault flags */ #define IOMMU_FAULT_READ 0x0 #define IOMMU_FAULT_WRITE 0x1 @@ -527,6 +529,14 @@ static inline int __iommu_copy_struct_from_user_array( * @of_xlate: add OF master IDs to iommu grouping * @is_attach_deferred: Check if domain attach should be deferred from iommu * driver init to device driver init (default no) + * @viommu_alloc: Allocate an iommufd_viommu as a user space IOMMU instance, + * associated to a nested parent @domain, for iommu-specific + * hardware acceleration. The @viommu_type must be defined in + * the include/uapi/linux/iommufd.h header. + * It is suggested to call iommufd_viommu_alloc() helper for + * a bundled allocation of the core and the driver structure, + * and a driver in gernal should assign an iommufd_viommu_ops + * to the core structure's viommu->ops. * @dev_enable/disable_feat: per device entries to enable/disable * iommu specific features. * @page_response: handle page request response @@ -570,6 +580,11 @@ struct iommu_ops { int (*of_xlate)(struct device *dev, const struct of_phandle_args *args); bool (*is_attach_deferred)(struct device *dev); + /* User space instance allocation by the iommu driver */ + struct iommufd_viommu *(*viommu_alloc)(struct device *dev, + unsigned int viommu_type, + struct iommu_domain *domain); + /* Per device IOMMU features */ int (*dev_enable_feat)(struct device *dev, enum iommu_dev_features f); int (*dev_disable_feat)(struct device *dev, enum iommu_dev_features f); diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index a0cb08a4b653..650acfac307a 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -16,6 +16,7 @@ struct iommufd_device; struct page; struct iommufd_ctx; struct iommufd_access; +struct iommufd_hwpt_paging; struct file; struct iommu_group; @@ -77,6 +78,26 @@ void iommufd_access_detach(struct iommufd_access *access); void iommufd_ctx_get(struct iommufd_ctx *ictx); +struct iommufd_viommu { + struct iommufd_object obj; + struct iommufd_ctx *ictx; + struct iommu_device *iommu_dev; + struct iommufd_hwpt_paging *hwpt; + + const struct iommufd_viommu_ops *ops; + + unsigned int type; +}; + +/** + * struct iommufd_viommu_ops - viommu specific operations + * @free: Free all driver-specific parts of an iommufd_viommu. The memory + * of the entire viommu will be free-ed by iommufd core + */ +struct iommufd_viommu_ops { + void (*free)(struct iommufd_viommu *viommu); +}; + #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_ctx *iommufd_ctx_from_file(struct file *file); struct iommufd_ctx *iommufd_ctx_from_fd(int fd); @@ -93,6 +114,8 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova, int iommufd_vfio_compat_ioas_get_id(struct iommufd_ctx *ictx, u32 *out_ioas_id); int iommufd_vfio_compat_ioas_create(struct iommufd_ctx *ictx); int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx); + +struct iommufd_viommu *_iommufd_viommu_alloc(size_t size); #else /* !CONFIG_IOMMUFD */ static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file) { @@ -133,5 +156,20 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx) { return -EOPNOTSUPP; } + +static inline struct iommufd_viommu *_iommufd_viommu_alloc(size_t size) +{ + return NULL; +} #endif /* CONFIG_IOMMUFD */ + +/* + * Helpers for IOMMU driver to allocate driver structures that will be freed by + * the iommufd core. A driver is only responsible for its own struct cleanup. + */ +#define iommufd_viommu_alloc(drv_struct, member) \ + container_of(_iommufd_viommu_alloc(sizeof(struct drv_struct) + \ + BUILD_BUG_ON_ZERO(offsetof( \ + struct drv_struct, member))), \ + struct drv_struct, member) #endif