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