mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nicolin Chen <nicolinc@nvidia.com>
To: <jgg@nvidia.com>, <kevin.tian@intel.com>, <will@kernel.org>
Cc: <joro@8bytes.org>, <suravee.suthikulpanit@amd.com>,
	<robin.murphy@arm.com>, <dwmw2@infradead.org>,
	<baolu.lu@linux.intel.com>, <shuah@kernel.org>,
	<linux-kernel@vger.kernel.org>, <iommu@lists.linux.dev>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-kselftest@vger.kernel.org>, <eric.auger@redhat.com>,
	<jean-philippe@linaro.org>, <mdf@kernel.org>,
	<mshavit@google.com>, <shameerali.kolothum.thodi@huawei.com>,
	<smostafa@google.com>, <yi.l.liu@intel.com>
Subject: [PATCH v2 17/19] iommu/arm-smmu-v3: Add arm_smmu_viommu_cache_invalidate
Date: Tue, 27 Aug 2024 09:59:54 -0700	[thread overview]
Message-ID: <4b61aba3bc6c1cce628d9db44d5b18ea567a8be1.1724776335.git.nicolinc@nvidia.com> (raw)
In-Reply-To: <cover.1724776335.git.nicolinc@nvidia.com>

Add an arm_smmu_viommu_cache_invalidate() function for user space to issue
cache invalidation commands via viommu.

The viommu invalidation takes the same native format of a 128-bit command,
as the hwpt invalidation. Thus, reuse the same driver data structure, but
make it wider to accept CMDQ_OP_ATC_INV and CMDQ_OP_CFGI_CD{_ALL}.

Scan the commands against the supported ist and fix the VMIDs and SIDs.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 76 ++++++++++++++++++++-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  1 +
 include/uapi/linux/iommufd.h                |  7 +-
 3 files changed, 80 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index a2af693bc7b2..bddbb98da414 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -3267,15 +3267,32 @@ static void arm_smmu_domain_nested_free(struct iommu_domain *domain)
 	kfree(container_of(domain, struct arm_smmu_nested_domain, domain));
 }
 
+static int arm_smmu_convert_viommu_vdev_id(struct iommufd_viommu *viommu,
+					   u32 vdev_id, u32 *sid)
+{
+	struct arm_smmu_master *master;
+	struct device *dev;
+
+	dev = iommufd_viommu_find_device(viommu, vdev_id);
+	if (!dev)
+		return -EIO;
+	master = dev_iommu_priv_get(dev);
+
+	if (sid)
+		*sid = master->streams[0].id;
+	return 0;
+}
+
 /*
  * Convert, in place, the raw invalidation command into an internal format that
  * can be passed to arm_smmu_cmdq_issue_cmdlist(). Internally commands are
  * stored in CPU endian.
  *
- * Enforce the VMID on the command.
+ * Enforce the VMID or the SID on the command.
  */
 static int
 arm_smmu_convert_user_cmd(struct arm_smmu_domain *s2_parent,
+			  struct iommufd_viommu *viommu,
 			  struct iommu_hwpt_arm_smmuv3_invalidate *cmd)
 {
 	u16 vmid = s2_parent->s2_cfg.vmid;
@@ -3297,13 +3314,46 @@ arm_smmu_convert_user_cmd(struct arm_smmu_domain *s2_parent,
 		cmd->cmd[0] &= ~CMDQ_TLBI_0_VMID;
 		cmd->cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, vmid);
 		break;
+	case CMDQ_OP_ATC_INV:
+	case CMDQ_OP_CFGI_CD:
+	case CMDQ_OP_CFGI_CD_ALL:
+		if (viommu) {
+			u32 sid, vsid = FIELD_GET(CMDQ_CFGI_0_SID, cmd->cmd[0]);
+
+			if (arm_smmu_convert_viommu_vdev_id(viommu, vsid, &sid))
+				return -EIO;
+			cmd->cmd[0] &= ~CMDQ_CFGI_0_SID;
+			cmd->cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, sid);
+			break;
+		}
+		fallthrough;
 	default:
 		return -EIO;
 	}
 	return 0;
 }
 
