xref: /linux-6.15/include/linux/vfio.h (revision 29064134)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2cba3345cSAlex Williamson /*
3cba3345cSAlex Williamson  * VFIO API definition
4cba3345cSAlex Williamson  *
5cba3345cSAlex Williamson  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
6cba3345cSAlex Williamson  *     Author: Alex Williamson <[email protected]>
7cba3345cSAlex Williamson  */
8cba3345cSAlex Williamson #ifndef VFIO_H
9cba3345cSAlex Williamson #define VFIO_H
10cba3345cSAlex Williamson 
11cba3345cSAlex Williamson 
12cba3345cSAlex Williamson #include <linux/iommu.h>
13cba3345cSAlex Williamson #include <linux/mm.h>
147e992d69SAntonios Motakis #include <linux/workqueue.h>
157e992d69SAntonios Motakis #include <linux/poll.h>
168b6f173aSYi Liu #include <linux/cdev.h>
17607ca46eSDavid Howells #include <uapi/linux/vfio.h>
1880c4b92aSYishai Hadas #include <linux/iova_bitmap.h>
19cba3345cSAlex Williamson 
20ba70a89fSJason Gunthorpe struct kvm;
21a4d1f91dSJason Gunthorpe struct iommufd_ctx;
22a4d1f91dSJason Gunthorpe struct iommufd_device;
234741f2e9SJason Gunthorpe struct iommufd_access;
24ba70a89fSJason Gunthorpe 
252fd585f4SJason Gunthorpe /*
262fd585f4SJason Gunthorpe  * VFIO devices can be placed in a set, this allows all devices to share this
272fd585f4SJason Gunthorpe  * structure and the VFIO core will provide a lock that is held around
282fd585f4SJason Gunthorpe  * open_device()/close_device() for all devices in the set.
292fd585f4SJason Gunthorpe  */
302fd585f4SJason Gunthorpe struct vfio_device_set {
312fd585f4SJason Gunthorpe 	void *set_id;
322fd585f4SJason Gunthorpe 	struct mutex lock;
332fd585f4SJason Gunthorpe 	struct list_head device_list;
342fd585f4SJason Gunthorpe 	unsigned int device_count;
352fd585f4SJason Gunthorpe };
362fd585f4SJason Gunthorpe 
370bfc6a4eSJason Gunthorpe struct vfio_device {
380bfc6a4eSJason Gunthorpe 	struct device *dev;
390bfc6a4eSJason Gunthorpe 	const struct vfio_device_ops *ops;
406e97eba8SYishai Hadas 	/*
4180c4b92aSYishai Hadas 	 * mig_ops/log_ops is a static property of the vfio_device which must
4280c4b92aSYishai Hadas 	 * be set prior to registering the vfio_device.
436e97eba8SYishai Hadas 	 */
446e97eba8SYishai Hadas 	const struct vfio_migration_ops *mig_ops;
4580c4b92aSYishai Hadas 	const struct vfio_log_ops *log_ops;
46c1cce6d0SYi Liu #if IS_ENABLED(CONFIG_VFIO_GROUP)
470bfc6a4eSJason Gunthorpe 	struct vfio_group *group;
48c1cce6d0SYi Liu 	struct list_head group_next;
49c1cce6d0SYi Liu 	struct list_head iommu_entry;
50c1cce6d0SYi Liu #endif
512fd585f4SJason Gunthorpe 	struct vfio_device_set *dev_set;
522fd585f4SJason Gunthorpe 	struct list_head dev_set_list;
538cb3d83bSJason Gunthorpe 	unsigned int migration_flags;
54421cfe65SMatthew Rosato 	struct kvm *kvm;
550bfc6a4eSJason Gunthorpe 
560bfc6a4eSJason Gunthorpe 	/* Members below here are private, not for driver use */
573c28a761SYi Liu 	unsigned int index;
583c28a761SYi Liu 	struct device device;	/* device.kref covers object life circle */
598b6f173aSYi Liu #if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
608b6f173aSYi Liu 	struct cdev cdev;
618b6f173aSYi Liu #endif
62cb9ff3f3SKevin Tian 	refcount_t refcount;	/* user count on registered device*/
632fd585f4SJason Gunthorpe 	unsigned int open_count;
640bfc6a4eSJason Gunthorpe 	struct completion comp;
654741f2e9SJason Gunthorpe 	struct iommufd_access *iommufd_access;
662b48f52fSMatthew Rosato 	void (*put_kvm)(struct kvm *kvm);
67b7c5e64fSAlex Williamson 	struct inode *inode;
68a4d1f91dSJason Gunthorpe #if IS_ENABLED(CONFIG_IOMMUFD)
69a4d1f91dSJason Gunthorpe 	struct iommufd_device *iommufd_device;
70*29064134SYi Liu 	struct ida pasids;
715fcc2696SYi Liu 	u8 iommufd_attached:1;
72a4d1f91dSJason Gunthorpe #endif
735fcc2696SYi Liu 	u8 cdev_opened:1;
742202844eSLongfang Liu #ifdef CONFIG_DEBUG_FS
752202844eSLongfang Liu 	/*
762202844eSLongfang Liu 	 * debug_root is a static property of the vfio_device
772202844eSLongfang Liu 	 * which must be set prior to registering the vfio_device.
782202844eSLongfang Liu 	 */
792202844eSLongfang Liu 	struct dentry *debug_root;
802202844eSLongfang Liu #endif
810bfc6a4eSJason Gunthorpe };
820bfc6a4eSJason Gunthorpe 
83cba3345cSAlex Williamson /**
84cba3345cSAlex Williamson  * struct vfio_device_ops - VFIO bus driver device callbacks
85cba3345cSAlex Williamson  *
8638e4614cSSimon Horman  * @name: Name of the device driver.
87cb9ff3f3SKevin Tian  * @init: initialize private fields in device structure
88cb9ff3f3SKevin Tian  * @release: Reclaim private fields in device structure
89fae90680SYi Liu  * @bind_iommufd: Called when binding the device to an iommufd
90fae90680SYi Liu  * @unbind_iommufd: Opposite of bind_iommufd
91fae90680SYi Liu  * @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the
929048c734SYi Liu  *		 bound iommufd. Undo in unbind_iommufd if @detach_ioas is not
939048c734SYi Liu  *		 called.
949048c734SYi Liu  * @detach_ioas: Opposite of attach_ioas
95*29064134SYi Liu  * @pasid_attach_ioas: The pasid variation of attach_ioas
96*29064134SYi Liu  * @pasid_detach_ioas: Opposite of pasid_attach_ioas
972fd585f4SJason Gunthorpe  * @open_device: Called when the first file descriptor is opened for this device
982fd585f4SJason Gunthorpe  * @close_device: Opposite of open_device
99cba3345cSAlex Williamson  * @read: Perform read(2) on device file descriptor
100cba3345cSAlex Williamson  * @write: Perform write(2) on device file descriptor
101cba3345cSAlex Williamson  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
102cba3345cSAlex Williamson  *         operations documented below
103cba3345cSAlex Williamson  * @mmap: Perform mmap(2) on a region of the device file descriptor
10413060b64SAlex Williamson  * @request: Request for the bus driver to release the device
1055f3874c2SAlex Williamson  * @match: Optional device name match callback (return: 0 for no-match, >0 for
1065f3874c2SAlex Williamson  *         match, -errno for abort (ex. match with insufficient or incorrect
1075f3874c2SAlex Williamson  *         additional args)
108ce4b4657SJason Gunthorpe  * @dma_unmap: Called when userspace unmaps IOVA from the container
109ce4b4657SJason Gunthorpe  *             this device is attached to.
110445ad495SJason Gunthorpe  * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
111cba3345cSAlex Williamson  */
112cba3345cSAlex Williamson struct vfio_device_ops {
113cba3345cSAlex Williamson 	char	*name;
114cb9ff3f3SKevin Tian 	int	(*init)(struct vfio_device *vdev);
115cb9ff3f3SKevin Tian 	void	(*release)(struct vfio_device *vdev);
116a4d1f91dSJason Gunthorpe 	int	(*bind_iommufd)(struct vfio_device *vdev,
117a4d1f91dSJason Gunthorpe 				struct iommufd_ctx *ictx, u32 *out_device_id);
118a4d1f91dSJason Gunthorpe 	void	(*unbind_iommufd)(struct vfio_device *vdev);
119a4d1f91dSJason Gunthorpe 	int	(*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
1209048c734SYi Liu 	void	(*detach_ioas)(struct vfio_device *vdev);
121*29064134SYi Liu 	int	(*pasid_attach_ioas)(struct vfio_device *vdev, u32 pasid,
122*29064134SYi Liu 				     u32 *pt_id);
123*29064134SYi Liu 	void	(*pasid_detach_ioas)(struct vfio_device *vdev, u32 pasid);
1242fd585f4SJason Gunthorpe 	int	(*open_device)(struct vfio_device *vdev);
1252fd585f4SJason Gunthorpe 	void	(*close_device)(struct vfio_device *vdev);
1266df62c5bSJason Gunthorpe 	ssize_t	(*read)(struct vfio_device *vdev, char __user *buf,
127cba3345cSAlex Williamson 			size_t count, loff_t *ppos);
1286df62c5bSJason Gunthorpe 	ssize_t	(*write)(struct vfio_device *vdev, const char __user *buf,
129cba3345cSAlex Williamson 			 size_t count, loff_t *size);
1306df62c5bSJason Gunthorpe 	long	(*ioctl)(struct vfio_device *vdev, unsigned int cmd,
131cba3345cSAlex Williamson 			 unsigned long arg);
1326df62c5bSJason Gunthorpe 	int	(*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
1336df62c5bSJason Gunthorpe 	void	(*request)(struct vfio_device *vdev, unsigned int count);
1346df62c5bSJason Gunthorpe 	int	(*match)(struct vfio_device *vdev, char *buf);
135ce4b4657SJason Gunthorpe 	void	(*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
136445ad495SJason Gunthorpe 	int	(*device_feature)(struct vfio_device *device, u32 flags,
137445ad495SJason Gunthorpe 				  void __user *arg, size_t argsz);
1386e97eba8SYishai Hadas };
1396e97eba8SYishai Hadas 
140a4d1f91dSJason Gunthorpe #if IS_ENABLED(CONFIG_IOMMUFD)
1419062ff40SYi Liu struct iommufd_ctx *vfio_iommufd_device_ictx(struct vfio_device *vdev);
1429062ff40SYi Liu int vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx);
143a4d1f91dSJason Gunthorpe int vfio_iommufd_physical_bind(struct vfio_device *vdev,
144a4d1f91dSJason Gunthorpe 			       struct iommufd_ctx *ictx, u32 *out_device_id);
145a4d1f91dSJason Gunthorpe void vfio_iommufd_physical_unbind(struct vfio_device *vdev);
146a4d1f91dSJason Gunthorpe int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
1479048c734SYi Liu void vfio_iommufd_physical_detach_ioas(struct vfio_device *vdev);
148*29064134SYi Liu int vfio_iommufd_physical_pasid_attach_ioas(struct vfio_device *vdev,
149*29064134SYi Liu 					    u32 pasid, u32 *pt_id);
150*29064134SYi Liu void vfio_iommufd_physical_pasid_detach_ioas(struct vfio_device *vdev,
151*29064134SYi Liu 					     u32 pasid);
1524741f2e9SJason Gunthorpe int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
1534741f2e9SJason Gunthorpe 			       struct iommufd_ctx *ictx, u32 *out_device_id);
1544741f2e9SJason Gunthorpe void vfio_iommufd_emulated_unbind(struct vfio_device *vdev);
1554741f2e9SJason Gunthorpe int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
1568cfa7186SYi Liu void vfio_iommufd_emulated_detach_ioas(struct vfio_device *vdev);
157a4d1f91dSJason Gunthorpe #else
1589062ff40SYi Liu static inline struct iommufd_ctx *
vfio_iommufd_device_ictx(struct vfio_device * vdev)1599062ff40SYi Liu vfio_iommufd_device_ictx(struct vfio_device *vdev)
1609062ff40SYi Liu {
1619062ff40SYi Liu 	return NULL;
1629062ff40SYi Liu }
1639062ff40SYi Liu 
1649062ff40SYi Liu static inline int
vfio_iommufd_get_dev_id(struct vfio_device * vdev,struct iommufd_ctx * ictx)1659062ff40SYi Liu vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx)
1669062ff40SYi Liu {
1679062ff40SYi Liu 	return VFIO_PCI_DEVID_NOT_OWNED;
1689062ff40SYi Liu }
1699062ff40SYi Liu 
170a4d1f91dSJason Gunthorpe #define vfio_iommufd_physical_bind                                      \
171a4d1f91dSJason Gunthorpe 	((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx,   \
172a4d1f91dSJason Gunthorpe 		  u32 *out_device_id)) NULL)
173a4d1f91dSJason Gunthorpe #define vfio_iommufd_physical_unbind \
174a4d1f91dSJason Gunthorpe 	((void (*)(struct vfio_device *vdev)) NULL)
175a4d1f91dSJason Gunthorpe #define vfio_iommufd_physical_attach_ioas \
176a4d1f91dSJason Gunthorpe 	((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
1779048c734SYi Liu #define vfio_iommufd_physical_detach_ioas \
1789048c734SYi Liu 	((void (*)(struct vfio_device *vdev)) NULL)
179*29064134SYi Liu #define vfio_iommufd_physical_pasid_attach_ioas \
180*29064134SYi Liu 	((int (*)(struct vfio_device *vdev, u32 pasid, u32 *pt_id)) NULL)
181*29064134SYi Liu #define vfio_iommufd_physical_pasid_detach_ioas \
182*29064134SYi Liu 	((void (*)(struct vfio_device *vdev, u32 pasid)) NULL)
1834741f2e9SJason Gunthorpe #define vfio_iommufd_emulated_bind                                      \
1844741f2e9SJason Gunthorpe 	((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx,   \
1854741f2e9SJason Gunthorpe 		  u32 *out_device_id)) NULL)
1864741f2e9SJason Gunthorpe #define vfio_iommufd_emulated_unbind \
1874741f2e9SJason Gunthorpe 	((void (*)(struct vfio_device *vdev)) NULL)
1884741f2e9SJason Gunthorpe #define vfio_iommufd_emulated_attach_ioas \
1894741f2e9SJason Gunthorpe 	((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
1908cfa7186SYi Liu #define vfio_iommufd_emulated_detach_ioas \
1918cfa7186SYi Liu 	((void (*)(struct vfio_device *vdev)) NULL)
192a4d1f91dSJason Gunthorpe #endif
193a4d1f91dSJason Gunthorpe 
vfio_device_cdev_opened(struct vfio_device * device)194af949759SYi Liu static inline bool vfio_device_cdev_opened(struct vfio_device *device)
195af949759SYi Liu {
1965fcc2696SYi Liu 	return device->cdev_opened;
197af949759SYi Liu }
198af949759SYi Liu 
1996e97eba8SYishai Hadas /**
20038e4614cSSimon Horman  * struct vfio_migration_ops - VFIO bus device driver migration callbacks
20138e4614cSSimon Horman  *
2026e97eba8SYishai Hadas  * @migration_set_state: Optional callback to change the migration state for
2036e97eba8SYishai Hadas  *         devices that support migration. It's mandatory for
2046e97eba8SYishai Hadas  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
2056e97eba8SYishai Hadas  *         The returned FD is used for data transfer according to the FSM
2066e97eba8SYishai Hadas  *         definition. The driver is responsible to ensure that FD reaches end
2076e97eba8SYishai Hadas  *         of stream or error whenever the migration FSM leaves a data transfer
2086e97eba8SYishai Hadas  *         state or before close_device() returns.
2096e97eba8SYishai Hadas  * @migration_get_state: Optional callback to get the migration state for
2106e97eba8SYishai Hadas  *         devices that support migration. It's mandatory for
2116e97eba8SYishai Hadas  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
2124e016f96SYishai Hadas  * @migration_get_data_size: Optional callback to get the estimated data
2134e016f96SYishai Hadas  *          length that will be required to complete stop copy. It's mandatory for
2144e016f96SYishai Hadas  *          VFIO_DEVICE_FEATURE_MIGRATION migration support.
2156e97eba8SYishai Hadas  */
2166e97eba8SYishai Hadas struct vfio_migration_ops {
217115dcec6SJason Gunthorpe 	struct file *(*migration_set_state)(
218115dcec6SJason Gunthorpe 		struct vfio_device *device,
219115dcec6SJason Gunthorpe 		enum vfio_device_mig_state new_state);
220115dcec6SJason Gunthorpe 	int (*migration_get_state)(struct vfio_device *device,
221115dcec6SJason Gunthorpe 				   enum vfio_device_mig_state *curr_state);
2224e016f96SYishai Hadas 	int (*migration_get_data_size)(struct vfio_device *device,
2234e016f96SYishai Hadas 				       unsigned long *stop_copy_length);
224cba3345cSAlex Williamson };
225cba3345cSAlex Williamson 
226445ad495SJason Gunthorpe /**
22738e4614cSSimon Horman  * struct vfio_log_ops - VFIO bus device driver logging callbacks
22838e4614cSSimon Horman  *
22980c4b92aSYishai Hadas  * @log_start: Optional callback to ask the device start DMA logging.
23080c4b92aSYishai Hadas  * @log_stop: Optional callback to ask the device stop DMA logging.
23180c4b92aSYishai Hadas  * @log_read_and_clear: Optional callback to ask the device read
23280c4b92aSYishai Hadas  *         and clear the dirty DMAs in some given range.
23380c4b92aSYishai Hadas  *
23480c4b92aSYishai Hadas  * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set
23580c4b92aSYishai Hadas  * of features does not track logging state relative to the device,
23680c4b92aSYishai Hadas  * therefore the device implementation of vfio_log_ops must handle
23780c4b92aSYishai Hadas  * arbitrary user requests. This includes rejecting subsequent calls
23880c4b92aSYishai Hadas  * to log_start without an intervening log_stop, as well as graceful
23980c4b92aSYishai Hadas  * handling of log_stop and log_read_and_clear from invalid states.
24080c4b92aSYishai Hadas  */
24180c4b92aSYishai Hadas struct vfio_log_ops {
24280c4b92aSYishai Hadas 	int (*log_start)(struct vfio_device *device,
24380c4b92aSYishai Hadas 		struct rb_root_cached *ranges, u32 nnodes, u64 *page_size);
24480c4b92aSYishai Hadas 	int (*log_stop)(struct vfio_device *device);
24580c4b92aSYishai Hadas 	int (*log_read_and_clear)(struct vfio_device *device,
24680c4b92aSYishai Hadas 		unsigned long iova, unsigned long length,
24780c4b92aSYishai Hadas 		struct iova_bitmap *dirty);
24880c4b92aSYishai Hadas };
24980c4b92aSYishai Hadas 
25080c4b92aSYishai Hadas /**
251445ad495SJason Gunthorpe  * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
252445ad495SJason Gunthorpe  * @flags: Arg from the device_feature op
253445ad495SJason Gunthorpe  * @argsz: Arg from the device_feature op
254445ad495SJason Gunthorpe  * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
255445ad495SJason Gunthorpe  *                 supports
256445ad495SJason Gunthorpe  * @minsz: Minimum data size the driver accepts
257445ad495SJason Gunthorpe  *
258445ad495SJason Gunthorpe  * For use in a driver's device_feature op. Checks that the inputs to the
259445ad495SJason Gunthorpe  * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
260445ad495SJason Gunthorpe  * the driver should execute the get or set, otherwise the relevant
261445ad495SJason Gunthorpe  * value should be returned.
262445ad495SJason Gunthorpe  */
vfio_check_feature(u32 flags,size_t argsz,u32 supported_ops,size_t minsz)263445ad495SJason Gunthorpe static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
264445ad495SJason Gunthorpe 				    size_t minsz)
265445ad495SJason Gunthorpe {
266445ad495SJason Gunthorpe 	if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
267445ad495SJason Gunthorpe 	    ~supported_ops)
268445ad495SJason Gunthorpe 		return -EINVAL;
269445ad495SJason Gunthorpe 	if (flags & VFIO_DEVICE_FEATURE_PROBE)
270445ad495SJason Gunthorpe 		return 0;
271445ad495SJason Gunthorpe 	/* Without PROBE one of GET or SET must be requested */
272445ad495SJason Gunthorpe 	if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
273445ad495SJason Gunthorpe 		return -EINVAL;
274445ad495SJason Gunthorpe 	if (argsz < minsz)
275445ad495SJason Gunthorpe 		return -EINVAL;
276445ad495SJason Gunthorpe 	return 1;
277445ad495SJason Gunthorpe }
278445ad495SJason Gunthorpe 
279cb9ff3f3SKevin Tian struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev,
280cb9ff3f3SKevin Tian 				       const struct vfio_device_ops *ops);
281cb9ff3f3SKevin Tian #define vfio_alloc_device(dev_struct, member, dev, ops)				\
282cb9ff3f3SKevin Tian 	container_of(_vfio_alloc_device(sizeof(struct dev_struct) +		\
283cb9ff3f3SKevin Tian 					BUILD_BUG_ON_ZERO(offsetof(		\
284cb9ff3f3SKevin Tian 						struct dev_struct, member)),	\
285cb9ff3f3SKevin Tian 					dev, ops),				\
286cb9ff3f3SKevin Tian 		     struct dev_struct, member)
287cb9ff3f3SKevin Tian 
vfio_put_device(struct vfio_device * device)288cb9ff3f3SKevin Tian static inline void vfio_put_device(struct vfio_device *device)
289cb9ff3f3SKevin Tian {
2903c28a761SYi Liu 	put_device(&device->device);
291cb9ff3f3SKevin Tian }
292cb9ff3f3SKevin Tian 
2930bfc6a4eSJason Gunthorpe int vfio_register_group_dev(struct vfio_device *device);
294c68ea0d0SChristoph Hellwig int vfio_register_emulated_iommu_dev(struct vfio_device *device);
2950bfc6a4eSJason Gunthorpe void vfio_unregister_group_dev(struct vfio_device *device);
296cba3345cSAlex Williamson 
2972fd585f4SJason Gunthorpe int vfio_assign_device_set(struct vfio_device *device, void *set_id);
2985cd189e4SAnthony DeRossi unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set);
299a80e1de9SYi Liu struct vfio_device *
300a80e1de9SYi Liu vfio_find_device_in_devset(struct vfio_device_set *dev_set,
301a80e1de9SYi Liu 			   struct device *dev);
3022fd585f4SJason Gunthorpe 
303115dcec6SJason Gunthorpe int vfio_mig_get_next_state(struct vfio_device *device,
304115dcec6SJason Gunthorpe 			    enum vfio_device_mig_state cur_fsm,
305115dcec6SJason Gunthorpe 			    enum vfio_device_mig_state new_fsm,
306115dcec6SJason Gunthorpe 			    enum vfio_device_mig_state *next_fsm);
307115dcec6SJason Gunthorpe 
3089a4087faSBrett Creeley void vfio_combine_iova_ranges(struct rb_root_cached *root, u32 cur_nodes,
3099a4087faSBrett Creeley 			      u32 req_nodes);
3109a4087faSBrett Creeley 
3116cdd9782SAlexey Kardashevskiy /*
3126cdd9782SAlexey Kardashevskiy  * External user API
3136cdd9782SAlexey Kardashevskiy  */
314d1877e63SAlex Williamson struct iommu_group *vfio_file_iommu_group(struct file *file);
3154ea95c04SSean Christopherson 
3164ea95c04SSean Christopherson #if IS_ENABLED(CONFIG_VFIO_GROUP)
3174b22ef04SJason Gunthorpe bool vfio_file_is_group(struct file *file);
318c1cce6d0SYi Liu bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
319c1cce6d0SYi Liu #else
vfio_file_is_group(struct file * file)320c1cce6d0SYi Liu static inline bool vfio_file_is_group(struct file *file)
321c1cce6d0SYi Liu {
322c1cce6d0SYi Liu 	return false;
323c1cce6d0SYi Liu }
324c1cce6d0SYi Liu 
vfio_file_has_dev(struct file * file,struct vfio_device * device)325c1cce6d0SYi Liu static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
326c1cce6d0SYi Liu {
327c1cce6d0SYi Liu 	return false;
328c1cce6d0SYi Liu }
329c1cce6d0SYi Liu #endif
330b1a59be8SYi Liu bool vfio_file_is_valid(struct file *file);
331d1877e63SAlex Williamson bool vfio_file_enforced_coherent(struct file *file);
332d1877e63SAlex Williamson void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
3336cdd9782SAlexey Kardashevskiy 
3342169037dSKirti Wankhede #define VFIO_PIN_PAGES_MAX_ENTRIES	(PAGE_SIZE/sizeof(unsigned long))
3352169037dSKirti Wankhede 
33644abdd16SNicolin Chen int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
33734a255e6SNicolin Chen 		   int npage, int prot, struct page **pages);
33844abdd16SNicolin Chen void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
3398561aa4fSNicolin Chen int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
3408d46c0ccSYan Zhao 		void *data, size_t len, bool write);
3418d46c0ccSYan Zhao 
342d7a8d5edSAlex Williamson /*
343d7a8d5edSAlex Williamson  * Sub-module helpers
344d7a8d5edSAlex Williamson  */
345d7a8d5edSAlex Williamson struct vfio_info_cap {
346d7a8d5edSAlex Williamson 	struct vfio_info_cap_header *buf;
347d7a8d5edSAlex Williamson 	size_t size;
348d7a8d5edSAlex Williamson };
349d1877e63SAlex Williamson struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
350d1877e63SAlex Williamson 					       size_t size, u16 id,
351d1877e63SAlex Williamson 					       u16 version);
352d1877e63SAlex Williamson void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
353d7a8d5edSAlex Williamson 
354d1877e63SAlex Williamson int vfio_info_add_capability(struct vfio_info_cap *caps,
355d1877e63SAlex Williamson 			     struct vfio_info_cap_header *cap, size_t size);
356b3c0a866SKirti Wankhede 
357d1877e63SAlex Williamson int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
358c747f08aSKirti Wankhede 				       int num_irqs, int max_irq_type,
359c747f08aSKirti Wankhede 				       size_t *data_size);
360c747f08aSKirti Wankhede 
3617e992d69SAntonios Motakis /*
3627e992d69SAntonios Motakis  * IRQfd - generic
3637e992d69SAntonios Motakis  */
3647e992d69SAntonios Motakis struct virqfd {
3657e992d69SAntonios Motakis 	void			*opaque;
3667e992d69SAntonios Motakis 	struct eventfd_ctx	*eventfd;
3677e992d69SAntonios Motakis 	int			(*handler)(void *, void *);
3687e992d69SAntonios Motakis 	void			(*thread)(void *, void *);
3697e992d69SAntonios Motakis 	void			*data;
3707e992d69SAntonios Motakis 	struct work_struct	inject;
371ac6424b9SIngo Molnar 	wait_queue_entry_t		wait;
3727e992d69SAntonios Motakis 	poll_table		pt;
3737e992d69SAntonios Motakis 	struct work_struct	shutdown;
374b620ecbdSAlex Williamson 	struct work_struct	flush_inject;
3757e992d69SAntonios Motakis 	struct virqfd		**pvirqfd;
3767e992d69SAntonios Motakis };
3777e992d69SAntonios Motakis 
378d1877e63SAlex Williamson int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
379d1877e63SAlex Williamson 		       void (*thread)(void *, void *), void *data,
380d1877e63SAlex Williamson 		       struct virqfd **pvirqfd, int fd);
381d1877e63SAlex Williamson void vfio_virqfd_disable(struct virqfd **pvirqfd);
382b620ecbdSAlex Williamson void vfio_virqfd_flush_thread(struct virqfd **pvirqfd);
3837e992d69SAntonios Motakis 
384cba3345cSAlex Williamson #endif /* VFIO_H */
385