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 "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"
24 * The alignment to use between consumer and producer parts of vring.
25 * x86 pagesize again. This is the default, used by transports like PCI
26 * which don't provide a means for the guest to tell the host the alignment.
28 #define VIRTIO_PCI_VRING_ALIGN 4096
30 typedef struct VRingDesc
38 typedef struct VRingAvail
45 typedef struct VRingUsedElem
51 typedef struct VRingUsed
55 VRingUsedElem ring
[0];
71 uint16_t last_avail_idx
;
72 /* Last used index value we have signalled on */
73 uint16_t signalled_used
;
75 /* Last used index value we have signalled on */
76 bool signalled_used_valid
;
78 /* Notification enabled? */
86 void (*handle_output
)(VirtIODevice
*vdev
, VirtQueue
*vq
);
88 EventNotifier guest_notifier
;
89 EventNotifier host_notifier
;
92 /* virt queue functions */
93 static void virtqueue_init(VirtQueue
*vq
)
98 vq
->vring
.avail
= pa
+ vq
->vring
.num
* sizeof(VRingDesc
);
99 vq
->vring
.used
= vring_align(vq
->vring
.avail
+
100 offsetof(VRingAvail
, ring
[vq
->vring
.num
]),
104 static inline uint64_t vring_desc_addr(hwaddr desc_pa
, int i
)
107 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, addr
);
111 static inline uint32_t vring_desc_len(hwaddr desc_pa
, int i
)
114 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, len
);
115 return ldl_phys(&address_space_memory
, pa
);
118 static inline uint16_t vring_desc_flags(hwaddr desc_pa
, int i
)
121 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, flags
);
122 return lduw_phys(pa
);
125 static inline uint16_t vring_desc_next(hwaddr desc_pa
, int i
)
128 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, next
);
129 return lduw_phys(pa
);
132 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
135 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, flags
);
136 return lduw_phys(pa
);
139 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
142 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, idx
);
143 return lduw_phys(pa
);
146 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
149 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, ring
[i
]);
150 return lduw_phys(pa
);
153 static inline uint16_t vring_used_event(VirtQueue
*vq
)
155 return vring_avail_ring(vq
, vq
->vring
.num
);
158 static inline void vring_used_ring_id(VirtQueue
*vq
, int i
, uint32_t val
)
161 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].id
);
165 static inline void vring_used_ring_len(VirtQueue
*vq
, int i
, uint32_t val
)
168 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].len
);
172 static uint16_t vring_used_idx(VirtQueue
*vq
)
175 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
176 return lduw_phys(pa
);
179 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
182 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
186 static inline void vring_used_flags_set_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_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
196 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
197 stw_phys(pa
, lduw_phys(pa
) & ~mask
);
200 static inline void vring_avail_event(VirtQueue
*vq
, uint16_t val
)
203 if (!vq
->notification
) {
206 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
210 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
212 vq
->notification
= enable
;
213 if (vq
->vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
214 vring_avail_event(vq
, vring_avail_idx(vq
));
216 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
218 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
221 /* Expose avail event/used flags before caller checks the avail idx. */
226 int virtio_queue_ready(VirtQueue
*vq
)
228 return vq
->vring
.avail
!= 0;
231 int virtio_queue_empty(VirtQueue
*vq
)
233 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
236 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
237 unsigned int len
, unsigned int idx
)
242 trace_virtqueue_fill(vq
, elem
, len
, idx
);
245 for (i
= 0; i
< elem
->in_num
; i
++) {
246 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
248 cpu_physical_memory_unmap(elem
->in_sg
[i
].iov_base
,
249 elem
->in_sg
[i
].iov_len
,
255 for (i
= 0; i
< elem
->out_num
; i
++)
256 cpu_physical_memory_unmap(elem
->out_sg
[i
].iov_base
,
257 elem
->out_sg
[i
].iov_len
,
258 0, elem
->out_sg
[i
].iov_len
);
260 idx
= (idx
+ vring_used_idx(vq
)) % vq
->vring
.num
;
262 /* Get a pointer to the next entry in the used ring. */
263 vring_used_ring_id(vq
, idx
, elem
->index
);
264 vring_used_ring_len(vq
, idx
, len
);
267 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
270 /* Make sure buffer is written before we update index. */
272 trace_virtqueue_flush(vq
, count
);
273 old
= vring_used_idx(vq
);
275 vring_used_idx_set(vq
, new);
277 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
278 vq
->signalled_used_valid
= false;
281 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
284 virtqueue_fill(vq
, elem
, len
, 0);
285 virtqueue_flush(vq
, 1);
288 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
290 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
292 /* Check it isn't doing very strange things with descriptor numbers. */
293 if (num_heads
> vq
->vring
.num
) {
294 error_report("Guest moved used index from %u to %u",
295 idx
, vring_avail_idx(vq
));
298 /* On success, callers read a descriptor at vq->last_avail_idx.
299 * Make sure descriptor read does not bypass avail index read. */
307 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
311 /* Grab the next descriptor number they're advertising, and increment
312 * the index we've seen. */
313 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
315 /* If their number is silly, that's a fatal mistake. */
316 if (head
>= vq
->vring
.num
) {
317 error_report("Guest says index %u is available", head
);
324 static unsigned virtqueue_next_desc(hwaddr desc_pa
,
325 unsigned int i
, unsigned int max
)
329 /* If this descriptor says it doesn't chain, we're done. */
330 if (!(vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_NEXT
))
333 /* Check they're not leading us off end of descriptors. */
334 next
= vring_desc_next(desc_pa
, i
);
335 /* Make sure compiler knows to grab that: we don't want it changing! */
339 error_report("Desc next is %u", next
);
346 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
347 unsigned int *out_bytes
,
348 unsigned max_in_bytes
, unsigned max_out_bytes
)
351 unsigned int total_bufs
, in_total
, out_total
;
353 idx
= vq
->last_avail_idx
;
355 total_bufs
= in_total
= out_total
= 0;
356 while (virtqueue_num_heads(vq
, idx
)) {
357 unsigned int max
, num_bufs
, indirect
= 0;
362 num_bufs
= total_bufs
;
363 i
= virtqueue_get_head(vq
, idx
++);
364 desc_pa
= vq
->vring
.desc
;
366 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
367 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
368 error_report("Invalid size for indirect buffer table");
372 /* If we've got too many, that implies a descriptor loop. */
373 if (num_bufs
>= max
) {
374 error_report("Looped descriptor");
378 /* loop over the indirect descriptor table */
380 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
381 desc_pa
= vring_desc_addr(desc_pa
, i
);
386 /* If we've got too many, that implies a descriptor loop. */
387 if (++num_bufs
> max
) {
388 error_report("Looped descriptor");
392 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
393 in_total
+= vring_desc_len(desc_pa
, i
);
395 out_total
+= vring_desc_len(desc_pa
, i
);
397 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
400 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
403 total_bufs
= num_bufs
;
409 *in_bytes
= in_total
;
412 *out_bytes
= out_total
;
416 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
417 unsigned int out_bytes
)
419 unsigned int in_total
, out_total
;
421 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
422 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
425 void virtqueue_map_sg(struct iovec
*sg
, hwaddr
*addr
,
426 size_t num_sg
, int is_write
)
431 for (i
= 0; i
< num_sg
; i
++) {
433 sg
[i
].iov_base
= cpu_physical_memory_map(addr
[i
], &len
, is_write
);
434 if (sg
[i
].iov_base
== NULL
|| len
!= sg
[i
].iov_len
) {
435 error_report("virtio: trying to map MMIO memory");
441 int virtqueue_pop(VirtQueue
*vq
, VirtQueueElement
*elem
)
443 unsigned int i
, head
, max
;
444 hwaddr desc_pa
= vq
->vring
.desc
;
446 if (!virtqueue_num_heads(vq
, vq
->last_avail_idx
))
449 /* When we start there are none of either input nor output. */
450 elem
->out_num
= elem
->in_num
= 0;
454 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
455 if (vq
->vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
)) {
456 vring_avail_event(vq
, vring_avail_idx(vq
));
459 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
460 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
461 error_report("Invalid size for indirect buffer table");
465 /* loop over the indirect descriptor table */
466 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
467 desc_pa
= vring_desc_addr(desc_pa
, i
);
471 /* Collect all the descriptors */
475 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
476 if (elem
->in_num
>= ARRAY_SIZE(elem
->in_sg
)) {
477 error_report("Too many write descriptors in indirect table");
480 elem
->in_addr
[elem
->in_num
] = vring_desc_addr(desc_pa
, i
);
481 sg
= &elem
->in_sg
[elem
->in_num
++];
483 if (elem
->out_num
>= ARRAY_SIZE(elem
->out_sg
)) {
484 error_report("Too many read descriptors in indirect table");
487 elem
->out_addr
[elem
->out_num
] = vring_desc_addr(desc_pa
, i
);
488 sg
= &elem
->out_sg
[elem
->out_num
++];
491 sg
->iov_len
= vring_desc_len(desc_pa
, i
);
493 /* If we've got too many, that implies a descriptor loop. */
494 if ((elem
->in_num
+ elem
->out_num
) > max
) {
495 error_report("Looped descriptor");
498 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
500 /* Now map what we have collected */
501 virtqueue_map_sg(elem
->in_sg
, elem
->in_addr
, elem
->in_num
, 1);
502 virtqueue_map_sg(elem
->out_sg
, elem
->out_addr
, elem
->out_num
, 0);
508 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
509 return elem
->in_num
+ elem
->out_num
;
513 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
515 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
516 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
519 k
->notify(qbus
->parent
, vector
);
523 void virtio_update_irq(VirtIODevice
*vdev
)
525 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
528 void virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
530 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
531 trace_virtio_set_status(vdev
, val
);
534 k
->set_status(vdev
, val
);
539 void virtio_reset(void *opaque
)
541 VirtIODevice
*vdev
= opaque
;
542 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
545 virtio_set_status(vdev
, 0);
551 vdev
->guest_features
= 0;
555 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
556 virtio_notify_vector(vdev
, vdev
->config_vector
);
558 for(i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
559 vdev
->vq
[i
].vring
.desc
= 0;
560 vdev
->vq
[i
].vring
.avail
= 0;
561 vdev
->vq
[i
].vring
.used
= 0;
562 vdev
->vq
[i
].last_avail_idx
= 0;
564 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
565 vdev
->vq
[i
].signalled_used
= 0;
566 vdev
->vq
[i
].signalled_used_valid
= false;
567 vdev
->vq
[i
].notification
= true;
571 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
573 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
576 if (addr
+ sizeof(val
) > vdev
->config_len
) {
580 k
->get_config(vdev
, vdev
->config
);
582 val
= ldub_p(vdev
->config
+ addr
);
586 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
588 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
591 if (addr
+ sizeof(val
) > vdev
->config_len
) {
595 k
->get_config(vdev
, vdev
->config
);
597 val
= lduw_p(vdev
->config
+ addr
);
601 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
603 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
606 if (addr
+ sizeof(val
) > vdev
->config_len
) {
610 k
->get_config(vdev
, vdev
->config
);
612 val
= ldl_p(vdev
->config
+ addr
);
616 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
618 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
621 if (addr
+ sizeof(val
) > vdev
->config_len
) {
625 stb_p(vdev
->config
+ addr
, val
);
628 k
->set_config(vdev
, vdev
->config
);
632 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
634 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
637 if (addr
+ sizeof(val
) > vdev
->config_len
) {
641 stw_p(vdev
->config
+ addr
, val
);
644 k
->set_config(vdev
, vdev
->config
);
648 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
650 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
653 if (addr
+ sizeof(val
) > vdev
->config_len
) {
657 stl_p(vdev
->config
+ addr
, val
);
660 k
->set_config(vdev
, vdev
->config
);
664 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
666 vdev
->vq
[n
].pa
= addr
;
667 virtqueue_init(&vdev
->vq
[n
]);
670 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
672 return vdev
->vq
[n
].pa
;
675 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
677 /* Don't allow guest to flip queue between existent and
678 * nonexistent states, or to set it to an invalid size.
680 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
681 num
> VIRTQUEUE_MAX_SIZE
||
685 vdev
->vq
[n
].vring
.num
= num
;
686 virtqueue_init(&vdev
->vq
[n
]);
689 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
691 return vdev
->vq
[n
].vring
.num
;
694 int virtio_queue_get_id(VirtQueue
*vq
)
696 VirtIODevice
*vdev
= vq
->vdev
;
697 assert(vq
>= &vdev
->vq
[0] && vq
< &vdev
->vq
[VIRTIO_PCI_QUEUE_MAX
]);
698 return vq
- &vdev
->vq
[0];
701 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
703 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
704 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
706 /* Check that the transport told us it was going to do this
707 * (so a buggy transport will immediately assert rather than
708 * silently failing to migrate this state)
710 assert(k
->has_variable_vring_alignment
);
712 vdev
->vq
[n
].vring
.align
= align
;
713 virtqueue_init(&vdev
->vq
[n
]);
716 void virtio_queue_notify_vq(VirtQueue
*vq
)
718 if (vq
->vring
.desc
) {
719 VirtIODevice
*vdev
= vq
->vdev
;
720 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
721 vq
->handle_output(vdev
, vq
);
725 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
727 virtio_queue_notify_vq(&vdev
->vq
[n
]);
730 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
732 return n
< VIRTIO_PCI_QUEUE_MAX
? vdev
->vq
[n
].vector
:
736 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
738 if (n
< VIRTIO_PCI_QUEUE_MAX
)
739 vdev
->vq
[n
].vector
= vector
;
742 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
743 void (*handle_output
)(VirtIODevice
*, VirtQueue
*))
747 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
748 if (vdev
->vq
[i
].vring
.num
== 0)
752 if (i
== VIRTIO_PCI_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
755 vdev
->vq
[i
].vring
.num
= queue_size
;
756 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
757 vdev
->vq
[i
].handle_output
= handle_output
;
762 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
764 if (n
< 0 || n
>= VIRTIO_PCI_QUEUE_MAX
) {
768 vdev
->vq
[n
].vring
.num
= 0;
771 void virtio_irq(VirtQueue
*vq
)
773 trace_virtio_irq(vq
);
774 vq
->vdev
->isr
|= 0x01;
775 virtio_notify_vector(vq
->vdev
, vq
->vector
);
778 /* Assuming a given event_idx value from the other size, if
779 * we have just incremented index from old to new_idx,
780 * should we trigger an event? */
781 static inline int vring_need_event(uint16_t event
, uint16_t new, uint16_t old
)
783 /* Note: Xen has similar logic for notification hold-off
784 * in include/xen/interface/io/ring.h with req_event and req_prod
785 * corresponding to event_idx + 1 and new respectively.
786 * Note also that req_event and req_prod in Xen start at 1,
787 * event indexes in virtio start at 0. */
788 return (uint16_t)(new - event
- 1) < (uint16_t)(new - old
);
791 static bool vring_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
795 /* We need to expose used array entries before checking used event. */
797 /* Always notify when queue is empty (when feature acknowledge) */
798 if (((vdev
->guest_features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) &&
799 !vq
->inuse
&& vring_avail_idx(vq
) == vq
->last_avail_idx
)) {
803 if (!(vdev
->guest_features
& (1 << VIRTIO_RING_F_EVENT_IDX
))) {
804 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
807 v
= vq
->signalled_used_valid
;
808 vq
->signalled_used_valid
= true;
809 old
= vq
->signalled_used
;
810 new = vq
->signalled_used
= vring_used_idx(vq
);
811 return !v
|| vring_need_event(vring_used_event(vq
), new, old
);
814 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
816 if (!vring_notify(vdev
, vq
)) {
820 trace_virtio_notify(vdev
, vq
);
822 virtio_notify_vector(vdev
, vq
->vector
);
825 void virtio_notify_config(VirtIODevice
*vdev
)
827 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
831 virtio_notify_vector(vdev
, vdev
->config_vector
);
834 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
836 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
837 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
840 if (k
->save_config
) {
841 k
->save_config(qbus
->parent
, f
);
844 qemu_put_8s(f
, &vdev
->status
);
845 qemu_put_8s(f
, &vdev
->isr
);
846 qemu_put_be16s(f
, &vdev
->queue_sel
);
847 qemu_put_be32s(f
, &vdev
->guest_features
);
848 qemu_put_be32(f
, vdev
->config_len
);
849 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
851 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
852 if (vdev
->vq
[i
].vring
.num
== 0)
858 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
859 if (vdev
->vq
[i
].vring
.num
== 0)
862 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
863 if (k
->has_variable_vring_alignment
) {
864 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
866 qemu_put_be64(f
, vdev
->vq
[i
].pa
);
867 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
869 k
->save_queue(qbus
->parent
, i
, f
);
874 int virtio_set_features(VirtIODevice
*vdev
, uint32_t val
)
876 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
877 VirtioBusClass
*vbusk
= VIRTIO_BUS_GET_CLASS(qbus
);
878 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
879 uint32_t supported_features
= vbusk
->get_features(qbus
->parent
);
880 bool bad
= (val
& ~supported_features
) != 0;
882 val
&= supported_features
;
883 if (k
->set_features
) {
884 k
->set_features(vdev
, val
);
886 vdev
->guest_features
= val
;
890 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
)
894 uint32_t supported_features
;
895 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
896 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
898 if (k
->load_config
) {
899 ret
= k
->load_config(qbus
->parent
, f
);
904 qemu_get_8s(f
, &vdev
->status
);
905 qemu_get_8s(f
, &vdev
->isr
);
906 qemu_get_be16s(f
, &vdev
->queue_sel
);
907 qemu_get_be32s(f
, &features
);
909 if (virtio_set_features(vdev
, features
) < 0) {
910 supported_features
= k
->get_features(qbus
->parent
);
911 error_report("Features 0x%x unsupported. Allowed features: 0x%x",
912 features
, supported_features
);
915 vdev
->config_len
= qemu_get_be32(f
);
916 qemu_get_buffer(f
, vdev
->config
, vdev
->config_len
);
918 num
= qemu_get_be32(f
);
920 for (i
= 0; i
< num
; i
++) {
921 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
922 if (k
->has_variable_vring_alignment
) {
923 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
925 vdev
->vq
[i
].pa
= qemu_get_be64(f
);
926 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
927 vdev
->vq
[i
].signalled_used_valid
= false;
928 vdev
->vq
[i
].notification
= true;
930 if (vdev
->vq
[i
].pa
) {
932 virtqueue_init(&vdev
->vq
[i
]);
933 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
934 /* Check it isn't doing very strange things with descriptor numbers. */
935 if (nheads
> vdev
->vq
[i
].vring
.num
) {
936 error_report("VQ %d size 0x%x Guest index 0x%x "
937 "inconsistent with Host index 0x%x: delta 0x%x",
938 i
, vdev
->vq
[i
].vring
.num
,
939 vring_avail_idx(&vdev
->vq
[i
]),
940 vdev
->vq
[i
].last_avail_idx
, nheads
);
943 } else if (vdev
->vq
[i
].last_avail_idx
) {
944 error_report("VQ %d address 0x0 "
945 "inconsistent with Host index 0x%x",
946 i
, vdev
->vq
[i
].last_avail_idx
);
950 ret
= k
->load_queue(qbus
->parent
, i
, f
);
956 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
960 void virtio_cleanup(VirtIODevice
*vdev
)
962 qemu_del_vm_change_state_handler(vdev
->vmstate
);
963 g_free(vdev
->config
);
967 static void virtio_vmstate_change(void *opaque
, int running
, RunState state
)
969 VirtIODevice
*vdev
= opaque
;
970 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
971 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
972 bool backend_run
= running
&& (vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
);
973 vdev
->vm_running
= running
;
976 virtio_set_status(vdev
, vdev
->status
);
979 if (k
->vmstate_change
) {
980 k
->vmstate_change(qbus
->parent
, backend_run
);
984 virtio_set_status(vdev
, vdev
->status
);
988 void virtio_init(VirtIODevice
*vdev
, const char *name
,
989 uint16_t device_id
, size_t config_size
)
992 vdev
->device_id
= device_id
;
996 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
997 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_PCI_QUEUE_MAX
);
998 vdev
->vm_running
= runstate_is_running();
999 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
1000 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
1001 vdev
->vq
[i
].vdev
= vdev
;
1002 vdev
->vq
[i
].queue_index
= i
;
1006 vdev
->config_len
= config_size
;
1007 if (vdev
->config_len
) {
1008 vdev
->config
= g_malloc0(config_size
);
1010 vdev
->config
= NULL
;
1012 vdev
->vmstate
= qemu_add_vm_change_state_handler(virtio_vmstate_change
,
1016 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
1018 return vdev
->vq
[n
].vring
.desc
;
1021 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
1023 return vdev
->vq
[n
].vring
.avail
;
1026 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
1028 return vdev
->vq
[n
].vring
.used
;
1031 hwaddr
virtio_queue_get_ring_addr(VirtIODevice
*vdev
, int n
)
1033 return vdev
->vq
[n
].vring
.desc
;
1036 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
1038 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
1041 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
1043 return offsetof(VRingAvail
, ring
) +
1044 sizeof(uint64_t) * vdev
->vq
[n
].vring
.num
;
1047 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
1049 return offsetof(VRingUsed
, ring
) +
1050 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
;
1053 hwaddr
virtio_queue_get_ring_size(VirtIODevice
*vdev
, int n
)
1055 return vdev
->vq
[n
].vring
.used
- vdev
->vq
[n
].vring
.desc
+
1056 virtio_queue_get_used_size(vdev
, n
);
1059 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
1061 return vdev
->vq
[n
].last_avail_idx
;
1064 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
, uint16_t idx
)
1066 vdev
->vq
[n
].last_avail_idx
= idx
;
1069 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
1071 vdev
->vq
[n
].signalled_used_valid
= false;
1074 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
1076 return vdev
->vq
+ n
;
1079 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
1081 return vq
->queue_index
;
1084 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
1086 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
1087 if (event_notifier_test_and_clear(n
)) {
1092 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1095 if (assign
&& !with_irqfd
) {
1096 event_notifier_set_handler(&vq
->guest_notifier
,
1097 virtio_queue_guest_notifier_read
);
1099 event_notifier_set_handler(&vq
->guest_notifier
, NULL
);
1102 /* Test and clear notifier before closing it,
1103 * in case poll callback didn't have time to run. */
1104 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
1108 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
1110 return &vq
->guest_notifier
;
1113 static void virtio_queue_host_notifier_read(EventNotifier
*n
)
1115 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
1116 if (event_notifier_test_and_clear(n
)) {
1117 virtio_queue_notify_vq(vq
);
1121 void virtio_queue_set_host_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
1124 if (assign
&& set_handler
) {
1125 event_notifier_set_handler(&vq
->host_notifier
,
1126 virtio_queue_host_notifier_read
);
1128 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
1131 /* Test and clear notifier before after disabling event,
1132 * in case poll callback didn't have time to run. */
1133 virtio_queue_host_notifier_read(&vq
->host_notifier
);
1137 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
1139 return &vq
->host_notifier
;
1142 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
1144 if (vdev
->bus_name
) {
1145 g_free(vdev
->bus_name
);
1146 vdev
->bus_name
= NULL
;
1150 vdev
->bus_name
= g_strdup(bus_name
);
1154 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
1156 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1157 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
1160 if (vdc
->realize
!= NULL
) {
1161 vdc
->realize(dev
, &err
);
1163 error_propagate(errp
, err
);
1167 virtio_bus_device_plugged(vdev
);
1170 static void virtio_device_unrealize(DeviceState
*dev
, Error
**errp
)
1172 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1173 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
1176 virtio_bus_device_unplugged(vdev
);
1178 if (vdc
->unrealize
!= NULL
) {
1179 vdc
->unrealize(dev
, &err
);
1181 error_propagate(errp
, err
);
1186 if (vdev
->bus_name
) {
1187 g_free(vdev
->bus_name
);
1188 vdev
->bus_name
= NULL
;
1192 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
1194 /* Set the default value here. */
1195 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1197 dc
->realize
= virtio_device_realize
;
1198 dc
->unrealize
= virtio_device_unrealize
;
1199 dc
->bus_type
= TYPE_VIRTIO_BUS
;
1202 static const TypeInfo virtio_device_info
= {
1203 .name
= TYPE_VIRTIO_DEVICE
,
1204 .parent
= TYPE_DEVICE
,
1205 .instance_size
= sizeof(VirtIODevice
),
1206 .class_init
= virtio_device_class_init
,
1208 .class_size
= sizeof(VirtioDeviceClass
),
1211 static void virtio_register_types(void)
1213 type_register_static(&virtio_device_info
);
1216 type_init(virtio_register_types
)