12ff4bed7SJason Gunthorpe /* SPDX-License-Identifier: GPL-2.0-only */
22ff4bed7SJason Gunthorpe /* Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES
32ff4bed7SJason Gunthorpe  */
42ff4bed7SJason Gunthorpe #ifndef __IOMMUFD_PRIVATE_H
52ff4bed7SJason Gunthorpe #define __IOMMUFD_PRIVATE_H
62ff4bed7SJason Gunthorpe 
72ff4bed7SJason Gunthorpe #include <linux/rwsem.h>
82ff4bed7SJason Gunthorpe #include <linux/xarray.h>
92ff4bed7SJason Gunthorpe #include <linux/refcount.h>
102ff4bed7SJason Gunthorpe #include <linux/uaccess.h>
112ff4bed7SJason Gunthorpe 
1251fe6141SJason Gunthorpe struct iommu_domain;
1351fe6141SJason Gunthorpe struct iommu_group;
14aad37e71SJason Gunthorpe struct iommu_option;
157e7ec8a5SJason Gunthorpe struct iommufd_device;
1651fe6141SJason Gunthorpe 
172ff4bed7SJason Gunthorpe struct iommufd_ctx {
182ff4bed7SJason Gunthorpe 	struct file *file;
192ff4bed7SJason Gunthorpe 	struct xarray objects;
203a3329a7SJason Gunthorpe 	struct xarray groups;
2151fe6141SJason Gunthorpe 
2251fe6141SJason Gunthorpe 	u8 account_mode;
23c9a397ceSJason Gunthorpe 	/* Compatibility with VFIO no iommu */
24c9a397ceSJason Gunthorpe 	u8 no_iommu_mode;
25d624d665SJason Gunthorpe 	struct iommufd_ioas *vfio_ioas;
262ff4bed7SJason Gunthorpe };
272ff4bed7SJason Gunthorpe 
28f394576eSJason Gunthorpe /*
29f394576eSJason Gunthorpe  * The IOVA to PFN map. The map automatically copies the PFNs into multiple
30f394576eSJason Gunthorpe  * domains and permits sharing of PFNs between io_pagetable instances. This
31f394576eSJason Gunthorpe  * supports both a design where IOAS's are 1:1 with a domain (eg because the
32f394576eSJason Gunthorpe  * domain is HW customized), or where the IOAS is 1:N with multiple generic
33f394576eSJason Gunthorpe  * domains.  The io_pagetable holds an interval tree of iopt_areas which point
34f394576eSJason Gunthorpe  * to shared iopt_pages which hold the pfns mapped to the page table.
35f394576eSJason Gunthorpe  *
36f394576eSJason Gunthorpe  * The locking order is domains_rwsem -> iova_rwsem -> pages::mutex
37f394576eSJason Gunthorpe  */
38f394576eSJason Gunthorpe struct io_pagetable {
39f394576eSJason Gunthorpe 	struct rw_semaphore domains_rwsem;
40f394576eSJason Gunthorpe 	struct xarray domains;
4151fe6141SJason Gunthorpe 	struct xarray access_list;
42f394576eSJason Gunthorpe 	unsigned int next_domain_id;
43f394576eSJason Gunthorpe 
44f394576eSJason Gunthorpe 	struct rw_semaphore iova_rwsem;
45f394576eSJason Gunthorpe 	struct rb_root_cached area_itree;
46f394576eSJason Gunthorpe 	/* IOVA that cannot become reserved, struct iopt_allowed */
47f394576eSJason Gunthorpe 	struct rb_root_cached allowed_itree;
48f394576eSJason Gunthorpe 	/* IOVA that cannot be allocated, struct iopt_reserved */
49f394576eSJason Gunthorpe 	struct rb_root_cached reserved_itree;
50f394576eSJason Gunthorpe 	u8 disable_large_pages;
5151fe6141SJason Gunthorpe 	unsigned long iova_alignment;
52f394576eSJason Gunthorpe };
53f394576eSJason Gunthorpe 
5451fe6141SJason Gunthorpe void iopt_init_table(struct io_pagetable *iopt);
5551fe6141SJason Gunthorpe void iopt_destroy_table(struct io_pagetable *iopt);
5651fe6141SJason Gunthorpe int iopt_get_pages(struct io_pagetable *iopt, unsigned long iova,
5751fe6141SJason Gunthorpe 		   unsigned long length, struct list_head *pages_list);
5851fe6141SJason Gunthorpe void iopt_free_pages_list(struct list_head *pages_list);
5951fe6141SJason Gunthorpe enum {
6051fe6141SJason Gunthorpe 	IOPT_ALLOC_IOVA = 1 << 0,
6151fe6141SJason Gunthorpe };
6251fe6141SJason Gunthorpe int iopt_map_user_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt,
6351fe6141SJason Gunthorpe 			unsigned long *iova, void __user *uptr,
6451fe6141SJason Gunthorpe 			unsigned long length, int iommu_prot,
6551fe6141SJason Gunthorpe 			unsigned int flags);
6651fe6141SJason Gunthorpe int iopt_map_pages(struct io_pagetable *iopt, struct list_head *pages_list,
6751fe6141SJason Gunthorpe 		   unsigned long length, unsigned long *dst_iova,
6851fe6141SJason Gunthorpe 		   int iommu_prot, unsigned int flags);
6951fe6141SJason Gunthorpe int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova,
7051fe6141SJason Gunthorpe 		    unsigned long length, unsigned long *unmapped);
7151fe6141SJason Gunthorpe int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped);
7251fe6141SJason Gunthorpe 
738d40205fSJason Gunthorpe void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova,
748d40205fSJason Gunthorpe 				 unsigned long length);
7551fe6141SJason Gunthorpe int iopt_table_add_domain(struct io_pagetable *iopt,
7651fe6141SJason Gunthorpe 			  struct iommu_domain *domain);
7751fe6141SJason Gunthorpe void iopt_table_remove_domain(struct io_pagetable *iopt,
7851fe6141SJason Gunthorpe 			      struct iommu_domain *domain);
7934f327a9SJason Gunthorpe int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt,
8034f327a9SJason Gunthorpe 					struct device *dev,
8151fe6141SJason Gunthorpe 					phys_addr_t *sw_msi_start);
8251fe6141SJason Gunthorpe int iopt_set_allow_iova(struct io_pagetable *iopt,
8351fe6141SJason Gunthorpe 			struct rb_root_cached *allowed_iova);
8451fe6141SJason Gunthorpe int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start,
8551fe6141SJason Gunthorpe 		      unsigned long last, void *owner);
8651fe6141SJason Gunthorpe void iopt_remove_reserved_iova(struct io_pagetable *iopt, void *owner);
8751fe6141SJason Gunthorpe int iopt_cut_iova(struct io_pagetable *iopt, unsigned long *iovas,
8851fe6141SJason Gunthorpe 		  size_t num_iovas);
8951fe6141SJason Gunthorpe void iopt_enable_large_pages(struct io_pagetable *iopt);
9051fe6141SJason Gunthorpe int iopt_disable_large_pages(struct io_pagetable *iopt);
9151fe6141SJason Gunthorpe 
922ff4bed7SJason Gunthorpe struct iommufd_ucmd {
932ff4bed7SJason Gunthorpe 	struct iommufd_ctx *ictx;
942ff4bed7SJason Gunthorpe 	void __user *ubuffer;
952ff4bed7SJason Gunthorpe 	u32 user_size;
962ff4bed7SJason Gunthorpe 	void *cmd;
972ff4bed7SJason Gunthorpe };
982ff4bed7SJason Gunthorpe 
99d624d665SJason Gunthorpe int iommufd_vfio_ioctl(struct iommufd_ctx *ictx, unsigned int cmd,
100d624d665SJason Gunthorpe 		       unsigned long arg);
101d624d665SJason Gunthorpe 
1022ff4bed7SJason Gunthorpe /* Copy the response in ucmd->cmd back to userspace. */
iommufd_ucmd_respond(struct iommufd_ucmd * ucmd,size_t cmd_len)1032ff4bed7SJason Gunthorpe static inline int iommufd_ucmd_respond(struct iommufd_ucmd *ucmd,
1042ff4bed7SJason Gunthorpe 				       size_t cmd_len)
1052ff4bed7SJason Gunthorpe {
1062ff4bed7SJason Gunthorpe 	if (copy_to_user(ucmd->ubuffer, ucmd->cmd,
1072ff4bed7SJason Gunthorpe 			 min_t(size_t, ucmd->user_size, cmd_len)))
1082ff4bed7SJason Gunthorpe 		return -EFAULT;
1092ff4bed7SJason Gunthorpe 	return 0;
1102ff4bed7SJason Gunthorpe }
1112ff4bed7SJason Gunthorpe 
1122ff4bed7SJason Gunthorpe enum iommufd_object_type {
1132ff4bed7SJason Gunthorpe 	IOMMUFD_OBJ_NONE,
1142ff4bed7SJason Gunthorpe 	IOMMUFD_OBJ_ANY = IOMMUFD_OBJ_NONE,
115e8d57210SJason Gunthorpe 	IOMMUFD_OBJ_DEVICE,
116ea4acfacSJason Gunthorpe 	IOMMUFD_OBJ_HW_PAGETABLE,
117aad37e71SJason Gunthorpe 	IOMMUFD_OBJ_IOAS,
1188d40205fSJason Gunthorpe 	IOMMUFD_OBJ_ACCESS,
119f4b20bb3SJason Gunthorpe #ifdef CONFIG_IOMMUFD_TEST
120f4b20bb3SJason Gunthorpe 	IOMMUFD_OBJ_SELFTEST,
121f4b20bb3SJason Gunthorpe #endif
12283f7bc6fSJason Gunthorpe 	IOMMUFD_OBJ_MAX,
1232ff4bed7SJason Gunthorpe };
1242ff4bed7SJason Gunthorpe 
1252ff4bed7SJason Gunthorpe /* Base struct for all objects with a userspace ID handle. */
1262ff4bed7SJason Gunthorpe struct iommufd_object {
1272ff4bed7SJason Gunthorpe 	struct rw_semaphore destroy_rwsem;
1282ff4bed7SJason Gunthorpe 	refcount_t users;
1292ff4bed7SJason Gunthorpe 	enum iommufd_object_type type;
1302ff4bed7SJason Gunthorpe 	unsigned int id;
1312ff4bed7SJason Gunthorpe };
1322ff4bed7SJason Gunthorpe 
iommufd_lock_obj(struct iommufd_object * obj)1332ff4bed7SJason Gunthorpe static inline bool iommufd_lock_obj(struct iommufd_object *obj)
1342ff4bed7SJason Gunthorpe {
1352ff4bed7SJason Gunthorpe 	if (!down_read_trylock(&obj->destroy_rwsem))
1362ff4bed7SJason Gunthorpe 		return false;
1372ff4bed7SJason Gunthorpe 	if (!refcount_inc_not_zero(&obj->users)) {
1382ff4bed7SJason Gunthorpe 		up_read(&obj->destroy_rwsem);
1392ff4bed7SJason Gunthorpe 		return false;
1402ff4bed7SJason Gunthorpe 	}
1412ff4bed7SJason Gunthorpe 	return true;
1422ff4bed7SJason Gunthorpe }
1432ff4bed7SJason Gunthorpe 
1442ff4bed7SJason Gunthorpe struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,
1452ff4bed7SJason Gunthorpe 					  enum iommufd_object_type type);
iommufd_put_object(struct iommufd_object * obj)1462ff4bed7SJason Gunthorpe static inline void iommufd_put_object(struct iommufd_object *obj)
1472ff4bed7SJason Gunthorpe {
1482ff4bed7SJason Gunthorpe 	refcount_dec(&obj->users);
1492ff4bed7SJason Gunthorpe 	up_read(&obj->destroy_rwsem);
1502ff4bed7SJason Gunthorpe }
1512ff4bed7SJason Gunthorpe 
1522ff4bed7SJason Gunthorpe void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj);
1532ff4bed7SJason Gunthorpe void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx,
1542ff4bed7SJason Gunthorpe 				      struct iommufd_object *obj);
1552ff4bed7SJason Gunthorpe void iommufd_object_finalize(struct iommufd_ctx *ictx,
1562ff4bed7SJason Gunthorpe 			     struct iommufd_object *obj);
15799f98a7cSJason Gunthorpe void __iommufd_object_destroy_user(struct iommufd_ctx *ictx,
15899f98a7cSJason Gunthorpe 				   struct iommufd_object *obj, bool allow_fail);
iommufd_object_destroy_user(struct iommufd_ctx * ictx,struct iommufd_object * obj)15999f98a7cSJason Gunthorpe static inline void iommufd_object_destroy_user(struct iommufd_ctx *ictx,
16099f98a7cSJason Gunthorpe 					       struct iommufd_object *obj)
16199f98a7cSJason Gunthorpe {
16299f98a7cSJason Gunthorpe 	__iommufd_object_destroy_user(ictx, obj, false);
16399f98a7cSJason Gunthorpe }
iommufd_object_deref_user(struct iommufd_ctx * ictx,struct iommufd_object * obj)16499f98a7cSJason Gunthorpe static inline void iommufd_object_deref_user(struct iommufd_ctx *ictx,
16599f98a7cSJason Gunthorpe 					     struct iommufd_object *obj)
16699f98a7cSJason Gunthorpe {
16799f98a7cSJason Gunthorpe 	__iommufd_object_destroy_user(ictx, obj, true);
16899f98a7cSJason Gunthorpe }
16999f98a7cSJason Gunthorpe 
1702ff4bed7SJason Gunthorpe struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
1712ff4bed7SJason Gunthorpe 					     size_t size,
1722ff4bed7SJason Gunthorpe 					     enum iommufd_object_type type);
1732ff4bed7SJason Gunthorpe 
1742ff4bed7SJason Gunthorpe #define iommufd_object_alloc(ictx, ptr, type)                                  \
1752ff4bed7SJason Gunthorpe 	container_of(_iommufd_object_alloc(                                    \
1762ff4bed7SJason Gunthorpe 			     ictx,                                             \
1772ff4bed7SJason Gunthorpe 			     sizeof(*(ptr)) + BUILD_BUG_ON_ZERO(               \
1782ff4bed7SJason Gunthorpe 						      offsetof(typeof(*(ptr)), \
1792ff4bed7SJason Gunthorpe 							       obj) != 0),     \
1802ff4bed7SJason Gunthorpe 			     type),                                            \
1812ff4bed7SJason Gunthorpe 		     typeof(*(ptr)), obj)
1822ff4bed7SJason Gunthorpe 
183aad37e71SJason Gunthorpe /*
184aad37e71SJason Gunthorpe  * The IO Address Space (IOAS) pagetable is a virtual page table backed by the
185aad37e71SJason Gunthorpe  * io_pagetable object. It is a user controlled mapping of IOVA -> PFNs. The
186aad37e71SJason Gunthorpe  * mapping is copied into all of the associated domains and made available to
187aad37e71SJason Gunthorpe  * in-kernel users.
188ea4acfacSJason Gunthorpe  *
189ea4acfacSJason Gunthorpe  * Every iommu_domain that is created is wrapped in a iommufd_hw_pagetable
190ea4acfacSJason Gunthorpe  * object. When we go to attach a device to an IOAS we need to get an
191ea4acfacSJason Gunthorpe  * iommu_domain and wrapping iommufd_hw_pagetable for it.
192ea4acfacSJason Gunthorpe  *
193ea4acfacSJason Gunthorpe  * An iommu_domain & iommfd_hw_pagetable will be automatically selected
194ea4acfacSJason Gunthorpe  * for a device based on the hwpt_list. If no suitable iommu_domain
195ea4acfacSJason Gunthorpe  * is found a new iommu_domain will be created.
196aad37e71SJason Gunthorpe  */
197aad37e71SJason Gunthorpe struct iommufd_ioas {
198aad37e71SJason Gunthorpe 	struct iommufd_object obj;
199aad37e71SJason Gunthorpe 	struct io_pagetable iopt;
200ea4acfacSJason Gunthorpe 	struct mutex mutex;
201ea4acfacSJason Gunthorpe 	struct list_head hwpt_list;
202aad37e71SJason Gunthorpe };
203aad37e71SJason Gunthorpe 
iommufd_get_ioas(struct iommufd_ctx * ictx,u32 id)204325de950SYi Liu static inline struct iommufd_ioas *iommufd_get_ioas(struct iommufd_ctx *ictx,
205aad37e71SJason Gunthorpe 						    u32 id)
206aad37e71SJason Gunthorpe {
207325de950SYi Liu 	return container_of(iommufd_get_object(ictx, id,
208aad37e71SJason Gunthorpe 					       IOMMUFD_OBJ_IOAS),
209aad37e71SJason Gunthorpe 			    struct iommufd_ioas, obj);
210aad37e71SJason Gunthorpe }
211aad37e71SJason Gunthorpe 
212aad37e71SJason Gunthorpe struct iommufd_ioas *iommufd_ioas_alloc(struct iommufd_ctx *ictx);
213aad37e71SJason Gunthorpe int iommufd_ioas_alloc_ioctl(struct iommufd_ucmd *ucmd);
214aad37e71SJason Gunthorpe void iommufd_ioas_destroy(struct iommufd_object *obj);
215aad37e71SJason Gunthorpe int iommufd_ioas_iova_ranges(struct iommufd_ucmd *ucmd);
216aad37e71SJason Gunthorpe int iommufd_ioas_allow_iovas(struct iommufd_ucmd *ucmd);
217aad37e71SJason Gunthorpe int iommufd_ioas_map(struct iommufd_ucmd *ucmd);
218aad37e71SJason Gunthorpe int iommufd_ioas_copy(struct iommufd_ucmd *ucmd);
219aad37e71SJason Gunthorpe int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd);
220aad37e71SJason Gunthorpe int iommufd_ioas_option(struct iommufd_ucmd *ucmd);
221aad37e71SJason Gunthorpe int iommufd_option_rlimit_mode(struct iommu_option *cmd,
222aad37e71SJason Gunthorpe 			       struct iommufd_ctx *ictx);
223aad37e71SJason Gunthorpe 
224d624d665SJason Gunthorpe int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd);
225d624d665SJason Gunthorpe 
226ea4acfacSJason Gunthorpe /*
227ea4acfacSJason Gunthorpe  * A HW pagetable is called an iommu_domain inside the kernel. This user object
228ea4acfacSJason Gunthorpe  * allows directly creating and inspecting the domains. Domains that have kernel
229ea4acfacSJason Gunthorpe  * owned page tables will be associated with an iommufd_ioas that provides the
230ea4acfacSJason Gunthorpe  * IOVA to PFN map.
231ea4acfacSJason Gunthorpe  */
232ea4acfacSJason Gunthorpe struct iommufd_hw_pagetable {
233ea4acfacSJason Gunthorpe 	struct iommufd_object obj;
234ea4acfacSJason Gunthorpe 	struct iommufd_ioas *ioas;
235ea4acfacSJason Gunthorpe 	struct iommu_domain *domain;
236ea4acfacSJason Gunthorpe 	bool auto_domain : 1;
237e8d57210SJason Gunthorpe 	bool enforce_cache_coherency : 1;
238e8d57210SJason Gunthorpe 	bool msi_cookie : 1;
239ea4acfacSJason Gunthorpe 	/* Head at iommufd_ioas::hwpt_list */
240ea4acfacSJason Gunthorpe 	struct list_head hwpt_item;
241ea4acfacSJason Gunthorpe };
242ea4acfacSJason Gunthorpe 
243ea4acfacSJason Gunthorpe struct iommufd_hw_pagetable *
244ea4acfacSJason Gunthorpe iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
245339fbf3aSJason Gunthorpe 			   struct iommufd_device *idev, bool immediate_attach);
24617bad527SJason Gunthorpe int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt);
247339fbf3aSJason Gunthorpe int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
248339fbf3aSJason Gunthorpe 				struct iommufd_device *idev);
24991a2e17eSJason Gunthorpe struct iommufd_hw_pagetable *
25091a2e17eSJason Gunthorpe iommufd_hw_pagetable_detach(struct iommufd_device *idev);
251ea4acfacSJason Gunthorpe void iommufd_hw_pagetable_destroy(struct iommufd_object *obj);
25270eadc7fSJason Gunthorpe void iommufd_hw_pagetable_abort(struct iommufd_object *obj);
2537074d7bdSJason Gunthorpe int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd);
254ea4acfacSJason Gunthorpe 
iommufd_hw_pagetable_put(struct iommufd_ctx * ictx,struct iommufd_hw_pagetable * hwpt)255d03f1336SJason Gunthorpe static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx,
256d03f1336SJason Gunthorpe 					    struct iommufd_hw_pagetable *hwpt)
257d03f1336SJason Gunthorpe {
258d03f1336SJason Gunthorpe 	lockdep_assert_not_held(&hwpt->ioas->mutex);
259d03f1336SJason Gunthorpe 	if (hwpt->auto_domain)
260a35762ddSJason Gunthorpe 		iommufd_object_deref_user(ictx, &hwpt->obj);
261d03f1336SJason Gunthorpe 	else
262d03f1336SJason Gunthorpe 		refcount_dec(&hwpt->obj.users);
263d03f1336SJason Gunthorpe }
264d03f1336SJason Gunthorpe 
2653a3329a7SJason Gunthorpe struct iommufd_group {
2663a3329a7SJason Gunthorpe 	struct kref ref;
26791a2e17eSJason Gunthorpe 	struct mutex lock;
2683a3329a7SJason Gunthorpe 	struct iommufd_ctx *ictx;
2693a3329a7SJason Gunthorpe 	struct iommu_group *group;
27091a2e17eSJason Gunthorpe 	struct iommufd_hw_pagetable *hwpt;
27191a2e17eSJason Gunthorpe 	struct list_head device_list;
2721d149ab2SJason Gunthorpe 	phys_addr_t sw_msi_start;
2733a3329a7SJason Gunthorpe };
2743a3329a7SJason Gunthorpe 
2757e7ec8a5SJason Gunthorpe /*
2767e7ec8a5SJason Gunthorpe  * A iommufd_device object represents the binding relationship between a
2777e7ec8a5SJason Gunthorpe  * consuming driver and the iommufd. These objects are created/destroyed by
2787e7ec8a5SJason Gunthorpe  * external drivers, not by userspace.
2797e7ec8a5SJason Gunthorpe  */
2807e7ec8a5SJason Gunthorpe struct iommufd_device {
2817e7ec8a5SJason Gunthorpe 	struct iommufd_object obj;
2827e7ec8a5SJason Gunthorpe 	struct iommufd_ctx *ictx;
2833a3329a7SJason Gunthorpe 	struct iommufd_group *igroup;
28491a2e17eSJason Gunthorpe 	struct list_head group_item;
2857e7ec8a5SJason Gunthorpe 	/* always the physical device */
2867e7ec8a5SJason Gunthorpe 	struct device *dev;
2877e7ec8a5SJason Gunthorpe 	bool enforce_cache_coherency;
2887e7ec8a5SJason Gunthorpe };
2897e7ec8a5SJason Gunthorpe 
2907074d7bdSJason Gunthorpe static inline struct iommufd_device *
iommufd_get_device(struct iommufd_ucmd * ucmd,u32 id)2917074d7bdSJason Gunthorpe iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id)
2927074d7bdSJason Gunthorpe {
2937074d7bdSJason Gunthorpe 	return container_of(iommufd_get_object(ucmd->ictx, id,
2947074d7bdSJason Gunthorpe 					       IOMMUFD_OBJ_DEVICE),
2957074d7bdSJason Gunthorpe 			    struct iommufd_device, obj);
2967074d7bdSJason Gunthorpe }
2977074d7bdSJason Gunthorpe 
298e8d57210SJason Gunthorpe void iommufd_device_destroy(struct iommufd_object *obj);
299*55dd4023SYi Liu int iommufd_get_hw_info(struct iommufd_ucmd *ucmd);
300e8d57210SJason Gunthorpe 
30151fe6141SJason Gunthorpe struct iommufd_access {
3028d40205fSJason Gunthorpe 	struct iommufd_object obj;
3038d40205fSJason Gunthorpe 	struct iommufd_ctx *ictx;
3048d40205fSJason Gunthorpe 	struct iommufd_ioas *ioas;
305e23a6217SNicolin Chen 	struct iommufd_ioas *ioas_unpin;
306e23a6217SNicolin Chen 	struct mutex ioas_lock;
3078d40205fSJason Gunthorpe 	const struct iommufd_access_ops *ops;
3088d40205fSJason Gunthorpe 	void *data;
30951fe6141SJason Gunthorpe 	unsigned long iova_alignment;
31051fe6141SJason Gunthorpe 	u32 iopt_access_list_id;
31151fe6141SJason Gunthorpe };
31251fe6141SJason Gunthorpe 
31351fe6141SJason Gunthorpe int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access);
31451fe6141SJason Gunthorpe void iopt_remove_access(struct io_pagetable *iopt,
3155d5c85ffSNicolin Chen 			struct iommufd_access *access,
3165d5c85ffSNicolin Chen 			u32 iopt_access_list_id);
3178d40205fSJason Gunthorpe void iommufd_access_destroy_object(struct iommufd_object *obj);
3188d40205fSJason Gunthorpe 
319f4b20bb3SJason Gunthorpe #ifdef CONFIG_IOMMUFD_TEST
320f4b20bb3SJason Gunthorpe int iommufd_test(struct iommufd_ucmd *ucmd);
321f4b20bb3SJason Gunthorpe void iommufd_selftest_destroy(struct iommufd_object *obj);
322f4b20bb3SJason Gunthorpe extern size_t iommufd_test_memory_limit;
323f4b20bb3SJason Gunthorpe void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
324f4b20bb3SJason Gunthorpe 				   unsigned int ioas_id, u64 *iova, u32 *flags);
325f4b20bb3SJason Gunthorpe bool iommufd_should_fail(void);
32623a1b46fSJason Gunthorpe int __init iommufd_test_init(void);
327f4b20bb3SJason Gunthorpe void iommufd_test_exit(void);
32865c619aeSJason Gunthorpe bool iommufd_selftest_is_mock_dev(struct device *dev);
329f4b20bb3SJason Gunthorpe #else
iommufd_test_syz_conv_iova_id(struct iommufd_ucmd * ucmd,unsigned int ioas_id,u64 * iova,u32 * flags)330f4b20bb3SJason Gunthorpe static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
331f4b20bb3SJason Gunthorpe 						 unsigned int ioas_id,
332f4b20bb3SJason Gunthorpe 						 u64 *iova, u32 *flags)
333f4b20bb3SJason Gunthorpe {
334f4b20bb3SJason Gunthorpe }
iommufd_should_fail(void)335f4b20bb3SJason Gunthorpe static inline bool iommufd_should_fail(void)
336f4b20bb3SJason Gunthorpe {
337f4b20bb3SJason Gunthorpe 	return false;
338f4b20bb3SJason Gunthorpe }
iommufd_test_init(void)33923a1b46fSJason Gunthorpe static inline int __init iommufd_test_init(void)
340f4b20bb3SJason Gunthorpe {
34123a1b46fSJason Gunthorpe 	return 0;
342f4b20bb3SJason Gunthorpe }
iommufd_test_exit(void)343f4b20bb3SJason Gunthorpe static inline void iommufd_test_exit(void)
344f4b20bb3SJason Gunthorpe {
345f4b20bb3SJason Gunthorpe }
iommufd_selftest_is_mock_dev(struct device * dev)34665c619aeSJason Gunthorpe static inline bool iommufd_selftest_is_mock_dev(struct device *dev)
34765c619aeSJason Gunthorpe {
34865c619aeSJason Gunthorpe 	return false;
34965c619aeSJason Gunthorpe }
350f4b20bb3SJason Gunthorpe #endif
3512ff4bed7SJason Gunthorpe #endif
352