1 /* SPDX-License-Identifier: BSD-3-Clause */
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
6 * From Linux kernel include/uapi/linux/virtio_ring.h
9 #ifndef _LINUX_VIRTIO_RING_H
10 #define _LINUX_VIRTIO_RING_H
12 #include <virtio_types.h>
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
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.
26 #define VRING_USED_F_NO_NOTIFY 1
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.
32 #define VRING_AVAIL_F_NO_INTERRUPT 1
34 /* We support indirect buffer descriptors */
35 #define VIRTIO_RING_F_INDIRECT_DESC 28
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.
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.
44 #define VIRTIO_RING_F_EVENT_IDX 29
46 /* Virtio ring descriptors: 16 bytes. These can chain together via "next". */
48 /* Address (guest-physical) */
52 /* The flags as indicated above */
54 /* We chain unused descriptors via this, too */
58 /* Shadow of struct vring_desc in guest byte order. */
59 struct vring_desc_shadow
{
64 /* Metadata about the descriptor. */
74 struct vring_used_elem
{
75 /* Index of start of used descriptor chain */
77 /* Total length of the descriptor chain which was used (written to) */
84 struct vring_used_elem ring
[];
90 struct bounce_buffer
*bouncebufs
;
91 struct vring_desc
*desc
;
92 struct vring_avail
*avail
;
93 struct vring_used
*used
;
97 * virtqueue - a queue to register buffers for sending or receiving.
99 * @list: the chain of virtqueues for this device
100 * @vdev: the virtio device this queue was created for
101 * @index: the zero-based ordinal number for this queue
102 * @num_free: number of elements we expect to be able to fit
103 * @vring: actual memory layout for this queue
104 * @vring_desc_shadow: guest-only copy of descriptors
105 * @event: host publishes avail event idx
106 * @free_head: head of free buffer list
107 * @num_added: number we've added since last sync
108 * @last_used_idx: last used index we've seen
109 * @avail_flags_shadow: last written value to avail->flags
110 * @avail_idx_shadow: last written value to avail->idx in guest byte order
113 struct list_head list
;
114 struct udevice
*vdev
;
116 unsigned int num_free
;
118 struct vring_desc_shadow
*vring_desc_shadow
;
120 unsigned int free_head
;
121 unsigned int num_added
;
123 u16 avail_flags_shadow
;
124 u16 avail_idx_shadow
;
128 * Alignment requirements for vring elements.
129 * When using pre-virtio 1.0 layout, these fall out naturally.
131 #define VRING_AVAIL_ALIGN_SIZE 2
132 #define VRING_USED_ALIGN_SIZE 4
133 #define VRING_DESC_ALIGN_SIZE 16
136 * We publish the used event index at the end of the available ring,
137 * and vice versa. They are at the end for backwards compatibility.
139 #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
140 #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
142 static inline unsigned int vring_size(unsigned int num
, unsigned long align
)
144 return ((sizeof(struct vring_desc
) * num
+
145 sizeof(__virtio16
) * (3 + num
) + align
- 1) & ~(align
- 1)) +
146 sizeof(__virtio16
) * 3 + sizeof(struct vring_used_elem
) * num
;
149 static inline void vring_init(struct vring
*vr
, unsigned int num
, void *p
,
151 struct bounce_buffer
*bouncebufs
)
154 vr
->size
= vring_size(num
, align
);
155 vr
->bouncebufs
= bouncebufs
;
157 vr
->avail
= p
+ num
* sizeof(struct vring_desc
);
158 vr
->used
= (void *)(((uintptr_t)&vr
->avail
->ring
[num
] +
159 sizeof(__virtio16
) + align
- 1) & ~(align
- 1));
163 * The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX.
164 * Assuming a given event_idx value from the other side, if we have just
165 * incremented index from old to new_idx, should we trigger an event?
167 static inline int vring_need_event(__u16 event_idx
, __u16 new_idx
, __u16 old
)
170 * Note: Xen has similar logic for notification hold-off
171 * in include/xen/interface/io/ring.h with req_event and req_prod
172 * corresponding to event_idx + 1 and new_idx respectively.
173 * Note also that req_event and req_prod in Xen start at 1,
174 * event indexes in virtio start at 0.
176 return (__u16
)(new_idx
- event_idx
- 1) < (__u16
)(new_idx
- old
);
182 * virtqueue_add - expose buffers to other end
184 * @vq: the struct virtqueue we're talking about
185 * @sgs: array of terminated scatterlists
186 * @out_sgs: the number of scatterlists readable by other side
187 * @in_sgs: the number of scatterlists which are writable
188 * (after readable ones)
190 * Caller must ensure we don't call this with other virtqueue operations
191 * at the same time (except where noted).
193 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
195 int virtqueue_add(struct virtqueue
*vq
, struct virtio_sg
*sgs
[],
196 unsigned int out_sgs
, unsigned int in_sgs
);
199 * virtqueue_kick - update after add_buf
201 * @vq: the struct virtqueue
203 * After one or more virtqueue_add() calls, invoke this to kick
206 * Caller must ensure we don't call this with other virtqueue
207 * operations at the same time (except where noted).
209 void virtqueue_kick(struct virtqueue
*vq
);
212 * virtqueue_get_buf - get the next used buffer
214 * @vq: the struct virtqueue we're talking about
215 * @len: the length written into the buffer
217 * If the device wrote data into the buffer, @len will be set to the
218 * amount written. This means you don't need to clear the buffer
219 * beforehand to ensure there's no data leakage in the case of short
222 * Caller must ensure we don't call this with other virtqueue
223 * operations at the same time (except where noted).
225 * Returns NULL if there are no used buffers, or the memory buffer
226 * handed to virtqueue_add_*().
228 void *virtqueue_get_buf(struct virtqueue
*vq
, unsigned int *len
);
231 * vring_create_virtqueue - create a virtqueue for a virtio device
233 * @index: the index of the queue
234 * @num: number of elements of the queue
235 * @vring_align:the alignment requirement of the descriptor ring
236 * @udev: the virtio transport udevice
237 * @return: the virtqueue pointer or NULL if failed
239 * This creates a virtqueue and allocates the descriptor ring for a virtio
240 * device. The caller should query virtqueue_get_ring_size() to learn the
241 * actual size of the ring.
243 * This API is supposed to be called by the virtio transport driver in the
244 * virtio find_vqs() uclass method.
246 struct virtqueue
*vring_create_virtqueue(unsigned int index
, unsigned int num
,
247 unsigned int vring_align
,
248 struct udevice
*udev
);
251 * vring_del_virtqueue - destroy a virtqueue
253 * @vq: the struct virtqueue we're talking about
255 * This destroys a virtqueue. If created with vring_create_virtqueue(),
256 * this also frees the descriptor ring.
258 * This API is supposed to be called by the virtio transport driver in the
259 * virtio del_vqs() uclass method.
261 void vring_del_virtqueue(struct virtqueue
*vq
);
264 * virtqueue_get_vring_size - get the size of the virtqueue's vring
266 * @vq: the struct virtqueue containing the vring of interest
267 * @return: the size of the vring in a virtqueue.
269 unsigned int virtqueue_get_vring_size(struct virtqueue
*vq
);
272 * virtqueue_get_desc_addr - get the vring descriptor table address
274 * @vq: the struct virtqueue containing the vring of interest
275 * @return: the descriptor table address of the vring in a virtqueue.
277 ulong
virtqueue_get_desc_addr(struct virtqueue
*vq
);
280 * virtqueue_get_avail_addr - get the vring available ring address
282 * @vq: the struct virtqueue containing the vring of interest
283 * @return: the available ring address of the vring in a virtqueue.
285 ulong
virtqueue_get_avail_addr(struct virtqueue
*vq
);
288 * virtqueue_get_used_addr - get the vring used ring address
290 * @vq: the struct virtqueue containing the vring of interest
291 * @return: the used ring address of the vring in a virtqueue.
293 ulong
virtqueue_get_used_addr(struct virtqueue
*vq
);
296 * virtqueue_poll - query pending used buffers
298 * @vq: the struct virtqueue we're talking about
299 * @last_used_idx: virtqueue last used index
301 * Returns "true" if there are pending used buffers in the queue.
303 bool virtqueue_poll(struct virtqueue
*vq
, u16 last_used_idx
);
306 * virtqueue_dump - dump the virtqueue for debugging
308 * @vq: the struct virtqueue we're talking about
310 * Caller must ensure we don't call this with other virtqueue operations
311 * at the same time (except where noted).
313 void virtqueue_dump(struct virtqueue
*vq
);
316 * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest
317 * scenario, having these as nops is enough to work as expected.
320 static inline void virtio_mb(void)
324 static inline void virtio_rmb(void)
328 static inline void virtio_wmb(void)
332 static inline void virtio_store_mb(__virtio16
*p
, __virtio16 v
)
337 #endif /* _LINUX_VIRTIO_RING_H */