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