virtio-pci.c 80.7 KB
Newer Older
P
Paul Brook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Virtio PCI Bindings
 *
 * Copyright IBM, Corp. 2007
 * Copyright (c) 2009 CodeSourcery
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *  Paul Brook        <paul@codesourcery.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
14 15
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
P
Paul Brook 已提交
16 17 18 19
 */

#include <inttypes.h>

20
#include "standard-headers/linux/virtio_pci.h"
P
Paolo Bonzini 已提交
21 22 23 24 25 26
#include "hw/virtio/virtio.h"
#include "hw/virtio/virtio-blk.h"
#include "hw/virtio/virtio-net.h"
#include "hw/virtio/virtio-serial.h"
#include "hw/virtio/virtio-scsi.h"
#include "hw/virtio/virtio-balloon.h"
27
#include "hw/virtio/virtio-input.h"
28
#include "hw/pci/pci.h"
29
#include "qemu/error-report.h"
30 31 32
#include "hw/pci/msi.h"
#include "hw/pci/msix.h"
#include "hw/loader.h"
33
#include "sysemu/kvm.h"
34
#include "sysemu/block-backend.h"
35
#include "virtio-pci.h"
36
#include "qemu/range.h"
P
Paolo Bonzini 已提交
37
#include "hw/virtio/virtio-bus.h"
38
#include "qapi/visitor.h"
P
Paul Brook 已提交
39

40
#define VIRTIO_PCI_REGION_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
41

42 43
#undef VIRTIO_PCI_CONFIG

44 45
/* The remaining space is defined by each driver as the per-driver
 * configuration space */
46
#define VIRTIO_PCI_CONFIG_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev))
P
Paul Brook 已提交
47

48 49
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtIOPCIProxy *dev);
50

P
Paul Brook 已提交
51
/* virtio device */
52 53 54 55 56
/* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
{
    return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
}
P
Paul Brook 已提交
57

58 59 60 61
/* DeviceState to VirtIOPCIProxy. Note: used on datapath,
 * be careful and test performance if you change this.
 */
static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
P
Paul Brook 已提交
62
{
63 64 65 66 67 68
    return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
}

static void virtio_pci_notify(DeviceState *d, uint16_t vector)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
P
Paolo Bonzini 已提交
69

70 71
    if (msix_enabled(&proxy->pci_dev))
        msix_notify(&proxy->pci_dev, vector);
P
Paolo Bonzini 已提交
72 73 74 75
    else {
        VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
        pci_set_irq(&proxy->pci_dev, vdev->isr & 1);
    }
P
Paul Brook 已提交
76 77
}

78
static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
79
{
80
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
81 82
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

83 84 85
    pci_device_save(&proxy->pci_dev, f);
    msix_save(&proxy->pci_dev, f);
    if (msix_present(&proxy->pci_dev))
P
Paolo Bonzini 已提交
86
        qemu_put_be16(f, vdev->config_vector);
87 88
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static void virtio_pci_load_modern_queue_state(VirtIOPCIQueue *vq,
                                               QEMUFile *f)
{
    vq->num = qemu_get_be16(f);
    vq->enabled = qemu_get_be16(f);
    vq->desc[0] = qemu_get_be32(f);
    vq->desc[1] = qemu_get_be32(f);
    vq->avail[0] = qemu_get_be32(f);
    vq->avail[1] = qemu_get_be32(f);
    vq->used[0] = qemu_get_be32(f);
    vq->used[1] = qemu_get_be32(f);
}

static bool virtio_pci_has_extra_state(DeviceState *d)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
}

static int get_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
{
    VirtIOPCIProxy *proxy = pv;
    int i;

    proxy->dfselect = qemu_get_be32(f);
    proxy->gfselect = qemu_get_be32(f);
    proxy->guest_features[0] = qemu_get_be32(f);
    proxy->guest_features[1] = qemu_get_be32(f);
    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        virtio_pci_load_modern_queue_state(&proxy->vqs[i], f);
    }

    return 0;
}

static void virtio_pci_save_modern_queue_state(VirtIOPCIQueue *vq,
                                               QEMUFile *f)
{
    qemu_put_be16(f, vq->num);
    qemu_put_be16(f, vq->enabled);
    qemu_put_be32(f, vq->desc[0]);
    qemu_put_be32(f, vq->desc[1]);
    qemu_put_be32(f, vq->avail[0]);
    qemu_put_be32(f, vq->avail[1]);
    qemu_put_be32(f, vq->used[0]);
    qemu_put_be32(f, vq->used[1]);
}

static void put_virtio_pci_modern_state(QEMUFile *f, void *pv, size_t size)
{
    VirtIOPCIProxy *proxy = pv;
    int i;

    qemu_put_be32(f, proxy->dfselect);
    qemu_put_be32(f, proxy->gfselect);
    qemu_put_be32(f, proxy->guest_features[0]);
    qemu_put_be32(f, proxy->guest_features[1]);
    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        virtio_pci_save_modern_queue_state(&proxy->vqs[i], f);
    }
}

static const VMStateInfo vmstate_info_virtio_pci_modern_state = {
    .name = "virtqueue_state",
    .get = get_virtio_pci_modern_state,
    .put = put_virtio_pci_modern_state,
};

static bool virtio_pci_modern_state_needed(void *opaque)
{
    VirtIOPCIProxy *proxy = opaque;

    return !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
}

static const VMStateDescription vmstate_virtio_pci_modern_state = {
    .name = "virtio_pci/modern_state",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = &virtio_pci_modern_state_needed,
    .fields = (VMStateField[]) {
        {
            .name         = "modern_state",
            .version_id   = 0,
            .field_exists = NULL,
            .size         = 0,
            .info         = &vmstate_info_virtio_pci_modern_state,
            .flags        = VMS_SINGLE,
            .offset       = 0,
        },
        VMSTATE_END_OF_LIST()
    }
};

static const VMStateDescription vmstate_virtio_pci = {
    .name = "virtio_pci",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields = (VMStateField[]) {
        VMSTATE_END_OF_LIST()
    },
    .subsections = (const VMStateDescription*[]) {
        &vmstate_virtio_pci_modern_state,
        NULL
    }
};

static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL);
}

static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1);
}

212
static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
213
{
214
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
215 216
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

217
    if (msix_present(&proxy->pci_dev))
P
Paolo Bonzini 已提交
218
        qemu_put_be16(f, virtio_queue_vector(vdev, n));
219 220
}

221
static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
222
{
223
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
224 225
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

226 227
    int ret;
    ret = pci_device_load(&proxy->pci_dev, f);
228
    if (ret) {
229
        return ret;
230
    }
231
    msix_unuse_all_vectors(&proxy->pci_dev);
232
    msix_load(&proxy->pci_dev, f);
233
    if (msix_present(&proxy->pci_dev)) {
P
Paolo Bonzini 已提交
234
        qemu_get_be16s(f, &vdev->config_vector);
235
    } else {
P
Paolo Bonzini 已提交
236
        vdev->config_vector = VIRTIO_NO_VECTOR;
237
    }
P
Paolo Bonzini 已提交
238 239
    if (vdev->config_vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
240
    }
241 242 243
    return 0;
}

244
static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
245
{
246
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
247 248
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

249
    uint16_t vector;
250 251 252 253 254
    if (msix_present(&proxy->pci_dev)) {
        qemu_get_be16s(f, &vector);
    } else {
        vector = VIRTIO_NO_VECTOR;
    }
P
Paolo Bonzini 已提交
255
    virtio_queue_set_vector(vdev, n, vector);
256 257 258
    if (vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, vector);
    }
259

260 261 262
    return 0;
}

263 264
#define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000

265
static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
P
Paolo Bonzini 已提交
266
                                                 int n, bool assign, bool set_handler)
267
{
P
Paolo Bonzini 已提交
268 269
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
270
    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
271 272
    bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
273
    bool fast_mmio = kvm_ioeventfd_any_length_enabled();
274
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
275
    MemoryRegion *modern_mr = &proxy->notify.mr;
276
    MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
277 278 279 280
    MemoryRegion *legacy_mr = &proxy->bar;
    hwaddr modern_addr = QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
                         virtio_get_queue_index(vq);
    hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
A
Avi Kivity 已提交
281 282
    int r = 0;

283 284 285
    if (assign) {
        r = event_notifier_init(notifier, 1);
        if (r < 0) {
286 287
            error_report("%s: unable to init event notifier: %d",
                         __func__, r);
288 289
            return r;
        }
P
Paolo Bonzini 已提交
290
        virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
291
        if (modern) {
292 293 294 295 296 297 298
            if (fast_mmio) {
                memory_region_add_eventfd(modern_mr, modern_addr, 0,
                                          false, n, notifier);
            } else {
                memory_region_add_eventfd(modern_mr, modern_addr, 2,
                                          false, n, notifier);
            }
299 300 301 302
            if (modern_pio) {
                memory_region_add_eventfd(modern_notify_mr, 0, 2,
                                              true, n, notifier);
            }
303 304 305 306 307
        }
        if (legacy) {
            memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
                                      true, n, notifier);
        }
308
    } else {
309
        if (modern) {
310 311 312 313 314 315 316
            if (fast_mmio) {
                memory_region_del_eventfd(modern_mr, modern_addr, 0,
                                          false, n, notifier);
            } else {
                memory_region_del_eventfd(modern_mr, modern_addr, 2,
                                          false, n, notifier);
            }
317 318 319 320
            if (modern_pio) {
                memory_region_del_eventfd(modern_notify_mr, 0, 2,
                                          true, n, notifier);
            }
321 322 323 324 325
        }
        if (legacy) {
            memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
                                      true, n, notifier);
        }
