]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio.c
virtio-balloon: discard virtqueue element on reset
[mirror_qemu.git] / hw / virtio / virtio.c
CommitLineData
967f97fa
AL
1/*
2 * Virtio Support
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
9b8bfe21 14#include "qemu/osdep.h"
da34e65c 15#include "qapi/error.h"
4771d756
PB
16#include "qemu-common.h"
17#include "cpu.h"
64979a4d 18#include "trace.h"
fdfba1a2 19#include "exec/address-spaces.h"
1de7afc9 20#include "qemu/error-report.h"
0d09e41a 21#include "hw/virtio/virtio.h"
1de7afc9 22#include "qemu/atomic.h"
0d09e41a 23#include "hw/virtio/virtio-bus.h"
6b321a3d 24#include "migration/migration.h"
cee3ca00 25#include "hw/virtio/virtio-access.h"
967f97fa 26
6ce69d1c
PM
27/*
28 * The alignment to use between consumer and producer parts of vring.
29 * x86 pagesize again. This is the default, used by transports like PCI
30 * which don't provide a means for the guest to tell the host the alignment.
31 */
f46f15bc
AL
32#define VIRTIO_PCI_VRING_ALIGN 4096
33
967f97fa
AL
34typedef struct VRingDesc
35{
36 uint64_t addr;
37 uint32_t len;
38 uint16_t flags;
39 uint16_t next;
40} VRingDesc;
41
42typedef struct VRingAvail
43{
44 uint16_t flags;
45 uint16_t idx;
46 uint16_t ring[0];
47} VRingAvail;
48
49typedef struct VRingUsedElem
50{
51 uint32_t id;
52 uint32_t len;
53} VRingUsedElem;
54
55typedef struct VRingUsed
56{
57 uint16_t flags;
58 uint16_t idx;
59 VRingUsedElem ring[0];
60} VRingUsed;
61
62typedef struct VRing
63{
64 unsigned int num;
46c5d082 65 unsigned int num_default;
6ce69d1c 66 unsigned int align;
a8170e5e
AK
67 hwaddr desc;
68 hwaddr avail;
69 hwaddr used;
967f97fa
AL
70} VRing;
71
72struct VirtQueue
73{
74 VRing vring;
be1fea9b
VM
75
76 /* Next head to pop */
967f97fa 77 uint16_t last_avail_idx;
b796fcd1 78
be1fea9b
VM
79 /* Last avail_idx read from VQ. */
80 uint16_t shadow_avail_idx;
81
b796fcd1
VM
82 uint16_t used_idx;
83
bcbabae8
MT
84 /* Last used index value we have signalled on */
85 uint16_t signalled_used;
86
87 /* Last used index value we have signalled on */
88 bool signalled_used_valid;
89
90 /* Notification enabled? */
91 bool notification;
92
e78a2b42
JW
93 uint16_t queue_index;
94
967f97fa 95 int inuse;
bcbabae8 96
7055e687 97 uint16_t vector;
bf1780b0
FZ
98 VirtIOHandleOutput handle_output;
99 VirtIOHandleOutput handle_aio_output;
872dd82c 100 bool use_aio;
1cbdabe2
MT
101 VirtIODevice *vdev;
102 EventNotifier guest_notifier;
103 EventNotifier host_notifier;
e0d686bf 104 QLIST_ENTRY(VirtQueue) node;
967f97fa
AL
105};
106
967f97fa 107/* virt queue functions */
ab223c95 108void virtio_queue_update_rings(VirtIODevice *vdev, int n)
967f97fa 109{
ab223c95 110 VRing *vring = &vdev->vq[n].vring;
53c25cea 111
ab223c95
CH
112 if (!vring->desc) {
113 /* not yet setup -> nothing to do */
114 return;
115 }
116 vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
117 vring->used = vring_align(vring->avail +
118 offsetof(VRingAvail, ring[vring->num]),
119 vring->align);
967f97fa
AL
120}
121
aa570d6f
PB
122static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
123 hwaddr desc_pa, int i)
967f97fa 124{
aa570d6f
PB
125 address_space_read(&address_space_memory, desc_pa + i * sizeof(VRingDesc),
126 MEMTXATTRS_UNSPECIFIED, (void *)desc, sizeof(VRingDesc));
127 virtio_tswap64s(vdev, &desc->addr);
128 virtio_tswap32s(vdev, &desc->len);
129 virtio_tswap16s(vdev, &desc->flags);
130 virtio_tswap16s(vdev, &desc->next);
967f97fa
AL
131}
132
133static inline uint16_t vring_avail_flags(VirtQueue *vq)
134{
a8170e5e 135 hwaddr pa;
967f97fa 136 pa = vq->vring.avail + offsetof(VRingAvail, flags);
cee3ca00 137 return virtio_lduw_phys(vq->vdev, pa);
967f97fa
AL
138}
139
140static inline uint16_t vring_avail_idx(VirtQueue *vq)
141{
a8170e5e 142 hwaddr pa;
967f97fa 143 pa = vq->vring.avail + offsetof(VRingAvail, idx);
be1fea9b
VM
144 vq->shadow_avail_idx = virtio_lduw_phys(vq->vdev, pa);
145 return vq->shadow_avail_idx;
967f97fa
AL
146}
147
148static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
149{
a8170e5e 150 hwaddr pa;
967f97fa 151 pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
cee3ca00 152 return virtio_lduw_phys(vq->vdev, pa);
967f97fa
AL
153}
154
e9600c6c 155static inline uint16_t vring_get_used_event(VirtQueue *vq)
bcbabae8
MT
156{
157 return vring_avail_ring(vq, vq->vring.num);
158}
159
1cdd2ee5
VM
160static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
161 int i)
967f97fa 162{
a8170e5e 163 hwaddr pa;
1cdd2ee5
VM
164 virtio_tswap32s(vq->vdev, &uelem->id);
165 virtio_tswap32s(vq->vdev, &uelem->len);
166 pa = vq->vring.used + offsetof(VRingUsed, ring[i]);
167 address_space_write(&address_space_memory, pa, MEMTXATTRS_UNSPECIFIED,
168 (void *)uelem, sizeof(VRingUsedElem));
967f97fa
AL
169}
170
171static uint16_t vring_used_idx(VirtQueue *vq)
172{
a8170e5e 173 hwaddr pa;
967f97fa 174 pa = vq->vring.used + offsetof(VRingUsed, idx);
cee3ca00 175 return virtio_lduw_phys(vq->vdev, pa);
967f97fa
AL
176}
177
bcbabae8 178static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
967f97fa 179{
a8170e5e 180 hwaddr pa;
967f97fa 181 pa = vq->vring.used + offsetof(VRingUsed, idx);
cee3ca00 182 virtio_stw_phys(vq->vdev, pa, val);
b796fcd1 183 vq->used_idx = val;
967f97fa
AL
184}
185
186static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
187{
cee3ca00 188 VirtIODevice *vdev = vq->vdev;
a8170e5e 189 hwaddr pa;
967f97fa 190 pa = vq->vring.used + offsetof(VRingUsed, flags);
cee3ca00 191 virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask);
967f97fa
AL
192}
193
194static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
195{
cee3ca00 196 VirtIODevice *vdev = vq->vdev;
a8170e5e 197 hwaddr pa;
967f97fa 198 pa = vq->vring.used + offsetof(VRingUsed, flags);
cee3ca00 199 virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask);
967f97fa
AL
200}
201
e9600c6c 202static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
bcbabae8 203{
a8170e5e 204 hwaddr pa;
bcbabae8
MT
205 if (!vq->notification) {
206 return;
207 }
208 pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
cee3ca00 209 virtio_stw_phys(vq->vdev, pa, val);
bcbabae8
MT
210}
211
967f97fa
AL
212void virtio_queue_set_notification(VirtQueue *vq, int enable)
213{
bcbabae8 214 vq->notification = enable;
95129d6f 215 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 216 vring_set_avail_event(vq, vring_avail_idx(vq));
bcbabae8 217 } else if (enable) {
967f97fa 218 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 219 } else {
967f97fa 220 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 221 }
92045d80
MT
222 if (enable) {
223 /* Expose avail event/used flags before caller checks the avail idx. */
224 smp_mb();
225 }
967f97fa
AL
226}
227
228int virtio_queue_ready(VirtQueue *vq)
229{
230 return vq->vring.avail != 0;
231}
232
be1fea9b
VM
233/* Fetch avail_idx from VQ memory only when we really need to know if
234 * guest has added some buffers. */
967f97fa
AL
235int virtio_queue_empty(VirtQueue *vq)
236{
be1fea9b
VM
237 if (vq->shadow_avail_idx != vq->last_avail_idx) {
238 return 0;
239 }
240
967f97fa
AL
241 return vring_avail_idx(vq) == vq->last_avail_idx;
242}
243
ce317461
JW
244static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
245 unsigned int len)
967f97fa
AL
246{
247 unsigned int offset;
248 int i;
249
967f97fa
AL
250 offset = 0;
251 for (i = 0; i < elem->in_num; i++) {
252 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
253
26b258e1
AL
254 cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
255 elem->in_sg[i].iov_len,
256 1, size);
967f97fa 257
0cea71a2 258 offset += size;
967f97fa
AL
259 }
260
26b258e1
AL
261 for (i = 0; i < elem->out_num; i++)
262 cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
263 elem->out_sg[i].iov_len,
264 0, elem->out_sg[i].iov_len);
ce317461
JW
265}
266
29b9f5ef
JW
267void virtqueue_discard(VirtQueue *vq, const VirtQueueElement *elem,
268 unsigned int len)
269{
270 vq->last_avail_idx--;
58a83c61 271 vq->inuse--;
29b9f5ef
JW
272 virtqueue_unmap_sg(vq, elem, len);
273}
274
ce317461
JW
275void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
276 unsigned int len, unsigned int idx)
277{
1cdd2ee5
VM
278 VRingUsedElem uelem;
279
ce317461
JW
280 trace_virtqueue_fill(vq, elem, len, idx);
281
282 virtqueue_unmap_sg(vq, elem, len);
26b258e1 283
b796fcd1 284 idx = (idx + vq->used_idx) % vq->vring.num;
967f97fa 285
1cdd2ee5
VM
286 uelem.id = elem->index;
287 uelem.len = len;
288 vring_used_write(vq, &uelem, idx);
967f97fa
AL
289}
290
291void virtqueue_flush(VirtQueue *vq, unsigned int count)
292{
bcbabae8 293 uint16_t old, new;
967f97fa 294 /* Make sure buffer is written before we update index. */
b90d2f35 295 smp_wmb();
64979a4d 296 trace_virtqueue_flush(vq, count);
b796fcd1 297 old = vq->used_idx;
bcbabae8
MT
298 new = old + count;
299 vring_used_idx_set(vq, new);
967f97fa 300 vq->inuse -= count;
bcbabae8
MT
301 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
302 vq->signalled_used_valid = false;
967f97fa
AL
303}
304
305void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
306 unsigned int len)
307{
308 virtqueue_fill(vq, elem, len, 0);
309 virtqueue_flush(vq, 1);
310}
311
312static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
313{
314 uint16_t num_heads = vring_avail_idx(vq) - idx;
315
316 /* Check it isn't doing very strange things with descriptor numbers. */
bb6834cf 317 if (num_heads > vq->vring.num) {
ce67ed65 318 error_report("Guest moved used index from %u to %u",
be1fea9b 319 idx, vq->shadow_avail_idx);
bb6834cf
AL
320 exit(1);
321 }
a821ce59
MT
322 /* On success, callers read a descriptor at vq->last_avail_idx.
323 * Make sure descriptor read does not bypass avail index read. */
324 if (num_heads) {
325 smp_rmb();
326 }
967f97fa
AL
327
328 return num_heads;
329}
330
331static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
332{
333 unsigned int head;
334
335 /* Grab the next descriptor number they're advertising, and increment
336 * the index we've seen. */
337 head = vring_avail_ring(vq, idx % vq->vring.num);
338
339 /* If their number is silly, that's a fatal mistake. */
bb6834cf 340 if (head >= vq->vring.num) {
ce67ed65 341 error_report("Guest says index %u is available", head);
bb6834cf
AL
342 exit(1);
343 }
967f97fa
AL
344
345 return head;
346}
347
aa570d6f
PB
348static unsigned virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
349 hwaddr desc_pa, unsigned int max)
967f97fa
AL
350{
351 unsigned int next;
352
353 /* If this descriptor says it doesn't chain, we're done. */
aa570d6f 354 if (!(desc->flags & VRING_DESC_F_NEXT)) {
5774cf98 355 return max;
cee3ca00 356 }
967f97fa
AL
357
358 /* Check they're not leading us off end of descriptors. */
aa570d6f 359 next = desc->next;
967f97fa 360 /* Make sure compiler knows to grab that: we don't want it changing! */
b90d2f35 361 smp_wmb();
967f97fa 362
5774cf98 363 if (next >= max) {
ce67ed65 364 error_report("Desc next is %u", next);
bb6834cf
AL
365 exit(1);
366 }
967f97fa 367
aa570d6f 368 vring_desc_read(vdev, desc, desc_pa, next);
967f97fa
AL
369 return next;
370}
371
0d8d7690 372void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
e1f7b481
MT
373 unsigned int *out_bytes,
374 unsigned max_in_bytes, unsigned max_out_bytes)
967f97fa 375{
efeea6d0 376 unsigned int idx;
385ce95d 377 unsigned int total_bufs, in_total, out_total;
967f97fa
AL
378
379 idx = vq->last_avail_idx;
380
efeea6d0 381 total_bufs = in_total = out_total = 0;
967f97fa 382 while (virtqueue_num_heads(vq, idx)) {
cee3ca00 383 VirtIODevice *vdev = vq->vdev;
efeea6d0 384 unsigned int max, num_bufs, indirect = 0;
aa570d6f 385 VRingDesc desc;
a8170e5e 386 hwaddr desc_pa;
967f97fa
AL
387 int i;
388
efeea6d0
MM
389 max = vq->vring.num;
390 num_bufs = total_bufs;
967f97fa 391 i = virtqueue_get_head(vq, idx++);
efeea6d0 392 desc_pa = vq->vring.desc;
aa570d6f 393 vring_desc_read(vdev, &desc, desc_pa, i);
efeea6d0 394
aa570d6f
PB
395 if (desc.flags & VRING_DESC_F_INDIRECT) {
396 if (desc.len % sizeof(VRingDesc)) {
ce67ed65 397 error_report("Invalid size for indirect buffer table");
efeea6d0
MM
398 exit(1);
399 }
400
401 /* If we've got too many, that implies a descriptor loop. */
402 if (num_bufs >= max) {
ce67ed65 403 error_report("Looped descriptor");
efeea6d0
MM
404 exit(1);
405 }
406
407 /* loop over the indirect descriptor table */
408 indirect = 1;
aa570d6f
PB
409 max = desc.len / sizeof(VRingDesc);
410 desc_pa = desc.addr;
1ae2757c 411 num_bufs = i = 0;
aa570d6f 412 vring_desc_read(vdev, &desc, desc_pa, i);
efeea6d0
MM
413 }
414
967f97fa
AL
415 do {
416 /* If we've got too many, that implies a descriptor loop. */
5774cf98 417 if (++num_bufs > max) {
ce67ed65 418 error_report("Looped descriptor");
bb6834cf
AL
419 exit(1);
420 }
967f97fa 421
aa570d6f
PB
422 if (desc.flags & VRING_DESC_F_WRITE) {
423 in_total += desc.len;
967f97fa 424 } else {
aa570d6f 425 out_total += desc.len;
967f97fa 426 }
e1f7b481
MT
427 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
428 goto done;
429 }
aa570d6f 430 } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) != max);
efeea6d0
MM
431
432 if (!indirect)
433 total_bufs = num_bufs;
434 else
435 total_bufs++;
967f97fa 436 }
e1f7b481 437done:
0d8d7690
AS
438 if (in_bytes) {
439 *in_bytes = in_total;
440 }
441 if (out_bytes) {
442 *out_bytes = out_total;
443 }
444}
967f97fa 445
0d8d7690
AS
446int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
447 unsigned int out_bytes)
448{
449 unsigned int in_total, out_total;
450
e1f7b481
MT
451 virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
452 return in_bytes <= in_total && out_bytes <= out_total;
967f97fa
AL
453}
454
3b3b0628
PB
455static void virtqueue_map_desc(unsigned int *p_num_sg, hwaddr *addr, struct iovec *iov,
456 unsigned int max_num_sg, bool is_write,
457 hwaddr pa, size_t sz)
458{
459 unsigned num_sg = *p_num_sg;
460 assert(num_sg <= max_num_sg);
461
1e7aed70
PP
462 if (!sz) {
463 error_report("virtio: zero sized buffers are not allowed");
464 exit(1);
465 }
466
3b3b0628
PB
467 while (sz) {
468 hwaddr len = sz;
469
470 if (num_sg == max_num_sg) {
471 error_report("virtio: too many write descriptors in indirect table");
472 exit(1);
473 }
474
475 iov[num_sg].iov_base = cpu_physical_memory_map(pa, &len, is_write);
476 iov[num_sg].iov_len = len;
477 addr[num_sg] = pa;
478
479 sz -= len;
480 pa += len;
481 num_sg++;
482 }
483 *p_num_sg = num_sg;
484}
485
8059feee
MT
486static void virtqueue_map_iovec(struct iovec *sg, hwaddr *addr,
487 unsigned int *num_sg, unsigned int max_size,
488 int is_write)
42fb2e07
KW
489{
490 unsigned int i;
a8170e5e 491 hwaddr len;
42fb2e07 492
8059feee
MT
493 /* Note: this function MUST validate input, some callers
494 * are passing in num_sg values received over the network.
495 */
496 /* TODO: teach all callers that this can fail, and return failure instead
497 * of asserting here.
498 * When we do, we might be able to re-enable NDEBUG below.
499 */
500#ifdef NDEBUG
501#error building with NDEBUG is not supported
502#endif
503 assert(*num_sg <= max_size);
504
505 for (i = 0; i < *num_sg; i++) {
42fb2e07
KW
506 len = sg[i].iov_len;
507 sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
8059feee 508 if (!sg[i].iov_base) {
1a285899 509 error_report("virtio: error trying to map MMIO memory");
42fb2e07
KW
510 exit(1);
511 }
3b3b0628
PB
512 if (len != sg[i].iov_len) {
513 error_report("virtio: unexpected memory split");
8059feee
MT
514 exit(1);
515 }
42fb2e07
KW
516 }
517}
518
8059feee
MT
519void virtqueue_map(VirtQueueElement *elem)
520{
521 virtqueue_map_iovec(elem->in_sg, elem->in_addr, &elem->in_num,
3724650d 522 VIRTQUEUE_MAX_SIZE, 1);
8059feee 523 virtqueue_map_iovec(elem->out_sg, elem->out_addr, &elem->out_num,
3724650d
PB
524 VIRTQUEUE_MAX_SIZE, 0);
525}
526
527void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
528{
529 VirtQueueElement *elem;
530 size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
531 size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
532 size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
533 size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
534 size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
535 size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
536
537 assert(sz >= sizeof(VirtQueueElement));
538 elem = g_malloc(out_sg_end);
539 elem->out_num = out_num;
540 elem->in_num = in_num;
541 elem->in_addr = (void *)elem + in_addr_ofs;
542 elem->out_addr = (void *)elem + out_addr_ofs;
543 elem->in_sg = (void *)elem + in_sg_ofs;
544 elem->out_sg = (void *)elem + out_sg_ofs;
545 return elem;
8059feee
MT
546}
547
51b19ebe 548void *virtqueue_pop(VirtQueue *vq, size_t sz)
967f97fa 549{
5774cf98 550 unsigned int i, head, max;
a8170e5e 551 hwaddr desc_pa = vq->vring.desc;
cee3ca00 552 VirtIODevice *vdev = vq->vdev;
51b19ebe 553 VirtQueueElement *elem;
3b3b0628
PB
554 unsigned out_num, in_num;
555 hwaddr addr[VIRTQUEUE_MAX_SIZE];
556 struct iovec iov[VIRTQUEUE_MAX_SIZE];
aa570d6f 557 VRingDesc desc;
967f97fa 558
be1fea9b 559 if (virtio_queue_empty(vq)) {
51b19ebe
PB
560 return NULL;
561 }
be1fea9b
VM
562 /* Needed after virtio_queue_empty(), see comment in
563 * virtqueue_num_heads(). */
564 smp_rmb();
967f97fa
AL
565
566 /* When we start there are none of either input nor output. */
3b3b0628 567 out_num = in_num = 0;
967f97fa 568
5774cf98
MM
569 max = vq->vring.num;
570
afd9096e
SH
571 if (vq->inuse >= vq->vring.num) {
572 error_report("Virtqueue size exceeded");
573 exit(1);
574 }
575
967f97fa 576 i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
95129d6f 577 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 578 vring_set_avail_event(vq, vq->last_avail_idx);
bcbabae8 579 }
efeea6d0 580
aa570d6f
PB
581 vring_desc_read(vdev, &desc, desc_pa, i);
582 if (desc.flags & VRING_DESC_F_INDIRECT) {
583 if (desc.len % sizeof(VRingDesc)) {
ce67ed65 584 error_report("Invalid size for indirect buffer table");
efeea6d0
MM
585 exit(1);
586 }
587
588 /* loop over the indirect descriptor table */
aa570d6f
PB
589 max = desc.len / sizeof(VRingDesc);
590 desc_pa = desc.addr;
efeea6d0 591 i = 0;
aa570d6f 592 vring_desc_read(vdev, &desc, desc_pa, i);
efeea6d0
MM
593 }
594
42fb2e07 595 /* Collect all the descriptors */
967f97fa 596 do {
aa570d6f 597 if (desc.flags & VRING_DESC_F_WRITE) {
3b3b0628 598 virtqueue_map_desc(&in_num, addr + out_num, iov + out_num,
aa570d6f 599 VIRTQUEUE_MAX_SIZE - out_num, true, desc.addr, desc.len);
42fb2e07 600 } else {
3b3b0628
PB
601 if (in_num) {
602 error_report("Incorrect order for descriptors");
c8eac1cf
MT
603 exit(1);
604 }
3b3b0628 605 virtqueue_map_desc(&out_num, addr, iov,
aa570d6f 606 VIRTQUEUE_MAX_SIZE, false, desc.addr, desc.len);
42fb2e07 607 }
967f97fa 608
967f97fa 609 /* If we've got too many, that implies a descriptor loop. */
3b3b0628 610 if ((in_num + out_num) > max) {
ce67ed65 611 error_report("Looped descriptor");
bb6834cf
AL
612 exit(1);
613 }
aa570d6f 614 } while ((i = virtqueue_read_next_desc(vdev, &desc, desc_pa, max)) != max);
967f97fa 615
3b3b0628
PB
616 /* Now copy what we have collected and mapped */
617 elem = virtqueue_alloc_element(sz, out_num, in_num);
967f97fa 618 elem->index = head;
3b3b0628
PB
619 for (i = 0; i < out_num; i++) {
620 elem->out_addr[i] = addr[i];
621 elem->out_sg[i] = iov[i];
622 }
623 for (i = 0; i < in_num; i++) {
624 elem->in_addr[i] = addr[out_num + i];
625 elem->in_sg[i] = iov[out_num + i];
626 }
967f97fa
AL
627
628 vq->inuse++;
629
64979a4d 630 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
51b19ebe 631 return elem;
967f97fa
AL
632}
633
3724650d
PB
634/* Reading and writing a structure directly to QEMUFile is *awful*, but
635 * it is what QEMU has always done by mistake. We can change it sooner
636 * or later by bumping the version number of the affected vm states.
637 * In the meanwhile, since the in-memory layout of VirtQueueElement
638 * has changed, we need to marshal to and from the layout that was
639 * used before the change.
640 */
641typedef struct VirtQueueElementOld {
642 unsigned int index;
643 unsigned int out_num;
644 unsigned int in_num;
645 hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
646 hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
647 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
648 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
649} VirtQueueElementOld;
650
ab281c17
PB
651void *qemu_get_virtqueue_element(QEMUFile *f, size_t sz)
652{
3724650d
PB
653 VirtQueueElement *elem;
654 VirtQueueElementOld data;
655 int i;
656
657 qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
658
659 elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
660 elem->index = data.index;
661
662 for (i = 0; i < elem->in_num; i++) {
663 elem->in_addr[i] = data.in_addr[i];
664 }
665
666 for (i = 0; i < elem->out_num; i++) {
667 elem->out_addr[i] = data.out_addr[i];
668 }
669
670 for (i = 0; i < elem->in_num; i++) {
671 /* Base is overwritten by virtqueue_map. */
672 elem->in_sg[i].iov_base = 0;
673 elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
674 }
675
676 for (i = 0; i < elem->out_num; i++) {
677 /* Base is overwritten by virtqueue_map. */
678 elem->out_sg[i].iov_base = 0;
679 elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
680 }
681
ab281c17
PB
682 virtqueue_map(elem);
683 return elem;
684}
685
686void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem)
687{
3724650d
PB
688 VirtQueueElementOld data;
689 int i;
690
691 memset(&data, 0, sizeof(data));
692 data.index = elem->index;
693 data.in_num = elem->in_num;
694 data.out_num = elem->out_num;
695
696 for (i = 0; i < elem->in_num; i++) {
697 data.in_addr[i] = elem->in_addr[i];
698 }
699
700 for (i = 0; i < elem->out_num; i++) {
701 data.out_addr[i] = elem->out_addr[i];
702 }
703
704 for (i = 0; i < elem->in_num; i++) {
705 /* Base is overwritten by virtqueue_map when loading. Do not
706 * save it, as it would leak the QEMU address space layout. */
707 data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
708 }
709
710 for (i = 0; i < elem->out_num; i++) {
711 /* Do not save iov_base as above. */
712 data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
713 }
714 qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
ab281c17
PB
715}
716
967f97fa 717/* virtio device */
7055e687
MT
718static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
719{
1c819449
FK
720 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
721 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
722
723 if (k->notify) {
724 k->notify(qbus->parent, vector);
7055e687
MT
725 }
726}
967f97fa 727
53c25cea 728void virtio_update_irq(VirtIODevice *vdev)
967f97fa 729{
7055e687 730 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
967f97fa
AL
731}
732
0b352fd6
CH
733static int virtio_validate_features(VirtIODevice *vdev)
734{
735 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
736
737 if (k->validate_features) {
738 return k->validate_features(vdev);
739 } else {
740 return 0;
741 }
742}
743
744int virtio_set_status(VirtIODevice *vdev, uint8_t val)
4e1837f8 745{
181103cd 746 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
4e1837f8
SH
747 trace_virtio_set_status(vdev, val);
748
95129d6f 749 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
0b352fd6
CH
750 if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
751 val & VIRTIO_CONFIG_S_FEATURES_OK) {
752 int ret = virtio_validate_features(vdev);
753
754 if (ret) {
755 return ret;
756 }
757 }
758 }
181103cd
FK
759 if (k->set_status) {
760 k->set_status(vdev, val);
4e1837f8
SH
761 }
762 vdev->status = val;
0b352fd6 763 return 0;
4e1837f8
SH
764}
765
616a6552
GK
766bool target_words_bigendian(void);
767static enum virtio_device_endian virtio_default_endian(void)
768{
769 if (target_words_bigendian()) {
770 return VIRTIO_DEVICE_ENDIAN_BIG;
771 } else {
772 return VIRTIO_DEVICE_ENDIAN_LITTLE;
773 }
774}
775
776static enum virtio_device_endian virtio_current_cpu_endian(void)
777{
778 CPUClass *cc = CPU_GET_CLASS(current_cpu);
779
780 if (cc->virtio_is_big_endian(current_cpu)) {
781 return VIRTIO_DEVICE_ENDIAN_BIG;
782 } else {
783 return VIRTIO_DEVICE_ENDIAN_LITTLE;
784 }
785}
786
53c25cea 787void virtio_reset(void *opaque)
967f97fa
AL
788{
789 VirtIODevice *vdev = opaque;
181103cd 790 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
791 int i;
792
e0c472d8 793 virtio_set_status(vdev, 0);
616a6552
GK
794 if (current_cpu) {
795 /* Guest initiated reset */
796 vdev->device_endian = virtio_current_cpu_endian();
797 } else {
798 /* System reset */
799 vdev->device_endian = virtio_default_endian();
800 }
e0c472d8 801
181103cd
FK
802 if (k->reset) {
803 k->reset(vdev);
804 }
967f97fa 805
704a76fc 806 vdev->guest_features = 0;
967f97fa
AL
807 vdev->queue_sel = 0;
808 vdev->status = 0;
809 vdev->isr = 0;
7055e687
MT
810 vdev->config_vector = VIRTIO_NO_VECTOR;
811 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa 812
87b3bd1c 813 for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
814 vdev->vq[i].vring.desc = 0;
815 vdev->vq[i].vring.avail = 0;
816 vdev->vq[i].vring.used = 0;
817 vdev->vq[i].last_avail_idx = 0;
be1fea9b 818 vdev->vq[i].shadow_avail_idx = 0;
b796fcd1 819 vdev->vq[i].used_idx = 0;
e0d686bf 820 virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
bcbabae8
MT
821 vdev->vq[i].signalled_used = 0;
822 vdev->vq[i].signalled_used_valid = false;
823 vdev->vq[i].notification = true;
46c5d082 824 vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
4b7f91ed 825 vdev->vq[i].inuse = 0;
967f97fa
AL
826 }
827}
828
53c25cea 829uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
967f97fa 830{
181103cd 831 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
832 uint8_t val;
833
5f5a1318 834 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 835 return (uint32_t)-1;
5f5a1318
JW
836 }
837
838 k->get_config(vdev, vdev->config);
967f97fa 839
06dbfc6f 840 val = ldub_p(vdev->config + addr);
967f97fa
AL
841 return val;
842}
843
53c25cea 844uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
967f97fa 845{
181103cd 846 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
847 uint16_t val;
848
5f5a1318 849 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 850 return (uint32_t)-1;
5f5a1318
JW
851 }
852
853 k->get_config(vdev, vdev->config);
967f97fa 854
06dbfc6f 855 val = lduw_p(vdev->config + addr);
967f97fa
AL
856 return val;
857}
858
53c25cea 859uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
967f97fa 860{
181103cd 861 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
862 uint32_t val;
863
5f5a1318 864 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 865 return (uint32_t)-1;
5f5a1318
JW
866 }
867
868 k->get_config(vdev, vdev->config);
967f97fa 869
06dbfc6f 870 val = ldl_p(vdev->config + addr);
967f97fa
AL
871 return val;
872}
873
53c25cea 874void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 875{
181103cd 876 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
877 uint8_t val = data;
878
5f5a1318 879 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 880 return;
5f5a1318 881 }
967f97fa 882
06dbfc6f 883 stb_p(vdev->config + addr, val);
967f97fa 884
181103cd
FK
885 if (k->set_config) {
886 k->set_config(vdev, vdev->config);
887 }
967f97fa
AL
888}
889
53c25cea 890void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 891{
181103cd 892 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
893 uint16_t val = data;
894
5f5a1318 895 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 896 return;
5f5a1318 897 }
967f97fa 898
06dbfc6f 899 stw_p(vdev->config + addr, val);
967f97fa 900
181103cd
FK
901 if (k->set_config) {
902 k->set_config(vdev, vdev->config);
903 }
967f97fa
AL
904}
905
53c25cea 906void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 907{
181103cd 908 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
909 uint32_t val = data;
910
5f5a1318 911 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 912 return;
5f5a1318 913 }
967f97fa 914
06dbfc6f 915 stl_p(vdev->config + addr, val);
967f97fa 916
181103cd
FK
917 if (k->set_config) {
918 k->set_config(vdev, vdev->config);
919 }
967f97fa
AL
920}
921
adfb743c
MT
922uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
923{
924 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
925 uint8_t val;
926
927 if (addr + sizeof(val) > vdev->config_len) {
928 return (uint32_t)-1;
929 }
930
931 k->get_config(vdev, vdev->config);
932
933 val = ldub_p(vdev->config + addr);
934 return val;
935}
936
937uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
938{
939 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
940 uint16_t val;
941
942 if (addr + sizeof(val) > vdev->config_len) {
943 return (uint32_t)-1;
944 }
945
946 k->get_config(vdev, vdev->config);
947
948 val = lduw_le_p(vdev->config + addr);
949 return val;
950}
951
952uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
953{
954 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
955 uint32_t val;
956
957 if (addr + sizeof(val) > vdev->config_len) {
958 return (uint32_t)-1;
959 }
960
961 k->get_config(vdev, vdev->config);
962
963 val = ldl_le_p(vdev->config + addr);
964 return val;
965}
966
967void virtio_config_modern_writeb(VirtIODevice *vdev,
968 uint32_t addr, uint32_t data)
969{
970 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
971 uint8_t val = data;
972
973 if (addr + sizeof(val) > vdev->config_len) {
974 return;
975 }
976
977 stb_p(vdev->config + addr, val);
978
979 if (k->set_config) {
980 k->set_config(vdev, vdev->config);
981 }
982}
983
984void virtio_config_modern_writew(VirtIODevice *vdev,
985 uint32_t addr, uint32_t data)
986{
987 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
988 uint16_t val = data;
989
990 if (addr + sizeof(val) > vdev->config_len) {
991 return;
992 }
993
994 stw_le_p(vdev->config + addr, val);
995
996 if (k->set_config) {
997 k->set_config(vdev, vdev->config);
998 }
999}
1000
1001void virtio_config_modern_writel(VirtIODevice *vdev,
1002 uint32_t addr, uint32_t data)
1003{
1004 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1005 uint32_t val = data;
1006
1007 if (addr + sizeof(val) > vdev->config_len) {
1008 return;
1009 }
1010
1011 stl_le_p(vdev->config + addr, val);
1012
1013 if (k->set_config) {
1014 k->set_config(vdev, vdev->config);
1015 }
1016}
1017
a8170e5e 1018void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
967f97fa 1019{
ab223c95
CH
1020 vdev->vq[n].vring.desc = addr;
1021 virtio_queue_update_rings(vdev, n);
53c25cea
PB
1022}
1023
a8170e5e 1024hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
53c25cea 1025{
ab223c95
CH
1026 return vdev->vq[n].vring.desc;
1027}
1028
1029void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
1030 hwaddr avail, hwaddr used)
1031{
1032 vdev->vq[n].vring.desc = desc;
1033 vdev->vq[n].vring.avail = avail;
1034 vdev->vq[n].vring.used = used;
53c25cea
PB
1035}
1036
e63c0ba1
PM
1037void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
1038{
f6049f44
PM
1039 /* Don't allow guest to flip queue between existent and
1040 * nonexistent states, or to set it to an invalid size.
1041 */
1042 if (!!num != !!vdev->vq[n].vring.num ||
1043 num > VIRTQUEUE_MAX_SIZE ||
1044 num < 0) {
1045 return;
e63c0ba1 1046 }
f6049f44 1047 vdev->vq[n].vring.num = num;
e63c0ba1
PM
1048}
1049
e0d686bf
JW
1050VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
1051{
1052 return QLIST_FIRST(&vdev->vector_queues[vector]);
1053}
1054
1055VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
1056{
1057 return QLIST_NEXT(vq, node);
1058}
1059
53c25cea
PB
1060int virtio_queue_get_num(VirtIODevice *vdev, int n)
1061{
1062 return vdev->vq[n].vring.num;
1063}
967f97fa 1064
8ad176aa
JW
1065int virtio_get_num_queues(VirtIODevice *vdev)
1066{
1067 int i;
1068
87b3bd1c 1069 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
8ad176aa
JW
1070 if (!virtio_queue_get_num(vdev, i)) {
1071 break;
1072 }
1073 }
1074
1075 return i;
1076}
1077
6ce69d1c
PM
1078void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
1079{
1080 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1081 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1082
ab223c95 1083 /* virtio-1 compliant devices cannot change the alignment */
95129d6f 1084 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
ab223c95
CH
1085 error_report("tried to modify queue alignment for virtio-1 device");
1086 return;
1087 }
6ce69d1c
PM
1088 /* Check that the transport told us it was going to do this
1089 * (so a buggy transport will immediately assert rather than
1090 * silently failing to migrate this state)
1091 */
1092 assert(k->has_variable_vring_alignment);
1093
1094 vdev->vq[n].vring.align = align;
ab223c95 1095 virtio_queue_update_rings(vdev, n);
6ce69d1c
PM
1096}
1097
344dc16f
MT
1098static void virtio_queue_notify_aio_vq(VirtQueue *vq)
1099{
1100 if (vq->vring.desc && vq->handle_aio_output) {
1101 VirtIODevice *vdev = vq->vdev;
1102
1103 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1104 vq->handle_aio_output(vdev, vq);
1105 }
1106}
1107
2b2cbcad 1108static void virtio_queue_notify_vq(VirtQueue *vq)
25db9ebe 1109{
9e0f5b81 1110 if (vq->vring.desc && vq->handle_output) {
25db9ebe 1111 VirtIODevice *vdev = vq->vdev;
9e0f5b81 1112
25db9ebe
SH
1113 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1114 vq->handle_output(vdev, vq);
1115 }
1116}
1117
53c25cea
PB
1118void virtio_queue_notify(VirtIODevice *vdev, int n)
1119{
7157e2e2 1120 virtio_queue_notify_vq(&vdev->vq[n]);
967f97fa
AL
1121}
1122
7055e687
MT
1123uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
1124{
87b3bd1c 1125 return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
7055e687
MT
1126 VIRTIO_NO_VECTOR;
1127}
1128
1129void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
1130{
e0d686bf
JW
1131 VirtQueue *vq = &vdev->vq[n];
1132
87b3bd1c 1133 if (n < VIRTIO_QUEUE_MAX) {
e0d686bf
JW
1134 if (vdev->vector_queues &&
1135 vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
1136 QLIST_REMOVE(vq, node);
1137 }
7055e687 1138 vdev->vq[n].vector = vector;
e0d686bf
JW
1139 if (vdev->vector_queues &&
1140 vector != VIRTIO_NO_VECTOR) {
1141 QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
1142 }
1143 }
7055e687
MT
1144}
1145
872dd82c
FZ
1146static VirtQueue *virtio_add_queue_internal(VirtIODevice *vdev, int queue_size,
1147 VirtIOHandleOutput handle_output,
1148 bool use_aio)
967f97fa
AL
1149{
1150 int i;
1151
87b3bd1c 1152 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
1153 if (vdev->vq[i].vring.num == 0)
1154 break;
1155 }
1156
87b3bd1c 1157 if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
967f97fa
AL
1158 abort();
1159
1160 vdev->vq[i].vring.num = queue_size;
46c5d082 1161 vdev->vq[i].vring.num_default = queue_size;
6ce69d1c 1162 vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
967f97fa 1163 vdev->vq[i].handle_output = handle_output;
344dc16f 1164 vdev->vq[i].handle_aio_output = NULL;
872dd82c 1165 vdev->vq[i].use_aio = use_aio;
967f97fa
AL
1166
1167 return &vdev->vq[i];
1168}
1169
872dd82c
FZ
1170/* Add a virt queue and mark AIO.
1171 * An AIO queue will use the AioContext based event interface instead of the
1172 * default IOHandler and EventNotifier interface.
1173 */
1174VirtQueue *virtio_add_queue_aio(VirtIODevice *vdev, int queue_size,
1175 VirtIOHandleOutput handle_output)
1176{
1177 return virtio_add_queue_internal(vdev, queue_size, handle_output, true);
1178}
1179
1180/* Add a normal virt queue (on the contrary to the AIO version above. */
1181VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
1182 VirtIOHandleOutput handle_output)
1183{
1184 return virtio_add_queue_internal(vdev, queue_size, handle_output, false);
1185}
1186
f23fd811
JW
1187void virtio_del_queue(VirtIODevice *vdev, int n)
1188{
87b3bd1c 1189 if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
f23fd811
JW
1190 abort();
1191 }
1192
1193 vdev->vq[n].vring.num = 0;
46c5d082 1194 vdev->vq[n].vring.num_default = 0;
f23fd811
JW
1195}
1196
1cbdabe2
MT
1197void virtio_irq(VirtQueue *vq)
1198{
64979a4d 1199 trace_virtio_irq(vq);
1cbdabe2
MT
1200 vq->vdev->isr |= 0x01;
1201 virtio_notify_vector(vq->vdev, vq->vector);
1202}
1203
adb3feda 1204bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
bcbabae8
MT
1205{
1206 uint16_t old, new;
1207 bool v;
a281ebc1
MT
1208 /* We need to expose used array entries before checking used event. */
1209 smp_mb();
97b83deb 1210 /* Always notify when queue is empty (when feature acknowledge) */
95129d6f 1211 if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
be1fea9b 1212 !vq->inuse && virtio_queue_empty(vq)) {
bcbabae8
MT
1213 return true;
1214 }
1215
95129d6f 1216 if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
bcbabae8
MT
1217 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
1218 }
1219
1220 v = vq->signalled_used_valid;
1221 vq->signalled_used_valid = true;
1222 old = vq->signalled_used;
b796fcd1 1223 new = vq->signalled_used = vq->used_idx;
e9600c6c 1224 return !v || vring_need_event(vring_get_used_event(vq), new, old);
bcbabae8
MT
1225}
1226
1227void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
1228{
adb3feda 1229 if (!virtio_should_notify(vdev, vq)) {
967f97fa 1230 return;
bcbabae8 1231 }
967f97fa 1232
64979a4d 1233 trace_virtio_notify(vdev, vq);
967f97fa 1234 vdev->isr |= 0x01;
7055e687 1235 virtio_notify_vector(vdev, vq->vector);
967f97fa
AL
1236}
1237
1238void virtio_notify_config(VirtIODevice *vdev)
1239{
7625162c
AL
1240 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
1241 return;
1242
967f97fa 1243 vdev->isr |= 0x03;
b8f05908 1244 vdev->generation++;
7055e687 1245 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa
AL
1246}
1247
616a6552
GK
1248static bool virtio_device_endian_needed(void *opaque)
1249{
1250 VirtIODevice *vdev = opaque;
1251
1252 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
95129d6f 1253 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3c185597
CH
1254 return vdev->device_endian != virtio_default_endian();
1255 }
1256 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1257 return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
616a6552
GK
1258}
1259
019a3edb
GH
1260static bool virtio_64bit_features_needed(void *opaque)
1261{
1262 VirtIODevice *vdev = opaque;
1263
1264 return (vdev->host_features >> 32) != 0;
1265}
1266
74aae7b2
JW
1267static bool virtio_virtqueue_needed(void *opaque)
1268{
1269 VirtIODevice *vdev = opaque;
1270
1271 return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
1272}
1273
46c5d082
CH
1274static bool virtio_ringsize_needed(void *opaque)
1275{
1276 VirtIODevice *vdev = opaque;
1277 int i;
1278
1279 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1280 if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
1281 return true;
1282 }
1283 }
1284 return false;
1285}
1286
a6df8adf
JW
1287static bool virtio_extra_state_needed(void *opaque)
1288{
1289 VirtIODevice *vdev = opaque;
1290 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1291 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1292
1293 return k->has_extra_state &&
1294 k->has_extra_state(qbus->parent);
1295}
1296
50e5ae4d 1297static const VMStateDescription vmstate_virtqueue = {
74aae7b2 1298 .name = "virtqueue_state",
50e5ae4d
DDAG
1299 .version_id = 1,
1300 .minimum_version_id = 1,
1301 .fields = (VMStateField[]) {
1302 VMSTATE_UINT64(vring.avail, struct VirtQueue),
1303 VMSTATE_UINT64(vring.used, struct VirtQueue),
1304 VMSTATE_END_OF_LIST()
1305 }
74aae7b2
JW
1306};
1307
1308static const VMStateDescription vmstate_virtio_virtqueues = {
1309 .name = "virtio/virtqueues",
1310 .version_id = 1,
1311 .minimum_version_id = 1,
1312 .needed = &virtio_virtqueue_needed,
1313 .fields = (VMStateField[]) {
3e996cc5
DDAG
1314 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1315 VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
74aae7b2
JW
1316 VMSTATE_END_OF_LIST()
1317 }
1318};
1319
50e5ae4d 1320static const VMStateDescription vmstate_ringsize = {
46c5d082 1321 .name = "ringsize_state",
50e5ae4d
DDAG
1322 .version_id = 1,
1323 .minimum_version_id = 1,
1324 .fields = (VMStateField[]) {
1325 VMSTATE_UINT32(vring.num_default, struct VirtQueue),
1326 VMSTATE_END_OF_LIST()
1327 }
46c5d082
CH
1328};
1329
1330static const VMStateDescription vmstate_virtio_ringsize = {
1331 .name = "virtio/ringsize",
1332 .version_id = 1,
1333 .minimum_version_id = 1,
1334 .needed = &virtio_ringsize_needed,
1335 .fields = (VMStateField[]) {
3e996cc5
DDAG
1336 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1337 VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
46c5d082
CH
1338 VMSTATE_END_OF_LIST()
1339 }
1340};
1341
a6df8adf
JW
1342static int get_extra_state(QEMUFile *f, void *pv, size_t size)
1343{
1344 VirtIODevice *vdev = pv;
1345 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1346 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1347
1348 if (!k->load_extra_state) {
1349 return -1;
1350 } else {
1351 return k->load_extra_state(qbus->parent, f);
1352 }
1353}
1354
1355static void put_extra_state(QEMUFile *f, void *pv, size_t size)
1356{
1357 VirtIODevice *vdev = pv;
1358 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1359 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1360
1361 k->save_extra_state(qbus->parent, f);
1362}
1363
1364static const VMStateInfo vmstate_info_extra_state = {
1365 .name = "virtqueue_extra_state",
1366 .get = get_extra_state,
1367 .put = put_extra_state,
1368};
1369
1370static const VMStateDescription vmstate_virtio_extra_state = {
1371 .name = "virtio/extra_state",
1372 .version_id = 1,
1373 .minimum_version_id = 1,
1374 .needed = &virtio_extra_state_needed,
1375 .fields = (VMStateField[]) {
1376 {
1377 .name = "extra_state",
1378 .version_id = 0,
1379 .field_exists = NULL,
1380 .size = 0,
1381 .info = &vmstate_info_extra_state,
1382 .flags = VMS_SINGLE,
1383 .offset = 0,
1384 },
1385 VMSTATE_END_OF_LIST()
1386 }
1387};
1388
616a6552
GK
1389static const VMStateDescription vmstate_virtio_device_endian = {
1390 .name = "virtio/device_endian",
1391 .version_id = 1,
1392 .minimum_version_id = 1,
5cd8cada 1393 .needed = &virtio_device_endian_needed,
616a6552
GK
1394 .fields = (VMStateField[]) {
1395 VMSTATE_UINT8(device_endian, VirtIODevice),
1396 VMSTATE_END_OF_LIST()
1397 }
1398};
1399
019a3edb
GH
1400static const VMStateDescription vmstate_virtio_64bit_features = {
1401 .name = "virtio/64bit_features",
1402 .version_id = 1,
1403 .minimum_version_id = 1,
5cd8cada 1404 .needed = &virtio_64bit_features_needed,
019a3edb
GH
1405 .fields = (VMStateField[]) {
1406 VMSTATE_UINT64(guest_features, VirtIODevice),
1407 VMSTATE_END_OF_LIST()
1408 }
1409};
1410
6b321a3d
GK
1411static const VMStateDescription vmstate_virtio = {
1412 .name = "virtio",
1413 .version_id = 1,
1414 .minimum_version_id = 1,
1415 .minimum_version_id_old = 1,
1416 .fields = (VMStateField[]) {
1417 VMSTATE_END_OF_LIST()
616a6552 1418 },
5cd8cada
JQ
1419 .subsections = (const VMStateDescription*[]) {
1420 &vmstate_virtio_device_endian,
1421 &vmstate_virtio_64bit_features,
74aae7b2 1422 &vmstate_virtio_virtqueues,
46c5d082 1423 &vmstate_virtio_ringsize,
a6df8adf 1424 &vmstate_virtio_extra_state,
5cd8cada 1425 NULL
6b321a3d
GK
1426 }
1427};
1428
967f97fa
AL
1429void virtio_save(VirtIODevice *vdev, QEMUFile *f)
1430{
1c819449
FK
1431 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1432 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 1433 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
019a3edb 1434 uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
967f97fa
AL
1435 int i;
1436
1c819449
FK
1437 if (k->save_config) {
1438 k->save_config(qbus->parent, f);
1439 }
967f97fa 1440
967f97fa
AL
1441 qemu_put_8s(f, &vdev->status);
1442 qemu_put_8s(f, &vdev->isr);
1443 qemu_put_be16s(f, &vdev->queue_sel);
019a3edb 1444 qemu_put_be32s(f, &guest_features_lo);
967f97fa
AL
1445 qemu_put_be32(f, vdev->config_len);
1446 qemu_put_buffer(f, vdev->config, vdev->config_len);
1447
87b3bd1c 1448 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
1449 if (vdev->vq[i].vring.num == 0)
1450 break;
1451 }
1452
1453 qemu_put_be32(f, i);
1454
87b3bd1c 1455 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
1456 if (vdev->vq[i].vring.num == 0)
1457 break;
1458
1459 qemu_put_be32(f, vdev->vq[i].vring.num);
6ce69d1c
PM
1460 if (k->has_variable_vring_alignment) {
1461 qemu_put_be32(f, vdev->vq[i].vring.align);
1462 }
ab223c95
CH
1463 /* XXX virtio-1 devices */
1464 qemu_put_be64(f, vdev->vq[i].vring.desc);
967f97fa 1465 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1c819449
FK
1466 if (k->save_queue) {
1467 k->save_queue(qbus->parent, i, f);
1468 }
967f97fa 1469 }
1b5fc0de
GK
1470
1471 if (vdc->save != NULL) {
1472 vdc->save(vdev, f);
1473 }
6b321a3d
GK
1474
1475 /* Subsections */
8118f095 1476 vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
967f97fa
AL
1477}
1478
5943124c
DDAG
1479/* A wrapper for use as a VMState .put function */
1480void virtio_vmstate_save(QEMUFile *f, void *opaque, size_t size)
1481{
1482 virtio_save(VIRTIO_DEVICE(opaque), f);
1483}
1484
6c0196d7 1485static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
ad0c9332 1486{
181103cd 1487 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
6b8f1020 1488 bool bad = (val & ~(vdev->host_features)) != 0;
ad0c9332 1489
6b8f1020 1490 val &= vdev->host_features;
181103cd
FK
1491 if (k->set_features) {
1492 k->set_features(vdev, val);
ad0c9332
PB
1493 }
1494 vdev->guest_features = val;
1495 return bad ? -1 : 0;
1496}
1497
6c0196d7
CH
1498int virtio_set_features(VirtIODevice *vdev, uint64_t val)
1499{
1500 /*
1501 * The driver must not attempt to set features after feature negotiation
1502 * has finished.
1503 */
1504 if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
1505 return -EINVAL;
1506 }
1507 return virtio_set_features_nocheck(vdev, val);
1508}
1509
1b5fc0de 1510int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
967f97fa 1511{
cc459952 1512 int i, ret;
a890a2f9 1513 int32_t config_len;
cc459952 1514 uint32_t num;
6d74ca5a 1515 uint32_t features;
1c819449
FK
1516 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1517 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 1518 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa 1519
616a6552
GK
1520 /*
1521 * We poison the endianness to ensure it does not get used before
1522 * subsections have been loaded.
1523 */
1524 vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
1525
1c819449
FK
1526 if (k->load_config) {
1527 ret = k->load_config(qbus->parent, f);
ff24bd58
MT
1528 if (ret)
1529 return ret;
1530 }
967f97fa 1531
967f97fa
AL
1532 qemu_get_8s(f, &vdev->status);
1533 qemu_get_8s(f, &vdev->isr);
1534 qemu_get_be16s(f, &vdev->queue_sel);
87b3bd1c 1535 if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
4b53c2c7
MR
1536 return -1;
1537 }
6d74ca5a 1538 qemu_get_be32s(f, &features);
ad0c9332 1539
62cee1a2
MT
1540 /*
1541 * Temporarily set guest_features low bits - needed by
1542 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
1543 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
1544 *
1545 * Note: devices should always test host features in future - don't create
1546 * new dependencies like this.
1547 */
1548 vdev->guest_features = features;
1549
a890a2f9 1550 config_len = qemu_get_be32(f);
2f5732e9
DDAG
1551
1552 /*
1553 * There are cases where the incoming config can be bigger or smaller
1554 * than what we have; so load what we have space for, and skip
1555 * any excess that's in the stream.
1556 */
1557 qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
1558
1559 while (config_len > vdev->config_len) {
1560 qemu_get_byte(f);
1561 config_len--;
a890a2f9 1562 }
967f97fa
AL
1563
1564 num = qemu_get_be32(f);
1565
87b3bd1c 1566 if (num > VIRTIO_QUEUE_MAX) {
8a1be662 1567 error_report("Invalid number of virtqueues: 0x%x", num);
cc459952
MT
1568 return -1;
1569 }
1570
967f97fa
AL
1571 for (i = 0; i < num; i++) {
1572 vdev->vq[i].vring.num = qemu_get_be32(f);
6ce69d1c
PM
1573 if (k->has_variable_vring_alignment) {
1574 vdev->vq[i].vring.align = qemu_get_be32(f);
1575 }
ab223c95 1576 vdev->vq[i].vring.desc = qemu_get_be64(f);
967f97fa 1577 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
bcbabae8
MT
1578 vdev->vq[i].signalled_used_valid = false;
1579 vdev->vq[i].notification = true;
967f97fa 1580
ab223c95
CH
1581 if (vdev->vq[i].vring.desc) {
1582 /* XXX virtio-1 devices */
1583 virtio_queue_update_rings(vdev, i);
1abeb5a6
MT
1584 } else if (vdev->vq[i].last_avail_idx) {
1585 error_report("VQ %d address 0x0 "
6daf194d 1586 "inconsistent with Host index 0x%x",
1abeb5a6
MT
1587 i, vdev->vq[i].last_avail_idx);
1588 return -1;
258dc7c9 1589 }
1c819449
FK
1590 if (k->load_queue) {
1591 ret = k->load_queue(qbus->parent, i, f);
ff24bd58
MT
1592 if (ret)
1593 return ret;
7055e687 1594 }
967f97fa
AL
1595 }
1596
7055e687 1597 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
1b5fc0de
GK
1598
1599 if (vdc->load != NULL) {
6b321a3d
GK
1600 ret = vdc->load(vdev, f, version_id);
1601 if (ret) {
1602 return ret;
1603 }
1b5fc0de
GK
1604 }
1605
616a6552
GK
1606 /* Subsections */
1607 ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
1608 if (ret) {
1609 return ret;
1610 }
1611
1612 if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
1613 vdev->device_endian = virtio_default_endian();
1614 }
1615
019a3edb
GH
1616 if (virtio_64bit_features_needed(vdev)) {
1617 /*
1618 * Subsection load filled vdev->guest_features. Run them
1619 * through virtio_set_features to sanity-check them against
1620 * host_features.
1621 */
1622 uint64_t features64 = vdev->guest_features;
6c0196d7 1623 if (virtio_set_features_nocheck(vdev, features64) < 0) {
019a3edb
GH
1624 error_report("Features 0x%" PRIx64 " unsupported. "
1625 "Allowed features: 0x%" PRIx64,
1626 features64, vdev->host_features);
1627 return -1;
1628 }
1629 } else {
6c0196d7 1630 if (virtio_set_features_nocheck(vdev, features) < 0) {
019a3edb
GH
1631 error_report("Features 0x%x unsupported. "
1632 "Allowed features: 0x%" PRIx64,
1633 features, vdev->host_features);
1634 return -1;
1635 }
1636 }
1637
616a6552 1638 for (i = 0; i < num; i++) {
ab223c95 1639 if (vdev->vq[i].vring.desc) {
616a6552
GK
1640 uint16_t nheads;
1641 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
1642 /* Check it isn't doing strange things with descriptor numbers. */
1643 if (nheads > vdev->vq[i].vring.num) {
1644 error_report("VQ %d size 0x%x Guest index 0x%x "
1645 "inconsistent with Host index 0x%x: delta 0x%x",
1646 i, vdev->vq[i].vring.num,
1647 vring_avail_idx(&vdev->vq[i]),
1648 vdev->vq[i].last_avail_idx, nheads);
1649 return -1;
1650 }
b796fcd1 1651 vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
be1fea9b 1652 vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
bccdef6b
SH
1653
1654 /*
1655 * Some devices migrate VirtQueueElements that have been popped
1656 * from the avail ring but not yet returned to the used ring.
1657 */
1658 vdev->vq[i].inuse = vdev->vq[i].last_avail_idx -
1659 vdev->vq[i].used_idx;
1660 if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
1661 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
1662 "used_idx 0x%x",
1663 i, vdev->vq[i].vring.num,
1664 vdev->vq[i].last_avail_idx,
1665 vdev->vq[i].used_idx);
1666 return -1;
1667 }
616a6552
GK
1668 }
1669 }
1670
1671 return 0;
967f97fa
AL
1672}
1673
6a1a8cc7 1674void virtio_cleanup(VirtIODevice *vdev)
b946a153 1675{
85cf2a8d 1676 qemu_del_vm_change_state_handler(vdev->vmstate);
6f79e06b 1677 g_free(vdev->config);
7267c094 1678 g_free(vdev->vq);
e0d686bf 1679 g_free(vdev->vector_queues);
8e05db92
FK
1680}
1681
1dfb4dd9 1682static void virtio_vmstate_change(void *opaque, int running, RunState state)
85cf2a8d
MT
1683{
1684 VirtIODevice *vdev = opaque;
1c819449
FK
1685 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1686 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
85cf2a8d 1687 bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
9e8e8c48 1688 vdev->vm_running = running;
85cf2a8d
MT
1689
1690 if (backend_run) {
1691 virtio_set_status(vdev, vdev->status);
1692 }
1693
1c819449
FK
1694 if (k->vmstate_change) {
1695 k->vmstate_change(qbus->parent, backend_run);
85cf2a8d
MT
1696 }
1697
1698 if (!backend_run) {
1699 virtio_set_status(vdev, vdev->status);
1700 }
1701}
1702
c8075caf
GA
1703void virtio_instance_init_common(Object *proxy_obj, void *data,
1704 size_t vdev_size, const char *vdev_name)
1705{
1706 DeviceState *vdev = data;
1707
1708 object_initialize(vdev, vdev_size, vdev_name);
1709 object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL);
1710 object_unref(OBJECT(vdev));
1711 qdev_alias_all_properties(vdev, proxy_obj);
1712}
1713
8e05db92
FK
1714void virtio_init(VirtIODevice *vdev, const char *name,
1715 uint16_t device_id, size_t config_size)
967f97fa 1716{
e0d686bf
JW
1717 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1718 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
b8193adb 1719 int i;
e0d686bf
JW
1720 int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
1721
1722 if (nvectors) {
1723 vdev->vector_queues =
1724 g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
1725 }
1726
53c25cea 1727 vdev->device_id = device_id;
967f97fa
AL
1728 vdev->status = 0;
1729 vdev->isr = 0;
1730 vdev->queue_sel = 0;
7055e687 1731 vdev->config_vector = VIRTIO_NO_VECTOR;
87b3bd1c 1732 vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
1354869c 1733 vdev->vm_running = runstate_is_running();
87b3bd1c 1734 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
b8193adb 1735 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
1cbdabe2 1736 vdev->vq[i].vdev = vdev;
e78a2b42 1737 vdev->vq[i].queue_index = i;
1cbdabe2 1738 }
967f97fa 1739
967f97fa
AL
1740 vdev->name = name;
1741 vdev->config_len = config_size;
8e05db92 1742 if (vdev->config_len) {
7267c094 1743 vdev->config = g_malloc0(config_size);
8e05db92 1744 } else {
967f97fa 1745 vdev->config = NULL;
8e05db92
FK
1746 }
1747 vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
1748 vdev);
616a6552 1749 vdev->device_endian = virtio_default_endian();
5669655a 1750 vdev->use_guest_notifier_mask = true;
8e05db92 1751}
967f97fa 1752
a8170e5e 1753hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1754{
1755 return vdev->vq[n].vring.desc;
1756}
1757
a8170e5e 1758hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1759{
1760 return vdev->vq[n].vring.avail;
1761}
1762
a8170e5e 1763hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1764{
1765 return vdev->vq[n].vring.used;
1766}
1767
a8170e5e 1768hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
1769{
1770 return vdev->vq[n].vring.desc;
1771}
1772
a8170e5e 1773hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1774{
1775 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
1776}
1777
a8170e5e 1778hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1779{
1780 return offsetof(VRingAvail, ring) +
50764fc8 1781 sizeof(uint16_t) * vdev->vq[n].vring.num;
1cbdabe2
MT
1782}
1783
a8170e5e 1784hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1785{
1786 return offsetof(VRingUsed, ring) +
1787 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
1788}
1789
a8170e5e 1790hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
1791{
1792 return vdev->vq[n].vring.used - vdev->vq[n].vring.desc +
1793 virtio_queue_get_used_size(vdev, n);
1794}
1795
1796uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
1797{
1798 return vdev->vq[n].last_avail_idx;
1799}
1800
1801void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
1802{
1803 vdev->vq[n].last_avail_idx = idx;
be1fea9b 1804 vdev->vq[n].shadow_avail_idx = idx;
1cbdabe2
MT
1805}
1806
6793dfd1
SH
1807void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
1808{
1809 vdev->vq[n].signalled_used_valid = false;
1810}
1811
1cbdabe2
MT
1812VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
1813{
1814 return vdev->vq + n;
1815}
1816
e78a2b42
JW
1817uint16_t virtio_get_queue_index(VirtQueue *vq)
1818{
1819 return vq->queue_index;
1820}
1821
15b2bd18
PB
1822static void virtio_queue_guest_notifier_read(EventNotifier *n)
1823{
1824 VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
1825 if (event_notifier_test_and_clear(n)) {
1826 virtio_irq(vq);
1827 }
1828}
1829
1830void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
1831 bool with_irqfd)
1832{
1833 if (assign && !with_irqfd) {
54e18d35 1834 event_notifier_set_handler(&vq->guest_notifier, false,
15b2bd18
PB
1835 virtio_queue_guest_notifier_read);
1836 } else {
54e18d35 1837 event_notifier_set_handler(&vq->guest_notifier, false, NULL);
15b2bd18
PB
1838 }
1839 if (!assign) {
1840 /* Test and clear notifier before closing it,
1841 * in case poll callback didn't have time to run. */
1842 virtio_queue_guest_notifier_read(&vq->guest_notifier);
1843 }
1844}
1845
1cbdabe2
MT
1846EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
1847{
1848 return &vq->guest_notifier;
1849}
b1f416aa 1850
344dc16f 1851static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
b1f416aa
PB
1852{
1853 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
1854 if (event_notifier_test_and_clear(n)) {
344dc16f 1855 virtio_queue_notify_aio_vq(vq);
b1f416aa
PB
1856 }
1857}
1858
a1afb606 1859void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
bf1780b0 1860 VirtIOHandleOutput handle_output)
a1afb606 1861{
a378b49a
PB
1862 if (handle_output) {
1863 vq->handle_aio_output = handle_output;
a1afb606 1864 aio_set_event_notifier(ctx, &vq->host_notifier, true,
344dc16f 1865 virtio_queue_host_notifier_aio_read);
a1afb606
PB
1866 } else {
1867 aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL);
a1afb606
PB
1868 /* Test and clear notifier before after disabling event,
1869 * in case poll callback didn't have time to run. */
344dc16f 1870 virtio_queue_host_notifier_aio_read(&vq->host_notifier);
a378b49a 1871 vq->handle_aio_output = NULL;
344dc16f
MT
1872 }
1873}
1874
1875static void virtio_queue_host_notifier_read(EventNotifier *n)
1876{
1877 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
1878 if (event_notifier_test_and_clear(n)) {
1879 virtio_queue_notify_vq(vq);
a1afb606
PB
1880 }
1881}
1882
26b9b5fe
PB
1883void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
1884 bool set_handler)
b1f416aa 1885{
872dd82c 1886 AioContext *ctx = qemu_get_aio_context();
26b9b5fe 1887 if (assign && set_handler) {
872dd82c
FZ
1888 if (vq->use_aio) {
1889 aio_set_event_notifier(ctx, &vq->host_notifier, true,
b1f416aa 1890 virtio_queue_host_notifier_read);
872dd82c
FZ
1891 } else {
1892 event_notifier_set_handler(&vq->host_notifier, true,
1893 virtio_queue_host_notifier_read);
1894 }
b1f416aa 1895 } else {
872dd82c
FZ
1896 if (vq->use_aio) {
1897 aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL);
1898 } else {
1899 event_notifier_set_handler(&vq->host_notifier, true, NULL);
1900 }
26b9b5fe
PB
1901 }
1902 if (!assign) {
b1f416aa
PB
1903 /* Test and clear notifier before after disabling event,
1904 * in case poll callback didn't have time to run. */
1905 virtio_queue_host_notifier_read(&vq->host_notifier);
1906 }
1907}
1908
1cbdabe2
MT
1909EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
1910{
1911 return &vq->host_notifier;
1912}
8e05db92 1913
1034e9cf
FK
1914void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
1915{
9e288406 1916 g_free(vdev->bus_name);
80e0090a 1917 vdev->bus_name = g_strdup(bus_name);
1034e9cf
FK
1918}
1919
1d244b42
AF
1920static void virtio_device_realize(DeviceState *dev, Error **errp)
1921{
1922 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1923 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1924 Error *err = NULL;
1925
1d244b42
AF
1926 if (vdc->realize != NULL) {
1927 vdc->realize(dev, &err);
1928 if (err != NULL) {
1929 error_propagate(errp, err);
1930 return;
1931 }
8e05db92 1932 }
e8398045
JW
1933
1934 virtio_bus_device_plugged(vdev, &err);
1935 if (err != NULL) {
1936 error_propagate(errp, err);
1937 return;
1938 }
8e05db92
FK
1939}
1940
1d244b42 1941static void virtio_device_unrealize(DeviceState *dev, Error **errp)
1034e9cf 1942{
1d244b42 1943 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
306ec6c3
AF
1944 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1945 Error *err = NULL;
1d244b42 1946
83d07047
PB
1947 virtio_bus_device_unplugged(vdev);
1948
306ec6c3
AF
1949 if (vdc->unrealize != NULL) {
1950 vdc->unrealize(dev, &err);
1951 if (err != NULL) {
1952 error_propagate(errp, err);
1953 return;
1954 }
5e96f5d2 1955 }
1d244b42 1956
9e288406
MA
1957 g_free(vdev->bus_name);
1958 vdev->bus_name = NULL;
1034e9cf
FK
1959}
1960
6b8f1020
CH
1961static Property virtio_properties[] = {
1962 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
1963 DEFINE_PROP_END_OF_LIST(),
1964};
1965
8e05db92
FK
1966static void virtio_device_class_init(ObjectClass *klass, void *data)
1967{
1968 /* Set the default value here. */
1969 DeviceClass *dc = DEVICE_CLASS(klass);
1d244b42
AF
1970
1971 dc->realize = virtio_device_realize;
1972 dc->unrealize = virtio_device_unrealize;
8e05db92 1973 dc->bus_type = TYPE_VIRTIO_BUS;
6b8f1020 1974 dc->props = virtio_properties;
8e05db92
FK
1975}
1976
1977static const TypeInfo virtio_device_info = {
1978 .name = TYPE_VIRTIO_DEVICE,
1979 .parent = TYPE_DEVICE,
1980 .instance_size = sizeof(VirtIODevice),
1981 .class_init = virtio_device_class_init,
1982 .abstract = true,
1983 .class_size = sizeof(VirtioDeviceClass),
1984};
1985
1986static void virtio_register_types(void)
1987{
1988 type_register_static(&virtio_device_info);
1989}
1990
1991type_init(virtio_register_types)