virtio.c 33.5 KB
Newer Older
A
aliguori 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Virtio Support
 *
 * Copyright IBM, Corp. 2007
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#include <inttypes.h>

16
#include "trace.h"
17
#include "exec/address-spaces.h"
18
#include "qemu/error-report.h"
P
Paolo Bonzini 已提交
19
#include "hw/virtio/virtio.h"
20
#include "qemu/atomic.h"
P
Paolo Bonzini 已提交
21
#include "hw/virtio/virtio-bus.h"
22
#include "migration/migration.h"
A
aliguori 已提交
23

24 25 26 27 28
/*
 * The alignment to use between consumer and producer parts of vring.
 * x86 pagesize again. This is the default, used by transports like PCI
 * which don't provide a means for the guest to tell the host the alignment.
 */
29 30
#define VIRTIO_PCI_VRING_ALIGN         4096

A
aliguori 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
typedef struct VRingDesc
{
    uint64_t addr;
    uint32_t len;
    uint16_t flags;
    uint16_t next;
} VRingDesc;

typedef struct VRingAvail
{
    uint16_t flags;
    uint16_t idx;
    uint16_t ring[0];
} VRingAvail;

typedef struct VRingUsedElem
{
    uint32_t id;
    uint32_t len;
} VRingUsedElem;

typedef struct VRingUsed
{
    uint16_t flags;
    uint16_t idx;
    VRingUsedElem ring[0];
} VRingUsed;

typedef struct VRing
{
    unsigned int num;
62
    unsigned int align;
A
Avi Kivity 已提交
63 64 65
    hwaddr desc;
    hwaddr avail;
    hwaddr used;
A
aliguori 已提交
66 67 68 69 70
} VRing;

struct VirtQueue
{
    VRing vring;
A
Avi Kivity 已提交
71
    hwaddr pa;
A
aliguori 已提交
72
    uint16_t last_avail_idx;
M
Michael S. Tsirkin 已提交
73 74 75 76 77 78 79 80 81
    /* Last used index value we have signalled on */
    uint16_t signalled_used;

    /* Last used index value we have signalled on */
    bool signalled_used_valid;

    /* Notification enabled? */
    bool notification;

82 83
    uint16_t queue_index;

A
aliguori 已提交
84
    int inuse;
M
Michael S. Tsirkin 已提交
85

86
    uint16_t vector;
A
aliguori 已提交
87
    void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
88 89 90
    VirtIODevice *vdev;
    EventNotifier guest_notifier;
    EventNotifier host_notifier;
A
aliguori 已提交
91 92 93
};

/* virt queue functions */
P
Paul Brook 已提交
94
static void virtqueue_init(VirtQueue *vq)
A
aliguori 已提交
95
{
A
Avi Kivity 已提交
96
    hwaddr pa = vq->pa;
P
Paul Brook 已提交
97

A
aliguori 已提交
98 99
    vq->vring.desc = pa;
    vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
100 101
    vq->vring.used = vring_align(vq->vring.avail +
                                 offsetof(VRingAvail, ring[vq->vring.num]),
102
                                 vq->vring.align);
A
aliguori 已提交
103 104
}

A
Avi Kivity 已提交
105
static inline uint64_t vring_desc_addr(hwaddr desc_pa, int i)
A
aliguori 已提交
106
{
A
Avi Kivity 已提交
107
    hwaddr pa;
108
    pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr);
109
    return ldq_phys(&address_space_memory, pa);
A
aliguori 已提交
110 111
}

A
Avi Kivity 已提交
112
static inline uint32_t vring_desc_len(hwaddr desc_pa, int i)
A
aliguori 已提交
113
{
A
Avi Kivity 已提交
114
    hwaddr pa;
115
    pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len);
116
    return ldl_phys(&address_space_memory, pa);
A
aliguori 已提交
117 118
}

A
Avi Kivity 已提交
119
static inline uint16_t vring_desc_flags(hwaddr desc_pa, int i)
A
aliguori 已提交
120
{
A
Avi Kivity 已提交
121
    hwaddr pa;
122
    pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags);
123
    return lduw_phys(&address_space_memory, pa);
A
aliguori 已提交
124 125
}

A
Avi Kivity 已提交
126
static inline uint16_t vring_desc_next(hwaddr desc_pa, int i)
A
aliguori 已提交
127
{
A
Avi Kivity 已提交
128
    hwaddr pa;
129
    pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next);
130
    return lduw_phys(&address_space_memory, pa);
A
aliguori 已提交
131 132 133 134
}

static inline uint16_t vring_avail_flags(VirtQueue *vq)
{
A
Avi Kivity 已提交
135
    hwaddr pa;
A
aliguori 已提交
136
    pa = vq->vring.avail + offsetof(VRingAvail, flags);
137
    return lduw_phys(&address_space_memory, pa);
A
aliguori 已提交
138 139 140 141
}

static inline uint16_t vring_avail_idx(VirtQueue *vq)
{
A
Avi Kivity 已提交
142
    hwaddr pa;
A
aliguori 已提交
143
    pa = vq->vring.avail + offsetof(VRingAvail, idx);
144
    return lduw_phys(&address_space_memory, pa);
A
aliguori 已提交
145 146 147 148
}

static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
{
A
Avi Kivity 已提交
149
    hwaddr pa;
A
aliguori 已提交
150
    pa = vq->vring.avail + offsetof(VRingAvail, ring[i]);
151
    return lduw_phys(&address_space_memory, pa);
A
aliguori 已提交
152 153
}

