mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
To: <linux-kernel@vger.kernel.org>, <iommu@lists.linux.dev>,
	<kvm@vger.kernel.org>
Cc: <joro@8bytes.org>, <robin.murphy@arm.com>, <yi.l.liu@intel.com>,
	<alex.williamson@redhat.com>, <jgg@nvidia.com>,
	<nicolinc@nvidia.com>, <baolu.lu@linux.intel.com>,
	<eric.auger@redhat.com>, <pandoh@google.com>,
	<kumaranand@google.com>, <jon.grimm@amd.com>,
	<santosh.shukla@amd.com>, <vasant.hegde@amd.com>,
	<jay.chen@amd.com>, <joseph.chung@amd.com>,
	"Suravee Suthikulpanit" <suravee.suthikulpanit@amd.com>
Subject: [RFC PATCH 21/21] iommufd: Introduce AMD HW-vIOMMU IOCTL
Date: Wed, 21 Jun 2023 18:55:08 -0500	[thread overview]
Message-ID: <20230621235508.113949-22-suravee.suthikulpanit@amd.com> (raw)
In-Reply-To: <20230621235508.113949-1-suravee.suthikulpanit@amd.com>

Add support for AMD HW-vIOMMU in the iommufd /dev/iommu devfs.

Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
---
 drivers/iommu/iommufd/Makefile     |   3 +-
 drivers/iommu/iommufd/amd_viommu.c | 158 +++++++++++++++++++++++++++++
 drivers/iommu/iommufd/main.c       |  17 ++--
 include/linux/amd-viommu.h         |  26 +++++
 include/linux/iommufd.h            |   8 ++
 5 files changed, 203 insertions(+), 9 deletions(-)
 create mode 100644 drivers/iommu/iommufd/amd_viommu.c
 create mode 100644 include/linux/amd-viommu.h

diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile
index 8aeba81800c5..84d771c9cfba 100644
--- a/drivers/iommu/iommufd/Makefile
+++ b/drivers/iommu/iommufd/Makefile
@@ -6,7 +6,8 @@ iommufd-y := \
 	ioas.o \
 	main.o \
 	pages.o \
-	vfio_compat.o
+	vfio_compat.o \
+	amd_viommu.o
 
 iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
 
