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.
14 #include "qemu/osdep.h"
17 #include "exec/address-spaces.h"
18 #include "qemu/error-report.h"
19 #include "hw/virtio/virtio.h"
20 #include "qemu/atomic.h"
21 #include "hw/virtio/virtio-bus.h"
22 #include "migration/migration.h"
23 #include "hw/virtio/virtio-access.h"
26 * The alignment to use between consumer and producer parts of vring.
27 * x86 pagesize again. This is the default, used by transports like PCI
28 * which don't provide a means for the guest to tell the host the alignment.
30 #define VIRTIO_PCI_VRING_ALIGN 4096
32 typedef struct VRingDesc
40 typedef struct VRingAvail
47 typedef struct VRingUsedElem
53 typedef struct VRingUsed
57 VRingUsedElem ring
[0];
63 unsigned int num_default
;
73 uint16_t last_avail_idx
;
74 /* Last used index value we have signalled on */
75 uint16_t signalled_used
;
77 /* Last used index value we have signalled on */
78 bool signalled_used_valid
;
80 /* Notification enabled? */
88 void (*handle_output
)(VirtIODevice
*vdev
, VirtQueue
*vq
);
90 EventNotifier guest_notifier
;
91 EventNotifier host_notifier
;
92 QLIST_ENTRY(VirtQueue
) node
;
95 /* virt queue functions */
96 void virtio_queue_update_rings(VirtIODevice
*vdev
, int n
)
98 VRing
*vring
= &vdev
->vq
[n
].vring
;
101 /* not yet setup -> nothing to do */
104 vring
->avail
= vring
->desc
+ vring
->num
* sizeof(VRingDesc
);
105 vring
->used
= vring_align(vring
->avail
+
106 offsetof(VRingAvail
, ring
[vring
->num
]),
110 static inline uint64_t vring_desc_addr(VirtIODevice
*vdev
, hwaddr desc_pa
,
114 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, addr
);
115 return virtio_ldq_phys(vdev
, pa
);
118 static inline uint32_t vring_desc_len(VirtIODevice
*vdev
, hwaddr desc_pa
, int i
)
121 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, len
);
122 return virtio_ldl_phys(vdev
, pa
);
125 static inline uint16_t vring_desc_flags(VirtIODevice
*vdev
, hwaddr desc_pa
,
129 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, flags
);
130 return virtio_lduw_phys(vdev
, pa
);
133 static inline uint16_t vring_desc_next(VirtIODevice
*vdev
, hwaddr desc_pa
,
137 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, next
);
138 return virtio_lduw_phys(vdev
, pa
);
141 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
144 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, flags
);
145 return virtio_lduw_phys(vq
->vdev
, pa
);
148 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
151 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, idx
);
152 return virtio_lduw_phys(vq
->vdev
, pa
);
155 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
158 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, ring
[i
]);
159 return virtio_lduw_phys(vq
->vdev
, pa
);
162 static inline uint16_t vring_get_used_event(VirtQueue
*vq
)
164 return vring_avail_ring(vq
, vq
->vring
.num
);
167 static inline void vring_used_ring_id(VirtQueue
*vq
, int i
, uint32_t val
)
170 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].id
);
171 virtio_stl_phys(vq
->vdev
, pa
, val
);
174 static inline void vring_used_ring_len(VirtQueue
*vq
, int i
, uint32_t val
)
177 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].len
);
178 virtio_stl_phys(vq
->vdev
, pa
, val
);
181 static uint16_t vring_used_idx(VirtQueue
*vq
)
184 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
185 return virtio_lduw_phys(vq
->vdev
, pa
);
188 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
191 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
192 virtio_stw_phys(vq
->vdev
, pa
, val
);
195 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
197 VirtIODevice
*vdev
= vq
->vdev
;
199 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
200 virtio_stw_phys(vdev
, pa
, virtio_lduw_phys(vdev
, pa
) | mask
);
203 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
205 VirtIODevice
*vdev
= vq
->vdev
;
207 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
208 virtio_stw_phys(vdev
, pa
, virtio_lduw_phys(vdev
, pa
) & ~mask
);
211 static inline void vring_set_avail_event(VirtQueue
*vq
, uint16_t val
)
214 if (!vq
->notification
) {
217 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
218 virtio_stw_phys(vq
->vdev
, pa
, val
);
221 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
223 vq
->notification
= enable
;
224 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
225 vring_set_avail_event(vq
, vring_avail_idx(vq
));
227 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
229 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
232 /* Expose avail event/used flags before caller checks the avail idx. */
237 int virtio_queue_ready(VirtQueue
*vq
)
239 return vq
->vring
.avail
!= 0;
242 int virtio_queue_empty(VirtQueue
*vq
)
244 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
247 static void virtqueue_unmap_sg(VirtQueue
*vq
, const VirtQueueElement
*elem
,
254 for (i
= 0; i
< elem
->in_num
; i
++) {
255 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
257 cpu_physical_memory_unmap(elem
->in_sg
[i
].iov_base
,
258 elem
->in_sg
[i
].iov_len
,
264 for (i
= 0; i
< elem
->out_num
; i
++)
265 cpu_physical_memory_unmap(elem
->out_sg
[i
].iov_base
,
266 elem
->out_sg
[i
].iov_len
,
267 0, elem
->out_sg
[i
].iov_len
);
270 void virtqueue_discard(VirtQueue
*vq
, const VirtQueueElement
*elem
,
273 vq
->last_avail_idx
--;
274 virtqueue_unmap_sg(vq
, elem
, len
);
277 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
278 unsigned int len
, unsigned int idx
)
280 trace_virtqueue_fill(vq
, elem
, len
, idx
);
282 virtqueue_unmap_sg(vq
, elem
, len
);
284 idx
= (idx
+ vring_used_idx(vq
)) % vq
->vring
.num
;
286 /* Get a pointer to the next entry in the used ring. */
287 vring_used_ring_id(vq
, idx
, elem
->index
);
288 vring_used_ring_len(vq
, idx
, len
);
291 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
294 /* Make sure buffer is written before we update index. */
296 trace_virtqueue_flush(vq
, count
);
297 old
= vring_used_idx(vq
);
299 vring_used_idx_set(vq
, new);
301 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
302 vq
->signalled_used_valid
= false;
305 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
308 virtqueue_fill(vq
, elem
, len
, 0);
309 virtqueue_flush(vq
, 1);
312 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
314 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
316 /* Check it isn't doing very strange things with descriptor numbers. */
317 if (num_heads
> vq
->vring
.num
) {
318 error_report("Guest moved used index from %u to %u",
319 idx
, vring_avail_idx(vq
));
322 /* On success, callers read a descriptor at vq->last_avail_idx.
323 * Make sure descriptor read does not bypass avail index read. */
331 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
335 /* Grab the next descriptor number they're advertising, and increment
336 * the index we've seen. */
337 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
339 /* If their number is silly, that's a fatal mistake. */
340 if (head
>= vq
->vring
.num
) {
341 error_report("Guest says index %u is available", head
);
348 static unsigned virtqueue_next_desc(VirtIODevice
*vdev
, hwaddr desc_pa
,
349 unsigned int i
, unsigned int max
)
353 /* If this descriptor says it doesn't chain, we're done. */
354 if (!(vring_desc_flags(vdev
, desc_pa
, i
) & VRING_DESC_F_NEXT
)) {
358 /* Check they're not leading us off end of descriptors. */
359 next
= vring_desc_next(vdev
, desc_pa
, i
);
360 /* Make sure compiler knows to grab that: we don't want it changing! */
364 error_report("Desc next is %u", next
);
371 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
372 unsigned int *out_bytes
,
373 unsigned max_in_bytes
, unsigned max_out_bytes
)
376 unsigned int total_bufs
, in_total
, out_total
;
378 idx
= vq
->last_avail_idx
;
380 total_bufs
= in_total
= out_total
= 0;
381 while (virtqueue_num_heads(vq
, idx
)) {
382 VirtIODevice
*vdev
= vq
->vdev
;
383 unsigned int max
, num_bufs
, indirect
= 0;
388 num_bufs
= total_bufs
;
389 i
= virtqueue_get_head(vq
, idx
++);
390 desc_pa
= vq
->vring
.desc
;
392 if (vring_desc_flags(vdev
, desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
393 if (vring_desc_len(vdev
, desc_pa
, i
) % sizeof(VRingDesc
)) {
394 error_report("Invalid size for indirect buffer table");
398 /* If we've got too many, that implies a descriptor loop. */
399 if (num_bufs
>= max
) {
400 error_report("Looped descriptor");
404 /* loop over the indirect descriptor table */
406 max
= vring_desc_len(vdev
, desc_pa
, i
) / sizeof(VRingDesc
);
407 desc_pa
= vring_desc_addr(vdev
, desc_pa
, i
);
412 /* If we've got too many, that implies a descriptor loop. */
413 if (++num_bufs
> max
) {
414 error_report("Looped descriptor");
418 if (vring_desc_flags(vdev
, desc_pa
, i
) & VRING_DESC_F_WRITE
) {
419 in_total
+= vring_desc_len(vdev
, desc_pa
, i
);
421 out_total
+= vring_desc_len(vdev
, desc_pa
, i
);
423 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
426 } while ((i
= virtqueue_next_desc(vdev
, desc_pa
, i
, max
)) != max
);
429 total_bufs
= num_bufs
;
435 *in_bytes
= in_total
;
438 *out_bytes
= out_total
;
442 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
443 unsigned int out_bytes
)
445 unsigned int in_total
, out_total
;
447 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
448 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
451 static void virtqueue_map_iovec(struct iovec
*sg
, hwaddr
*addr
,
452 unsigned int *num_sg
, unsigned int max_size
,
458 /* Note: this function MUST validate input, some callers
459 * are passing in num_sg values received over the network.
461 /* TODO: teach all callers that this can fail, and return failure instead
463 * When we do, we might be able to re-enable NDEBUG below.
466 #error building with NDEBUG is not supported
468 assert(*num_sg
<= max_size
);
470 for (i
= 0; i
< *num_sg
; i
++) {
472 sg
[i
].iov_base
= cpu_physical_memory_map(addr
[i
], &len
, is_write
);
473 if (!sg
[i
].iov_base
) {
474 error_report("virtio: error trying to map MMIO memory");
477 if (len
== sg
[i
].iov_len
) {
480 if (*num_sg
>= max_size
) {
481 error_report("virtio: memory split makes iovec too large");
484 memmove(sg
+ i
+ 1, sg
+ i
, sizeof(*sg
) * (*num_sg
- i
));
485 memmove(addr
+ i
+ 1, addr
+ i
, sizeof(*addr
) * (*num_sg
- i
));
486 assert(len
< sg
[i
+ 1].iov_len
);
489 sg
[i
+ 1].iov_len
-= len
;
494 void virtqueue_map(VirtQueueElement
*elem
)
496 virtqueue_map_iovec(elem
->in_sg
, elem
->in_addr
, &elem
->in_num
,
497 VIRTQUEUE_MAX_SIZE
, 1);
498 virtqueue_map_iovec(elem
->out_sg
, elem
->out_addr
, &elem
->out_num
,
499 VIRTQUEUE_MAX_SIZE
, 0);
502 void *virtqueue_alloc_element(size_t sz
, unsigned out_num
, unsigned in_num
)
504 VirtQueueElement
*elem
;
505 size_t in_addr_ofs
= QEMU_ALIGN_UP(sz
, __alignof__(elem
->in_addr
[0]));
506 size_t out_addr_ofs
= in_addr_ofs
+ in_num
* sizeof(elem
->in_addr
[0]);
507 size_t out_addr_end
= out_addr_ofs
+ out_num
* sizeof(elem
->out_addr
[0]);
508 size_t in_sg_ofs
= QEMU_ALIGN_UP(out_addr_end
, __alignof__(elem
->in_sg
[0]));
509 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
510 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
512 assert(sz
>= sizeof(VirtQueueElement
));
513 elem
= g_malloc(out_sg_end
);
514 elem
->out_num
= out_num
;
515 elem
->in_num
= in_num
;
516 elem
->in_addr
= (void *)elem
+ in_addr_ofs
;
517 elem
->out_addr
= (void *)elem
+ out_addr_ofs
;
518 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
519 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
523 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
)
525 unsigned int i
, head
, max
;
526 hwaddr desc_pa
= vq
->vring
.desc
;
527 VirtIODevice
*vdev
= vq
->vdev
;
528 VirtQueueElement
*elem
;
530 if (!virtqueue_num_heads(vq
, vq
->last_avail_idx
)) {
534 /* When we start there are none of either input nor output. */
535 elem
= virtqueue_alloc_element(sz
, VIRTQUEUE_MAX_SIZE
, VIRTQUEUE_MAX_SIZE
);
536 elem
->out_num
= elem
->in_num
= 0;
540 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
541 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
542 vring_set_avail_event(vq
, vq
->last_avail_idx
);
545 if (vring_desc_flags(vdev
, desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
546 if (vring_desc_len(vdev
, desc_pa
, i
) % sizeof(VRingDesc
)) {
547 error_report("Invalid size for indirect buffer table");
551 /* loop over the indirect descriptor table */
552 max
= vring_desc_len(vdev
, desc_pa
, i
) / sizeof(VRingDesc
);
553 desc_pa
= vring_desc_addr(vdev
, desc_pa
, i
);
557 /* Collect all the descriptors */
561 if (vring_desc_flags(vdev
, desc_pa
, i
) & VRING_DESC_F_WRITE
) {
562 if (elem
->in_num
>= VIRTQUEUE_MAX_SIZE
) {
563 error_report("Too many write descriptors in indirect table");
566 elem
->in_addr
[elem
->in_num
] = vring_desc_addr(vdev
, desc_pa
, i
);
567 sg
= &elem
->in_sg
[elem
->in_num
++];
569 if (elem
->out_num
>= VIRTQUEUE_MAX_SIZE
) {
570 error_report("Too many read descriptors in indirect table");
573 elem
->out_addr
[elem
->out_num
] = vring_desc_addr(vdev
, desc_pa
, i
);
574 sg
= &elem
->out_sg
[elem
->out_num
++];
577 sg
->iov_len
= vring_desc_len(vdev
, desc_pa
, i
);
579 /* If we've got too many, that implies a descriptor loop. */
580 if ((elem
->in_num
+ elem
->out_num
) > max
) {
581 error_report("Looped descriptor");
584 } while ((i
= virtqueue_next_desc(vdev
, desc_pa
, i
, max
)) != max
);
586 /* Now map what we have collected */
593 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
597 /* Reading and writing a structure directly to QEMUFile is *awful*, but
598 * it is what QEMU has always done by mistake. We can change it sooner
599 * or later by bumping the version number of the affected vm states.
600 * In the meanwhile, since the in-memory layout of VirtQueueElement
601 * has changed, we need to marshal to and from the layout that was
602 * used before the change.
604 typedef struct VirtQueueElementOld
{
606 unsigned int out_num
;
608 hwaddr in_addr
[VIRTQUEUE_MAX_SIZE
];
609 hwaddr out_addr
[VIRTQUEUE_MAX_SIZE
];
610 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
611 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
612 } VirtQueueElementOld
;
614 void *qemu_get_virtqueue_element(QEMUFile
*f
, size_t sz
)
616 VirtQueueElement
*elem
;
617 VirtQueueElementOld data
;
620 qemu_get_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
622 elem
= virtqueue_alloc_element(sz
, data
.out_num
, data
.in_num
);
623 elem
->index
= data
.index
;
625 for (i
= 0; i
< elem
->in_num
; i
++) {
626 elem
->in_addr
[i
] = data
.in_addr
[i
];
629 for (i
= 0; i
< elem
->out_num
; i
++) {
630 elem
->out_addr
[i
] = data
.out_addr
[i
];
633 for (i
= 0; i
< elem
->in_num
; i
++) {
634 /* Base is overwritten by virtqueue_map. */
635 elem
->in_sg
[i
].iov_base
= 0;
636 elem
->in_sg
[i
].iov_len
= data
.in_sg
[i
].iov_len
;
639 for (i
= 0; i
< elem
->out_num
; i
++) {
640 /* Base is overwritten by virtqueue_map. */
641 elem
->out_sg
[i
].iov_base
= 0;
642 elem
->out_sg
[i
].iov_len
= data
.out_sg
[i
].iov_len
;
649 void qemu_put_virtqueue_element(QEMUFile
*f
, VirtQueueElement
*elem
)
651 VirtQueueElementOld data
;
654 memset(&data
, 0, sizeof(data
));
655 data
.index
= elem
->index
;
656 data
.in_num
= elem
->in_num
;
657 data
.out_num
= elem
->out_num
;
659 for (i
= 0; i
< elem
->in_num
; i
++) {
660 data
.in_addr
[i
] = elem
->in_addr
[i
];
663 for (i
= 0; i
< elem
->out_num
; i
++) {
664 data
.out_addr
[i
] = elem
->out_addr
[i
];
667 for (i
= 0; i
< elem
->in_num
; i
++) {
668 /* Base is overwritten by virtqueue_map when loading. Do not
669 * save it, as it would leak the QEMU address space layout. */
670 data
.in_sg
[i
].iov_len
= elem
->in_sg
[i
].iov_len
;
673 for (i
= 0; i
< elem
->out_num
; i
++) {
674 /* Do not save iov_base as above. */
675 data
.out_sg
[i
].iov_len
= elem
->out_sg
[i
].iov_len
;
677 qemu_put_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
681 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
683 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
684 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
687 k
->notify(qbus
->parent
, vector
);
691 void virtio_update_irq(VirtIODevice
*vdev
)
693 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
696 static int virtio_validate_features(VirtIODevice
*vdev
)
698 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
700 if (k
->validate_features
) {
701 return k
->validate_features(vdev
);
707 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
709 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
710 trace_virtio_set_status(vdev
, val
);
712 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
713 if (!(vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) &&
714 val
& VIRTIO_CONFIG_S_FEATURES_OK
) {
715 int ret
= virtio_validate_features(vdev
);
723 k
->set_status(vdev
, val
);
729 bool target_words_bigendian(void);
730 static enum virtio_device_endian
virtio_default_endian(void)
732 if (target_words_bigendian()) {
733 return VIRTIO_DEVICE_ENDIAN_BIG
;
735 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
739 static enum virtio_device_endian
virtio_current_cpu_endian(void)
741 CPUClass
*cc
= CPU_GET_CLASS(current_cpu
);
743 if (cc
->virtio_is_big_endian(current_cpu
)) {
744 return VIRTIO_DEVICE_ENDIAN_BIG
;
746 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
750 void virtio_reset(void *opaque
)
752 VirtIODevice
*vdev
= opaque
;
753 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
756 virtio_set_status(vdev
, 0);
758 /* Guest initiated reset */
759 vdev
->device_endian
= virtio_current_cpu_endian();
762 vdev
->device_endian
= virtio_default_endian();
769 vdev
->guest_features
= 0;
773 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
774 virtio_notify_vector(vdev
, vdev
->config_vector
);
776 for(i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
777 vdev
->vq
[i
].vring
.desc
= 0;
778 vdev
->vq
[i
].vring
.avail
= 0;
779 vdev
->vq
[i
].vring
.used
= 0;
780 vdev
->vq
[i
].last_avail_idx
= 0;
781 virtio_queue_set_vector(vdev
, i
, VIRTIO_NO_VECTOR
);
782 vdev
->vq
[i
].signalled_used
= 0;
783 vdev
->vq
[i
].signalled_used_valid
= false;
784 vdev
->vq
[i
].notification
= true;
785 vdev
->vq
[i
].vring
.num
= vdev
->vq
[i
].vring
.num_default
;
789 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
791 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
794 if (addr
+ sizeof(val
) > vdev
->config_len
) {
798 k
->get_config(vdev
, vdev
->config
);
800 val
= ldub_p(vdev
->config
+ addr
);
804 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
806 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
809 if (addr
+ sizeof(val
) > vdev
->config_len
) {
813 k
->get_config(vdev
, vdev
->config
);
815 val
= lduw_p(vdev
->config
+ addr
);
819 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
821 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
824 if (addr
+ sizeof(val
) > vdev
->config_len
) {
828 k
->get_config(vdev
, vdev
->config
);
830 val
= ldl_p(vdev
->config
+ addr
);
834 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
836 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
839 if (addr
+ sizeof(val
) > vdev
->config_len
) {
843 stb_p(vdev
->config
+ addr
, val
);
846 k
->set_config(vdev
, vdev
->config
);
850 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
852 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
855 if (addr
+ sizeof(val
) > vdev
->config_len
) {
859 stw_p(vdev
->config
+ addr
, val
);
862 k
->set_config(vdev
, vdev
->config
);
866 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
868 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
871 if (addr
+ sizeof(val
) > vdev
->config_len
) {
875 stl_p(vdev
->config
+ addr
, val
);
878 k
->set_config(vdev
, vdev
->config
);
882 uint32_t virtio_config_modern_readb(VirtIODevice
*vdev
, uint32_t addr
)
884 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
887 if (addr
+ sizeof(val
) > vdev
->config_len
) {
891 k
->get_config(vdev
, vdev
->config
);
893 val
= ldub_p(vdev
->config
+ addr
);
897 uint32_t virtio_config_modern_readw(VirtIODevice
*vdev
, uint32_t addr
)
899 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
902 if (addr
+ sizeof(val
) > vdev
->config_len
) {
906 k
->get_config(vdev
, vdev
->config
);
908 val
= lduw_le_p(vdev
->config
+ addr
);
912 uint32_t virtio_config_modern_readl(VirtIODevice
*vdev
, uint32_t addr
)
914 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
917 if (addr
+ sizeof(val
) > vdev
->config_len
) {
921 k
->get_config(vdev
, vdev
->config
);
923 val
= ldl_le_p(vdev
->config
+ addr
);
927 void virtio_config_modern_writeb(VirtIODevice
*vdev
,
928 uint32_t addr
, uint32_t data
)
930 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
933 if (addr
+ sizeof(val
) > vdev
->config_len
) {
937 stb_p(vdev
->config
+ addr
, val
);
940 k
->set_config(vdev
, vdev
->config
);
944 void virtio_config_modern_writew(VirtIODevice
*vdev
,
945 uint32_t addr
, uint32_t data
)
947 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
950 if (addr
+ sizeof(val
) > vdev
->config_len
) {
954 stw_le_p(vdev
->config
+ addr
, val
);
957 k
->set_config(vdev
, vdev
->config
);
961 void virtio_config_modern_writel(VirtIODevice
*vdev
,
962 uint32_t addr
, uint32_t data
)
964 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
967 if (addr
+ sizeof(val
) > vdev
->config_len
) {
971 stl_le_p(vdev
->config
+ addr
, val
);
974 k
->set_config(vdev
, vdev
->config
);
978 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
980 vdev
->vq
[n
].vring
.desc
= addr
;
981 virtio_queue_update_rings(vdev
, n
);
984 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
986 return vdev
->vq
[n
].vring
.desc
;
989 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
990 hwaddr avail
, hwaddr used
)
992 vdev
->vq
[n
].vring
.desc
= desc
;
993 vdev
->vq
[n
].vring
.avail
= avail
;
994 vdev
->vq
[n
].vring
.used
= used
;
997 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
999 /* Don't allow guest to flip queue between existent and
1000 * nonexistent states, or to set it to an invalid size.
1002 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
1003 num
> VIRTQUEUE_MAX_SIZE
||
1007 vdev
->vq
[n
].vring
.num
= num
;
1010 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
)
1012 return QLIST_FIRST(&vdev
->vector_queues
[vector
]);
1015 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
)
1017 return QLIST_NEXT(vq
, node
);
1020 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
1022 return vdev
->vq
[n
].vring
.num
;
1025 int virtio_get_num_queues(VirtIODevice
*vdev
)
1029 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1030 if (!virtio_queue_get_num(vdev
, i
)) {
1038 int virtio_queue_get_id(VirtQueue
*vq
)
1040 VirtIODevice
*vdev
= vq
->vdev
;
1041 assert(vq
>= &vdev
->vq
[0] && vq
< &vdev
->vq
[VIRTIO_QUEUE_MAX
]);
1042 return vq
- &vdev
->vq
[0];
1045 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
1047 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1048 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1050 /* virtio-1 compliant devices cannot change the alignment */
1051 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1052 error_report("tried to modify queue alignment for virtio-1 device");
1055 /* Check that the transport told us it was going to do this
1056 * (so a buggy transport will immediately assert rather than
1057 * silently failing to migrate this state)
1059 assert(k
->has_variable_vring_alignment
);
1061 vdev
->vq
[n
].vring
.align
= align
;
1062 virtio_queue_update_rings(vdev
, n
);
1065 void virtio_queue_notify_vq(VirtQueue
*vq
)
1067 if (vq
->vring
.desc
&& vq
->handle_output
) {
1068 VirtIODevice
*vdev
= vq
->vdev
;
1070 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
1071 vq
->handle_output(vdev
, vq
);
1075 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
1077 virtio_queue_notify_vq(&vdev
->vq
[n
]);
1080 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
1082 return n
< VIRTIO_QUEUE_MAX
? vdev
->vq
[n
].vector
:
1086 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
1088 VirtQueue
*vq
= &vdev
->vq
[n
];
1090 if (n
< VIRTIO_QUEUE_MAX
) {
1091 if (vdev
->vector_queues
&&
1092 vdev
->vq
[n
].vector
!= VIRTIO_NO_VECTOR
) {
1093 QLIST_REMOVE(vq
, node
);
1095 vdev
->vq
[n
].vector
= vector
;
1096 if (vdev
->vector_queues
&&
1097 vector
!= VIRTIO_NO_VECTOR
) {
1098 QLIST_INSERT_HEAD(&vdev
->vector_queues
[vector
], vq
, node
);
1103 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
1104 void (*handle_output
)(VirtIODevice
*, VirtQueue
*))
1108 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1109 if (vdev
->vq
[i
].vring
.num
== 0)
1113 if (i
== VIRTIO_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
1116 vdev
->vq
[i
].vring
.num
= queue_size
;
1117 vdev
->vq
[i
].vring
.num_default
= queue_size
;
1118 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
1119 vdev
->vq
[i
].handle_output
= handle_output
;
1121 return &vdev
->vq
[i
];
1124 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
1126 if (n
< 0 || n
>= VIRTIO_QUEUE_MAX
) {
1130 vdev
->vq
[n
].vring
.num
= 0;
1131 vdev
->vq
[n
].vring
.num_default
= 0;
1134 void virtio_irq(VirtQueue
*vq
)
1136 trace_virtio_irq(vq
);
1137 vq
->vdev
->isr
|= 0x01;
1138 virtio_notify_vector(vq
->vdev
, vq
->vector
);
1141 static bool vring_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
1145 /* We need to expose used array entries before checking used event. */
1147 /* Always notify when queue is empty (when feature acknowledge) */
1148 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
1149 !vq
->inuse
&& vring_avail_idx(vq
) == vq
->last_avail_idx
) {
1153 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
1154 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
1157 v
= vq
->signalled_used_valid
;
1158 vq
->signalled_used_valid
= true;
1159 old
= vq
->signalled_used
;
1160 new = vq
->signalled_used
= vring_used_idx(vq
);
1161 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
1164 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
1166 if (!vring_notify(vdev
, vq
)) {
1170 trace_virtio_notify(vdev
, vq
);
1172 virtio_notify_vector(vdev
, vq
->vector
);
1175 void virtio_notify_config(VirtIODevice
*vdev
)
1177 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
1182 virtio_notify_vector(vdev
, vdev
->config_vector
);
1185 static bool virtio_device_endian_needed(void *opaque
)
1187 VirtIODevice
*vdev
= opaque
;
1189 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
1190 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1191 return vdev
->device_endian
!= virtio_default_endian();
1193 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1194 return vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_LITTLE
;
1197 static bool virtio_64bit_features_needed(void *opaque
)
1199 VirtIODevice
*vdev
= opaque
;
1201 return (vdev
->host_features
>> 32) != 0;
1204 static bool virtio_virtqueue_needed(void *opaque
)
1206 VirtIODevice
*vdev
= opaque
;
1208 return virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
);
1211 static bool virtio_ringsize_needed(void *opaque
)
1213 VirtIODevice
*vdev
= opaque
;
1216 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1217 if (vdev
->vq
[i
].vring
.num
!= vdev
->vq
[i
].vring
.num_default
) {
1224 static bool virtio_extra_state_needed(void *opaque
)
1226 VirtIODevice
*vdev
= opaque
;
1227 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1228 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1230 return k
->has_extra_state
&&
1231 k
->has_extra_state(qbus
->parent
);
1234 static const VMStateDescription vmstate_virtqueue
= {
1235 .name
= "virtqueue_state",
1237 .minimum_version_id
= 1,
1238 .fields
= (VMStateField
[]) {
1239 VMSTATE_UINT64(vring
.avail
, struct VirtQueue
),
1240 VMSTATE_UINT64(vring
.used
, struct VirtQueue
),
1241 VMSTATE_END_OF_LIST()
1245 static const VMStateDescription vmstate_virtio_virtqueues
= {
1246 .name
= "virtio/virtqueues",
1248 .minimum_version_id
= 1,
1249 .needed
= &virtio_virtqueue_needed
,
1250 .fields
= (VMStateField
[]) {
1251 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
1252 VIRTIO_QUEUE_MAX
, 0, vmstate_virtqueue
, VirtQueue
),
1253 VMSTATE_END_OF_LIST()
1257 static const VMStateDescription vmstate_ringsize
= {
1258 .name
= "ringsize_state",
1260 .minimum_version_id
= 1,
1261 .fields
= (VMStateField
[]) {
1262 VMSTATE_UINT32(vring
.num_default
, struct VirtQueue
),
1263 VMSTATE_END_OF_LIST()
1267 static const VMStateDescription vmstate_virtio_ringsize
= {
1268 .name
= "virtio/ringsize",
1270 .minimum_version_id
= 1,
1271 .needed
= &virtio_ringsize_needed
,
1272 .fields
= (VMStateField
[]) {
1273 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
1274 VIRTIO_QUEUE_MAX
, 0, vmstate_ringsize
, VirtQueue
),
1275 VMSTATE_END_OF_LIST()
1279 static int get_extra_state(QEMUFile
*f
, void *pv
, size_t size
)
1281 VirtIODevice
*vdev
= pv
;
1282 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1283 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1285 if (!k
->load_extra_state
) {
1288 return k
->load_extra_state(qbus
->parent
, f
);
1292 static void put_extra_state(QEMUFile
*f
, void *pv
, size_t size
)
1294 VirtIODevice
*vdev
= pv
;
1295 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1296 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1298 k
->save_extra_state(qbus
->parent
, f
);
1301 static const VMStateInfo vmstate_info_extra_state
= {
1302 .name
= "virtqueue_extra_state",
1303 .get
= get_extra_state
,
1304 .put
= put_extra_state
,
1307 static const VMStateDescription vmstate_virtio_extra_state
= {
1308 .name
= "virtio/extra_state",
1310 .minimum_version_id
= 1,
1311 .needed
= &virtio_extra_state_needed
,
1312 .fields
= (VMStateField
[]) {
1314 .name
= "extra_state",
1316 .field_exists
= NULL
,
1318 .info
= &vmstate_info_extra_state
,
1319 .flags
= VMS_SINGLE
,
1322 VMSTATE_END_OF_LIST()
1326 static const VMStateDescription vmstate_virtio_device_endian
= {
1327 .name
= "virtio/device_endian",
1329 .minimum_version_id
= 1,
1330 .needed
= &virtio_device_endian_needed
,
1331 .fields
= (VMStateField
[]) {
1332 VMSTATE_UINT8(device_endian
, VirtIODevice
),
1333 VMSTATE_END_OF_LIST()
1337 static const VMStateDescription vmstate_virtio_64bit_features
= {
1338 .name
= "virtio/64bit_features",
1340 .minimum_version_id
= 1,
1341 .needed
= &virtio_64bit_features_needed
,
1342 .fields
= (VMStateField
[]) {
1343 VMSTATE_UINT64(guest_features
, VirtIODevice
),
1344 VMSTATE_END_OF_LIST()
1348 static const VMStateDescription vmstate_virtio
= {
1351 .minimum_version_id
= 1,
1352 .minimum_version_id_old
= 1,
1353 .fields
= (VMStateField
[]) {
1354 VMSTATE_END_OF_LIST()
1356 .subsections
= (const VMStateDescription
*[]) {
1357 &vmstate_virtio_device_endian
,
1358 &vmstate_virtio_64bit_features
,
1359 &vmstate_virtio_virtqueues
,
1360 &vmstate_virtio_ringsize
,
1361 &vmstate_virtio_extra_state
,
1366 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
1368 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1369 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1370 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1371 uint32_t guest_features_lo
= (vdev
->guest_features
& 0xffffffff);
1374 if (k
->save_config
) {
1375 k
->save_config(qbus
->parent
, f
);
1378 qemu_put_8s(f
, &vdev
->status
);
1379 qemu_put_8s(f
, &vdev
->isr
);
1380 qemu_put_be16s(f
, &vdev
->queue_sel
);
1381 qemu_put_be32s(f
, &guest_features_lo
);
1382 qemu_put_be32(f
, vdev
->config_len
);
1383 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
1385 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1386 if (vdev
->vq
[i
].vring
.num
== 0)
1390 qemu_put_be32(f
, i
);
1392 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1393 if (vdev
->vq
[i
].vring
.num
== 0)
1396 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
1397 if (k
->has_variable_vring_alignment
) {
1398 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
1400 /* XXX virtio-1 devices */
1401 qemu_put_be64(f
, vdev
->vq
[i
].vring
.desc
);
1402 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
1403 if (k
->save_queue
) {
1404 k
->save_queue(qbus
->parent
, i
, f
);
1408 if (vdc
->save
!= NULL
) {
1413 vmstate_save_state(f
, &vmstate_virtio
, vdev
, NULL
);
1416 static int virtio_set_features_nocheck(VirtIODevice
*vdev
, uint64_t val
)
1418 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1419 bool bad
= (val
& ~(vdev
->host_features
)) != 0;
1421 val
&= vdev
->host_features
;
1422 if (k
->set_features
) {
1423 k
->set_features(vdev
, val
);
1425 vdev
->guest_features
= val
;
1426 return bad
? -1 : 0;
1429 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
)
1432 * The driver must not attempt to set features after feature negotiation
1435 if (vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
1438 return virtio_set_features_nocheck(vdev
, val
);
1441 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
)
1447 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1448 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1449 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1452 * We poison the endianness to ensure it does not get used before
1453 * subsections have been loaded.
1455 vdev
->device_endian
= VIRTIO_DEVICE_ENDIAN_UNKNOWN
;
1457 if (k
->load_config
) {
1458 ret
= k
->load_config(qbus
->parent
, f
);
1463 qemu_get_8s(f
, &vdev
->status
);
1464 qemu_get_8s(f
, &vdev
->isr
);
1465 qemu_get_be16s(f
, &vdev
->queue_sel
);
1466 if (vdev
->queue_sel
>= VIRTIO_QUEUE_MAX
) {
1469 qemu_get_be32s(f
, &features
);
1471 config_len
= qemu_get_be32(f
);
1474 * There are cases where the incoming config can be bigger or smaller
1475 * than what we have; so load what we have space for, and skip
1476 * any excess that's in the stream.
1478 qemu_get_buffer(f
, vdev
->config
, MIN(config_len
, vdev
->config_len
));
1480 while (config_len
> vdev
->config_len
) {
1485 num
= qemu_get_be32(f
);
1487 if (num
> VIRTIO_QUEUE_MAX
) {
1488 error_report("Invalid number of virtqueues: 0x%x", num
);
1492 for (i
= 0; i
< num
; i
++) {
1493 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
1494 if (k
->has_variable_vring_alignment
) {
1495 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
1497 vdev
->vq
[i
].vring
.desc
= qemu_get_be64(f
);
1498 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
1499 vdev
->vq
[i
].signalled_used_valid
= false;
1500 vdev
->vq
[i
].notification
= true;
1502 if (vdev
->vq
[i
].vring
.desc
) {
1503 /* XXX virtio-1 devices */
1504 virtio_queue_update_rings(vdev
, i
);
1505 } else if (vdev
->vq
[i
].last_avail_idx
) {
1506 error_report("VQ %d address 0x0 "
1507 "inconsistent with Host index 0x%x",
1508 i
, vdev
->vq
[i
].last_avail_idx
);
1511 if (k
->load_queue
) {
1512 ret
= k
->load_queue(qbus
->parent
, i
, f
);
1518 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
1520 if (vdc
->load
!= NULL
) {
1521 ret
= vdc
->load(vdev
, f
, version_id
);
1528 ret
= vmstate_load_state(f
, &vmstate_virtio
, vdev
, 1);
1533 if (vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_UNKNOWN
) {
1534 vdev
->device_endian
= virtio_default_endian();
1537 if (virtio_64bit_features_needed(vdev
)) {
1539 * Subsection load filled vdev->guest_features. Run them
1540 * through virtio_set_features to sanity-check them against
1543 uint64_t features64
= vdev
->guest_features
;
1544 if (virtio_set_features_nocheck(vdev
, features64
) < 0) {
1545 error_report("Features 0x%" PRIx64
" unsupported. "
1546 "Allowed features: 0x%" PRIx64
,
1547 features64
, vdev
->host_features
);
1551 if (virtio_set_features_nocheck(vdev
, features
) < 0) {
1552 error_report("Features 0x%x unsupported. "
1553 "Allowed features: 0x%" PRIx64
,
1554 features
, vdev
->host_features
);
1559 for (i
= 0; i
< num
; i
++) {
1560 if (vdev
->vq
[i
].vring
.desc
) {
1562 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
1563 /* Check it isn't doing strange things with descriptor numbers. */
1564 if (nheads
> vdev
->vq
[i
].vring
.num
) {
1565 error_report("VQ %d size 0x%x Guest index 0x%x "
1566 "inconsistent with Host index 0x%x: delta 0x%x",
1567 i
, vdev
->vq
[i
].vring
.num
,
1568 vring_avail_idx(&vdev
->vq
[i
]),
1569 vdev
->vq
[i
].last_avail_idx
, nheads
);
1578 void virtio_cleanup(VirtIODevice
*vdev
)
1580 qemu_del_vm_change_state_handler(vdev
->vmstate
);
1581 g_free(vdev
->config
);
1583 g_free(vdev
->vector_queues
);
1586 static void virtio_vmstate_change(void *opaque
, int running
, RunState state
)
1588 VirtIODevice
*vdev
= opaque
;
1589 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1590 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1591 bool backend_run
= running
&& (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
);
1592 vdev
->vm_running
= running
;
1595 virtio_set_status(vdev
, vdev
->status
);
1598 if (k
->vmstate_change
) {
1599 k
->vmstate_change(qbus
->parent
, backend_run
);
1603 virtio_set_status(vdev
, vdev
->status
);
1607 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
1608 size_t vdev_size
, const char *vdev_name
)
1610 DeviceState
*vdev
= data
;
1612 object_initialize(vdev
, vdev_size
, vdev_name
);
1613 object_property_add_child(proxy_obj
, "virtio-backend", OBJECT(vdev
), NULL
);
1614 object_unref(OBJECT(vdev
));
1615 qdev_alias_all_properties(vdev
, proxy_obj
);
1618 void virtio_init(VirtIODevice
*vdev
, const char *name
,
1619 uint16_t device_id
, size_t config_size
)
1621 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1622 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1624 int nvectors
= k
->query_nvectors
? k
->query_nvectors(qbus
->parent
) : 0;
1627 vdev
->vector_queues
=
1628 g_malloc0(sizeof(*vdev
->vector_queues
) * nvectors
);
1631 vdev
->device_id
= device_id
;
1634 vdev
->queue_sel
= 0;
1635 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
1636 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_QUEUE_MAX
);
1637 vdev
->vm_running
= runstate_is_running();
1638 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
1639 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
1640 vdev
->vq
[i
].vdev
= vdev
;
1641 vdev
->vq
[i
].queue_index
= i
;
1645 vdev
->config_len
= config_size
;
1646 if (vdev
->config_len
) {
1647 vdev
->config
= g_malloc0(config_size
);
1649 vdev
->config
= NULL
;
1651 vdev
->vmstate
= qemu_add_vm_change_state_handler(virtio_vmstate_change
,
1653 vdev
->device_endian
= virtio_default_endian();
1656 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
1658 return vdev
->vq
[n
].vring
.desc
;
1661 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
1663 return vdev
->vq
[n
].vring
.avail
;
1666 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
1668 return vdev
->vq
[n
].vring
.used
;
1671 hwaddr
virtio_queue_get_ring_addr(VirtIODevice
*vdev
, int n
)
1673 return vdev
->vq
[n
].vring
.desc
;
1676 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
1678 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
1681 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
1683 return offsetof(VRingAvail
, ring
) +
1684 sizeof(uint16_t) * vdev
->vq
[n
].vring
.num
;
1687 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
1689 return offsetof(VRingUsed
, ring
) +
1690 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
;
1693 hwaddr
virtio_queue_get_ring_size(VirtIODevice
*vdev
, int n
)
1695 return vdev
->vq
[n
].vring
.used
- vdev
->vq
[n
].vring
.desc
+
1696 virtio_queue_get_used_size(vdev
, n
);
1699 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
1701 return vdev
->vq
[n
].last_avail_idx
;
1704 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
, uint16_t idx
)
1706 vdev
->vq
[n
].last_avail_idx
= idx
;
1709 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
1711 vdev
->vq
[n
].signalled_used_valid
= false;
1714 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
1716 return vdev
->vq
+ n
;
1719 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
1721 return vq
->queue_index
;
1724 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
1726 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
1727 if (event_notifier_test_and_clear(n
)) {
1732 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1735 if (assign
&& !with_irqfd
) {
1736 event_notifier_set_handler(&vq
->guest_notifier
,
1737 virtio_queue_guest_notifier_read
);
1739 event_notifier_set_handler(&vq
->guest_notifier
, NULL
);
1742 /* Test and clear notifier before closing it,
1743 * in case poll callback didn't have time to run. */
1744 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
1748 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
1750 return &vq
->guest_notifier
;
1753 static void virtio_queue_host_notifier_read(EventNotifier
*n
)
1755 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
1756 if (event_notifier_test_and_clear(n
)) {
1757 virtio_queue_notify_vq(vq
);
1761 void virtio_queue_set_host_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1764 if (assign
&& set_handler
) {
1765 event_notifier_set_handler(&vq
->host_notifier
,
1766 virtio_queue_host_notifier_read
);
1768 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
1771 /* Test and clear notifier before after disabling event,
1772 * in case poll callback didn't have time to run. */
1773 virtio_queue_host_notifier_read(&vq
->host_notifier
);
1777 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
1779 return &vq
->host_notifier
;
1782 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
1784 g_free(vdev
->bus_name
);
1785 vdev
->bus_name
= g_strdup(bus_name
);
1788 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
1790 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1791 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
1794 if (vdc
->realize
!= NULL
) {
1795 vdc
->realize(dev
, &err
);
1797 error_propagate(errp
, err
);
1802 virtio_bus_device_plugged(vdev
, &err
);
1804 error_propagate(errp
, err
);
1809 static void virtio_device_unrealize(DeviceState
*dev
, Error
**errp
)
1811 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1812 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
1815 virtio_bus_device_unplugged(vdev
);
1817 if (vdc
->unrealize
!= NULL
) {
1818 vdc
->unrealize(dev
, &err
);
1820 error_propagate(errp
, err
);
1825 g_free(vdev
->bus_name
);
1826 vdev
->bus_name
= NULL
;
1829 static Property virtio_properties
[] = {
1830 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice
, host_features
),
1831 DEFINE_PROP_END_OF_LIST(),
1834 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
1836 /* Set the default value here. */
1837 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1839 dc
->realize
= virtio_device_realize
;
1840 dc
->unrealize
= virtio_device_unrealize
;
1841 dc
->bus_type
= TYPE_VIRTIO_BUS
;
1842 dc
->props
= virtio_properties
;
1845 static const TypeInfo virtio_device_info
= {
1846 .name
= TYPE_VIRTIO_DEVICE
,
1847 .parent
= TYPE_DEVICE
,
1848 .instance_size
= sizeof(VirtIODevice
),
1849 .class_init
= virtio_device_class_init
,
1851 .class_size
= sizeof(VirtioDeviceClass
),
1854 static void virtio_register_types(void)
1856 type_register_static(&virtio_device_info
);
1859 type_init(virtio_register_types
)