4 * Copyright IBM, Corp. 2007
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.
17 #include "qemu-error.h"
21 /* The alignment to use between consumer and producer parts of vring.
22 * x86 pagesize again. */
23 #define VIRTIO_PCI_VRING_ALIGN 4096
25 /* QEMU doesn't strictly need write barriers since everything runs in
26 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
27 * KVM or if kqemu gets SMP support.
28 * In any case, we must prevent the compiler from reordering the code.
29 * TODO: we likely need some rmb()/mb() as well.
32 #define wmb() __asm__ __volatile__("": : :"memory")
34 typedef struct VRingDesc
42 typedef struct VRingAvail
49 typedef struct VRingUsedElem
55 typedef struct VRingUsed
59 VRingUsedElem ring
[0];
65 target_phys_addr_t desc
;
66 target_phys_addr_t avail
;
67 target_phys_addr_t used
;
73 target_phys_addr_t pa
;
74 uint16_t last_avail_idx
;
77 void (*handle_output
)(VirtIODevice
*vdev
, VirtQueue
*vq
);
79 EventNotifier guest_notifier
;
80 EventNotifier host_notifier
;
83 /* virt queue functions */
84 static void virtqueue_init(VirtQueue
*vq
)
86 target_phys_addr_t pa
= vq
->pa
;
89 vq
->vring
.avail
= pa
+ vq
->vring
.num
* sizeof(VRingDesc
);
90 vq
->vring
.used
= vring_align(vq
->vring
.avail
+
91 offsetof(VRingAvail
, ring
[vq
->vring
.num
]),
92 VIRTIO_PCI_VRING_ALIGN
);
95 static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa
, int i
)
97 target_phys_addr_t pa
;
98 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, addr
);
102 static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa
, int i
)
104 target_phys_addr_t pa
;
105 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, len
);
109 static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa
, int i
)
111 target_phys_addr_t pa
;
112 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, flags
);
113 return lduw_phys(pa
);
116 static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa
, int i
)
118 target_phys_addr_t pa
;
119 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, next
);
120 return lduw_phys(pa
);
123 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
125 target_phys_addr_t pa
;
126 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, flags
);
127 return lduw_phys(pa
);
130 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
132 target_phys_addr_t pa
;
133 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, idx
);
134 return lduw_phys(pa
);
137 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
139 target_phys_addr_t pa
;
140 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, ring
[i
]);
141 return lduw_phys(pa
);
144 static inline void vring_used_ring_id(VirtQueue
*vq
, int i
, uint32_t val
)
146 target_phys_addr_t pa
;
147 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].id
);
151 static inline void vring_used_ring_len(VirtQueue
*vq
, int i
, uint32_t val
)
153 target_phys_addr_t pa
;
154 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].len
);
158 static uint16_t vring_used_idx(VirtQueue
*vq
)
160 target_phys_addr_t pa
;
161 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
162 return lduw_phys(pa
);
165 static inline void vring_used_idx_increment(VirtQueue
*vq
, uint16_t val
)
167 target_phys_addr_t pa
;
168 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
)
189 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
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
)
210 trace_virtqueue_fill(vq
, elem
, len
, idx
);
213 for (i
= 0; i
< elem
->in_num
; i
++) {
214 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
216 cpu_physical_memory_unmap(elem
->in_sg
[i
].iov_base
,
217 elem
->in_sg
[i
].iov_len
,
220 offset
+= elem
->in_sg
[i
].iov_len
;
223 for (i
= 0; i
< elem
->out_num
; i
++)
224 cpu_physical_memory_unmap(elem
->out_sg
[i
].iov_base
,
225 elem
->out_sg
[i
].iov_len
,
226 0, elem
->out_sg
[i
].iov_len
);
228 idx
= (idx
+ vring_used_idx(vq
)) % vq
->vring
.num
;
230 /* Get a pointer to the next entry in the used ring. */
231 vring_used_ring_id(vq
, idx
, elem
->index
);
232 vring_used_ring_len(vq
, idx
, len
);
235 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
237 /* Make sure buffer is written before we update index. */
239 trace_virtqueue_flush(vq
, count
);
240 vring_used_idx_increment(vq
, count
);
244 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
247 virtqueue_fill(vq
, elem
, len
, 0);
248 virtqueue_flush(vq
, 1);
251 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
253 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
255 /* Check it isn't doing very strange things with descriptor numbers. */
256 if (num_heads
> vq
->vring
.num
) {
257 error_report("Guest moved used index from %u to %u",
258 idx
, vring_avail_idx(vq
));
265 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
269 /* Grab the next descriptor number they're advertising, and increment
270 * the index we've seen. */
271 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
273 /* If their number is silly, that's a fatal mistake. */
274 if (head
>= vq
->vring
.num
) {
275 error_report("Guest says index %u is available", head
);
282 static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa
,
283 unsigned int i
, unsigned int max
)
287 /* If this descriptor says it doesn't chain, we're done. */
288 if (!(vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_NEXT
))
291 /* Check they're not leading us off end of descriptors. */
292 next
= vring_desc_next(desc_pa
, i
);
293 /* Make sure compiler knows to grab that: we don't want it changing! */
297 error_report("Desc next is %u", next
);
304 int virtqueue_avail_bytes(VirtQueue
*vq
, int in_bytes
, int out_bytes
)
307 int total_bufs
, in_total
, out_total
;
309 idx
= vq
->last_avail_idx
;
311 total_bufs
= in_total
= out_total
= 0;
312 while (virtqueue_num_heads(vq
, idx
)) {
313 unsigned int max
, num_bufs
, indirect
= 0;
314 target_phys_addr_t desc_pa
;
318 num_bufs
= total_bufs
;
319 i
= virtqueue_get_head(vq
, idx
++);
320 desc_pa
= vq
->vring
.desc
;
322 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
323 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
324 error_report("Invalid size for indirect buffer table");
328 /* If we've got too many, that implies a descriptor loop. */
329 if (num_bufs
>= max
) {
330 error_report("Looped descriptor");
334 /* loop over the indirect descriptor table */
336 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
338 desc_pa
= vring_desc_addr(desc_pa
, i
);
342 /* If we've got too many, that implies a descriptor loop. */
343 if (++num_bufs
> max
) {
344 error_report("Looped descriptor");
348 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
350 (in_total
+= vring_desc_len(desc_pa
, i
)) >= in_bytes
)
354 (out_total
+= vring_desc_len(desc_pa
, i
)) >= out_bytes
)
357 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
360 total_bufs
= num_bufs
;
368 void virtqueue_map_sg(struct iovec
*sg
, target_phys_addr_t
*addr
,
369 size_t num_sg
, int is_write
)
372 target_phys_addr_t len
;
374 for (i
= 0; i
< num_sg
; i
++) {
376 sg
[i
].iov_base
= cpu_physical_memory_map(addr
[i
], &len
, is_write
);
377 if (sg
[i
].iov_base
== NULL
|| len
!= sg
[i
].iov_len
) {
378 error_report("virtio: trying to map MMIO memory");
384 int virtqueue_pop(VirtQueue
*vq
, VirtQueueElement
*elem
)
386 unsigned int i
, head
, max
;
387 target_phys_addr_t desc_pa
= vq
->vring
.desc
;
389 if (!virtqueue_num_heads(vq
, vq
->last_avail_idx
))
392 /* When we start there are none of either input nor output. */
393 elem
->out_num
= elem
->in_num
= 0;
397 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
399 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
400 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
401 error_report("Invalid size for indirect buffer table");
405 /* loop over the indirect descriptor table */
406 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
407 desc_pa
= vring_desc_addr(desc_pa
, i
);
411 /* Collect all the descriptors */
415 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
416 elem
->in_addr
[elem
->in_num
] = vring_desc_addr(desc_pa
, i
);
417 sg
= &elem
->in_sg
[elem
->in_num
++];
419 elem
->out_addr
[elem
->out_num
] = vring_desc_addr(desc_pa
, i
);
420 sg
= &elem
->out_sg
[elem
->out_num
++];
423 sg
->iov_len
= vring_desc_len(desc_pa
, i
);
425 /* If we've got too many, that implies a descriptor loop. */
426 if ((elem
->in_num
+ elem
->out_num
) > max
) {
427 error_report("Looped descriptor");
430 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
432 /* Now map what we have collected */
433 virtqueue_map_sg(elem
->in_sg
, elem
->in_addr
, elem
->in_num
, 1);
434 virtqueue_map_sg(elem
->out_sg
, elem
->out_addr
, elem
->out_num
, 0);
440 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
441 return elem
->in_num
+ elem
->out_num
;
445 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
447 if (vdev
->binding
->notify
) {
448 vdev
->binding
->notify(vdev
->binding_opaque
, vector
);
452 void virtio_update_irq(VirtIODevice
*vdev
)
454 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
457 void virtio_reset(void *opaque
)
459 VirtIODevice
*vdev
= opaque
;
462 virtio_set_status(vdev
, 0);
467 vdev
->guest_features
= 0;
471 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
472 virtio_notify_vector(vdev
, vdev
->config_vector
);
474 for(i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
475 vdev
->vq
[i
].vring
.desc
= 0;
476 vdev
->vq
[i
].vring
.avail
= 0;
477 vdev
->vq
[i
].vring
.used
= 0;
478 vdev
->vq
[i
].last_avail_idx
= 0;
480 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
484 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
488 vdev
->get_config(vdev
, vdev
->config
);
490 if (addr
> (vdev
->config_len
- sizeof(val
)))
493 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
497 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
501 vdev
->get_config(vdev
, vdev
->config
);
503 if (addr
> (vdev
->config_len
- sizeof(val
)))
506 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
510 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
514 vdev
->get_config(vdev
, vdev
->config
);
516 if (addr
> (vdev
->config_len
- sizeof(val
)))
519 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
523 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
527 if (addr
> (vdev
->config_len
- sizeof(val
)))
530 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
532 if (vdev
->set_config
)
533 vdev
->set_config(vdev
, vdev
->config
);
536 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
540 if (addr
> (vdev
->config_len
- sizeof(val
)))
543 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
545 if (vdev
->set_config
)
546 vdev
->set_config(vdev
, vdev
->config
);
549 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
553 if (addr
> (vdev
->config_len
- sizeof(val
)))
556 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
558 if (vdev
->set_config
)
559 vdev
->set_config(vdev
, vdev
->config
);
562 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, target_phys_addr_t addr
)
564 vdev
->vq
[n
].pa
= addr
;
565 virtqueue_init(&vdev
->vq
[n
]);
568 target_phys_addr_t
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
570 return vdev
->vq
[n
].pa
;
573 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
575 return vdev
->vq
[n
].vring
.num
;
578 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
580 if (n
< VIRTIO_PCI_QUEUE_MAX
&& vdev
->vq
[n
].vring
.desc
) {
581 trace_virtio_queue_notify(vdev
, n
, &vdev
->vq
[n
]);
582 vdev
->vq
[n
].handle_output(vdev
, &vdev
->vq
[n
]);
586 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
588 return n
< VIRTIO_PCI_QUEUE_MAX
? vdev
->vq
[n
].vector
:
592 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
594 if (n
< VIRTIO_PCI_QUEUE_MAX
)
595 vdev
->vq
[n
].vector
= vector
;
598 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
599 void (*handle_output
)(VirtIODevice
*, VirtQueue
*))
603 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
604 if (vdev
->vq
[i
].vring
.num
== 0)
608 if (i
== VIRTIO_PCI_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
611 vdev
->vq
[i
].vring
.num
= queue_size
;
612 vdev
->vq
[i
].handle_output
= handle_output
;
617 void virtio_irq(VirtQueue
*vq
)
619 trace_virtio_irq(vq
);
620 vq
->vdev
->isr
|= 0x01;
621 virtio_notify_vector(vq
->vdev
, vq
->vector
);
624 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
626 /* Always notify when queue is empty (when feature acknowledge) */
627 if ((vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
) &&
628 (!(vdev
->guest_features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) ||
629 (vq
->inuse
|| vring_avail_idx(vq
) != vq
->last_avail_idx
)))
632 trace_virtio_notify(vdev
, vq
);
634 virtio_notify_vector(vdev
, vq
->vector
);
637 void virtio_notify_config(VirtIODevice
*vdev
)
639 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
643 virtio_notify_vector(vdev
, vdev
->config_vector
);
646 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
650 if (vdev
->binding
->save_config
)
651 vdev
->binding
->save_config(vdev
->binding_opaque
, f
);
653 qemu_put_8s(f
, &vdev
->status
);
654 qemu_put_8s(f
, &vdev
->isr
);
655 qemu_put_be16s(f
, &vdev
->queue_sel
);
656 qemu_put_be32s(f
, &vdev
->guest_features
);
657 qemu_put_be32(f
, vdev
->config_len
);
658 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
660 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
661 if (vdev
->vq
[i
].vring
.num
== 0)
667 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
668 if (vdev
->vq
[i
].vring
.num
== 0)
671 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
672 qemu_put_be64(f
, vdev
->vq
[i
].pa
);
673 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
674 if (vdev
->binding
->save_queue
)
675 vdev
->binding
->save_queue(vdev
->binding_opaque
, i
, f
);
679 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
)
683 uint32_t supported_features
=
684 vdev
->binding
->get_features(vdev
->binding_opaque
);
686 if (vdev
->binding
->load_config
) {
687 ret
= vdev
->binding
->load_config(vdev
->binding_opaque
, f
);
692 qemu_get_8s(f
, &vdev
->status
);
693 qemu_get_8s(f
, &vdev
->isr
);
694 qemu_get_be16s(f
, &vdev
->queue_sel
);
695 qemu_get_be32s(f
, &features
);
696 if (features
& ~supported_features
) {
697 error_report("Features 0x%x unsupported. Allowed features: 0x%x",
698 features
, supported_features
);
701 if (vdev
->set_features
)
702 vdev
->set_features(vdev
, features
);
703 vdev
->guest_features
= features
;
704 vdev
->config_len
= qemu_get_be32(f
);
705 qemu_get_buffer(f
, vdev
->config
, vdev
->config_len
);
707 num
= qemu_get_be32(f
);
709 for (i
= 0; i
< num
; i
++) {
710 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
711 vdev
->vq
[i
].pa
= qemu_get_be64(f
);
712 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
714 if (vdev
->vq
[i
].pa
) {
716 virtqueue_init(&vdev
->vq
[i
]);
717 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
718 /* Check it isn't doing very strange things with descriptor numbers. */
719 if (nheads
> vdev
->vq
[i
].vring
.num
) {
720 error_report("VQ %d size 0x%x Guest index 0x%x "
721 "inconsistent with Host index 0x%x: delta 0x%x\n",
722 i
, vdev
->vq
[i
].vring
.num
,
723 vring_avail_idx(&vdev
->vq
[i
]),
724 vdev
->vq
[i
].last_avail_idx
, nheads
);
727 } else if (vdev
->vq
[i
].last_avail_idx
) {
728 error_report("VQ %d address 0x0 "
729 "inconsistent with Host index 0x%x\n",
730 i
, vdev
->vq
[i
].last_avail_idx
);
733 if (vdev
->binding
->load_queue
) {
734 ret
= vdev
->binding
->load_queue(vdev
->binding_opaque
, i
, f
);
740 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
744 void virtio_cleanup(VirtIODevice
*vdev
)
747 qemu_free(vdev
->config
);
751 VirtIODevice
*virtio_common_init(const char *name
, uint16_t device_id
,
752 size_t config_size
, size_t struct_size
)
757 vdev
= qemu_mallocz(struct_size
);
759 vdev
->device_id
= device_id
;
763 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
764 vdev
->vq
= qemu_mallocz(sizeof(VirtQueue
) * VIRTIO_PCI_QUEUE_MAX
);
765 for(i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
766 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
767 vdev
->vq
[i
].vdev
= vdev
;
771 vdev
->config_len
= config_size
;
772 if (vdev
->config_len
)
773 vdev
->config
= qemu_mallocz(config_size
);
780 void virtio_bind_device(VirtIODevice
*vdev
, const VirtIOBindings
*binding
,
783 vdev
->binding
= binding
;
784 vdev
->binding_opaque
= opaque
;
787 target_phys_addr_t
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
789 return vdev
->vq
[n
].vring
.desc
;
792 target_phys_addr_t
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
794 return vdev
->vq
[n
].vring
.avail
;
797 target_phys_addr_t
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
799 return vdev
->vq
[n
].vring
.used
;
802 target_phys_addr_t
virtio_queue_get_ring_addr(VirtIODevice
*vdev
, int n
)
804 return vdev
->vq
[n
].vring
.desc
;
807 target_phys_addr_t
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
809 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
812 target_phys_addr_t
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
814 return offsetof(VRingAvail
, ring
) +
815 sizeof(uint64_t) * vdev
->vq
[n
].vring
.num
;
818 target_phys_addr_t
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
820 return offsetof(VRingUsed
, ring
) +
821 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
;
824 target_phys_addr_t
virtio_queue_get_ring_size(VirtIODevice
*vdev
, int n
)
826 return vdev
->vq
[n
].vring
.used
- vdev
->vq
[n
].vring
.desc
+
827 virtio_queue_get_used_size(vdev
, n
);
830 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
832 return vdev
->vq
[n
].last_avail_idx
;
835 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
, uint16_t idx
)
837 vdev
->vq
[n
].last_avail_idx
= idx
;
840 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
845 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
847 return &vq
->guest_notifier
;
849 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
851 return &vq
->host_notifier
;