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