blob: c9adcce5c09ba18892fd8ec8291fcefa8b19041e [file] [log] [blame]
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 *
6 * virtio ring implementation
7 */
8
Will Deacon37e53db2023-03-29 22:24:59 +08009#include <bouncebuf.h>
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070010#include <common.h>
11#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070013#include <malloc.h>
14#include <virtio_types.h>
15#include <virtio.h>
16#include <virtio_ring.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060017#include <linux/bug.h>
Simon Glass336d4612020-02-03 07:36:16 -070018#include <linux/compat.h>
Will Deacon75582fc2023-03-29 22:24:57 +080019#include <linux/kernel.h>
20
21static void *virtio_alloc_pages(struct udevice *vdev, u32 npages)
22{
23 return memalign(PAGE_SIZE, npages * PAGE_SIZE);
24}
25
26static void virtio_free_pages(struct udevice *vdev, void *ptr, u32 npages)
27{
28 free(ptr);
29}
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070030
Will Deaconb0a2fe12023-03-29 22:25:00 +080031static int __bb_force_page_align(struct bounce_buffer *state)
32{
33 const ulong align_mask = PAGE_SIZE - 1;
34
35 if ((ulong)state->user_buffer & align_mask)
36 return 0;
37
38 if (state->len != state->len_aligned)
39 return 0;
40
41 return 1;
42}
43
Andrew Scullb0952972022-05-16 10:41:30 +000044static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i,
45 struct virtio_sg *sg, u16 flags)
46{
Andrew Scull10a14532022-05-16 10:41:31 +000047 struct vring_desc_shadow *desc_shadow = &vq->vring_desc_shadow[i];
Andrew Scullb0952972022-05-16 10:41:30 +000048 struct vring_desc *desc = &vq->vring.desc[i];
Will Deaconb0a2fe12023-03-29 22:25:00 +080049 void *addr;
50
51 if (IS_ENABLED(CONFIG_BOUNCE_BUFFER) && vq->vring.bouncebufs) {
52 struct bounce_buffer *bb = &vq->vring.bouncebufs[i];
53 unsigned int bbflags;
54 int ret;
55
56 if (flags & VRING_DESC_F_WRITE)
57 bbflags = GEN_BB_WRITE;
58 else
59 bbflags = GEN_BB_READ;
60
61 ret = bounce_buffer_start_extalign(bb, sg->addr, sg->length,
62 bbflags, PAGE_SIZE,
63 __bb_force_page_align);
64 if (ret) {
65 debug("%s: failed to allocate bounce buffer (length 0x%zx)\n",
66 vq->vdev->name, sg->length);
67 }
68
69 addr = bb->bounce_buffer;
70 } else {
71 addr = sg->addr;
72 }
Andrew Scullb0952972022-05-16 10:41:30 +000073
Andrew Scull10a14532022-05-16 10:41:31 +000074 /* Update the shadow descriptor. */
Will Deaconb0a2fe12023-03-29 22:25:00 +080075 desc_shadow->addr = (u64)(uintptr_t)addr;
Andrew Scull10a14532022-05-16 10:41:31 +000076 desc_shadow->len = sg->length;
77 desc_shadow->flags = flags;
Andrew Scullb0952972022-05-16 10:41:30 +000078
Andrew Scull10a14532022-05-16 10:41:31 +000079 /* Update the shared descriptor to match the shadow. */
80 desc->addr = cpu_to_virtio64(vq->vdev, desc_shadow->addr);
81 desc->len = cpu_to_virtio32(vq->vdev, desc_shadow->len);
82 desc->flags = cpu_to_virtio16(vq->vdev, desc_shadow->flags);
83 desc->next = cpu_to_virtio16(vq->vdev, desc_shadow->next);
84
85 return desc_shadow->next;
Andrew Scullb0952972022-05-16 10:41:30 +000086}
87
Will Deacona15d67e2023-03-29 22:24:58 +080088static void virtqueue_detach_desc(struct virtqueue *vq, unsigned int idx)
89{
Will Deaconb0a2fe12023-03-29 22:25:00 +080090 struct vring_desc *desc = &vq->vring.desc[idx];
91 struct bounce_buffer *bb;
92
93 if (!IS_ENABLED(CONFIG_BOUNCE_BUFFER) || !vq->vring.bouncebufs)
94 return;
95
96 bb = &vq->vring.bouncebufs[idx];
97 bounce_buffer_stop(bb);
98 desc->addr = cpu_to_virtio64(vq->vdev, (u64)(uintptr_t)bb->user_buffer);
Will Deacona15d67e2023-03-29 22:24:58 +080099}
100
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700101int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
102 unsigned int out_sgs, unsigned int in_sgs)
103{
104 struct vring_desc *desc;
Andrew Scull68f8bf22022-05-16 10:41:29 +0000105 unsigned int descs_used = out_sgs + in_sgs;
106 unsigned int i, n, avail, uninitialized_var(prev);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700107 int head;
108
Andrew Scull68f8bf22022-05-16 10:41:29 +0000109 WARN_ON(descs_used == 0);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700110
111 head = vq->free_head;
112
113 desc = vq->vring.desc;
114 i = head;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700115
116 if (vq->num_free < descs_used) {
117 debug("Can't add buf len %i - avail = %i\n",
118 descs_used, vq->num_free);
119 /*
120 * FIXME: for historical reasons, we force a notify here if
121 * there are outgoing parts to the buffer. Presumably the
122 * host should service the ring ASAP.
123 */
124 if (out_sgs)
125 virtio_notify(vq->vdev, vq);
126 return -ENOSPC;
127 }
128
Andrew Scullb0952972022-05-16 10:41:30 +0000129 for (n = 0; n < descs_used; n++) {
130 u16 flags = VRING_DESC_F_NEXT;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700131
Andrew Scullb0952972022-05-16 10:41:30 +0000132 if (n >= out_sgs)
133 flags |= VRING_DESC_F_WRITE;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700134 prev = i;
Andrew Scullb0952972022-05-16 10:41:30 +0000135 i = virtqueue_attach_desc(vq, i, sgs[n], flags);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700136 }
137 /* Last one doesn't continue */
Andrew Scull10a14532022-05-16 10:41:31 +0000138 vq->vring_desc_shadow[prev].flags &= ~VRING_DESC_F_NEXT;
139 desc[prev].flags = cpu_to_virtio16(vq->vdev, vq->vring_desc_shadow[prev].flags);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700140
141 /* We're using some buffers from the free list. */
142 vq->num_free -= descs_used;
143
144 /* Update free pointer */
145 vq->free_head = i;
146
Andrew Scullfbef3f52022-05-16 10:41:32 +0000147 /* Mark the descriptor as the head of a chain. */
148 vq->vring_desc_shadow[head].chain_head = true;
149
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700150 /*
151 * Put entry in available array (but don't update avail->idx
152 * until they do sync).
153 */
154 avail = vq->avail_idx_shadow & (vq->vring.num - 1);
155 vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head);
156
157 /*
158 * Descriptors and available array need to be set before we expose the
159 * new available array entries.
160 */
161 virtio_wmb();
162 vq->avail_idx_shadow++;
163 vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow);
164 vq->num_added++;
165
166 /*
167 * This is very unlikely, but theoretically possible.
168 * Kick just in case.
169 */
170 if (unlikely(vq->num_added == (1 << 16) - 1))
171 virtqueue_kick(vq);
172
173 return 0;
174}
175
176static bool virtqueue_kick_prepare(struct virtqueue *vq)
177{
178 u16 new, old;
179 bool needs_kick;
180
181 /*
182 * We need to expose available array entries before checking
183 * avail event.
184 */
185 virtio_mb();
186
187 old = vq->avail_idx_shadow - vq->num_added;
188 new = vq->avail_idx_shadow;
189 vq->num_added = 0;
190
191 if (vq->event) {
192 needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev,
193 vring_avail_event(&vq->vring)), new, old);
194 } else {
195 needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev,
196 VRING_USED_F_NO_NOTIFY));
197 }
198
199 return needs_kick;
200}
201
202void virtqueue_kick(struct virtqueue *vq)
203{
204 if (virtqueue_kick_prepare(vq))
205 virtio_notify(vq->vdev, vq);
206}
207
208static void detach_buf(struct virtqueue *vq, unsigned int head)
209{
210 unsigned int i;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700211
Andrew Scullfbef3f52022-05-16 10:41:32 +0000212 /* Unmark the descriptor as the head of a chain. */
213 vq->vring_desc_shadow[head].chain_head = false;
214
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700215 /* Put back on free list: unmap first-level descriptors and find end */
216 i = head;
217
Andrew Scull10a14532022-05-16 10:41:31 +0000218 while (vq->vring_desc_shadow[i].flags & VRING_DESC_F_NEXT) {
Will Deacona15d67e2023-03-29 22:24:58 +0800219 virtqueue_detach_desc(vq, i);
Andrew Scull10a14532022-05-16 10:41:31 +0000220 i = vq->vring_desc_shadow[i].next;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700221 vq->num_free++;
222 }
223
Will Deacona15d67e2023-03-29 22:24:58 +0800224 virtqueue_detach_desc(vq, i);
Andrew Scull10a14532022-05-16 10:41:31 +0000225 vq->vring_desc_shadow[i].next = vq->free_head;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700226 vq->free_head = head;
227
228 /* Plus final descriptor */
229 vq->num_free++;
230}
231
232static inline bool more_used(const struct virtqueue *vq)
233{
234 return vq->last_used_idx != virtio16_to_cpu(vq->vdev,
235 vq->vring.used->idx);
236}
237
238void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len)
239{
240 unsigned int i;
241 u16 last_used;
242
243 if (!more_used(vq)) {
244 debug("(%s.%d): No more buffers in queue\n",
245 vq->vdev->name, vq->index);
246 return NULL;
247 }
248
249 /* Only get used array entries after they have been exposed by host */
250 virtio_rmb();
251
252 last_used = (vq->last_used_idx & (vq->vring.num - 1));
253 i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id);
254 if (len) {
255 *len = virtio32_to_cpu(vq->vdev,
256 vq->vring.used->ring[last_used].len);
257 debug("(%s.%d): last used idx %u with len %u\n",
258 vq->vdev->name, vq->index, i, *len);
259 }
260
261 if (unlikely(i >= vq->vring.num)) {
262 printf("(%s.%d): id %u out of range\n",
263 vq->vdev->name, vq->index, i);
264 return NULL;
265 }
266
Andrew Scullfbef3f52022-05-16 10:41:32 +0000267 if (unlikely(!vq->vring_desc_shadow[i].chain_head)) {
268 printf("(%s.%d): id %u is not a head\n",
269 vq->vdev->name, vq->index, i);
270 return NULL;
271 }
272
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700273 detach_buf(vq, i);
274 vq->last_used_idx++;
275 /*
276 * If we expect an interrupt for the next entry, tell host
277 * by writing event index and flush out the write before
278 * the read in the next get_buf call.
279 */
280 if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
281 virtio_store_mb(&vring_used_event(&vq->vring),
282 cpu_to_virtio16(vq->vdev, vq->last_used_idx));
283
Andrew Scull10a14532022-05-16 10:41:31 +0000284 return (void *)(uintptr_t)vq->vring_desc_shadow[i].addr;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700285}
286
287static struct virtqueue *__vring_new_virtqueue(unsigned int index,
288 struct vring vring,
289 struct udevice *udev)
290{
291 unsigned int i;
292 struct virtqueue *vq;
Andrew Scull10a14532022-05-16 10:41:31 +0000293 struct vring_desc_shadow *vring_desc_shadow;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700294 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
295 struct udevice *vdev = uc_priv->vdev;
296
297 vq = malloc(sizeof(*vq));
298 if (!vq)
299 return NULL;
300
Andrew Scull10a14532022-05-16 10:41:31 +0000301 vring_desc_shadow = calloc(vring.num, sizeof(struct vring_desc_shadow));
302 if (!vring_desc_shadow) {
303 free(vq);
304 return NULL;
305 }
306
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700307 vq->vdev = vdev;
308 vq->index = index;
309 vq->num_free = vring.num;
310 vq->vring = vring;
Andrew Scull10a14532022-05-16 10:41:31 +0000311 vq->vring_desc_shadow = vring_desc_shadow;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700312 vq->last_used_idx = 0;
313 vq->avail_flags_shadow = 0;
314 vq->avail_idx_shadow = 0;
315 vq->num_added = 0;
316 list_add_tail(&vq->list, &uc_priv->vqs);
317
318 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
319
320 /* Tell other side not to bother us */
321 vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
322 if (!vq->event)
323 vq->vring.avail->flags = cpu_to_virtio16(vdev,
324 vq->avail_flags_shadow);
325
326 /* Put everything in free lists */
327 vq->free_head = 0;
328 for (i = 0; i < vring.num - 1; i++)
Andrew Scull10a14532022-05-16 10:41:31 +0000329 vq->vring_desc_shadow[i].next = i + 1;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700330
331 return vq;
332}
333
334struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
335 unsigned int vring_align,
336 struct udevice *udev)
337{
Will Deacon75582fc2023-03-29 22:24:57 +0800338 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
339 struct udevice *vdev = uc_priv->vdev;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700340 struct virtqueue *vq;
341 void *queue = NULL;
Will Deacon37e53db2023-03-29 22:24:59 +0800342 struct bounce_buffer *bbs = NULL;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700343 struct vring vring;
344
345 /* We assume num is a power of 2 */
346 if (num & (num - 1)) {
347 printf("Bad virtqueue length %u\n", num);
348 return NULL;
349 }
350
351 /* TODO: allocate each queue chunk individually */
352 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
Will Deacon75582fc2023-03-29 22:24:57 +0800353 size_t sz = vring_size(num, vring_align);
354
355 queue = virtio_alloc_pages(vdev, DIV_ROUND_UP(sz, PAGE_SIZE));
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700356 if (queue)
357 break;
358 }
359
360 if (!num)
361 return NULL;
362
363 if (!queue) {
364 /* Try to get a single page. You are my only hope! */
Will Deacon75582fc2023-03-29 22:24:57 +0800365 queue = virtio_alloc_pages(vdev, 1);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700366 }
367 if (!queue)
368 return NULL;
369
370 memset(queue, 0, vring_size(num, vring_align));
Will Deacon37e53db2023-03-29 22:24:59 +0800371
372 if (virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
373 bbs = calloc(num, sizeof(*bbs));
374 if (!bbs)
375 goto err_free_queue;
376 }
377
378 vring_init(&vring, num, queue, vring_align, bbs);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700379
380 vq = __vring_new_virtqueue(index, vring, udev);
Will Deacon37e53db2023-03-29 22:24:59 +0800381 if (!vq)
382 goto err_free_bbs;
383
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700384 debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name,
385 queue, vq, num);
386
387 return vq;
Will Deacon37e53db2023-03-29 22:24:59 +0800388
389err_free_bbs:
390 free(bbs);
391err_free_queue:
392 virtio_free_pages(vdev, queue, DIV_ROUND_UP(vring.size, PAGE_SIZE));
393 return NULL;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700394}
395
396void vring_del_virtqueue(struct virtqueue *vq)
397{
Will Deacon75582fc2023-03-29 22:24:57 +0800398 virtio_free_pages(vq->vdev, vq->vring.desc,
399 DIV_ROUND_UP(vq->vring.size, PAGE_SIZE));
Andrew Scull10a14532022-05-16 10:41:31 +0000400 free(vq->vring_desc_shadow);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700401 list_del(&vq->list);
Will Deacon37e53db2023-03-29 22:24:59 +0800402 free(vq->vring.bouncebufs);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700403 free(vq);
404}
405
406unsigned int virtqueue_get_vring_size(struct virtqueue *vq)
407{
408 return vq->vring.num;
409}
410
411ulong virtqueue_get_desc_addr(struct virtqueue *vq)
412{
413 return (ulong)vq->vring.desc;
414}
415
416ulong virtqueue_get_avail_addr(struct virtqueue *vq)
417{
418 return (ulong)vq->vring.desc +
419 ((char *)vq->vring.avail - (char *)vq->vring.desc);
420}
421
422ulong virtqueue_get_used_addr(struct virtqueue *vq)
423{
424 return (ulong)vq->vring.desc +
425 ((char *)vq->vring.used - (char *)vq->vring.desc);
426}
427
428bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx)
429{
430 virtio_mb();
431
432 return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx);
433}
434
435void virtqueue_dump(struct virtqueue *vq)
436{
437 unsigned int i;
438
439 printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name);
440 printf("\tindex %u, phys addr %p num %u\n",
441 vq->index, vq->vring.desc, vq->vring.num);
442 printf("\tfree_head %u, num_added %u, num_free %u\n",
443 vq->free_head, vq->num_added, vq->num_free);
444 printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n",
445 vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow);
446
Andrew Scull10a14532022-05-16 10:41:31 +0000447 printf("Shadow descriptor dump:\n");
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700448 for (i = 0; i < vq->vring.num; i++) {
Andrew Scull10a14532022-05-16 10:41:31 +0000449 struct vring_desc_shadow *desc = &vq->vring_desc_shadow[i];
450
451 printf("\tdesc_shadow[%u] = { 0x%llx, len %u, flags %u, next %u }\n",
452 i, desc->addr, desc->len, desc->flags, desc->next);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700453 }
454
455 printf("Avail ring dump:\n");
456 printf("\tflags %u, idx %u\n",
457 vq->vring.avail->flags, vq->vring.avail->idx);
458 for (i = 0; i < vq->vring.num; i++) {
459 printf("\tavail[%u] = %u\n",
460 i, vq->vring.avail->ring[i]);
461 }
462
463 printf("Used ring dump:\n");
464 printf("\tflags %u, idx %u\n",
465 vq->vring.used->flags, vq->vring.used->idx);
466 for (i = 0; i < vq->vring.num; i++) {
467 printf("\tused[%u] = { %u, %u }\n", i,
468 vq->vring.used->ring[i].id, vq->vring.used->ring[i].len);
469 }
470}