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. */
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 
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);
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 /**
1532ff4bed7SJason Gunthorpe  * iommufd_ref_to_users() - Switch from destroy_rwsem to users refcount
1542ff4bed7SJason Gunthorpe  *        protection
1552ff4bed7SJason Gunthorpe  * @obj - Object to release
1562ff4bed7SJason Gunthorpe  *
1572ff4bed7SJason Gunthorpe  * Objects have two refcount protections (destroy_rwsem and the refcount_t
1582ff4bed7SJason Gunthorpe  * users). Holding either of these will prevent the object from being destroyed.
1592ff4bed7SJason Gunthorpe  *
1602ff4bed7SJason Gunthorpe  * Depending on the use case, one protection or the other is appropriate.  In
1612ff4bed7SJason Gunthorpe  * most cases references are being protected by the destroy_rwsem. This allows
1622ff4bed7SJason Gunthorpe  * orderly destruction of the object because iommufd_object_destroy_user() will
1632ff4bed7SJason Gunthorpe  * wait for it to become unlocked. However, as a rwsem, it cannot be held across
1642ff4bed7SJason Gunthorpe  * a system call return. So cases that have longer term needs must switch
1652ff4bed7SJason Gunthorpe  * to the weaker users refcount_t.
1662ff4bed7SJason Gunthorpe  *
1672ff4bed7SJason Gunthorpe  * With users protection iommufd_object_destroy_user() will return false,
1682ff4bed7SJason Gunthorpe  * refusing to destroy the object, causing -EBUSY to userspace.
1692ff4bed7SJason Gunthorpe  */
1702ff4bed7SJason Gunthorpe static inline void iommufd_ref_to_users(struct iommufd_object *obj)
1712ff4bed7SJason Gunthorpe {
1722ff4bed7SJason Gunthorpe 	up_read(&obj->destroy_rwsem);
1732ff4bed7SJason Gunthorpe 	/* iommufd_lock_obj() obtains users as well */
1742ff4bed7SJason Gunthorpe }
1752ff4bed7SJason Gunthorpe void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj);
1762ff4bed7SJason Gunthorpe void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx,
1772ff4bed7SJason Gunthorpe 				      struct iommufd_object *obj);
1782ff4bed7SJason Gunthorpe void iommufd_object_finalize(struct iommufd_ctx *ictx,
1792ff4bed7SJason Gunthorpe 			     struct iommufd_object *obj);
1802ff4bed7SJason Gunthorpe bool iommufd_object_destroy_user(struct iommufd_ctx *ictx,
1812ff4bed7SJason Gunthorpe 				 struct iommufd_object *obj);
1822ff4bed7SJason Gunthorpe struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
1832ff4bed7SJason Gunthorpe 					     size_t size,
1842ff4bed7SJason Gunthorpe 					     enum iommufd_object_type type);
1852ff4bed7SJason Gunthorpe 
1862ff4bed7SJason Gunthorpe #define iommufd_object_alloc(ictx, ptr, type)                                  \
1872ff4bed7SJason Gunthorpe 	container_of(_iommufd_object_alloc(                                    \
1882ff4bed7SJason Gunthorpe 			     ictx,                                             \
1892ff4bed7SJason Gunthorpe 			     sizeof(*(ptr)) + BUILD_BUG_ON_ZERO(               \
1902ff4bed7SJason Gunthorpe 						      offsetof(typeof(*(ptr)), \
1912ff4bed7SJason Gunthorpe 							       obj) != 0),     \
1922ff4bed7SJason Gunthorpe 			     type),                                            \
1932ff4bed7SJason Gunthorpe 		     typeof(*(ptr)), obj)
1942ff4bed7SJason Gunthorpe 
195aad37e71SJason Gunthorpe /*
196aad37e71SJason Gunthorpe  * The IO Address Space (IOAS) pagetable is a virtual page table backed by the
197aad37e71SJason Gunthorpe  * io_pagetable object. It is a user controlled mapping of IOVA -> PFNs. The
198aad37e71SJason Gunthorpe  * mapping is copied into all of the associated domains and made available to
199aad37e71SJason Gunthorpe  * in-kernel users.
200ea4acfacSJason Gunthorpe  *
201ea4acfacSJason Gunthorpe  * Every iommu_domain that is created is wrapped in a iommufd_hw_pagetable
202ea4acfacSJason Gunthorpe  * object. When we go to attach a device to an IOAS we need to get an
203ea4acfacSJason Gunthorpe  * iommu_domain and wrapping iommufd_hw_pagetable for it.
204ea4acfacSJason Gunthorpe  *
205ea4acfacSJason Gunthorpe  * An iommu_domain & iommfd_hw_pagetable will be automatically selected
206ea4acfacSJason Gunthorpe  * for a device based on the hwpt_list. If no suitable iommu_domain
207ea4acfacSJason Gunthorpe  * is found a new iommu_domain will be created.
208aad37e71SJason Gunthorpe  */
209aad37e71SJason Gunthorpe struct iommufd_ioas {
210aad37e71SJason Gunthorpe 	struct iommufd_object obj;
211aad37e71SJason Gunthorpe 	struct io_pagetable iopt;
212ea4acfacSJason Gunthorpe 	struct mutex mutex;
213ea4acfacSJason Gunthorpe 	struct list_head hwpt_list;
214aad37e71SJason Gunthorpe };
215aad37e71SJason Gunthorpe 
216325de950SYi Liu static inline struct iommufd_ioas *iommufd_get_ioas(struct iommufd_ctx *ictx,
217aad37e71SJason Gunthorpe 						    u32 id)
218aad37e71SJason Gunthorpe {
219325de950SYi Liu 	return container_of(iommufd_get_object(ictx, id,
220aad37e71SJason Gunthorpe 					       IOMMUFD_OBJ_IOAS),
221aad37e71SJason Gunthorpe 			    struct iommufd_ioas, obj);
222aad37e71SJason Gunthorpe }
223aad37e71SJason Gunthorpe 
224aad37e71SJason Gunthorpe struct iommufd_ioas *iommufd_ioas_alloc(struct iommufd_ctx *ictx);
225aad37e71SJason Gunthorpe int iommufd_ioas_alloc_ioctl(struct iommufd_ucmd *ucmd);
226aad37e71SJason Gunthorpe void iommufd_ioas_destroy(struct iommufd_object *obj);
227aad37e71SJason Gunthorpe int iommufd_ioas_iova_ranges(struct iommufd_ucmd *ucmd);
228aad37e71SJason Gunthorpe int iommufd_ioas_allow_iovas(struct iommufd_ucmd *ucmd);
229aad37e71SJason Gunthorpe int iommufd_ioas_map(struct iommufd_ucmd *ucmd);
230aad37e71SJason Gunthorpe int iommufd_ioas_copy(struct iommufd_ucmd *ucmd);
231aad37e71SJason Gunthorpe int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd);
232aad37e71SJason Gunthorpe int iommufd_ioas_option(struct iommufd_ucmd *ucmd);
233aad37e71SJason Gunthorpe int iommufd_option_rlimit_mode(struct iommu_option *cmd,
234aad37e71SJason Gunthorpe 			       struct iommufd_ctx *ictx);
235aad37e71SJason Gunthorpe 
236d624d665SJason Gunthorpe int iommufd_vfio_ioas(struct iommufd_ucmd *ucmd);
237d624d665SJason Gunthorpe 
238ea4acfacSJason Gunthorpe /*
239ea4acfacSJason Gunthorpe  * A HW pagetable is called an iommu_domain inside the kernel. This user object
240ea4acfacSJason Gunthorpe  * allows directly creating and inspecting the domains. Domains that have kernel
241ea4acfacSJason Gunthorpe  * owned page tables will be associated with an iommufd_ioas that provides the
242ea4acfacSJason Gunthorpe  * IOVA to PFN map.
243ea4acfacSJason Gunthorpe  */
244ea4acfacSJason Gunthorpe struct iommufd_hw_pagetable {
245ea4acfacSJason Gunthorpe 	struct iommufd_object obj;
246ea4acfacSJason Gunthorpe 	struct iommufd_ioas *ioas;
247ea4acfacSJason Gunthorpe 	struct iommu_domain *domain;
248ea4acfacSJason Gunthorpe 	bool auto_domain : 1;
249e8d57210SJason Gunthorpe 	bool enforce_cache_coherency : 1;
250e8d57210SJason Gunthorpe 	bool msi_cookie : 1;
251ea4acfacSJason Gunthorpe 	/* Head at iommufd_ioas::hwpt_list */
252ea4acfacSJason Gunthorpe 	struct list_head hwpt_item;
253ea4acfacSJason Gunthorpe };
254ea4acfacSJason Gunthorpe 
255ea4acfacSJason Gunthorpe struct iommufd_hw_pagetable *
256ea4acfacSJason Gunthorpe iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
257339fbf3aSJason Gunthorpe 			   struct iommufd_device *idev, bool immediate_attach);
25817bad527SJason Gunthorpe int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt);
259339fbf3aSJason Gunthorpe int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
260339fbf3aSJason Gunthorpe 				struct iommufd_device *idev);
26191a2e17eSJason Gunthorpe struct iommufd_hw_pagetable *
26291a2e17eSJason Gunthorpe iommufd_hw_pagetable_detach(struct iommufd_device *idev);
263ea4acfacSJason Gunthorpe void iommufd_hw_pagetable_destroy(struct iommufd_object *obj);
26470eadc7fSJason Gunthorpe void iommufd_hw_pagetable_abort(struct iommufd_object *obj);
2657074d7bdSJason Gunthorpe int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd);
266ea4acfacSJason Gunthorpe 
267d03f1336SJason Gunthorpe static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx,
268d03f1336SJason Gunthorpe 					    struct iommufd_hw_pagetable *hwpt)
269d03f1336SJason Gunthorpe {
270d03f1336SJason Gunthorpe 	lockdep_assert_not_held(&hwpt->ioas->mutex);
271d03f1336SJason Gunthorpe 	if (hwpt->auto_domain)
272d03f1336SJason Gunthorpe 		iommufd_object_destroy_user(ictx, &hwpt->obj);
273d03f1336SJason Gunthorpe 	else
274d03f1336SJason Gunthorpe 		refcount_dec(&hwpt->obj.users);
275d03f1336SJason Gunthorpe }
276d03f1336SJason Gunthorpe 
2773a3329a7SJason Gunthorpe struct iommufd_group {
2783a3329a7SJason Gunthorpe 	struct kref ref;
27991a2e17eSJason Gunthorpe 	struct mutex lock;
2803a3329a7SJason Gunthorpe 	struct iommufd_ctx *ictx;
2813a3329a7SJason Gunthorpe 	struct iommu_group *group;
28291a2e17eSJason Gunthorpe 	struct iommufd_hw_pagetable *hwpt;
28391a2e17eSJason Gunthorpe 	struct list_head device_list;
2841d149ab2SJason Gunthorpe 	phys_addr_t sw_msi_start;
2853a3329a7SJason Gunthorpe };
2863a3329a7SJason Gunthorpe 
2877e7ec8a5SJason Gunthorpe /*
2887e7ec8a5SJason Gunthorpe  * A iommufd_device object represents the binding relationship between a
2897e7ec8a5SJason Gunthorpe  * consuming driver and the iommufd. These objects are created/destroyed by
2907e7ec8a5SJason Gunthorpe  * external drivers, not by userspace.
2917e7ec8a5SJason Gunthorpe  */
2927e7ec8a5SJason Gunthorpe struct iommufd_device {
2937e7ec8a5SJason Gunthorpe 	struct iommufd_object obj;
2947e7ec8a5SJason Gunthorpe 	struct iommufd_ctx *ictx;
2953a3329a7SJason Gunthorpe 	struct iommufd_group *igroup;
29691a2e17eSJason Gunthorpe 	struct list_head group_item;
2977e7ec8a5SJason Gunthorpe 	/* always the physical device */
2987e7ec8a5SJason Gunthorpe 	struct device *dev;
2997e7ec8a5SJason Gunthorpe 	bool enforce_cache_coherency;
3007e7ec8a5SJason Gunthorpe };
3017e7ec8a5SJason Gunthorpe 
3027074d7bdSJason Gunthorpe static inline struct iommufd_device *
3037074d7bdSJason Gunthorpe iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id)
3047074d7bdSJason Gunthorpe {
3057074d7bdSJason Gunthorpe 	return container_of(iommufd_get_object(ucmd->ictx, id,
3067074d7bdSJason Gunthorpe 					       IOMMUFD_OBJ_DEVICE),
3077074d7bdSJason Gunthorpe 			    struct iommufd_device, obj);
3087074d7bdSJason Gunthorpe }
3097074d7bdSJason Gunthorpe 
310e8d57210SJason Gunthorpe void iommufd_device_destroy(struct iommufd_object *obj);
311e8d57210SJason Gunthorpe 
31251fe6141SJason Gunthorpe struct iommufd_access {
3138d40205fSJason Gunthorpe 	struct iommufd_object obj;
3148d40205fSJason Gunthorpe 	struct iommufd_ctx *ictx;
3158d40205fSJason Gunthorpe 	struct iommufd_ioas *ioas;
316e23a6217SNicolin Chen 	struct iommufd_ioas *ioas_unpin;
317e23a6217SNicolin Chen 	struct mutex ioas_lock;
3188d40205fSJason Gunthorpe 	const struct iommufd_access_ops *ops;
3198d40205fSJason Gunthorpe 	void *data;
32051fe6141SJason Gunthorpe 	unsigned long iova_alignment;
32151fe6141SJason Gunthorpe 	u32 iopt_access_list_id;
32251fe6141SJason Gunthorpe };
32351fe6141SJason Gunthorpe 
32451fe6141SJason Gunthorpe int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access);
32551fe6141SJason Gunthorpe void iopt_remove_access(struct io_pagetable *iopt,
326*5d5c85ffSNicolin Chen 			struct iommufd_access *access,
327*5d5c85ffSNicolin Chen 			u32 iopt_access_list_id);
3288d40205fSJason Gunthorpe void iommufd_access_destroy_object(struct iommufd_object *obj);
3298d40205fSJason Gunthorpe 
330f4b20bb3SJason Gunthorpe #ifdef CONFIG_IOMMUFD_TEST
331f4b20bb3SJason Gunthorpe int iommufd_test(struct iommufd_ucmd *ucmd);
332f4b20bb3SJason Gunthorpe void iommufd_selftest_destroy(struct iommufd_object *obj);
333f4b20bb3SJason Gunthorpe extern size_t iommufd_test_memory_limit;
334f4b20bb3SJason Gunthorpe void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
335f4b20bb3SJason Gunthorpe 				   unsigned int ioas_id, u64 *iova, u32 *flags);
336f4b20bb3SJason Gunthorpe bool iommufd_should_fail(void);
337f4b20bb3SJason Gunthorpe void __init iommufd_test_init(void);
338f4b20bb3SJason Gunthorpe void iommufd_test_exit(void);
33965c619aeSJason Gunthorpe bool iommufd_selftest_is_mock_dev(struct device *dev);
340f4b20bb3SJason Gunthorpe #else
341f4b20bb3SJason Gunthorpe static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
342f4b20bb3SJason Gunthorpe 						 unsigned int ioas_id,
343f4b20bb3SJason Gunthorpe 						 u64 *iova, u32 *flags)
344f4b20bb3SJason Gunthorpe {
345f4b20bb3SJason Gunthorpe }
346f4b20bb3SJason Gunthorpe static inline bool iommufd_should_fail(void)
347f4b20bb3SJason Gunthorpe {
348f4b20bb3SJason Gunthorpe 	return false;
349f4b20bb3SJason Gunthorpe }
350f4b20bb3SJason Gunthorpe static inline void __init iommufd_test_init(void)
351f4b20bb3SJason Gunthorpe {
352f4b20bb3SJason Gunthorpe }
353f4b20bb3SJason Gunthorpe static inline void iommufd_test_exit(void)
354f4b20bb3SJason Gunthorpe {
355f4b20bb3SJason Gunthorpe }
35665c619aeSJason Gunthorpe static inline bool iommufd_selftest_is_mock_dev(struct device *dev)
35765c619aeSJason Gunthorpe {
35865c619aeSJason Gunthorpe 	return false;
35965c619aeSJason Gunthorpe }
360f4b20bb3SJason Gunthorpe #endif
3612ff4bed7SJason Gunthorpe #endif
362