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.
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.
26 * In any case, we must prevent the compiler from reordering the code.
27 * TODO: we likely need some rmb()/mb() as well.
30 #define wmb() __asm__ __volatile__("": : :"memory")
32 typedef struct VRingDesc
40 typedef struct VRingAvail
47 typedef struct VRingUsedElem
53 typedef struct VRingUsed
57 VRingUsedElem ring
[0];
63 target_phys_addr_t desc
;
64 target_phys_addr_t avail
;
65 target_phys_addr_t used
;
71 target_phys_addr_t pa
;
72 uint16_t last_avail_idx
;
75 void (*handle_output
)(VirtIODevice
*vdev
, VirtQueue
*vq
);
77 EventNotifier guest_notifier
;
78 EventNotifier host_notifier
;
81 /* virt queue functions */
82 static void virtqueue_init(VirtQueue
*vq
)
84 target_phys_addr_t pa
= vq
->pa
;
87 vq
->vring
.avail
= pa
+ vq
->vring
.num
* sizeof(VRingDesc
);
88 vq
->vring
.used
= vring_align(vq
->vring
.avail
+
89 offsetof(VRingAvail
, ring
[vq
->vring
.num
]),
90 VIRTIO_PCI_VRING_ALIGN
);
93 static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa
, int i
)
95 target_phys_addr_t pa
;
96 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, addr
);
100 static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa
, int i
)
102 target_phys_addr_t pa
;
103 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, len
);
107 static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa
, int i
)
109 target_phys_addr_t pa
;
110 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, flags
);
111 return lduw_phys(pa
);
114 static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa
, int i
)
116 target_phys_addr_t pa
;
117 pa
= desc_pa
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, next
);
118 return lduw_phys(pa
);
121 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
123 target_phys_addr_t pa
;
124 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, flags
);
125 return lduw_phys(pa
);
128 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
130 target_phys_addr_t pa
;
131 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, idx
);
132 return lduw_phys(pa
);
135 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
137 target_phys_addr_t pa
;
138 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, ring
[i
]);
139 return lduw_phys(pa
);
142 static inline void vring_used_ring_id(VirtQueue
*vq
, int i
, uint32_t val
)
144 target_phys_addr_t pa
;
145 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].id
);
149 static inline void vring_used_ring_len(VirtQueue
*vq
, int i
, uint32_t val
)
151 target_phys_addr_t pa
;
152 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].len
);
156 static uint16_t vring_used_idx(VirtQueue
*vq
)
158 target_phys_addr_t pa
;
159 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
160 return lduw_phys(pa
);
163 static inline void vring_used_idx_increment(VirtQueue
*vq
, uint16_t val
)
165 target_phys_addr_t pa
;
166 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
167 stw_phys(pa
, vring_used_idx(vq
) + val
);
170 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
172 target_phys_addr_t pa
;
173 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
174 stw_phys(pa
, lduw_phys(pa
) | mask
);
177 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
179 target_phys_addr_t pa
;
180 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
181 stw_phys(pa
, lduw_phys(pa
) & ~mask
);
184 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
187 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
189 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
192 int virtio_queue_ready(VirtQueue
*vq
)
194 return vq
->vring
.avail
!= 0;
197 int virtio_queue_empty(VirtQueue
*vq
)
199 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
202 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
203 unsigned int len
, unsigned int idx
)
209 for (i
= 0; i
< elem
->in_num
; i
++) {
210 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
212 cpu_physical_memory_unmap(elem
->in_sg
[i
].iov_base
,
213 elem
->in_sg
[i
].iov_len
,
216 offset
+= elem
->in_sg
[i
].iov_len
;
219 for (i
= 0; i
< elem
->out_num
; i
++)
220 cpu_physical_memory_unmap(elem
->out_sg
[i
].iov_base
,
221 elem
->out_sg
[i
].iov_len
,
222 0, elem
->out_sg
[i
].iov_len
);
224 idx
= (idx
+ vring_used_idx(vq
)) % vq
->vring
.num
;
226 /* Get a pointer to the next entry in the used ring. */
227 vring_used_ring_id(vq
, idx
, elem
->index
);
228 vring_used_ring_len(vq
, idx
, len
);
231 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
233 /* Make sure buffer is written before we update index. */
235 vring_used_idx_increment(vq
, count
);
239 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
242 virtqueue_fill(vq
, elem
, len
, 0);
243 virtqueue_flush(vq
, 1);
246 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
248 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
250 /* Check it isn't doing very strange things with descriptor numbers. */
251 if (num_heads
> vq
->vring
.num
) {
252 fprintf(stderr
, "Guest moved used index from %u to %u",
253 idx
, vring_avail_idx(vq
));
260 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
264 /* Grab the next descriptor number they're advertising, and increment
265 * the index we've seen. */
266 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
268 /* If their number is silly, that's a fatal mistake. */
269 if (head
>= vq
->vring
.num
) {
270 fprintf(stderr
, "Guest says index %u is available", head
);
277 static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa
,
278 unsigned int i
, unsigned int max
)
282 /* If this descriptor says it doesn't chain, we're done. */
283 if (!(vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_NEXT
))
286 /* Check they're not leading us off end of descriptors. */
287 next
= vring_desc_next(desc_pa
, i
);
288 /* Make sure compiler knows to grab that: we don't want it changing! */
292 fprintf(stderr
, "Desc next is %u", next
);
299 int virtqueue_avail_bytes(VirtQueue
*vq
, int in_bytes
, int out_bytes
)
302 int total_bufs
, in_total
, out_total
;
304 idx
= vq
->last_avail_idx
;
306 total_bufs
= in_total
= out_total
= 0;
307 while (virtqueue_num_heads(vq
, idx
)) {
308 unsigned int max
, num_bufs
, indirect
= 0;
309 target_phys_addr_t desc_pa
;
313 num_bufs
= total_bufs
;
314 i
= virtqueue_get_head(vq
, idx
++);
315 desc_pa
= vq
->vring
.desc
;
317 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
318 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
319 fprintf(stderr
, "Invalid size for indirect buffer table\n");
323 /* If we've got too many, that implies a descriptor loop. */
324 if (num_bufs
>= max
) {
325 fprintf(stderr
, "Looped descriptor");
329 /* loop over the indirect descriptor table */
331 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
333 desc_pa
= vring_desc_addr(desc_pa
, i
);
337 /* If we've got too many, that implies a descriptor loop. */
338 if (++num_bufs
> max
) {
339 fprintf(stderr
, "Looped descriptor");
343 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
345 (in_total
+= vring_desc_len(desc_pa
, i
)) >= in_bytes
)
349 (out_total
+= vring_desc_len(desc_pa
, i
)) >= out_bytes
)
352 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
355 total_bufs
= num_bufs
;
363 int virtqueue_pop(VirtQueue
*vq
, VirtQueueElement
*elem
)
365 unsigned int i
, head
, max
;
366 target_phys_addr_t desc_pa
= vq
->vring
.desc
;
367 target_phys_addr_t len
;
369 if (!virtqueue_num_heads(vq
, vq
->last_avail_idx
))
372 /* When we start there are none of either input nor output. */
373 elem
->out_num
= elem
->in_num
= 0;
377 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
379 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_INDIRECT
) {
380 if (vring_desc_len(desc_pa
, i
) % sizeof(VRingDesc
)) {
381 fprintf(stderr
, "Invalid size for indirect buffer table\n");
385 /* loop over the indirect descriptor table */
386 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
387 desc_pa
= vring_desc_addr(desc_pa
, i
);
395 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
396 elem
->in_addr
[elem
->in_num
] = vring_desc_addr(desc_pa
, i
);
397 sg
= &elem
->in_sg
[elem
->in_num
++];
400 sg
= &elem
->out_sg
[elem
->out_num
++];
402 /* Grab the first descriptor, and check it's OK. */
403 sg
->iov_len
= vring_desc_len(desc_pa
, i
);
406 sg
->iov_base
= cpu_physical_memory_map(vring_desc_addr(desc_pa
, i
),
409 if (sg
->iov_base
== NULL
|| len
!= sg
->iov_len
) {
410 fprintf(stderr
, "virtio: trying to map MMIO memory\n");
414 /* If we've got too many, that implies a descriptor loop. */
415 if ((elem
->in_num
+ elem
->out_num
) > max
) {
416 fprintf(stderr
, "Looped descriptor");
419 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
425 return elem
->in_num
+ elem
->out_num
;
429 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
431 if (vdev
->binding
->notify
) {
432 vdev
->binding
->notify(vdev
->binding_opaque
, vector
);
436 void virtio_update_irq(VirtIODevice
*vdev
)
438 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
441 void virtio_reset(void *opaque
)
443 VirtIODevice
*vdev
= opaque
;
449 vdev
->guest_features
= 0;
453 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
454 virtio_notify_vector(vdev
, vdev
->config_vector
);
456 for(i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
457 vdev
->vq
[i
].vring
.desc
= 0;
458 vdev
->vq
[i
].vring
.avail
= 0;
459 vdev
->vq
[i
].vring
.used
= 0;
460 vdev
->vq
[i
].last_avail_idx
= 0;
462 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
466 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
470 vdev
->get_config(vdev
, vdev
->config
);
472 if (addr
> (vdev
->config_len
- sizeof(val
)))
475 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
479 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
483 vdev
->get_config(vdev
, vdev
->config
);
485 if (addr
> (vdev
->config_len
- sizeof(val
)))
488 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
492 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
496 vdev
->get_config(vdev
, vdev
->config
);
498 if (addr
> (vdev
->config_len
- sizeof(val
)))
501 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
505 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
509 if (addr
> (vdev
->config_len
- sizeof(val
)))
512 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
514 if (vdev
->set_config
)
515 vdev
->set_config(vdev
, vdev
->config
);
518 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
522 if (addr
> (vdev
->config_len
- sizeof(val
)))
525 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
527 if (vdev
->set_config
)
528 vdev
->set_config(vdev
, vdev
->config
);
531 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
535 if (addr
> (vdev
->config_len
- sizeof(val
)))
538 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
540 if (vdev
->set_config
)
541 vdev
->set_config(vdev
, vdev
->config
);
544 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, target_phys_addr_t addr
)
546 vdev
->vq
[n
].pa
= addr
;
547 virtqueue_init(&vdev
->vq
[n
]);
550 target_phys_addr_t
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
552 return vdev
->vq
[n
].pa
;
555 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
557 return vdev
->vq
[n
].vring
.num
;
560 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
562 if (n
< VIRTIO_PCI_QUEUE_MAX
&& vdev
->vq
[n
].vring
.desc
) {
563 vdev
->vq
[n
].handle_output(vdev
, &vdev
->vq
[n
]);
567 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
569 return n
< VIRTIO_PCI_QUEUE_MAX
? vdev
->vq
[n
].vector
:
573 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
575 if (n
< VIRTIO_PCI_QUEUE_MAX
)
576 vdev
->vq
[n
].vector
= vector
;
579 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
580 void (*handle_output
)(VirtIODevice
*, VirtQueue
*))
584 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
585 if (vdev
->vq
[i
].vring
.num
== 0)
589 if (i
== VIRTIO_PCI_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
592 vdev
->vq
[i
].vring
.num
= queue_size
;
593 vdev
->vq
[i
].handle_output
= handle_output
;
598 void virtio_irq(VirtQueue
*vq
)
600 vq
->vdev
->isr
|= 0x01;
601 virtio_notify_vector(vq
->vdev
, vq
->vector
);
604 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
606 /* Always notify when queue is empty (when feature acknowledge) */
607 if ((vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
) &&
608 (!(vdev
->guest_features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) ||
609 (vq
->inuse
|| vring_avail_idx(vq
) != vq
->last_avail_idx
)))
613 virtio_notify_vector(vdev
, vq
->vector
);
616 void virtio_notify_config(VirtIODevice
*vdev
)
618 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
622 virtio_notify_vector(vdev
, vdev
->config_vector
);
625 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
629 if (vdev
->binding
->save_config
)
630 vdev
->binding
->save_config(vdev
->binding_opaque
, f
);
632 qemu_put_8s(f
, &vdev
->status
);
633 qemu_put_8s(f
, &vdev
->isr
);
634 qemu_put_be16s(f
, &vdev
->queue_sel
);
635 qemu_put_be32s(f
, &vdev
->guest_features
);
636 qemu_put_be32(f
, vdev
->config_len
);
637 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
639 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
640 if (vdev
->vq
[i
].vring
.num
== 0)
646 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
647 if (vdev
->vq
[i
].vring
.num
== 0)
650 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
651 qemu_put_be64(f
, vdev
->vq
[i
].pa
);
652 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
653 if (vdev
->binding
->save_queue
)
654 vdev
->binding
->save_queue(vdev
->binding_opaque
, i
, f
);
658 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
)
662 uint32_t supported_features
=
663 vdev
->binding
->get_features(vdev
->binding_opaque
);
665 if (vdev
->binding
->load_config
) {
666 ret
= vdev
->binding
->load_config(vdev
->binding_opaque
, f
);
671 qemu_get_8s(f
, &vdev
->status
);
672 qemu_get_8s(f
, &vdev
->isr
);
673 qemu_get_be16s(f
, &vdev
->queue_sel
);
674 qemu_get_be32s(f
, &features
);
675 if (features
& ~supported_features
) {
676 fprintf(stderr
, "Features 0x%x unsupported. Allowed features: 0x%x\n",
677 features
, supported_features
);
680 vdev
->guest_features
= features
;
681 vdev
->config_len
= qemu_get_be32(f
);
682 qemu_get_buffer(f
, vdev
->config
, vdev
->config_len
);
684 num
= qemu_get_be32(f
);
686 for (i
= 0; i
< num
; i
++) {
687 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
688 vdev
->vq
[i
].pa
= qemu_get_be64(f
);
689 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
691 if (vdev
->vq
[i
].pa
) {
692 virtqueue_init(&vdev
->vq
[i
]);
694 if (vdev
->binding
->load_queue
) {
695 ret
= vdev
->binding
->load_queue(vdev
->binding_opaque
, i
, f
);
701 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
705 void virtio_cleanup(VirtIODevice
*vdev
)
708 qemu_free(vdev
->config
);
712 VirtIODevice
*virtio_common_init(const char *name
, uint16_t device_id
,
713 size_t config_size
, size_t struct_size
)
718 vdev
= qemu_mallocz(struct_size
);
720 vdev
->device_id
= device_id
;
724 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
725 vdev
->vq
= qemu_mallocz(sizeof(VirtQueue
) * VIRTIO_PCI_QUEUE_MAX
);
726 for(i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
727 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
728 vdev
->vq
[i
].vdev
= vdev
;
732 vdev
->config_len
= config_size
;
733 if (vdev
->config_len
)
734 vdev
->config
= qemu_mallocz(config_size
);
741 void virtio_bind_device(VirtIODevice
*vdev
, const VirtIOBindings
*binding
,
744 vdev
->binding
= binding
;
745 vdev
->binding_opaque
= opaque
;
748 target_phys_addr_t
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
750 return vdev
->vq
[n
].vring
.desc
;
753 target_phys_addr_t
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
755 return vdev
->vq
[n
].vring
.avail
;
758 target_phys_addr_t
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
760 return vdev
->vq
[n
].vring
.used
;
763 target_phys_addr_t
virtio_queue_get_ring_addr(VirtIODevice
*vdev
, int n
)
765 return vdev
->vq
[n
].vring
.desc
;
768 target_phys_addr_t
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
770 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
773 target_phys_addr_t
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
775 return offsetof(VRingAvail
, ring
) +
776 sizeof(uint64_t) * vdev
->vq
[n
].vring
.num
;
779 target_phys_addr_t
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
781 return offsetof(VRingUsed
, ring
) +
782 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
;
785 target_phys_addr_t
virtio_queue_get_ring_size(VirtIODevice
*vdev
, int n
)
787 return vdev
->vq
[n
].vring
.used
- vdev
->vq
[n
].vring
.desc
+
788 virtio_queue_get_used_size(vdev
, n
);
791 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
793 return vdev
->vq
[n
].last_avail_idx
;
796 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
, uint16_t idx
)
798 vdev
->vq
[n
].last_avail_idx
= idx
;
801 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
806 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
808 return &vq
->guest_notifier
;
810 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
812 return &vq
->host_notifier
;