M
Michael S. Tsirkin 已提交
154 155 156 157 158
static inline uint16_t vring_used_event(VirtQueue *vq)
{
    return vring_avail_ring(vq, vq->vring.num);
}

A
aliguori 已提交
159 160
static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val)
{
A
Avi Kivity 已提交
161
    hwaddr pa;
A
aliguori 已提交
162
    pa = vq->vring.used + offsetof(VRingUsed, ring[i].id);
163
    stl_phys(&address_space_memory, pa, val);
A
aliguori 已提交
164 165 166 167
}

static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val)
{
A
Avi Kivity 已提交
168
    hwaddr pa;
A
aliguori 已提交
169
    pa = vq->vring.used + offsetof(VRingUsed, ring[i].len);
170
    stl_phys(&address_space_memory, pa, val);
A
aliguori 已提交
171 172 173 174
}

static uint16_t vring_used_idx(VirtQueue *vq)
{
A
Avi Kivity 已提交
175
    hwaddr pa;
A
aliguori 已提交
176
    pa = vq->vring.used + offsetof(VRingUsed, idx);
177
    return lduw_phys(&address_space_memory, pa);
A
aliguori 已提交
178 179
}

M
Michael S. Tsirkin 已提交
180
static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
A
aliguori 已提交
181
{
A
Avi Kivity 已提交
182
    hwaddr pa;
A
aliguori 已提交
183
    pa = vq->vring.used + offsetof(VRingUsed, idx);
184
    stw_phys(&address_space_memory, pa, val);
A
aliguori 已提交
185 186 187 188
}

static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
{
A
Avi Kivity 已提交
189
    hwaddr pa;
A
aliguori 已提交
190
    pa = vq->vring.used + offsetof(VRingUsed, flags);
191 192
    stw_phys(&address_space_memory,
             pa, lduw_phys(&address_space_memory, pa) | mask);
A
aliguori 已提交
193 194 195 196
}

static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
{
A
Avi Kivity 已提交
197
    hwaddr pa;
A
aliguori 已提交
198
    pa = vq->vring.used + offsetof(VRingUsed, flags);
199 200
    stw_phys(&address_space_memory,
             pa, lduw_phys(&address_space_memory, pa) & ~mask);
A
aliguori 已提交
201 202
}

M
Michael S. Tsirkin 已提交
203 204
static inline void vring_avail_event(VirtQueue *vq, uint16_t val)
{
A
Avi Kivity 已提交
205
    hwaddr pa;
M
Michael S. Tsirkin 已提交
206 207 208 209
    if (!vq->notification) {
        return;
    }
    pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]);
210
    stw_phys(&address_space_memory, pa, val);
M
Michael S. Tsirkin 已提交
211 212
}

A
aliguori 已提交
213 214
void virtio_queue_set_notification(VirtQueue *vq, int enable)
{
M
Michael S. Tsirkin 已提交
215 216 217 218
    vq->notification = enable;
    if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
        vring_avail_event(vq, vring_avail_idx(vq));
    } else if (enable) {
A
aliguori 已提交
219
        vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
M
Michael S. Tsirkin 已提交
220
    } else {
A
aliguori 已提交
221
        vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
M
Michael S. Tsirkin 已提交
222
    }
223 224 225 226
    if (enable) {
        /* Expose avail event/used flags before caller checks the avail idx. */
        smp_mb();
    }
A
aliguori 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
}

int virtio_queue_ready(VirtQueue *vq)
{
    return vq->vring.avail != 0;
}

int virtio_queue_empty(VirtQueue *vq)
{
    return vring_avail_idx(vq) == vq->last_avail_idx;
}

void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
                    unsigned int len, unsigned int idx)
{
    unsigned int offset;
    int i;

245 246
    trace_virtqueue_fill(vq, elem, len, idx);

A
aliguori 已提交
247 248 249 250
    offset = 0;
    for (i = 0; i < elem->in_num; i++) {
        size_t size = MIN(len - offset, elem->in_sg[i].iov_len);

251 252 253
        cpu_physical_memory_unmap(elem->in_sg[i].iov_base,
                                  elem->in_sg[i].iov_len,
                                  1, size);
A
aliguori 已提交
254

255
        offset += size;
A
aliguori 已提交
256 257
    }

258 259 260 261 262
    for (i = 0; i < elem->out_num; i++)
        cpu_physical_memory_unmap(elem->out_sg[i].iov_base,
                                  elem->out_sg[i].iov_len,
                                  0, elem->out_sg[i].iov_len);

A
aliguori 已提交
263 264 265 266 267 268 269 270 271
    idx = (idx + vring_used_idx(vq)) % vq->vring.num;

    /* Get a pointer to the next entry in the used ring. */
    vring_used_ring_id(vq, idx, elem->index);
    vring_used_ring_len(vq, idx, len);
}

void virtqueue_flush(VirtQueue *vq, unsigned int count)
{
M
Michael S. Tsirkin 已提交
272
    uint16_t old, new;
A
aliguori 已提交
273
    /* Make sure buffer is written before we update index. */
274
    smp_wmb();
275
    trace_virtqueue_flush(vq, count);
M
Michael S. Tsirkin 已提交
276 277 278
    old = vring_used_idx(vq);
    new = old + count;
    vring_used_idx_set(vq, new);
A
aliguori 已提交
279
    vq->inuse -= count;
M
Michael S. Tsirkin 已提交
280 281
    if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
        vq->signalled_used_valid = false;
A
aliguori 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
}

void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
                    unsigned int len)
{
    virtqueue_fill(vq, elem, len, 0);
    virtqueue_flush(vq, 1);
}

