~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to hw/virtio/virtio.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "hw/virtio/virtio-bus.h"
24
24
#include "migration/migration.h"
25
25
#include "hw/virtio/virtio-access.h"
 
26
#include "sysemu/dma.h"
26
27
 
27
28
/*
28
29
 * The alignment to use between consumer and producer parts of vring.
59
60
    VRingUsedElem ring[0];
60
61
} VRingUsed;
61
62
 
 
63
typedef struct VRingMemoryRegionCaches {
 
64
    struct rcu_head rcu;
 
65
    MemoryRegionCache desc;
 
66
    MemoryRegionCache avail;
 
67
    MemoryRegionCache used;
 
68
} VRingMemoryRegionCaches;
 
69
 
62
70
typedef struct VRing
63
71
{
64
72
    unsigned int num;
67
75
    hwaddr desc;
68
76
    hwaddr avail;
69
77
    hwaddr used;
 
78
    VRingMemoryRegionCaches *caches;
70
79
} VRing;
71
80
 
72
81
struct VirtQueue
96
105
 
97
106
    uint16_t vector;
98
107
    VirtIOHandleOutput handle_output;
99
 
    VirtIOHandleOutput handle_aio_output;
 
108
    VirtIOHandleAIOOutput handle_aio_output;
100
109
    VirtIODevice *vdev;
101
110
    EventNotifier guest_notifier;
102
111
    EventNotifier host_notifier;
103
112
    QLIST_ENTRY(VirtQueue) node;
104
113
};
105
114
 
 
115
static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
 
116
{
 
117
    if (!caches) {
 
118
        return;
 
119
    }
 
120
 
 
121
    address_space_cache_destroy(&caches->desc);
 
122
    address_space_cache_destroy(&caches->avail);
 
123
    address_space_cache_destroy(&caches->used);
 
124
    g_free(caches);
 
125
}
 
126
 
 
127
static void virtio_init_region_cache(VirtIODevice *vdev, int n)
 
128
{
 
129
    VirtQueue *vq = &vdev->vq[n];
 
130
    VRingMemoryRegionCaches *old = vq->vring.caches;
 
131
    VRingMemoryRegionCaches *new;
 
132
    hwaddr addr, size;
 
133
    int event_size;
 
134
    int64_t len;
 
135
 
 
136
    event_size = virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
 
137
 
 
138
    addr = vq->vring.desc;
 
139
    if (!addr) {
 
140
        return;
 
141
    }
 
142
    new = g_new0(VRingMemoryRegionCaches, 1);
 
143
    size = virtio_queue_get_desc_size(vdev, n);
 
144
    len = address_space_cache_init(&new->desc, vdev->dma_as,
 
145
                                   addr, size, false);
 
146
    if (len < size) {
 
147
        virtio_error(vdev, "Cannot map desc");
 
148
        goto err_desc;
 
149
    }
 
150
 
 
151
    size = virtio_queue_get_used_size(vdev, n) + event_size;
 
152
    len = address_space_cache_init(&new->used, vdev->dma_as,
 
153
                                   vq->vring.used, size, true);
 
154
    if (len < size) {
 
155
        virtio_error(vdev, "Cannot map used");
 
156
        goto err_used;
 
157
    }
 
158
 
 
159
    size = virtio_queue_get_avail_size(vdev, n) + event_size;
 
160
    len = address_space_cache_init(&new->avail, vdev->dma_as,
 
161
                                   vq->vring.avail, size, false);
 
162
    if (len < size) {
 
163
        virtio_error(vdev, "Cannot map avail");
 
164
        goto err_avail;
 
165
    }
 
166
 
 
167
    atomic_rcu_set(&vq->vring.caches, new);
 
168
    if (old) {
 
169
        call_rcu(old, virtio_free_region_cache, rcu);
 
170
    }
 
171
    return;
 
172
 
 
173
err_avail:
 
174
    address_space_cache_destroy(&new->used);
 
175
err_used:
 
176
    address_space_cache_destroy(&new->desc);
 
177
err_desc:
 
178
    g_free(new);
 
179
}
 
180
 
106
181
/* virt queue functions */
107
182
void virtio_queue_update_rings(VirtIODevice *vdev, int n)
108
183
{
116
191
    vring->used = vring_align(vring->avail +
117
192
                              offsetof(VRingAvail, ring[vring->num]),
118
193
                              vring->align);
 
194
    virtio_init_region_cache(vdev, n);
119
195
}
120
196
 
 
197
/* Called within rcu_read_lock().  */
121
198
static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
122
 
                            hwaddr desc_pa, int i)
 
199
                            MemoryRegionCache *cache, int i)
123
200
{
124
 
    address_space_read(&address_space_memory, desc_pa + i * sizeof(VRingDesc),
125
 
                       MEMTXATTRS_UNSPECIFIED, (void *)desc, sizeof(VRingDesc));
 
201
    address_space_read_cached(cache, i * sizeof(VRingDesc),
 
202
                              desc, sizeof(VRingDesc));
126
203
    virtio_tswap64s(vdev, &desc->addr);
127
204
    virtio_tswap32s(vdev, &desc->len);
128
205
    virtio_tswap16s(vdev, &desc->flags);
129
206
    virtio_tswap16s(vdev, &desc->next);
130
207
}
131
208
 
 
209
static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *vq)
 
210
{
 
211
    VRingMemoryRegionCaches *caches = atomic_rcu_read(&vq->vring.caches);
 
212
    assert(caches != NULL);
 
213
    return caches;
 
214
}
 
