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