static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
{
    uint16_t num_heads = vring_avail_idx(vq) - idx;

    /* Check it isn't doing very strange things with descriptor numbers. */
A
aliguori 已提交
296
    if (num_heads > vq->vring.num) {
297 298
        error_report("Guest moved used index from %u to %u",
                     idx, vring_avail_idx(vq));
A
aliguori 已提交
299 300
        exit(1);
    }
301 302 303 304 305
    /* On success, callers read a descriptor at vq->last_avail_idx.
     * Make sure descriptor read does not bypass avail index read. */
    if (num_heads) {
        smp_rmb();
    }
A
aliguori 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318

    return num_heads;
}

static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx)
{
    unsigned int head;

    /* Grab the next descriptor number they're advertising, and increment
     * the index we've seen. */
    head = vring_avail_ring(vq, idx % vq->vring.num);

    /* If their number is silly, that's a fatal mistake. */
A
aliguori 已提交
319
    if (head >= vq->vring.num) {
320
        error_report("Guest says index %u is available", head);
A
aliguori 已提交
321 322
        exit(1);
    }
A
aliguori 已提交
323 324 325 326

    return head;
}

A
Avi Kivity 已提交
327
static unsigned virtqueue_next_desc(hwaddr desc_pa,
328
                                    unsigned int i, unsigned int max)
A
aliguori 已提交
329 330 331 332
{
    unsigned int next;

    /* If this descriptor says it doesn't chain, we're done. */
333 334
    if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT))
        return max;
A
aliguori 已提交
335 336

    /* Check they're not leading us off end of descriptors. */
337
    next = vring_desc_next(desc_pa, i);
A
aliguori 已提交
338
    /* Make sure compiler knows to grab that: we don't want it changing! */
339
    smp_wmb();
A
aliguori 已提交
340

341
    if (next >= max) {
342
        error_report("Desc next is %u", next);
A
aliguori 已提交
343 344
        exit(1);
    }
A
aliguori 已提交
345 346 347 348

    return next;
}

349
void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
350 351
                               unsigned int *out_bytes,
                               unsigned max_in_bytes, unsigned max_out_bytes)
A
aliguori 已提交
352
{
353
    unsigned int idx;
354
    unsigned int total_bufs, in_total, out_total;
A
aliguori 已提交
355 356 357

    idx = vq->last_avail_idx;

358
    total_bufs = in_total = out_total = 0;
A
aliguori 已提交
359
    while (virtqueue_num_heads(vq, idx)) {
360
        unsigned int max, num_bufs, indirect = 0;
A
Avi Kivity 已提交
361
        hwaddr desc_pa;
A
aliguori 已提交
362 363
        int i;

364 365
        max = vq->vring.num;
        num_bufs = total_bufs;
A
aliguori 已提交
366
        i = virtqueue_get_head(vq, idx++);
367 368 369 370
        desc_pa = vq->vring.desc;

        if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
            if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
371
                error_report("Invalid size for indirect buffer table");
372 373 374 375 376
                exit(1);
            }

            /* If we've got too many, that implies a descriptor loop. */
            if (num_bufs >= max) {
377
                error_report("Looped descriptor");
378 379 380 381 382 383 384
                exit(1);
            }

            /* loop over the indirect descriptor table */
            indirect = 1;
            max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
            desc_pa = vring_desc_addr(desc_pa, i);
385
            num_bufs = i = 0;
386 387
        }

A
aliguori 已提交
388 389
        do {
            /* If we've got too many, that implies a descriptor loop. */
390
            if (++num_bufs > max) {
391
                error_report("Looped descriptor");
A
aliguori 已提交
392 393
                exit(1);
            }
A
aliguori 已提交
394

395
            if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
396
                in_total += vring_desc_len(desc_pa, i);
A
aliguori 已提交
397
            } else {
398
                out_total += vring_desc_len(desc_pa, i);
A
aliguori 已提交
399
            }
400 401 402
            if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
                goto done;
            }
403
        } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
404 405 406 407 408

        if (!indirect)
            total_bufs = num_bufs;
        else
            total_bufs++;
A
aliguori 已提交
409
    }
410
done:
411 412 413 414 415 416 417
    if (in_bytes) {
        *in_bytes = in_total;
    }
    if (out_bytes) {
        *out_bytes = out_total;
    }
}
A
aliguori 已提交
418

419 420 421 422 423
int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
                          unsigned int out_bytes)
{
    unsigned int in_total, out_total;

424 425
    virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
    return in_bytes <= in_total && out_bytes <= out_total;
A
aliguori 已提交
426 427
}

A
Avi Kivity 已提交
428
void virtqueue_map_sg(struct iovec *sg, hwaddr *addr,
K
Kevin Wolf 已提交
429 430 431
    size_t num_sg, int is_write)
{
    unsigned int i;
A
Avi Kivity 已提交
432
    hwaddr len;
K
Kevin Wolf 已提交
433

434
    if (num_sg > VIRTQUEUE_MAX_SIZE) {
435 436 437 438 439
        error_report("virtio: map attempt out of bounds: %zd > %d",
                     num_sg, VIRTQUEUE_MAX_SIZE);
        exit(1);
    }

K
Kevin Wolf 已提交
440 441 442 443
    for (i = 0; i < num_sg; i++) {
        len = sg[i].iov_len;
        sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write);
        if (sg[i].iov_base == NULL || len != sg[i].iov_len) {
M
Michael Tokarev 已提交
444
            error_report("virtio: error trying to map MMIO memory");
K
Kevin Wolf 已提交
445 446 447 448 449
            exit(1);
        }
    }
}

