1 /* SPDX-License-Identifier: GPL-2.0-only */
5 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Author: Alex Williamson <alex.williamson@redhat.com>
12 #include <linux/iommu.h>
14 #include <linux/workqueue.h>
15 #include <linux/poll.h>
16 #include <uapi/linux/vfio.h>
17 #include <linux/iova_bitmap.h>
21 struct iommufd_device;
22 struct iommufd_access;
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.
29 struct vfio_device_set {
32 struct list_head device_list;
33 unsigned int device_count;
38 const struct vfio_device_ops *ops;
40 * mig_ops/log_ops is a static property of the vfio_device which must
41 * be set prior to registering the vfio_device.
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;
51 /* Members below here are private, not for driver use */
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 struct iommufd_ctx *iommufd_ictx;
64 bool iommufd_attached;
69 * struct vfio_device_ops - VFIO bus driver device callbacks
71 * @name: Name of the device driver.
72 * @init: initialize private fields in device structure
73 * @release: Reclaim private fields in device structure
74 * @bind_iommufd: Called when binding the device to an iommufd
75 * @unbind_iommufd: Opposite of bind_iommufd
76 * @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the
77 * bound iommufd. Undo in unbind_iommufd.
78 * @open_device: Called when the first file descriptor is opened for this device
79 * @close_device: Opposite of open_device
80 * @read: Perform read(2) on device file descriptor
81 * @write: Perform write(2) on device file descriptor
82 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
83 * operations documented below
84 * @mmap: Perform mmap(2) on a region of the device file descriptor
85 * @request: Request for the bus driver to release the device
86 * @match: Optional device name match callback (return: 0 for no-match, >0 for
87 * match, -errno for abort (ex. match with insufficient or incorrect
89 * @dma_unmap: Called when userspace unmaps IOVA from the container
90 * this device is attached to.
91 * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
93 struct vfio_device_ops {
95 int (*init)(struct vfio_device *vdev);
96 void (*release)(struct vfio_device *vdev);
97 int (*bind_iommufd)(struct vfio_device *vdev,
98 struct iommufd_ctx *ictx, u32 *out_device_id);
99 void (*unbind_iommufd)(struct vfio_device *vdev);
100 int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
101 int (*open_device)(struct vfio_device *vdev);
102 void (*close_device)(struct vfio_device *vdev);
103 ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
104 size_t count, loff_t *ppos);
105 ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
106 size_t count, loff_t *size);
107 long (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
109 int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
110 void (*request)(struct vfio_device *vdev, unsigned int count);
111 int (*match)(struct vfio_device *vdev, char *buf);
112 void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
113 int (*device_feature)(struct vfio_device *device, u32 flags,
114 void __user *arg, size_t argsz);
117 #if IS_ENABLED(CONFIG_IOMMUFD)
118 int vfio_iommufd_physical_bind(struct vfio_device *vdev,
119 struct iommufd_ctx *ictx, u32 *out_device_id);
120 void vfio_iommufd_physical_unbind(struct vfio_device *vdev);
121 int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
122 int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
123 struct iommufd_ctx *ictx, u32 *out_device_id);
124 void vfio_iommufd_emulated_unbind(struct vfio_device *vdev);
125 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
127 #define vfio_iommufd_physical_bind \
128 ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \
129 u32 *out_device_id)) NULL)
130 #define vfio_iommufd_physical_unbind \
131 ((void (*)(struct vfio_device *vdev)) NULL)
132 #define vfio_iommufd_physical_attach_ioas \
133 ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
134 #define vfio_iommufd_emulated_bind \
135 ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \
136 u32 *out_device_id)) NULL)
137 #define vfio_iommufd_emulated_unbind \
138 ((void (*)(struct vfio_device *vdev)) NULL)
139 #define vfio_iommufd_emulated_attach_ioas \
140 ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
144 * struct vfio_migration_ops - VFIO bus device driver migration callbacks
146 * @migration_set_state: Optional callback to change the migration state for
147 * devices that support migration. It's mandatory for
148 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
149 * The returned FD is used for data transfer according to the FSM
150 * definition. The driver is responsible to ensure that FD reaches end
151 * of stream or error whenever the migration FSM leaves a data transfer
152 * state or before close_device() returns.
153 * @migration_get_state: Optional callback to get the migration state for
154 * devices that support migration. It's mandatory for
155 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
156 * @migration_get_data_size: Optional callback to get the estimated data
157 * length that will be required to complete stop copy. It's mandatory for
158 * VFIO_DEVICE_FEATURE_MIGRATION migration support.
160 struct vfio_migration_ops {
161 struct file *(*migration_set_state)(
162 struct vfio_device *device,
163 enum vfio_device_mig_state new_state);
164 int (*migration_get_state)(struct vfio_device *device,
165 enum vfio_device_mig_state *curr_state);
166 int (*migration_get_data_size)(struct vfio_device *device,
167 unsigned long *stop_copy_length);
171 * struct vfio_log_ops - VFIO bus device driver logging callbacks
173 * @log_start: Optional callback to ask the device start DMA logging.
174 * @log_stop: Optional callback to ask the device stop DMA logging.
175 * @log_read_and_clear: Optional callback to ask the device read
176 * and clear the dirty DMAs in some given range.
178 * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set
179 * of features does not track logging state relative to the device,
180 * therefore the device implementation of vfio_log_ops must handle
181 * arbitrary user requests. This includes rejecting subsequent calls
182 * to log_start without an intervening log_stop, as well as graceful
183 * handling of log_stop and log_read_and_clear from invalid states.
185 struct vfio_log_ops {
186 int (*log_start)(struct vfio_device *device,
187 struct rb_root_cached *ranges, u32 nnodes, u64 *page_size);
188 int (*log_stop)(struct vfio_device *device);
189 int (*log_read_and_clear)(struct vfio_device *device,
190 unsigned long iova, unsigned long length,
191 struct iova_bitmap *dirty);
195 * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
196 * @flags: Arg from the device_feature op
197 * @argsz: Arg from the device_feature op
198 * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
200 * @minsz: Minimum data size the driver accepts
202 * For use in a driver's device_feature op. Checks that the inputs to the
203 * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
204 * the driver should execute the get or set, otherwise the relevant
205 * value should be returned.
207 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
210 if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
213 if (flags & VFIO_DEVICE_FEATURE_PROBE)
215 /* Without PROBE one of GET or SET must be requested */
216 if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
223 struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev,
224 const struct vfio_device_ops *ops);
225 #define vfio_alloc_device(dev_struct, member, dev, ops) \
226 container_of(_vfio_alloc_device(sizeof(struct dev_struct) + \
227 BUILD_BUG_ON_ZERO(offsetof( \
228 struct dev_struct, member)), \
230 struct dev_struct, member)
232 static inline void vfio_put_device(struct vfio_device *device)
234 put_device(&device->device);
237 int vfio_register_group_dev(struct vfio_device *device);
238 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
239 void vfio_unregister_group_dev(struct vfio_device *device);
241 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
242 unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set);
244 int vfio_mig_get_next_state(struct vfio_device *device,
245 enum vfio_device_mig_state cur_fsm,
246 enum vfio_device_mig_state new_fsm,
247 enum vfio_device_mig_state *next_fsm);
252 struct iommu_group *vfio_file_iommu_group(struct file *file);
253 bool vfio_file_is_group(struct file *file);
254 bool vfio_file_enforced_coherent(struct file *file);
255 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
256 bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
258 #define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
260 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
261 int npage, int prot, struct page **pages);
262 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
263 int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
264 void *data, size_t len, bool write);
269 struct vfio_info_cap {
270 struct vfio_info_cap_header *buf;
273 struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
276 void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
278 int vfio_info_add_capability(struct vfio_info_cap *caps,
279 struct vfio_info_cap_header *cap, size_t size);
281 int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
282 int num_irqs, int max_irq_type,
290 struct eventfd_ctx *eventfd;
291 int (*handler)(void *, void *);
292 void (*thread)(void *, void *);
294 struct work_struct inject;
295 wait_queue_entry_t wait;
297 struct work_struct shutdown;
298 struct virqfd **pvirqfd;
301 int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
302 void (*thread)(void *, void *), void *data,
303 struct virqfd **pvirqfd, int fd);
304 void vfio_virqfd_disable(struct virqfd **pvirqfd);