diff --git a/drivers/iommu/iommufd/amd_viommu.c b/drivers/iommu/iommufd/amd_viommu.c
new file mode 100644
index 000000000000..1836e19cb37d
--- /dev/null
+++ b/drivers/iommu/iommufd/amd_viommu.c
@@ -0,0 +1,158 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2023 Advanced Micro Devices, Inc.
+ * Author: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
+ */
+
+#include <linux/file.h>
+#include <linux/fs.h>
+#include <linux/amd-viommu.h>
+#include <uapi/linux/amd_viommu.h>
+#include <linux/iommufd.h>
+
+#include "iommufd_private.h"
+
+union amd_viommu_ucmd_buffer {
+	struct amd_viommu_iommu_info iommu;
+	struct amd_viommu_dev_info dev;
+	struct amd_viommu_dom_info dom;
+	struct amd_viommu_mmio_data mmio;
+	struct amd_viommu_cmdbuf_data cmdbuf;
+};
+
+#define IOCTL_OP(_ioctl, _fn, _struct, _last)                                  \
+	[_IOC_NR(_ioctl) - IOMMUFD_VIOMMU_CMD_BASE] = {                        \
+		.size = sizeof(_struct) +                                      \
+			BUILD_BUG_ON_ZERO(sizeof(union amd_viommu_ucmd_buffer) <          \
+					  sizeof(_struct)),                    \
+		.min_size = offsetofend(_struct, _last),                       \
+		.ioctl_num = _ioctl,                                           \
+		.execute = _fn,                                                \
+	}
+
+int viommu_iommu_init(struct iommufd_ucmd *ucmd)
+{
+	int ret;
+	struct amd_viommu_iommu_info *data = ucmd->cmd;
+
+	ret = amd_viommu_iommu_init(data);
+	if (ret)
+		return ret;
+
+	if (copy_to_user(ucmd->ubuffer, data, sizeof(*data)))
+		ret = -EFAULT;
+	return ret;
+}
+
+int viommu_iommu_destroy(struct iommufd_ucmd *ucmd)
+{
+	struct amd_viommu_iommu_info *data = ucmd->cmd;
+
+	return amd_viommu_iommu_destroy(data);
+}
+
+int viommu_domain_attach(struct iommufd_ucmd *ucmd)
+{
+	struct amd_viommu_dom_info *data = ucmd->cmd;
+
+	return amd_viommu_domain_update(data, true);
+}
+
+int viommu_domain_detach(struct iommufd_ucmd *ucmd)
+{
+	struct amd_viommu_dom_info *data = ucmd->cmd;
+
+	return amd_viommu_domain_update(data, false);
+}
+
+int viommu_device_attach(struct iommufd_ucmd *ucmd)
+{
+	struct amd_viommu_dev_info *data = ucmd->cmd;
+
+	return amd_viommu_device_update(data, true);
+}
+
+int viommu_device_detach(struct iommufd_ucmd *ucmd)
+{
+	struct amd_viommu_dev_info *data = ucmd->cmd;
+
+	return amd_viommu_device_update(data, false);
+}
+
+int viommu_mmio_access(struct iommufd_ucmd *ucmd)
+{
+	int ret;
+	struct amd_viommu_mmio_data *data = ucmd->cmd;
+
+	if (data->is_write) {
+		ret = amd_viommu_guest_mmio_write(data);
+	} else {
+		ret = amd_viommu_guest_mmio_read(data);
+		if (ret)
+			return ret;
+
+		if (copy_to_user(ucmd->ubuffer, data, sizeof(*data)))
+			ret = -EFAULT;
+	}
+	return ret;
+}
+
+int viommu_cmdbuf_update(struct iommufd_ucmd *ucmd)
+{
+	struct amd_viommu_cmdbuf_data *data = ucmd->cmd;
+
+	return amd_viommu_cmdbuf_update(data);
+}
+
+struct iommufd_ioctl_op viommu_ioctl_ops[] = {
+	IOCTL_OP(VIOMMU_IOMMU_INIT, viommu_iommu_init,
+		 struct amd_viommu_iommu_info, gid),
+	IOCTL_OP(VIOMMU_IOMMU_DESTROY, viommu_iommu_destroy,
+		 struct amd_viommu_iommu_info, gid),
+	IOCTL_OP(VIOMMU_DEVICE_ATTACH, viommu_device_attach,
+		 struct amd_viommu_dev_info, queue_id),
+	IOCTL_OP(VIOMMU_DEVICE_DETACH, viommu_device_detach,
+		 struct amd_viommu_dev_info, queue_id),
+	IOCTL_OP(VIOMMU_DOMAIN_ATTACH, viommu_domain_attach,
+		 struct amd_viommu_dom_info, gdom_id),
+	IOCTL_OP(VIOMMU_DOMAIN_DETACH, viommu_domain_detach,
+		 struct amd_viommu_dom_info, gdom_id),
+	IOCTL_OP(VIOMMU_MMIO_ACCESS, viommu_mmio_access,
+		 struct amd_viommu_mmio_data, is_write),
+	IOCTL_OP(VIOMMU_CMDBUF_UPDATE, viommu_cmdbuf_update,
+		 struct amd_viommu_cmdbuf_data, hva),
+};
+
+long iommufd_amd_viommu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct iommufd_ctx *ictx = filp->private_data;
+	struct iommufd_ucmd ucmd = {};
+	struct iommufd_ioctl_op *op;
+	union amd_viommu_ucmd_buffer buf;
+	unsigned int nr;
+	int ret;
+
+	nr = _IOC_NR(cmd);
+	if (nr < IOMMUFD_VIOMMU_CMD_BASE ||
+	    (nr - IOMMUFD_VIOMMU_CMD_BASE) >= ARRAY_SIZE(viommu_ioctl_ops))
+		return -ENOIOCTLCMD;
+
+	ucmd.ictx = ictx;
+	ucmd.ubuffer = (void __user *)arg;
+	ret = get_user(ucmd.user_size, (u32 __user *)ucmd.ubuffer);
+	if (ret)
+		return ret;
+
+	op = &viommu_ioctl_ops[nr - IOMMUFD_VIOMMU_CMD_BASE];
+	if (op->ioctl_num != cmd)
+		return -ENOIOCTLCMD;
+	if (ucmd.user_size < op->min_size)
+		return -EOPNOTSUPP;
+
+	ucmd.cmd = &buf;
+	ret = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer,
+				    ucmd.user_size);
+	if (ret)
+		return ret;
+	return op->execute(&ucmd);
+}
diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 83f8b8f19bcb..d5c2738a8355 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -17,6 +17,8 @@
 #include <linux/bug.h>
 #include <uapi/linux/iommufd.h>
 #include <linux/iommufd.h>
