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