The associated kvm instance will be used in later patch by iommufd to bind a tdi to kvm. Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@xxxxxxxxxx> --- drivers/iommu/iommufd/viommu.c | 48 ++++++++++++++++++++++++++++++++-- include/linux/iommufd.h | 3 +++ include/uapi/linux/iommufd.h | 2 ++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c index 01df2b985f02..10d343871fb2 100644 --- a/drivers/iommu/iommufd/viommu.c +++ b/drivers/iommu/iommufd/viommu.c @@ -2,6 +2,37 @@ /* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES */ #include "iommufd_private.h" +#include "linux/tsm.h" + +#if IS_ENABLED(CONFIG_KVM) +#include <linux/kvm_host.h> + +static int viommu_get_kvm(struct iommufd_viommu *viommu, int kvm_vm_fd) +{ + int rc = -EBADF; + struct kvm *kvm; + struct fd f = fdget(kvm_vm_fd); + + if (!fd_file(f) || !file_is_kvm(fd_file(f))) + goto err_out; + + kvm = fd_file(f)->private_data; + if (!kvm) + goto err_out; + + /* hold the kvm reference via file descriptor */ + viommu->kvm_fd = f; + return rc; +err_out: + fdput(f); + return rc; +} + +static void viommu_put_kvm(struct iommufd_viommu *viommu) +{ + fdput(viommu->kvm_fd); +} +#endif void iommufd_viommu_destroy(struct iommufd_object *obj) { @@ -12,6 +43,8 @@ void iommufd_viommu_destroy(struct iommufd_object *obj) viommu->ops->destroy(viommu); refcount_dec(&viommu->hwpt->common.obj.users); xa_destroy(&viommu->vdevs); + + viommu_put_kvm(viommu); } int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) @@ -23,7 +56,9 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) const struct iommu_ops *ops; int rc; - if (cmd->flags || cmd->type == IOMMU_VIOMMU_TYPE_DEFAULT) + if (cmd->flags & ~IOMMU_VIOMMU_KVM_FD) + return -EOPNOTSUPP; + if (cmd->type == IOMMU_VIOMMU_TYPE_DEFAULT) return -EOPNOTSUPP; idev = iommufd_get_device(ucmd, cmd->dev_id); @@ -68,10 +103,19 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) */ viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev); + /* get the kvm details if specified. */ + if (cmd->flags & IOMMU_VIOMMU_KVM_FD) { + rc = viommu_get_kvm(viommu, cmd->kvm_vm_fd); + if (rc) + goto out_abort; + } + cmd->out_viommu_id = viommu->obj.id; rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); - if (rc) + if (rc) { + viommu_put_kvm(viommu); goto out_abort; + } iommufd_object_finalize(ucmd->ictx, &viommu->obj); goto out_put_hwpt; diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 34b6e6ca4bfa..ed5c404f1b0b 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -12,6 +12,7 @@ #include <linux/refcount.h> #include <linux/types.h> #include <linux/xarray.h> +#include <linux/file.h> #include <uapi/linux/iommufd.h> struct device; @@ -51,6 +52,7 @@ struct iommufd_object { unsigned int id; }; +struct kvm; struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id); void iommufd_device_unbind(struct iommufd_device *idev); @@ -94,6 +96,7 @@ struct iommufd_viommu { struct iommufd_ctx *ictx; struct iommu_device *iommu_dev; struct iommufd_hwpt_paging *hwpt; + struct fd kvm_fd; const struct iommufd_viommu_ops *ops; diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index f29b6c44655e..b3b962d857c7 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -957,6 +957,7 @@ enum iommu_viommu_type { IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1, }; +#define IOMMU_VIOMMU_KVM_FD BIT(0) /** * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC) * @size: sizeof(struct iommu_viommu_alloc) @@ -985,6 +986,7 @@ struct iommu_viommu_alloc { __u32 dev_id; __u32 hwpt_id; __u32 out_viommu_id; + __u32 kvm_vm_fd; }; #define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC) -- 2.43.0