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