215
/* Called within rcu_read_lock().  */
132
216
static inline uint16_t vring_avail_flags(VirtQueue *vq)
133
217
{
134
 
    hwaddr pa;
135
 
    pa = vq->vring.avail + offsetof(VRingAvail, flags);
136
 
    return virtio_lduw_phys(vq->vdev, pa);
 
218
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
 
219
    hwaddr pa = offsetof(VRingAvail, flags);
 
220
    return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
137
221
}
138
222
 
 
223
/* Called within rcu_read_lock().  */
139
224
static inline uint16_t vring_avail_idx(VirtQueue *vq)
140
225
{
141
 
    hwaddr pa;
142
 
    pa = vq->vring.avail + offsetof(VRingAvail, idx);
143
 
    vq->shadow_avail_idx = virtio_lduw_phys(vq->vdev, pa);
 
226
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
 
227
    hwaddr pa = offsetof(VRingAvail, idx);
 
228
    vq->shadow_avail_idx = virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
144
229
    return vq->shadow_avail_idx;
145
230
}
146
231
 
 
232
/* Called within rcu_read_lock().  */
147
233
static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
148
234
{
149
 
    hwaddr pa;
150
 
    pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
151
 
    return virtio_lduw_phys(vq->vdev, pa);
 
235
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
 
236
    hwaddr pa = offsetof(VRingAvail, ring[i]);
 
237
    return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
152
238
}
153
239
 
 
240
/* Called within rcu_read_lock().  */
154
241
static inline uint16_t vring_get_used_event(VirtQueue *vq)
155
242
{
156
243
    return vring_avail_ring(vq, vq->vring.num);
157
244
}
158
245
 
 
246
/* Called within rcu_read_lock().  */
159
247
static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
160
248
                                    int i)
161
249
{
162
 
    hwaddr pa;
 
250
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
 
251
    hwaddr pa = offsetof(VRingUsed, ring[i]);
163
252
    virtio_tswap32s(vq->vdev, &uelem->id);
164
253
    virtio_tswap32s(vq->vdev, &uelem->len);
165
 
    pa = vq->vring.used + offsetof(VRingUsed, ring[i]);
166
 
    address_space_write(&address_space_memory, pa, MEMTXATTRS_UNSPECIFIED,
167
 
                       (void *)uelem, sizeof(VRingUsedElem));
 
254
    address_space_write_cached(&caches->used, pa, uelem, sizeof(VRingUsedElem));
 
255
    address_space_cache_invalidate(&caches->used, pa, sizeof(VRingUsedElem));
168
256
}
169
257
 
 
258
/* Called within rcu_read_lock().  */
170
259
static uint16_t vring_used_idx(VirtQueue *vq)
171
260
{
172
 
    hwaddr pa;
173
 
    pa = vq->vring.used + offsetof(VRingUsed, idx);
174
 
    return virtio_lduw_phys(vq->vdev, pa);
 
261
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
 
262
    hwaddr pa = offsetof(VRingUsed, idx);
 
263
    return virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
175
264
}
176
265
 
 
266
/* Called within rcu_read_lock().  */
177
267
static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
178
268
{
179
 
    hwaddr pa;
180
 
    pa = vq->vring.used + offsetof(VRingUsed, idx);
181
 
    virtio_stw_phys(vq->vdev, pa, val);
 
269
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
 
270
    hwaddr pa = offsetof(VRingUsed, idx);
 
271
    virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
 
272
    address_space_cache_invalidate(&caches->used, pa, sizeof(val));
182
273
    vq->used_idx = val;
183
274
}
184
275
 
 
276
/* Called within rcu_read_lock().  */
185
277
static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
186
278
{
 
279
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
187
280
    VirtIODevice *vdev = vq->vdev;
188
 
    hwaddr pa;
189
 
    pa = vq->vring.used + offsetof(VRingUsed, flags);
190
 
    virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) | mask);
 
281
    hwaddr pa = offsetof(VRingUsed, flags);
 
282
    uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
 
283
 
 
284
    virtio_stw_phys_cached(vdev, &caches->used, pa, flags | mask);
 
285
    address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
191
286
}
192
287
 
 
288
/* Called within rcu_read_lock().  */
193
289
static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
194
290
{
 
291
    VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
195
292
    VirtIODevice *vdev = vq->vdev;
196
 
    hwaddr pa;
197
 
    pa = vq->vring.used + offsetof(VRingUsed, flags);
198
 
    virtio_stw_phys(vdev, pa, virtio_lduw_phys(vdev, pa) & ~mask);
 
293
    hwaddr pa = offsetof(VRingUsed, flags);
 
294
    uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
 
295
 
 
296
    virtio_stw_phys_cached(vdev, &caches->used, pa, flags & ~mask);
 
297
    address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
199
298
}
200
299
 
 
300
/* Called within rcu_read_lock().  */
201
301
static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
202
302
{
 
303
    VRingMemoryRegionCaches *caches;
203
304
    hwaddr pa;
204
305
    if (!vq->notification) {
205
306
        return;
206
307
    }
207
 
    pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
208
 
    virtio_stw_phys(vq->vdev, pa, val);
 
308
 
 
309
    caches = vring_get_region_caches(vq);
 
310
    pa = offsetof(VRingUsed, ring[vq->vring.num]);
 
311
    virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
 
312
    address_space_cache_invalidate(&caches->used, pa, sizeof(val));
209
313
}
210
314
 
211
315
void virtio_queue_set_notification(VirtQueue *vq, int enable)
212
316
{
213
317
    vq->notification = enable;
 
318
 
 
319
    if (!vq->vring.desc) {
 
320
        return;
 
321
    }
 
322
 
 
323
    rcu_read_lock();
214
324
    if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
215
325
        vring_set_avail_event(vq, vring_avail_idx(vq));
216
326
    } else if (enable) {
222
332
        /* Expose avail event/used flags before caller checks the avail idx. */
223
333
        smp_mb();
224
334
    }
 
335
    rcu_read_unlock();
225
336
}
226
337
 
227
338
int virtio_queue_ready(VirtQueue *vq)
230
341
}
231
342
 