A
aliguori 已提交
450 451
int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
{
452
    unsigned int i, head, max;
A
Avi Kivity 已提交
453
    hwaddr desc_pa = vq->vring.desc;
A
aliguori 已提交
454 455 456 457 458 459 460

    if (!virtqueue_num_heads(vq, vq->last_avail_idx))
        return 0;

    /* When we start there are none of either input nor output. */
    elem->out_num = elem->in_num = 0;

461 462
    max = vq->vring.num;

A
aliguori 已提交
463
    i = head = virtqueue_get_head(vq, vq->last_avail_idx++);
M
Michael S. Tsirkin 已提交
464 465 466
    if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
        vring_avail_event(vq, vring_avail_idx(vq));
    }
467 468 469

    if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) {
        if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) {
470
            error_report("Invalid size for indirect buffer table");
471 472 473 474 475 476 477 478 479
            exit(1);
        }

        /* loop over the indirect descriptor table */
        max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc);
        desc_pa = vring_desc_addr(desc_pa, i);
        i = 0;
    }

K
Kevin Wolf 已提交
480
    /* Collect all the descriptors */
A
aliguori 已提交
481 482 483
    do {
        struct iovec *sg;

484
        if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) {
485 486 487 488
            if (elem->in_num >= ARRAY_SIZE(elem->in_sg)) {
                error_report("Too many write descriptors in indirect table");
                exit(1);
            }
489
            elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i);
A
aliguori 已提交
490
            sg = &elem->in_sg[elem->in_num++];
K
Kevin Wolf 已提交
491
        } else {
492 493 494 495
            if (elem->out_num >= ARRAY_SIZE(elem->out_sg)) {
                error_report("Too many read descriptors in indirect table");
                exit(1);
            }
K
Kevin Wolf 已提交
496
            elem->out_addr[elem->out_num] = vring_desc_addr(desc_pa, i);
A
aliguori 已提交
497
            sg = &elem->out_sg[elem->out_num++];
K
Kevin Wolf 已提交
498
        }
A
aliguori 已提交
499

500
        sg->iov_len = vring_desc_len(desc_pa, i);
A
aliguori 已提交
501 502

        /* If we've got too many, that implies a descriptor loop. */
503
        if ((elem->in_num + elem->out_num) > max) {
504
            error_report("Looped descriptor");
A
aliguori 已提交
505 506
            exit(1);
        }
507
    } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max);
A
aliguori 已提交
508

K
Kevin Wolf 已提交
509 510 511 512
    /* Now map what we have collected */
    virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1);
    virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0);

A
aliguori 已提交
513 514 515 516
    elem->index = head;

    vq->inuse++;

517
    trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
A
aliguori 已提交
518 519 520 521
    return elem->in_num + elem->out_num;
}

/* virtio device */
522 523
static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
{
K
KONRAD Frederic 已提交
524 525 526 527 528
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);

    if (k->notify) {
        k->notify(qbus->parent, vector);
529 530
    }
}
A
aliguori 已提交
531

P
Paul Brook 已提交
532
void virtio_update_irq(VirtIODevice *vdev)
A
aliguori 已提交
533
{
534
    virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
A
aliguori 已提交
535 536
}

537 538
void virtio_set_status(VirtIODevice *vdev, uint8_t val)
{
539
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
540 541
    trace_virtio_set_status(vdev, val);

542 543
    if (k->set_status) {
        k->set_status(vdev, val);
544 545 546 547
    }
    vdev->status = val;
}

P
Paul Brook 已提交
548
void virtio_reset(void *opaque)
A
aliguori 已提交
549 550
{
    VirtIODevice *vdev = opaque;
551
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
552 553
    int i;

554 555
    virtio_set_status(vdev, 0);

556 557 558
    if (k->reset) {
        k->reset(vdev);
    }
A
aliguori 已提交
559

560
    vdev->guest_features = 0;
A
aliguori 已提交
561 562 563
    vdev->queue_sel = 0;
    vdev->status = 0;
    vdev->isr = 0;
564 565
    vdev->config_vector = VIRTIO_NO_VECTOR;
    virtio_notify_vector(vdev, vdev->config_vector);
A
aliguori 已提交
566 567 568 569 570 571

    for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
        vdev->vq[i].vring.desc = 0;
        vdev->vq[i].vring.avail = 0;
        vdev->vq[i].vring.used = 0;
        vdev->vq[i].last_avail_idx = 0;
P
Paul Brook 已提交
572
        vdev->vq[i].pa = 0;
573
        vdev->vq[i].vector = VIRTIO_NO_VECTOR;
M
Michael S. Tsirkin 已提交
574 575 576
        vdev->vq[i].signalled_used = 0;
        vdev->vq[i].signalled_used_valid = false;
        vdev->vq[i].notification = true;
A
aliguori 已提交
577 578 579
    }
}

P
Paul Brook 已提交
580
uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
A
aliguori 已提交
581
{
582
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
583 584
    uint8_t val;

585
    if (addr + sizeof(val) > vdev->config_len) {
A
aliguori 已提交
586
        return (uint32_t)-1;
587 588 589
    }

    k->get_config(vdev, vdev->config);
A
aliguori 已提交
590

591
    val = ldub_p(vdev->config + addr);
A
aliguori 已提交
592 593 594
    return val;
}

P
Paul Brook 已提交
595
uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
A
aliguori 已提交
596
{
597
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
598 599
    uint16_t val;

600
    if (addr + sizeof(val) > vdev->config_len) {
A
aliguori 已提交
601
        return (uint32_t)-1;
602 603 604
    }

    k->get_config(vdev, vdev->config);
A
aliguori 已提交
605

606
    val = lduw_p(vdev->config + addr);
A
aliguori 已提交
607 608 609
    return val;
}

