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