232
343
/* Fetch avail_idx from VQ memory only when we really need to know if
233
 
 * guest has added some buffers. */
 
344
 * guest has added some buffers.
 
345
 * Called within rcu_read_lock().  */
 
346
static int virtio_queue_empty_rcu(VirtQueue *vq)
 
347
{
 
348
    if (unlikely(!vq->vring.avail)) {
 
349
        return 1;
 
350
    }
 
351
 
 
352
    if (vq->shadow_avail_idx != vq->last_avail_idx) {
 
353
        return 0;
 
354
    }
 
355
 
 
356
    return vring_avail_idx(vq) == vq->last_avail_idx;
 
357
}
 
358
 
234
359
int virtio_queue_empty(VirtQueue *vq)
235
360
{
 
361
    bool empty;
 
362
 
 
363
    if (unlikely(!vq->vring.avail)) {
 
364
        return 1;
 
365
    }
 
366
 
236
367
    if (vq->shadow_avail_idx != vq->last_avail_idx) {
237
368
        return 0;
238
369
    }
239
370
 
240
 
    return vring_avail_idx(vq) == vq->last_avail_idx;
 
371
    rcu_read_lock();
 
372
    empty = vring_avail_idx(vq) == vq->last_avail_idx;
 
373
    rcu_read_unlock();
 
374
    return empty;
241
375
}
242
376
 
243
377
static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
244
378
                               unsigned int len)
245
379
{
 
380
    AddressSpace *dma_as = vq->vdev->dma_as;
246
381
    unsigned int offset;
247
382
    int i;
248
383
 
250
385
    for (i = 0; i < elem->in_num; i++) {
251
386
        size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
252
387
 
253
 
        cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
254
 
                                  elem->in_sg[i].iov_len,
255
 
                                  1, size);
 
388
        dma_memory_unmap(dma_as, elem->in_sg[i].iov_base,
 
389
                         elem->in_sg[i].iov_len,
 
390
                         DMA_DIRECTION_FROM_DEVICE, size);
256
391
 
257
392
        offset += size;
258
393
    }
259
394
 
260
395
    for (i = 0; i < elem->out_num; i++)
261
 
        cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
262
 
                                  elem->out_sg[i].iov_len,
263
 
                                  0, elem->out_sg[i].iov_len);
 
396
        dma_memory_unmap(dma_as, elem->out_sg[i].iov_base,
 
397
                         elem->out_sg[i].iov_len,
 
398
                         DMA_DIRECTION_TO_DEVICE,
 
399
                         elem->out_sg[i].iov_len);
264
400
}
265
401
 
266
402
/* virtqueue_detach_element:
316
452
    return true;
317
453
}
318
454
 
 
455
/* Called within rcu_read_lock().  */
319
456
void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
320
457
                    unsigned int len, unsigned int idx)
321
458
{
329
466
        return;
330
467
    }
331
468
 
 
469
    if (unlikely(!vq->vring.used)) {
 
470
        return;
 
471
    }
 
472
 
332
473
    idx = (idx + vq->used_idx) % vq->vring.num;
333
474
 
334
475
    uelem.id = elem->index;
336
477
    vring_used_write(vq, &uelem, idx);
337
478
}
338
479
 
 
480
/* Called within rcu_read_lock().  */
339
481
void virtqueue_flush(VirtQueue *vq, unsigned int count)
340
482
{
341
483
    uint16_t old, new;
345
487
        return;
346
488
    }
347
489
 
 
490
    if (unlikely(!vq->vring.used)) {
 
491
        return;
 
492
    }
 
493
 
348
494
    /* Make sure buffer is written before we update index. */
349
495
    smp_wmb();
350
496
    trace_virtqueue_flush(vq, count);
359
505
void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
360
506
                    unsigned int len)
361
507
{
 
508
    rcu_read_lock();
362
509
    virtqueue_fill(vq, elem, len, 0);
363
510
    virtqueue_flush(vq, 1);
 
511
    rcu_read_unlock();
364
512
}
365
513
 
 
514
/* Called within rcu_read_lock().  */
366
515
static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
367
516
{
368
517
    uint16_t num_heads = vring_avail_idx(vq) - idx;
382
531
    return num_heads;
383
532
}
384
533
 
 
534
/* Called within rcu_read_lock().  */
385
535
static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
386
536
                               unsigned int *head)
387
537
{
405
555
};
406
556
 
407
557
static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
408
 
                                    hwaddr desc_pa, unsigned int max,
 
558
                                    MemoryRegionCache *desc_cache, unsigned int max,
409
559
                                    unsigned int *next)
410
560
{
411
561
    /* If this descriptor says it doesn't chain, we're done. */
423
573
        return VIRTQUEUE_READ_DESC_ERROR;
424
574
    }
425
575
 
426
 
    vring_desc_read(vdev, desc, desc_pa, *next);
 
576
    vring_desc_read(vdev, desc, desc_cache, *next);
427
577
    return VIRTQUEUE_READ_DESC_MORE;
428
578
}
429
579
 
431
581
                               unsigned int *out_bytes,
432
582
                               unsigned max_in_bytes, unsigned max_out_bytes)