P
Paul Brook 已提交
610
uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
A
aliguori 已提交
611
{
612
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
613 614
    uint32_t val;

615
    if (addr + sizeof(val) > vdev->config_len) {
A
aliguori 已提交
616
        return (uint32_t)-1;
617 618 619
    }

    k->get_config(vdev, vdev->config);
A
aliguori 已提交
620

621
    val = ldl_p(vdev->config + addr);
A
aliguori 已提交
622 623 624
    return val;
}

P
Paul Brook 已提交
625
void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
A
aliguori 已提交
626
{
627
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
628 629
    uint8_t val = data;

630
    if (addr + sizeof(val) > vdev->config_len) {
A
aliguori 已提交
631
        return;
632
    }
A
aliguori 已提交
633

634
    stb_p(vdev->config + addr, val);
A
aliguori 已提交
635

636 637 638
    if (k->set_config) {
        k->set_config(vdev, vdev->config);
    }
A
aliguori 已提交
639 640
}

P
Paul Brook 已提交
641
void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
A
aliguori 已提交
642
{
643
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
644 645
    uint16_t val = data;

646
    if (addr + sizeof(val) > vdev->config_len) {
A
aliguori 已提交
647
        return;
648
    }
A
aliguori 已提交
649

650
    stw_p(vdev->config + addr, val);
A
aliguori 已提交
651

652 653 654
    if (k->set_config) {
        k->set_config(vdev, vdev->config);
    }
A
aliguori 已提交
655 656
}

P
Paul Brook 已提交
657
void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
A
aliguori 已提交
658
{
659
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
660 661
    uint32_t val = data;

662
    if (addr + sizeof(val) > vdev->config_len) {
A
aliguori 已提交
663
        return;
664
    }
A
aliguori 已提交
665

666
    stl_p(vdev->config + addr, val);
A
aliguori 已提交
667

668 669 670
    if (k->set_config) {
        k->set_config(vdev, vdev->config);
    }
A
aliguori 已提交
671 672
}

A
Avi Kivity 已提交
673
void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
A
aliguori 已提交
674
{
675 676
    vdev->vq[n].pa = addr;
    virtqueue_init(&vdev->vq[n]);
P
Paul Brook 已提交
677 678
}

A
Avi Kivity 已提交
679
hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
P
Paul Brook 已提交
680 681 682 683
{
    return vdev->vq[n].pa;
}

684 685
void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
{
686 687 688 689 690 691 692
    /* Don't allow guest to flip queue between existent and
     * nonexistent states, or to set it to an invalid size.
     */
    if (!!num != !!vdev->vq[n].vring.num ||
        num > VIRTQUEUE_MAX_SIZE ||
        num < 0) {
        return;
693
    }
694 695
    vdev->vq[n].vring.num = num;
    virtqueue_init(&vdev->vq[n]);
696 697
}

P
Paul Brook 已提交
698 699 700 701
int virtio_queue_get_num(VirtIODevice *vdev, int n)
{
    return vdev->vq[n].vring.num;
}
A
aliguori 已提交
702

P
Paolo Bonzini 已提交
703 704 705 706 707 708 709
int virtio_queue_get_id(VirtQueue *vq)
{
    VirtIODevice *vdev = vq->vdev;
    assert(vq >= &vdev->vq[0] && vq < &vdev->vq[VIRTIO_PCI_QUEUE_MAX]);
    return vq - &vdev->vq[0];
}

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
{
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);

    /* Check that the transport told us it was going to do this
     * (so a buggy transport will immediately assert rather than
     * silently failing to migrate this state)
     */
    assert(k->has_variable_vring_alignment);

    vdev->vq[n].vring.align = align;
    virtqueue_init(&vdev->vq[n]);
}

725 726 727 728 729 730 731 732 733
void virtio_queue_notify_vq(VirtQueue *vq)
{
    if (vq->vring.desc) {
        VirtIODevice *vdev = vq->vdev;
        trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
        vq->handle_output(vdev, vq);
    }
}

P
Paul Brook 已提交
734 735
void virtio_queue_notify(VirtIODevice *vdev, int n)
{
736
    virtio_queue_notify_vq(&vdev->vq[n]);
A
aliguori 已提交
737 738
}

739 740 741 742 743 744 745 746 747 748 749 750
uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
{
    return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector :
        VIRTIO_NO_VECTOR;
}

void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
{
    if (n < VIRTIO_PCI_QUEUE_MAX)
        vdev->vq[n].vector = vector;
}

A
aliguori 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764
VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
                            void (*handle_output)(VirtIODevice *, VirtQueue *))
{
    int i;

    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
        if (vdev->vq[i].vring.num == 0)
            break;
    }

    if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
        abort();

    vdev->vq[i].vring.num = queue_size;
765
    vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
A
aliguori 已提交
766 767 768 769 770
    vdev->vq[i].handle_output = handle_output;

    return &vdev->vq[i];
}

771 772 773 774 775 776 777 778 779
void virtio_del_queue(VirtIODevice *vdev, int n)
{
    if (n < 0 || n >= VIRTIO_PCI_QUEUE_MAX) {
        abort();
    }

    vdev->vq[n].vring.num = 0;
}

780 781
void virtio_irq(VirtQueue *vq)
{
782
    trace_virtio_irq(vq);
783 784 785 786
    vq->vdev->isr |= 0x01;
    virtio_notify_vector(vq->vdev, vq->vector);
}

