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> | |
607ca46e | 16 | #include <uapi/linux/vfio.h> |
80c4b92a | 17 | #include <linux/iova_bitmap.h> |
cba3345c | 18 | |
ba70a89f | 19 | struct kvm; |
a4d1f91d JG |
20 | struct iommufd_ctx; |
21 | struct iommufd_device; | |
4741f2e9 | 22 | struct iommufd_access; |
ba70a89f | 23 | |
2fd585f4 JG |
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 | ||
0bfc6a4e JG |
36 | struct vfio_device { |
37 | struct device *dev; | |
38 | const struct vfio_device_ops *ops; | |
6e97eba8 | 39 | /* |
80c4b92a YH |
40 | * mig_ops/log_ops is a static property of the vfio_device which must |
41 | * be set prior to registering the vfio_device. | |
6e97eba8 YH |
42 | */ |
43 | const struct vfio_migration_ops *mig_ops; | |
80c4b92a | 44 | const struct vfio_log_ops *log_ops; |
0bfc6a4e | 45 | struct vfio_group *group; |
2fd585f4 JG |
46 | struct vfio_device_set *dev_set; |
47 | struct list_head dev_set_list; | |
8cb3d83b | 48 | unsigned int migration_flags; |
421cfe65 MR |
49 | /* Driver must reference the kvm during open_device or never touch it */ |
50 | struct kvm *kvm; | |
0bfc6a4e JG |
51 | |
52 | /* Members below here are private, not for driver use */ | |
3c28a761 YL |
53 | unsigned int index; |
54 | struct device device; /* device.kref covers object life circle */ | |
cb9ff3f3 | 55 | refcount_t refcount; /* user count on registered device*/ |
2fd585f4 | 56 | unsigned int open_count; |
0bfc6a4e JG |
57 | struct completion comp; |
58 | struct list_head group_next; | |
8cfc5b60 | 59 | struct list_head iommu_entry; |
4741f2e9 | 60 | struct iommufd_access *iommufd_access; |
a4d1f91d JG |
61 | #if IS_ENABLED(CONFIG_IOMMUFD) |
62 | struct iommufd_device *iommufd_device; | |
4741f2e9 | 63 | struct iommufd_ctx *iommufd_ictx; |
a4d1f91d JG |
64 | bool iommufd_attached; |
65 | #endif | |
0bfc6a4e JG |
66 | }; |
67 | ||
cba3345c AW |
68 | /** |
69 | * struct vfio_device_ops - VFIO bus driver device callbacks | |
70 | * | |
cb9ff3f3 KT |
71 | * @init: initialize private fields in device structure |
72 | * @release: Reclaim private fields in device structure | |
2fd585f4 JG |
73 | * @open_device: Called when the first file descriptor is opened for this device |
74 | * @close_device: Opposite of open_device | |
cba3345c AW |
75 | * @read: Perform read(2) on device file descriptor |
76 | * @write: Perform write(2) on device file descriptor | |
77 | * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_* | |
78 | * operations documented below | |
79 | * @mmap: Perform mmap(2) on a region of the device file descriptor | |
13060b64 | 80 | * @request: Request for the bus driver to release the device |
5f3874c2 AW |
81 | * @match: Optional device name match callback (return: 0 for no-match, >0 for |
82 | * match, -errno for abort (ex. match with insufficient or incorrect | |
83 | * additional args) | |
ce4b4657 JG |
84 | * @dma_unmap: Called when userspace unmaps IOVA from the container |
85 | * this device is attached to. | |
445ad495 | 86 | * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl |
cba3345c AW |
87 | */ |
88 | struct vfio_device_ops { | |
89 | char *name; | |
cb9ff3f3 KT |
90 | int (*init)(struct vfio_device *vdev); |
91 | void (*release)(struct vfio_device *vdev); | |
a4d1f91d JG |
92 | int (*bind_iommufd)(struct vfio_device *vdev, |
93 | struct iommufd_ctx *ictx, u32 *out_device_id); | |
94 | void (*unbind_iommufd)(struct vfio_device *vdev); | |
95 | int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); | |
2fd585f4 JG |
96 | int (*open_device)(struct vfio_device *vdev); |
97 | void (*close_device)(struct vfio_device *vdev); | |
6df62c5b | 98 | ssize_t (*read)(struct vfio_device *vdev, char __user *buf, |
cba3345c | 99 | size_t count, loff_t *ppos); |
6df62c5b | 100 | ssize_t (*write)(struct vfio_device *vdev, const char __user *buf, |
cba3345c | 101 | size_t count, loff_t *size); |
6df62c5b | 102 | long (*ioctl)(struct vfio_device *vdev, unsigned int cmd, |
cba3345c | 103 | unsigned long arg); |
6df62c5b JG |
104 | int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma); |
105 | void (*request)(struct vfio_device *vdev, unsigned int count); | |
106 | int (*match)(struct vfio_device *vdev, char *buf); | |
ce4b4657 | 107 | void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length); |
445ad495 JG |
108 | int (*device_feature)(struct vfio_device *device, u32 flags, |
109 | void __user *arg, size_t argsz); | |
6e97eba8 YH |
110 | }; |
111 | ||
a4d1f91d JG |
112 | #if IS_ENABLED(CONFIG_IOMMUFD) |
113 | int vfio_iommufd_physical_bind(struct vfio_device *vdev, | |
114 | struct iommufd_ctx *ictx, u32 *out_device_id); | |
115 | void vfio_iommufd_physical_unbind(struct vfio_device *vdev); | |
116 | int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); | |
4741f2e9 JG |
117 | int vfio_iommufd_emulated_bind(struct vfio_device *vdev, |
118 | struct iommufd_ctx *ictx, u32 *out_device_id); | |
119 | void vfio_iommufd_emulated_unbind(struct vfio_device *vdev); | |
120 | int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); | |
a4d1f91d JG |
121 | #else |
122 | #define vfio_iommufd_physical_bind \ | |
123 | ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ | |
124 | u32 *out_device_id)) NULL) | |
125 | #define vfio_iommufd_physical_unbind \ | |
126 | ((void (*)(struct vfio_device *vdev)) NULL) | |
127 | #define vfio_iommufd_physical_attach_ioas \ | |
128 | ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) | |
4741f2e9 JG |
129 | #define vfio_iommufd_emulated_bind \ |
130 | ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ | |
131 | u32 *out_device_id)) NULL) | |
132 | #define vfio_iommufd_emulated_unbind \ | |
133 | ((void (*)(struct vfio_device *vdev)) NULL) | |
134 | #define vfio_iommufd_emulated_attach_ioas \ | |
135 | ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) | |
a4d1f91d JG |
136 | #endif |
137 | ||
6e97eba8 YH |
138 | /** |
139 | * @migration_set_state: Optional callback to change the migration state for | |
140 | * devices that support migration. It's mandatory for | |
141 | * VFIO_DEVICE_FEATURE_MIGRATION migration support. | |
142 | * The returned FD is used for data transfer according to the FSM | |
143 | * definition. The driver is responsible to ensure that FD reaches end | |
144 | * of stream or error whenever the migration FSM leaves a data transfer | |
145 | * state or before close_device() returns. | |
146 | * @migration_get_state: Optional callback to get the migration state for | |
147 | * devices that support migration. It's mandatory for | |
148 | * VFIO_DEVICE_FEATURE_MIGRATION migration support. | |
4e016f96 YH |
149 | * @migration_get_data_size: Optional callback to get the estimated data |
150 | * length that will be required to complete stop copy. It's mandatory for | |
151 | * VFIO_DEVICE_FEATURE_MIGRATION migration support. | |
6e97eba8 YH |
152 | */ |
153 | struct vfio_migration_ops { | |
115dcec6 JG |
154 | struct file *(*migration_set_state)( |
155 | struct vfio_device *device, | |
156 | enum vfio_device_mig_state new_state); | |
157 | int (*migration_get_state)(struct vfio_device *device, | |
158 | enum vfio_device_mig_state *curr_state); | |
4e016f96 YH |
159 | int (*migration_get_data_size)(struct vfio_device *device, |
160 | unsigned long *stop_copy_length); | |
cba3345c AW |
161 | }; |
162 | ||
80c4b92a YH |
163 | /** |
164 | * @log_start: Optional callback to ask the device start DMA logging. | |
165 | * @log_stop: Optional callback to ask the device stop DMA logging. | |
166 | * @log_read_and_clear: Optional callback to ask the device read | |
167 | * and clear the dirty DMAs in some given range. | |
168 | * | |
169 | * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set | |
170 | * of features does not track logging state relative to the device, | |
171 | * therefore the device implementation of vfio_log_ops must handle | |
172 | * arbitrary user requests. This includes rejecting subsequent calls | |
173 | * to log_start without an intervening log_stop, as well as graceful | |
174 | * handling of log_stop and log_read_and_clear from invalid states. | |
175 | */ | |
176 | struct vfio_log_ops { | |
177 | int (*log_start)(struct vfio_device *device, | |
178 | struct rb_root_cached *ranges, u32 nnodes, u64 *page_size); | |
179 | int (*log_stop)(struct vfio_device *device); | |
180 | int (*log_read_and_clear)(struct vfio_device *device, | |
181 | unsigned long iova, unsigned long length, | |
182 | struct iova_bitmap *dirty); | |
183 | }; | |
184 | ||
445ad495 JG |
185 | /** |
186 | * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl | |
187 | * @flags: Arg from the device_feature op | |
188 | * @argsz: Arg from the device_feature op | |
189 | * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver | |
190 | * supports | |
191 | * @minsz: Minimum data size the driver accepts | |
192 | * | |
193 | * For use in a driver's device_feature op. Checks that the inputs to the | |
194 | * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if | |
195 | * the driver should execute the get or set, otherwise the relevant | |
196 | * value should be returned. | |
197 | */ | |
198 | static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops, | |
199 | size_t minsz) | |
200 | { | |
201 | if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) & | |
202 | ~supported_ops) | |
203 | return -EINVAL; | |
204 | if (flags & VFIO_DEVICE_FEATURE_PROBE) | |
205 | return 0; | |
206 | /* Without PROBE one of GET or SET must be requested */ | |
207 | if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET))) | |
208 | return -EINVAL; | |
209 | if (argsz < minsz) | |
210 | return -EINVAL; | |
211 | return 1; | |
212 | } | |
213 | ||
cb9ff3f3 KT |
214 | struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev, |
215 | const struct vfio_device_ops *ops); | |
216 | #define vfio_alloc_device(dev_struct, member, dev, ops) \ | |
217 | container_of(_vfio_alloc_device(sizeof(struct dev_struct) + \ | |
218 | BUILD_BUG_ON_ZERO(offsetof( \ | |
219 | struct dev_struct, member)), \ | |
220 | dev, ops), \ | |
221 | struct dev_struct, member) | |
222 | ||
cb9ff3f3 KT |
223 | static inline void vfio_put_device(struct vfio_device *device) |
224 | { | |
3c28a761 | 225 | put_device(&device->device); |
cb9ff3f3 KT |
226 | } |
227 | ||
0bfc6a4e | 228 | int vfio_register_group_dev(struct vfio_device *device); |
c68ea0d0 | 229 | int vfio_register_emulated_iommu_dev(struct vfio_device *device); |
0bfc6a4e | 230 | void vfio_unregister_group_dev(struct vfio_device *device); |
cba3345c | 231 | |
2fd585f4 | 232 | int vfio_assign_device_set(struct vfio_device *device, void *set_id); |
5cd189e4 | 233 | unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set); |
2fd585f4 | 234 | |
115dcec6 JG |
235 | int vfio_mig_get_next_state(struct vfio_device *device, |
236 | enum vfio_device_mig_state cur_fsm, | |
237 | enum vfio_device_mig_state new_fsm, | |
238 | enum vfio_device_mig_state *next_fsm); | |
239 | ||
6cdd9782 AK |
240 | /* |
241 | * External user API | |
242 | */ | |
d1877e63 | 243 | struct iommu_group *vfio_file_iommu_group(struct file *file); |
4b22ef04 | 244 | bool vfio_file_is_group(struct file *file); |
d1877e63 AW |
245 | bool vfio_file_enforced_coherent(struct file *file); |
246 | void vfio_file_set_kvm(struct file *file, struct kvm *kvm); | |
247 | bool vfio_file_has_dev(struct file *file, struct vfio_device *device); | |
6cdd9782 | 248 | |
2169037d KW |
249 | #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long)) |
250 | ||
44abdd16 | 251 | int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, |
34a255e6 | 252 | int npage, int prot, struct page **pages); |
44abdd16 | 253 | void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage); |
8561aa4f | 254 | int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, |
d1877e63 | 255 | void *data, size_t len, bool write); |
8d46c0cc | 256 | |
d7a8d5ed AW |
257 | /* |
258 | * Sub-module helpers | |
259 | */ | |
260 | struct vfio_info_cap { | |
261 | struct vfio_info_cap_header *buf; | |
262 | size_t size; | |
263 | }; | |
d1877e63 AW |
264 | struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps, |
265 | size_t size, u16 id, | |
266 | u16 version); | |
267 | void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset); | |
d7a8d5ed | 268 | |
d1877e63 AW |
269 | int vfio_info_add_capability(struct vfio_info_cap *caps, |
270 | struct vfio_info_cap_header *cap, size_t size); | |
b3c0a866 | 271 | |
d1877e63 AW |
272 | int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, |
273 | int num_irqs, int max_irq_type, | |
274 | size_t *data_size); | |
c747f08a | 275 | |
7e992d69 AM |
276 | /* |
277 | * IRQfd - generic | |
278 | */ | |
279 | struct virqfd { | |
280 | void *opaque; | |
281 | struct eventfd_ctx *eventfd; | |
282 | int (*handler)(void *, void *); | |
283 | void (*thread)(void *, void *); | |
284 | void *data; | |
285 | struct work_struct inject; | |
ac6424b9 | 286 | wait_queue_entry_t wait; |
7e992d69 AM |
287 | poll_table pt; |
288 | struct work_struct shutdown; | |
289 | struct virqfd **pvirqfd; | |
290 | }; | |
291 | ||
d1877e63 AW |
292 | int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *), |
293 | void (*thread)(void *, void *), void *data, | |
294 | struct virqfd **pvirqfd, int fd); | |
295 | void vfio_virqfd_disable(struct virqfd **pvirqfd); | |
7e992d69 | 296 | |
cba3345c | 297 | #endif /* VFIO_H */ |