P
Paolo Bonzini 已提交
326
        virtio_queue_set_host_notifier_fd_handler(vq, false, false);
327 328 329 330 331
        event_notifier_cleanup(notifier);
    }
    return r;
}

332
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
333
{
P
Paolo Bonzini 已提交
334
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
335 336 337 338 339
    int n, r;

    if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
        proxy->ioeventfd_disabled ||
        proxy->ioeventfd_started) {
340
        return;
341 342
    }

343
    for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
P
Paolo Bonzini 已提交
344
        if (!virtio_queue_get_num(vdev, n)) {
345 346 347
            continue;
        }

P
Paolo Bonzini 已提交
348
        r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
349 350 351 352 353
        if (r < 0) {
            goto assign_error;
        }
    }
    proxy->ioeventfd_started = true;
354
    return;
355 356 357

assign_error:
    while (--n >= 0) {
P
Paolo Bonzini 已提交
358
        if (!virtio_queue_get_num(vdev, n)) {
359 360 361
            continue;
        }

P
Paolo Bonzini 已提交
362
        r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
363
        assert(r >= 0);
364 365
    }
    proxy->ioeventfd_started = false;
366
    error_report("%s: failed. Fallback to a userspace (slower).", __func__);
367 368
}

369
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
370
{
P
Paolo Bonzini 已提交
371
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
372
    int r;
373 374 375
    int n;

    if (!proxy->ioeventfd_started) {
376
        return;
377 378
    }

379
    for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
P
Paolo Bonzini 已提交
380
        if (!virtio_queue_get_num(vdev, n)) {
381 382 383
            continue;
        }

P
Paolo Bonzini 已提交
384
        r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
385
        assert(r >= 0);
386 387 388 389
    }
    proxy->ioeventfd_started = false;
}

P
Paul Brook 已提交
390 391 392
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
P
Paolo Bonzini 已提交
393
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
A
Avi Kivity 已提交
394
    hwaddr pa;
P
Paul Brook 已提交
395 396 397

    switch (addr) {
    case VIRTIO_PCI_GUEST_FEATURES:
398 399 400 401
        /* Guest does not negotiate properly?  We have to assume nothing. */
        if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
            val = virtio_bus_get_vdev_bad_features(&proxy->bus);
        }
402
        virtio_set_features(vdev, val);
P
Paul Brook 已提交
403 404
        break;
    case VIRTIO_PCI_QUEUE_PFN:
A
Avi Kivity 已提交
405
        pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
406
        if (pa == 0) {
407
            virtio_pci_stop_ioeventfd(proxy);
P
Paolo Bonzini 已提交
408
            virtio_reset(vdev);
409 410
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
411 412
        else
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
P
Paul Brook 已提交
413 414
        break;
    case VIRTIO_PCI_QUEUE_SEL:
415
        if (val < VIRTIO_QUEUE_MAX)
P
Paul Brook 已提交
416 417 418
            vdev->queue_sel = val;
        break;
    case VIRTIO_PCI_QUEUE_NOTIFY:
419
        if (val < VIRTIO_QUEUE_MAX) {
420 421
            virtio_queue_notify(vdev, val);
        }
P
Paul Brook 已提交
422 423
        break;
    case VIRTIO_PCI_STATUS:
424 425 426 427
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
            virtio_pci_stop_ioeventfd(proxy);
        }

428
        virtio_set_status(vdev, val & 0xFF);
429 430 431 432 433

        if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
            virtio_pci_start_ioeventfd(proxy);
        }

434
        if (vdev->status == 0) {
P
Paolo Bonzini 已提交
435
            virtio_reset(vdev);
436 437
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
438

439 440 441 442 443 444 445 446 447
        /* Linux before 2.6.34 drives the device without enabling
           the PCI device bus master bit. Enable it automatically
           for the guest. This is a PCI spec violation but so is
           initiating DMA with bus master bit clear. */
        if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) {
            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
                                     proxy->pci_dev.config[PCI_COMMAND] |
                                     PCI_COMMAND_MASTER, 1);
        }
P
Paul Brook 已提交
448
        break;
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    case VIRTIO_MSI_CONFIG_VECTOR:
        msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
        /* Make it possible for guest to discover an error took place. */
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
            val = VIRTIO_NO_VECTOR;
        vdev->config_vector = val;
        break;
    case VIRTIO_MSI_QUEUE_VECTOR:
        msix_vector_unuse(&proxy->pci_dev,
                          virtio_queue_vector(vdev, vdev->queue_sel));
        /* Make it possible for guest to discover an error took place. */
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
            val = VIRTIO_NO_VECTOR;
        virtio_queue_set_vector(vdev, vdev->queue_sel, val);
        break;
    default:
465 466
        error_report("%s: unexpected address 0x%x value 0x%x",
                     __func__, addr, val);
467
        break;
P
Paul Brook 已提交
468 469 470
    }
}

471
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
P
Paul Brook 已提交
472
{
P
Paolo Bonzini 已提交
473
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
P
Paul Brook 已提交
474 475 476 477
    uint32_t ret = 0xFFFFFFFF;

    switch (addr) {
    case VIRTIO_PCI_HOST_FEATURES:
C
Cornelia Huck 已提交
478
        ret = vdev->host_features;
P
Paul Brook 已提交
479 480
        break;
    case VIRTIO_PCI_GUEST_FEATURES:
481
        ret = vdev->guest_features;
P
Paul Brook 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
        break;
    case VIRTIO_PCI_QUEUE_PFN:
        ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
              >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
        break;
    case VIRTIO_PCI_QUEUE_NUM:
        ret = virtio_queue_get_num(vdev, vdev->queue_sel);
        break;
    case VIRTIO_PCI_QUEUE_SEL:
        ret = vdev->queue_sel;
        break;
    case VIRTIO_PCI_STATUS:
        ret = vdev->status;
        break;
    case VIRTIO_PCI_ISR:
        /* reading from the ISR also clears it. */
        ret = vdev->isr;
        vdev->isr = 0;
500
        pci_irq_deassert(&proxy->pci_dev);
P
Paul Brook 已提交
501
        break;
502 503 504 505 506 507
    case VIRTIO_MSI_CONFIG_VECTOR:
        ret = vdev->config_vector;
        break;
    case VIRTIO_MSI_QUEUE_VECTOR:
        ret = virtio_queue_vector(vdev, vdev->queue_sel);
        break;
P
Paul Brook 已提交
508 509 510 511 512 513 514
    default:
        break;
    }

    return ret;
}

515 516
static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
                                       unsigned size)
P
Paul Brook 已提交
517 518
{
    VirtIOPCIProxy *proxy = opaque;
P
Paolo Bonzini 已提交
519
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
520
    uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
521
    uint64_t val = 0;
522
    if (addr < config) {
523
        return virtio_ioport_read(proxy, addr);
524 525
    }
    addr -= config;
P
Paul Brook 已提交
526

527 528
    switch (size) {
    case 1:
P
Paolo Bonzini 已提交
529
        val = virtio_config_readb(vdev, addr);
530 531
        break;
    case 2:
P
Paolo Bonzini 已提交
532
        val = virtio_config_readw(vdev, addr);
533
        if (virtio_is_big_endian(vdev)) {
534 535
            val = bswap16(val);
        }
536 537
        break;
    case 4:
P
Paolo Bonzini 已提交
538
        val = virtio_config_readl(vdev, addr);
539
        if (virtio_is_big_endian(vdev)) {
540 541
            val = bswap32(val);
        }
542
        break;
543
    }
544
    return val;
P
Paul Brook 已提交
545 546
}

547 548
static void virtio_pci_config_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned size)
P
Paul Brook 已提交
549 550
{
    VirtIOPCIProxy *proxy = opaque;
551
    uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
P
Paolo Bonzini 已提交
552
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
553 554 555 556 557
    if (addr < config) {
        virtio_ioport_write(proxy, addr, val);
        return;
    }
    addr -= config;
558 559 560 561 562 563
    /*
     * Virtio-PCI is odd. Ioports are LE but config space is target native
     * endian.
     */
    switch (size) {
    case 1:
P
Paolo Bonzini 已提交
564
        virtio_config_writeb(vdev, addr, val);
565 566
        break;
    case 2:
567
        if (virtio_is_big_endian(vdev)) {
568 569
            val = bswap16(val);
        }
P
Paolo Bonzini 已提交
570
        virtio_config_writew(vdev, addr, val);
571 572
        break;
    case 4:
573
        if (virtio_is_big_endian(vdev)) {
574 575
            val = bswap32(val);
        }
P
Paolo Bonzini 已提交
576
        virtio_config_writel(vdev, addr, val);
577
        break;
578
    }
P
Paul Brook 已提交
579 580
}

