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
;
71 void (*handle_output
)(VirtIODevice
*vdev
, VirtQueue
*vq
);
74 #define VIRTIO_PCI_QUEUE_MAX 16
76 /* virt queue functions */
77 static void virtqueue_init(VirtQueue
*vq
)
79 target_phys_addr_t pa
= vq
->pa
;
82 vq
->vring
.avail
= pa
+ vq
->vring
.num
* sizeof(VRingDesc
);
83 vq
->vring
.used
= vring_align(vq
->vring
.avail
+
84 offsetof(VRingAvail
, ring
[vq
->vring
.num
]),
85 VIRTIO_PCI_VRING_ALIGN
);
88 static inline uint64_t vring_desc_addr(VirtQueue
*vq
, int i
)
90 target_phys_addr_t pa
;
91 pa
= vq
->vring
.desc
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, addr
);
95 static inline uint32_t vring_desc_len(VirtQueue
*vq
, int i
)
97 target_phys_addr_t pa
;
98 pa
= vq
->vring
.desc
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, len
);
102 static inline uint16_t vring_desc_flags(VirtQueue
*vq
, int i
)
104 target_phys_addr_t pa
;
105 pa
= vq
->vring
.desc
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, flags
);
106 return lduw_phys(pa
);
109 static inline uint16_t vring_desc_next(VirtQueue
*vq
, int i
)
111 target_phys_addr_t pa
;
112 pa
= vq
->vring
.desc
+ sizeof(VRingDesc
) * i
+ offsetof(VRingDesc
, next
);
113 return lduw_phys(pa
);
116 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
118 target_phys_addr_t pa
;
119 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, flags
);
120 return lduw_phys(pa
);
123 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
125 target_phys_addr_t pa
;
126 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, idx
);
127 return lduw_phys(pa
);
130 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
132 target_phys_addr_t pa
;
133 pa
= vq
->vring
.avail
+ offsetof(VRingAvail
, ring
[i
]);
134 return lduw_phys(pa
);
137 static inline void vring_used_ring_id(VirtQueue
*vq
, int i
, uint32_t val
)
139 target_phys_addr_t pa
;
140 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].id
);
144 static inline void vring_used_ring_len(VirtQueue
*vq
, int i
, uint32_t val
)
146 target_phys_addr_t pa
;
147 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, ring
[i
].len
);
151 static uint16_t vring_used_idx(VirtQueue
*vq
)
153 target_phys_addr_t pa
;
154 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
155 return lduw_phys(pa
);
158 static inline void vring_used_idx_increment(VirtQueue
*vq
, uint16_t val
)
160 target_phys_addr_t pa
;
161 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, idx
);
162 stw_phys(pa
, vring_used_idx(vq
) + val
);
165 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
167 target_phys_addr_t pa
;
168 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
169 stw_phys(pa
, lduw_phys(pa
) | mask
);
172 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
174 target_phys_addr_t pa
;
175 pa
= vq
->vring
.used
+ offsetof(VRingUsed
, flags
);
176 stw_phys(pa
, lduw_phys(pa
) & ~mask
);
179 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
182 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
184 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
187 int virtio_queue_ready(VirtQueue
*vq
)
189 return vq
->vring
.avail
!= 0;
192 int virtio_queue_empty(VirtQueue
*vq
)
194 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
197 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
198 unsigned int len
, unsigned int idx
)
204 for (i
= 0; i
< elem
->in_num
; i
++) {
205 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
207 cpu_physical_memory_unmap(elem
->in_sg
[i
].iov_base
,
208 elem
->in_sg
[i
].iov_len
,
211 offset
+= elem
->in_sg
[i
].iov_len
;
214 for (i
= 0; i
< elem
->out_num
; i
++)
215 cpu_physical_memory_unmap(elem
->out_sg
[i
].iov_base
,
216 elem
->out_sg
[i
].iov_len
,
217 0, elem
->out_sg
[i
].iov_len
);
219 idx
= (idx
+ vring_used_idx(vq
)) % vq
->vring
.num
;
221 /* Get a pointer to the next entry in the used ring. */
222 vring_used_ring_id(vq
, idx
, elem
->index
);
223 vring_used_ring_len(vq
, idx
, len
);
226 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
228 /* Make sure buffer is written before we update index. */
230 vring_used_idx_increment(vq
, count
);
234 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
237 virtqueue_fill(vq
, elem
, len
, 0);
238 virtqueue_flush(vq
, 1);
241 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
243 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
245 /* Check it isn't doing very strange things with descriptor numbers. */
246 if (num_heads
> vq
->vring
.num
) {
247 fprintf(stderr
, "Guest moved used index from %u to %u",
248 idx
, vring_avail_idx(vq
));
255 static unsigned int virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
)
259 /* Grab the next descriptor number they're advertising, and increment
260 * the index we've seen. */
261 head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
263 /* If their number is silly, that's a fatal mistake. */
264 if (head
>= vq
->vring
.num
) {
265 fprintf(stderr
, "Guest says index %u is available", head
);
272 static unsigned virtqueue_next_desc(VirtQueue
*vq
, unsigned int i
)
276 /* If this descriptor says it doesn't chain, we're done. */
277 if (!(vring_desc_flags(vq
, i
) & VRING_DESC_F_NEXT
))
278 return vq
->vring
.num
;
280 /* Check they're not leading us off end of descriptors. */
281 next
= vring_desc_next(vq
, i
);
282 /* Make sure compiler knows to grab that: we don't want it changing! */
285 if (next
>= vq
->vring
.num
) {
286 fprintf(stderr
, "Desc next is %u", next
);
293 int virtqueue_avail_bytes(VirtQueue
*vq
, int in_bytes
, int out_bytes
)
296 int num_bufs
, in_total
, out_total
;
298 idx
= vq
->last_avail_idx
;
300 num_bufs
= in_total
= out_total
= 0;
301 while (virtqueue_num_heads(vq
, idx
)) {
304 i
= virtqueue_get_head(vq
, idx
++);
306 /* If we've got too many, that implies a descriptor loop. */
307 if (++num_bufs
> vq
->vring
.num
) {
308 fprintf(stderr
, "Looped descriptor");
312 if (vring_desc_flags(vq
, i
) & VRING_DESC_F_WRITE
) {
314 (in_total
+= vring_desc_len(vq
, i
)) >= in_bytes
)
318 (out_total
+= vring_desc_len(vq
, i
)) >= out_bytes
)
321 } while ((i
= virtqueue_next_desc(vq
, i
)) != vq
->vring
.num
);
327 int virtqueue_pop(VirtQueue
*vq
, VirtQueueElement
*elem
)
329 unsigned int i
, head
;
330 target_phys_addr_t len
;
332 if (!virtqueue_num_heads(vq
, vq
->last_avail_idx
))
335 /* When we start there are none of either input nor output. */
336 elem
->out_num
= elem
->in_num
= 0;
338 i
= head
= virtqueue_get_head(vq
, vq
->last_avail_idx
++);
343 if (vring_desc_flags(vq
, i
) & VRING_DESC_F_WRITE
) {
344 elem
->in_addr
[elem
->in_num
] = vring_desc_addr(vq
, i
);
345 sg
= &elem
->in_sg
[elem
->in_num
++];
348 sg
= &elem
->out_sg
[elem
->out_num
++];
350 /* Grab the first descriptor, and check it's OK. */
351 sg
->iov_len
= vring_desc_len(vq
, i
);
354 sg
->iov_base
= cpu_physical_memory_map(vring_desc_addr(vq
, i
), &len
, is_write
);
356 if (sg
->iov_base
== NULL
|| len
!= sg
->iov_len
) {
357 fprintf(stderr
, "virtio: trying to map MMIO memory\n");
361 /* If we've got too many, that implies a descriptor loop. */
362 if ((elem
->in_num
+ elem
->out_num
) > vq
->vring
.num
) {
363 fprintf(stderr
, "Looped descriptor");
366 } while ((i
= virtqueue_next_desc(vq
, i
)) != vq
->vring
.num
);
372 return elem
->in_num
+ elem
->out_num
;
377 void virtio_update_irq(VirtIODevice
*vdev
)
379 if (vdev
->binding
->update_irq
) {
380 vdev
->binding
->update_irq(vdev
->binding_opaque
);
384 void virtio_reset(void *opaque
)
386 VirtIODevice
*vdev
= opaque
;
396 virtio_update_irq(vdev
);
398 for(i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
399 vdev
->vq
[i
].vring
.desc
= 0;
400 vdev
->vq
[i
].vring
.avail
= 0;
401 vdev
->vq
[i
].vring
.used
= 0;
402 vdev
->vq
[i
].last_avail_idx
= 0;
407 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
411 vdev
->get_config(vdev
, vdev
->config
);
413 if (addr
> (vdev
->config_len
- sizeof(val
)))
416 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
420 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
424 vdev
->get_config(vdev
, vdev
->config
);
426 if (addr
> (vdev
->config_len
- sizeof(val
)))
429 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
433 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
437 vdev
->get_config(vdev
, vdev
->config
);
439 if (addr
> (vdev
->config_len
- sizeof(val
)))
442 memcpy(&val
, vdev
->config
+ addr
, sizeof(val
));
446 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
450 if (addr
> (vdev
->config_len
- sizeof(val
)))
453 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
455 if (vdev
->set_config
)
456 vdev
->set_config(vdev
, vdev
->config
);
459 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
463 if (addr
> (vdev
->config_len
- sizeof(val
)))
466 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
468 if (vdev
->set_config
)
469 vdev
->set_config(vdev
, vdev
->config
);
472 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
476 if (addr
> (vdev
->config_len
- sizeof(val
)))
479 memcpy(vdev
->config
+ addr
, &val
, sizeof(val
));
481 if (vdev
->set_config
)
482 vdev
->set_config(vdev
, vdev
->config
);
485 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, target_phys_addr_t addr
)
490 vdev
->vq
[n
].pa
= addr
;
491 virtqueue_init(&vdev
->vq
[n
]);
495 target_phys_addr_t
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
497 return vdev
->vq
[n
].pa
;
500 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
502 return vdev
->vq
[n
].vring
.num
;
505 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
507 if (n
< VIRTIO_PCI_QUEUE_MAX
&& vdev
->vq
[n
].vring
.desc
) {
508 vdev
->vq
[n
].handle_output(vdev
, &vdev
->vq
[n
]);
512 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
513 void (*handle_output
)(VirtIODevice
*, VirtQueue
*))
517 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
518 if (vdev
->vq
[i
].vring
.num
== 0)
522 if (i
== VIRTIO_PCI_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
525 vdev
->vq
[i
].vring
.num
= queue_size
;
526 vdev
->vq
[i
].handle_output
= handle_output
;
531 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
533 /* Always notify when queue is empty (when feature acknowledge) */
534 if ((vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
) &&
535 (!(vdev
->features
& (1 << VIRTIO_F_NOTIFY_ON_EMPTY
)) ||
536 (vq
->inuse
|| vring_avail_idx(vq
) != vq
->last_avail_idx
)))
540 virtio_update_irq(vdev
);
543 void virtio_notify_config(VirtIODevice
*vdev
)
545 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
549 virtio_update_irq(vdev
);
552 void virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
556 /* FIXME: load/save binding. */
557 //pci_device_save(&vdev->pci_dev, f);
559 qemu_put_8s(f
, &vdev
->status
);
560 qemu_put_8s(f
, &vdev
->isr
);
561 qemu_put_be16s(f
, &vdev
->queue_sel
);
562 qemu_put_be32s(f
, &vdev
->features
);
563 qemu_put_be32(f
, vdev
->config_len
);
564 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
566 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
567 if (vdev
->vq
[i
].vring
.num
== 0)
573 for (i
= 0; i
< VIRTIO_PCI_QUEUE_MAX
; i
++) {
574 if (vdev
->vq
[i
].vring
.num
== 0)
577 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
578 qemu_put_be64(f
, vdev
->vq
[i
].pa
);
579 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
583 void virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
)
587 /* FIXME: load/save binding. */
588 //pci_device_load(&vdev->pci_dev, f);
590 qemu_get_8s(f
, &vdev
->status
);
591 qemu_get_8s(f
, &vdev
->isr
);
592 qemu_get_be16s(f
, &vdev
->queue_sel
);
593 qemu_get_be32s(f
, &vdev
->features
);
594 vdev
->config_len
= qemu_get_be32(f
);
595 qemu_get_buffer(f
, vdev
->config
, vdev
->config_len
);
597 num
= qemu_get_be32(f
);
599 for (i
= 0; i
< num
; i
++) {
600 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
601 vdev
->vq
[i
].pa
= qemu_get_be64(f
);
602 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
604 if (vdev
->vq
[i
].pa
) {
605 virtqueue_init(&vdev
->vq
[i
]);
609 virtio_update_irq(vdev
);
612 void virtio_cleanup(VirtIODevice
*vdev
)
615 qemu_free(vdev
->config
);
619 VirtIODevice
*virtio_common_init(const char *name
, uint16_t device_id
,
620 size_t config_size
, size_t struct_size
)
624 vdev
= qemu_mallocz(struct_size
);
626 vdev
->device_id
= device_id
;
630 vdev
->vq
= qemu_mallocz(sizeof(VirtQueue
) * VIRTIO_PCI_QUEUE_MAX
);
633 vdev
->config_len
= config_size
;
634 if (vdev
->config_len
)
635 vdev
->config
= qemu_mallocz(config_size
);
639 qemu_register_reset(virtio_reset
, 0, vdev
);
644 void virtio_bind_device(VirtIODevice
*vdev
, const VirtIOBindings
*binding
,
647 vdev
->binding
= binding
;
648 vdev
->binding_opaque
= opaque
;