433
583
{
434
 
    unsigned int idx;
 
584
    VirtIODevice *vdev = vq->vdev;
 
585
    unsigned int max, idx;
435
586
    unsigned int total_bufs, in_total, out_total;
 
587
    VRingMemoryRegionCaches *caches;
 
588
    MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
 
589
    int64_t len = 0;
436
590
    int rc;
437
591
 
 
592
    if (unlikely(!vq->vring.desc)) {
 
593
        if (in_bytes) {
 
594
            *in_bytes = 0;
 
595
        }
 
596
        if (out_bytes) {
 
597
            *out_bytes = 0;
 
598
        }
 
599
        return;
 
600
    }
 
601
 
 
602
    rcu_read_lock();
438
603
    idx = vq->last_avail_idx;
439
 
 
440
604
    total_bufs = in_total = out_total = 0;
 
605
 
 
606
    max = vq->vring.num;
 
607
    caches = vring_get_region_caches(vq);
 
608
    if (caches->desc.len < max * sizeof(VRingDesc)) {
 
609
        virtio_error(vdev, "Cannot map descriptor ring");
 
610
        goto err;
 
611
    }
 
612
 
441
613
    while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
442
 
        VirtIODevice *vdev = vq->vdev;
443
 
        unsigned int max, num_bufs, indirect = 0;
 
614
        MemoryRegionCache *desc_cache = &caches->desc;
 
615
        unsigned int num_bufs;
444
616
        VRingDesc desc;
445
 
        hwaddr desc_pa;
446
617
        unsigned int i;
447
618
 
448
 
        max = vq->vring.num;
449
619
        num_bufs = total_bufs;
450
620
 
451
621
        if (!virtqueue_get_head(vq, idx++, &i)) {
452
622
            goto err;
453
623
        }
454
624
 
455
 
        desc_pa = vq->vring.desc;
456
 
        vring_desc_read(vdev, &desc, desc_pa, i);
 
625
        vring_desc_read(vdev, &desc, desc_cache, i);
457
626
 
458
627
        if (desc.flags & VRING_DESC_F_INDIRECT) {
459
628
            if (desc.len % sizeof(VRingDesc)) {
468
637
            }
469
638
 
470
639
            /* loop over the indirect descriptor table */
471
 
            indirect = 1;
 
640
            len = address_space_cache_init(&indirect_desc_cache,
 
641
                                           vdev->dma_as,
 
642
                                           desc.addr, desc.len, false);
 
643
            desc_cache = &indirect_desc_cache;
 
644
            if (len < desc.len) {
 
645
                virtio_error(vdev, "Cannot map indirect buffer");
 
646
                goto err;
 
647
            }
 
648
 
472
649
            max = desc.len / sizeof(VRingDesc);
473
 
            desc_pa = desc.addr;
474
650
            num_bufs = i = 0;
475
 
            vring_desc_read(vdev, &desc, desc_pa, i);
 
651
            vring_desc_read(vdev, &desc, desc_cache, i);
476
652
        }
477
653
 
478
654
        do {
491
667
                goto done;
492
668
            }
493
669
 
494
 
            rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
 
670
            rc = virtqueue_read_next_desc(vdev, &desc, desc_cache, max, &i);
495
671
        } while (rc == VIRTQUEUE_READ_DESC_MORE);
496
672
 
497
673
        if (rc == VIRTQUEUE_READ_DESC_ERROR) {
498
674
            goto err;
499
675
        }
500
676
 
501
 
        if (!indirect)
 
677
        if (desc_cache == &indirect_desc_cache) {
 
678
            address_space_cache_destroy(&indirect_desc_cache);
 
679
            total_bufs++;
 
680
        } else {
502
681
            total_bufs = num_bufs;
503
 
        else
504
 
            total_bufs++;
 
682
        }
505
683
    }
506
684
 
507
685
    if (rc < 0) {
509
687
    }
510
688
 
511
689
done:
 
690
    address_space_cache_destroy(&indirect_desc_cache);
512
691
    if (in_bytes) {
513
692
        *in_bytes = in_total;
514
693
    }
515
694
    if (out_bytes) {
516
695
        *out_bytes = out_total;
517
696
    }
 
697
    rcu_read_unlock();
518
698
    return;
519
699
 
520
700
err:
554
734
            goto out;
555
735
        }
556
736
 
557
 
        iov[num_sg].iov_base = cpu_physical_memory_map(pa, &len, is_write);
 
737
        iov[num_sg].iov_base = dma_memory_map(vdev->dma_as, pa, &len,
 
738
                                              is_write ?
 
739
                                              DMA_DIRECTION_FROM_DEVICE :
 
740
                                              DMA_DIRECTION_TO_DEVICE);
558
741
        if (!iov[num_sg].iov_base) {
559
742
            virtio_error(vdev, "virtio: bogus descriptor or out of resources");
560
743
            goto out;
591
774
    }
592
775
}
593
776
 
594
 
static void virtqueue_map_iovec(struct iovec *sg, hwaddr *addr,
595
 
                                unsigned int *num_sg,
 
777
static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
 
778
                                hwaddr *addr, unsigned int *num_sg,
596
779
                                int is_write)
597
780
{
598
781
    unsigned int i;
600
783
 
601
784
    for (i = 0; i < *num_sg; i++) {
602
785
        len = sg[i].iov_len;
603
 
        sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
 
786
        sg[i].iov_base = dma_memory_map(vdev->dma_as,
 
787
                                        addr[i], &len, is_write ?
 
788
                                        DMA_DIRECTION_FROM_DEVICE :
 
789
                                        DMA_DIRECTION_TO_DEVICE);
604
790
        if (!sg[i].iov_base) {
605
791
            error_report("virtio: error trying to map MMIO memory");
606
792
            exit(1);
612
798
    }
613
799
}
614
800
 
615
 
void virtqueue_map(VirtQueueElement *elem)
 
801
void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem)
616
802
{
617
 
    virtqueue_map_iovec(elem->in_sg, elem->in_addr, &elem->in_num, 1);
618
 
    virtqueue_map_iovec(elem->out_sg, elem->out_addr, &elem->out_num, 0);
 
803
    virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, &elem->in_num, 1);
 
804
    virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, &elem->out_num, 0);
619
805
}
620
806
 