A
Avi Kivity 已提交
581
static const MemoryRegionOps virtio_pci_config_ops = {
582 583 584 585 586 587
    .read = virtio_pci_config_read,
    .write = virtio_pci_config_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 4,
    },
588
    .endianness = DEVICE_LITTLE_ENDIAN,
A
Avi Kivity 已提交
589
};
590

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
/* Below are generic functions to do memcpy from/to an address space,
 * without byteswaps, with input validation.
 *
 * As regular address_space_* APIs all do some kind of byteswap at least for
 * some host/target combinations, we are forced to explicitly convert to a
 * known-endianness integer value.
 * It doesn't really matter which endian format to go through, so the code
 * below selects the endian that causes the least amount of work on the given
 * host.
 *
 * Note: host pointer must be aligned.
 */
static
void virtio_address_space_write(AddressSpace *as, hwaddr addr,
                                const uint8_t *buf, int len)
{
    uint32_t val;

    /* address_space_* APIs assume an aligned address.
     * As address is under guest control, handle illegal values.
     */
    addr &= ~(len - 1);

    /* Make sure caller aligned buf properly */
    assert(!(((uintptr_t)buf) & (len - 1)));

    switch (len) {
    case 1:
        val = pci_get_byte(buf);
        address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
        break;
    case 2:
        val = pci_get_word(buf);
        address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
        break;
    case 4:
        val = pci_get_long(buf);
        address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
        break;
    default:
        /* As length is under guest control, handle illegal values. */
        break;
    }
}

static void
virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
{
    uint32_t val;

    /* address_space_* APIs assume an aligned address.
     * As address is under guest control, handle illegal values.
     */
    addr &= ~(len - 1);

    /* Make sure caller aligned buf properly */
    assert(!(((uintptr_t)buf) & (len - 1)));

    switch (len) {
    case 1:
        val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
        pci_set_byte(buf, val);
        break;
    case 2:
        val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
        pci_set_word(buf, val);
        break;
    case 4:
        val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
        pci_set_long(buf, val);
        break;
    default:
        /* As length is under guest control, handle illegal values. */
        break;
    }
}

668 669 670
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
                                uint32_t val, int len)
{
671
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
P
Paolo Bonzini 已提交
672
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
673
    struct virtio_pci_cfg_cap *cfg;
674

675 676 677
    pci_default_write_config(pci_dev, address, val, len);

    if (range_covers_byte(address, len, PCI_COMMAND) &&
678
        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
679
        virtio_pci_stop_ioeventfd(proxy);
680
        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
681
    }
682 683 684 685 686 687 688 689 690 691 692 693

    if (proxy->config_cap &&
        ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
                                                                  pci_cfg_data),
                       sizeof cfg->pci_cfg_data)) {
        uint32_t off;
        uint32_t len;

        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
        off = le32_to_cpu(cfg->cap.offset);
        len = le32_to_cpu(cfg->cap.length);

694 695
        if (len == 1 || len == 2 || len == 4) {
            assert(len <= sizeof cfg->pci_cfg_data);
696 697
            virtio_address_space_write(&proxy->modern_as, off,
                                       cfg->pci_cfg_data, len);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
        }
    }
}

static uint32_t virtio_read_config(PCIDevice *pci_dev,
                                   uint32_t address, int len)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    struct virtio_pci_cfg_cap *cfg;

    if (proxy->config_cap &&
        ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
                                                                  pci_cfg_data),
                       sizeof cfg->pci_cfg_data)) {
        uint32_t off;
        uint32_t len;

        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
        off = le32_to_cpu(cfg->cap.offset);
        len = le32_to_cpu(cfg->cap.length);

719 720
        if (len == 1 || len == 2 || len == 4) {
            assert(len <= sizeof cfg->pci_cfg_data);
721 722
            virtio_address_space_read(&proxy->modern_as, off,
                                      cfg->pci_cfg_data, len);
723 724 725 726
        }
    }

    return pci_default_read_config(pci_dev, address, len);
P
Paul Brook 已提交
727 728
}

729 730 731 732 733 734
static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
                                        unsigned int queue_no,
                                        unsigned int vector,
                                        MSIMessage msg)
{
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
735
    int ret;
736 737

    if (irqfd->users == 0) {
738
        ret = kvm_irqchip_add_msi_route(kvm_state, msg, &proxy->pci_dev);
739 740 741 742 743 744 745 746 747 748 749
        if (ret < 0) {
            return ret;
        }
        irqfd->virq = ret;
    }
    irqfd->users++;
    return 0;
}

static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
                                             unsigned int vector)
750 751 752 753 754 755 756
{
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
    if (--irqfd->users == 0) {
        kvm_irqchip_release_virq(kvm_state, irqfd->virq);
    }
}

757 758 759 760 761
static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
                                 unsigned int queue_no,
                                 unsigned int vector)
{
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
P
Paolo Bonzini 已提交
762 763
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
764 765
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
    int ret;
766
    ret = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
767 768 769 770 771 772
    return ret;
}

static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
                                      unsigned int queue_no,
                                      unsigned int vector)
773
{
P
Paolo Bonzini 已提交
774 775
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
776
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
777
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
778
    int ret;
779

780
    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
781
    assert(ret == 0);
782
}
783

784 785 786
static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
{
    PCIDevice *dev = &proxy->pci_dev;
P
Paolo Bonzini 已提交
787
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
788
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
    unsigned int vector;
    int ret, queue_no;
    MSIMessage msg;

    for (queue_no = 0; queue_no < nvqs; queue_no++) {
        if (!virtio_queue_get_num(vdev, queue_no)) {
            break;
        }
        vector = virtio_queue_vector(vdev, queue_no);
        if (vector >= msix_nr_vectors_allocated(dev)) {
            continue;
        }
        msg = msix_get_message(dev, vector);
        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
        if (ret < 0) {
            goto undo;
805
        }
806 807 808
        /* If guest supports masking, set up irqfd now.
         * Otherwise, delay until unmasked in the frontend.
         */
809
        if (k->guest_notifier_mask) {
810 811 812 813 814 815
            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
            if (ret < 0) {
                kvm_virtio_pci_vq_vector_release(proxy, vector);
                goto undo;
            }
        }
816 817
    }
    return 0;
818 819 820 821 822 823 824

undo:
    while (--queue_no >= 0) {
        vector = virtio_queue_vector(vdev, queue_no);
        if (vector >= msix_nr_vectors_allocated(dev)) {
            continue;
        }
825
        if (k->guest_notifier_mask) {
826
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
827
        }
828 829 830
        kvm_virtio_pci_vq_vector_release(proxy, vector);
    }
    return ret;
831 832
}

833 834 835
static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
{
    PCIDevice *dev = &proxy->pci_dev;
P
Paolo Bonzini 已提交
836
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
837 838
    unsigned int vector;
    int queue_no;
839
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
840 841 842 843 844 845 846 847 848

    for (queue_no = 0; queue_no < nvqs; queue_no++) {
        if (!virtio_queue_get_num(vdev, queue_no)) {
            break;
        }
        vector = virtio_queue_vector(vdev, queue_no);
        if (vector >= msix_nr_vectors_allocated(dev)) {
            continue;
        }
849 850 851
        /* If guest supports masking, clean up irqfd now.
         * Otherwise, it was cleaned when masked in the frontend.
         */
852
        if (k->guest_notifier_mask) {
853
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
854
        }
855 856 857 858
        kvm_virtio_pci_vq_vector_release(proxy, vector);
    }
}

859 860 861 862
static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                       unsigned int queue_no,
                                       unsigned int vector,
                                       MSIMessage msg)
863
{
P
Paolo Bonzini 已提交
864 865 866
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
867
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
868
    VirtIOIRQFD *irqfd;
869
    int ret = 0;
870

871 872 873
    if (proxy->vector_irqfd) {
        irqfd = &proxy->vector_irqfd[vector];
        if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
874 875
            ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
                                               &proxy->pci_dev);
876 877 878
            if (ret < 0) {
                return ret;
            }
879 880 881
        }
    }

882 883 884
    /* If guest supports masking, irqfd is already setup, unmask it.
     * Otherwise, set it up now.
     */
885
    if (k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
886
        k->guest_notifier_mask(vdev, queue_no, false);
887
        /* Test after unmasking to avoid losing events. */
888
        if (k->guest_notifier_pending &&
P
Paolo Bonzini 已提交
889
            k->guest_notifier_pending(vdev, queue_no)) {
890 891 892 893
            event_notifier_set(n);
        }
    } else {
        ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
894
    }
895
    return ret;
896 897
}

