1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
6 * virtio ring implementation
12 #include <virtio_types.h>
14 #include <virtio_ring.h>
16 int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
17 unsigned int out_sgs, unsigned int in_sgs)
19 struct vring_desc *desc;
20 unsigned int total_sg = out_sgs + in_sgs;
21 unsigned int i, n, avail, descs_used, uninitialized_var(prev);
24 WARN_ON(total_sg == 0);
28 desc = vq->vring.desc;
30 descs_used = total_sg;
32 if (vq->num_free < descs_used) {
33 debug("Can't add buf len %i - avail = %i\n",
34 descs_used, vq->num_free);
36 * FIXME: for historical reasons, we force a notify here if
37 * there are outgoing parts to the buffer. Presumably the
38 * host should service the ring ASAP.
41 virtio_notify(vq->vdev, vq);
45 for (n = 0; n < out_sgs; n++) {
46 struct virtio_sg *sg = sgs[n];
48 desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT);
49 desc[i].addr = cpu_to_virtio64(vq->vdev, (u64)(size_t)sg->addr);
50 desc[i].len = cpu_to_virtio32(vq->vdev, sg->length);
53 i = virtio16_to_cpu(vq->vdev, desc[i].next);
55 for (; n < (out_sgs + in_sgs); n++) {
56 struct virtio_sg *sg = sgs[n];
58 desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT |
60 desc[i].addr = cpu_to_virtio64(vq->vdev,
61 (u64)(uintptr_t)sg->addr);
62 desc[i].len = cpu_to_virtio32(vq->vdev, sg->length);
65 i = virtio16_to_cpu(vq->vdev, desc[i].next);
67 /* Last one doesn't continue */
68 desc[prev].flags &= cpu_to_virtio16(vq->vdev, ~VRING_DESC_F_NEXT);
70 /* We're using some buffers from the free list. */
71 vq->num_free -= descs_used;
73 /* Update free pointer */
77 * Put entry in available array (but don't update avail->idx
78 * until they do sync).
80 avail = vq->avail_idx_shadow & (vq->vring.num - 1);
81 vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head);
84 * Descriptors and available array need to be set before we expose the
85 * new available array entries.
88 vq->avail_idx_shadow++;
89 vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow);
93 * This is very unlikely, but theoretically possible.
96 if (unlikely(vq->num_added == (1 << 16) - 1))
102 static bool virtqueue_kick_prepare(struct virtqueue *vq)
108 * We need to expose available array entries before checking
113 old = vq->avail_idx_shadow - vq->num_added;
114 new = vq->avail_idx_shadow;
118 needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev,
119 vring_avail_event(&vq->vring)), new, old);
121 needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev,
122 VRING_USED_F_NO_NOTIFY));
128 void virtqueue_kick(struct virtqueue *vq)
130 if (virtqueue_kick_prepare(vq))
131 virtio_notify(vq->vdev, vq);
134 static void detach_buf(struct virtqueue *vq, unsigned int head)
137 __virtio16 nextflag = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT);
139 /* Put back on free list: unmap first-level descriptors and find end */
142 while (vq->vring.desc[i].flags & nextflag) {
143 i = virtio16_to_cpu(vq->vdev, vq->vring.desc[i].next);
147 vq->vring.desc[i].next = cpu_to_virtio16(vq->vdev, vq->free_head);
148 vq->free_head = head;
150 /* Plus final descriptor */
154 static inline bool more_used(const struct virtqueue *vq)
156 return vq->last_used_idx != virtio16_to_cpu(vq->vdev,
157 vq->vring.used->idx);
160 void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len)
165 if (!more_used(vq)) {
166 debug("(%s.%d): No more buffers in queue\n",
167 vq->vdev->name, vq->index);
171 /* Only get used array entries after they have been exposed by host */
174 last_used = (vq->last_used_idx & (vq->vring.num - 1));
175 i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id);
177 *len = virtio32_to_cpu(vq->vdev,
178 vq->vring.used->ring[last_used].len);
179 debug("(%s.%d): last used idx %u with len %u\n",
180 vq->vdev->name, vq->index, i, *len);
183 if (unlikely(i >= vq->vring.num)) {
184 printf("(%s.%d): id %u out of range\n",
185 vq->vdev->name, vq->index, i);
192 * If we expect an interrupt for the next entry, tell host
193 * by writing event index and flush out the write before
194 * the read in the next get_buf call.
196 if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
197 virtio_store_mb(&vring_used_event(&vq->vring),
198 cpu_to_virtio16(vq->vdev, vq->last_used_idx));
200 return (void *)(uintptr_t)virtio64_to_cpu(vq->vdev,
201 vq->vring.desc[i].addr);
204 static struct virtqueue *__vring_new_virtqueue(unsigned int index,
206 struct udevice *udev)
209 struct virtqueue *vq;
210 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
211 struct udevice *vdev = uc_priv->vdev;
213 vq = malloc(sizeof(*vq));
219 vq->num_free = vring.num;
221 vq->last_used_idx = 0;
222 vq->avail_flags_shadow = 0;
223 vq->avail_idx_shadow = 0;
225 list_add_tail(&vq->list, &uc_priv->vqs);
227 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
229 /* Tell other side not to bother us */
230 vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
232 vq->vring.avail->flags = cpu_to_virtio16(vdev,
233 vq->avail_flags_shadow);
235 /* Put everything in free lists */
237 for (i = 0; i < vring.num - 1; i++)
238 vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1);
243 struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
244 unsigned int vring_align,
245 struct udevice *udev)
247 struct virtqueue *vq;
251 /* We assume num is a power of 2 */
252 if (num & (num - 1)) {
253 printf("Bad virtqueue length %u\n", num);
257 /* TODO: allocate each queue chunk individually */
258 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
259 queue = memalign(PAGE_SIZE, vring_size(num, vring_align));
268 /* Try to get a single page. You are my only hope! */
269 queue = memalign(PAGE_SIZE, vring_size(num, vring_align));
274 memset(queue, 0, vring_size(num, vring_align));
275 vring_init(&vring, num, queue, vring_align);
277 vq = __vring_new_virtqueue(index, vring, udev);
282 debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name,
288 void vring_del_virtqueue(struct virtqueue *vq)
290 free(vq->vring.desc);
295 unsigned int virtqueue_get_vring_size(struct virtqueue *vq)
297 return vq->vring.num;
300 ulong virtqueue_get_desc_addr(struct virtqueue *vq)
302 return (ulong)vq->vring.desc;
305 ulong virtqueue_get_avail_addr(struct virtqueue *vq)
307 return (ulong)vq->vring.desc +
308 ((char *)vq->vring.avail - (char *)vq->vring.desc);
311 ulong virtqueue_get_used_addr(struct virtqueue *vq)
313 return (ulong)vq->vring.desc +
314 ((char *)vq->vring.used - (char *)vq->vring.desc);
317 bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx)
321 return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx);
324 void virtqueue_dump(struct virtqueue *vq)
328 printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name);
329 printf("\tindex %u, phys addr %p num %u\n",
330 vq->index, vq->vring.desc, vq->vring.num);
331 printf("\tfree_head %u, num_added %u, num_free %u\n",
332 vq->free_head, vq->num_added, vq->num_free);
333 printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n",
334 vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow);
336 printf("Descriptor dump:\n");
337 for (i = 0; i < vq->vring.num; i++) {
338 printf("\tdesc[%u] = { 0x%llx, len %u, flags %u, next %u }\n",
339 i, vq->vring.desc[i].addr, vq->vring.desc[i].len,
340 vq->vring.desc[i].flags, vq->vring.desc[i].next);
343 printf("Avail ring dump:\n");
344 printf("\tflags %u, idx %u\n",
345 vq->vring.avail->flags, vq->vring.avail->idx);
346 for (i = 0; i < vq->vring.num; i++) {
347 printf("\tavail[%u] = %u\n",
348 i, vq->vring.avail->ring[i]);
351 printf("Used ring dump:\n");
352 printf("\tflags %u, idx %u\n",
353 vq->vring.used->flags, vq->vring.used->idx);
354 for (i = 0; i < vq->vring.num; i++) {
355 printf("\tused[%u] = { %u, %u }\n", i,
356 vq->vring.used->ring[i].id, vq->vring.used->ring[i].len);