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