+static inline bool
+arm_smmu_must_lock_vdev_id(struct iommu_hwpt_arm_smmuv3_invalidate *cmds,
+			   unsigned int num_cmds)
+{
+	int i;
+
+	for (i = 0; i < num_cmds; i++) {
+		switch (cmds[i].cmd[0] & CMDQ_0_OP) {
+		case CMDQ_OP_ATC_INV:
+		case CMDQ_OP_CFGI_CD:
+		case CMDQ_OP_CFGI_CD_ALL:
+			return true;
+		default:
+			continue;
+		}
+	}
+	return false;
+}
+
 static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent,
+					    struct iommufd_viommu *viommu,
 					    struct iommu_user_data_array *array)
 {
 	struct arm_smmu_device *smmu = s2_parent->smmu;
@@ -3313,6 +3363,7 @@ static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent,
 	struct iommu_hwpt_arm_smmuv3_invalidate *end;
 	struct arm_smmu_cmdq_ent ent;
 	struct arm_smmu_cmdq *cmdq;
+	bool must_lock = false;
 	int ret;
 
 	/* A zero-length array is allowed to validate the array type */
@@ -3335,12 +3386,17 @@ static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent,
 	if (ret)
 		goto out;
 
+	if (viommu)
+		must_lock = arm_smmu_must_lock_vdev_id(cmds, array->entry_num);
+	if (must_lock)
+		iommufd_viommu_lock_vdev_id(viommu);
+
 	ent.opcode = cmds->cmd[0] & CMDQ_0_OP;
 	cmdq = arm_smmu_get_cmdq(smmu, &ent);
 
 	last_batch = cmds;
 	while (cur != end) {
-		ret = arm_smmu_convert_user_cmd(s2_parent, cur);
+		ret = arm_smmu_convert_user_cmd(s2_parent, viommu, cur);
 		if (ret)
 			goto out;
 
@@ -3358,6 +3414,8 @@ static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent,
 		last_batch = cur;
 	}
 out:
+	if (must_lock)
+		iommufd_viommu_unlock_vdev_id(viommu);
 	array->entry_num = cur - cmds;
 	kfree(cmds);
 	return ret;
@@ -3370,7 +3428,7 @@ static int arm_smmu_cache_invalidate_user(struct iommu_domain *domain,
 		container_of(domain, struct arm_smmu_nested_domain, domain);
 
 	return __arm_smmu_cache_invalidate_user(
-			nested_domain->s2_parent, array);
+			nested_domain->s2_parent, NULL, array);
 }
 
 static const struct iommu_domain_ops arm_smmu_nested_ops = {
@@ -3863,6 +3921,15 @@ static int arm_smmu_def_domain_type(struct device *dev)
 	return 0;
 }
 
+static int arm_smmu_viommu_cache_invalidate(struct iommufd_viommu *viommu,
+					    struct iommu_user_data_array *array)
+{
+	struct iommu_domain *domain = iommufd_viommu_to_parent_domain(viommu);
+
+	return __arm_smmu_cache_invalidate_user(
+			to_smmu_domain(domain), viommu, array);
+}
+
 static struct iommu_ops arm_smmu_ops = {
 	.identity_domain	= &arm_smmu_identity_domain,
 	.blocked_domain		= &arm_smmu_blocked_domain,
@@ -3893,6 +3960,9 @@ static struct iommu_ops arm_smmu_ops = {
 		.iotlb_sync		= arm_smmu_iotlb_sync,
 		.iova_to_phys		= arm_smmu_iova_to_phys,
 		.free			= arm_smmu_domain_free_paging,
+		.default_viommu_ops = &(const struct iommufd_viommu_ops) {
+			.cache_invalidate = arm_smmu_viommu_cache_invalidate,
+		}
 	}
 };
 
diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
index 6c8ae70c90fe..e7f6e9194a9e 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h
@@ -10,6 +10,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/iommu.h>
+#include <linux/iommufd.h>
 #include <linux/kernel.h>
 #include <linux/mmzone.h>
 #include <linux/sizes.h>
diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h
index f3aefb11f681..0d973486b604 100644
--- a/include/uapi/linux/iommufd.h
+++ b/include/uapi/linux/iommufd.h
@@ -734,13 +734,18 @@ struct iommu_hwpt_vtd_s1_invalidate {
  * @cmd: 128-bit cache invalidation command that runs in SMMU CMDQ.
  *       Must be little-endian.
  *
- * Supported command list:
+ * Supported command list when passing in a HWPT via @hwpt_id:
  *     CMDQ_OP_TLBI_NSNH_ALL
  *     CMDQ_OP_TLBI_NH_VA
  *     CMDQ_OP_TLBI_NH_VAA
  *     CMDQ_OP_TLBI_NH_ALL
  *     CMDQ_OP_TLBI_NH_ASID
  *
+ * Additional to the list above, when passing in a VIOMMU via @hwpt_id:
+ *     CMDQ_OP_ATC_INV
+ *     CMDQ_OP_CFGI_CD
+ *     CMDQ_OP_CFGI_CD_ALL
+ *
  * -EIO will be returned if the command is not supported.
  */
 struct iommu_hwpt_arm_smmuv3_invalidate {
-- 
2.43.0


  parent reply	other threads:[~2024-08-27 17:02 UTC|newest]

Thread overview: 149+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-27 16:59 [PATCH v2 00/19] iommufd: Add VIOMMU infrastructure (Part-1) Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 01/19] iommufd: Reorder struct forward declarations Nicolin Chen
2024-09-05 15:44   ` Jason Gunthorpe
2024-08-27 16:59 ` [PATCH v2 02/19] iommufd/viommu: Add IOMMUFD_OBJ_VIOMMU and IOMMU_VIOMMU_ALLOC ioctl Nicolin Chen
2024-09-01  2:39   ` Baolu Lu
2024-09-02  5:27     ` Nicolin Chen
2024-09-04 16:26       ` Jason Gunthorpe
2024-09-04 17:29         ` Nicolin Chen
2024-09-04 23:37           ` Jason Gunthorpe
2024-09-05  3:07             ` Nicolin Chen
2024-09-12  3:39             ` Nicolin Chen
2024-09-15 21:10               ` Jason Gunthorpe
2024-09-05 15:53   ` Jason Gunthorpe
2024-09-05 17:10     ` Nicolin Chen
2024-09-05 17:41       ` Jason Gunthorpe
2024-09-05 18:04         ` Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 03/19] iommu: Pass in a viommu pointer to domain_alloc_user op Nicolin Chen
2024-09-05 15:54   ` Jason Gunthorpe
2024-09-05 17:19     ` Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 04/19] iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC Nicolin Chen
2024-09-05 15:56   ` Jason Gunthorpe
2024-09-26  8:50   ` Yi Liu
2024-09-26 20:10     ` Nicolin Chen
2024-09-27  0:43       ` Tian, Kevin
2024-09-27  1:25         ` Nicolin Chen
2024-09-27  2:23           ` Tian, Kevin
2024-09-27  2:57             ` Nicolin Chen
2024-09-27  5:38       ` Yi Liu
2024-09-27  6:02         ` Nicolin Chen
2024-09-27 11:59           ` Jason Gunthorpe
2024-09-27 19:52             ` Nicolin Chen
2024-09-27 12:12           ` Yi Liu
2024-09-27 20:29             ` Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 05/19] iommufd/selftest: Add IOMMU_VIOMMU_ALLOC test coverage Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 06/19] iommufd/viommu: Add IOMMU_VIOMMU_SET/UNSET_VDEV_ID ioctl Nicolin Chen
2024-09-05 16:03   ` Jason Gunthorpe
2024-09-05 17:37     ` Nicolin Chen
2024-09-05 17:43       ` Jason Gunthorpe
2024-09-05 20:14         ` Nicolin Chen
2024-09-11  6:19           ` Tian, Kevin
2024-09-11  7:11             ` Nicolin Chen
2024-09-11  7:18               ` Tian, Kevin
2024-09-11  7:47                 ` Nicolin Chen
2024-10-01  8:54       ` Nicolin Chen
2024-10-01 13:46         ` Jason Gunthorpe
2024-10-01 18:45           ` Nicolin Chen
2024-09-27 13:50   ` Mostafa Saleh
2024-09-27 14:01     ` Jason Gunthorpe
2024-09-27 14:22       ` Mostafa Saleh
2024-09-27 14:58         ` Jason Gunthorpe
2024-09-27 15:59           ` Mostafa Saleh
2024-10-04  4:32   ` Alexey Kardashevskiy
2024-10-04  5:33     ` Nicolin Chen
2024-10-04 11:41     ` Jason Gunthorpe
2024-10-04 18:13       ` Nicolin Chen
2024-10-04 18:50         ` Jason Gunthorpe
2024-10-04 19:25           ` Nicolin Chen
2024-10-04 20:17             ` Jason Gunthorpe
2024-10-04 20:33               ` Nicolin Chen
2024-10-07 17:18                 ` Jason Gunthorpe
2025-04-30  7:54       ` Alexey Kardashevskiy
2025-04-30  7:58         ` Nicolin Chen
2025-05-05 17:08           ` Jason Gunthorpe
2025-05-06  2:53             ` Nicolin Chen
2025-05-06  4:35               ` Alexey Kardashevskiy
2025-05-06  4:46               ` Tian, Kevin
2025-05-06 12:58               ` Jason Gunthorpe
2025-05-06 19:55                 ` Nicolin Chen
2025-05-07 12:28                   ` Jason Gunthorpe
2025-05-07 11:18                 ` Alexey Kardashevskiy
2025-05-07 12:24                   ` Jason Gunthorpe
2025-05-09  2:57                     ` Alexey Kardashevskiy
2025-05-09 22:07                       ` Jason Gunthorpe
2025-05-10 12:51                         ` Alexey Kardashevskiy
2025-05-12 15:31                           ` Jason Gunthorpe
2024-08-27 16:59 ` [PATCH v2 07/19] iommufd/selftest: Add IOMMU_VIOMMU_SET/UNSET_VDEV_ID test coverage Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 08/19] iommufd/viommu: Add cache_invalidate for IOMMU_VIOMMU_TYPE_DEFAULT Nicolin Chen
2024-09-05 16:05   ` Jason Gunthorpe
2024-08-27 16:59 ` [PATCH v2 09/19] iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE Nicolin Chen
2024-09-05 16:10   ` Jason Gunthorpe
2024-08-27 16:59 ` [PATCH v2 10/19] iommufd/viommu: Add vdev_id helpers for IOMMU drivers Nicolin Chen
2024-09-05 16:14   ` Jason Gunthorpe
2024-09-05 17:53     ` Nicolin Chen
2024-09-11 23:11       ` Jason Gunthorpe
2024-09-12  3:17         ` Nicolin Chen
2024-10-05  5:19           ` Nicolin Chen
2024-10-07 15:38             ` Jason Gunthorpe
2024-10-07 16:36               ` Nicolin Chen
2024-10-07 17:11                 ` Jason Gunthorpe
2024-10-07 17:25                   ` Nicolin Chen
2024-10-07 18:28                     ` Jason Gunthorpe
2024-10-07 19:56                       ` Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 11/19] iommu: Add iommu_copy_struct_from_full_user_array helper Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 12/19] iommufd/selftest: Add mock_viommu_invalidate_user op Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 13/19] iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_CACHE test command Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 14/19] iommufd/selftest: Add VIOMMU coverage for IOMMU_HWPT_INVALIDATE ioctl Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 15/19] iommufd/viommu: Add iommufd_viommu_to_parent_domain helper Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 16/19] iommu/arm-smmu-v3: Add arm_smmu_cache_invalidate_user Nicolin Chen
2024-09-05 16:23   ` Jason Gunthorpe
2024-09-05 17:56     ` Nicolin Chen
2024-08-27 16:59 ` Nicolin Chen [this message]
2024-09-05 16:20   ` [PATCH v2 17/19] iommu/arm-smmu-v3: Add arm_smmu_viommu_cache_invalidate Jason Gunthorpe
2024-09-05 18:00     ` Nicolin Chen
2024-09-05 18:21       ` Jason Gunthorpe
2024-09-11  6:25         ` Tian, Kevin
2024-09-11  7:20           ` Nicolin Chen
2024-09-11  7:50             ` Baolu Lu
2024-09-11  8:17               ` Tian, Kevin
2024-09-11  8:19                 ` Baolu Lu
2024-09-11 21:08                 ` Nicolin Chen
2024-09-12  4:45                   ` Baolu Lu
2024-09-11  8:13             ` Tian, Kevin
2024-09-11 20:53               ` Nicolin Chen
2024-09-11 23:07               ` Jason Gunthorpe
2024-09-13  2:33                 ` Tian, Kevin
2024-09-14 14:50                   ` Jason Gunthorpe
2024-09-18  8:10                     ` Tian, Kevin
2024-09-23 18:34                       ` Jason Gunthorpe
2024-09-11 23:00           ` Jason Gunthorpe
2024-08-27 16:59 ` [PATCH v2 18/19] iommu/arm-smmu-v3: Allow ATS for IOMMU_DOMAIN_NESTED Nicolin Chen
2024-08-27 16:59 ` [PATCH v2 19/19] iommu/arm-smmu-v3: Update comments about ATS and bypass Nicolin Chen
2024-09-11  6:12 ` [PATCH v2 00/19] iommufd: Add VIOMMU infrastructure (Part-1) Tian, Kevin
2024-09-11  7:08   ` Nicolin Chen
2024-09-11  7:18     ` Tian, Kevin
2024-09-11  7:40       ` Nicolin Chen
2024-09-11  8:08         ` Tian, Kevin
2024-09-11 20:21           ` Nicolin Chen
2024-10-01  1:55     ` Alexey Kardashevskiy
2024-10-01  3:36       ` Nicolin Chen
2024-10-01  5:06         ` Alexey Kardashevskiy
2024-10-01 13:44           ` Jason Gunthorpe
2024-09-25 10:30 ` Yi Liu
2024-09-25 18:55   ` Nicolin Chen
2024-09-26  8:47     ` Yi Liu
2024-09-26 20:03       ` Nicolin Chen
2024-09-27  2:05         ` Baolu Lu
2024-09-27  6:14           ` Yi Liu
2024-09-27  5:54         ` Yi Liu
2024-09-27  6:32           ` Nicolin Chen
2024-09-27 12:12             ` Yi Liu
2024-09-27 12:20               ` Jason Gunthorpe
2024-09-29  7:19                 ` Yi Liu
2024-10-01 13:48                   ` Jason Gunthorpe
2024-10-01 18:40                     ` Nicolin Chen
2024-09-27 20:44               ` Nicolin Chen
2024-09-29  7:16                 ` Yi Liu
2024-09-30 21:59                   ` Nicolin Chen
2024-10-09  7:20                     ` Yi Liu
2024-10-09 12:31                       ` Jason Gunthorpe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4b61aba3bc6c1cce628d9db44d5b18ea567a8be1.1724776335.git.nicolinc@nvidia.com \
    --to=nicolinc@nvidia.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=dwmw2@infradead.org \
    --cc=eric.auger@redhat.com \
    --cc=iommu@lists.linux.dev \
    --cc=jean-philippe@linaro.org \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mdf@kernel.org \
    --cc=mshavit@google.com \
    --cc=robin.murphy@arm.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=shuah@kernel.org \
    --cc=smostafa@google.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=will@kernel.org \
    --cc=yi.l.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®