621
807
static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
642
828
void *virtqueue_pop(VirtQueue *vq, size_t sz)
643
829
{
644
830
    unsigned int i, head, max;
645
 
    hwaddr desc_pa = vq->vring.desc;
 
831
    VRingMemoryRegionCaches *caches;
 
832
    MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
 
833
    MemoryRegionCache *desc_cache;
 
834
    int64_t len;
646
835
    VirtIODevice *vdev = vq->vdev;
647
 
    VirtQueueElement *elem;
 
836
    VirtQueueElement *elem = NULL;
648
837
    unsigned out_num, in_num;
649
838
    hwaddr addr[VIRTQUEUE_MAX_SIZE];
650
839
    struct iovec iov[VIRTQUEUE_MAX_SIZE];
654
843
    if (unlikely(vdev->broken)) {
655
844
        return NULL;
656
845
    }
657
 
    if (virtio_queue_empty(vq)) {
658
 
        return NULL;
 
846
    rcu_read_lock();
 
847
    if (virtio_queue_empty_rcu(vq)) {
 
848
        goto done;
659
849
    }
660
850
    /* Needed after virtio_queue_empty(), see comment in
661
851
     * virtqueue_num_heads(). */
668
858
 
669
859
    if (vq->inuse >= vq->vring.num) {
670
860
        virtio_error(vdev, "Virtqueue size exceeded");
671
 
        return NULL;
 
861
        goto done;
672
862
    }
673
863
 
674
864
    if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
675
 
        return NULL;
 
865
        goto done;
676
866
    }
677
867
 
678
868
    if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
680
870
    }
681
871
 
682
872
    i = head;
683
 
    vring_desc_read(vdev, &desc, desc_pa, i);
 
873
 
 
874
    caches = vring_get_region_caches(vq);
 
875
    if (caches->desc.len < max * sizeof(VRingDesc)) {
 
876
        virtio_error(vdev, "Cannot map descriptor ring");
 
877
        goto done;
 
878
    }
 
879
 
 
880
    desc_cache = &caches->desc;
 
881
    vring_desc_read(vdev, &desc, desc_cache, i);
684
882
    if (desc.flags & VRING_DESC_F_INDIRECT) {
685
883
        if (desc.len % sizeof(VRingDesc)) {
686
884
            virtio_error(vdev, "Invalid size for indirect buffer table");
687
 
            return NULL;
 
885
            goto done;
688
886
        }
689
887
 
690
888
        /* loop over the indirect descriptor table */
 
889
        len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
 
890
                                       desc.addr, desc.len, false);
 
891
        desc_cache = &indirect_desc_cache;
 
892
        if (len < desc.len) {
 
893
            virtio_error(vdev, "Cannot map indirect buffer");
 
894
            goto done;
 
895
        }
 
896
 
691
897
        max = desc.len / sizeof(VRingDesc);
692
 
        desc_pa = desc.addr;
693
898
        i = 0;
694
 
        vring_desc_read(vdev, &desc, desc_pa, i);
 
899
        vring_desc_read(vdev, &desc, desc_cache, i);
695
900
    }
696
901
 
697
902
    /* Collect all the descriptors */
722
927
            goto err_undo_map;
723
928
        }
724
929
 
725
 
        rc = virtqueue_read_next_desc(vdev, &desc, desc_pa, max, &i);
 
930
        rc = virtqueue_read_next_desc(vdev, &desc, desc_cache, max, &i);
726
931
    } while (rc == VIRTQUEUE_READ_DESC_MORE);
727
932
 
728
933
    if (rc == VIRTQUEUE_READ_DESC_ERROR) {
744
949
    vq->inuse++;
745
950
 
746
951
    trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
 
952
done:
 
953
    address_space_cache_destroy(&indirect_desc_cache);
 
954
    rcu_read_unlock();
 
955
 
747
956
    return elem;
748
957
 
749
958
err_undo_map:
750
959
    virtqueue_undo_map_desc(out_num, in_num, iov);
751
 
    return NULL;
 
960
    goto done;
 
961
}
 
962
 
 
963
/* virtqueue_drop_all:
 
964
 * @vq: The #VirtQueue
 
965
 * Drops all queued buffers and indicates them to the guest
 
966
 * as if they are done. Useful when buffers can not be
 
967
 * processed but must be returned to the guest.
 
968
 */
 
969
unsigned int virtqueue_drop_all(VirtQueue *vq)
 
970
{
 
971
    unsigned int dropped = 0;
 
972
    VirtQueueElement elem = {};
 
973
    VirtIODevice *vdev = vq->vdev;
 
974
    bool fEventIdx = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
 
975
 
 
976
    if (unlikely(vdev->broken)) {
 
977
        return 0;
 
978
    }
 
979
 
 
980
    while (!virtio_queue_empty(vq) && vq->inuse < vq->vring.num) {
 
981
        /* works similar to virtqueue_pop but does not map buffers
 
982
        * and does not allocate any memory */
 
983
        smp_rmb();
 
984
        if (!virtqueue_get_head(vq, vq->last_avail_idx, &elem.index)) {
 
985
            break;
 
986
        }
 
987
        vq->inuse++;
 
988
        vq->last_avail_idx++;
 
989
        if (fEventIdx) {
 
990
            vring_set_avail_event(vq, vq->last_avail_idx);
 
991
        }
 
992
        /* immediately push the element, nothing to unmap
 
993
         * as both in_num and out_num are set to 0 */
 
994
        virtqueue_push(vq, &elem, 0);
 
995
        dropped++;
 
996
    }
 
997
 
 
998
    return dropped;
752
999
}
753
1000
 