898
static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
899 900 901
                                             unsigned int queue_no,
                                             unsigned int vector)
{
P
Paolo Bonzini 已提交
902 903
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
904

905 906 907
    /* If guest supports masking, keep irqfd but mask it.
     * Otherwise, clean it up now.
     */ 
908
    if (k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
909
        k->guest_notifier_mask(vdev, queue_no, true);
910
    } else {
911
        kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
912
    }
913 914
}

915 916
static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
                                    MSIMessage msg)
917 918
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
919
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
920 921
    VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
    int ret, index, unmasked = 0;
922

923 924 925
    while (vq) {
        index = virtio_get_queue_index(vq);
        if (!virtio_queue_get_num(vdev, index)) {
926 927
            break;
        }
928 929 930 931 932 933
        if (index < proxy->nvqs_with_notifiers) {
            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
            if (ret < 0) {
                goto undo;
            }
            ++unmasked;
934
        }
935
        vq = virtio_vector_next_queue(vq);
936
    }
937

938 939 940
    return 0;

undo:
941
    vq = virtio_vector_first_queue(vdev, vector);
942
    while (vq && unmasked >= 0) {
943
        index = virtio_get_queue_index(vq);
944 945 946 947
        if (index < proxy->nvqs_with_notifiers) {
            virtio_pci_vq_vector_mask(proxy, index, vector);
            --unmasked;
        }
948
        vq = virtio_vector_next_queue(vq);
949 950 951 952
    }
    return ret;
}

953
static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
954 955
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
956
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
957 958
    VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
    int index;
959

960 961 962
    while (vq) {
        index = virtio_get_queue_index(vq);
        if (!virtio_queue_get_num(vdev, index)) {
963 964
            break;
        }
965 966 967
        if (index < proxy->nvqs_with_notifiers) {
            virtio_pci_vq_vector_mask(proxy, index, vector);
        }
968
        vq = virtio_vector_next_queue(vq);
969 970 971
    }
}

972 973 974
static void virtio_pci_vector_poll(PCIDevice *dev,
                                   unsigned int vector_start,
                                   unsigned int vector_end)
975 976
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
977
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
978
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
979 980 981 982 983
    int queue_no;
    unsigned int vector;
    EventNotifier *notifier;
    VirtQueue *vq;

984
    for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
985 986 987 988 989 990 991 992 993 994
        if (!virtio_queue_get_num(vdev, queue_no)) {
            break;
        }
        vector = virtio_queue_vector(vdev, queue_no);
        if (vector < vector_start || vector >= vector_end ||
            !msix_is_masked(dev, vector)) {
            continue;
        }
        vq = virtio_get_queue(vdev, queue_no);
        notifier = virtio_queue_get_guest_notifier(vq);
995 996
        if (k->guest_notifier_pending) {
            if (k->guest_notifier_pending(vdev, queue_no)) {
997 998 999
                msix_set_pending(dev, vector);
            }
        } else if (event_notifier_test_and_clear(notifier)) {
1000 1001 1002 1003 1004 1005 1006
            msix_set_pending(dev, vector);
        }
    }
}

static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
                                         bool with_irqfd)
1007
{
1008
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
1009 1010 1011
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
    VirtQueue *vq = virtio_get_queue(vdev, n);
1012 1013 1014 1015 1016 1017 1018
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);

    if (assign) {
        int r = event_notifier_init(notifier, 0);
        if (r < 0) {
            return r;
        }
1019
        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1020
    } else {
1021
        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1022 1023 1024
        event_notifier_cleanup(notifier);
    }

1025
    if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
P
Paolo Bonzini 已提交
1026
        vdc->guest_notifier_mask(vdev, n, !assign);
1027 1028
    }

1029 1030 1031
    return 0;
}

1032
static bool virtio_pci_query_guest_notifiers(DeviceState *d)
1033
{
1034
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1035 1036 1037
    return msix_enabled(&proxy->pci_dev);
}

1038
static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
1039
{
1040
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
1041
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1042
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1043
    int r, n;
1044 1045
    bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
        kvm_msi_via_irqfd_enabled();
1046

1047
    nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
1048 1049 1050 1051 1052 1053 1054 1055

    /* When deassigning, pass a consistent nvqs value
     * to avoid leaking notifiers.
     */
    assert(assign || nvqs == proxy->nvqs_with_notifiers);

    proxy->nvqs_with_notifiers = nvqs;

1056
    /* Must unset vector notifier while guest notifier is still assigned */
1057
    if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
1058
        msix_unset_vector_notifiers(&proxy->pci_dev);
1059 1060 1061 1062 1063
        if (proxy->vector_irqfd) {
            kvm_virtio_pci_vector_release(proxy, nvqs);
            g_free(proxy->vector_irqfd);
            proxy->vector_irqfd = NULL;
        }
1064 1065
    }

1066
    for (n = 0; n < nvqs; n++) {
1067 1068 1069 1070
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }

1071
        r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
1072 1073 1074 1075 1076
        if (r < 0) {
            goto assign_error;
        }
    }

1077
    /* Must set vector notifier after guest notifier has been assigned */
1078
    if ((with_irqfd || k->guest_notifier_mask) && assign) {
1079 1080 1081 1082 1083 1084 1085 1086
        if (with_irqfd) {
            proxy->vector_irqfd =
                g_malloc0(sizeof(*proxy->vector_irqfd) *
                          msix_nr_vectors_allocated(&proxy->pci_dev));
            r = kvm_virtio_pci_vector_use(proxy, nvqs);
            if (r < 0) {
                goto assign_error;
            }
1087
        }
1088
        r = msix_set_vector_notifiers(&proxy->pci_dev,
1089 1090 1091
                                      virtio_pci_vector_unmask,
                                      virtio_pci_vector_mask,
                                      virtio_pci_vector_poll);
1092
        if (r < 0) {
1093
            goto notifiers_error;
1094 1095 1096
        }
    }

1097 1098
    return 0;

1099
notifiers_error:
1100 1101 1102 1103
    if (with_irqfd) {
        assert(assign);
        kvm_virtio_pci_vector_release(proxy, nvqs);
    }
1104

1105 1106
assign_error:
    /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1107
    assert(assign);
1108
    while (--n >= 0) {
1109
        virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1110 1111 1112 1113
    }
    return r;
}

1114
static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
1115
{
1116
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1117 1118 1119 1120 1121

    /* Stop using ioeventfd for virtqueue kick if the device starts using host
     * notifiers.  This makes it easy to avoid stepping on each others' toes.
     */
    proxy->ioeventfd_disabled = assign;
1122
    if (assign) {
1123 1124 1125 1126 1127 1128
        virtio_pci_stop_ioeventfd(proxy);
    }
    /* We don't need to start here: it's not needed because backend
     * currently only stops on status change away from ok,
     * reset, vmstop and such. If we do add code to start here,
     * need to check vmstate, device state etc. */
P
Paolo Bonzini 已提交
1129
    return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
1130 1131
}

1132
static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1133
{
1134
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
1135
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1136 1137

    if (running) {
1138 1139 1140 1141 1142
        /* Old QEMU versions did not set bus master enable on status write.
         * Detect DRIVER set and enable it.
         */
        if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) &&
            (vdev->status & VIRTIO_CONFIG_S_DRIVER) &&
1143
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1144 1145 1146
            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
                                     proxy->pci_dev.config[PCI_COMMAND] |
                                     PCI_COMMAND_MASTER, 1);
1147
        }
1148
        virtio_pci_start_ioeventfd(proxy);
1149
    } else {
1150
        virtio_pci_stop_ioeventfd(proxy);
1151 1152 1153
    }
}

1154
#ifdef CONFIG_VIRTFS
1155
static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1156
{
1157 1158
    V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1159

1160
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1161
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1162 1163
}

1164 1165 1166
static Property virtio_9p_pci_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1167 1168 1169 1170
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

1171
static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1172 1173
{
    DeviceClass *dc = DEVICE_CLASS(klass);
1174 1175
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1176

1177
    k->realize = virtio_9p_pci_realize;
1178 1179 1180 1181
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
    pcidev_k->class_id = 0x2;
1182
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1183
    dc->props = virtio_9p_pci_properties;
1184 1185
}

1186 1187 1188
static void virtio_9p_pci_instance_init(Object *obj)
{
    V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1189 1190 1191

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_9P);
1192 1193 1194 1195 1196 1197 1198 1199
}

static const TypeInfo virtio_9p_pci_info = {
    .name          = TYPE_VIRTIO_9P_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(V9fsPCIState),
    .instance_init = virtio_9p_pci_instance_init,
    .class_init    = virtio_9p_pci_class_init,
1200
};
1201
#endif /* CONFIG_VIRTFS */
1202

1203 1204 1205 1206
/*
 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
 */

1207 1208 1209 1210 1211 1212 1213
static int virtio_pci_query_nvectors(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);

    return proxy->nvectors;
}

1214
static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
                                   struct virtio_pci_cap *cap)
{
    PCIDevice *dev = &proxy->pci_dev;
    int offset;

    offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, cap->cap_len);
    assert(offset > 0);

    assert(cap->cap_len >= sizeof *cap);
    memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len,
           cap->cap_len - PCI_CAP_FLAGS);
1226 1227

    return offset;
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
}

static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr,
                                       unsigned size)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    uint32_t val = 0;
    int i;

    switch (addr) {
    case VIRTIO_PCI_COMMON_DFSELECT:
        val = proxy->dfselect;
        break;
    case VIRTIO_PCI_COMMON_DF:
        if (proxy->dfselect <= 1) {
1244 1245
            val = (vdev->host_features & ~VIRTIO_LEGACY_FEATURES) >>
                (32 * proxy->dfselect);
1246 1247 1248 1249 1250 1251
        }
        break;
    case VIRTIO_PCI_COMMON_GFSELECT:
        val = proxy->gfselect;
        break;
    case VIRTIO_PCI_COMMON_GF:
G
Gonglei 已提交
1252
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
            val = proxy->guest_features[proxy->gfselect];
        }
        break;
    case VIRTIO_PCI_COMMON_MSIX:
        val = vdev->config_vector;
        break;
    case VIRTIO_PCI_COMMON_NUMQ:
        for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) {
            if (virtio_queue_get_num(vdev, i)) {
                val = i + 1;
            }
        }
        break;
    case VIRTIO_PCI_COMMON_STATUS:
        val = vdev->status;
        break;
    case VIRTIO_PCI_COMMON_CFGGENERATION:
1270
        val = vdev->generation;
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
        break;
    case VIRTIO_PCI_COMMON_Q_SELECT:
        val = vdev->queue_sel;
        break;
    case VIRTIO_PCI_COMMON_Q_SIZE:
        val = virtio_queue_get_num(vdev, vdev->queue_sel);
        break;
    case VIRTIO_PCI_COMMON_Q_MSIX:
        val = virtio_queue_vector(vdev, vdev->queue_sel);
        break;
    case VIRTIO_PCI_COMMON_Q_ENABLE:
        val = proxy->vqs[vdev->queue_sel].enabled;
        break;
    case VIRTIO_PCI_COMMON_Q_NOFF:
        /* Simply map queues in order */
        val = vdev->queue_sel;
        break;
    case VIRTIO_PCI_COMMON_Q_DESCLO:
        val = proxy->vqs[vdev->queue_sel].desc[0];
        break;
    case VIRTIO_PCI_COMMON_Q_DESCHI:
        val = proxy->vqs[vdev->queue_sel].desc[1];
        break;
    case VIRTIO_PCI_COMMON_Q_AVAILLO:
        val = proxy->vqs[vdev->queue_sel].avail[0];
        break;
    case VIRTIO_PCI_COMMON_Q_AVAILHI:
        val = proxy->vqs[vdev->queue_sel].avail[1];
        break;
    case VIRTIO_PCI_COMMON_Q_USEDLO:
        val = proxy->vqs[vdev->queue_sel].used[0];
        break;
    case VIRTIO_PCI_COMMON_Q_USEDHI:
        val = proxy->vqs[vdev->queue_sel].used[1];
        break;
    default:
        val = 0;
    }

    return val;
}

static void virtio_pci_common_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned size)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

    switch (addr) {
    case VIRTIO_PCI_COMMON_DFSELECT:
        proxy->dfselect = val;
        break;
    case VIRTIO_PCI_COMMON_GFSELECT:
        proxy->gfselect = val;
        break;
    case VIRTIO_PCI_COMMON_GF:
G
Gonglei 已提交
1327
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
            proxy->guest_features[proxy->gfselect] = val;
            virtio_set_features(vdev,
                                (((uint64_t)proxy->guest_features[1]) << 32) |
                                proxy->guest_features[0]);
        }
        break;
    case VIRTIO_PCI_COMMON_MSIX:
        msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
        /* Make it possible for guest to discover an error took place. */
        if (msix_vector_use(&proxy->pci_dev, val) < 0) {
            val = VIRTIO_NO_VECTOR;
        }
        vdev->config_vector = val;
        break;
    case VIRTIO_PCI_COMMON_STATUS:
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
            virtio_pci_stop_ioeventfd(proxy);
        }

        virtio_set_status(vdev, val & 0xFF);

        if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
            virtio_pci_start_ioeventfd(proxy);
        }

        if (vdev->status == 0) {
            virtio_reset(vdev);
            msix_unuse_all_vectors(&proxy->pci_dev);
        }

        break;
    case VIRTIO_PCI_COMMON_Q_SELECT:
        if (val < VIRTIO_QUEUE_MAX) {
            vdev->queue_sel = val;
        }
        break;
    case VIRTIO_PCI_COMMON_Q_SIZE:
        proxy->vqs[vdev->queue_sel].num = val;
        break;
    case VIRTIO_PCI_COMMON_Q_MSIX:
        msix_vector_unuse(&proxy->pci_dev,
                          virtio_queue_vector(vdev, vdev->queue_sel));
        /* Make it possible for guest to discover an error took place. */
        if (msix_vector_use(&proxy->pci_dev, val) < 0) {
            val = VIRTIO_NO_VECTOR;
        }
        virtio_queue_set_vector(vdev, vdev->queue_sel, val);
        break;
    case VIRTIO_PCI_COMMON_Q_ENABLE:
        /* TODO: need a way to put num back on reset. */
        virtio_queue_set_num(vdev, vdev->queue_sel,
                             proxy->vqs[vdev->queue_sel].num);
        virtio_queue_set_rings(vdev, vdev->queue_sel,
                       ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 |
                       proxy->vqs[vdev->queue_sel].desc[0],
                       ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 |
                       proxy->vqs[vdev->queue_sel].avail[0],
                       ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 |
                       proxy->vqs[vdev->queue_sel].used[0]);
J
Jason Wang 已提交
1387
        proxy->vqs[vdev->queue_sel].enabled = 1;
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
        break;
    case VIRTIO_PCI_COMMON_Q_DESCLO:
        proxy->vqs[vdev->queue_sel].desc[0] = val;
        break;
    case VIRTIO_PCI_COMMON_Q_DESCHI:
        proxy->vqs[vdev->queue_sel].desc[1] = val;
        break;
    case VIRTIO_PCI_COMMON_Q_AVAILLO:
        proxy->vqs[vdev->queue_sel].avail[0] = val;
        break;
    case VIRTIO_PCI_COMMON_Q_AVAILHI:
        proxy->vqs[vdev->queue_sel].avail[1] = val;
        break;
    case VIRTIO_PCI_COMMON_Q_USEDLO:
        proxy->vqs[vdev->queue_sel].used[0] = val;
        break;
    case VIRTIO_PCI_COMMON_Q_USEDHI:
        proxy->vqs[vdev->queue_sel].used[1] = val;
        break;
    default:
        break;
    }
}


static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr,
                                       unsigned size)
{
    return 0;
}

static void virtio_pci_notify_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned size)
{
    VirtIODevice *vdev = opaque;
    unsigned queue = addr / QEMU_VIRTIO_PCI_QUEUE_MEM_MULT;

    if (queue < VIRTIO_QUEUE_MAX) {
        virtio_queue_notify(vdev, queue);
    }
}

1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr,
                                        uint64_t val, unsigned size)
{
    VirtIODevice *vdev = opaque;
    unsigned queue = val;

    if (queue < VIRTIO_QUEUE_MAX) {
        virtio_queue_notify(vdev, queue);
    }
}

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
                                    unsigned size)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    uint64_t val = vdev->isr;

    vdev->isr = 0;
    pci_irq_deassert(&proxy->pci_dev);

    return val;
}

static void virtio_pci_isr_write(void *opaque, hwaddr addr,
                                 uint64_t val, unsigned size)
{
}

static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr,
                                       unsigned size)
{
    VirtIODevice *vdev = opaque;
    uint64_t val = 0;

    switch (size) {
    case 1:
1467
        val = virtio_config_modern_readb(vdev, addr);
1468 1469
        break;
    case 2:
1470
        val = virtio_config_modern_readw(vdev, addr);
1471 1472
        break;
    case 4:
1473
        val = virtio_config_modern_readl(vdev, addr);
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
        break;
    }
    return val;
}

static void virtio_pci_device_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned size)
{
    VirtIODevice *vdev = opaque;
    switch (size) {
    case 1:
1485
        virtio_config_modern_writeb(vdev, addr, val);
1486 1487
        break;
    case 2:
1488
        virtio_config_modern_writew(vdev, addr, val);
1489 1490
        break;
    case 4:
1491
        virtio_config_modern_writel(vdev, addr, val);
1492 1493 1494 1495
        break;
    }
}