M
Michael S. Tsirkin 已提交
787 788 789 790
/* Assuming a given event_idx value from the other size, if
 * we have just incremented index from old to new_idx,
 * should we trigger an event? */
static inline int vring_need_event(uint16_t event, uint16_t new, uint16_t old)
A
aliguori 已提交
791
{
M
Michael S. Tsirkin 已提交
792 793 794 795 796 797 798 799 800 801 802 803
	/* Note: Xen has similar logic for notification hold-off
	 * in include/xen/interface/io/ring.h with req_event and req_prod
	 * corresponding to event_idx + 1 and new respectively.
	 * Note also that req_event and req_prod in Xen start at 1,
	 * event indexes in virtio start at 0. */
	return (uint16_t)(new - event - 1) < (uint16_t)(new - old);
}

static bool vring_notify(VirtIODevice *vdev, VirtQueue *vq)
{
    uint16_t old, new;
    bool v;
804 805
    /* We need to expose used array entries before checking used event. */
    smp_mb();
806
    /* Always notify when queue is empty (when feature acknowledge) */
M
Michael S. Tsirkin 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
    if (((vdev->guest_features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) &&
         !vq->inuse && vring_avail_idx(vq) == vq->last_avail_idx)) {
        return true;
    }

    if (!(vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
        return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
    }

    v = vq->signalled_used_valid;
    vq->signalled_used_valid = true;
    old = vq->signalled_used;
    new = vq->signalled_used = vring_used_idx(vq);
    return !v || vring_need_event(vring_used_event(vq), new, old);
}

void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
{
    if (!vring_notify(vdev, vq)) {
A
aliguori 已提交
826
        return;
M
Michael S. Tsirkin 已提交
827
    }
A
aliguori 已提交
828

829
    trace_virtio_notify(vdev, vq);
A
aliguori 已提交
830
    vdev->isr |= 0x01;
831
    virtio_notify_vector(vdev, vq->vector);
A
aliguori 已提交
832 833 834 835
}

void virtio_notify_config(VirtIODevice *vdev)
{
836 837 838
    if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
        return;

A
aliguori 已提交
839
    vdev->isr |= 0x03;
840
    virtio_notify_vector(vdev, vdev->config_vector);
A
aliguori 已提交
841 842
}

843 844 845 846 847 848 849 850 851 852
static const VMStateDescription vmstate_virtio = {
    .name = "virtio",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields = (VMStateField[]) {
        VMSTATE_END_OF_LIST()
    }
};

A
aliguori 已提交
853 854
void virtio_save(VirtIODevice *vdev, QEMUFile *f)
{
K
KONRAD Frederic 已提交
855 856
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
857
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
858 859
    int i;

K
KONRAD Frederic 已提交
860 861 862
    if (k->save_config) {
        k->save_config(qbus->parent, f);
    }
A
aliguori 已提交
863 864 865 866

    qemu_put_8s(f, &vdev->status);
    qemu_put_8s(f, &vdev->isr);
    qemu_put_be16s(f, &vdev->queue_sel);
867
    qemu_put_be32s(f, &vdev->guest_features);
A
aliguori 已提交
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
    qemu_put_be32(f, vdev->config_len);
    qemu_put_buffer(f, vdev->config, vdev->config_len);

    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
        if (vdev->vq[i].vring.num == 0)
            break;
    }

    qemu_put_be32(f, i);

    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
        if (vdev->vq[i].vring.num == 0)
            break;

        qemu_put_be32(f, vdev->vq[i].vring.num);
883 884 885
        if (k->has_variable_vring_alignment) {
            qemu_put_be32(f, vdev->vq[i].vring.align);
        }
P
Paul Brook 已提交
886
        qemu_put_be64(f, vdev->vq[i].pa);
A
aliguori 已提交
887
        qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
K
KONRAD Frederic 已提交
888 889 890
        if (k->save_queue) {
            k->save_queue(qbus->parent, i, f);
        }
A
aliguori 已提交
891
    }
892 893 894 895

    if (vdc->save != NULL) {
        vdc->save(vdev, f);
    }
896 897 898

    /* Subsections */
    vmstate_save_state(f, &vmstate_virtio, vdev);
A
aliguori 已提交
899 900
}

901 902
int virtio_set_features(VirtIODevice *vdev, uint32_t val)
{
K
KONRAD Frederic 已提交
903 904
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *vbusk = VIRTIO_BUS_GET_CLASS(qbus);
905
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
K
KONRAD Frederic 已提交
906
    uint32_t supported_features = vbusk->get_features(qbus->parent);
907 908 909
    bool bad = (val & ~supported_features) != 0;

    val &= supported_features;
910 911
    if (k->set_features) {
        k->set_features(vdev, val);
912 913 914 915 916
    }
    vdev->guest_features = val;
    return bad ? -1 : 0;
}

