blob: c77c212cffd3d5e2d2cc9a3a689a528c10a0aed3 [file] [log] [blame]
Tuomas Tynkkynend58269f2018-10-15 02:21:01 -07001/* 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". */
47struct 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
Andrew Scull594d2a62022-05-16 10:41:31 +000058/* Shadow of struct vring_desc in guest byte order. */
59struct vring_desc_shadow {
60 u64 addr;
61 u32 len;
62 u16 flags;
63 u16 next;
Andrew Scullc282bdb2022-05-16 10:41:32 +000064 /* Metadata about the descriptor. */
65 bool chain_head;
Andrew Scull594d2a62022-05-16 10:41:31 +000066};
67
Tuomas Tynkkynend58269f2018-10-15 02:21:01 -070068struct vring_avail {
69 __virtio16 flags;
70 __virtio16 idx;
71 __virtio16 ring[];
72};
73
74struct vring_used_elem {
75 /* Index of start of used descriptor chain */
76 __virtio32 id;
77 /* Total length of the descriptor chain which was used (written to) */
78 __virtio32 len;
79};
80
81struct vring_used {
82 __virtio16 flags;
83 __virtio16 idx;
84 struct vring_used_elem ring[];
85};
86
87struct vring {
88 unsigned int num;
89 struct vring_desc *desc;
90 struct vring_avail *avail;
91 struct vring_used *used;
92};
93
94/**
95 * virtqueue - a queue to register buffers for sending or receiving.
96 *
97 * @list: the chain of virtqueues for this device
98 * @vdev: the virtio device this queue was created for
99 * @index: the zero-based ordinal number for this queue
100 * @num_free: number of elements we expect to be able to fit
101 * @vring: actual memory layout for this queue
Andrew Scull594d2a62022-05-16 10:41:31 +0000102 * @vring_desc_shadow: guest-only copy of descriptors
Tuomas Tynkkynend58269f2018-10-15 02:21:01 -0700103 * @event: host publishes avail event idx
104 * @free_head: head of free buffer list
105 * @num_added: number we've added since last sync
106 * @last_used_idx: last used index we've seen
107 * @avail_flags_shadow: last written value to avail->flags
108 * @avail_idx_shadow: last written value to avail->idx in guest byte order
109 */
110struct virtqueue {
111 struct list_head list;
112 struct udevice *vdev;
113 unsigned int index;
114 unsigned int num_free;
115 struct vring vring;
Andrew Scull594d2a62022-05-16 10:41:31 +0000116 struct vring_desc_shadow *vring_desc_shadow;
Tuomas Tynkkynend58269f2018-10-15 02:21:01 -0700117 bool event;
118 unsigned int free_head;
119 unsigned int num_added;
120 u16 last_used_idx;
121 u16 avail_flags_shadow;
122 u16 avail_idx_shadow;
123};
124
125/*
126 * Alignment requirements for vring elements.
127 * When using pre-virtio 1.0 layout, these fall out naturally.
128 */
129#define VRING_AVAIL_ALIGN_SIZE 2
130#define VRING_USED_ALIGN_SIZE 4
131#define VRING_DESC_ALIGN_SIZE 16
132
133/*
134 * We publish the used event index at the end of the available ring,
135 * and vice versa. They are at the end for backwards compatibility.
136 */
137#define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
138#define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
139
140static inline void vring_init(struct vring *vr, unsigned int num, void *p,
141 unsigned long align)
142{
143 vr->num = num;
144 vr->desc = p;
145 vr->avail = p + num * sizeof(struct vring_desc);
146 vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] +
147 sizeof(__virtio16) + align - 1) & ~(align - 1));
148}
149
150static inline unsigned int vring_size(unsigned int num, unsigned long align)
151{
152 return ((sizeof(struct vring_desc) * num +
153 sizeof(__virtio16) * (3 + num) + align - 1) & ~(align - 1)) +
154 sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num;
155}
156
157/*
158 * The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX.
159 * Assuming a given event_idx value from the other side, if we have just
160 * incremented index from old to new_idx, should we trigger an event?
161 */
162static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old)
163{
164 /*
165 * Note: Xen has similar logic for notification hold-off
166 * in include/xen/interface/io/ring.h with req_event and req_prod
167 * corresponding to event_idx + 1 and new_idx respectively.
168 * Note also that req_event and req_prod in Xen start at 1,
169 * event indexes in virtio start at 0.
170 */
171 return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old);
172}
173
174struct virtio_sg;
175
176/**
177 * virtqueue_add - expose buffers to other end
178 *
179 * @vq: the struct virtqueue we're talking about
180 * @sgs: array of terminated scatterlists
181 * @out_sgs: the number of scatterlists readable by other side
182 * @in_sgs: the number of scatterlists which are writable
183 * (after readable ones)
184 *
185 * Caller must ensure we don't call this with other virtqueue operations
186 * at the same time (except where noted).
187 *
188 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
189 */
190int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
191 unsigned int out_sgs, unsigned int in_sgs);
192
193/**
194 * virtqueue_kick - update after add_buf
195 *
196 * @vq: the struct virtqueue
197 *
198 * After one or more virtqueue_add() calls, invoke this to kick
199 * the other side.
200 *
201 * Caller must ensure we don't call this with other virtqueue
202 * operations at the same time (except where noted).
203 */
204void virtqueue_kick(struct virtqueue *vq);
205
206/**
207 * virtqueue_get_buf - get the next used buffer
208 *
209 * @vq: the struct virtqueue we're talking about
210 * @len: the length written into the buffer
211 *
212 * If the device wrote data into the buffer, @len will be set to the
213 * amount written. This means you don't need to clear the buffer
214 * beforehand to ensure there's no data leakage in the case of short
215 * writes.
216 *
217 * Caller must ensure we don't call this with other virtqueue
218 * operations at the same time (except where noted).
219 *
220 * Returns NULL if there are no used buffers, or the memory buffer
221 * handed to virtqueue_add_*().
222 */
223void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
224
225/**
226 * vring_create_virtqueue - create a virtqueue for a virtio device
227 *
228 * @index: the index of the queue
229 * @num: number of elements of the queue
230 * @vring_align:the alignment requirement of the descriptor ring
231 * @udev: the virtio transport udevice
232 * @return: the virtqueue pointer or NULL if failed
233 *
234 * This creates a virtqueue and allocates the descriptor ring for a virtio
235 * device. The caller should query virtqueue_get_ring_size() to learn the
236 * actual size of the ring.
237 *
238 * This API is supposed to be called by the virtio transport driver in the
239 * virtio find_vqs() uclass method.
240 */
241struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
242 unsigned int vring_align,
243 struct udevice *udev);
244
245/**
246 * vring_del_virtqueue - destroy a virtqueue
247 *
248 * @vq: the struct virtqueue we're talking about
249 *
250 * This destroys a virtqueue. If created with vring_create_virtqueue(),
251 * this also frees the descriptor ring.
252 *
253 * This API is supposed to be called by the virtio transport driver in the
254 * virtio del_vqs() uclass method.
255 */
256void vring_del_virtqueue(struct virtqueue *vq);
257
258/**
259 * virtqueue_get_vring_size - get the size of the virtqueue's vring
260 *
261 * @vq: the struct virtqueue containing the vring of interest
262 * @return: the size of the vring in a virtqueue.
263 */
264unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
265
266/**
267 * virtqueue_get_desc_addr - get the vring descriptor table address
268 *
269 * @vq: the struct virtqueue containing the vring of interest
270 * @return: the descriptor table address of the vring in a virtqueue.
271 */
272ulong virtqueue_get_desc_addr(struct virtqueue *vq);
273
274/**
275 * virtqueue_get_avail_addr - get the vring available ring address
276 *
277 * @vq: the struct virtqueue containing the vring of interest
278 * @return: the available ring address of the vring in a virtqueue.
279 */
280ulong virtqueue_get_avail_addr(struct virtqueue *vq);
281
282/**
283 * virtqueue_get_used_addr - get the vring used ring address
284 *
285 * @vq: the struct virtqueue containing the vring of interest
286 * @return: the used ring address of the vring in a virtqueue.
287 */
288ulong virtqueue_get_used_addr(struct virtqueue *vq);
289
290/**
291 * virtqueue_poll - query pending used buffers
292 *
293 * @vq: the struct virtqueue we're talking about
294 * @last_used_idx: virtqueue last used index
295 *
296 * Returns "true" if there are pending used buffers in the queue.
297 */
298bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx);
299
300/**
301 * virtqueue_dump - dump the virtqueue for debugging
302 *
303 * @vq: the struct virtqueue we're talking about
304 *
305 * Caller must ensure we don't call this with other virtqueue operations
306 * at the same time (except where noted).
307 */
308void virtqueue_dump(struct virtqueue *vq);
309
310/*
311 * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest
312 * scenario, having these as nops is enough to work as expected.
313 */
314
315static inline void virtio_mb(void)
316{
317}
318
319static inline void virtio_rmb(void)
320{
321}
322
323static inline void virtio_wmb(void)
324{
325}
326
327static inline void virtio_store_mb(__virtio16 *p, __virtio16 v)
328{
329 WRITE_ONCE(*p, v);
330}
331
332#endif /* _LINUX_VIRTIO_RING_H */