+#include <uapi/linux/amd_viommu.h>
+#include <linux/amd-viommu.h>
 #include "../iommu-priv.h"
 
 #include "io_pagetable.h"
@@ -442,13 +444,6 @@ union ucmd_buffer {
 	struct iommu_hwpt_arm_smmuv3_invalidate smmuv3;
 };
 
-struct iommufd_ioctl_op {
-	unsigned int size;
-	unsigned int min_size;
-	unsigned int ioctl_num;
-	int (*execute)(struct iommufd_ucmd *ucmd);
-};
-
 #define IOCTL_OP(_ioctl, _fn, _struct, _last)                                  \
 	[_IOC_NR(_ioctl) - IOMMUFD_CMD_BASE] = {                               \
 		.size = sizeof(_struct) +                                      \
@@ -503,8 +498,14 @@ static long iommufd_fops_ioctl(struct file *filp, unsigned int cmd,
 
 	nr = _IOC_NR(cmd);
 	if (nr < IOMMUFD_CMD_BASE ||
-	    (nr - IOMMUFD_CMD_BASE) >= ARRAY_SIZE(iommufd_ioctl_ops))
+	    (nr - IOMMUFD_CMD_BASE) >= ARRAY_SIZE(iommufd_ioctl_ops)) {
+		/* AMD VIOMMU ioctl */
+		if (!iommufd_amd_viommu_ioctl(filp, cmd, arg))
+			return 0;
+
+		/* VFIO ioctl */
 		return iommufd_vfio_ioctl(ictx, cmd, arg);
+	}
 
 	ucmd.ictx = ictx;
 	ucmd.ubuffer = (void __user *)arg;
diff --git a/include/linux/amd-viommu.h b/include/linux/amd-viommu.h
new file mode 100644
index 000000000000..645e25c493c2
--- /dev/null
+++ b/include/linux/amd-viommu.h
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2022 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _LINUX_AMD_VIOMMU_H
+#define _LINUX_AMD_VIOMMU_H
+
+#include <uapi/linux/amd_viommu.h>
+
+extern long iommufd_amd_viommu_ioctl(struct file *filp,
+				     unsigned int cmd,
+				     unsigned long arg);
+
+extern long iommufd_viommu_ioctl(struct file *filp, unsigned int cmd,
+			  unsigned long arg);
+
+extern int amd_viommu_iommu_init(struct amd_viommu_iommu_info *data);
+extern int amd_viommu_iommu_destroy(struct amd_viommu_iommu_info *data);
+extern int amd_viommu_domain_update(struct amd_viommu_dom_info *data, bool is_set);
+extern int amd_viommu_device_update(struct amd_viommu_dev_info *data, bool is_set);
+extern int amd_viommu_guest_mmio_write(struct amd_viommu_mmio_data *data);
+extern int amd_viommu_guest_mmio_read(struct amd_viommu_mmio_data *data);
+extern int amd_viommu_cmdbuf_update(struct amd_viommu_cmdbuf_data *data);
+
+#endif /* _LINUX_AMD_VIOMMU_H */
diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h
index 9269ce668d9b..91912e044038 100644
--- a/include/linux/iommufd.h
+++ b/include/linux/iommufd.h
@@ -17,6 +17,14 @@ struct iommufd_ctx;
 struct iommufd_access;
 struct file;
 struct iommu_group;
+struct iommufd_ucmd;
+
+struct iommufd_ioctl_op {
+	unsigned int size;
+	unsigned int min_size;
+	unsigned int ioctl_num;
+	int (*execute)(struct iommufd_ucmd *ucmd);
+};
 
 struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
 					   struct device *dev, u32 *id);