754
1001
/* Reading and writing a structure directly to QEMUFile is *awful*, but
768
1015
    struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
769
1016
} VirtQueueElementOld;
770
1017
 
771
 
void *qemu_get_virtqueue_element(QEMUFile *f, size_t sz)
 
1018
void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
772
1019
{
773
1020
    VirtQueueElement *elem;
774
1021
    VirtQueueElementOld data;
809
1056
        elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
810
1057
    }
811
1058
 
812
 
    virtqueue_map(elem);
 
1059
    virtqueue_map(vdev, elem);
813
1060
    return elem;
814
1061
}
815
1062
 
868
1115
{
869
1116
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
870
1117
 
 
1118
    if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM) &&
 
1119
        !virtio_vdev_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
 
1120
        return -EFAULT;
 
1121
    }
 
1122
 
871
1123
    if (k->validate_features) {
872
1124
        return k->validate_features(vdev);
873
1125
    } else {
918
1170
    }
919
1171
}
920
1172
 
 
1173
static void virtio_virtqueue_reset_region_cache(struct VirtQueue *vq)
 
1174
{
 
1175
    VRingMemoryRegionCaches *caches;
 
1176
 
 
1177
    caches = atomic_read(&vq->vring.caches);
 
1178
    atomic_rcu_set(&vq->vring.caches, NULL);
 
1179
    if (caches) {
 
1180
        call_rcu(caches, virtio_free_region_cache, rcu);
 
1181
    }
 
1182
}
 
1183
 
921
1184
void virtio_reset(void *opaque)
922
1185
{
923
1186
    VirtIODevice *vdev = opaque;
958
1221
        vdev->vq[i].notification = true;
959
1222
        vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
960
1223
        vdev->vq[i].inuse = 0;
 
1224
        virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
961
1225
    }
962
1226
}
963
1227
 
1167
1431
    vdev->vq[n].vring.desc = desc;
1168
1432
    vdev->vq[n].vring.avail = avail;
1169
1433
    vdev->vq[n].vring.used = used;
 
1434
    virtio_init_region_cache(vdev, n);
1170
1435
}
1171
1436
 
1172
1437
void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
1197
1462
    return vdev->vq[n].vring.num;
1198
1463
}
1199
1464
 
 
1465
int virtio_queue_get_max_num(VirtIODevice *vdev, int n)
 
1466
{
 
1467
    return vdev->vq[n].vring.num_default;
 
1468
}
 
1469
 
1200
1470
int virtio_get_num_queues(VirtIODevice *vdev)
1201
1471
{
1202
1472
    int i;
1230
1500
    virtio_queue_update_rings(vdev, n);
1231
1501
}
1232
1502
 
1233
 
static void virtio_queue_notify_aio_vq(VirtQueue *vq)
 
1503
static bool virtio_queue_notify_aio_vq(VirtQueue *vq)
1234
1504
{
1235
1505
    if (vq->vring.desc && vq->handle_aio_output) {
1236
1506
        VirtIODevice *vdev = vq->vdev;
1237
1507
 
1238
1508
        trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1239
 
        vq->handle_aio_output(vdev, vq);
 
1509
        return vq->handle_aio_output(vdev, vq);
1240
1510
    }
 
1511
 
 
1512
    return false;
1241
1513
}
1242
1514
 
1243
1515
static void virtio_queue_notify_vq(VirtQueue *vq)
1337
1609
    }
1338
1610
}
1339
1611
 
1340
 
bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
 
1612
/* Called within rcu_read_lock().  */
 
1613
static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
1341
1614
{
1342
1615
    uint16_t old, new;
1343
1616
    bool v;
1362
1635
 
1363
1636
void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
1364
1637
{
1365
 
    if (!virtio_should_notify(vdev, vq)) {
 
1638
    bool should_notify;
 
1639
    rcu_read_lock();
 
1640
    should_notify = virtio_should_notify(vdev, vq);
 
1641
    rcu_read_unlock();
 
1642
 
 
1643
    if (!should_notify) {
1366
1644
        return;
1367
1645
    }
1368
1646
 
1387
1665
    event_notifier_set(&vq->guest_notifier);
1388
1666
}
1389
1667
 
 
1668
static void virtio_irq(VirtQueue *vq)
 
1669
{
 
1670
    virtio_set_isr(vq->vdev, 0x1);
 
1671
    virtio_notify_vector(vq->vdev, vq->vector);
 
1672
}
 
1673
 
1390
1674
void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
1391
1675
{
1392
 
    if (!virtio_should_notify(vdev, vq)) {
 
1676
    bool should_notify;
 
1677
    rcu_read_lock();
 
1678
    should_notify = virtio_should_notify(vdev, vq);
 
1679
    rcu_read_unlock();
 
1680
 
 
1681
    if (!should_notify) {
1393
1682
        return;
1394
1683
    }
1395
1684
 
1396
1685
    trace_virtio_notify(vdev, vq);
1397
 
    virtio_set_isr(vq->vdev, 0x1);
1398
 
    virtio_notify_vector(vdev, vq->vector);
 
1686
    virtio_irq(vq);
1399
1687
}
1400
1688
 
1401
1689
void virtio_notify_config(VirtIODevice *vdev)
1509
1797
    }
1510
1798
};
1511
1799
 
1512
 
static int get_extra_state(QEMUFile *f, void *pv, size_t size)
 
1800
static int get_extra_state(QEMUFile *f, void *pv, size_t size,
 
1801
                           VMStateField *field)
1513
1802
{
1514
1803
    VirtIODevice *vdev = pv;
1515
1804
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1522
1811
    }
1523
1812
}
1524
1813
 
1525
 
static void put_extra_state(QEMUFile *f, void *pv, size_t size)
 
1814
static int put_extra_state(QEMUFile *f, void *pv, size_t size,
 
1815
                           VMStateField *field, QJSON *vmdesc)
1526
1816
{
1527
1817
    VirtIODevice *vdev = pv;
1528
1818
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1529
1819
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1530
1820
 
1531
1821
    k->save_extra_state(qbus->parent, f);
 
1822
    return 0;
1532
1823
}
1533
1824
 
