vhost: take worker or vq instead of dev for queueing
[platform/kernel/linux-rpi.git] / drivers / vhost / vhost.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _VHOST_H
3 #define _VHOST_H
4
5 #include <linux/eventfd.h>
6 #include <linux/vhost.h>
7 #include <linux/mm.h>
8 #include <linux/mutex.h>
9 #include <linux/poll.h>
10 #include <linux/file.h>
11 #include <linux/uio.h>
12 #include <linux/virtio_config.h>
13 #include <linux/virtio_ring.h>
14 #include <linux/atomic.h>
15 #include <linux/vhost_iotlb.h>
16 #include <linux/irqbypass.h>
17
18 struct vhost_work;
19 struct vhost_task;
20 typedef void (*vhost_work_fn_t)(struct vhost_work *work);
21
22 #define VHOST_WORK_QUEUED 1
23 struct vhost_work {
24         struct llist_node       node;
25         vhost_work_fn_t         fn;
26         unsigned long           flags;
27 };
28
29 struct vhost_worker {
30         struct vhost_task       *vtsk;
31         struct llist_head       work_list;
32         u64                     kcov_handle;
33 };
34
35 /* Poll a file (eventfd or socket) */
36 /* Note: there's nothing vhost specific about this structure. */
37 struct vhost_poll {
38         poll_table              table;
39         wait_queue_head_t       *wqh;
40         wait_queue_entry_t      wait;
41         struct vhost_work       work;
42         __poll_t                mask;
43         struct vhost_dev        *dev;
44 };
45
46 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn);
47 bool vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work);
48
49 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
50                      __poll_t mask, struct vhost_dev *dev);
51 int vhost_poll_start(struct vhost_poll *poll, struct file *file);
52 void vhost_poll_stop(struct vhost_poll *poll);
53 void vhost_poll_queue(struct vhost_poll *poll);
54 void vhost_dev_flush(struct vhost_dev *dev);
55
56 struct vhost_log {
57         u64 addr;
58         u64 len;
59 };
60
61 enum vhost_uaddr_type {
62         VHOST_ADDR_DESC = 0,
63         VHOST_ADDR_AVAIL = 1,
64         VHOST_ADDR_USED = 2,
65         VHOST_NUM_ADDRS = 3,
66 };
67
68 struct vhost_vring_call {
69         struct eventfd_ctx *ctx;
70         struct irq_bypass_producer producer;
71 };
72
73 /* The virtqueue structure describes a queue attached to a device. */
74 struct vhost_virtqueue {
75         struct vhost_dev *dev;
76         struct vhost_worker *worker;
77
78         /* The actual ring of buffers. */
79         struct mutex mutex;
80         unsigned int num;
81         vring_desc_t __user *desc;
82         vring_avail_t __user *avail;
83         vring_used_t __user *used;
84         const struct vhost_iotlb_map *meta_iotlb[VHOST_NUM_ADDRS];
85         struct file *kick;
86         struct vhost_vring_call call_ctx;
87         struct eventfd_ctx *error_ctx;
88         struct eventfd_ctx *log_ctx;
89
90         struct vhost_poll poll;
91
92         /* The routine to call when the Guest pings us, or timeout. */
93         vhost_work_fn_t handle_kick;
94
95         /* Last available index we saw.
96          * Values are limited to 0x7fff, and the high bit is used as
97          * a wrap counter when using VIRTIO_F_RING_PACKED. */
98         u16 last_avail_idx;
99
100         /* Caches available index value from user. */
101         u16 avail_idx;
102
103         /* Last index we used.
104          * Values are limited to 0x7fff, and the high bit is used as
105          * a wrap counter when using VIRTIO_F_RING_PACKED. */
106         u16 last_used_idx;
107
108         /* Used flags */
109         u16 used_flags;
110
111         /* Last used index value we have signalled on */
112         u16 signalled_used;
113
114         /* Last used index value we have signalled on */
115         bool signalled_used_valid;
116
117         /* Log writes to used structure. */
118         bool log_used;
119         u64 log_addr;
120
121         struct iovec iov[UIO_MAXIOV];
122         struct iovec iotlb_iov[64];
123         struct iovec *indirect;
124         struct vring_used_elem *heads;
125         /* Protected by virtqueue mutex. */
126         struct vhost_iotlb *umem;
127         struct vhost_iotlb *iotlb;
128         void *private_data;
129         u64 acked_features;
130         u64 acked_backend_features;
131         /* Log write descriptors */
132         void __user *log_base;
133         struct vhost_log *log;
134         struct iovec log_iov[64];
135
136         /* Ring endianness. Defaults to legacy native endianness.
137          * Set to true when starting a modern virtio device. */
138         bool is_le;
139 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
140         /* Ring endianness requested by userspace for cross-endian support. */
141         bool user_be;
142 #endif
143         u32 busyloop_timeout;
144 };
145
146 struct vhost_msg_node {
147   union {
148           struct vhost_msg msg;
149           struct vhost_msg_v2 msg_v2;
150   };
151   struct vhost_virtqueue *vq;
152   struct list_head node;
153 };
154
155 struct vhost_dev {
156         struct mm_struct *mm;
157         struct mutex mutex;
158         struct vhost_virtqueue **vqs;
159         int nvqs;
160         struct eventfd_ctx *log_ctx;
161         struct vhost_worker *worker;
162         struct vhost_iotlb *umem;
163         struct vhost_iotlb *iotlb;
164         spinlock_t iotlb_lock;
165         struct list_head read_list;
166         struct list_head pending_list;
167         wait_queue_head_t wait;
168         int iov_limit;
169         int weight;
170         int byte_weight;
171         bool use_worker;
172         int (*msg_handler)(struct vhost_dev *dev, u32 asid,
173                            struct vhost_iotlb_msg *msg);
174 };
175
176 bool vhost_exceeds_weight(struct vhost_virtqueue *vq, int pkts, int total_len);
177 void vhost_dev_init(struct vhost_dev *, struct vhost_virtqueue **vqs,
178                     int nvqs, int iov_limit, int weight, int byte_weight,
179                     bool use_worker,
180                     int (*msg_handler)(struct vhost_dev *dev, u32 asid,
181                                        struct vhost_iotlb_msg *msg));
182 long vhost_dev_set_owner(struct vhost_dev *dev);
183 bool vhost_dev_has_owner(struct vhost_dev *dev);
184 long vhost_dev_check_owner(struct vhost_dev *);
185 struct vhost_iotlb *vhost_dev_reset_owner_prepare(void);
186 void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *iotlb);
187 void vhost_dev_cleanup(struct vhost_dev *);
188 void vhost_dev_stop(struct vhost_dev *);
189 long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, void __user *argp);
190 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp);
191 bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
192 bool vhost_log_access_ok(struct vhost_dev *);
193 void vhost_clear_msg(struct vhost_dev *dev);
194
195 int vhost_get_vq_desc(struct vhost_virtqueue *,
196                       struct iovec iov[], unsigned int iov_count,
197                       unsigned int *out_num, unsigned int *in_num,
198                       struct vhost_log *log, unsigned int *log_num);
199 void vhost_discard_vq_desc(struct vhost_virtqueue *, int n);
200
201 bool vhost_vq_work_queue(struct vhost_virtqueue *vq, struct vhost_work *work);
202 bool vhost_vq_has_work(struct vhost_virtqueue *vq);
203 bool vhost_vq_is_setup(struct vhost_virtqueue *vq);
204 int vhost_vq_init_access(struct vhost_virtqueue *);
205 int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);
206 int vhost_add_used_n(struct vhost_virtqueue *, struct vring_used_elem *heads,
207                      unsigned count);
208 void vhost_add_used_and_signal(struct vhost_dev *, struct vhost_virtqueue *,
209                                unsigned int id, int len);
210 void vhost_add_used_and_signal_n(struct vhost_dev *, struct vhost_virtqueue *,
211                                struct vring_used_elem *heads, unsigned count);
212 void vhost_signal(struct vhost_dev *, struct vhost_virtqueue *);
213 void vhost_disable_notify(struct vhost_dev *, struct vhost_virtqueue *);
214 bool vhost_vq_avail_empty(struct vhost_dev *, struct vhost_virtqueue *);
215 bool vhost_enable_notify(struct vhost_dev *, struct vhost_virtqueue *);
216
217 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
218                     unsigned int log_num, u64 len,
219                     struct iovec *iov, int count);
220 int vq_meta_prefetch(struct vhost_virtqueue *vq);
221
222 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type);
223 void vhost_enqueue_msg(struct vhost_dev *dev,
224                        struct list_head *head,
225                        struct vhost_msg_node *node);
226 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
227                                          struct list_head *head);
228 void vhost_set_backend_features(struct vhost_dev *dev, u64 features);
229
230 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
231                             poll_table *wait);
232 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
233                             int noblock);
234 ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
235                              struct iov_iter *from);
236 int vhost_init_device_iotlb(struct vhost_dev *d);
237
238 void vhost_iotlb_map_free(struct vhost_iotlb *iotlb,
239                           struct vhost_iotlb_map *map);
240
241 #define vq_err(vq, fmt, ...) do {                                  \
242                 pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
243                 if ((vq)->error_ctx)                               \
244                                 eventfd_signal((vq)->error_ctx, 1);\
245         } while (0)
246
247 enum {
248         VHOST_FEATURES = (1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) |
249                          (1ULL << VIRTIO_RING_F_INDIRECT_DESC) |
250                          (1ULL << VIRTIO_RING_F_EVENT_IDX) |
251                          (1ULL << VHOST_F_LOG_ALL) |
252                          (1ULL << VIRTIO_F_ANY_LAYOUT) |
253                          (1ULL << VIRTIO_F_VERSION_1)
254 };
255
256 /**
257  * vhost_vq_set_backend - Set backend.
258  *
259  * @vq            Virtqueue.
260  * @private_data  The private data.
261  *
262  * Context: Need to call with vq->mutex acquired.
263  */
264 static inline void vhost_vq_set_backend(struct vhost_virtqueue *vq,
265                                         void *private_data)
266 {
267         vq->private_data = private_data;
268 }
269
270 /**
271  * vhost_vq_get_backend - Get backend.
272  *
273  * @vq            Virtqueue.
274  *
275  * Context: Need to call with vq->mutex acquired.
276  * Return: Private data previously set with vhost_vq_set_backend.
277  */
278 static inline void *vhost_vq_get_backend(struct vhost_virtqueue *vq)
279 {
280         return vq->private_data;
281 }
282
283 static inline bool vhost_has_feature(struct vhost_virtqueue *vq, int bit)
284 {
285         return vq->acked_features & (1ULL << bit);
286 }
287
288 static inline bool vhost_backend_has_feature(struct vhost_virtqueue *vq, int bit)
289 {
290         return vq->acked_backend_features & (1ULL << bit);
291 }
292
293 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
294 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
295 {
296         return vq->is_le;
297 }
298 #else
299 static inline bool vhost_is_little_endian(struct vhost_virtqueue *vq)
300 {
301         return virtio_legacy_is_little_endian() || vq->is_le;
302 }
303 #endif
304
305 /* Memory accessors */
306 static inline u16 vhost16_to_cpu(struct vhost_virtqueue *vq, __virtio16 val)
307 {
308         return __virtio16_to_cpu(vhost_is_little_endian(vq), val);
309 }
310
311 static inline __virtio16 cpu_to_vhost16(struct vhost_virtqueue *vq, u16 val)
312 {
313         return __cpu_to_virtio16(vhost_is_little_endian(vq), val);
314 }
315
316 static inline u32 vhost32_to_cpu(struct vhost_virtqueue *vq, __virtio32 val)
317 {
318         return __virtio32_to_cpu(vhost_is_little_endian(vq), val);
319 }
320
321 static inline __virtio32 cpu_to_vhost32(struct vhost_virtqueue *vq, u32 val)
322 {
323         return __cpu_to_virtio32(vhost_is_little_endian(vq), val);
324 }
325
326 static inline u64 vhost64_to_cpu(struct vhost_virtqueue *vq, __virtio64 val)
327 {
328         return __virtio64_to_cpu(vhost_is_little_endian(vq), val);
329 }
330
331 static inline __virtio64 cpu_to_vhost64(struct vhost_virtqueue *vq, u64 val)
332 {
333         return __cpu_to_virtio64(vhost_is_little_endian(vq), val);
334 }
335 #endif