be1e7db4a314091676151a995efa69aaf54df911
[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
18 /*
19  * VFIO devices can be placed in a set, this allows all devices to share this
20  * structure and the VFIO core will provide a lock that is held around
21  * open_device()/close_device() for all devices in the set.
22  */
23 struct vfio_device_set {
24         void *set_id;
25         struct mutex lock;
26         struct list_head device_list;
27         unsigned int device_count;
28 };
29
30 struct vfio_device {
31         struct device *dev;
32         const struct vfio_device_ops *ops;
33         struct vfio_group *group;
34         struct vfio_device_set *dev_set;
35         struct list_head dev_set_list;
36         unsigned int migration_flags;
37
38         /* Members below here are private, not for driver use */
39         refcount_t refcount;
40         unsigned int open_count;
41         struct completion comp;
42         struct list_head group_next;
43 };
44
45 /**
46  * struct vfio_device_ops - VFIO bus driver device callbacks
47  *
48  * @open_device: Called when the first file descriptor is opened for this device
49  * @close_device: Opposite of open_device
50  * @read: Perform read(2) on device file descriptor
51  * @write: Perform write(2) on device file descriptor
52  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
53  *         operations documented below
54  * @mmap: Perform mmap(2) on a region of the device file descriptor
55  * @request: Request for the bus driver to release the device
56  * @match: Optional device name match callback (return: 0 for no-match, >0 for
57  *         match, -errno for abort (ex. match with insufficient or incorrect
58  *         additional args)
59  * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
60  * @migration_set_state: Optional callback to change the migration state for
61  *         devices that support migration. It's mandatory for
62  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
63  *         The returned FD is used for data transfer according to the FSM
64  *         definition. The driver is responsible to ensure that FD reaches end
65  *         of stream or error whenever the migration FSM leaves a data transfer
66  *         state or before close_device() returns.
67  * @migration_get_state: Optional callback to get the migration state for
68  *         devices that support migration. It's mandatory for
69  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
70  */
71 struct vfio_device_ops {
72         char    *name;
73         int     (*open_device)(struct vfio_device *vdev);
74         void    (*close_device)(struct vfio_device *vdev);
75         ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
76                         size_t count, loff_t *ppos);
77         ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
78                          size_t count, loff_t *size);
79         long    (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
80                          unsigned long arg);
81         int     (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
82         void    (*request)(struct vfio_device *vdev, unsigned int count);
83         int     (*match)(struct vfio_device *vdev, char *buf);
84         int     (*device_feature)(struct vfio_device *device, u32 flags,
85                                   void __user *arg, size_t argsz);
86         struct file *(*migration_set_state)(
87                 struct vfio_device *device,
88                 enum vfio_device_mig_state new_state);
89         int (*migration_get_state)(struct vfio_device *device,
90                                    enum vfio_device_mig_state *curr_state);
91 };
92
93 /**
94  * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
95  * @flags: Arg from the device_feature op
96  * @argsz: Arg from the device_feature op
97  * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
98  *                 supports
99  * @minsz: Minimum data size the driver accepts
100  *
101  * For use in a driver's device_feature op. Checks that the inputs to the
102  * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
103  * the driver should execute the get or set, otherwise the relevant
104  * value should be returned.
105  */
106 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
107                                     size_t minsz)
108 {
109         if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
110             ~supported_ops)
111                 return -EINVAL;
112         if (flags & VFIO_DEVICE_FEATURE_PROBE)
113                 return 0;
114         /* Without PROBE one of GET or SET must be requested */
115         if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
116                 return -EINVAL;
117         if (argsz < minsz)
118                 return -EINVAL;
119         return 1;
120 }
121
122 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
123                          const struct vfio_device_ops *ops);
124 void vfio_uninit_group_dev(struct vfio_device *device);
125 int vfio_register_group_dev(struct vfio_device *device);
126 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
127 void vfio_unregister_group_dev(struct vfio_device *device);
128
129 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
130
131 int vfio_mig_get_next_state(struct vfio_device *device,
132                             enum vfio_device_mig_state cur_fsm,
133                             enum vfio_device_mig_state new_fsm,
134                             enum vfio_device_mig_state *next_fsm);
135
136 /*
137  * External user API
138  */
139 extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
140 extern void vfio_group_put_external_user(struct vfio_group *group);
141 extern bool vfio_external_group_match_file(struct vfio_group *group,
142                                            struct file *filep);
143 extern int vfio_external_user_iommu_id(struct vfio_group *group);
144 extern long vfio_external_check_extension(struct vfio_group *group,
145                                           unsigned long arg);
146
147 #define VFIO_PIN_PAGES_MAX_ENTRIES      (PAGE_SIZE/sizeof(unsigned long))
148
149 extern int vfio_pin_pages(struct vfio_device *device, unsigned long *user_pfn,
150                           int npage, int prot, unsigned long *phys_pfn);
151 extern int vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
152                             int npage);
153 extern int vfio_dma_rw(struct vfio_device *device, dma_addr_t user_iova,
154                        void *data, size_t len, bool write);
155
156 /* each type has independent events */
157 enum vfio_notify_type {
158         VFIO_IOMMU_NOTIFY = 0,
159         VFIO_GROUP_NOTIFY = 1,
160 };
161
162 /* events for VFIO_IOMMU_NOTIFY */
163 #define VFIO_IOMMU_NOTIFY_DMA_UNMAP     BIT(0)
164
165 /* events for VFIO_GROUP_NOTIFY */
166 #define VFIO_GROUP_NOTIFY_SET_KVM       BIT(0)
167
168 extern int vfio_register_notifier(struct vfio_device *device,
169                                   enum vfio_notify_type type,
170                                   unsigned long *required_events,
171                                   struct notifier_block *nb);
172 extern int vfio_unregister_notifier(struct vfio_device *device,
173                                     enum vfio_notify_type type,
174                                     struct notifier_block *nb);
175
176 struct kvm;
177 extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
178
179 /*
180  * Sub-module helpers
181  */
182 struct vfio_info_cap {
183         struct vfio_info_cap_header *buf;
184         size_t size;
185 };
186 extern struct vfio_info_cap_header *vfio_info_cap_add(
187                 struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
188 extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
189
190 extern int vfio_info_add_capability(struct vfio_info_cap *caps,
191                                     struct vfio_info_cap_header *cap,
192                                     size_t size);
193
194 extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
195                                               int num_irqs, int max_irq_type,
196                                               size_t *data_size);
197
198 struct pci_dev;
199 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
200 extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
201 extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
202 extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
203                                        unsigned int cmd,
204                                        unsigned long arg);
205 #else
206 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
207 {
208 }
209
210 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
211 {
212 }
213
214 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
215                                               unsigned int cmd,
216                                               unsigned long arg)
217 {
218         return -ENOTTY;
219 }
220 #endif /* CONFIG_VFIO_SPAPR_EEH */
221
222 /*
223  * IRQfd - generic
224  */
225 struct virqfd {
226         void                    *opaque;
227         struct eventfd_ctx      *eventfd;
228         int                     (*handler)(void *, void *);
229         void                    (*thread)(void *, void *);
230         void                    *data;
231         struct work_struct      inject;
232         wait_queue_entry_t              wait;
233         poll_table              pt;
234         struct work_struct      shutdown;
235         struct virqfd           **pvirqfd;
236 };
237
238 extern int vfio_virqfd_enable(void *opaque,
239                               int (*handler)(void *, void *),
240                               void (*thread)(void *, void *),
241                               void *data, struct virqfd **pvirqfd, int fd);
242 extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
243
244 #endif /* VFIO_H */