1534
1825
static const VMStateInfo vmstate_info_extra_state = {
1642
1933
        if (k->has_variable_vring_alignment) {
1643
1934
            qemu_put_be32(f, vdev->vq[i].vring.align);
1644
1935
        }
1645
 
        /* XXX virtio-1 devices */
 
1936
        /*
 
1937
         * Save desc now, the rest of the ring addresses are saved in
 
1938
         * subsections for VIRTIO-1 devices.
 
1939
         */
1646
1940
        qemu_put_be64(f, vdev->vq[i].vring.desc);
1647
1941
        qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1648
1942
        if (k->save_queue) {
1663
1957
}
1664
1958
 
1665
1959
/* A wrapper for use as a VMState .put function */
1666
 
static void virtio_device_put(QEMUFile *f, void *opaque, size_t size)
 
1960
static int virtio_device_put(QEMUFile *f, void *opaque, size_t size,
 
1961
                              VMStateField *field, QJSON *vmdesc)
1667
1962
{
1668
1963
    virtio_save(VIRTIO_DEVICE(opaque), f);
 
1964
 
 
1965
    return 0;
1669
1966
}
1670
1967
 
1671
1968
/* A wrapper for use as a VMState .get function */
1672
 
static int virtio_device_get(QEMUFile *f, void *opaque, size_t size)
 
1969
static int virtio_device_get(QEMUFile *f, void *opaque, size_t size,
 
1970
                             VMStateField *field)
1673
1971
{
1674
1972
    VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1675
1973
    DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
1779
2077
        vdev->vq[i].signalled_used_valid = false;
1780
2078
        vdev->vq[i].notification = true;
1781
2079
 
1782
 
        if (vdev->vq[i].vring.desc) {
1783
 
            /* XXX virtio-1 devices */
1784
 
            virtio_queue_update_rings(vdev, i);
1785
 
        } else if (vdev->vq[i].last_avail_idx) {
 
2080
        if (!vdev->vq[i].vring.desc && vdev->vq[i].last_avail_idx) {
1786
2081
            error_report("VQ %d address 0x0 "
1787
2082
                         "inconsistent with Host index 0x%x",
1788
2083
                         i, vdev->vq[i].last_avail_idx);
1789
 
                return -1;
 
2084
            return -1;
1790
2085
        }
1791
2086
        if (k->load_queue) {
1792
2087
            ret = k->load_queue(qbus->parent, i, f);
1843
2138
        }
1844
2139
    }
1845
2140
 
 
2141
    rcu_read_lock();
1846
2142
    for (i = 0; i < num; i++) {
1847
2143
        if (vdev->vq[i].vring.desc) {
1848
2144
            uint16_t nheads;
 
2145
 
 
2146
            /*
 
2147
             * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
 
2148
             * only the region cache needs to be set up.  Legacy devices need
 
2149
             * to calculate used and avail ring addresses based on the desc
 
2150
             * address.
 
2151
             */
 
2152
            if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
 
2153
                virtio_init_region_cache(vdev, i);
 
2154
            } else {
 
2155
                virtio_queue_update_rings(vdev, i);
 
2156
            }
 
2157
 
1849
2158
            nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
1850
2159
            /* Check it isn't doing strange things with descriptor numbers. */
1851
2160
            if (nheads > vdev->vq[i].vring.num) {
1877
2186
            }
1878
2187
        }
1879
2188
    }
 
2189
    rcu_read_unlock();
1880
2190
 
1881
2191
    return 0;
1882
2192
}
1884
2194
void virtio_cleanup(VirtIODevice *vdev)
1885
2195
{
1886
2196
    qemu_del_vm_change_state_handler(vdev->vmstate);
1887
 
    g_free(vdev->config);
1888
 
    g_free(vdev->vq);
1889
 
    g_free(vdev->vector_queues);
1890
2197
}
1891
2198
 
1892
2199
static void virtio_vmstate_change(void *opaque, int running, RunState state)
2004
2311
    vdev->vq[n].shadow_avail_idx = idx;
2005
2312
}
2006
2313
 
 
2314
void virtio_queue_update_used_idx(VirtIODevice *vdev, int n)
 
2315
{
 
2316
    rcu_read_lock();
 
2317
    if (vdev->vq[n].vring.desc) {
 
2318
        vdev->vq[n].used_idx = vring_used_idx(&vdev->vq[n]);
 
2319
    }
 
2320
    rcu_read_unlock();
 
2321
}
 
2322
 
2007
2323
void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
2008
2324
{
2009
2325
    vdev->vq[n].signalled_used_valid = false;
2023
2339
{
2024
2340
    VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
2025
2341
    if (event_notifier_test_and_clear(n)) {
2026
 
        virtio_notify_vector(vq->vdev, vq->vector);
 
2342
        virtio_irq(vq);
2027
2343
    }
2028
2344
}
2029
2345
 
2031
2347
                                                bool with_irqfd)
