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