blob: 73671d79daffb2f1a5504f11f347f9e0970e8433 [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
9#include <common.h>
10#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070012#include <malloc.h>
13#include <virtio_types.h>
14#include <virtio.h>
15#include <virtio_ring.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060016#include <linux/bug.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <linux/compat.h>
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070018
Andrew Scullb0952972022-05-16 10:41:30 +000019static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i,
20 struct virtio_sg *sg, u16 flags)
21{
Andrew Scull10a14532022-05-16 10:41:31 +000022 struct vring_desc_shadow *desc_shadow = &vq->vring_desc_shadow[i];
Andrew Scullb0952972022-05-16 10:41:30 +000023 struct vring_desc *desc = &vq->vring.desc[i];
24
Andrew Scull10a14532022-05-16 10:41:31 +000025 /* Update the shadow descriptor. */
26 desc_shadow->addr = (u64)(uintptr_t)sg->addr;
27 desc_shadow->len = sg->length;
28 desc_shadow->flags = flags;
Andrew Scullb0952972022-05-16 10:41:30 +000029
Andrew Scull10a14532022-05-16 10:41:31 +000030 /* Update the shared descriptor to match the shadow. */
31 desc->addr = cpu_to_virtio64(vq->vdev, desc_shadow->addr);
32 desc->len = cpu_to_virtio32(vq->vdev, desc_shadow->len);
33 desc->flags = cpu_to_virtio16(vq->vdev, desc_shadow->flags);
34 desc->next = cpu_to_virtio16(vq->vdev, desc_shadow->next);
35
36 return desc_shadow->next;
Andrew Scullb0952972022-05-16 10:41:30 +000037}
38
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070039int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
40 unsigned int out_sgs, unsigned int in_sgs)
41{
42 struct vring_desc *desc;
Andrew Scull68f8bf22022-05-16 10:41:29 +000043 unsigned int descs_used = out_sgs + in_sgs;
44 unsigned int i, n, avail, uninitialized_var(prev);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070045 int head;
46
Andrew Scull68f8bf22022-05-16 10:41:29 +000047 WARN_ON(descs_used == 0);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070048
49 head = vq->free_head;
50
51 desc = vq->vring.desc;
52 i = head;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070053
54 if (vq->num_free < descs_used) {
55 debug("Can't add buf len %i - avail = %i\n",
56 descs_used, vq->num_free);
57 /*
58 * FIXME: for historical reasons, we force a notify here if
59 * there are outgoing parts to the buffer. Presumably the
60 * host should service the ring ASAP.
61 */
62 if (out_sgs)
63 virtio_notify(vq->vdev, vq);
64 return -ENOSPC;
65 }
66
Andrew Scullb0952972022-05-16 10:41:30 +000067 for (n = 0; n < descs_used; n++) {
68 u16 flags = VRING_DESC_F_NEXT;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070069
Andrew Scullb0952972022-05-16 10:41:30 +000070 if (n >= out_sgs)
71 flags |= VRING_DESC_F_WRITE;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070072 prev = i;
Andrew Scullb0952972022-05-16 10:41:30 +000073 i = virtqueue_attach_desc(vq, i, sgs[n], flags);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070074 }
75 /* Last one doesn't continue */
Andrew Scull10a14532022-05-16 10:41:31 +000076 vq->vring_desc_shadow[prev].flags &= ~VRING_DESC_F_NEXT;
77 desc[prev].flags = cpu_to_virtio16(vq->vdev, vq->vring_desc_shadow[prev].flags);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -070078
79 /* We're using some buffers from the free list. */
80 vq->num_free -= descs_used;
81
82 /* Update free pointer */
83 vq->free_head = i;
84
85 /*
86 * Put entry in available array (but don't update avail->idx
87 * until they do sync).
88 */
89 avail = vq->avail_idx_shadow & (vq->vring.num - 1);
90 vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head);
91
92 /*
93 * Descriptors and available array need to be set before we expose the
94 * new available array entries.
95 */
96 virtio_wmb();
97 vq->avail_idx_shadow++;
98 vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow);
99 vq->num_added++;
100
101 /*
102 * This is very unlikely, but theoretically possible.
103 * Kick just in case.
104 */
105 if (unlikely(vq->num_added == (1 << 16) - 1))
106 virtqueue_kick(vq);
107
108 return 0;
109}
110
111static bool virtqueue_kick_prepare(struct virtqueue *vq)
112{
113 u16 new, old;
114 bool needs_kick;
115
116 /*
117 * We need to expose available array entries before checking
118 * avail event.
119 */
120 virtio_mb();
121
122 old = vq->avail_idx_shadow - vq->num_added;
123 new = vq->avail_idx_shadow;
124 vq->num_added = 0;
125
126 if (vq->event) {
127 needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev,
128 vring_avail_event(&vq->vring)), new, old);
129 } else {
130 needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev,
131 VRING_USED_F_NO_NOTIFY));
132 }
133
134 return needs_kick;
135}
136
137void virtqueue_kick(struct virtqueue *vq)
138{
139 if (virtqueue_kick_prepare(vq))
140 virtio_notify(vq->vdev, vq);
141}
142
143static void detach_buf(struct virtqueue *vq, unsigned int head)
144{
145 unsigned int i;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700146
147 /* Put back on free list: unmap first-level descriptors and find end */
148 i = head;
149
Andrew Scull10a14532022-05-16 10:41:31 +0000150 while (vq->vring_desc_shadow[i].flags & VRING_DESC_F_NEXT) {
151 i = vq->vring_desc_shadow[i].next;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700152 vq->num_free++;
153 }
154
Andrew Scull10a14532022-05-16 10:41:31 +0000155 vq->vring_desc_shadow[i].next = vq->free_head;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700156 vq->free_head = head;
157
158 /* Plus final descriptor */
159 vq->num_free++;
160}
161
162static inline bool more_used(const struct virtqueue *vq)
163{
164 return vq->last_used_idx != virtio16_to_cpu(vq->vdev,
165 vq->vring.used->idx);
166}
167
168void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len)
169{
170 unsigned int i;
171 u16 last_used;
172
173 if (!more_used(vq)) {
174 debug("(%s.%d): No more buffers in queue\n",
175 vq->vdev->name, vq->index);
176 return NULL;
177 }
178
179 /* Only get used array entries after they have been exposed by host */
180 virtio_rmb();
181
182 last_used = (vq->last_used_idx & (vq->vring.num - 1));
183 i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id);
184 if (len) {
185 *len = virtio32_to_cpu(vq->vdev,
186 vq->vring.used->ring[last_used].len);
187 debug("(%s.%d): last used idx %u with len %u\n",
188 vq->vdev->name, vq->index, i, *len);
189 }
190
191 if (unlikely(i >= vq->vring.num)) {
192 printf("(%s.%d): id %u out of range\n",
193 vq->vdev->name, vq->index, i);
194 return NULL;
195 }
196
197 detach_buf(vq, i);
198 vq->last_used_idx++;
199 /*
200 * If we expect an interrupt for the next entry, tell host
201 * by writing event index and flush out the write before
202 * the read in the next get_buf call.
203 */
204 if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT))
205 virtio_store_mb(&vring_used_event(&vq->vring),
206 cpu_to_virtio16(vq->vdev, vq->last_used_idx));
207
Andrew Scull10a14532022-05-16 10:41:31 +0000208 return (void *)(uintptr_t)vq->vring_desc_shadow[i].addr;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700209}
210
211static struct virtqueue *__vring_new_virtqueue(unsigned int index,
212 struct vring vring,
213 struct udevice *udev)
214{
215 unsigned int i;
216 struct virtqueue *vq;
Andrew Scull10a14532022-05-16 10:41:31 +0000217 struct vring_desc_shadow *vring_desc_shadow;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700218 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
219 struct udevice *vdev = uc_priv->vdev;
220
221 vq = malloc(sizeof(*vq));
222 if (!vq)
223 return NULL;
224
Andrew Scull10a14532022-05-16 10:41:31 +0000225 vring_desc_shadow = calloc(vring.num, sizeof(struct vring_desc_shadow));
226 if (!vring_desc_shadow) {
227 free(vq);
228 return NULL;
229 }
230
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700231 vq->vdev = vdev;
232 vq->index = index;
233 vq->num_free = vring.num;
234 vq->vring = vring;
Andrew Scull10a14532022-05-16 10:41:31 +0000235 vq->vring_desc_shadow = vring_desc_shadow;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700236 vq->last_used_idx = 0;
237 vq->avail_flags_shadow = 0;
238 vq->avail_idx_shadow = 0;
239 vq->num_added = 0;
240 list_add_tail(&vq->list, &uc_priv->vqs);
241
242 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
243
244 /* Tell other side not to bother us */
245 vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT;
246 if (!vq->event)
247 vq->vring.avail->flags = cpu_to_virtio16(vdev,
248 vq->avail_flags_shadow);
249
250 /* Put everything in free lists */
251 vq->free_head = 0;
252 for (i = 0; i < vring.num - 1; i++)
Andrew Scull10a14532022-05-16 10:41:31 +0000253 vq->vring_desc_shadow[i].next = i + 1;
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700254
255 return vq;
256}
257
258struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
259 unsigned int vring_align,
260 struct udevice *udev)
261{
262 struct virtqueue *vq;
263 void *queue = NULL;
264 struct vring vring;
265
266 /* We assume num is a power of 2 */
267 if (num & (num - 1)) {
268 printf("Bad virtqueue length %u\n", num);
269 return NULL;
270 }
271
272 /* TODO: allocate each queue chunk individually */
273 for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) {
274 queue = memalign(PAGE_SIZE, vring_size(num, vring_align));
275 if (queue)
276 break;
277 }
278
279 if (!num)
280 return NULL;
281
282 if (!queue) {
283 /* Try to get a single page. You are my only hope! */
284 queue = memalign(PAGE_SIZE, vring_size(num, vring_align));
285 }
286 if (!queue)
287 return NULL;
288
289 memset(queue, 0, vring_size(num, vring_align));
290 vring_init(&vring, num, queue, vring_align);
291
292 vq = __vring_new_virtqueue(index, vring, udev);
293 if (!vq) {
294 free(queue);
295 return NULL;
296 }
297 debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name,
298 queue, vq, num);
299
300 return vq;
301}
302
303void vring_del_virtqueue(struct virtqueue *vq)
304{
305 free(vq->vring.desc);
Andrew Scull10a14532022-05-16 10:41:31 +0000306 free(vq->vring_desc_shadow);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700307 list_del(&vq->list);
308 free(vq);
309}
310
311unsigned int virtqueue_get_vring_size(struct virtqueue *vq)
312{
313 return vq->vring.num;
314}
315
316ulong virtqueue_get_desc_addr(struct virtqueue *vq)
317{
318 return (ulong)vq->vring.desc;
319}
320
321ulong virtqueue_get_avail_addr(struct virtqueue *vq)
322{
323 return (ulong)vq->vring.desc +
324 ((char *)vq->vring.avail - (char *)vq->vring.desc);
325}
326
327ulong virtqueue_get_used_addr(struct virtqueue *vq)
328{
329 return (ulong)vq->vring.desc +
330 ((char *)vq->vring.used - (char *)vq->vring.desc);
331}
332
333bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx)
334{
335 virtio_mb();
336
337 return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx);
338}
339
340void virtqueue_dump(struct virtqueue *vq)
341{
342 unsigned int i;
343
344 printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name);
345 printf("\tindex %u, phys addr %p num %u\n",
346 vq->index, vq->vring.desc, vq->vring.num);
347 printf("\tfree_head %u, num_added %u, num_free %u\n",
348 vq->free_head, vq->num_added, vq->num_free);
349 printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n",
350 vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow);
351
Andrew Scull10a14532022-05-16 10:41:31 +0000352 printf("Shadow descriptor dump:\n");
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700353 for (i = 0; i < vq->vring.num; i++) {
Andrew Scull10a14532022-05-16 10:41:31 +0000354 struct vring_desc_shadow *desc = &vq->vring_desc_shadow[i];
355
356 printf("\tdesc_shadow[%u] = { 0x%llx, len %u, flags %u, next %u }\n",
357 i, desc->addr, desc->len, desc->flags, desc->next);
Tuomas Tynkkynenc0116412018-10-15 02:21:01 -0700358 }
359
360 printf("Avail ring dump:\n");
361 printf("\tflags %u, idx %u\n",
362 vq->vring.avail->flags, vq->vring.avail->idx);
363 for (i = 0; i < vq->vring.num; i++) {
364 printf("\tavail[%u] = %u\n",
365 i, vq->vring.avail->ring[i]);
366 }
367
368 printf("Used ring dump:\n");
369 printf("\tflags %u, idx %u\n",
370 vq->vring.used->flags, vq->vring.used->idx);
371 for (i = 0; i < vq->vring.num; i++) {
372 printf("\tused[%u] = { %u, %u }\n", i,
373 vq->vring.used->ring[i].id, vq->vring.used->ring[i].len);
374 }
375}