Merge commit 'ca77d175912f7d0b2296e8e3a803a0763c00bc0b' into upstream-merge
[qemu-kvm/markmc.git] / hw / virtio.c
blob337ff27ba99ed26b2027d0b3f8e591ed72626974
1 /*
2 * Virtio Support
4 * Copyright IBM, Corp. 2007
6 * Authors:
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 <inttypes.h>
16 #include "virtio.h"
17 #include "sysemu.h"
19 /* The alignment to use between consumer and producer parts of vring.
20 * x86 pagesize again. */
21 #define VIRTIO_PCI_VRING_ALIGN 4096
23 /* QEMU doesn't strictly need write barriers since everything runs in
24 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
25 * KVM or if kqemu gets SMP support.
27 #define wmb() do { } while (0)
29 typedef struct VRingDesc
31 uint64_t addr;
32 uint32_t len;
33 uint16_t flags;
34 uint16_t next;
35 } VRingDesc;
37 typedef struct VRingAvail
39 uint16_t flags;
40 uint16_t idx;
41 uint16_t ring[0];
42 } VRingAvail;
44 typedef struct VRingUsedElem
46 uint32_t id;
47 uint32_t len;
48 } VRingUsedElem;
50 typedef struct VRingUsed
52 uint16_t flags;
53 uint16_t idx;
54 VRingUsedElem ring[0];
55 } VRingUsed;
57 typedef struct VRing
59 unsigned int num;
60 target_phys_addr_t desc;
61 target_phys_addr_t avail;
62 target_phys_addr_t used;
63 } VRing;
65 struct VirtQueue
67 VRing vring;
68 target_phys_addr_t pa;
69 uint16_t last_avail_idx;
70 int inuse;
71 uint16_t vector;
72 void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
75 #define VIRTIO_PCI_QUEUE_MAX 16
77 /* virt queue functions */
78 static void virtqueue_init(VirtQueue *vq)
80 target_phys_addr_t pa = vq->pa;
82 vq->vring.desc = pa;
83 vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
84 vq->vring.used = vring_align(vq->vring.avail +
85 offsetof(VRingAvail, ring[vq->vring.num]),
86 VIRTIO_PCI_VRING_ALIGN);
89 static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa, int i)
91 target_phys_addr_t pa;
92 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
93 return ldq_phys(pa);
96 static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa, int i)
98 target_phys_addr_t pa;
99 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
100 return ldl_phys(pa);
103 static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa, int i)
105 target_phys_addr_t pa;
106 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
107 return lduw_phys(pa);
110 static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa, int i)
112 target_phys_addr_t pa;
113 pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
114 return lduw_phys(pa);
117 static inline uint16_t vring_avail_flags(VirtQueue *vq)
119 target_phys_addr_t pa;
120 pa = vq->vring.avail + offsetof(VRingAvail, flags);
121 return lduw_phys(pa);
124 static inline uint16_t vring_avail_idx(VirtQueue *vq)
126 target_phys_addr_t pa;
127 pa = vq->vring.avail + offsetof(VRingAvail, idx);
128 return lduw_phys(pa);
131 static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
133 target_phys_addr_t pa;
134 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
135 return lduw_phys(pa);
138 static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val)
140 target_phys_addr_t pa;
141 pa = vq->vring.used + offsetof(VRingUsed, ring[i].id);
142 stl_phys(pa, val);
145 static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val)
147 target_phys_addr_t pa;
148 pa = vq->vring.used + offsetof(VRingUsed, ring[i].len);
149 stl_phys(pa, val);
152 static uint16_t vring_used_idx(VirtQueue *vq)
154 target_phys_addr_t pa;
155 pa = vq->vring.used + offsetof(VRingUsed, idx);
156 return lduw_phys(pa);
159 static inline void vring_used_idx_increment(VirtQueue *vq, uint16_t val)
161 target_phys_addr_t pa;
162 pa = vq->vring.used + offsetof(VRingUsed, idx);
163 stw_phys(pa, vring_used_idx(vq) + val);
166 static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
168 target_phys_addr_t pa;
169 pa = vq->vring.used + offsetof(VRingUsed, flags);
170 stw_phys(pa, lduw_phys(pa) | mask);
173 static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
175 target_phys_addr_t pa;
176 pa = vq->vring.used + offsetof(VRingUsed, flags);
177 stw_phys(pa, lduw_phys(pa) & ~mask);
180 void virtio_queue_set_notification(VirtQueue *vq, int enable)
182 if (enable)
183 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
184 else
185 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
188 int virtio_queue_ready(VirtQueue *vq)
190 return vq->vring.avail != 0;
193 int virtio_queue_empty(VirtQueue *vq)
195 return vring_avail_idx(vq) == vq->last_avail_idx;
198 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
199 unsigned int len, unsigned int idx)
201 unsigned int offset;
202 int i;
204 offset = 0;
205 for (i = 0; i < elem->in_num; i++) {
206 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
208 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
209 elem->in_sg[i].iov_len,
210 1, size);
212 offset += elem->in_sg[i].iov_len;
215 for (i = 0; i < elem->out_num; i++)
216 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
217 elem->out_sg[i].iov_len,
218 0, elem->out_sg[i].iov_len);
220 idx = (idx + vring_used_idx(vq)) % vq->vring.num;
222 /* Get a pointer to the next entry in the used ring. */
223 vring_used_ring_id(vq, idx, elem->index);
224 vring_used_ring_len(vq, idx, len);
227 void virtqueue_flush(VirtQueue *vq, unsigned int count)
229 /* Make sure buffer is written before we update index. */
230 wmb();
231 vring_used_idx_increment(vq, count);
232 vq->inuse -= count;
235 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
236 unsigned int len)
238 virtqueue_fill(vq, elem, len, 0);
239 virtqueue_flush(vq, 1);
242 static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
244 uint16_t num_heads = vring_avail_idx(vq) - idx;
246 /* Check it isn't doing very strange things with descriptor numbers. */
247 if (num_heads > vq->vring.num) {
248 fprintf(stderr, "Guest moved used index from %u to %u",
249 idx, vring_avail_idx(vq));
250 exit(1);
253 return num_heads;
256 static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
258 unsigned int head;
260 /* Grab the next descriptor number they're advertising, and increment
261 * the index we've seen. */
262 head = vring_avail_ring(vq, idx % vq->vring.num);
264 /* If their number is silly, that's a fatal mistake. */
265 if (head >= vq->vring.num) {
266 fprintf(stderr, "Guest says index %u is available", head);
267 exit(1);
270 return head;
273 static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa,
274 unsigned int i, unsigned int max)
276 unsigned int next;
278 /* If this descriptor says it doesn't chain, we're done. */
279 if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT))
280 return max;
282 /* Check they're not leading us off end of descriptors. */
283 next = vring_desc_next(desc_pa, i);
284 /* Make sure compiler knows to grab that: we don't want it changing! */
285 wmb();
287 if (next >= max) {
288 fprintf(stderr, "Desc next is %u", next);
289 exit(1);
292 return next;
295 int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes)
297 unsigned int idx;
298 int total_bufs, in_total, out_total;
300 idx = vq->last_avail_idx;
302 total_bufs = in_total = out_total = 0;
303 while (virtqueue_num_heads(vq, idx)) {
304 unsigned int max, num_bufs, indirect = 0;
305 target_phys_addr_t desc_pa;
306 int i;
308 max = vq->vring.num;
309 num_bufs = total_bufs;
310 i = virtqueue_get_head(vq, idx++);
311 desc_pa = vq->vring.desc;
313 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
314 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
315 fprintf(stderr, "Invalid size for indirect buffer table\n");
316 exit(1);
319 /* If we've got too many, that implies a descriptor loop. */
320 if (num_bufs >= max) {
321 fprintf(stderr, "Looped descriptor");
322 exit(1);
325 /* loop over the indirect descriptor table */
326 indirect = 1;
327 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
328 num_bufs = i = 0;
329 desc_pa = vring_desc_addr(desc_pa, i);
332 do {
333 /* If we've got too many, that implies a descriptor loop. */
334 if (++num_bufs > max) {
335 fprintf(stderr, "Looped descriptor");
336 exit(1);
339 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
340 if (in_bytes > 0 &&
341 (in_total += vring_desc_len(desc_pa, i)) >= in_bytes)
342 return 1;
343 } else {
344 if (out_bytes > 0 &&
345 (out_total += vring_desc_len(desc_pa, i)) >= out_bytes)
346 return 1;
348 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
350 if (!indirect)
351 total_bufs = num_bufs;
352 else
353 total_bufs++;
356 return 0;
359 int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
361 unsigned int i, head, max;
362 target_phys_addr_t desc_pa = vq->vring.desc;
363 target_phys_addr_t len;
365 if (!virtqueue_num_heads(vq, vq->last_avail_idx))
366 return 0;
368 /* When we start there are none of either input nor output. */
369 elem->out_num = elem->in_num = 0;
371 max = vq->vring.num;
373 i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
375 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
376 if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
377 fprintf(stderr, "Invalid size for indirect buffer table\n");
378 exit(1);
381 /* loop over the indirect descriptor table */
382 max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
383 desc_pa = vring_desc_addr(desc_pa, i);
384 i = 0;
387 do {
388 struct iovec *sg;
389 int is_write = 0;
391 if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
392 elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i);
393 sg = &elem->in_sg[elem->in_num++];
394 is_write = 1;
395 } else
396 sg = &elem->out_sg[elem->out_num++];
398 /* Grab the first descriptor, and check it's OK. */
399 sg->iov_len = vring_desc_len(desc_pa, i);
400 len = sg->iov_len;
402 sg->iov_base = cpu_physical_memory_map(vring_desc_addr(desc_pa, i),
403 &len, is_write);
405 if (sg->iov_base == NULL || len != sg->iov_len) {
406 fprintf(stderr, "virtio: trying to map MMIO memory\n");
407 exit(1);
410 /* If we've got too many, that implies a descriptor loop. */
411 if ((elem->in_num + elem->out_num) > max) {
412 fprintf(stderr, "Looped descriptor");
413 exit(1);
415 } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
417 elem->index = head;
419 vq->inuse++;
421 return elem->in_num + elem->out_num;
424 /* virtio device */
425 static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
427 if (vdev->binding->notify) {
428 vdev->binding->notify(vdev->binding_opaque, vector);
432 void virtio_update_irq(VirtIODevice *vdev)
434 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
437 void virtio_reset(void *opaque)
439 VirtIODevice *vdev = opaque;
440 int i;
442 if (vdev->reset)
443 vdev->reset(vdev);
445 vdev->features = 0;
446 vdev->queue_sel = 0;
447 vdev->status = 0;
448 vdev->isr = 0;
449 vdev->config_vector = VIRTIO_NO_VECTOR;
450 virtio_notify_vector(vdev, vdev->config_vector);
452 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
453 vdev->vq[i].vring.desc = 0;
454 vdev->vq[i].vring.avail = 0;
455 vdev->vq[i].vring.used = 0;
456 vdev->vq[i].last_avail_idx = 0;
457 vdev->vq[i].pa = 0;
458 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
462 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
464 uint8_t val;
466 vdev->get_config(vdev, vdev->config);
468 if (addr > (vdev->config_len - sizeof(val)))
469 return (uint32_t)-1;
471 memcpy(&val, vdev->config + addr, sizeof(val));
472 return val;
475 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
477 uint16_t val;
479 vdev->get_config(vdev, vdev->config);
481 if (addr > (vdev->config_len - sizeof(val)))
482 return (uint32_t)-1;
484 memcpy(&val, vdev->config + addr, sizeof(val));
485 return val;
488 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
490 uint32_t val;
492 vdev->get_config(vdev, vdev->config);
494 if (addr > (vdev->config_len - sizeof(val)))
495 return (uint32_t)-1;
497 memcpy(&val, vdev->config + addr, sizeof(val));
498 return val;
501 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
503 uint8_t val = data;
505 if (addr > (vdev->config_len - sizeof(val)))
506 return;
508 memcpy(vdev->config + addr, &val, sizeof(val));
510 if (vdev->set_config)
511 vdev->set_config(vdev, vdev->config);
514 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
516 uint16_t val = data;
518 if (addr > (vdev->config_len - sizeof(val)))
519 return;
521 memcpy(vdev->config + addr, &val, sizeof(val));
523 if (vdev->set_config)
524 vdev->set_config(vdev, vdev->config);
527 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
529 uint32_t val = data;
531 if (addr > (vdev->config_len - sizeof(val)))
532 return;
534 memcpy(vdev->config + addr, &val, sizeof(val));
536 if (vdev->set_config)
537 vdev->set_config(vdev, vdev->config);
540 void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr)
542 vdev->vq[n].pa = addr;
543 virtqueue_init(&vdev->vq[n]);
546 target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n)
548 return vdev->vq[n].pa;
551 int virtio_queue_get_num(VirtIODevice *vdev, int n)
553 return vdev->vq[n].vring.num;
556 void virtio_queue_notify(VirtIODevice *vdev, int n)
558 if (n < VIRTIO_PCI_QUEUE_MAX && vdev->vq[n].vring.desc) {
559 vdev->vq[n].handle_output(vdev, &vdev->vq[n]);
563 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
565 return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector :
566 VIRTIO_NO_VECTOR;
569 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
571 if (n < VIRTIO_PCI_QUEUE_MAX)
572 vdev->vq[n].vector = vector;
575 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
576 void (*handle_output)(VirtIODevice *, VirtQueue *))
578 int i;
580 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
581 if (vdev->vq[i].vring.num == 0)
582 break;
585 if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
586 abort();
588 vdev->vq[i].vring.num = queue_size;
589 vdev->vq[i].handle_output = handle_output;
591 return &vdev->vq[i];
594 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
596 /* Always notify when queue is empty (when feature acknowledge) */
597 if ((vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT) &&
598 (!(vdev->features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) ||
599 (vq->inuse || vring_avail_idx(vq) != vq->last_avail_idx)))
600 return;
602 vdev->isr |= 0x01;
603 virtio_notify_vector(vdev, vq->vector);
606 void virtio_notify_config(VirtIODevice *vdev)
608 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
609 return;
611 vdev->isr |= 0x03;
612 virtio_notify_vector(vdev, vdev->config_vector);
615 void virtio_save(VirtIODevice *vdev, QEMUFile *f)
617 int i;
619 if (vdev->binding->save_config)
620 vdev->binding->save_config(vdev->binding_opaque, f);
622 qemu_put_8s(f, &vdev->status);
623 qemu_put_8s(f, &vdev->isr);
624 qemu_put_be16s(f, &vdev->queue_sel);
625 qemu_put_be32s(f, &vdev->features);
626 qemu_put_be32(f, vdev->config_len);
627 qemu_put_buffer(f, vdev->config, vdev->config_len);
629 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
630 if (vdev->vq[i].vring.num == 0)
631 break;
634 qemu_put_be32(f, i);
636 for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
637 if (vdev->vq[i].vring.num == 0)
638 break;
640 qemu_put_be32(f, vdev->vq[i].vring.num);
641 qemu_put_be64(f, vdev->vq[i].pa);
642 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
643 if (vdev->binding->save_queue)
644 vdev->binding->save_queue(vdev->binding_opaque, i, f);
648 int virtio_load(VirtIODevice *vdev, QEMUFile *f)
650 int num, i, ret;
652 if (vdev->binding->load_config) {
653 ret = vdev->binding->load_config(vdev->binding_opaque, f);
654 if (ret)
655 return ret;
658 qemu_get_8s(f, &vdev->status);
659 qemu_get_8s(f, &vdev->isr);
660 qemu_get_be16s(f, &vdev->queue_sel);
661 qemu_get_be32s(f, &vdev->features);
662 vdev->config_len = qemu_get_be32(f);
663 qemu_get_buffer(f, vdev->config, vdev->config_len);
665 num = qemu_get_be32(f);
667 for (i = 0; i < num; i++) {
668 vdev->vq[i].vring.num = qemu_get_be32(f);
669 vdev->vq[i].pa = qemu_get_be64(f);
670 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
672 if (vdev->vq[i].pa) {
673 virtqueue_init(&vdev->vq[i]);
675 if (vdev->binding->load_queue) {
676 ret = vdev->binding->load_queue(vdev->binding_opaque, i, f);
677 if (ret)
678 return ret;
682 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
683 return 0;
686 void virtio_cleanup(VirtIODevice *vdev)
688 if (vdev->config)
689 qemu_free(vdev->config);
690 qemu_free(vdev->vq);
693 VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
694 size_t config_size, size_t struct_size)
696 VirtIODevice *vdev;
697 int i;
699 vdev = qemu_mallocz(struct_size);
701 vdev->device_id = device_id;
702 vdev->status = 0;
703 vdev->isr = 0;
704 vdev->queue_sel = 0;
705 vdev->config_vector = VIRTIO_NO_VECTOR;
706 vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
707 for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++)
708 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
710 vdev->name = name;
711 vdev->config_len = config_size;
712 if (vdev->config_len)
713 vdev->config = qemu_mallocz(config_size);
714 else
715 vdev->config = NULL;
717 return vdev;
720 void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
721 void *opaque)
723 vdev->binding = binding;
724 vdev->binding_opaque = opaque;