blob: 84db7b8f912f474310aa59b8970ce7a349def7c8 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Rusty Russellf87d0fb2013-03-20 13:50:14 +10302/*
3 * Linux host-side vring helpers; for when the kernel needs to access
4 * someone else's vring.
5 *
6 * Copyright IBM Corporation, 2013.
7 * Parts taken from drivers/vhost/vhost.c Copyright 2009 Red Hat, Inc.
8 *
Rusty Russellf87d0fb2013-03-20 13:50:14 +10309 * Written by: Rusty Russell <[email protected]>
10 */
11#ifndef _LINUX_VRINGH_H
12#define _LINUX_VRINGH_H
13#include <uapi/linux/virtio_ring.h>
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +020014#include <linux/virtio_byteorder.h>
Rusty Russellf87d0fb2013-03-20 13:50:14 +103015#include <linux/uio.h>
16#include <linux/slab.h>
Michael S. Tsirkin33023632020-04-01 12:46:22 -040017#if IS_REACHABLE(CONFIG_VHOST_IOTLB)
Jason Wang9ad9c492020-03-26 22:01:20 +080018#include <linux/dma-direction.h>
19#include <linux/vhost_iotlb.h>
Michael S. Tsirkin33023632020-04-01 12:46:22 -040020#endif
Rusty Russellf87d0fb2013-03-20 13:50:14 +103021#include <asm/barrier.h>
22
23/* virtio_ring with information needed for host access. */
24struct vringh {
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +020025 /* Everything is little endian */
26 bool little_endian;
27
Rusty Russellf87d0fb2013-03-20 13:50:14 +103028 /* Guest publishes used event idx (note: we always do). */
29 bool event_indices;
30
31 /* Can we get away with weak barriers? */
32 bool weak_barriers;
33
34 /* Last available index we saw (ie. where we're up to). */
35 u16 last_avail_idx;
36
37 /* Last index we used. */
38 u16 last_used_idx;
39
40 /* How many descriptors we've completed since last need_notify(). */
41 u32 completed;
42
43 /* The vring (note: it may contain user pointers!) */
44 struct vring vring;
Sjur Brændeland3beee862013-03-20 13:51:24 +103045
Jason Wang9ad9c492020-03-26 22:01:20 +080046 /* IOTLB for this vring */
47 struct vhost_iotlb *iotlb;
48
Stefano Garzarellaf53d9912021-03-15 17:34:38 +010049 /* spinlock to synchronize IOTLB accesses */
50 spinlock_t *iotlb_lock;
51
Sjur Brændeland3beee862013-03-20 13:51:24 +103052 /* The function to call to notify the guest about added buffers */
53 void (*notify)(struct vringh *);
54};
55
56/**
57 * struct vringh_config_ops - ops for creating a host vring from a virtio driver
58 * @find_vrhs: find the host vrings and instantiate them
59 * vdev: the virtio_device
60 * nhvrs: the number of host vrings to find
61 * hvrs: on success, includes new host vrings
62 * callbacks: array of driver callbacks, for each host vring
63 * include a NULL entry for vqs that do not need a callback
64 * Returns 0 on success or error status
65 * @del_vrhs: free the host vrings found by find_vrhs().
66 */
67struct virtio_device;
68typedef void vrh_callback_t(struct virtio_device *, struct vringh *);
69struct vringh_config_ops {
70 int (*find_vrhs)(struct virtio_device *vdev, unsigned nhvrs,
71 struct vringh *vrhs[], vrh_callback_t *callbacks[]);
72 void (*del_vrhs)(struct virtio_device *vdev);
Rusty Russellf87d0fb2013-03-20 13:50:14 +103073};
74
75/* The memory the vring can access, and what offset to apply. */
76struct vringh_range {
77 u64 start, end_incl;
78 u64 offset;
79};
80
81/**
82 * struct vringh_iov - iovec mangler.
83 *
84 * Mangles iovec in place, and restores it.
85 * Remaining data is iov + i, of used - i elements.
86 */
87struct vringh_iov {
88 struct iovec *iov;
89 size_t consumed; /* Within iov[i] */
90 unsigned i, used, max_num;
91};
92
93/**
94 * struct vringh_iov - kvec mangler.
95 *
96 * Mangles kvec in place, and restores it.
97 * Remaining data is iov + i, of used - i elements.
98 */
99struct vringh_kiov {
100 struct kvec *iov;
101 size_t consumed; /* Within iov[i] */
102 unsigned i, used, max_num;
103};
104
105/* Flag on max_num to indicate we're kmalloced. */
106#define VRINGH_IOV_ALLOCATED 0x8000000
107
108/* Helpers for userspace vrings. */
Michael S. Tsirkinb97a8a92014-12-12 00:36:06 +0200109int vringh_init_user(struct vringh *vrh, u64 features,
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030110 unsigned int num, bool weak_barriers,
Michael S. Tsirkina865e422020-04-06 08:42:55 -0400111 vring_desc_t __user *desc,
112 vring_avail_t __user *avail,
113 vring_used_t __user *used);
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030114
115static inline void vringh_iov_init(struct vringh_iov *iov,
116 struct iovec *iovec, unsigned num)
117{
118 iov->used = iov->i = 0;
119 iov->consumed = 0;
120 iov->max_num = num;
121 iov->iov = iovec;
122}
123
124static inline void vringh_iov_reset(struct vringh_iov *iov)
125{
126 iov->iov[iov->i].iov_len += iov->consumed;
127 iov->iov[iov->i].iov_base -= iov->consumed;
128 iov->consumed = 0;
129 iov->i = 0;
130}
131
132static inline void vringh_iov_cleanup(struct vringh_iov *iov)
133{
134 if (iov->max_num & VRINGH_IOV_ALLOCATED)
135 kfree(iov->iov);
136 iov->max_num = iov->used = iov->i = iov->consumed = 0;
137 iov->iov = NULL;
138}
139
140/* Convert a descriptor into iovecs. */
141int vringh_getdesc_user(struct vringh *vrh,
142 struct vringh_iov *riov,
143 struct vringh_iov *wiov,
144 bool (*getrange)(struct vringh *vrh,
145 u64 addr, struct vringh_range *r),
146 u16 *head);
147
148/* Copy bytes from readable vsg, consuming it (and incrementing wiov->i). */
149ssize_t vringh_iov_pull_user(struct vringh_iov *riov, void *dst, size_t len);
150
151/* Copy bytes into writable vsg, consuming it (and incrementing wiov->i). */
152ssize_t vringh_iov_push_user(struct vringh_iov *wiov,
153 const void *src, size_t len);
154
155/* Mark a descriptor as used. */
156int vringh_complete_user(struct vringh *vrh, u16 head, u32 len);
157int vringh_complete_multi_user(struct vringh *vrh,
158 const struct vring_used_elem used[],
159 unsigned num_used);
160
161/* Pretend we've never seen descriptor (for easy error handling). */
162void vringh_abandon_user(struct vringh *vrh, unsigned int num);
163
164/* Do we need to fire the eventfd to notify the other side? */
165int vringh_need_notify_user(struct vringh *vrh);
166
167bool vringh_notify_enable_user(struct vringh *vrh);
168void vringh_notify_disable_user(struct vringh *vrh);
169
170/* Helpers for kernelspace vrings. */
Michael S. Tsirkinb97a8a92014-12-12 00:36:06 +0200171int vringh_init_kern(struct vringh *vrh, u64 features,
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030172 unsigned int num, bool weak_barriers,
173 struct vring_desc *desc,
174 struct vring_avail *avail,
175 struct vring_used *used);
176
177static inline void vringh_kiov_init(struct vringh_kiov *kiov,
178 struct kvec *kvec, unsigned num)
179{
180 kiov->used = kiov->i = 0;
181 kiov->consumed = 0;
182 kiov->max_num = num;
183 kiov->iov = kvec;
184}
185
186static inline void vringh_kiov_reset(struct vringh_kiov *kiov)
187{
188 kiov->iov[kiov->i].iov_len += kiov->consumed;
189 kiov->iov[kiov->i].iov_base -= kiov->consumed;
190 kiov->consumed = 0;
191 kiov->i = 0;
192}
193
194static inline void vringh_kiov_cleanup(struct vringh_kiov *kiov)
195{
196 if (kiov->max_num & VRINGH_IOV_ALLOCATED)
197 kfree(kiov->iov);
198 kiov->max_num = kiov->used = kiov->i = kiov->consumed = 0;
199 kiov->iov = NULL;
200}
201
Stefano Garzarella14c9ac02021-03-15 17:34:42 +0100202static inline size_t vringh_kiov_length(struct vringh_kiov *kiov)
203{
204 size_t len = 0;
205 int i;
206
207 for (i = kiov->i; i < kiov->used; i++)
208 len += kiov->iov[i].iov_len;
209
210 return len;
211}
212
Stefano Garzarellab8c06ad2021-03-15 17:34:41 +0100213void vringh_kiov_advance(struct vringh_kiov *kiov, size_t len);
214
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030215int vringh_getdesc_kern(struct vringh *vrh,
216 struct vringh_kiov *riov,
217 struct vringh_kiov *wiov,
218 u16 *head,
219 gfp_t gfp);
220
221ssize_t vringh_iov_pull_kern(struct vringh_kiov *riov, void *dst, size_t len);
222ssize_t vringh_iov_push_kern(struct vringh_kiov *wiov,
223 const void *src, size_t len);
224void vringh_abandon_kern(struct vringh *vrh, unsigned int num);
225int vringh_complete_kern(struct vringh *vrh, u16 head, u32 len);
226
227bool vringh_notify_enable_kern(struct vringh *vrh);
228void vringh_notify_disable_kern(struct vringh *vrh);
229
230int vringh_need_notify_kern(struct vringh *vrh);
231
Sjur Brændeland3beee862013-03-20 13:51:24 +1030232/* Notify the guest about buffers added to the used ring */
233static inline void vringh_notify(struct vringh *vrh)
234{
235 if (vrh->notify)
236 vrh->notify(vrh);
237}
238
Greg Kurz5da7b162015-04-24 14:24:58 +0200239static inline bool vringh_is_little_endian(const struct vringh *vrh)
240{
Greg Kurz7d824102015-04-24 14:26:24 +0200241 return vrh->little_endian ||
242 virtio_legacy_is_little_endian();
Greg Kurz5da7b162015-04-24 14:24:58 +0200243}
244
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200245static inline u16 vringh16_to_cpu(const struct vringh *vrh, __virtio16 val)
246{
Greg Kurz5da7b162015-04-24 14:24:58 +0200247 return __virtio16_to_cpu(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200248}
249
250static inline __virtio16 cpu_to_vringh16(const struct vringh *vrh, u16 val)
251{
Greg Kurz5da7b162015-04-24 14:24:58 +0200252 return __cpu_to_virtio16(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200253}
254
255static inline u32 vringh32_to_cpu(const struct vringh *vrh, __virtio32 val)
256{
Greg Kurz5da7b162015-04-24 14:24:58 +0200257 return __virtio32_to_cpu(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200258}
259
260static inline __virtio32 cpu_to_vringh32(const struct vringh *vrh, u32 val)
261{
Greg Kurz5da7b162015-04-24 14:24:58 +0200262 return __cpu_to_virtio32(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200263}
264
265static inline u64 vringh64_to_cpu(const struct vringh *vrh, __virtio64 val)
266{
Greg Kurz5da7b162015-04-24 14:24:58 +0200267 return __virtio64_to_cpu(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200268}
269
270static inline __virtio64 cpu_to_vringh64(const struct vringh *vrh, u64 val)
271{
Greg Kurz5da7b162015-04-24 14:24:58 +0200272 return __cpu_to_virtio64(vringh_is_little_endian(vrh), val);
Michael S. Tsirkinb9f7ac82014-12-12 01:10:49 +0200273}
Jason Wang9ad9c492020-03-26 22:01:20 +0800274
Michael S. Tsirkin33023632020-04-01 12:46:22 -0400275#if IS_REACHABLE(CONFIG_VHOST_IOTLB)
276
Stefano Garzarellaf53d9912021-03-15 17:34:38 +0100277void vringh_set_iotlb(struct vringh *vrh, struct vhost_iotlb *iotlb,
278 spinlock_t *iotlb_lock);
Jason Wang9ad9c492020-03-26 22:01:20 +0800279
280int vringh_init_iotlb(struct vringh *vrh, u64 features,
281 unsigned int num, bool weak_barriers,
282 struct vring_desc *desc,
283 struct vring_avail *avail,
284 struct vring_used *used);
285
286int vringh_getdesc_iotlb(struct vringh *vrh,
287 struct vringh_kiov *riov,
288 struct vringh_kiov *wiov,
289 u16 *head,
290 gfp_t gfp);
291
292ssize_t vringh_iov_pull_iotlb(struct vringh *vrh,
293 struct vringh_kiov *riov,
294 void *dst, size_t len);
295ssize_t vringh_iov_push_iotlb(struct vringh *vrh,
296 struct vringh_kiov *wiov,
297 const void *src, size_t len);
298
299void vringh_abandon_iotlb(struct vringh *vrh, unsigned int num);
300
301int vringh_complete_iotlb(struct vringh *vrh, u16 head, u32 len);
302
303bool vringh_notify_enable_iotlb(struct vringh *vrh);
304void vringh_notify_disable_iotlb(struct vringh *vrh);
305
306int vringh_need_notify_iotlb(struct vringh *vrh);
307
Michael S. Tsirkin33023632020-04-01 12:46:22 -0400308#endif /* CONFIG_VHOST_IOTLB */
309
Rusty Russellf87d0fb2013-03-20 13:50:14 +1030310#endif /* _LINUX_VRINGH_H */