2022-11-29 16:29:29 -04:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Intel Corporation
|
|
|
|
* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
|
|
|
|
*/
|
|
|
|
#ifndef __LINUX_IOMMUFD_H
|
|
|
|
#define __LINUX_IOMMUFD_H
|
|
|
|
|
|
|
|
#include <linux/err.h>
|
2024-08-02 17:32:02 -07:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/types.h>
|
2022-11-29 16:29:29 -04:00
|
|
|
|
2022-11-29 16:29:37 -04:00
|
|
|
struct device;
|
2022-11-29 16:29:29 -04:00
|
|
|
struct file;
|
2023-07-18 03:55:36 -07:00
|
|
|
struct iommu_group;
|
2024-08-27 09:59:38 -07:00
|
|
|
struct iommufd_access;
|
|
|
|
struct iommufd_ctx;
|
|
|
|
struct iommufd_device;
|
|
|
|
struct page;
|
2022-11-29 16:29:29 -04:00
|
|
|
|
2022-11-29 16:29:36 -04:00
|
|
|
struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
|
|
|
|
struct device *dev, u32 *id);
|
|
|
|
void iommufd_device_unbind(struct iommufd_device *idev);
|
|
|
|
|
|
|
|
int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id);
|
2023-07-17 15:12:12 -03:00
|
|
|
int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id);
|
2022-11-29 16:29:36 -04:00
|
|
|
void iommufd_device_detach(struct iommufd_device *idev);
|
|
|
|
|
2023-07-18 03:55:37 -07:00
|
|
|
struct iommufd_ctx *iommufd_device_to_ictx(struct iommufd_device *idev);
|
|
|
|
u32 iommufd_device_to_id(struct iommufd_device *idev);
|
|
|
|
|
2022-11-29 16:29:37 -04:00
|
|
|
struct iommufd_access_ops {
|
|
|
|
u8 needs_pin_pages : 1;
|
|
|
|
void (*unmap)(void *data, unsigned long iova, unsigned long length);
|
|
|
|
};
|
|
|
|
|
2022-11-29 16:29:31 -04:00
|
|
|
enum {
|
|
|
|
IOMMUFD_ACCESS_RW_READ = 0,
|
|
|
|
IOMMUFD_ACCESS_RW_WRITE = 1 << 0,
|
|
|
|
/* Set if the caller is in a kthread then rw will use kthread_use_mm() */
|
|
|
|
IOMMUFD_ACCESS_RW_KTHREAD = 1 << 1,
|
2022-11-29 16:29:39 -04:00
|
|
|
|
|
|
|
/* Only for use by selftest */
|
|
|
|
__IOMMUFD_ACCESS_RW_SLOW_PATH = 1 << 2,
|
2022-11-29 16:29:31 -04:00
|
|
|
};
|
|
|
|
|
2022-11-29 16:29:37 -04:00
|
|
|
struct iommufd_access *
|
2023-03-27 02:33:47 -07:00
|
|
|
iommufd_access_create(struct iommufd_ctx *ictx,
|
2023-03-27 02:33:49 -07:00
|
|
|
const struct iommufd_access_ops *ops, void *data, u32 *id);
|
2022-11-29 16:29:37 -04:00
|
|
|
void iommufd_access_destroy(struct iommufd_access *access);
|
2023-03-27 02:33:47 -07:00
|
|
|
int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id);
|
2023-07-27 23:33:27 -07:00
|
|
|
int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id);
|
iommufd/device: Add iommufd_access_detach() API
Previously, the detach routine is only done by the destroy(). And it was
called by vfio_iommufd_emulated_unbind() when the device runs close(), so
all the mappings in iopt were cleaned in that setup, when the call trace
reaches this detach() routine.
Now, there's a need of a detach uAPI, meaning that it does not only need
a new iommufd_access_detach() API, but also requires access->ops->unmap()
call as a cleanup. So add one.
However, leaving that unprotected can introduce some potential of a race
condition during the pin_/unpin_pages() call, where access->ioas->iopt is
getting referenced. So, add an ioas_lock to protect the context of iopt
referencings.
Also, to allow the iommufd_access_unpin_pages() callback to happen via
this unmap() call, add an ioas_unpin pointer, so the unpin routine won't
be affected by the "access->ioas = NULL" trick.
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Tested-by: Terrence Xu <terrence.xu@intel.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
Tested-by: Yanting Jiang <yanting.jiang@intel.com>
Tested-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
Tested-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20230718135551.6592-15-yi.l.liu@intel.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2023-07-18 06:55:39 -07:00
|
|
|
void iommufd_access_detach(struct iommufd_access *access);
|
2022-11-29 16:29:37 -04:00
|
|
|
|
2022-11-29 16:29:29 -04:00
|
|
|
void iommufd_ctx_get(struct iommufd_ctx *ictx);
|
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_IOMMUFD)
|
|
|
|
struct iommufd_ctx *iommufd_ctx_from_file(struct file *file);
|
2023-07-18 06:55:45 -07:00
|
|
|
struct iommufd_ctx *iommufd_ctx_from_fd(int fd);
|
2022-11-29 16:29:29 -04:00
|
|
|
void iommufd_ctx_put(struct iommufd_ctx *ictx);
|
2023-07-18 03:55:36 -07:00
|
|
|
bool iommufd_ctx_has_group(struct iommufd_ctx *ictx, struct iommu_group *group);
|
2022-11-29 16:29:37 -04:00
|
|
|
|
|
|
|
int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
|
|
|
|
unsigned long length, struct page **out_pages,
|
|
|
|
unsigned int flags);
|
|
|
|
void iommufd_access_unpin_pages(struct iommufd_access *access,
|
|
|
|
unsigned long iova, unsigned long length);
|
|
|
|
int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
|
|
|
|
void *data, size_t len, unsigned int flags);
|
2023-01-18 13:50:28 -04:00
|
|
|
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);
|
2022-11-29 16:29:29 -04:00
|
|
|
#else /* !CONFIG_IOMMUFD */
|
|
|
|
static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file)
|
|
|
|
{
|
|
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void iommufd_ctx_put(struct iommufd_ctx *ictx)
|
|
|
|
{
|
|
|
|
}
|
2022-11-29 16:29:37 -04:00
|
|
|
|
|
|
|
static inline int iommufd_access_pin_pages(struct iommufd_access *access,
|
|
|
|
unsigned long iova,
|
|
|
|
unsigned long length,
|
|
|
|
struct page **out_pages,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void iommufd_access_unpin_pages(struct iommufd_access *access,
|
|
|
|
unsigned long iova,
|
|
|
|
unsigned long length)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
|
|
|
|
void *data, size_t len, unsigned int flags)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2022-11-29 16:29:38 -04:00
|
|
|
|
2023-01-18 13:50:28 -04:00
|
|
|
static inline int iommufd_vfio_compat_ioas_create(struct iommufd_ctx *ictx)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx)
|
2022-11-29 16:29:38 -04:00
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
2022-11-29 16:29:29 -04:00
|
|
|
#endif /* CONFIG_IOMMUFD */
|
|
|
|
#endif
|