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.
27 #define wmb() do { } while (0)
29 typedef struct VRingDesc
37 typedef struct VRingAvail
44 typedef struct VRingUsedElem
50 typedef struct VRingUsed
54 VRingUsedElem ring
[0];
60 target_phys_addr_t desc
;
61 target_phys_addr_t avail
;
62 target_phys_addr_t used
;
68 target_phys_addr_t pa
;
69 uint16_t last_avail_idx
;
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
;
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
);
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
);
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
);
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
);
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
)
183 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
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
)
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
,
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. */
231 vring_used_idx_increment(vq
, count
);
235 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
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
));
256 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
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
);
273 static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa
,
274 unsigned int i
, unsigned int max
)
278 /* If this descriptor says it doesn't chain, we're done. */
279 if (!(vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_NEXT
))
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! */
288 fprintf(stderr
, "Desc next is %u", next
);
295 int virtqueue_avail_bytes(VirtQueue
*vq
, int in_bytes
, int out_bytes
)
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
;
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");
319 /* If we've got too many, that implies a descriptor loop. */
320 if (num_bufs
>= max
) {
321 fprintf(stderr
, "Looped descriptor");
325 /* loop over the indirect descriptor table */
327 max
= vring_desc_len(desc_pa
, i
) / sizeof(VRingDesc
);
329 desc_pa
= vring_desc_addr(desc_pa
, i
);
333 /* If we've got too many, that implies a descriptor loop. */
334 if (++num_bufs
> max
) {
335 fprintf(stderr
, "Looped descriptor");
339 if (vring_desc_flags(desc_pa
, i
) & VRING_DESC_F_WRITE
) {
341 (in_total
+= vring_desc_len(desc_pa
, i
)) >= in_bytes
)
345 (out_total
+= vring_desc_len(desc_pa
, i
)) >= out_bytes
)
348 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
351 total_bufs
= num_bufs
;
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
))
368 /* When we start there are none of either input nor output. */
369 elem
->out_num
= elem
->in_num
= 0;
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");
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
);
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
++];
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
);
402 sg
->iov_base
= cpu_physical_memory_map(vring_desc_addr(desc_pa
, i
),
405 if (sg
->iov_base
== NULL
|| len
!= sg
->iov_len
) {
406 fprintf(stderr
, "virtio: trying to map MMIO memory\n");
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");
415 } while ((i
= virtqueue_next_desc(desc_pa
, i
, max
)) != max
);
421 return elem
->in_num
+ elem
->out_num
;
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
;
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;
458 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
462 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
466 vdev
->get_config(vdev
, vdev
->config
);
468 if (addr
> (vdev
->config_len
- sizeof(val
)))
471 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
475 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
479 vdev
->get_config(vdev
, vdev
->config
);
481 if (addr
> (vdev
->config_len
- sizeof(val
)))
484 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
488 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
492 vdev
->get_config(vdev
, vdev
->config
);
494 if (addr
> (vdev
->config_len
- sizeof(val
)))
497 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
501 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
505 if (addr
> (vdev
->config_len
- sizeof(val
)))
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
)
518 if (addr
> (vdev
->config_len
- sizeof(val
)))
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
)
531 if (addr
> (vdev
->config_len
- sizeof(val
)))
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
:
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
*))
580 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
581 if (vdev
->vq
[i
].vring
.num
== 0)
585 if (i
== VIRTIO_PCI_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
588 vdev
->vq
[i
].vring
.num
= queue_size
;
589 vdev
->vq
[i
].handle_output
= handle_output
;
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
)))
603 virtio_notify_vector(vdev
, vq
->vector
);
606 void virtio_notify_config(VirtIODevice
*vdev
)
608 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
612 virtio_notify_vector(vdev
, vdev
->config_vector
);
615 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
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
);
630 qemu_put_be16s(f
, &vdev
->config_vector
);
632 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
633 if (vdev
->vq
[i
].vring
.num
== 0)
639 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
640 if (vdev
->vq
[i
].vring
.num
== 0)
643 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
644 qemu_put_be64(f
, vdev
->vq
[i
].pa
);
645 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
646 if (vdev
->binding
->save_queue
)
647 vdev
->binding
->save_queue(vdev
->binding_opaque
, i
, f
);
651 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
)
655 if (vdev
->binding
->load_config
) {
656 ret
= vdev
->binding
->load_config(vdev
->binding_opaque
, f
);
661 qemu_get_8s(f
, &vdev
->status
);
662 qemu_get_8s(f
, &vdev
->isr
);
663 qemu_get_be16s(f
, &vdev
->queue_sel
);
664 qemu_get_be32s(f
, &vdev
->features
);
665 vdev
->config_len
= qemu_get_be32(f
);
666 qemu_get_buffer(f
, vdev
->config
, vdev
->config_len
);
668 num
= qemu_get_be32(f
);
670 for (i
= 0; i
< num
; i
++) {
671 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
672 vdev
->vq
[i
].pa
= qemu_get_be64(f
);
673 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
675 if (vdev
->vq
[i
].pa
) {
676 virtqueue_init(&vdev
->vq
[i
]);
678 if (vdev
->binding
->load_queue
) {
679 ret
= vdev
->binding
->load_queue(vdev
->binding_opaque
, i
, f
);
685 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
689 void virtio_cleanup(VirtIODevice
*vdev
)
692 qemu_free(vdev
->config
);
696 VirtIODevice
*virtio_common_init(const char *name
, uint16_t device_id
,
697 size_t config_size
, size_t struct_size
)
701 vdev
= qemu_mallocz(struct_size
);
703 vdev
->device_id
= device_id
;
707 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
708 vdev
->vq
= qemu_mallocz(sizeof(VirtQueue
) * VIRTIO_PCI_QUEUE_MAX
);
711 vdev
->config_len
= config_size
;
712 if (vdev
->config_len
)
713 vdev
->config
= qemu_mallocz(config_size
);
720 void virtio_bind_device(VirtIODevice
*vdev
, const VirtIOBindings
*binding
,
723 vdev
->binding
= binding
;
724 vdev
->binding_opaque
= opaque
;