-- 
2.34.1


  parent reply	other threads:[~2023-06-21 23:57 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-21 23:54 [RFC PATCH 00/21] iommu/amd: Introduce support for HW accelerated vIOMMU w/ nested page table Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 01/21] iommu/amd: Declare helper functions as extern Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 02/21] iommu/amd: Clean up spacing in amd_iommu_ops declaration Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 03/21] iommu/amd: Update PASID, GATS, and GLX feature related macros Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 04/21] iommu/amd: Modify domain_enable_v2() to add giov parameter Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 05/21] iommu/amd: Refactor set_dte_entry() helper function Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 06/21] iommu/amd: Modify set_dte_entry() to add gcr3 input parameter Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 07/21] iommu/amd: Modify set_dte_entry() to add user domain " Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 08/21] iommu/amd: Allow nested IOMMU page tables Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 09/21] iommu/amd: Add support for hw_info for iommu capability query Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 10/21] iommu/amd: Introduce vIOMMU-specific events and event info Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 11/21] iommu/amd: Introduce Reset vMMIO Command Suravee Suthikulpanit
2023-06-21 23:54 ` [RFC PATCH 12/21] iommu/amd: Introduce AMD vIOMMU-specific UAPI Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 13/21] iommu/amd: Introduce vIOMMU command-line option Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 14/21] iommu/amd: Initialize vIOMMU private address space regions Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 15/21] iommu/amd: Introduce vIOMMU vminit and vmdestroy ioctl Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 16/21] iommu/amd: Introduce vIOMMU ioctl for updating device mapping table Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 17/21] iommu/amd: Introduce vIOMMU ioctl for updating domain mapping Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 18/21] iommu/amd: Introduce vIOMMU ioctl for handling guest MMIO accesses Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 19/21] iommu/amd: Introduce vIOMMU ioctl for handling command buffer mapping Suravee Suthikulpanit
2023-06-21 23:55 ` [RFC PATCH 20/21] iommu/amd: Introduce vIOMMU ioctl for setting up guest CR3 Suravee Suthikulpanit
2023-06-21 23:55 ` Suravee Suthikulpanit [this message]
2023-06-22 13:46 ` [RFC PATCH 00/21] iommu/amd: Introduce support for HW accelerated vIOMMU w/ nested page table Jason Gunthorpe
2023-06-23  1:15   ` Suthikulpanit, Suravee
2023-06-23 11:45     ` Jason Gunthorpe
2023-06-23 22:05       ` Suthikulpanit, Suravee
2023-06-23 22:56         ` Jason Gunthorpe
2023-06-24  2:08           ` Suthikulpanit, Suravee
2023-06-26 13:20             ` 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=20230621235508.113949-22-suravee.suthikulpanit@amd.com \
    --to=suravee.suthikulpanit@amd.com \
    --cc=alex.williamson@redhat.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=eric.auger@redhat.com \
    --cc=iommu@lists.linux.dev \
    --cc=jay.chen@amd.com \
    --cc=jgg@nvidia.com \
    --cc=jon.grimm@amd.com \
    --cc=joro@8bytes.org \
    --cc=joseph.chung@amd.com \
    --cc=kumaranand@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicolinc@nvidia.com \
    --cc=pandoh@google.com \
    --cc=robin.murphy@arm.com \
    --cc=santosh.shukla@amd.com \
    --cc=vasant.hegde@amd.com \
    --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®