1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy)
{
    static const MemoryRegionOps common_ops = {
        .read = virtio_pci_common_read,
        .write = virtio_pci_common_write,
        .impl = {
            .min_access_size = 1,
            .max_access_size = 4,
        },
        .endianness = DEVICE_LITTLE_ENDIAN,
    };
    static const MemoryRegionOps isr_ops = {
        .read = virtio_pci_isr_read,
        .write = virtio_pci_isr_write,
        .impl = {
            .min_access_size = 1,
            .max_access_size = 4,
        },
        .endianness = DEVICE_LITTLE_ENDIAN,
    };
    static const MemoryRegionOps device_ops = {
        .read = virtio_pci_device_read,
        .write = virtio_pci_device_write,
        .impl = {
            .min_access_size = 1,
            .max_access_size = 4,
        },
        .endianness = DEVICE_LITTLE_ENDIAN,
    };
    static const MemoryRegionOps notify_ops = {
        .read = virtio_pci_notify_read,
        .write = virtio_pci_notify_write,
        .impl = {
            .min_access_size = 1,
            .max_access_size = 4,
        },
        .endianness = DEVICE_LITTLE_ENDIAN,
    };
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
    static const MemoryRegionOps notify_pio_ops = {
        .read = virtio_pci_notify_read,
        .write = virtio_pci_notify_write_pio,
        .impl = {
            .min_access_size = 1,
            .max_access_size = 4,
        },
        .endianness = DEVICE_LITTLE_ENDIAN,
    };

1544 1545 1546 1547

    memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
                          &common_ops,
                          proxy,
1548 1549
                          "virtio-pci-common",
                          proxy->common.size);
1550

1551 1552 1553
    memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
                          &isr_ops,
                          proxy,
1554 1555
                          "virtio-pci-isr",
                          proxy->isr.size);
1556

1557 1558 1559
    memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
                          &device_ops,
                          virtio_bus_get_device(&proxy->bus),
1560 1561
                          "virtio-pci-device",
                          proxy->device.size);
1562

1563 1564 1565 1566
    memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
                          &notify_ops,
                          virtio_bus_get_device(&proxy->bus),
                          "virtio-pci-notify",
1567
                          proxy->notify.size);
1568 1569 1570 1571 1572 1573

    memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
                          &notify_pio_ops,
                          virtio_bus_get_device(&proxy->bus),
                          "virtio-pci-notify-pio",
                          proxy->notify.size);
1574 1575 1576
}

static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1577
                                         VirtIOPCIRegion *region,
1578 1579 1580
                                         struct virtio_pci_cap *cap,
                                         MemoryRegion *mr,
                                         uint8_t bar)
1581
{
1582
    memory_region_add_subregion(mr, region->offset, &region->mr);
1583

1584
    cap->cfg_type = region->type;
1585
    cap->bar = bar;
1586
    cap->offset = cpu_to_le32(region->offset);
1587
    cap->length = cpu_to_le32(region->size);
1588
    virtio_pci_add_mem_cap(proxy, cap);
1589 1590 1591 1592 1593 1594 1595 1596 1597

}

static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy,
                                             VirtIOPCIRegion *region,
                                             struct virtio_pci_cap *cap)
{
    virtio_pci_modern_region_map(proxy, region, cap,
                                 &proxy->modern_bar, proxy->modern_mem_bar);
1598
}
1599

1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy,
                                            VirtIOPCIRegion *region,
                                            struct virtio_pci_cap *cap)
{
    virtio_pci_modern_region_map(proxy, region, cap,
                                 &proxy->io_bar, proxy->modern_io_bar);
}

static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
                                               VirtIOPCIRegion *region)
1610 1611 1612 1613 1614
{
    memory_region_del_subregion(&proxy->modern_bar,
                                &region->mr);
}

1615 1616 1617 1618 1619 1620 1621
static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
                                              VirtIOPCIRegion *region)
{
    memory_region_del_subregion(&proxy->io_bar,
                                &region->mr);
}

1622
/* This is called by virtio-bus just after the device is plugged. */
J
Jason Wang 已提交
1623
static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1624 1625 1626
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
    VirtioBusState *bus = &proxy->bus;
1627 1628
    bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1629
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1630 1631
    uint8_t *config;
    uint32_t size;
C
Cornelia Huck 已提交
1632
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1633 1634 1635 1636 1637

    config = proxy->pci_dev.config;
    if (proxy->class_code) {
        pci_config_set_class(config, proxy->class_code);
    }
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651

    if (legacy) {
        /* legacy and transitional */
        pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
                     pci_get_word(config + PCI_VENDOR_ID));
        pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
    } else {
        /* pure virtio-1.0 */
        pci_set_word(config + PCI_VENDOR_ID,
                     PCI_VENDOR_ID_REDHAT_QUMRANET);
        pci_set_word(config + PCI_DEVICE_ID,
                     0x1040 + virtio_bus_get_vdev_id(bus));
        pci_config_set_revision(config, 1);
    }
1652 1653
    config[PCI_INTERRUPT_PIN] = 1;

1654

1655
    if (modern) {
1656 1657
        struct virtio_pci_cap cap = {
            .cap_len = sizeof cap,
1658 1659 1660 1661 1662 1663
        };
        struct virtio_pci_notify_cap notify = {
            .cap.cap_len = sizeof notify,
            .notify_off_multiplier =
                cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT),
        };
1664 1665 1666 1667
        struct virtio_pci_cfg_cap cfg = {
            .cap.cap_len = sizeof cfg,
            .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
        };
1668 1669 1670 1671
        struct virtio_pci_notify_cap notify_pio = {
            .cap.cap_len = sizeof notify,
            .notify_off_multiplier = cpu_to_le32(0x0),
        };
1672

1673
        struct virtio_pci_cfg_cap *cfg_mask;
1674 1675

        virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1676
        virtio_pci_modern_regions_init(proxy);
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692

        virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap);
        virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap);
        virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap);
        virtio_pci_modern_mem_region_map(proxy, &proxy->notify, &notify.cap);

        if (modern_pio) {
            memory_region_init(&proxy->io_bar, OBJECT(proxy),
                               "virtio-pci-io", 0x4);

            pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar,
                             PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);

            virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio,
                                            &notify_pio.cap);
        }
1693

1694
        pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar,
1695 1696 1697
                         PCI_BASE_ADDRESS_SPACE_MEMORY |
                         PCI_BASE_ADDRESS_MEM_PREFETCH |
                         PCI_BASE_ADDRESS_MEM_TYPE_64,
1698
                         &proxy->modern_bar);
1699 1700 1701 1702 1703 1704 1705

        proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap);
        cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap);
        pci_set_byte(&cfg_mask->cap.bar, ~0x0);
        pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0);
        pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0);
        pci_set_long(cfg_mask->pci_cfg_data, ~0x0);
1706 1707
    }

1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
    if (proxy->nvectors) {
        int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
                                          proxy->msix_bar);
        if (err) {
            /* Notice when a system that supports MSIx can't initialize it.  */
            if (err != -ENOTSUP) {
                error_report("unable to init msix vectors to %" PRIu32,
                             proxy->nvectors);
            }
            proxy->nvectors = 0;
        }
1719 1720 1721
    }

    proxy->pci_dev.config_write = virtio_write_config;
1722
    proxy->pci_dev.config_read = virtio_read_config;
1723

1724 1725 1726
    if (legacy) {
        size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
            + virtio_bus_get_vdev_config_len(bus);
1727
        size = pow2ceil(size);
1728

1729 1730 1731
        memory_region_init_io(&proxy->bar, OBJECT(proxy),
                              &virtio_pci_config_ops,
                              proxy, "virtio-pci", size);
1732

1733
        pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar,
1734
                         PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1735
    }
1736 1737 1738 1739 1740

    if (!kvm_has_many_ioeventfds()) {
        proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
    }

C
Cornelia Huck 已提交
1741
    virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1742 1743
}

1744 1745 1746
static void virtio_pci_device_unplugged(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1747
    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1748
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1749 1750

    virtio_pci_stop_ioeventfd(proxy);
1751 1752

    if (modern) {
1753 1754 1755 1756 1757 1758 1759
        virtio_pci_modern_mem_region_unmap(proxy, &proxy->common);
        virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr);
        virtio_pci_modern_mem_region_unmap(proxy, &proxy->device);
        virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify);
        if (modern_pio) {
            virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio);
        }
1760
    }
1761 1762
}

1763
static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1764
{
1765
    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1766
    VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1767

1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
    /*
     * virtio pci bar layout used by default.
     * subclasses can re-arrange things if needed.
     *
     *   region 0   --  virtio legacy io bar
     *   region 1   --  msi-x bar
     *   region 4+5 --  virtio modern memory (64bit) bar
     *
     */
    proxy->legacy_io_bar  = 0;
    proxy->msix_bar       = 1;
1779
    proxy->modern_io_bar  = 2;
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
    proxy->modern_mem_bar = 4;

    proxy->common.offset = 0x0;
    proxy->common.size = 0x1000;
    proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG;

    proxy->isr.offset = 0x1000;
    proxy->isr.size = 0x1000;
    proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG;

    proxy->device.offset = 0x2000;
    proxy->device.size = 0x1000;
    proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG;

    proxy->notify.offset = 0x3000;
    proxy->notify.size =
        QEMU_VIRTIO_PCI_QUEUE_MEM_MULT * VIRTIO_QUEUE_MAX;
    proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;

1799 1800 1801 1802
    proxy->notify_pio.offset = 0x0;
    proxy->notify_pio.size = 0x4;
    proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;

1803 1804 1805 1806 1807
    /* subclasses can enforce modern, so do this unconditionally */
    memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
                       2 * QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *
                       VIRTIO_QUEUE_MAX);

