]> git.ipfire.org Git - thirdparty/u-boot.git/blob - include/virtio_ring.h
virtio_ring: Maintain a shadow copy of descriptors
[thirdparty/u-boot.git] / include / virtio_ring.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 *
6 * From Linux kernel include/uapi/linux/virtio_ring.h
7 */
8
9 #ifndef _LINUX_VIRTIO_RING_H
10 #define _LINUX_VIRTIO_RING_H
11
12 #include <virtio_types.h>
13
14 /* This marks a buffer as continuing via the next field */
15 #define VRING_DESC_F_NEXT 1
16 /* This marks a buffer as write-only (otherwise read-only) */
17 #define VRING_DESC_F_WRITE 2
18 /* This means the buffer contains a list of buffer descriptors */
19 #define VRING_DESC_F_INDIRECT 4
20
21 /*
22 * The Host uses this in used->flags to advise the Guest: don't kick me when
23 * you add a buffer. It's unreliable, so it's simply an optimization. Guest
24 * will still kick if it's out of buffers.
25 */
26 #define VRING_USED_F_NO_NOTIFY 1
27
28 /*
29 * The Guest uses this in avail->flags to advise the Host: don't interrupt me
30 * when you consume a buffer. It's unreliable, so it's simply an optimization.
31 */
32 #define VRING_AVAIL_F_NO_INTERRUPT 1
33
34 /* We support indirect buffer descriptors */
35 #define VIRTIO_RING_F_INDIRECT_DESC 28
36
37 /*
38 * The Guest publishes the used index for which it expects an interrupt
39 * at the end of the avail ring. Host should ignore the avail->flags field.
40 *
41 * The Host publishes the avail index for which it expects a kick
42 * at the end of the used ring. Guest should ignore the used->flags field.
43 */
44 #define VIRTIO_RING_F_EVENT_IDX 29
45
46 /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
47 struct vring_desc {
48 /* Address (guest-physical) */
49 __virtio64 addr;
50 /* Length */
51 __virtio32 len;
52 /* The flags as indicated above */
53 __virtio16 flags;
54 /* We chain unused descriptors via this, too */
55 __virtio16 next;
56 };
57
58 /* Shadow of struct vring_desc in guest byte order. */
59 struct vring_desc_shadow {
60 u64 addr;
61 u32 len;
62 u16 flags;
63 u16 next;
64 };
65
66 struct vring_avail {
67 __virtio16 flags;
68 __virtio16 idx;
69 __virtio16 ring[];
70 };
71
72 struct vring_used_elem {
73 /* Index of start of used descriptor chain */
74 __virtio32 id;
75 /* Total length of the descriptor chain which was used (written to) */
76 __virtio32 len;
77 };
78
79 struct vring_used {
80 __virtio16 flags;
81 __virtio16 idx;
82 struct vring_used_elem ring[];
83 };
84
85 struct vring {
86 unsigned int num;
87 struct vring_desc *desc;
88 struct vring_avail *avail;
89 struct vring_used *used;
90 };
91
92 /**
93 * virtqueue - a queue to register buffers for sending or receiving.
94 *
95 * @list: the chain of virtqueues for this device
96 * @vdev: the virtio device this queue was created for
97 * @index: the zero-based ordinal number for this queue
98 * @num_free: number of elements we expect to be able to fit
99 * @vring: actual memory layout for this queue
100 * @vring_desc_shadow: guest-only copy of descriptors
101 * @event: host publishes avail event idx
102 * @free_head: head of free buffer list
103 * @num_added: number we've added since last sync
104 * @last_used_idx: last used index we've seen
105 * @avail_flags_shadow: last written value to avail->flags
106 * @avail_idx_shadow: last written value to avail->idx in guest byte order
107 */
108 struct virtqueue {
109 struct list_head list;
110 struct udevice *vdev;
111 unsigned int index;
112 unsigned int num_free;
113 struct vring vring;
114 struct vring_desc_shadow *vring_desc_shadow;
115 bool event;
116 unsigned int free_head;
117 unsigned int num_added;
118 u16 last_used_idx;
119 u16 avail_flags_shadow;
120 u16 avail_idx_shadow;
121 };
122
123 /*
124 * Alignment requirements for vring elements.
125 * When using pre-virtio 1.0 layout, these fall out naturally.
126 */
127 #define VRING_AVAIL_ALIGN_SIZE 2
128 #define VRING_USED_ALIGN_SIZE 4
129 #define VRING_DESC_ALIGN_SIZE 16
130
131 /*
132 * We publish the used event index at the end of the available ring,
133 * and vice versa. They are at the end for backwards compatibility.
134 */
135 #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
136 #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
137
138 static inline void vring_init(struct vring *vr, unsigned int num, void *p,
139 unsigned long align)
140 {
141 vr->num = num;
142 vr->desc = p;
143 vr->avail = p + num * sizeof(struct vring_desc);
144 vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] +
145 sizeof(__virtio16) + align - 1) & ~(align - 1));
146 }
147
148 static inline unsigned int vring_size(unsigned int num, unsigned long align)
149 {
150 return ((sizeof(struct vring_desc) * num +
151 sizeof(__virtio16) * (3 + num) + align - 1) & ~(align - 1)) +
152 sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
153 }
154
155 /*
156 * The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX.
157 * Assuming a given event_idx value from the other side, if we have just
158 * incremented index from old to new_idx, should we trigger an event?
159 */
160 static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old)
161 {
162 /*
163 * Note: Xen has similar logic for notification hold-off
164 * in include/xen/interface/io/ring.h with req_event and req_prod
165 * corresponding to event_idx + 1 and new_idx respectively.
166 * Note also that req_event and req_prod in Xen start at 1,
167 * event indexes in virtio start at 0.
168 */
169 return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old);
170 }
171
172 struct virtio_sg;
173
174 /**
175 * virtqueue_add - expose buffers to other end
176 *
177 * @vq: the struct virtqueue we're talking about
178 * @sgs: array of terminated scatterlists
179 * @out_sgs: the number of scatterlists readable by other side
180 * @in_sgs: the number of scatterlists which are writable
181 * (after readable ones)
182 *
183 * Caller must ensure we don't call this with other virtqueue operations
184 * at the same time (except where noted).
185 *
186 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
187 */
188 int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
189 unsigned int out_sgs, unsigned int in_sgs);
190
191 /**
192 * virtqueue_kick - update after add_buf
193 *
194 * @vq: the struct virtqueue
195 *
196 * After one or more virtqueue_add() calls, invoke this to kick
197 * the other side.
198 *
199 * Caller must ensure we don't call this with other virtqueue
200 * operations at the same time (except where noted).
201 */
202 void virtqueue_kick(struct virtqueue *vq);
203
204 /**
205 * virtqueue_get_buf - get the next used buffer
206 *
207 * @vq: the struct virtqueue we're talking about
208 * @len: the length written into the buffer
209 *
210 * If the device wrote data into the buffer, @len will be set to the
211 * amount written. This means you don't need to clear the buffer
212 * beforehand to ensure there's no data leakage in the case of short
213 * writes.
214 *
215 * Caller must ensure we don't call this with other virtqueue
216 * operations at the same time (except where noted).
217 *
218 * Returns NULL if there are no used buffers, or the memory buffer
219 * handed to virtqueue_add_*().
220 */
221 void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
222
223 /**
224 * vring_create_virtqueue - create a virtqueue for a virtio device
225 *
226 * @index: the index of the queue
227 * @num: number of elements of the queue
228 * @vring_align:the alignment requirement of the descriptor ring
229 * @udev: the virtio transport udevice
230 * @return: the virtqueue pointer or NULL if failed
231 *
232 * This creates a virtqueue and allocates the descriptor ring for a virtio
233 * device. The caller should query virtqueue_get_ring_size() to learn the
234 * actual size of the ring.
235 *
236 * This API is supposed to be called by the virtio transport driver in the
237 * virtio find_vqs() uclass method.
238 */
239 struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
240 unsigned int vring_align,
241 struct udevice *udev);
242
243 /**
244 * vring_del_virtqueue - destroy a virtqueue
245 *
246 * @vq: the struct virtqueue we're talking about
247 *
248 * This destroys a virtqueue. If created with vring_create_virtqueue(),
249 * this also frees the descriptor ring.
250 *
251 * This API is supposed to be called by the virtio transport driver in the
252 * virtio del_vqs() uclass method.
253 */
254 void vring_del_virtqueue(struct virtqueue *vq);
255
256 /**
257 * virtqueue_get_vring_size - get the size of the virtqueue's vring
258 *
259 * @vq: the struct virtqueue containing the vring of interest
260 * @return: the size of the vring in a virtqueue.
261 */
262 unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
263
264 /**
265 * virtqueue_get_desc_addr - get the vring descriptor table address
266 *
267 * @vq: the struct virtqueue containing the vring of interest
268 * @return: the descriptor table address of the vring in a virtqueue.
269 */
270 ulong virtqueue_get_desc_addr(struct virtqueue *vq);
271
272 /**
273 * virtqueue_get_avail_addr - get the vring available ring address
274 *
275 * @vq: the struct virtqueue containing the vring of interest
276 * @return: the available ring address of the vring in a virtqueue.
277 */
278 ulong virtqueue_get_avail_addr(struct virtqueue *vq);
279
280 /**
281 * virtqueue_get_used_addr - get the vring used ring address
282 *
283 * @vq: the struct virtqueue containing the vring of interest
284 * @return: the used ring address of the vring in a virtqueue.
285 */
286 ulong virtqueue_get_used_addr(struct virtqueue *vq);
287
288 /**
289 * virtqueue_poll - query pending used buffers
290 *
291 * @vq: the struct virtqueue we're talking about
292 * @last_used_idx: virtqueue last used index
293 *
294 * Returns "true" if there are pending used buffers in the queue.
295 */
296 bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx);
297
298 /**
299 * virtqueue_dump - dump the virtqueue for debugging
300 *
301 * @vq: the struct virtqueue we're talking about
302 *
303 * Caller must ensure we don't call this with other virtqueue operations
304 * at the same time (except where noted).
305 */
306 void virtqueue_dump(struct virtqueue *vq);
307
308 /*
309 * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest
310 * scenario, having these as nops is enough to work as expected.
311 */
312
313 static inline void virtio_mb(void)
314 {
315 }
316
317 static inline void virtio_rmb(void)
318 {
319 }
320
321 static inline void virtio_wmb(void)
322 {
323 }
324
325 static inline void virtio_store_mb(__virtio16 *p, __virtio16 v)
326 {
327 WRITE_ONCE(*p, v);
328 }
329
330 #endif /* _LINUX_VIRTIO_RING_H */