917
int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
A
aliguori 已提交
918
{
919
    int i, ret;
920
    int32_t config_len;
921
    uint32_t num;
922
    uint32_t features;
923
    uint32_t supported_features;
K
KONRAD Frederic 已提交
924 925
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
926
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
A
aliguori 已提交
927

K
KONRAD Frederic 已提交
928 929
    if (k->load_config) {
        ret = k->load_config(qbus->parent, f);
930 931 932
        if (ret)
            return ret;
    }
A
aliguori 已提交
933 934 935 936

    qemu_get_8s(f, &vdev->status);
    qemu_get_8s(f, &vdev->isr);
    qemu_get_be16s(f, &vdev->queue_sel);
937 938 939
    if (vdev->queue_sel >= VIRTIO_PCI_QUEUE_MAX) {
        return -1;
    }
940
    qemu_get_be32s(f, &features);
941 942

    if (virtio_set_features(vdev, features) < 0) {
K
KONRAD Frederic 已提交
943
        supported_features = k->get_features(qbus->parent);
944 945
        error_report("Features 0x%x unsupported. Allowed features: 0x%x",
                     features, supported_features);
946 947
        return -1;
    }
948
    config_len = qemu_get_be32(f);
949 950 951 952 953 954 955 956 957 958 959

    /*
     * There are cases where the incoming config can be bigger or smaller
     * than what we have; so load what we have space for, and skip
     * any excess that's in the stream.
     */
    qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));

    while (config_len > vdev->config_len) {
        qemu_get_byte(f);
        config_len--;
960
    }
A
aliguori 已提交
961 962 963

    num = qemu_get_be32(f);

964 965 966 967 968
    if (num > VIRTIO_PCI_QUEUE_MAX) {
        error_report("Invalid number of PCI queues: 0x%x", num);
        return -1;
    }

A
aliguori 已提交
969 970
    for (i = 0; i < num; i++) {
        vdev->vq[i].vring.num = qemu_get_be32(f);
971 972 973
        if (k->has_variable_vring_alignment) {
            vdev->vq[i].vring.align = qemu_get_be32(f);
        }
P
Paul Brook 已提交
974
        vdev->vq[i].pa = qemu_get_be64(f);
A
aliguori 已提交
975
        qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
M
Michael S. Tsirkin 已提交
976 977
        vdev->vq[i].signalled_used_valid = false;
        vdev->vq[i].notification = true;
A
aliguori 已提交
978

P
Paul Brook 已提交
979
        if (vdev->vq[i].pa) {
M
Michael S. Tsirkin 已提交
980
            uint16_t nheads;
P
Paul Brook 已提交
981
            virtqueue_init(&vdev->vq[i]);
M
Michael S. Tsirkin 已提交
982 983 984 985
            nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
            /* Check it isn't doing very strange things with descriptor numbers. */
            if (nheads > vdev->vq[i].vring.num) {
                error_report("VQ %d size 0x%x Guest index 0x%x "
986
                             "inconsistent with Host index 0x%x: delta 0x%x",
M
Michael S. Tsirkin 已提交
987 988 989 990 991 992 993
                             i, vdev->vq[i].vring.num,
                             vring_avail_idx(&vdev->vq[i]),
                             vdev->vq[i].last_avail_idx, nheads);
                return -1;
            }
        } else if (vdev->vq[i].last_avail_idx) {
            error_report("VQ %d address 0x0 "
994
                         "inconsistent with Host index 0x%x",
M
Michael S. Tsirkin 已提交
995 996
                         i, vdev->vq[i].last_avail_idx);
                return -1;
997
	}
K
KONRAD Frederic 已提交
998 999
        if (k->load_queue) {
            ret = k->load_queue(qbus->parent, i, f);
1000 1001
            if (ret)
                return ret;
1002
        }
A
aliguori 已提交
1003 1004
    }

1005
    virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
1006 1007

    if (vdc->load != NULL) {
1008 1009 1010 1011
        ret = vdc->load(vdev, f, version_id);
        if (ret) {
            return ret;
        }
1012 1013
    }

1014
    return vmstate_load_state(f, &vmstate_virtio, vdev, 1);
A
aliguori 已提交
1015 1016
}

1017
void virtio_cleanup(VirtIODevice *vdev)
1018
{
1019
    qemu_del_vm_change_state_handler(vdev->vmstate);
1020
    g_free(vdev->config);
1021
    g_free(vdev->vq);
1022 1023
}

1024
static void virtio_vmstate_change(void *opaque, int running, RunState state)
1025 1026
{
    VirtIODevice *vdev = opaque;
K
KONRAD Frederic 已提交
1027 1028
    BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1029 1030 1031 1032 1033 1034 1035
    bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
    vdev->vm_running = running;

    if (backend_run) {
        virtio_set_status(vdev, vdev->status);
    }

K
KONRAD Frederic 已提交
1036 1037
    if (k->vmstate_change) {
        k->vmstate_change(qbus->parent, backend_run);
1038 1039 1040 1041 1042 1043 1044
    }

    if (!backend_run) {
        virtio_set_status(vdev, vdev->status);
    }
}

1045 1046
void virtio_init(VirtIODevice *vdev, const char *name,
                 uint16_t device_id, size_t config_size)
A
aliguori 已提交
1047
{
1048
    int i;
P
Paul Brook 已提交
1049
    vdev->device_id = device_id;
A
aliguori 已提交
1050 1051 1052
    vdev->status = 0;
    vdev->isr = 0;
    vdev->queue_sel = 0;
1053
    vdev->config_vector = VIRTIO_NO_VECTOR;
1054
    vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
1055
    vdev->vm_running = runstate_is_running();
1056
    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
1057
        vdev->vq[i].vector = VIRTIO_NO_VECTOR;
1058
        vdev->vq[i].vdev = vdev;
1059
        vdev->vq[i].queue_index = i;
1060
    }
A
aliguori 已提交
1061 1062 1063

    vdev->name = name;
    vdev->config_len = config_size;
1064
    if (vdev->config_len) {
1065
        vdev->config = g_malloc0(config_size);
1066
    } else {
A
aliguori 已提交
1067
        vdev->config = NULL;
1068 1069 1070 1071
    }
    vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
                                                     vdev);
}
A
aliguori 已提交
1072

