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