1 // 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 <bouncebuf.h>
10 #include <common.h>
11 #include <dm.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <virtio_types.h>
15 #include <virtio.h>
16 #include <virtio_ring.h>
17 #include <linux/bug.h>
18 #include <linux/compat.h>
19 #include <linux/kernel.h>
20
virtio_alloc_pages(struct udevice * vdev,u32 npages)21 static void *virtio_alloc_pages(struct udevice *vdev, u32 npages)
22 {
23 return memalign(PAGE_SIZE, npages * PAGE_SIZE);
24 }
25
virtio_free_pages(struct udevice * vdev,void * ptr,u32 npages)26 static void virtio_free_pages(struct udevice *vdev, void *ptr, u32 npages)
27 {
28 free(ptr);
29 }
30
__bb_force_page_align(struct bounce_buffer * state)31 static 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
virtqueue_attach_desc(struct virtqueue * vq,unsigned int i,struct virtio_sg * sg,u16 flags)44 static unsigned int virtqueue_attach_desc(struct virtqueue *vq, unsigned int i,
45 struct virtio_sg *sg, u16 flags)
46 {
47 struct vring_desc_shadow *desc_shadow = &vq->vring_desc_shadow[i];
48 struct vring_desc *desc = &vq->vring.desc[i];
49 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 }
73
74 /* Update the shadow descriptor. */
75 desc_shadow->addr = (u64)(uintptr_t)addr;
76 desc_shadow->len = sg->length;
77 desc_shadow->flags = flags;
78
79 /* 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;
86 }
87
virtqueue_detach_desc(struct virtqueue * vq,unsigned int idx)88 static void virtqueue_detach_desc(struct virtqueue *vq, unsigned int idx)
89 {
90 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);
99 }
100
virtqueue_add(struct virtqueue * vq,struct virtio_sg * sgs[],unsigned int out_sgs,unsigned int in_sgs)101 int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[],
102 unsigned int out_sgs, unsigned int in_sgs)
103 {
104 struct vring_desc *desc;
105 unsigned int descs_used = out_sgs + in_sgs;
106 unsigned int i, n, avail, uninitialized_var(prev);
107 int head;
108
109 WARN_ON(descs_used == 0);
110
111 head = vq->free_head;
112
113 desc = vq->vring.desc;
114 i = head;
115
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
129 for (n = 0; n < descs_used; n++) {
130 u16 flags = VRING_DESC_F_NEXT;
131
132 if (n >= out_sgs)
133 flags |= VRING_DESC_F_WRITE;
134 prev = i;
135 i = virtqueue_attach_desc(vq, i, sgs[n], flags);
136 }
137 /* Last one doesn't continue */
138 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);
140
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
147 /* Mark the descriptor as the head of a chain. */
148 vq->vring_desc_shadow[head].chain_head = true;
149
150 /*
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
virtqueue_kick_prepare(struct virtqueue * vq)176 static 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
virtqueue_kick(struct virtqueue * vq)202 void virtqueue_kick(struct virtqueue *vq)
203 {
204 if (virtqueue_kick_prepare(vq))
205 virtio_notify(vq->vdev, vq);
206 }
207
detach_buf(struct virtqueue * vq,unsigned int head)208 static void detach_buf(struct virtqueue *vq, unsigned int head)
209 {
210 unsigned int i;
211
212 /* Unmark the descriptor as the head of a chain. */
213 vq->vring_desc_shadow[head].chain_head = false;
214
215 /* Put back on free list: unmap first-level descriptors and find end */
216 i = head;
217
218 while (vq->vring_desc_shadow[i].flags & VRING_DESC_F_NEXT) {
219 virtqueue_detach_desc(vq, i);
220 i = vq->vring_desc_shadow[i].next;
221 vq->num_free++;
222 }
223
224 virtqueue_detach_desc(vq, i);
225 vq->vring_desc_shadow[i].next = vq->free_head;
226 vq->free_head = head;
227
228 /* Plus final descriptor */
229 vq->num_free++;
230 }
231
more_used(const struct virtqueue * vq)232 static 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
virtqueue_get_buf(struct virtqueue * vq,unsigned int * len)238 void *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
267 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
273 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
284 return (void *)(uintptr_t)vq->vring_desc_shadow[i].addr;
285 }
286
__vring_new_virtqueue(unsigned int index,struct vring vring,struct udevice * udev)287 static 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;
293 struct vring_desc_shadow *vring_desc_shadow;
294 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
301 vring_desc_shadow = calloc(vring.num, sizeof(struct vring_desc_shadow));
302 if (!vring_desc_shadow) {
303 free(vq);
304 return NULL;
305 }
306
307 vq->vdev = vdev;
308 vq->index = index;
309 vq->num_free = vring.num;
310 vq->vring = vring;
311 vq->vring_desc_shadow = vring_desc_shadow;
312 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++)
329 vq->vring_desc_shadow[i].next = i + 1;
330
331 return vq;
332 }
333
vring_create_virtqueue(unsigned int index,unsigned int num,unsigned int vring_align,struct udevice * udev)334 struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num,
335 unsigned int vring_align,
336 struct udevice *udev)
337 {
338 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
339 struct udevice *vdev = uc_priv->vdev;
340 struct virtqueue *vq;
341 void *queue = NULL;
342 struct bounce_buffer *bbs = NULL;
343 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) {
353 size_t sz = vring_size(num, vring_align);
354
355 queue = virtio_alloc_pages(vdev, DIV_ROUND_UP(sz, PAGE_SIZE));
356 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! */
365 queue = virtio_alloc_pages(vdev, 1);
366 }
367 if (!queue)
368 return NULL;
369
370 memset(queue, 0, vring_size(num, vring_align));
371
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);
379
380 vq = __vring_new_virtqueue(index, vring, udev);
381 if (!vq)
382 goto err_free_bbs;
383
384 debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name,
385 queue, vq, num);
386
387 return vq;
388
389 err_free_bbs:
390 free(bbs);
391 err_free_queue:
392 virtio_free_pages(vdev, queue, DIV_ROUND_UP(vring.size, PAGE_SIZE));
393 return NULL;
394 }
395
vring_del_virtqueue(struct virtqueue * vq)396 void vring_del_virtqueue(struct virtqueue *vq)
397 {
398 virtio_free_pages(vq->vdev, vq->vring.desc,
399 DIV_ROUND_UP(vq->vring.size, PAGE_SIZE));
400 free(vq->vring_desc_shadow);
401 list_del(&vq->list);
402 free(vq->vring.bouncebufs);
403 free(vq);
404 }
405
virtqueue_get_vring_size(struct virtqueue * vq)406 unsigned int virtqueue_get_vring_size(struct virtqueue *vq)
407 {
408 return vq->vring.num;
409 }
410
virtqueue_get_desc_addr(struct virtqueue * vq)411 ulong virtqueue_get_desc_addr(struct virtqueue *vq)
412 {
413 return (ulong)vq->vring.desc;
414 }
415
virtqueue_get_avail_addr(struct virtqueue * vq)416 ulong 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
virtqueue_get_used_addr(struct virtqueue * vq)422 ulong 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
virtqueue_poll(struct virtqueue * vq,u16 last_used_idx)428 bool 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
virtqueue_dump(struct virtqueue * vq)435 void 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
447 printf("Shadow descriptor dump:\n");
448 for (i = 0; i < vq->vring.num; i++) {
449 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);
453 }
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 }
471