1808 1809 1810 1811 1812 1813 1814 1815 1816
    memory_region_init_alias(&proxy->modern_cfg,
                             OBJECT(proxy),
                             "virtio-pci-cfg",
                             &proxy->modern_bar,
                             0,
                             memory_region_size(&proxy->modern_bar));

    address_space_init(&proxy->modern_as, &proxy->modern_cfg, "virtio-pci-cfg-as");

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
    if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE)
        && !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN)
        && pci_bus_is_express(pci_dev->bus)
        && !pci_bus_is_root(pci_dev->bus)) {
        int pos;

        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
        pos = pcie_endpoint_cap_init(pci_dev, 0);
        assert(pos > 0);

        pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, PCI_PM_SIZEOF);
        assert(pos > 0);

        /*
         * Indicates that this function complies with revision 1.2 of the
         * PCI Power Management Interface Specification.
         */
        pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
    }

1837
    virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1838
    if (k->realize) {
1839
        k->realize(proxy, errp);
1840 1841 1842 1843 1844
    }
}

static void virtio_pci_exit(PCIDevice *pci_dev)
{
1845 1846
    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);

1847
    msix_uninit_exclusive_bar(pci_dev);
1848
    address_space_destroy(&proxy->modern_as);
1849 1850
}

1851
static void virtio_pci_reset(DeviceState *qdev)
1852 1853 1854
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
    VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
J
Jason Wang 已提交
1855 1856
    int i;

1857 1858 1859
    virtio_pci_stop_ioeventfd(proxy);
    virtio_bus_reset(bus);
    msix_unuse_all_vectors(&proxy->pci_dev);
J
Jason Wang 已提交
1860 1861 1862 1863

    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        proxy->vqs[i].enabled = 0;
    }
1864 1865
}

1866
static Property virtio_pci_properties[] = {
1867 1868
    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1869 1870 1871 1872
    DEFINE_PROP_BIT("disable-legacy", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_DISABLE_LEGACY_BIT, false),
    DEFINE_PROP_BIT("disable-modern", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_DISABLE_MODERN_BIT, true),
1873 1874
    DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1875 1876
    DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1877 1878
    DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1879 1880 1881
    DEFINE_PROP_END_OF_LIST(),
};

1882 1883 1884 1885 1886
static void virtio_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

1887
    dc->props = virtio_pci_properties;
1888
    k->realize = virtio_pci_realize;
1889 1890 1891 1892
    k->exit = virtio_pci_exit;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_OTHERS;
1893
    dc->reset = virtio_pci_reset;
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
}

static const TypeInfo virtio_pci_info = {
    .name          = TYPE_VIRTIO_PCI,
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_pci_class_init,
    .class_size    = sizeof(VirtioPCIClass),
    .abstract      = true,
};

1905 1906 1907
/* virtio-blk-pci */

static Property virtio_blk_pci_properties[] = {
1908
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1909 1910 1911 1912 1913 1914
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

1915
static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1916 1917 1918
{
    VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1919

1920
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1921
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1922 1923 1924 1925 1926 1927 1928 1929
}

static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);

1930
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1931
    dc->props = virtio_blk_pci_properties;
1932
    k->realize = virtio_blk_pci_realize;
1933 1934 1935 1936 1937 1938 1939 1940 1941
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
    pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
}

static void virtio_blk_pci_instance_init(Object *obj)
{
    VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1942 1943 1944

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BLK);
1945 1946
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
                              &error_abort);
1947 1948
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
}

static const TypeInfo virtio_blk_pci_info = {
    .name          = TYPE_VIRTIO_BLK_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VirtIOBlkPCI),
    .instance_init = virtio_blk_pci_instance_init,
    .class_init    = virtio_blk_pci_class_init,
};

1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
/* virtio-scsi-pci */

static Property virtio_scsi_pci_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
                       DEV_NVECTORS_UNSPECIFIED),
    DEFINE_PROP_END_OF_LIST(),
};

1969
static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1970 1971 1972
{
    VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1973
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1974 1975
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
1976 1977

    if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1978
        vpci_dev->nvectors = vs->conf.num_queues + 3;
1979 1980
    }

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
    /*
     * For command line compatibility, this sets the virtio-scsi-device bus
     * name as before.
     */
    if (proxy->id) {
        bus_name = g_strdup_printf("%s.0", proxy->id);
        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
        g_free(bus_name);
    }

1991
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1992
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1993 1994 1995 1996 1997 1998 1999
}

static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2000 2001

    k->realize = virtio_scsi_pci_realize;
2002
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
    dc->props = virtio_scsi_pci_properties;
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
    pcidev_k->revision = 0x00;
    pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
}

static void virtio_scsi_pci_instance_init(Object *obj)
{
    VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
2013 2014 2015

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SCSI);
2016 2017
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
                              &error_abort);
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
}

static const TypeInfo virtio_scsi_pci_info = {
    .name          = TYPE_VIRTIO_SCSI_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VirtIOSCSIPCI),
    .instance_init = virtio_scsi_pci_instance_init,
    .class_init    = virtio_scsi_pci_class_init,
};

2028 2029 2030 2031 2032 2033 2034 2035 2036
/* vhost-scsi-pci */

#ifdef CONFIG_VHOST_SCSI
static Property vhost_scsi_pci_properties[] = {
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
                       DEV_NVECTORS_UNSPECIFIED),
    DEFINE_PROP_END_OF_LIST(),
};

2037
static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
{
    VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);

    if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
        vpci_dev->nvectors = vs->conf.num_queues + 3;
    }

    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2048
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2049 2050 2051 2052 2053 2054 2055
}

static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2056
    k->realize = vhost_scsi_pci_realize;
2057
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
    dc->props = vhost_scsi_pci_properties;
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
    pcidev_k->revision = 0x00;
    pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
}

static void vhost_scsi_pci_instance_init(Object *obj)
{
    VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
2068 2069 2070

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_SCSI);
G
Gonglei 已提交
2071 2072
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
}

static const TypeInfo vhost_scsi_pci_info = {
    .name          = TYPE_VHOST_SCSI_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VHostSCSIPCI),
    .instance_init = vhost_scsi_pci_instance_init,
    .class_init    = vhost_scsi_pci_class_init,
};
#endif

2084 2085 2086
/* virtio-balloon-pci */

static Property virtio_balloon_pci_properties[] = {
2087
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2088 2089 2090
    DEFINE_PROP_END_OF_LIST(),
};

2091
static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
{
    VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

    if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
        vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
        vpci_dev->class_code = PCI_CLASS_OTHERS;
    }

    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2102
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2103 2104 2105 2106 2107 2108 2109
}

static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2110
    k->realize = virtio_balloon_pci_realize;
2111
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
    dc->props = virtio_balloon_pci_properties;
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
    pcidev_k->class_id = PCI_CLASS_OTHERS;
}

static void virtio_balloon_pci_instance_init(Object *obj)
{
    VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
2122

2123 2124
    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BALLOON);
2125 2126 2127 2128 2129
    object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
                                  "guest-stats", &error_abort);
    object_property_add_alias(obj, "guest-stats-polling-interval",
                              OBJECT(&dev->vdev),
                              "guest-stats-polling-interval", &error_abort);
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
}

static const TypeInfo virtio_balloon_pci_info = {
    .name          = TYPE_VIRTIO_BALLOON_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VirtIOBalloonPCI),
    .instance_init = virtio_balloon_pci_instance_init,
    .class_init    = virtio_balloon_pci_class_init,
};

2140 2141
/* virtio-serial-pci */

2142
static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2143 2144 2145
{
    VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
2146 2147
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160

    if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
        vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
        vpci_dev->class_code != PCI_CLASS_OTHERS) {        /* qemu-kvm  */
            vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
    }

    /* backwards-compatibility with machines that were created with
       DEV_NVECTORS_UNSPECIFIED */
    if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
        vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
    }

2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
    /*
     * For command line compatibility, this sets the virtio-serial-device bus
     * name as before.
     */
    if (proxy->id) {
        bus_name = g_strdup_printf("%s.0", proxy->id);
        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
        g_free(bus_name);
    }

2171
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2172
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2173 2174 2175 2176 2177 2178
}

static Property virtio_serial_pci_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
2179
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2180 2181 2182 2183 2184 2185 2186 2187
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
2188
    k->realize = virtio_serial_pci_realize;
2189
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
    dc->props = virtio_serial_pci_properties;
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
    pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
}

static void virtio_serial_pci_instance_init(Object *obj)
{
    VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
2200 2201 2202

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SERIAL);
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
}

