xref: /linux-6.15/include/linux/vfio.h (revision 9062ff40)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * VFIO API definition
4  *
5  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
6  *     Author: Alex Williamson <[email protected]>
7  */
8 #ifndef VFIO_H
9 #define VFIO_H
10 
11 
12 #include <linux/iommu.h>
13 #include <linux/mm.h>
14 #include <linux/workqueue.h>
15 #include <linux/poll.h>
16 #include <uapi/linux/vfio.h>
17 #include <linux/iova_bitmap.h>
18 
19 struct kvm;
20 struct iommufd_ctx;
21 struct iommufd_device;
22 struct iommufd_access;
23 
24 /*
25  * VFIO devices can be placed in a set, this allows all devices to share this
26  * structure and the VFIO core will provide a lock that is held around
27  * open_device()/close_device() for all devices in the set.
28  */
29 struct vfio_device_set {
30 	void *set_id;
31 	struct mutex lock;
32 	struct list_head device_list;
33 	unsigned int device_count;
34 };
35 
36 struct vfio_device {
37 	struct device *dev;
38 	const struct vfio_device_ops *ops;
39 	/*
40 	 * mig_ops/log_ops is a static property of the vfio_device which must
41 	 * be set prior to registering the vfio_device.
42 	 */
43 	const struct vfio_migration_ops *mig_ops;
44 	const struct vfio_log_ops *log_ops;
45 	struct vfio_group *group;
46 	struct vfio_device_set *dev_set;
47 	struct list_head dev_set_list;
48 	unsigned int migration_flags;
49 	struct kvm *kvm;
50 
51 	/* Members below here are private, not for driver use */
52 	unsigned int index;
53 	struct device device;	/* device.kref covers object life circle */
54 	refcount_t refcount;	/* user count on registered device*/
55 	unsigned int open_count;
56 	struct completion comp;
57 	struct list_head group_next;
58 	struct list_head iommu_entry;
59 	struct iommufd_access *iommufd_access;
60 	void (*put_kvm)(struct kvm *kvm);
61 #if IS_ENABLED(CONFIG_IOMMUFD)
62 	struct iommufd_device *iommufd_device;
63 	bool iommufd_attached;
64 #endif
65 };
66 
67 /**
68  * struct vfio_device_ops - VFIO bus driver device callbacks
69  *
70  * @name: Name of the device driver.
71  * @init: initialize private fields in device structure
72  * @release: Reclaim private fields in device structure
73  * @bind_iommufd: Called when binding the device to an iommufd
74  * @unbind_iommufd: Opposite of bind_iommufd
75  * @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the
76  *		 bound iommufd. Undo in unbind_iommufd.
77  * @open_device: Called when the first file descriptor is opened for this device
78  * @close_device: Opposite of open_device
79  * @read: Perform read(2) on device file descriptor
80  * @write: Perform write(2) on device file descriptor
81  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
82  *         operations documented below
83  * @mmap: Perform mmap(2) on a region of the device file descriptor
84  * @request: Request for the bus driver to release the device
85  * @match: Optional device name match callback (return: 0 for no-match, >0 for
86  *         match, -errno for abort (ex. match with insufficient or incorrect
87  *         additional args)
88  * @dma_unmap: Called when userspace unmaps IOVA from the container
89  *             this device is attached to.
90  * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
91  */
92 struct vfio_device_ops {
93 	char	*name;
94 	int	(*init)(struct vfio_device *vdev);
95 	void	(*release)(struct vfio_device *vdev);
96 	int	(*bind_iommufd)(struct vfio_device *vdev,
97 				struct iommufd_ctx *ictx, u32 *out_device_id);
98 	void	(*unbind_iommufd)(struct vfio_device *vdev);
99 	int	(*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
100 	int	(*open_device)(struct vfio_device *vdev);
101 	void	(*close_device)(struct vfio_device *vdev);
102 	ssize_t	(*read)(struct vfio_device *vdev, char __user *buf,
103 			size_t count, loff_t *ppos);
104 	ssize_t	(*write)(struct vfio_device *vdev, const char __user *buf,
105 			 size_t count, loff_t *size);
106 	long	(*ioctl)(struct vfio_device *vdev, unsigned int cmd,
107 			 unsigned long arg);
108 	int	(*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
109 	void	(*request)(struct vfio_device *vdev, unsigned int count);
110 	int	(*match)(struct vfio_device *vdev, char *buf);
111 	void	(*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
112 	int	(*device_feature)(struct vfio_device *device, u32 flags,
113 				  void __user *arg, size_t argsz);
114 };
115 
116 #if IS_ENABLED(CONFIG_IOMMUFD)
117 struct iommufd_ctx *vfio_iommufd_device_ictx(struct vfio_device *vdev);
118 int vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx);
119 int vfio_iommufd_physical_bind(struct vfio_device *vdev,
120 			       struct iommufd_ctx *ictx, u32 *out_device_id);
121 void vfio_iommufd_physical_unbind(struct vfio_device *vdev);
122 int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
123 int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
124 			       struct iommufd_ctx *ictx, u32 *out_device_id);
125 void vfio_iommufd_emulated_unbind(struct vfio_device *vdev);
126 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
127 #else
128 static inline struct iommufd_ctx *
129 vfio_iommufd_device_ictx(struct vfio_device *vdev)
130 {
131 	return NULL;
132 }
133 
134 static inline int
135 vfio_iommufd_get_dev_id(struct vfio_device *vdev, struct iommufd_ctx *ictx)
136 {
137 	return VFIO_PCI_DEVID_NOT_OWNED;
138 }
139 
140 #define vfio_iommufd_physical_bind                                      \
141 	((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx,   \
142 		  u32 *out_device_id)) NULL)
143 #define vfio_iommufd_physical_unbind \
144 	((void (*)(struct vfio_device *vdev)) NULL)
145 #define vfio_iommufd_physical_attach_ioas \
146 	((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
147 #define vfio_iommufd_emulated_bind                                      \
148 	((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx,   \
149 		  u32 *out_device_id)) NULL)
150 #define vfio_iommufd_emulated_unbind \
151 	((void (*)(struct vfio_device *vdev)) NULL)
152 #define vfio_iommufd_emulated_attach_ioas \
153 	((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
154 #endif
155 
156 static inline bool vfio_device_cdev_opened(struct vfio_device *device)
157 {
158 	return false;
159 }
160 
161 /**
162  * struct vfio_migration_ops - VFIO bus device driver migration callbacks
163  *
164  * @migration_set_state: Optional callback to change the migration state for
165  *         devices that support migration. It's mandatory for
166  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
167  *         The returned FD is used for data transfer according to the FSM
168  *         definition. The driver is responsible to ensure that FD reaches end
169  *         of stream or error whenever the migration FSM leaves a data transfer
170  *         state or before close_device() returns.
171  * @migration_get_state: Optional callback to get the migration state for
172  *         devices that support migration. It's mandatory for
173  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
174  * @migration_get_data_size: Optional callback to get the estimated data
175  *          length that will be required to complete stop copy. It's mandatory for
176  *          VFIO_DEVICE_FEATURE_MIGRATION migration support.
177  */
178 struct vfio_migration_ops {
179 	struct file *(*migration_set_state)(
180 		struct vfio_device *device,
181 		enum vfio_device_mig_state new_state);
182 	int (*migration_get_state)(struct vfio_device *device,
183 				   enum vfio_device_mig_state *curr_state);
184 	int (*migration_get_data_size)(struct vfio_device *device,
185 				       unsigned long *stop_copy_length);
186 };
187 
188 /**
189  * struct vfio_log_ops - VFIO bus device driver logging callbacks
190  *
191  * @log_start: Optional callback to ask the device start DMA logging.
192  * @log_stop: Optional callback to ask the device stop DMA logging.
193  * @log_read_and_clear: Optional callback to ask the device read
194  *         and clear the dirty DMAs in some given range.
195  *
196  * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set
197  * of features does not track logging state relative to the device,
198  * therefore the device implementation of vfio_log_ops must handle
199  * arbitrary user requests. This includes rejecting subsequent calls
200  * to log_start without an intervening log_stop, as well as graceful
201  * handling of log_stop and log_read_and_clear from invalid states.
202  */
203 struct vfio_log_ops {
204 	int (*log_start)(struct vfio_device *device,
205 		struct rb_root_cached *ranges, u32 nnodes, u64 *page_size);
206 	int (*log_stop)(struct vfio_device *device);
207 	int (*log_read_and_clear)(struct vfio_device *device,
208 		unsigned long iova, unsigned long length,
209 		struct iova_bitmap *dirty);
210 };
211 
212 /**
213  * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
214  * @flags: Arg from the device_feature op
215  * @argsz: Arg from the device_feature op
216  * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
217  *                 supports
218  * @minsz: Minimum data size the driver accepts
219  *
220  * For use in a driver's device_feature op. Checks that the inputs to the
221  * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
222  * the driver should execute the get or set, otherwise the relevant
223  * value should be returned.
224  */
225 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
226 				    size_t minsz)
227 {
228 	if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
229 	    ~supported_ops)
230 		return -EINVAL;
231 	if (flags & VFIO_DEVICE_FEATURE_PROBE)
232 		return 0;
233 	/* Without PROBE one of GET or SET must be requested */
234 	if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
235 		return -EINVAL;
236 	if (argsz < minsz)
237 		return -EINVAL;
238 	return 1;
239 }
240 
241 struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev,
242 				       const struct vfio_device_ops *ops);
243 #define vfio_alloc_device(dev_struct, member, dev, ops)				\
244 	container_of(_vfio_alloc_device(sizeof(struct dev_struct) +		\
245 					BUILD_BUG_ON_ZERO(offsetof(		\
246 						struct dev_struct, member)),	\
247 					dev, ops),				\
248 		     struct dev_struct, member)
249 
250 static inline void vfio_put_device(struct vfio_device *device)
251 {
252 	put_device(&device->device);
253 }
254 
255 int vfio_register_group_dev(struct vfio_device *device);
256 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
257 void vfio_unregister_group_dev(struct vfio_device *device);
258 
259 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
260 unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set);
261 struct vfio_device *
262 vfio_find_device_in_devset(struct vfio_device_set *dev_set,
263 			   struct device *dev);
264 
265 int vfio_mig_get_next_state(struct vfio_device *device,
266 			    enum vfio_device_mig_state cur_fsm,
267 			    enum vfio_device_mig_state new_fsm,
268 			    enum vfio_device_mig_state *next_fsm);
269 
270 /*
271  * External user API
272  */
273 struct iommu_group *vfio_file_iommu_group(struct file *file);
274 bool vfio_file_is_group(struct file *file);
275 bool vfio_file_enforced_coherent(struct file *file);
276 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
277 bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
278 
279 #define VFIO_PIN_PAGES_MAX_ENTRIES	(PAGE_SIZE/sizeof(unsigned long))
280 
281 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
282 		   int npage, int prot, struct page **pages);
283 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
284 int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
285 		void *data, size_t len, bool write);
286 
287 /*
288  * Sub-module helpers
289  */
290 struct vfio_info_cap {
291 	struct vfio_info_cap_header *buf;
292 	size_t size;
293 };
294 struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
295 					       size_t size, u16 id,
296 					       u16 version);
297 void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
298 
299 int vfio_info_add_capability(struct vfio_info_cap *caps,
300 			     struct vfio_info_cap_header *cap, size_t size);
301 
302 int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
303 				       int num_irqs, int max_irq_type,
304 				       size_t *data_size);
305 
306 /*
307  * IRQfd - generic
308  */
309 struct virqfd {
310 	void			*opaque;
311 	struct eventfd_ctx	*eventfd;
312 	int			(*handler)(void *, void *);
313 	void			(*thread)(void *, void *);
314 	void			*data;
315 	struct work_struct	inject;
316 	wait_queue_entry_t		wait;
317 	poll_table		pt;
318 	struct work_struct	shutdown;
319 	struct virqfd		**pvirqfd;
320 };
321 
322 int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
323 		       void (*thread)(void *, void *), void *data,
324 		       struct virqfd **pvirqfd, int fd);
325 void vfio_virqfd_disable(struct virqfd **pvirqfd);
326 
327 #endif /* VFIO_H */
328