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-report.h"
19 #include "qemu/atomic.h"
20 #include "virtio-bus.h"
22 /* The alignment to use between consumer and producer parts of vring.
23 * x86 pagesize again. */
24 #define VIRTIO_PCI_VRING_ALIGN 4096
26 typedef struct VRingDesc
34 typedef struct VRingAvail
41 typedef struct VRingUsedElem
47 typedef struct VRingUsed
51 VRingUsedElem ring
[0];
66 uint16_t last_avail_idx
;
67 /* Last used index value we have signalled on */
68 uint16_t signalled_used
;
70 /* Last used index value we have signalled on */
71 bool signalled_used_valid
;
73 /* Notification enabled? */
79 void (*handle_output
)(VirtIODevice
*vdev
, VirtQueue
*vq
);
81 EventNotifier guest_notifier
;
82 EventNotifier host_notifier
;
85 /* virt queue functions */
86 static void virtqueue_init(VirtQueue
*vq
)
91 vq
->vring
.avail
= pa
+ vq
->vring
.num
* sizeof(VRingDesc
);
92 vq
->vring
.used
= vring_align(vq
->vring
.avail
+
93 offsetof(VRingAvail
, ring
[vq
->vring
.num
]),
94 VIRTIO_PCI_VRING_ALIGN
);
97 static inline uint64_t vring_desc_addr(hwaddr desc_pa
, int i
)
100 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, addr
);
104 static inline uint32_t vring_desc_len(hwaddr desc_pa
, int i
)
107 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, len
);
111 static inline uint16_t vring_desc_flags(hwaddr desc_pa
, int i
)
114 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, flags
);
115 return lduw_phys(pa
);
118 static inline uint16_t vring_desc_next(hwaddr desc_pa
, int i
)
121 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, next
);
122 return lduw_phys(pa
);
125 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
128 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, flags
);
129 return lduw_phys(pa
);
132 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
135 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, idx
);
136 return lduw_phys(pa
);
139 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
142 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, ring
[i
]);
143 return lduw_phys(pa
);
146 static inline uint16_t vring_used_event(VirtQueue
*vq
)
148 return vring_avail_ring(vq
, vq
->vring
.num
);
151 static inline void vring_used_ring_id(VirtQueue
*vq
, int i
, uint32_t val
)
154 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].id
);
158 static inline void vring_used_ring_len(VirtQueue
*vq
, int i
, uint32_t val
)
161 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].len
);
165 static uint16_t vring_used_idx(VirtQueue
*vq
)
168 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
169 return lduw_phys(pa
);
172 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
175 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
179 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
182 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
183 stw_phys(pa
, lduw_phys(pa
) | mask
);
186 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
189 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
190 stw_phys(pa
, lduw_phys(pa
) & ~mask
);
193 static inline void vring_avail_event(VirtQueue
*vq
, uint16_t val
)
196 if (!vq
->notification
) {
199 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
203 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
205 vq
->notification
= enable
;
206 if (vq
->vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
207 vring_avail_event(vq
, vring_avail_idx(vq
));
209 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
211 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
214 /* Expose avail event/used flags before caller checks the avail idx. */
219 int virtio_queue_ready(VirtQueue
*vq
)
221 return vq
->vring
.avail
!= 0;
224 int virtio_queue_empty(VirtQueue
*vq
)
226 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
229 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
230 unsigned int len
, unsigned int idx
)
235 trace_virtqueue_fill(vq
, elem
, len
, idx
);
238 for (i
= 0; i
< elem
->in_num
; i
++) {
239 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
241 cpu_physical_memory_unmap(elem
->in_sg
[i
].iov_base
,
242 elem
->in_sg
[i
].iov_len
,
248 for (i
= 0; i
< elem
->out_num
; i
++)
249 cpu_physical_memory_unmap(elem
->out_sg
[i
].iov_base
,
250 elem
->out_sg
[i
].iov_len
,
251 0, elem
->out_sg
[i
].iov_len
);
253 idx
= (idx
+ vring_used_idx(vq
)) % vq
->vring
.num
;
255 /* Get a pointer to the next entry in the used ring. */
256 vring_used_ring_id(vq
, idx
, elem
->index
);
257 vring_used_ring_len(vq
, idx
, len
);
260 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
263 /* Make sure buffer is written before we update index. */
265 trace_virtqueue_flush(vq
, count
);
266 old
= vring_used_idx(vq
);
268 vring_used_idx_set(vq
, new);
270 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
271 vq
->signalled_used_valid
= false;
274 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
277 virtqueue_fill(vq
, elem
, len
, 0);
278 virtqueue_flush(vq
, 1);
281 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
283 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
285 /* Check it isn't doing very strange things with descriptor numbers. */
286 if (num_heads
> vq
->vring
.num
) {
287 error_report("Guest moved used index from %u to %u",
288 idx
, vring_avail_idx(vq
));
291 /* On success, callers read a descriptor at vq->last_avail_idx.
292 * Make sure descriptor read does not bypass avail index read. */
300 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
304 /* Grab the next descriptor number they're advertising, and increment
305 * the index we've seen. */
306 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
308 /* If their number is silly, that's a fatal mistake. */
309 if (head
>= vq
->vring
.num
) {
310 error_report("Guest says index %u is available", head
);
317 static unsigned virtqueue_next_desc(hwaddr desc_pa
,
318 unsigned int i
, unsigned int max
)
322 /* If this descriptor says it doesn't chain, we're done. */
323 if (!(vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_NEXT
))
326 /* Check they're not leading us off end of descriptors. */
327 next
= vring_desc_next(desc_pa
, i
);
328 /* Make sure compiler knows to grab that: we don't want it changing! */
332 error_report("Desc next is %u", next
);
339 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
340 unsigned int *out_bytes
,
341 unsigned max_in_bytes
, unsigned max_out_bytes
)
344 unsigned int total_bufs
, in_total
, out_total
;
346 idx
= vq
->last_avail_idx
;
348 total_bufs
= in_total
= out_total
= 0;
349 while (virtqueue_num_heads(vq
, idx
)) {
350 unsigned int max
, num_bufs
, indirect
= 0;
355 num_bufs
= total_bufs
;
356 i
= virtqueue_get_head(vq
, idx
++);
357 desc_pa
= vq
->vring
.desc
;
359 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
360 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
361 error_report("Invalid size for indirect buffer table");
365 /* If we've got too many, that implies a descriptor loop. */
366 if (num_bufs
>= max
) {
367 error_report("Looped descriptor");
371 /* loop over the indirect descriptor table */
373 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
375 desc_pa
= vring_desc_addr(desc_pa
, i
);
379 /* If we've got too many, that implies a descriptor loop. */
380 if (++num_bufs
> max
) {
381 error_report("Looped descriptor");
385 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
386 in_total
+= vring_desc_len(desc_pa
, i
);
388 out_total
+= vring_desc_len(desc_pa
, i
);
390 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
393 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
396 total_bufs
= num_bufs
;
402 *in_bytes
= in_total
;
405 *out_bytes
= out_total
;
409 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
410 unsigned int out_bytes
)
412 unsigned int in_total
, out_total
;
414 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
415 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
418 void virtqueue_map_sg(struct iovec
*sg
, hwaddr
*addr
,
419 size_t num_sg
, int is_write
)
424 for (i
= 0; i
< num_sg
; i
++) {
426 sg
[i
].iov_base
= cpu_physical_memory_map(addr
[i
], &len
, is_write
);
427 if (sg
[i
].iov_base
== NULL
|| len
!= sg
[i
].iov_len
) {
428 error_report("virtio: trying to map MMIO memory");
434 int virtqueue_pop(VirtQueue
*vq
, VirtQueueElement
*elem
)
436 unsigned int i
, head
, max
;
437 hwaddr desc_pa
= vq
->vring
.desc
;
439 if (!virtqueue_num_heads(vq
, vq
->last_avail_idx
))
442 /* When we start there are none of either input nor output. */
443 elem
->out_num
= elem
->in_num
= 0;
447 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
448 if (vq
->vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
449 vring_avail_event(vq
, vring_avail_idx(vq
));
452 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
453 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
454 error_report("Invalid size for indirect buffer table");
458 /* loop over the indirect descriptor table */
459 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
460 desc_pa
= vring_desc_addr(desc_pa
, i
);
464 /* Collect all the descriptors */
468 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
469 if (elem
->in_num
>= ARRAY_SIZE(elem
->in_sg
)) {
470 error_report("Too many write descriptors in indirect table");
473 elem
->in_addr
[elem
->in_num
] = vring_desc_addr(desc_pa
, i
);
474 sg
= &elem
->in_sg
[elem
->in_num
++];
476 if (elem
->out_num
>= ARRAY_SIZE(elem
->out_sg
)) {
477 error_report("Too many read descriptors in indirect table");
480 elem
->out_addr
[elem
->out_num
] = vring_desc_addr(desc_pa
, i
);
481 sg
= &elem
->out_sg
[elem
->out_num
++];
484 sg
->iov_len
= vring_desc_len(desc_pa
, i
);
486 /* If we've got too many, that implies a descriptor loop. */
487 if ((elem
->in_num
+ elem
->out_num
) > max
) {
488 error_report("Looped descriptor");
491 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
493 /* Now map what we have collected */
494 virtqueue_map_sg(elem
->in_sg
, elem
->in_addr
, elem
->in_num
, 1);
495 virtqueue_map_sg(elem
->out_sg
, elem
->out_addr
, elem
->out_num
, 0);
501 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
502 return elem
->in_num
+ elem
->out_num
;
506 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
508 if (vdev
->binding
->notify
) {
509 vdev
->binding
->notify(vdev
->binding_opaque
, vector
);
513 void virtio_update_irq(VirtIODevice
*vdev
)
515 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
518 void virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
520 trace_virtio_set_status(vdev
, val
);
522 if (vdev
->set_status
) {
523 vdev
->set_status(vdev
, val
);
528 void virtio_reset(void *opaque
)
530 VirtIODevice
*vdev
= opaque
;
533 virtio_set_status(vdev
, 0);
538 vdev
->guest_features
= 0;
542 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
543 virtio_notify_vector(vdev
, vdev
->config_vector
);
545 for(i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
546 vdev
->vq
[i
].vring
.desc
= 0;
547 vdev
->vq
[i
].vring
.avail
= 0;
548 vdev
->vq
[i
].vring
.used
= 0;
549 vdev
->vq
[i
].last_avail_idx
= 0;
551 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
552 vdev
->vq
[i
].signalled_used
= 0;
553 vdev
->vq
[i
].signalled_used_valid
= false;
554 vdev
->vq
[i
].notification
= true;
558 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
562 vdev
->get_config(vdev
, vdev
->config
);
564 if (addr
> (vdev
->config_len
- sizeof(val
)))
567 val
= ldub_p(vdev
->config
+ addr
);
571 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
575 vdev
->get_config(vdev
, vdev
->config
);
577 if (addr
> (vdev
->config_len
- sizeof(val
)))
580 val
= lduw_p(vdev
->config
+ addr
);
584 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
588 vdev
->get_config(vdev
, vdev
->config
);
590 if (addr
> (vdev
->config_len
- sizeof(val
)))
593 val
= ldl_p(vdev
->config
+ addr
);
597 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
601 if (addr
> (vdev
->config_len
- sizeof(val
)))
604 stb_p(vdev
->config
+ addr
, val
);
606 if (vdev
->set_config
)
607 vdev
->set_config(vdev
, vdev
->config
);
610 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
614 if (addr
> (vdev
->config_len
- sizeof(val
)))
617 stw_p(vdev
->config
+ addr
, val
);
619 if (vdev
->set_config
)
620 vdev
->set_config(vdev
, vdev
->config
);
623 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
627 if (addr
> (vdev
->config_len
- sizeof(val
)))
630 stl_p(vdev
->config
+ addr
, val
);
632 if (vdev
->set_config
)
633 vdev
->set_config(vdev
, vdev
->config
);
636 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
638 vdev
->vq
[n
].pa
= addr
;
639 virtqueue_init(&vdev
->vq
[n
]);
642 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
644 return vdev
->vq
[n
].pa
;
647 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
649 return vdev
->vq
[n
].vring
.num
;
652 int virtio_queue_get_id(VirtQueue
*vq
)
654 VirtIODevice
*vdev
= vq
->vdev
;
655 assert(vq
>= &vdev
->vq
[0] && vq
< &vdev
->vq
[VIRTIO_PCI_QUEUE_MAX
]);
656 return vq
- &vdev
->vq
[0];
659 void virtio_queue_notify_vq(VirtQueue
*vq
)
661 if (vq
->vring
.desc
) {
662 VirtIODevice
*vdev
= vq
->vdev
;
663 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
664 vq
->handle_output(vdev
, vq
);
668 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
670 virtio_queue_notify_vq(&vdev
->vq
[n
]);
673 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
675 return n
< VIRTIO_PCI_QUEUE_MAX
? vdev
->vq
[n
].vector
:
679 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
681 if (n
< VIRTIO_PCI_QUEUE_MAX
)
682 vdev
->vq
[n
].vector
= vector
;
685 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
686 void (*handle_output
)(VirtIODevice
*, VirtQueue
*))
690 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
691 if (vdev
->vq
[i
].vring
.num
== 0)
695 if (i
== VIRTIO_PCI_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
698 vdev
->vq
[i
].vring
.num
= queue_size
;
699 vdev
->vq
[i
].handle_output
= handle_output
;
704 void virtio_irq(VirtQueue
*vq
)
706 trace_virtio_irq(vq
);
707 vq
->vdev
->isr
|= 0x01;
708 virtio_notify_vector(vq
->vdev
, vq
->vector
);
711 /* Assuming a given event_idx value from the other size, if
712 * we have just incremented index from old to new_idx,
713 * should we trigger an event? */
714 static inline int vring_need_event(uint16_t event
, uint16_t new, uint16_t old
)
716 /* Note: Xen has similar logic for notification hold-off
717 * in include/xen/interface/io/ring.h with req_event and req_prod
718 * corresponding to event_idx + 1 and new respectively.
719 * Note also that req_event and req_prod in Xen start at 1,
720 * event indexes in virtio start at 0. */
721 return (uint16_t)(new - event
- 1) < (uint16_t)(new - old
);
724 static bool vring_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
728 /* We need to expose used array entries before checking used event. */
730 /* Always notify when queue is empty (when feature acknowledge) */
731 if (((vdev
->guest_features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) &&
732 !vq
->inuse
&& vring_avail_idx(vq
) == vq
->last_avail_idx
)) {
736 if (!(vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
))) {
737 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
740 v
= vq
->signalled_used_valid
;
741 vq
->signalled_used_valid
= true;
742 old
= vq
->signalled_used
;
743 new = vq
->signalled_used
= vring_used_idx(vq
);
744 return !v
|| vring_need_event(vring_used_event(vq
), new, old
);
747 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
749 if (!vring_notify(vdev
, vq
)) {
753 trace_virtio_notify(vdev
, vq
);
755 virtio_notify_vector(vdev
, vq
->vector
);
758 void virtio_notify_config(VirtIODevice
*vdev
)
760 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
764 virtio_notify_vector(vdev
, vdev
->config_vector
);
767 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
771 if (vdev
->binding
->save_config
)
772 vdev
->binding
->save_config(vdev
->binding_opaque
, f
);
774 qemu_put_8s(f
, &vdev
->status
);
775 qemu_put_8s(f
, &vdev
->isr
);
776 qemu_put_be16s(f
, &vdev
->queue_sel
);
777 qemu_put_be32s(f
, &vdev
->guest_features
);
778 qemu_put_be32(f
, vdev
->config_len
);
779 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
781 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
782 if (vdev
->vq
[i
].vring
.num
== 0)
788 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
789 if (vdev
->vq
[i
].vring
.num
== 0)
792 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
793 qemu_put_be64(f
, vdev
->vq
[i
].pa
);
794 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
795 if (vdev
->binding
->save_queue
)
796 vdev
->binding
->save_queue(vdev
->binding_opaque
, i
, f
);
800 int virtio_set_features(VirtIODevice
*vdev
, uint32_t val
)
802 uint32_t supported_features
=
803 vdev
->binding
->get_features(vdev
->binding_opaque
);
804 bool bad
= (val
& ~supported_features
) != 0;
806 val
&= supported_features
;
807 if (vdev
->set_features
) {
808 vdev
->set_features(vdev
, val
);
810 vdev
->guest_features
= val
;
814 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
)
818 uint32_t supported_features
;
820 if (vdev
->binding
->load_config
) {
821 ret
= vdev
->binding
->load_config(vdev
->binding_opaque
, f
);
826 qemu_get_8s(f
, &vdev
->status
);
827 qemu_get_8s(f
, &vdev
->isr
);
828 qemu_get_be16s(f
, &vdev
->queue_sel
);
829 qemu_get_be32s(f
, &features
);
831 if (virtio_set_features(vdev
, features
) < 0) {
832 supported_features
= vdev
->binding
->get_features(vdev
->binding_opaque
);
833 error_report("Features 0x%x unsupported. Allowed features: 0x%x",
834 features
, supported_features
);
837 vdev
->config_len
= qemu_get_be32(f
);
838 qemu_get_buffer(f
, vdev
->config
, vdev
->config_len
);
840 num
= qemu_get_be32(f
);
842 for (i
= 0; i
< num
; i
++) {
843 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
844 vdev
->vq
[i
].pa
= qemu_get_be64(f
);
845 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
846 vdev
->vq
[i
].signalled_used_valid
= false;
847 vdev
->vq
[i
].notification
= true;
849 if (vdev
->vq
[i
].pa
) {
851 virtqueue_init(&vdev
->vq
[i
]);
852 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
853 /* Check it isn't doing very strange things with descriptor numbers. */
854 if (nheads
> vdev
->vq
[i
].vring
.num
) {
855 error_report("VQ %d size 0x%x Guest index 0x%x "
856 "inconsistent with Host index 0x%x: delta 0x%x",
857 i
, vdev
->vq
[i
].vring
.num
,
858 vring_avail_idx(&vdev
->vq
[i
]),
859 vdev
->vq
[i
].last_avail_idx
, nheads
);
862 } else if (vdev
->vq
[i
].last_avail_idx
) {
863 error_report("VQ %d address 0x0 "
864 "inconsistent with Host index 0x%x",
865 i
, vdev
->vq
[i
].last_avail_idx
);
868 if (vdev
->binding
->load_queue
) {
869 ret
= vdev
->binding
->load_queue(vdev
->binding_opaque
, i
, f
);
875 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
879 void virtio_common_cleanup(VirtIODevice
*vdev
)
881 qemu_del_vm_change_state_handler(vdev
->vmstate
);
882 g_free(vdev
->config
);
886 void virtio_cleanup(VirtIODevice
*vdev
)
888 virtio_common_cleanup(vdev
);
892 static void virtio_vmstate_change(void *opaque
, int running
, RunState state
)
894 VirtIODevice
*vdev
= opaque
;
895 bool backend_run
= running
&& (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
);
896 vdev
->vm_running
= running
;
899 virtio_set_status(vdev
, vdev
->status
);
902 if (vdev
->binding
->vmstate_change
) {
903 vdev
->binding
->vmstate_change(vdev
->binding_opaque
, backend_run
);
907 virtio_set_status(vdev
, vdev
->status
);
911 void virtio_init(VirtIODevice
*vdev
, const char *name
,
912 uint16_t device_id
, size_t config_size
)
915 vdev
->device_id
= device_id
;
919 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
920 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_PCI_QUEUE_MAX
);
921 vdev
->vm_running
= runstate_is_running();
922 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
923 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
924 vdev
->vq
[i
].vdev
= vdev
;
928 vdev
->config_len
= config_size
;
929 if (vdev
->config_len
) {
930 vdev
->config
= g_malloc0(config_size
);
934 vdev
->vmstate
= qemu_add_vm_change_state_handler(virtio_vmstate_change
,
938 VirtIODevice
*virtio_common_init(const char *name
, uint16_t device_id
,
939 size_t config_size
, size_t struct_size
)
942 vdev
= g_malloc0(struct_size
);
943 virtio_init(vdev
, name
, device_id
, config_size
);
947 void virtio_bind_device(VirtIODevice
*vdev
, const VirtIOBindings
*binding
,
950 vdev
->binding
= binding
;
951 vdev
->binding_opaque
= opaque
;
954 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
956 return vdev
->vq
[n
].vring
.desc
;
959 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
961 return vdev
->vq
[n
].vring
.avail
;
964 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
966 return vdev
->vq
[n
].vring
.used
;
969 hwaddr
virtio_queue_get_ring_addr(VirtIODevice
*vdev
, int n
)
971 return vdev
->vq
[n
].vring
.desc
;
974 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
976 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
979 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
981 return offsetof(VRingAvail
, ring
) +
982 sizeof(uint64_t) * vdev
->vq
[n
].vring
.num
;
985 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
987 return offsetof(VRingUsed
, ring
) +
988 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
;
991 hwaddr
virtio_queue_get_ring_size(VirtIODevice
*vdev
, int n
)
993 return vdev
->vq
[n
].vring
.used
- vdev
->vq
[n
].vring
.desc
+
994 virtio_queue_get_used_size(vdev
, n
);
997 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
999 return vdev
->vq
[n
].last_avail_idx
;
1002 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
, uint16_t idx
)
1004 vdev
->vq
[n
].last_avail_idx
= idx
;
1007 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
1009 return vdev
->vq
+ n
;
1012 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
1014 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
1015 if (event_notifier_test_and_clear(n
)) {
1020 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1023 if (assign
&& !with_irqfd
) {
1024 event_notifier_set_handler(&vq
->guest_notifier
,
1025 virtio_queue_guest_notifier_read
);
1027 event_notifier_set_handler(&vq
->guest_notifier
, NULL
);
1030 /* Test and clear notifier before closing it,
1031 * in case poll callback didn't have time to run. */
1032 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
1036 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
1038 return &vq
->guest_notifier
;
1041 static void virtio_queue_host_notifier_read(EventNotifier
*n
)
1043 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
1044 if (event_notifier_test_and_clear(n
)) {
1045 virtio_queue_notify_vq(vq
);
1049 void virtio_queue_set_host_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1052 if (assign
&& set_handler
) {
1053 event_notifier_set_handler(&vq
->host_notifier
,
1054 virtio_queue_host_notifier_read
);
1056 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
1059 /* Test and clear notifier before after disabling event,
1060 * in case poll callback didn't have time to run. */
1061 virtio_queue_host_notifier_read(&vq
->host_notifier
);
1065 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
1067 return &vq
->host_notifier
;
1070 static int virtio_device_init(DeviceState
*qdev
)
1072 VirtIODevice
*vdev
= VIRTIO_DEVICE(qdev
);
1073 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(qdev
);
1074 assert(k
->init
!= NULL
);
1075 if (k
->init(vdev
) < 0) {
1078 virtio_bus_plug_device(vdev
);
1082 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
1084 /* Set the default value here. */
1085 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1086 dc
->init
= virtio_device_init
;
1087 dc
->bus_type
= TYPE_VIRTIO_BUS
;
1090 static const TypeInfo virtio_device_info
= {
1091 .name
= TYPE_VIRTIO_DEVICE
,
1092 .parent
= TYPE_DEVICE
,
1093 .instance_size
= sizeof(VirtIODevice
),
1094 .class_init
= virtio_device_class_init
,
1096 .class_size
= sizeof(VirtioDeviceClass
),
1099 static void virtio_register_types(void)
1101 type_register_static(&virtio_device_info
);
1104 type_init(virtio_register_types
)