From patchwork Thu Feb 9 04:16:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 652223 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67B9AC05027 for ; Thu, 9 Feb 2023 04:19:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229539AbjBIETY (ORCPT ); Wed, 8 Feb 2023 23:19:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjBIETY (ORCPT ); Wed, 8 Feb 2023 23:19:24 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5E4D10242; Wed, 8 Feb 2023 20:18:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916308; x=1707452308; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yLfWIPfMZf4ex+xdPNvOR4QIAocyXgXzIiZTA67s30A=; b=D538TFeNArPwCJ3B2b+VXX2e33wRjGsR6l0o5tmgmjSst0/YJhbi1jmp nxr4uyN6P51gcB53b+9GjfT1ctKxALXC8iQ01ZSbAsC0Pw84tybaBnTg/ hI8gI/0jNsOp6z0/FCFzbJM+oLO8kepQpMkGdfHZAmDspZxM/3lgb2Qtu uK6ZTV8PjeKRro+rtCuh9isTP7OSOIrLVqmeX05leBIUkhjzRyadimrWx W91YOpqjRy74vavhdUBEqcpUZZr/U/wKDsoKVRKt2BQ/zaKZ1MCKYELec gw9Es7nb8l0ngfy81M9xhN4M3fVWfnQTNLYuPQgrse78XfYgIGqLRNo6q w==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600702" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600702" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982084" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982084" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:45 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, baolu.lu@linux.intel.com Subject: [PATCH 1/6] iommu: Add new iommu op to get iommu hardware information Date: Wed, 8 Feb 2023 20:16:37 -0800 Message-Id: <20230209041642.9346-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Lu Baolu Introduce a new iommu op get the IOMMU hardware capabilities for iommufd. This information will be used by any vIOMMU driver which is owned by userspace. This op chooses to make the special parameters opaque to the core. This suits the current usage model where accessing any of the IOMMU device special parameters does require a userspace driver that matches the kernel driver. If a need for common parameters, implemented similarly by several drivers, arises then there is room in the design to grow a generic parameter set as well. No warpper API is added as it is supposed to be used by iommufd only. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu Signed-off-by: Nicolin Chen --- include/linux/iommu.h | 8 ++++++++ include/uapi/linux/iommufd.h | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index cb586d054c57..97b398d19fd2 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -15,6 +15,7 @@ #include #include #include +#include #define IOMMU_READ (1 << 0) #define IOMMU_WRITE (1 << 1) @@ -223,6 +224,11 @@ struct iommu_iotlb_gather { /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability + * @hw_info: IOMMU hardware capabilities. The type of the returned data is + * defined in include/uapi/linux/iommufd.h. The data buffer is + * allocated in the IOMMU driver and the caller should free it + * after use. Return the data buffer if success, or ERR_PTR on + * failure. * @domain_alloc: allocate iommu domain * @probe_device: Add device to iommu driver handling * @release_device: Remove device from iommu driver handling @@ -252,6 +258,7 @@ struct iommu_iotlb_gather { */ struct iommu_ops { bool (*capable)(struct device *dev, enum iommu_cap); + void *(*hw_info)(struct device *dev, u32 *length); /* Domain allocation and freeing by the iommu driver */ struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); @@ -280,6 +287,7 @@ struct iommu_ops { void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid); const struct iommu_domain_ops *default_domain_ops; + enum iommu_device_data_type driver_type; unsigned long pgsize_bitmap; struct module *owner; }; diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 98ebba80cfa1..2309edb55028 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -344,4 +344,10 @@ struct iommu_vfio_ioas { __u16 __reserved; }; #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) + +/** + * enum iommu_device_data_type - IOMMU hardware Data types + */ +enum iommu_device_data_type { +}; #endif From patchwork Thu Feb 9 04:16:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 653081 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCE9DC05027 for ; Thu, 9 Feb 2023 04:19:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229585AbjBIETu (ORCPT ); Wed, 8 Feb 2023 23:19:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229580AbjBIETs (ORCPT ); Wed, 8 Feb 2023 23:19:48 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D7B837B46; Wed, 8 Feb 2023 20:18:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916317; x=1707452317; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FHpZ/QqiMDOUDlV/6MH/yoSElx9gu3oQTNg7c3bopYs=; b=UjdcylbahddBAdOaH8Fjk75LslKMVyuzY7+zGkxwiwr1n5d1JOYvxkCK 5nIm6Bc7GXVnv6c8ceYHyTonqExmCIDkWBXAu5joCeB28m1QdHOUKGRIA H+cuIdO00cC0vrwImfj/+BLE4BvctprfFQjklDohGZtdi4Oh9ymvgm4Bh /6B7aNUmjCpF7KEtUtp6G9Kz7NsvCpSFzBEI1Uv5iZU2CvurkaFQse43E GrGy4fQSjNPc9h5Cz5c6x1i9zpIFr6nyGwJLMxbIaXcEmoJO6GjB+fNUk KjO1JLXMNaH8Wajm7Hie8/csnRKl8r8qmf+QRXS4ta48vclESUGNPniPQ w==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600718" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600718" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982088" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982088" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:47 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, baolu.lu@linux.intel.com Subject: [PATCH 2/6] iommu/vt-d: Implement hw_info for iommu capability query Date: Wed, 8 Feb 2023 20:16:38 -0800 Message-Id: <20230209041642.9346-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Lu Baolu To support nested translation in the userspace, it should check the underlying hardware information for the capabilities. Add intel_iommu_hw_info() to report cap_reg and ecap_reg information. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu Signed-off-by: Nicolin Chen Signed-off-by: Yi Sun --- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ drivers/iommu/intel/iommu.h | 1 + include/uapi/linux/iommufd.h | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 59df7e42fd53..929f600cc350 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4760,8 +4760,26 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) intel_pasid_tear_down_entry(iommu, dev, pasid, false); } +static void *intel_iommu_hw_info(struct device *dev, u32 *length) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + struct iommu_device_info_vtd *vtd; + + vtd = kzalloc(sizeof(*vtd), GFP_KERNEL); + if (!vtd) + return ERR_PTR(-ENOMEM); + + vtd->cap_reg = iommu->cap; + vtd->ecap_reg = iommu->ecap; + *length = sizeof(*vtd); + + return vtd; +} + const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, + .hw_info = intel_iommu_hw_info, .domain_alloc = intel_iommu_domain_alloc, .probe_device = intel_iommu_probe_device, .probe_finalize = intel_iommu_probe_finalize, @@ -4774,6 +4792,7 @@ const struct iommu_ops intel_iommu_ops = { .def_domain_type = device_def_domain_type, .remove_dev_pasid = intel_iommu_remove_dev_pasid, .pgsize_bitmap = SZ_4K, + .driver_type = IOMMU_DEVICE_DATA_INTEL_VTD, #ifdef CONFIG_INTEL_IOMMU_SVM .page_response = intel_svm_page_response, #endif diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 06e61e474856..2e70265d4ceb 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 2309edb55028..fda75c8450ee 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -347,7 +347,28 @@ struct iommu_vfio_ioas { /** * enum iommu_device_data_type - IOMMU hardware Data types + * @IOMMU_DEVICE_DATA_INTEL_VTD: Intel VT-d iommu data type */ enum iommu_device_data_type { + IOMMU_DEVICE_DATA_INTEL_VTD = 1, +}; + +/** + * struct iommu_device_info_vtd - Intel VT-d device info + * + * @flags: Must be set to 0 + * @__reserved: Must be 0 + * @cap_reg: Value of Intel VT-d capability register defined in chapter + * 11.4.2 of Intel VT-d spec. + * @ecap_reg: Value of Intel VT-d capability register defined in chapter + * 11.4.3 of Intel VT-d spec. + * + * Intel hardware iommu capability. + */ +struct iommu_device_info_vtd { + __u32 flags; + __u32 __reserved; + __aligned_u64 cap_reg; + __aligned_u64 ecap_reg; }; #endif From patchwork Thu Feb 9 04:16:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 652222 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40103C05027 for ; Thu, 9 Feb 2023 04:20:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229479AbjBIEUR (ORCPT ); Wed, 8 Feb 2023 23:20:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229564AbjBIEUO (ORCPT ); Wed, 8 Feb 2023 23:20:14 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D86A0125AE; Wed, 8 Feb 2023 20:19:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916360; x=1707452360; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zg75NlnzG6bl+FEokzV5mm4wJdtzxfEPl+1gdcoX9v4=; b=WZXVMdplkhYeXlnDg6B7TelrovySZiy+APS08/1iBteCNqI41nyFzYKj dyCTzInlDBZbK4qr2Lp3z6kdObpT+tu6tkEiOMiQwhbmv7F3MHsmNmfnt 88mcbJLZR2PGR/8TULYEBJ91K9vDLXf5sVzdxjZfl0RXrx5ZEEihRK2l+ eFN4AgREXOvJ2XdQCzrW1JkzkKZf10RZJu6nELnrRjlk8xdbh4Tr0b2y/ y5fl0b4ANRE09JTp41uHjLjOTlji493IiGx99hyCJOCj1Vcfxi2OXcAC5 CLq78y2smkZdwACzfk9DcxZF2w0meBYnXIBTWNx+NiLkVcP10nGhwfuk0 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600728" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600728" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982101" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982101" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:49 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, baolu.lu@linux.intel.com Subject: [PATCH 3/6] iommufd: Add IOMMU_DEVICE_GET_INFO Date: Wed, 8 Feb 2023 20:16:39 -0800 Message-Id: <20230209041642.9346-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Under nested IOMMU translation, userspace owns the stage-1 translation structure (e.g. the stage-1 page table of Intel VT-d or the context table of ARM SMMUv3, and etc.). Such stage-1 translation structures are vendor specific, and needs to be compatiable with the underlying IOMMU hardware. Hence, userspace should know the IOMMU hardware capability before creating and configuring the stage-1 translation structure to kernel. This adds ioctl: IOMMU_DEVICE_GET_INFO to query the IOMMU hardware capability for a given device. The returned data is vendor specific, userspace can tell it by the @out_device_type field. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 72 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 36 +++++++++++++ 4 files changed, 112 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 8a9834fc129a..3b64aef24807 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -134,6 +134,78 @@ void iommufd_device_unbind(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD); +static int iommufd_zero_fill_user(u64 ptr, int bytes) +{ + int index = 0; + + for (; index < bytes; index++) { + if (put_user(0, (uint8_t __user *)(ptr + index))) + return -EFAULT; + } + return 0; +} + +int iommufd_device_get_info(struct iommufd_ucmd *ucmd) +{ + struct iommu_device_info *cmd = ucmd->cmd; + struct iommufd_object *dev_obj; + struct device *dev; + const struct iommu_ops *ops; + void *data; + unsigned int length, data_len; + int rc; + + if (cmd->flags || cmd->__reserved || !cmd->data_len) + return -EOPNOTSUPP; + + dev_obj = iommufd_get_object(ucmd->ictx, cmd->dev_id, + IOMMUFD_OBJ_DEVICE); + if (IS_ERR(dev_obj)) + return PTR_ERR(dev_obj); + + dev = container_of(dev_obj, struct iommufd_device, obj)->dev; + + ops = dev_iommu_ops(dev); + if (!ops || !ops->hw_info) { + rc = -EOPNOTSUPP; + goto out_put; + } + + data = ops->hw_info(dev, &data_len); + if (IS_ERR(data)) { + rc = PTR_ERR(data); + goto out_put; + } + + length = min(cmd->data_len, data_len); + if (copy_to_user(u64_to_user_ptr(cmd->data_ptr), data, length)) { + rc = -EFAULT; + goto out_free_data; + } + + /* + * Zero the trailing bytes for userspace if the buffer is bigger + * than the data size kernel actually has. + */ + if (length < cmd->data_len) { + rc = iommufd_zero_fill_user(cmd->data_ptr + length, + cmd->data_len - length); + if (rc) + goto out_free_data; + } + + cmd->out_device_type = ops->driver_type; + cmd->data_len = data_len; + + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + +out_free_data: + kfree(data); +out_put: + iommufd_put_object(dev_obj); + return rc; +} + static int iommufd_device_setup_msi(struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt, phys_addr_t sw_msi_start) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 200c783800ad..4a0a1a7fdae1 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -258,6 +258,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); void iommufd_device_destroy(struct iommufd_object *obj); +int iommufd_device_get_info(struct iommufd_ucmd *ucmd); struct iommufd_access { struct iommufd_object obj; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a..59aa30ad1090 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -250,6 +250,7 @@ static int iommufd_option(struct iommufd_ucmd *ucmd) union ucmd_buffer { struct iommu_destroy destroy; + struct iommu_device_info info; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -281,6 +282,8 @@ struct iommufd_ioctl_op { } static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id), + IOCTL_OP(IOMMU_DEVICE_GET_INFO, iommufd_device_get_info, struct iommu_device_info, + __reserved), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, struct iommu_ioas_alloc, out_ioas_id), IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index fda75c8450ee..6cfe102f26f3 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -45,6 +45,7 @@ enum { IOMMUFD_CMD_IOAS_UNMAP, IOMMUFD_CMD_OPTION, IOMMUFD_CMD_VFIO_IOAS, + IOMMUFD_CMD_DEVICE_GET_INFO, }; /** @@ -371,4 +372,39 @@ struct iommu_device_info_vtd { __aligned_u64 cap_reg; __aligned_u64 ecap_reg; }; + +/** + * struct iommu_device_info - ioctl(IOMMU_DEVICE_GET_INFO) + * @size: sizeof(struct iommu_device_info) + * @flags: Must be 0 + * @dev_id: The device being attached to the IOMMU + * @data_len: Input the type specific data buffer length in bytes + * @data_ptr: Pointer to the type specific structure (e.g. + * struct iommu_device_info_vtd) + * @out_device_type: Output the underlying iommu hardware type, it is + * one of enum iommu_device_data_type. + * @__reserved: Must be 0 + * + * Query the hardware iommu capability for given device which has been + * bound to iommufd. @data_len is set to be the size of the buffer to + * type specific data and the data will be filled. Trailing bytes are + * zeroed if the user buffer is larger than the data kernel has. + * + * The type specific data would be used to sync capability between the + * vIOMMU and the hardware IOMMU, also for the availabillity checking of + * iommu hardware features like dirty page tracking in I/O page table. + * + * The @out_device_type will be filled if the ioctl succeeds. It would + * be used to decode the data filled in the buffer pointed by @data_ptr. + */ +struct iommu_device_info { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 data_len; + __aligned_u64 data_ptr; + __u32 out_device_type; + __u32 __reserved; +}; +#define IOMMU_DEVICE_GET_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_GET_INFO) #endif From patchwork Thu Feb 9 04:16:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 653080 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA3FFC636D3 for ; Thu, 9 Feb 2023 04:20:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229605AbjBIEUW (ORCPT ); Wed, 8 Feb 2023 23:20:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229602AbjBIEUT (ORCPT ); Wed, 8 Feb 2023 23:20:19 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 177085BB6; Wed, 8 Feb 2023 20:19:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916364; x=1707452364; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FvzP1cm3MJo8mMYBDAYm1g80hB7lsWqyw4EEdmHNXb4=; b=DuT36FuySU9sAa4zuVmP34locoWLSwXQu3814a2c8s21lGEvF4ASJQpz 9Ou73zbYJ/65EwgBZSPGWHWYUwfGFCzTaO11XjpitqTYhOC0tBdIcO+LY soyEwGLNuEC2AJBWcRjlj/g7jhtO5SidoVqYtWDZVaAL/k98bTsxcb2aB J03EoWYizV3nhUGMnh2ECamyiCm8x0AM4wSjnYvTk5t34LbRMJD2qXDQC ElvSk4ImnaWJAtWjFhXrjwnabZdIz0Q/rLVQvGjnB7bFEra227nUcvS3v 6MLSeyqnX2imXYFhU4G128/wN/ohZ8c30wVXGvx0iwgWrRCBMiPA11bMq w==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600742" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600742" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982117" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982117" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:51 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, baolu.lu@linux.intel.com Subject: [PATCH 4/6] iommufd/device: Add mock_device support in iommufd_device_get_info() Date: Wed, 8 Feb 2023 20:16:40 -0800 Message-Id: <20230209041642.9346-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This prepares for adding selftest for IOMMU_DEVICE_GET_INFO. Selftest uses mock device, while physical device uses iommufd_device, so add a helper iommufd_obj_dev() to get struct device from the iommufd_object for a given dev_id. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 22 ++++++++++++++++++++-- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/selftest.c | 7 +++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 3b64aef24807..470838e6902d 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -145,6 +145,20 @@ static int iommufd_zero_fill_user(u64 ptr, int bytes) return 0; } +static struct device * +iommufd_obj_dev(struct iommufd_object *obj) +{ + struct device *dev = NULL; + + if (obj->type == IOMMUFD_OBJ_DEVICE) + dev = container_of(obj, struct iommufd_device, obj)->dev; +#ifdef CONFIG_IOMMUFD_TEST + else if (obj->type == IOMMUFD_OBJ_SELFTEST) + dev = iommufd_selftest_obj_to_dev(obj); +#endif + return dev; +} + int iommufd_device_get_info(struct iommufd_ucmd *ucmd) { struct iommu_device_info *cmd = ucmd->cmd; @@ -159,11 +173,15 @@ int iommufd_device_get_info(struct iommufd_ucmd *ucmd) return -EOPNOTSUPP; dev_obj = iommufd_get_object(ucmd->ictx, cmd->dev_id, - IOMMUFD_OBJ_DEVICE); + IOMMUFD_OBJ_ANY); if (IS_ERR(dev_obj)) return PTR_ERR(dev_obj); - dev = container_of(dev_obj, struct iommufd_device, obj)->dev; + dev = iommufd_obj_dev(dev_obj); + if (!dev) { + rc = -EINVAL; + goto out_put; + } ops = dev_iommu_ops(dev); if (!ops || !ops->hw_info) { diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 4a0a1a7fdae1..7748117e36f9 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -284,6 +284,7 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx, struct device *mock_dev); void iommufd_device_selftest_detach(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt); +struct device *iommufd_selftest_obj_to_dev(struct iommufd_object *obj); int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); extern size_t iommufd_test_memory_limit; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index b94870f93138..2ecde22a60f4 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -802,6 +802,13 @@ void iommufd_selftest_destroy(struct iommufd_object *obj) } } +struct device *iommufd_selftest_obj_to_dev(struct iommufd_object *obj) +{ + struct selftest_obj *sobj = container_of(obj, struct selftest_obj, obj); + + return &sobj->idev.mock_dev; +} + int iommufd_test(struct iommufd_ucmd *ucmd) { struct iommu_test_cmd *cmd = ucmd->cmd; From patchwork Thu Feb 9 04:16:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 652221 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2098AC64EC5 for ; Thu, 9 Feb 2023 04:20:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229564AbjBIEUX (ORCPT ); Wed, 8 Feb 2023 23:20:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229629AbjBIEUW (ORCPT ); Wed, 8 Feb 2023 23:20:22 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A46E91A95D; Wed, 8 Feb 2023 20:19:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916367; x=1707452367; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HKkQ3XLh/cgddbfTjAzae85+rSiZHI2gRNi0xQo2hVQ=; b=l+ZwhvxAJzdUjkfPoz4EG/G7HBdWBqT8G4Duaa3ifPIjNu2NaeJuTRzz bquZ49LBbZCauWhiIbJqfWipQl9qyLrrE9STsoDd6qmSEw4IorsJrcUwz K85faZHY7thNoifj/illtyjKyxOP6ZpAOHt9KapgbpCqtfzkaR76YGVMt SqjXfURM5FlA1KYRxAcfnWfZs/11cJnVUwiov2BKm/0tq226+JCOTBoTN WG33CuZnKrZXgQbjD7yaIpBX8L3WYC7VgPkhAvmJmaMgbPzv0laytLGCT 4n2KieltwojgQJXFXYKMwaO46VlE4gDcxroXKA18NvmwkEY4JNK4trX8+ Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600757" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600757" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982130" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982130" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:54 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, baolu.lu@linux.intel.com Subject: [PATCH 5/6] iommufd/selftest: Set iommu_device for mock_device Date: Wed, 8 Feb 2023 20:16:41 -0800 Message-Id: <20230209041642.9346-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Nicolin Chen This is required to avoid NULL pointer when iommufd selftest tries to allocate domain for mock_device. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/selftest.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 2ecde22a60f4..5afed3fc30fe 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -273,6 +273,8 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, struct iommu_test_cmd *cmd) { + static struct iommu_device iommu_dev = { .ops = &mock_ops }; + static struct dev_iommu iommu = { .iommu_dev = &iommu_dev }; static struct bus_type mock_bus = { .iommu_ops = &mock_ops }; struct iommufd_hw_pagetable *hwpt; struct selftest_obj *sobj; @@ -291,6 +293,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, sobj->idev.ictx = ucmd->ictx; sobj->type = TYPE_IDEV; sobj->idev.mock_dev.bus = &mock_bus; + sobj->idev.mock_dev.iommu = &iommu; hwpt = iommufd_device_selftest_attach(ucmd->ictx, ioas, &sobj->idev.mock_dev); From patchwork Thu Feb 9 04:16:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 652220 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B17B2C61DA4 for ; Thu, 9 Feb 2023 04:21:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229540AbjBIEVM (ORCPT ); Wed, 8 Feb 2023 23:21:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34484 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229460AbjBIEVK (ORCPT ); Wed, 8 Feb 2023 23:21:10 -0500 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A861DBCD; Wed, 8 Feb 2023 20:20:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675916414; x=1707452414; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vnj0cCi2ra/GTQprP6oGdeKLwYac70d/g6YVtvXPxLQ=; b=ixHXGSQ98xLDq0naL4+db3i/9Jcs86tfL/LTZu/Pc8vW4DVHPukE9WCa KK2fdLYwHMt7tNziWw8nCMYnxtJZX2omVnScKEETw/SjI44rrsOTn7JeS OYzNBwSyockfncxU33pRhEPkF8AgMVFtQ7pplvLMaCOxmB/l0+eOjRdME ZDzpU9BPos6LQW9OvilY92QYzqVebtT6Lx6AoSQZfg5gP8b0HjBitmZNV 6SRLuh+G7LvxiuSLbmJBWWyeBwzuHSZ9JvqOsgHFBJs/uDdNrQ+cpd95f bR0sx6R3bhX5pKWMEKkuorDPg6E9MT5mO2vXMKvETgxmt5UgheAPxFFce g==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="394600768" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="394600768" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 20:16:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="912982142" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="912982142" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 08 Feb 2023 20:16:56 -0800 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, baolu.lu@linux.intel.com Subject: [PATCH 6/6] iommufd/selftest: Add coverage for IOMMU_DEVICE_GET_INFO ioctl Date: Wed, 8 Feb 2023 20:16:42 -0800 Message-Id: <20230209041642.9346-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209041642.9346-1-yi.l.liu@intel.com> References: <20230209041642.9346-1-yi.l.liu@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Nicolin Chen This allows to get info for the mock_dev, helps to run selftest to check IOMMU_DEVICE_GET_INFO in selftest. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 1 + drivers/iommu/iommufd/iommufd_test.h | 15 +++++++++++ drivers/iommu/iommufd/selftest.c | 16 ++++++++++++ tools/testing/selftests/iommu/iommufd.c | 18 ++++++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 26 +++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 470838e6902d..0e5d2bde7b3c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -8,6 +8,7 @@ #include "io_pagetable.h" #include "iommufd_private.h" +#include "iommufd_test.h" MODULE_IMPORT_NS(IOMMUFD_INTERNAL); diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index f2c61a9500e7..1605ff2b1a90 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -94,4 +94,19 @@ struct iommu_test_cmd { }; #define IOMMU_TEST_CMD _IO(IOMMUFD_TYPE, IOMMUFD_CMD_BASE + 32) +/* Mock structs for IOMMU_DEVICE_GET_INFO ioctl */ +#define IOMMU_DEVICE_DATA_SELFTEST 0xfeedbeef +#define IOMMU_DEVICE_INFO_SELFTEST_REGVAL 0xdeadbeef + +/** + * struct iommu_device_info_selftest + * + * @flags: Must be set to 0 + * @test_reg: Pass IOMMU_DEVICE_INFO_SELFTEST_REGVAL to user selftest program + */ +struct iommu_device_info_selftest { + __u32 flags; + __u32 test_reg; +}; + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 5afed3fc30fe..5013c8757f4b 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -104,6 +104,20 @@ struct selftest_obj { }; }; +static void *mock_domain_hw_info(struct device *dev, u32 *length) +{ + struct iommu_device_info_selftest *info; + + info = kzalloc(sizeof(*info), GFP_KERNEL); + if (!info) + return ERR_PTR(-ENOMEM); + + info->test_reg = IOMMU_DEVICE_INFO_SELFTEST_REGVAL; + *length = sizeof(*info); + + return info; +} + static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type) { struct mock_iommu_domain *mock; @@ -239,6 +253,8 @@ static phys_addr_t mock_domain_iova_to_phys(struct iommu_domain *domain, static const struct iommu_ops mock_ops = { .owner = THIS_MODULE, .pgsize_bitmap = MOCK_IO_PAGE_SIZE, + .driver_type = IOMMU_DEVICE_DATA_SELFTEST, + .hw_info = mock_domain_hw_info, .domain_alloc = mock_domain_alloc, .default_domain_ops = &(struct iommu_domain_ops){ diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 1e293950ac88..8e1369451464 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -124,6 +124,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP); TEST_LENGTH(iommu_option, IOMMU_OPTION); TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS); + TEST_LENGTH(iommu_device_info, IOMMU_DEVICE_GET_INFO); #undef TEST_LENGTH } @@ -185,6 +186,7 @@ TEST_F(iommufd, global_options) FIXTURE(iommufd_ioas) { int fd; + uint32_t dev_id; uint32_t ioas_id; uint32_t domain_id; uint64_t base_iova; @@ -212,7 +214,8 @@ FIXTURE_SETUP(iommufd_ioas) } for (i = 0; i != variant->mock_domains; i++) { - test_cmd_mock_domain(self->ioas_id, NULL, &self->domain_id); + test_cmd_mock_domain(self->ioas_id, &self->dev_id, + &self->domain_id); self->base_iova = MOCK_APERTURE_START; } } @@ -281,6 +284,19 @@ TEST_F(iommufd_ioas, ioas_area_auto_destroy) } } +TEST_F(iommufd_ioas, device_get_info) +{ + struct iommu_device_info_selftest info; + + if (self->dev_id) { + test_cmd_device_get_info(self->dev_id, sizeof(info), &info); + assert(info.test_reg == IOMMU_DEVICE_INFO_SELFTEST_REGVAL); + } else { + test_err_device_get_info(ENOENT, self->dev_id, + sizeof(info), &info); + } +} + TEST_F(iommufd_ioas, area) { int i; diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 67805afc620f..1807d29c05f8 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -294,3 +294,29 @@ static void teardown_iommufd(int fd, struct __test_metadata *_metadata) }) #endif + +static int _test_cmd_device_get_info(int fd, __u32 device_id, + __u32 data_len, void *data) +{ + struct iommu_device_info cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .data_len = data_len, + .data_ptr = (uint64_t)data, + }; + int ret; + + ret = ioctl(fd, IOMMU_DEVICE_GET_INFO, &cmd); + if (ret) + return ret; + return 0; +} + +#define test_cmd_device_get_info(device_id, data_len, data) \ + ASSERT_EQ(0, _test_cmd_device_get_info(self->fd, device_id, \ + data_len, data)) + +#define test_err_device_get_info(_errno, device_id, data_len, data) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_device_get_info(self->fd, device_id, \ + data_len, data))