Merge tag 'i3c/for-6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/i3c/linux
[sfrench/cifs-2.6.git] / include / linux / vfio.h
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 <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  * @init: initialize private fields in device structure
71  * @release: Reclaim private fields in device structure
72  * @bind_iommufd: Called when binding the device to an iommufd
73  * @unbind_iommufd: Opposite of bind_iommufd
74  * @attach_ioas: Called when attaching device to an IOAS/HWPT managed by the
75  *               bound iommufd. Undo in unbind_iommufd.
76  * @open_device: Called when the first file descriptor is opened for this device
77  * @close_device: Opposite of open_device
78  * @read: Perform read(2) on device file descriptor
79  * @write: Perform write(2) on device file descriptor
80  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
81  *         operations documented below
82  * @mmap: Perform mmap(2) on a region of the device file descriptor
83  * @request: Request for the bus driver to release the device
84  * @match: Optional device name match callback (return: 0 for no-match, >0 for
85  *         match, -errno for abort (ex. match with insufficient or incorrect
86  *         additional args)
87  * @dma_unmap: Called when userspace unmaps IOVA from the container
88  *             this device is attached to.
89  * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
90  */
91 struct vfio_device_ops {
92         char    *name;
93         int     (*init)(struct vfio_device *vdev);
94         void    (*release)(struct vfio_device *vdev);
95         int     (*bind_iommufd)(struct vfio_device *vdev,
96                                 struct iommufd_ctx *ictx, u32 *out_device_id);
97         void    (*unbind_iommufd)(struct vfio_device *vdev);
98         int     (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id);
99         int     (*open_device)(struct vfio_device *vdev);
100         void    (*close_device)(struct vfio_device *vdev);
101         ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
102                         size_t count, loff_t *ppos);
103         ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
104                          size_t count, loff_t *size);
105         long    (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
106                          unsigned long arg);
107         int     (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
108         void    (*request)(struct vfio_device *vdev, unsigned int count);
109         int     (*match)(struct vfio_device *vdev, char *buf);
110         void    (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
111         int     (*device_feature)(struct vfio_device *device, u32 flags,
112                                   void __user *arg, size_t argsz);
113 };
114
115 #if IS_ENABLED(CONFIG_IOMMUFD)
116 int vfio_iommufd_physical_bind(struct vfio_device *vdev,
117                                struct iommufd_ctx *ictx, u32 *out_device_id);
118 void vfio_iommufd_physical_unbind(struct vfio_device *vdev);
119 int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
120 int vfio_iommufd_emulated_bind(struct vfio_device *vdev,
121                                struct iommufd_ctx *ictx, u32 *out_device_id);
122 void vfio_iommufd_emulated_unbind(struct vfio_device *vdev);
123 int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id);
124 #else
125 #define vfio_iommufd_physical_bind                                      \
126         ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx,   \
127                   u32 *out_device_id)) NULL)
128 #define vfio_iommufd_physical_unbind \
129         ((void (*)(struct vfio_device *vdev)) NULL)
130 #define vfio_iommufd_physical_attach_ioas \
131         ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
132 #define vfio_iommufd_emulated_bind                                      \
133         ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx,   \
134                   u32 *out_device_id)) NULL)
135 #define vfio_iommufd_emulated_unbind \
136         ((void (*)(struct vfio_device *vdev)) NULL)
137 #define vfio_iommufd_emulated_attach_ioas \
138         ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL)
139 #endif
140
141 /**
142  * @migration_set_state: Optional callback to change the migration state for
143  *         devices that support migration. It's mandatory for
144  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
145  *         The returned FD is used for data transfer according to the FSM
146  *         definition. The driver is responsible to ensure that FD reaches end
147  *         of stream or error whenever the migration FSM leaves a data transfer
148  *         state or before close_device() returns.
149  * @migration_get_state: Optional callback to get the migration state for
150  *         devices that support migration. It's mandatory for
151  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
152  * @migration_get_data_size: Optional callback to get the estimated data
153  *          length that will be required to complete stop copy. It's mandatory for
154  *          VFIO_DEVICE_FEATURE_MIGRATION migration support.
155  */
156 struct vfio_migration_ops {
157         struct file *(*migration_set_state)(
158                 struct vfio_device *device,
159                 enum vfio_device_mig_state new_state);
160         int (*migration_get_state)(struct vfio_device *device,
161                                    enum vfio_device_mig_state *curr_state);
162         int (*migration_get_data_size)(struct vfio_device *device,
163                                        unsigned long *stop_copy_length);
164 };
165
166 /**
167  * @log_start: Optional callback to ask the device start DMA logging.
168  * @log_stop: Optional callback to ask the device stop DMA logging.
169  * @log_read_and_clear: Optional callback to ask the device read
170  *         and clear the dirty DMAs in some given range.
171  *
172  * The vfio core implementation of the DEVICE_FEATURE_DMA_LOGGING_ set
173  * of features does not track logging state relative to the device,
174  * therefore the device implementation of vfio_log_ops must handle
175  * arbitrary user requests. This includes rejecting subsequent calls
176  * to log_start without an intervening log_stop, as well as graceful
177  * handling of log_stop and log_read_and_clear from invalid states.
178  */
179 struct vfio_log_ops {
180         int (*log_start)(struct vfio_device *device,
181                 struct rb_root_cached *ranges, u32 nnodes, u64 *page_size);
182         int (*log_stop)(struct vfio_device *device);
183         int (*log_read_and_clear)(struct vfio_device *device,
184                 unsigned long iova, unsigned long length,
185                 struct iova_bitmap *dirty);
186 };
187
188 /**
189  * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
190  * @flags: Arg from the device_feature op
191  * @argsz: Arg from the device_feature op
192  * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
193  *                 supports
194  * @minsz: Minimum data size the driver accepts
195  *
196  * For use in a driver's device_feature op. Checks that the inputs to the
197  * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
198  * the driver should execute the get or set, otherwise the relevant
199  * value should be returned.
200  */
201 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
202                                     size_t minsz)
203 {
204         if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
205             ~supported_ops)
206                 return -EINVAL;
207         if (flags & VFIO_DEVICE_FEATURE_PROBE)
208                 return 0;
209         /* Without PROBE one of GET or SET must be requested */
210         if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
211                 return -EINVAL;
212         if (argsz < minsz)
213                 return -EINVAL;
214         return 1;
215 }
216
217 struct vfio_device *_vfio_alloc_device(size_t size, struct device *dev,
218                                        const struct vfio_device_ops *ops);
219 #define vfio_alloc_device(dev_struct, member, dev, ops)                         \
220         container_of(_vfio_alloc_device(sizeof(struct dev_struct) +             \
221                                         BUILD_BUG_ON_ZERO(offsetof(             \
222                                                 struct dev_struct, member)),    \
223                                         dev, ops),                              \
224                      struct dev_struct, member)
225
226 static inline void vfio_put_device(struct vfio_device *device)
227 {
228         put_device(&device->device);
229 }
230
231 int vfio_register_group_dev(struct vfio_device *device);
232 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
233 void vfio_unregister_group_dev(struct vfio_device *device);
234
235 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
236 unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set);
237
238 int vfio_mig_get_next_state(struct vfio_device *device,
239                             enum vfio_device_mig_state cur_fsm,
240                             enum vfio_device_mig_state new_fsm,
241                             enum vfio_device_mig_state *next_fsm);
242
243 /*
244  * External user API
245  */
246 struct iommu_group *vfio_file_iommu_group(struct file *file);
247 bool vfio_file_is_group(struct file *file);
248 bool vfio_file_enforced_coherent(struct file *file);
249 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
250 bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
251
252 #define VFIO_PIN_PAGES_MAX_ENTRIES      (PAGE_SIZE/sizeof(unsigned long))
253
254 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
255                    int npage, int prot, struct page **pages);
256 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
257 int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
258                 void *data, size_t len, bool write);
259
260 /*
261  * Sub-module helpers
262  */
263 struct vfio_info_cap {
264         struct vfio_info_cap_header *buf;
265         size_t size;
266 };
267 struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
268                                                size_t size, u16 id,
269                                                u16 version);
270 void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
271
272 int vfio_info_add_capability(struct vfio_info_cap *caps,
273                              struct vfio_info_cap_header *cap, size_t size);
274
275 int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
276                                        int num_irqs, int max_irq_type,
277                                        size_t *data_size);
278
279 /*
280  * IRQfd - generic
281  */
282 struct virqfd {
283         void                    *opaque;
284         struct eventfd_ctx      *eventfd;
285         int                     (*handler)(void *, void *);
286         void                    (*thread)(void *, void *);
287         void                    *data;
288         struct work_struct      inject;
289         wait_queue_entry_t              wait;
290         poll_table              pt;
291         struct work_struct      shutdown;
292         struct virqfd           **pvirqfd;
293 };
294
295 int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
296                        void (*thread)(void *, void *), void *data,
297                        struct virqfd **pvirqfd, int fd);
298 void vfio_virqfd_disable(struct virqfd **pvirqfd);
299
300 #endif /* VFIO_H */