static const TypeInfo virtio_serial_pci_info = {
    .name          = TYPE_VIRTIO_SERIAL_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VirtIOSerialPCI),
    .instance_init = virtio_serial_pci_instance_init,
    .class_init    = virtio_serial_pci_class_init,
};

2213 2214 2215 2216 2217 2218 2219 2220 2221
/* virtio-net-pci */

static Property virtio_net_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
    DEFINE_PROP_END_OF_LIST(),
};

2222
static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2223
{
2224
    DeviceState *qdev = DEVICE(vpci_dev);
2225 2226 2227
    VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

2228 2229
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
                                  object_get_typename(OBJECT(qdev)));
2230
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2231
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
}

static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
    VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);

    k->romfile = "efi-virtio.rom";
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_NETWORK_ETHERNET;
2245
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2246
    dc->props = virtio_net_properties;
2247
    vpciklass->realize = virtio_net_pci_realize;
2248 2249 2250 2251 2252
}

static void virtio_net_pci_instance_init(Object *obj)
{
    VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2253 2254 2255

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_NET);
2256 2257
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
}

static const TypeInfo virtio_net_pci_info = {
    .name          = TYPE_VIRTIO_NET_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VirtIONetPCI),
    .instance_init = virtio_net_pci_instance_init,
    .class_init    = virtio_net_pci_class_init,
};

2268 2269
/* virtio-rng-pci */

2270
static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2271 2272 2273
{
    VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&vrng->vdev);
2274
    Error *err = NULL;
2275 2276

    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2277 2278 2279 2280
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
        return;
2281 2282 2283
    }

    object_property_set_link(OBJECT(vrng),
2284
                             OBJECT(vrng->vdev.conf.rng), "rng",
2285 2286 2287 2288 2289 2290 2291 2292 2293
                             NULL);
}

static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);

2294
    k->realize = virtio_rng_pci_realize;
2295
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305

    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
    pcidev_k->class_id = PCI_CLASS_OTHERS;
}

static void virtio_rng_initfn(Object *obj)
{
    VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
2306 2307 2308

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_RNG);
2309 2310
    object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
                              &error_abort);
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
}

static const TypeInfo virtio_rng_pci_info = {
    .name          = TYPE_VIRTIO_RNG_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VirtIORngPCI),
    .instance_init = virtio_rng_initfn,
    .class_init    = virtio_rng_pci_class_init,
};

2321 2322
/* virtio-input-pci */

2323
static Property virtio_input_pci_properties[] = {
2324 2325 2326 2327
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
static void virtio_input_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
{
    VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&vinput->vdev);

    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
    /* force virtio-1.0 */
    vpci_dev->flags &= ~VIRTIO_PCI_FLAG_DISABLE_MODERN;
    vpci_dev->flags |= VIRTIO_PCI_FLAG_DISABLE_LEGACY;
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
}

static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);

2346
    dc->props = virtio_input_pci_properties;
2347 2348 2349 2350 2351 2352
    k->realize = virtio_input_pci_realize;
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);

    pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
}

2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
static void virtio_input_hid_kbd_pci_class_init(ObjectClass *klass, void *data)
{
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);

    pcidev_k->class_id = PCI_CLASS_INPUT_KEYBOARD;
}

static void virtio_input_hid_mouse_pci_class_init(ObjectClass *klass,
                                                  void *data)
{
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);

    pcidev_k->class_id = PCI_CLASS_INPUT_MOUSE;
}

static void virtio_keyboard_initfn(Object *obj)
{
    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2371 2372 2373

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_KEYBOARD);
2374 2375 2376 2377 2378
}

static void virtio_mouse_initfn(Object *obj)
{
    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2379 2380 2381

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_MOUSE);
2382 2383 2384 2385 2386
}

static void virtio_tablet_initfn(Object *obj)
{
    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2387 2388 2389

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_TABLET);
2390 2391
}

2392 2393 2394 2395 2396 2397 2398 2399
static const TypeInfo virtio_input_pci_info = {
    .name          = TYPE_VIRTIO_INPUT_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VirtIOInputPCI),
    .class_init    = virtio_input_pci_class_init,
    .abstract      = true,
};

2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
static const TypeInfo virtio_input_hid_pci_info = {
    .name          = TYPE_VIRTIO_INPUT_HID_PCI,
    .parent        = TYPE_VIRTIO_INPUT_PCI,
    .instance_size = sizeof(VirtIOInputHIDPCI),
    .abstract      = true,
};

static const TypeInfo virtio_keyboard_pci_info = {
    .name          = TYPE_VIRTIO_KEYBOARD_PCI,
    .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
    .class_init    = virtio_input_hid_kbd_pci_class_init,
    .instance_size = sizeof(VirtIOInputHIDPCI),
    .instance_init = virtio_keyboard_initfn,
};

static const TypeInfo virtio_mouse_pci_info = {
    .name          = TYPE_VIRTIO_MOUSE_PCI,
    .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
    .class_init    = virtio_input_hid_mouse_pci_class_init,
    .instance_size = sizeof(VirtIOInputHIDPCI),
    .instance_init = virtio_mouse_initfn,
};

static const TypeInfo virtio_tablet_pci_info = {
    .name          = TYPE_VIRTIO_TABLET_PCI,
    .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
    .instance_size = sizeof(VirtIOInputHIDPCI),
    .instance_init = virtio_tablet_initfn,
};

2430 2431 2432 2433 2434 2435 2436 2437 2438
#ifdef CONFIG_LINUX
static void virtio_host_initfn(Object *obj)
{
    VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_INPUT_HOST);
}

G
Gerd Hoffmann 已提交
2439 2440 2441 2442 2443 2444
static const TypeInfo virtio_host_pci_info = {
    .name          = TYPE_VIRTIO_INPUT_HOST_PCI,
    .parent        = TYPE_VIRTIO_INPUT_PCI,
    .instance_size = sizeof(VirtIOInputHostPCI),
    .instance_init = virtio_host_initfn,
};
2445
#endif
G
Gerd Hoffmann 已提交
2446

2447 2448
/* virtio-pci-bus */

2449 2450
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtIOPCIProxy *dev)
2451 2452
{
    DeviceState *qdev = DEVICE(dev);
2453 2454
    char virtio_bus_name[] = "virtio-bus";

2455
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2456
                        virtio_bus_name);
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468
}

static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
{
    BusClass *bus_class = BUS_CLASS(klass);
    VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
    bus_class->max_dev = 1;
    k->notify = virtio_pci_notify;
    k->save_config = virtio_pci_save_config;
    k->load_config = virtio_pci_load_config;
    k->save_queue = virtio_pci_save_queue;
    k->load_queue = virtio_pci_load_queue;
2469 2470 2471
    k->save_extra_state = virtio_pci_save_extra_state;
    k->load_extra_state = virtio_pci_load_extra_state;
    k->has_extra_state = virtio_pci_has_extra_state;
2472 2473 2474 2475
    k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
    k->set_host_notifier = virtio_pci_set_host_notifier;
    k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
    k->vmstate_change = virtio_pci_vmstate_change;
2476
    k->device_plugged = virtio_pci_device_plugged;
2477
    k->device_unplugged = virtio_pci_device_unplugged;
2478
    k->query_nvectors = virtio_pci_query_nvectors;
2479 2480 2481 2482 2483 2484 2485 2486 2487
}

static const TypeInfo virtio_pci_bus_info = {
    .name          = TYPE_VIRTIO_PCI_BUS,
    .parent        = TYPE_VIRTIO_BUS,
    .instance_size = sizeof(VirtioPCIBusState),
    .class_init    = virtio_pci_bus_class_init,
};

A
Andreas Färber 已提交
2488
static void virtio_pci_register_types(void)
P
Paul Brook 已提交
2489
{
2490
    type_register_static(&virtio_rng_pci_info);
2491
    type_register_static(&virtio_input_pci_info);
2492 2493 2494 2495
    type_register_static(&virtio_input_hid_pci_info);
    type_register_static(&virtio_keyboard_pci_info);
    type_register_static(&virtio_mouse_pci_info);
    type_register_static(&virtio_tablet_pci_info);
2496
#ifdef CONFIG_LINUX
G
Gerd Hoffmann 已提交
2497
    type_register_static(&virtio_host_pci_info);
2498
#endif
2499
    type_register_static(&virtio_pci_bus_info);
2500
    type_register_static(&virtio_pci_info);
2501
#ifdef CONFIG_VIRTFS
2502
    type_register_static(&virtio_9p_pci_info);
2503
#endif
2504
    type_register_static(&virtio_blk_pci_info);
2505
    type_register_static(&virtio_scsi_pci_info);
2506
    type_register_static(&virtio_balloon_pci_info);
2507
    type_register_static(&virtio_serial_pci_info);
2508
    type_register_static(&virtio_net_pci_info);
2509 2510 2511
#ifdef CONFIG_VHOST_SCSI
    type_register_static(&vhost_scsi_pci_info);
#endif
P
Paul Brook 已提交
2512 2513
}

A
Andreas Färber 已提交
2514
type_init(virtio_pci_register_types)