2032
2348
{
2033
2349
    if (assign && !with_irqfd) {
2034
 
        event_notifier_set_handler(&vq->guest_notifier, false,
 
2350
        event_notifier_set_handler(&vq->guest_notifier,
2035
2351
                                   virtio_queue_guest_notifier_read);
2036
2352
    } else {
2037
 
        event_notifier_set_handler(&vq->guest_notifier, false, NULL);
 
2353
        event_notifier_set_handler(&vq->guest_notifier, NULL);
2038
2354
    }
2039
2355
    if (!assign) {
2040
2356
        /* Test and clear notifier before closing it,
2056
2372
    }
2057
2373
}
2058
2374
 
 
2375
static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier *n)
 
2376
{
 
2377
    VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
 
2378
 
 
2379
    virtio_queue_set_notification(vq, 0);
 
2380
}
 
2381
 
 
2382
static bool virtio_queue_host_notifier_aio_poll(void *opaque)
 
2383
{
 
2384
    EventNotifier *n = opaque;
 
2385
    VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
 
2386
    bool progress;
 
2387
 
 
2388
    if (!vq->vring.desc || virtio_queue_empty(vq)) {
 
2389
        return false;
 
2390
    }
 
2391
 
 
2392
    progress = virtio_queue_notify_aio_vq(vq);
 
2393
 
 
2394
    /* In case the handler function re-enabled notifications */
 
2395
    virtio_queue_set_notification(vq, 0);
 
2396
    return progress;
 
2397
}
 
2398
 
 
2399
static void virtio_queue_host_notifier_aio_poll_end(EventNotifier *n)
 
2400
{
 
2401
    VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
 
2402
 
 
2403
    /* Caller polls once more after this to catch requests that race with us */
 
2404
    virtio_queue_set_notification(vq, 1);
 
2405
}
 
2406
 
2059
2407
void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
2060
 
                                                VirtIOHandleOutput handle_output)
 
2408
                                                VirtIOHandleAIOOutput handle_output)
2061
2409
{
2062
2410
    if (handle_output) {
2063
2411
        vq->handle_aio_output = handle_output;
2064
2412
        aio_set_event_notifier(ctx, &vq->host_notifier, true,
2065
 
                               virtio_queue_host_notifier_aio_read);
 
2413
                               virtio_queue_host_notifier_aio_read,
 
2414
                               virtio_queue_host_notifier_aio_poll);
 
2415
        aio_set_event_notifier_poll(ctx, &vq->host_notifier,
 
2416
                                    virtio_queue_host_notifier_aio_poll_begin,
 
2417
                                    virtio_queue_host_notifier_aio_poll_end);
2066
2418
    } else {
2067
 
        aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL);
 
2419
        aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL);
2068
2420
        /* Test and clear notifier before after disabling event,
2069
2421
         * in case poll callback didn't have time to run. */
2070
2422
        virtio_queue_host_notifier_aio_read(&vq->host_notifier);
2107
2459
    }
2108
2460
}
2109
2461
 
 
2462
static void virtio_memory_listener_commit(MemoryListener *listener)
 
2463
{
 
2464
    VirtIODevice *vdev = container_of(listener, VirtIODevice, listener);
 
2465
    int i;
 
2466
 
 
2467
    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
 
2468
        if (vdev->vq[i].vring.num == 0) {
 
2469
            break;
 
2470
        }
 
2471
        virtio_init_region_cache(vdev, i);
 
2472
    }
 
2473
}
 
2474
 
2110
2475
static void virtio_device_realize(DeviceState *dev, Error **errp)
2111
2476
{
2112
2477
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
2129
2494
        error_propagate(errp, err);
2130
2495
        return;
2131
2496
    }
 
2497
 
 
2498
    vdev->listener.commit = virtio_memory_listener_commit;
 
2499
    memory_listener_register(&vdev->listener, vdev->dma_as);
2132
2500
}
2133
2501
 
2134
2502
static void virtio_device_unrealize(DeviceState *dev, Error **errp)
2151
2519
    vdev->bus_name = NULL;
2152
2520
}
2153
2521
 
 
2522
static void virtio_device_free_virtqueues(VirtIODevice *vdev)
 
2523
{
 
2524
    int i;
 
2525
    if (!vdev->vq) {
 
2526
        return;
 
2527
    }
 
2528
 
 
2529
    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
 
2530
        if (vdev->vq[i].vring.num == 0) {
 
2531
            break;
 
2532
        }
 
2533
        virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
 
2534
    }
 
2535
    g_free(vdev->vq);
 
2536
}
 
2537
 
 
2538
static void virtio_device_instance_finalize(Object *obj)
 
2539
{
 
2540
    VirtIODevice *vdev = VIRTIO_DEVICE(obj);
 
2541
 
 
2542
    memory_listener_unregister(&vdev->listener);
 
2543
    virtio_device_free_virtqueues(vdev);
 
2544
 
 
2545
    g_free(vdev->config);
 
2546
    g_free(vdev->vector_queues);
 
2547
}
 
2548
 
2154
2549
static Property virtio_properties[] = {
2155
2550
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
2156
2551
    DEFINE_PROP_END_OF_LIST(),
2171
2566
            err = r;
2172
2567
            goto assign_error;
2173
2568
        }
2174
 
        event_notifier_set_handler(&vq->host_notifier, true,
 
2569
        event_notifier_set_handler(&vq->host_notifier,
2175
2570
                                   virtio_queue_host_notifier_read);
2176
2571
    }
2177
2572
 
2192
2587
            continue;
2193
2588
        }
2194
2589
 
2195
 
        event_notifier_set_handler(&vq->host_notifier, true, NULL);
 
2590
        event_notifier_set_handler(&vq->host_notifier, NULL);
2196
2591
        r = virtio_bus_set_host_notifier(qbus, n, false);
2197
2592
        assert(r >= 0);
2198
2593
    }
2218
2613
        if (!virtio_queue_get_num(vdev, n)) {
2219
2614
            continue;
2220
2615
        }
2221
 
        event_notifier_set_handler(&vq->host_notifier, true, NULL);
 
2616
        event_notifier_set_handler(&vq->host_notifier, NULL);
2222
2617
        r = virtio_bus_set_host_notifier(qbus, n, false);
2223
2618
        assert(r >= 0);
2224
2619
    }
2277
2672
    .parent = TYPE_DEVICE,
2278
2673
    .instance_size = sizeof(VirtIODevice),
2279
2674
    .class_init = virtio_device_class_init,
 
2675
    .instance_finalize = virtio_device_instance_finalize,
2280
2676
    .abstract = true,
2281
2677
    .class_size = sizeof(VirtioDeviceClass),
2282
2678
};