4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #include "qemu-error.h"
20 /* The alignment to use between consumer and producer parts of vring.
21 * x86 pagesize again. */
22 #define VIRTIO_PCI_VRING_ALIGN 4096
24 /* QEMU doesn't strictly need write barriers since everything runs in
25 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
26 * KVM or if kqemu gets SMP support.
27 * In any case, we must prevent the compiler from reordering the code.
28 * TODO: we likely need some rmb()/mb() as well.
31 #define wmb() __asm__ __volatile__("": : :"memory")
33 typedef struct VRingDesc
41 typedef struct VRingAvail
48 typedef struct VRingUsedElem
54 typedef struct VRingUsed
58 VRingUsedElem ring[0];
64 target_phys_addr_t desc;
65 target_phys_addr_t avail;
66 target_phys_addr_t used;
72 target_phys_addr_t pa;
73 uint16_t last_avail_idx;
76 void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
78 EventNotifier guest_notifier;
79 EventNotifier host_notifier;
82 /* virt queue functions */
83 static void virtqueue_init(VirtQueue *vq)
85 target_phys_addr_t pa = vq->pa;
88 vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
89 vq->vring.used = vring_align(vq->vring.avail +
90 offsetof(VRingAvail, ring[vq->vring.num]),
91 VIRTIO_PCI_VRING_ALIGN);
94 static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa, int i)
96 target_phys_addr_t pa;
97 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
101 static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa, int i)
103 target_phys_addr_t pa;
104 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
108 static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa, int i)
110 target_phys_addr_t pa;
111 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
112 return lduw_phys(pa);
115 static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa, int i)
117 target_phys_addr_t pa;
118 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
119 return lduw_phys(pa);
122 static inline uint16_t vring_avail_flags(VirtQueue *vq)
124 target_phys_addr_t pa;
125 pa = vq->vring.avail + offsetof(VRingAvail, flags);
126 return lduw_phys(pa);
129 static inline uint16_t vring_avail_idx(VirtQueue *vq)
131 target_phys_addr_t pa;
132 pa = vq->vring.avail + offsetof(VRingAvail, idx);
133 return lduw_phys(pa);
136 static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
138 target_phys_addr_t pa;
139 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
140 return lduw_phys(pa);
143 static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val)
145 target_phys_addr_t pa;
146 pa = vq->vring.used + offsetof(VRingUsed, ring[i].id);
150 static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val)
152 target_phys_addr_t pa;
153 pa = vq->vring.used + offsetof(VRingUsed, ring[i].len);
157 static uint16_t vring_used_idx(VirtQueue *vq)
159 target_phys_addr_t pa;
160 pa = vq->vring.used + offsetof(VRingUsed, idx);
161 return lduw_phys(pa);
164 static inline void vring_used_idx_increment(VirtQueue *vq, uint16_t val)
166 target_phys_addr_t pa;
167 pa = vq->vring.used + offsetof(VRingUsed, idx);
168 stw_phys(pa, vring_used_idx(vq) + val);
171 static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
173 target_phys_addr_t pa;
174 pa = vq->vring.used + offsetof(VRingUsed, flags);
175 stw_phys(pa, lduw_phys(pa) | mask);
178 static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
180 target_phys_addr_t pa;
181 pa = vq->vring.used + offsetof(VRingUsed, flags);
182 stw_phys(pa, lduw_phys(pa) & ~mask);
185 void virtio_queue_set_notification(VirtQueue *vq, int enable)
188 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
190 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
193 int virtio_queue_ready(VirtQueue *vq)
195 return vq->vring.avail != 0;
198 int virtio_queue_empty(VirtQueue *vq)
200 return vring_avail_idx(vq) == vq->last_avail_idx;
203 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
204 unsigned int len, unsigned int idx)
209 trace_virtqueue_fill(vq, elem, len, idx);
212 for (i = 0; i < elem->in_num; i++) {
213 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
215 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
216 elem->in_sg[i].iov_len,
219 offset += elem->in_sg[i].iov_len;
222 for (i = 0; i < elem->out_num; i++)
223 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
224 elem->out_sg[i].iov_len,
225 0, elem->out_sg[i].iov_len);
227 idx = (idx + vring_used_idx(vq)) % vq->vring.num;
229 /* Get a pointer to the next entry in the used ring. */
230 vring_used_ring_id(vq, idx, elem->index);
231 vring_used_ring_len(vq, idx, len);
234 void virtqueue_flush(VirtQueue *vq, unsigned int count)
236 /* Make sure buffer is written before we update index. */
238 trace_virtqueue_flush(vq, count);
239 vring_used_idx_increment(vq, count);
243 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
246 virtqueue_fill(vq, elem, len, 0);
247 virtqueue_flush(vq, 1);
250 static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
252 uint16_t num_heads = vring_avail_idx(vq) - idx;
254 /* Check it isn't doing very strange things with descriptor numbers. */
255 if (num_heads > vq->vring.num) {
256 error_report("Guest moved used index from %u to %u",
257 idx, vring_avail_idx(vq));
264 static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
268 /* Grab the next descriptor number they're advertising, and increment
269 * the index we've seen. */
270 head = vring_avail_ring(vq, idx % vq->vring.num);
272 /* If their number is silly, that's a fatal mistake. */
273 if (head >= vq->vring.num) {
274 error_report("Guest says index %u is available", head);
281 static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa,
282 unsigned int i, unsigned int max)
286 /* If this descriptor says it doesn't chain, we're done. */
287 if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT))
290 /* Check they're not leading us off end of descriptors. */
291 next = vring_desc_next(desc_pa, i);
292 /* Make sure compiler knows to grab that: we don't want it changing! */
296 error_report("Desc next is %u", next);
303 int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes)
306 int total_bufs, in_total, out_total;
308 idx = vq->last_avail_idx;
310 total_bufs = in_total = out_total = 0;
311 while (virtqueue_num_heads(vq, idx)) {
312 unsigned int max, num_bufs, indirect = 0;
313 target_phys_addr_t desc_pa;
317 num_bufs = total_bufs;
318 i = virtqueue_get_head(vq, idx++);
319 desc_pa = vq->vring.desc;
321 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
322 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
323 error_report("Invalid size for indirect buffer table");
327 /* If we've got too many, that implies a descriptor loop. */
328 if (num_bufs >= max) {
329 error_report("Looped descriptor");
333 /* loop over the indirect descriptor table */
335 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
337 desc_pa = vring_desc_addr(desc_pa, i);
341 /* If we've got too many, that implies a descriptor loop. */
342 if (++num_bufs > max) {
343 error_report("Looped descriptor");
347 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
349 (in_total += vring_desc_len(desc_pa, i)) >= in_bytes)
353 (out_total += vring_desc_len(desc_pa, i)) >= out_bytes)
356 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
359 total_bufs = num_bufs;
367 void virtqueue_map_sg(struct iovec *sg, target_phys_addr_t *addr,
368 size_t num_sg, int is_write)
371 target_phys_addr_t len;
373 for (i = 0; i < num_sg; i++) {
375 sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
376 if (sg[i].iov_base == NULL || len != sg[i].iov_len) {
377 error_report("virtio: trying to map MMIO memory");
383 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
385 unsigned int i, head, max;
386 target_phys_addr_t desc_pa = vq->vring.desc;
388 if (!virtqueue_num_heads(vq, vq->last_avail_idx))
391 /* When we start there are none of either input nor output. */
392 elem->out_num = elem->in_num = 0;
396 i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
398 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
399 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
400 error_report("Invalid size for indirect buffer table");
404 /* loop over the indirect descriptor table */
405 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
406 desc_pa = vring_desc_addr(desc_pa, i);
410 /* Collect all the descriptors */
414 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
415 elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i);
416 sg = &elem->in_sg[elem->in_num++];
418 elem->out_addr[elem->out_num] = vring_desc_addr(desc_pa, i);
419 sg = &elem->out_sg[elem->out_num++];
422 sg->iov_len = vring_desc_len(desc_pa, i);
424 /* If we've got too many, that implies a descriptor loop. */
425 if ((elem->in_num + elem->out_num) > max) {
426 error_report("Looped descriptor");
429 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
431 /* Now map what we have collected */
432 virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
433 virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);
439 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
440 return elem->in_num + elem->out_num;
444 static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
446 if (vdev->binding->notify) {
447 vdev->binding->notify(vdev->binding_opaque, vector);
451 void virtio_update_irq(VirtIODevice *vdev)
453 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
456 void virtio_reset(void *opaque)
458 VirtIODevice *vdev = opaque;
461 virtio_set_status(vdev, 0);
466 vdev->guest_features = 0;
470 vdev->config_vector = VIRTIO_NO_VECTOR;
471 virtio_notify_vector(vdev, vdev->config_vector);
473 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
474 vdev->vq[i].vring.desc = 0;
475 vdev->vq[i].vring.avail = 0;
476 vdev->vq[i].vring.used = 0;
477 vdev->vq[i].last_avail_idx = 0;
479 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
483 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
487 vdev->get_config(vdev, vdev->config);
489 if (addr > (vdev->config_len - sizeof(val)))
492 memcpy(&val, vdev->config + addr, sizeof(val));
496 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
500 vdev->get_config(vdev, vdev->config);
502 if (addr > (vdev->config_len - sizeof(val)))
505 memcpy(&val, vdev->config + addr, sizeof(val));
509 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
513 vdev->get_config(vdev, vdev->config);
515 if (addr > (vdev->config_len - sizeof(val)))
518 memcpy(&val, vdev->config + addr, sizeof(val));
522 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
526 if (addr > (vdev->config_len - sizeof(val)))
529 memcpy(vdev->config + addr, &val, sizeof(val));
531 if (vdev->set_config)
532 vdev->set_config(vdev, vdev->config);
535 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
539 if (addr > (vdev->config_len - sizeof(val)))
542 memcpy(vdev->config + addr, &val, sizeof(val));
544 if (vdev->set_config)
545 vdev->set_config(vdev, vdev->config);
548 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
552 if (addr > (vdev->config_len - sizeof(val)))
555 memcpy(vdev->config + addr, &val, sizeof(val));
557 if (vdev->set_config)
558 vdev->set_config(vdev, vdev->config);
561 void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr)
563 vdev->vq[n].pa = addr;
564 virtqueue_init(&vdev->vq[n]);
567 target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n)
569 return vdev->vq[n].pa;
572 int virtio_queue_get_num(VirtIODevice *vdev, int n)
574 return vdev->vq[n].vring.num;
577 void virtio_queue_notify_vq(VirtQueue *vq)
579 if (vq->vring.desc) {
580 VirtIODevice *vdev = vq->vdev;
581 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
582 vq->handle_output(vdev, vq);
586 void virtio_queue_notify(VirtIODevice *vdev, int n)
588 if (n < VIRTIO_PCI_QUEUE_MAX) {
589 virtio_queue_notify_vq(&vdev->vq[n]);
593 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
595 return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector :
599 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
601 if (n < VIRTIO_PCI_QUEUE_MAX)
602 vdev->vq[n].vector = vector;
605 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
606 void (*handle_output)(VirtIODevice *, VirtQueue *))
610 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
611 if (vdev->vq[i].vring.num == 0)
615 if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
618 vdev->vq[i].vring.num = queue_size;
619 vdev->vq[i].handle_output = handle_output;
624 void virtio_irq(VirtQueue *vq)
626 trace_virtio_irq(vq);
627 vq->vdev->isr |= 0x01;
628 virtio_notify_vector(vq->vdev, vq->vector);
631 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
633 /* Always notify when queue is empty (when feature acknowledge) */
634 if ((vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT) &&
635 (!(vdev->guest_features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) ||
636 (vq->inuse || vring_avail_idx(vq) != vq->last_avail_idx)))
639 trace_virtio_notify(vdev, vq);
641 virtio_notify_vector(vdev, vq->vector);
644 void virtio_notify_config(VirtIODevice *vdev)
646 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
650 virtio_notify_vector(vdev, vdev->config_vector);
653 void virtio_save(VirtIODevice *vdev, QEMUFile *f)
657 if (vdev->binding->save_config)
658 vdev->binding->save_config(vdev->binding_opaque, f);
660 qemu_put_8s(f, &vdev->status);
661 qemu_put_8s(f, &vdev->isr);
662 qemu_put_be16s(f, &vdev->queue_sel);
663 qemu_put_be32s(f, &vdev->guest_features);
664 qemu_put_be32(f, vdev->config_len);
665 qemu_put_buffer(f, vdev->config, vdev->config_len);
667 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
668 if (vdev->vq[i].vring.num == 0)
674 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
675 if (vdev->vq[i].vring.num == 0)
678 qemu_put_be32(f, vdev->vq[i].vring.num);
679 qemu_put_be64(f, vdev->vq[i].pa);
680 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
681 if (vdev->binding->save_queue)
682 vdev->binding->save_queue(vdev->binding_opaque, i, f);
686 int virtio_load(VirtIODevice *vdev, QEMUFile *f)
690 uint32_t supported_features =
691 vdev->binding->get_features(vdev->binding_opaque);
693 if (vdev->binding->load_config) {
694 ret = vdev->binding->load_config(vdev->binding_opaque, f);
699 qemu_get_8s(f, &vdev->status);
700 qemu_get_8s(f, &vdev->isr);
701 qemu_get_be16s(f, &vdev->queue_sel);
702 qemu_get_be32s(f, &features);
703 if (features & ~supported_features) {
704 error_report("Features 0x%x unsupported. Allowed features: 0x%x",
705 features, supported_features);
708 if (vdev->set_features)
709 vdev->set_features(vdev, features);
710 vdev->guest_features = features;
711 vdev->config_len = qemu_get_be32(f);
712 qemu_get_buffer(f, vdev->config, vdev->config_len);
714 num = qemu_get_be32(f);
716 for (i = 0; i < num; i++) {
717 vdev->vq[i].vring.num = qemu_get_be32(f);
718 vdev->vq[i].pa = qemu_get_be64(f);
719 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
721 if (vdev->vq[i].pa) {
723 virtqueue_init(&vdev->vq[i]);
724 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
725 /* Check it isn't doing very strange things with descriptor numbers. */
726 if (nheads > vdev->vq[i].vring.num) {
727 error_report("VQ %d size 0x%x Guest index 0x%x "
728 "inconsistent with Host index 0x%x: delta 0x%x\n",
729 i, vdev->vq[i].vring.num,
730 vring_avail_idx(&vdev->vq[i]),
731 vdev->vq[i].last_avail_idx, nheads);
734 } else if (vdev->vq[i].last_avail_idx) {
735 error_report("VQ %d address 0x0 "
736 "inconsistent with Host index 0x%x\n",
737 i, vdev->vq[i].last_avail_idx);
740 if (vdev->binding->load_queue) {
741 ret = vdev->binding->load_queue(vdev->binding_opaque, i, f);
747 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
751 void virtio_cleanup(VirtIODevice *vdev)
753 qemu_del_vm_change_state_handler(vdev->vmstate);
755 qemu_free(vdev->config);
759 static void virtio_vmstate_change(void *opaque, int running, int reason)
761 VirtIODevice *vdev = opaque;
762 bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
763 vdev->vm_running = running;
766 virtio_set_status(vdev, vdev->status);
769 if (vdev->binding->vmstate_change) {
770 vdev->binding->vmstate_change(vdev->binding_opaque, backend_run);
774 virtio_set_status(vdev, vdev->status);
778 VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
779 size_t config_size, size_t struct_size)
784 vdev = qemu_mallocz(struct_size);
786 vdev->device_id = device_id;
790 vdev->config_vector = VIRTIO_NO_VECTOR;
791 vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
792 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
793 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
794 vdev->vq[i].vdev = vdev;
798 vdev->config_len = config_size;
799 if (vdev->config_len)
800 vdev->config = qemu_mallocz(config_size);
804 vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change, vdev);
809 void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
812 vdev->binding = binding;
813 vdev->binding_opaque = opaque;
816 target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
818 return vdev->vq[n].vring.desc;
821 target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
823 return vdev->vq[n].vring.avail;
826 target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
828 return vdev->vq[n].vring.used;
831 target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n)
833 return vdev->vq[n].vring.desc;
836 target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
838 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
841 target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
843 return offsetof(VRingAvail, ring) +
844 sizeof(uint64_t) * vdev->vq[n].vring.num;
847 target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n)
849 return offsetof(VRingUsed, ring) +
850 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
853 target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n)
855 return vdev->vq[n].vring.used - vdev->vq[n].vring.desc +
856 virtio_queue_get_used_size(vdev, n);
859 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
861 return vdev->vq[n].last_avail_idx;
864 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
866 vdev->vq[n].last_avail_idx = idx;
869 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
874 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
876 return &vq->guest_notifier;
878 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
880 return &vq->host_notifier;