Remove vring buffer more_req checking
[qemu/ovp.git] / hw / virtio.c
blob0bd97c5d9dcb1e0c34748816d31593444de70803
1 /*
2 * Virtio Support
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include <inttypes.h>
16 #include "virtio.h"
17 #include "sysemu.h"
19 /* The alignment to use between consumer and producer parts of vring.
20 * x86 pagesize again. */
21 #define VIRTIO_PCI_VRING_ALIGN 4096
23 /* QEMU doesn't strictly need write barriers since everything runs in
24 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
25 * KVM or if kqemu gets SMP support.
26 * In any case, we must prevent the compiler from reordering the code.
27 * TODO: we likely need some rmb()/mb() as well.
30 #define wmb() __asm__ __volatile__("": : :"memory")
32 typedef struct VRingDesc
34 uint64_t addr;
35 uint32_t len;
36 uint16_t flags;
37 uint16_t next;
38 } VRingDesc;
40 typedef struct VRingAvail
42 uint16_t flags;
43 uint16_t idx;
44 uint16_t ring[0];
45 } VRingAvail;
47 typedef struct VRingUsedElem
49 uint32_t id;
50 uint32_t len;
51 } VRingUsedElem;
53 typedef struct VRingUsed
55 uint16_t flags;
56 uint16_t idx;
57 VRingUsedElem ring[0];
58 } VRingUsed;
60 typedef struct VRing
62 unsigned int num;
63 target_phys_addr_t desc;
64 target_phys_addr_t avail;
65 target_phys_addr_t used;
66 } VRing;
68 struct VirtQueue
70 VRing vring;
71 target_phys_addr_t pa;
72 uint16_t last_avail_idx;
73 int inuse;
74 uint16_t vector;
75 void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
76 VirtIODevice *vdev;
77 EventNotifier guest_notifier;
78 EventNotifier host_notifier;
81 /* virt queue functions */
82 static void virtqueue_init(VirtQueue *vq)
84 target_phys_addr_t pa = vq->pa;
86 vq->vring.desc = pa;
87 vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
88 vq->vring.used = vring_align(vq->vring.avail +
89 offsetof(VRingAvail, ring[vq->vring.num]),
90 VIRTIO_PCI_VRING_ALIGN);
93 static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa, int i)
95 target_phys_addr_t pa;
96 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
97 return ldq_phys(pa);
100 static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa, int i)
102 target_phys_addr_t pa;
103 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
104 return ldl_phys(pa);
107 static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa, int i)
109 target_phys_addr_t pa;
110 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
111 return lduw_phys(pa);
114 static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa, int i)
116 target_phys_addr_t pa;
117 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
118 return lduw_phys(pa);
121 static inline uint16_t vring_avail_flags(VirtQueue *vq)
123 target_phys_addr_t pa;
124 pa = vq->vring.avail + offsetof(VRingAvail, flags);
125 return lduw_phys(pa);
128 static inline uint16_t vring_avail_idx(VirtQueue *vq)
130 target_phys_addr_t pa;
131 pa = vq->vring.avail + offsetof(VRingAvail, idx);
132 return lduw_phys(pa);
135 static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
137 target_phys_addr_t pa;
138 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
139 return lduw_phys(pa);
142 static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val)
144 target_phys_addr_t pa;
145 pa = vq->vring.used + offsetof(VRingUsed, ring[i].id);
146 stl_phys(pa, val);
149 static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val)
151 target_phys_addr_t pa;
152 pa = vq->vring.used + offsetof(VRingUsed, ring[i].len);
153 stl_phys(pa, val);
156 static uint16_t vring_used_idx(VirtQueue *vq)
158 target_phys_addr_t pa;
159 pa = vq->vring.used + offsetof(VRingUsed, idx);
160 return lduw_phys(pa);
163 static inline void vring_used_idx_increment(VirtQueue *vq, uint16_t val)
165 target_phys_addr_t pa;
167 pa = vq->vring.used + offsetof(VRingUsed, idx);
169 stw_phys(pa, vring_used_idx(vq) + val);
172 static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
174 target_phys_addr_t pa;
175 pa = vq->vring.used + offsetof(VRingUsed, flags);
176 stw_phys(pa, lduw_phys(pa) | mask);
179 static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
181 target_phys_addr_t pa;
182 pa = vq->vring.used + offsetof(VRingUsed, flags);
183 stw_phys(pa, lduw_phys(pa) & ~mask);
186 void virtio_queue_set_notification(VirtQueue *vq, int enable)
188 if (enable)
189 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
190 else
191 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
194 int virtio_queue_ready(VirtQueue *vq)
196 return vq->vring.avail != 0;
199 int virtio_queue_empty(VirtQueue *vq)
201 return vring_avail_idx(vq) == vq->last_avail_idx;
204 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
205 unsigned int len, unsigned int idx)
207 unsigned int offset;
208 int i;
210 offset = 0;
211 for (i = 0; i < elem->in_num; i++) {
212 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
214 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
215 elem->in_sg[i].iov_len,
216 1, size);
218 offset += elem->in_sg[i].iov_len;
221 for (i = 0; i < elem->out_num; i++)
222 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
223 elem->out_sg[i].iov_len,
224 0, elem->out_sg[i].iov_len);
226 idx = (idx + vring_used_idx(vq)) % vq->vring.num;
228 /* Get a pointer to the next entry in the used ring. */
229 vring_used_ring_id(vq, idx, elem->index);
230 vring_used_ring_len(vq, idx, len);
233 void virtqueue_flush(VirtQueue *vq, unsigned int count)
235 /* Make sure buffer is written before we update index. */
236 wmb();
237 vring_used_idx_increment(vq, count);
238 vq->inuse -= count;
241 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
242 unsigned int len)
244 virtqueue_fill(vq, elem, len, 0);
245 virtqueue_flush(vq, 1);
248 static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
250 uint16_t num_heads = vring_avail_idx(vq) - idx;
252 /* Check it isn't doing very strange things with descriptor numbers. */
253 if (num_heads > vq->vring.num) {
254 fprintf(stderr, "Guest moved used index from %u to %u",
255 idx, vring_avail_idx(vq));
256 exit(1);
259 return num_heads;
262 static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
264 unsigned int head;
266 /* Grab the next descriptor number they're advertising, and increment
267 * the index we've seen. */
268 head = vring_avail_ring(vq, idx % vq->vring.num);
270 /* If their number is silly, that's a fatal mistake. */
271 if (head >= vq->vring.num) {
272 fprintf(stderr, "Guest says index %u is available", head);
273 exit(1);
276 return head;
279 static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa,
280 unsigned int i, unsigned int max)
282 unsigned int next;
284 /* If this descriptor says it doesn't chain, we're done. */
285 if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT))
286 return max;
288 /* Check they're not leading us off end of descriptors. */
289 next = vring_desc_next(desc_pa, i);
290 /* Make sure compiler knows to grab that: we don't want it changing! */
291 wmb();
293 if (next >= max) {
294 fprintf(stderr, "Desc next is %u", next);
295 exit(1);
298 return next;
301 int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes)
303 unsigned int idx;
304 int total_bufs, in_total, out_total;
306 idx = vq->last_avail_idx;
308 total_bufs = in_total = out_total = 0;
309 while (virtqueue_num_heads(vq, idx)) {
310 unsigned int max, num_bufs, indirect = 0;
311 target_phys_addr_t desc_pa;
312 int i;
314 max = vq->vring.num;
315 num_bufs = total_bufs;
316 i = virtqueue_get_head(vq, idx++);
317 desc_pa = vq->vring.desc;
319 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
320 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
321 fprintf(stderr, "Invalid size for indirect buffer table\n");
322 exit(1);
325 /* If we've got too many, that implies a descriptor loop. */
326 if (num_bufs >= max) {
327 fprintf(stderr, "Looped descriptor");
328 exit(1);
331 /* loop over the indirect descriptor table */
332 indirect = 1;
333 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
334 num_bufs = i = 0;
335 desc_pa = vring_desc_addr(desc_pa, i);
338 do {
339 /* If we've got too many, that implies a descriptor loop. */
340 if (++num_bufs > max) {
341 fprintf(stderr, "Looped descriptor");
342 exit(1);
345 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
346 if (in_bytes > 0 &&
347 (in_total += vring_desc_len(desc_pa, i)) >= in_bytes)
348 return 1;
349 } else {
350 if (out_bytes > 0 &&
351 (out_total += vring_desc_len(desc_pa, i)) >= out_bytes)
352 return 1;
354 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
356 if (!indirect)
357 total_bufs = num_bufs;
358 else
359 total_bufs++;
362 return 0;
365 void virtqueue_map_sg(struct iovec *sg, target_phys_addr_t *addr,
366 size_t num_sg, int is_write)
368 unsigned int i;
369 target_phys_addr_t len;
371 for (i = 0; i < num_sg; i++) {
372 len = sg[i].iov_len;
373 sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
374 if (sg[i].iov_base == NULL || len != sg[i].iov_len) {
375 fprintf(stderr, "virtio: trying to map MMIO memory\n");
376 exit(1);
381 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
383 unsigned int i, head, max;
384 target_phys_addr_t desc_pa = vq->vring.desc;
386 if (!virtqueue_num_heads(vq, vq->last_avail_idx))
387 return 0;
389 /* When we start there are none of either input nor output. */
390 elem->out_num = elem->in_num = 0;
392 max = vq->vring.num;
394 i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
396 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
397 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
398 fprintf(stderr, "Invalid size for indirect buffer table\n");
399 exit(1);
402 /* loop over the indirect descriptor table */
403 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
404 desc_pa = vring_desc_addr(desc_pa, i);
405 i = 0;
408 /* Collect all the descriptors */
409 do {
410 struct iovec *sg;
412 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
413 elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i);
414 sg = &elem->in_sg[elem->in_num++];
415 } else {
416 elem->out_addr[elem->out_num] = vring_desc_addr(desc_pa, i);
417 sg = &elem->out_sg[elem->out_num++];
420 sg->iov_len = vring_desc_len(desc_pa, i);
422 /* If we've got too many, that implies a descriptor loop. */
423 if ((elem->in_num + elem->out_num) > max) {
424 fprintf(stderr, "Looped descriptor");
425 exit(1);
427 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
429 /* Now map what we have collected */
430 virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
431 virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);
433 elem->index = head;
435 vq->inuse++;
437 return elem->in_num + elem->out_num;
440 /* virtio device */
441 static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
443 if (vdev->binding->notify) {
444 vdev->binding->notify(vdev->binding_opaque, vector);
448 void virtio_update_irq(VirtIODevice *vdev)
450 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
453 void virtio_reset(void *opaque)
455 VirtIODevice *vdev = opaque;
456 int i;
458 virtio_set_status(vdev, 0);
460 if (vdev->reset)
461 vdev->reset(vdev);
463 vdev->guest_features = 0;
464 vdev->queue_sel = 0;
465 vdev->status = 0;
466 vdev->isr = 0;
467 vdev->config_vector = VIRTIO_NO_VECTOR;
468 virtio_notify_vector(vdev, vdev->config_vector);
470 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
471 vdev->vq[i].vring.desc = 0;
472 vdev->vq[i].vring.avail = 0;
473 vdev->vq[i].vring.used = 0;
474 vdev->vq[i].last_avail_idx = 0;
475 vdev->vq[i].pa = 0;
476 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
480 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
482 uint8_t val;
484 vdev->get_config(vdev, vdev->config);
486 if (addr > (vdev->config_len - sizeof(val)))
487 return (uint32_t)-1;
489 memcpy(&val, vdev->config + addr, sizeof(val));
490 return val;
493 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
495 uint16_t val;
497 vdev->get_config(vdev, vdev->config);
499 if (addr > (vdev->config_len - sizeof(val)))
500 return (uint32_t)-1;
502 memcpy(&val, vdev->config + addr, sizeof(val));
503 return val;
506 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
508 uint32_t val;
510 vdev->get_config(vdev, vdev->config);
512 if (addr > (vdev->config_len - sizeof(val)))
513 return (uint32_t)-1;
515 memcpy(&val, vdev->config + addr, sizeof(val));
516 return val;
519 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
521 uint8_t val = data;
523 if (addr > (vdev->config_len - sizeof(val)))
524 return;
526 memcpy(vdev->config + addr, &val, sizeof(val));
528 if (vdev->set_config)
529 vdev->set_config(vdev, vdev->config);
532 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
534 uint16_t val = data;
536 if (addr > (vdev->config_len - sizeof(val)))
537 return;
539 memcpy(vdev->config + addr, &val, sizeof(val));
541 if (vdev->set_config)
542 vdev->set_config(vdev, vdev->config);
545 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
547 uint32_t val = data;
549 if (addr > (vdev->config_len - sizeof(val)))
550 return;
552 memcpy(vdev->config + addr, &val, sizeof(val));
554 if (vdev->set_config)
555 vdev->set_config(vdev, vdev->config);
558 void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr)
560 vdev->vq[n].pa = addr;
561 virtqueue_init(&vdev->vq[n]);
564 target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n)
566 return vdev->vq[n].pa;
569 int virtio_queue_get_num(VirtIODevice *vdev, int n)
571 return vdev->vq[n].vring.num;
574 void virtio_queue_notify(VirtIODevice *vdev, int n)
576 if (n < VIRTIO_PCI_QUEUE_MAX && vdev->vq[n].vring.desc) {
577 vdev->vq[n].handle_output(vdev, &vdev->vq[n]);
581 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
583 return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector :
584 VIRTIO_NO_VECTOR;
587 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
589 if (n < VIRTIO_PCI_QUEUE_MAX)
590 vdev->vq[n].vector = vector;
593 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
594 void (*handle_output)(VirtIODevice *, VirtQueue *))
596 int i;
598 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
599 if (vdev->vq[i].vring.num == 0)
600 break;
603 if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
604 abort();
606 vdev->vq[i].vring.num = queue_size;
607 vdev->vq[i].handle_output = handle_output;
609 return &vdev->vq[i];
612 void virtio_irq(VirtQueue *vq)
614 vq->vdev->isr |= 0x01;
615 virtio_notify_vector(vq->vdev, vq->vector);
618 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
620 /* Always notify when queue is empty (when feature acknowledge) */
621 if ((vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT) &&
622 (!(vdev->guest_features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) ||
623 (vq->inuse || vring_avail_idx(vq) != vq->last_avail_idx)))
624 return;
626 vdev->isr |= 0x01;
627 virtio_notify_vector(vdev, vq->vector);
630 void virtio_notify_config(VirtIODevice *vdev)
632 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
633 return;
635 vdev->isr |= 0x03;
636 virtio_notify_vector(vdev, vdev->config_vector);
639 void virtio_save(VirtIODevice *vdev, QEMUFile *f)
641 int i;
643 if (vdev->binding->save_config)
644 vdev->binding->save_config(vdev->binding_opaque, f);
646 qemu_put_8s(f, &vdev->status);
647 qemu_put_8s(f, &vdev->isr);
648 qemu_put_be16s(f, &vdev->queue_sel);
649 qemu_put_be32s(f, &vdev->guest_features);
650 qemu_put_be32(f, vdev->config_len);
651 qemu_put_buffer(f, vdev->config, vdev->config_len);
653 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
654 if (vdev->vq[i].vring.num == 0)
655 break;
658 qemu_put_be32(f, i);
660 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
661 if (vdev->vq[i].vring.num == 0)
662 break;
664 qemu_put_be32(f, vdev->vq[i].vring.num);
665 qemu_put_be64(f, vdev->vq[i].pa);
666 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
667 if (vdev->binding->save_queue)
668 vdev->binding->save_queue(vdev->binding_opaque, i, f);
672 int virtio_load(VirtIODevice *vdev, QEMUFile *f)
674 int num, i, ret;
675 uint32_t features;
676 uint32_t supported_features =
677 vdev->binding->get_features(vdev->binding_opaque);
679 if (vdev->binding->load_config) {
680 ret = vdev->binding->load_config(vdev->binding_opaque, f);
681 if (ret)
682 return ret;
685 qemu_get_8s(f, &vdev->status);
686 qemu_get_8s(f, &vdev->isr);
687 qemu_get_be16s(f, &vdev->queue_sel);
688 qemu_get_be32s(f, &features);
689 if (features & ~supported_features) {
690 fprintf(stderr, "Features 0x%x unsupported. Allowed features: 0x%x\n",
691 features, supported_features);
692 return -1;
694 if (vdev->set_features)
695 vdev->set_features(vdev, features);
696 vdev->guest_features = features;
697 vdev->config_len = qemu_get_be32(f);
698 qemu_get_buffer(f, vdev->config, vdev->config_len);
700 num = qemu_get_be32(f);
702 for (i = 0; i < num; i++) {
703 vdev->vq[i].vring.num = qemu_get_be32(f);
704 vdev->vq[i].pa = qemu_get_be64(f);
705 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
707 if (vdev->vq[i].pa) {
708 virtqueue_init(&vdev->vq[i]);
710 if (vdev->binding->load_queue) {
711 ret = vdev->binding->load_queue(vdev->binding_opaque, i, f);
712 if (ret)
713 return ret;
717 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
718 return 0;
721 void virtio_cleanup(VirtIODevice *vdev)
723 if (vdev->config)
724 qemu_free(vdev->config);
725 qemu_free(vdev->vq);
728 VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
729 size_t config_size, size_t struct_size)
731 VirtIODevice *vdev;
732 int i;
734 vdev = qemu_mallocz(struct_size);
736 vdev->device_id = device_id;
737 vdev->status = 0;
738 vdev->isr = 0;
739 vdev->queue_sel = 0;
740 vdev->config_vector = VIRTIO_NO_VECTOR;
741 vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
742 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
743 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
744 vdev->vq[i].vdev = vdev;
747 vdev->name = name;
748 vdev->config_len = config_size;
749 if (vdev->config_len)
750 vdev->config = qemu_mallocz(config_size);
751 else
752 vdev->config = NULL;
754 return vdev;
757 void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
758 void *opaque)
760 vdev->binding = binding;
761 vdev->binding_opaque = opaque;
764 target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
766 return vdev->vq[n].vring.desc;
769 target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
771 return vdev->vq[n].vring.avail;
774 target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
776 return vdev->vq[n].vring.used;
779 target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n)
781 return vdev->vq[n].vring.desc;
784 target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
786 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
789 target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
791 return offsetof(VRingAvail, ring) +
792 sizeof(uint64_t) * vdev->vq[n].vring.num;
795 target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n)
797 return offsetof(VRingUsed, ring) +
798 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
801 target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n)
803 return vdev->vq[n].vring.used - vdev->vq[n].vring.desc +
804 virtio_queue_get_used_size(vdev, n);
807 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
809 return vdev->vq[n].last_avail_idx;
812 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
814 vdev->vq[n].last_avail_idx = idx;
817 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
819 return vdev->vq + n;
822 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
824 return &vq->guest_notifier;
826 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
828 return &vq->host_notifier;