A
Avi Kivity 已提交
1073
hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
1074 1075 1076 1077
{
    return vdev->vq[n].vring.desc;
}

A
Avi Kivity 已提交
1078
hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
1079 1080 1081 1082
{
    return vdev->vq[n].vring.avail;
}

A
Avi Kivity 已提交
1083
hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
1084 1085 1086 1087
{
    return vdev->vq[n].vring.used;
}

A
Avi Kivity 已提交
1088
hwaddr virtio_queue_get_ring_addr(VirtIODevice *vdev, int n)
1089 1090 1091 1092
{
    return vdev->vq[n].vring.desc;
}

A
Avi Kivity 已提交
1093
hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
1094 1095 1096 1097
{
    return sizeof(VRingDesc) * vdev->vq[n].vring.num;
}

A
Avi Kivity 已提交
1098
hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
1099 1100
{
    return offsetof(VRingAvail, ring) +
1101
        sizeof(uint64_t) * vdev->vq[n].vring.num;
1102 1103
}

A
Avi Kivity 已提交
1104
hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
1105 1106 1107 1108 1109
{
    return offsetof(VRingUsed, ring) +
        sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
}

A
Avi Kivity 已提交
1110
hwaddr virtio_queue_get_ring_size(VirtIODevice *vdev, int n)
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
{
    return vdev->vq[n].vring.used - vdev->vq[n].vring.desc +
	    virtio_queue_get_used_size(vdev, n);
}

uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
{
    return vdev->vq[n].last_avail_idx;
}

void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
{
    vdev->vq[n].last_avail_idx = idx;
}

1126 1127 1128 1129 1130
void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
{
    vdev->vq[n].signalled_used_valid = false;
}

1131 1132 1133 1134 1135
VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
{
    return vdev->vq + n;
}

1136 1137 1138 1139 1140
uint16_t virtio_get_queue_index(VirtQueue *vq)
{
    return vq->queue_index;
}

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
static void virtio_queue_guest_notifier_read(EventNotifier *n)
{
    VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
    if (event_notifier_test_and_clear(n)) {
        virtio_irq(vq);
    }
}

void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
                                                bool with_irqfd)
{
    if (assign && !with_irqfd) {
        event_notifier_set_handler(&vq->guest_notifier,
                                   virtio_queue_guest_notifier_read);
    } else {
        event_notifier_set_handler(&vq->guest_notifier, NULL);
    }
    if (!assign) {
        /* Test and clear notifier before closing it,
         * in case poll callback didn't have time to run. */
        virtio_queue_guest_notifier_read(&vq->guest_notifier);
    }
}

1165 1166 1167 1168
EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
{
    return &vq->guest_notifier;
}
1169 1170 1171 1172 1173 1174 1175 1176 1177

static void virtio_queue_host_notifier_read(EventNotifier *n)
{
    VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
    if (event_notifier_test_and_clear(n)) {
        virtio_queue_notify_vq(vq);
    }
}

P
Paolo Bonzini 已提交
1178 1179
void virtio_queue_set_host_notifier_fd_handler(VirtQueue *vq, bool assign,
                                               bool set_handler)
1180
{
P
Paolo Bonzini 已提交
1181
    if (assign && set_handler) {
1182 1183 1184 1185
        event_notifier_set_handler(&vq->host_notifier,
                                   virtio_queue_host_notifier_read);
    } else {
        event_notifier_set_handler(&vq->host_notifier, NULL);
P
Paolo Bonzini 已提交
1186 1187
    }
    if (!assign) {
1188 1189 1190 1191 1192 1193
        /* Test and clear notifier before after disabling event,
         * in case poll callback didn't have time to run. */
        virtio_queue_host_notifier_read(&vq->host_notifier);
    }
}

1194 1195 1196 1197
EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
{
    return &vq->host_notifier;
}
1198

1199 1200
void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
{
1201
    g_free(vdev->bus_name);
1202
    vdev->bus_name = g_strdup(bus_name);
1203 1204
}

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
static void virtio_device_realize(DeviceState *dev, Error **errp)
{
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
    Error *err = NULL;

    if (vdc->realize != NULL) {
        vdc->realize(dev, &err);
        if (err != NULL) {
            error_propagate(errp, err);
            return;
        }
1217
    }
1218
    virtio_bus_device_plugged(vdev);
1219 1220
}

1221
static void virtio_device_unrealize(DeviceState *dev, Error **errp)
1222
{
1223
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1224 1225
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
    Error *err = NULL;
1226

1227 1228
    virtio_bus_device_unplugged(vdev);

1229 1230 1231 1232 1233 1234
    if (vdc->unrealize != NULL) {
        vdc->unrealize(dev, &err);
        if (err != NULL) {
            error_propagate(errp, err);
            return;
        }
1235
    }
1236

1237 1238
    g_free(vdev->bus_name);
    vdev->bus_name = NULL;
1239 1240
}

1241 1242 1243 1244
static void virtio_device_class_init(ObjectClass *klass, void *data)
{
    /* Set the default value here. */
    DeviceClass *dc = DEVICE_CLASS(klass);
1245 1246 1247

    dc->realize = virtio_device_realize;
    dc->unrealize = virtio_device_unrealize;
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
    dc->bus_type = TYPE_VIRTIO_BUS;
}

static const TypeInfo virtio_device_info = {
    .name = TYPE_VIRTIO_DEVICE,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(VirtIODevice),
    .class_init = virtio_device_class_init,
    .abstract = true,
    .class_size = sizeof(VirtioDeviceClass),
};

static void virtio_register_types(void)
{
    type_register_static(&virtio_device_info);
}

type_init(virtio_register_types)