]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - include/virtio_ring.h
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
[];
89 struct vring_desc
*desc
;
90 struct vring_avail
*avail
;
91 struct vring_used
*used
;
95 * virtqueue - a queue to register buffers for sending or receiving.
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
102 * @vring_desc_shadow: guest-only copy of descriptors
103 * @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
111 struct list_head list
;
112 struct udevice
*vdev
;
114 unsigned int num_free
;
116 struct vring_desc_shadow
*vring_desc_shadow
;
118 unsigned int free_head
;
119 unsigned int num_added
;
121 u16 avail_flags_shadow
;
122 u16 avail_idx_shadow
;
126 * Alignment requirements for vring elements.
127 * When using pre-virtio 1.0 layout, these fall out naturally.
129 #define VRING_AVAIL_ALIGN_SIZE 2
130 #define VRING_USED_ALIGN_SIZE 4
131 #define VRING_DESC_ALIGN_SIZE 16
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.
137 #define vring_used_event(vr) ((vr)->avail->ring[(vr)->num])
138 #define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num])
140 static inline void vring_init(struct vring
*vr
, unsigned int num
, void *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));
150 static inline unsigned int vring_size(unsigned int num
, unsigned long align
)
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
;
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?
162 static inline int vring_need_event(__u16 event_idx
, __u16 new_idx
, __u16 old
)
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.
171 return (__u16
)(new_idx
- event_idx
- 1) < (__u16
)(new_idx
- old
);
177 * virtqueue_add - expose buffers to other end
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)
185 * Caller must ensure we don't call this with other virtqueue operations
186 * at the same time (except where noted).
188 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
190 int virtqueue_add(struct virtqueue
*vq
, struct virtio_sg
*sgs
[],
191 unsigned int out_sgs
, unsigned int in_sgs
);
194 * virtqueue_kick - update after add_buf
196 * @vq: the struct virtqueue
198 * After one or more virtqueue_add() calls, invoke this to kick
201 * Caller must ensure we don't call this with other virtqueue
202 * operations at the same time (except where noted).
204 void virtqueue_kick(struct virtqueue
*vq
);
207 * virtqueue_get_buf - get the next used buffer
209 * @vq: the struct virtqueue we're talking about
210 * @len: the length written into the buffer
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
217 * Caller must ensure we don't call this with other virtqueue
218 * operations at the same time (except where noted).
220 * Returns NULL if there are no used buffers, or the memory buffer
221 * handed to virtqueue_add_*().
223 void *virtqueue_get_buf(struct virtqueue
*vq
, unsigned int *len
);
226 * vring_create_virtqueue - create a virtqueue for a virtio device
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
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.
238 * This API is supposed to be called by the virtio transport driver in the
239 * virtio find_vqs() uclass method.
241 struct virtqueue
*vring_create_virtqueue(unsigned int index
, unsigned int num
,
242 unsigned int vring_align
,
243 struct udevice
*udev
);
246 * vring_del_virtqueue - destroy a virtqueue
248 * @vq: the struct virtqueue we're talking about
250 * This destroys a virtqueue. If created with vring_create_virtqueue(),
251 * this also frees the descriptor ring.
253 * This API is supposed to be called by the virtio transport driver in the
254 * virtio del_vqs() uclass method.
256 void vring_del_virtqueue(struct virtqueue
*vq
);
259 * virtqueue_get_vring_size - get the size of the virtqueue's vring
261 * @vq: the struct virtqueue containing the vring of interest
262 * @return: the size of the vring in a virtqueue.
264 unsigned int virtqueue_get_vring_size(struct virtqueue
*vq
);
267 * virtqueue_get_desc_addr - get the vring descriptor table address
269 * @vq: the struct virtqueue containing the vring of interest
270 * @return: the descriptor table address of the vring in a virtqueue.
272 ulong
virtqueue_get_desc_addr(struct virtqueue
*vq
);
275 * virtqueue_get_avail_addr - get the vring available ring address
277 * @vq: the struct virtqueue containing the vring of interest
278 * @return: the available ring address of the vring in a virtqueue.
280 ulong
virtqueue_get_avail_addr(struct virtqueue
*vq
);
283 * virtqueue_get_used_addr - get the vring used ring address
285 * @vq: the struct virtqueue containing the vring of interest
286 * @return: the used ring address of the vring in a virtqueue.
288 ulong
virtqueue_get_used_addr(struct virtqueue
*vq
);
291 * virtqueue_poll - query pending used buffers
293 * @vq: the struct virtqueue we're talking about
294 * @last_used_idx: virtqueue last used index
296 * Returns "true" if there are pending used buffers in the queue.
298 bool virtqueue_poll(struct virtqueue
*vq
, u16 last_used_idx
);
301 * virtqueue_dump - dump the virtqueue for debugging
303 * @vq: the struct virtqueue we're talking about
305 * Caller must ensure we don't call this with other virtqueue operations
306 * at the same time (except where noted).
308 void virtqueue_dump(struct virtqueue
*vq
);
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.
315 static inline void virtio_mb(void)
319 static inline void virtio_rmb(void)
323 static inline void virtio_wmb(void)
327 static inline void virtio_store_mb(__virtio16
*p
, __virtio16 v
)
332 #endif /* _LINUX_VIRTIO_RING_H */