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