virtio-pci.c 80.2 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
 */

P
Peter Maydell 已提交
18
#include "qemu/osdep.h"
P
Paul Brook 已提交
19

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 "qapi/error.h"
30
#include "qemu/error-report.h"
31 32 33
#include "hw/pci/msi.h"
#include "hw/pci/msix.h"
#include "hw/loader.h"
34
#include "sysemu/kvm.h"
35
#include "sysemu/block-backend.h"
36
#include "virtio-pci.h"
37
#include "qemu/range.h"
P
Paolo Bonzini 已提交
38
#include "hw/virtio/virtio-bus.h"
39
#include "qapi/visitor.h"
P
Paul Brook 已提交
40

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

43 44
#undef VIRTIO_PCI_CONFIG

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

49 50
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtIOPCIProxy *dev);
51
static void virtio_pci_reset(DeviceState *qdev);
52

P
Paul Brook 已提交
53
/* virtio device */
54 55 56 57 58
/* 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 已提交
59

60 61 62 63
/* 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 已提交
64
{
65 66 67 68 69 70
    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 已提交
71

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

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

85 86 87
    pci_device_save(&proxy->pci_dev, f);
    msix_save(&proxy->pci_dev, f);
    if (msix_present(&proxy->pci_dev))
P
Paolo Bonzini 已提交
88
        qemu_put_be16(f, vdev->config_vector);
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 212 213
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);
}

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

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

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

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

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

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

262 263 264
    return 0;
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
static bool virtio_pci_ioeventfd_started(DeviceState *d)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    return proxy->ioeventfd_started;
}

static void virtio_pci_ioeventfd_set_started(DeviceState *d, bool started,
                                             bool err)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    proxy->ioeventfd_started = started;
}

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

    return proxy->ioeventfd_disabled ||
        !(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD);
}

static void virtio_pci_ioeventfd_set_disabled(DeviceState *d, bool disabled)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    proxy->ioeventfd_disabled = disabled;
}

295 296
#define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000

297 298
static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
                                       int n, bool assign)
299
{
300
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
301 302
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
303 304
    bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
305
    bool fast_mmio = kvm_ioeventfd_any_length_enabled();
306
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
307
    MemoryRegion *modern_mr = &proxy->notify.mr;
308
    MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
309 310 311 312
    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 已提交
313

314
    if (assign) {
315
        if (modern) {
316 317 318 319 320 321 322
            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);
            }
323 324 325 326
            if (modern_pio) {
                memory_region_add_eventfd(modern_notify_mr, 0, 2,
                                              true, n, notifier);
            }
327 328 329 330 331
        }
        if (legacy) {
            memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
                                      true, n, notifier);
        }
332
    } else {
333
        if (modern) {
334 335 336 337 338 339 340
            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);
            }
341 342 343 344
            if (modern_pio) {
                memory_region_del_eventfd(modern_notify_mr, 0, 2,
                                          true, n, notifier);
            }
345 346 347 348 349
        }
        if (legacy) {
            memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
                                      true, n, notifier);
        }
350
    }
351
    return 0;
352 353
}

354
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
355
{
356
    virtio_bus_start_ioeventfd(&proxy->bus);
357 358
}

359
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
360
{
361
    virtio_bus_stop_ioeventfd(&proxy->bus);
362 363
}

P
Paul Brook 已提交
364 365 366
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
P
Paolo Bonzini 已提交
367
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
A
Avi Kivity 已提交
368
    hwaddr pa;
P
Paul Brook 已提交
369 370 371

    switch (addr) {
    case VIRTIO_PCI_GUEST_FEATURES:
372 373 374 375
        /* 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);
        }
376
        virtio_set_features(vdev, val);
P
Paul Brook 已提交
377 378
        break;
    case VIRTIO_PCI_QUEUE_PFN:
A
Avi Kivity 已提交
379
        pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
380
        if (pa == 0) {
381
            virtio_pci_reset(DEVICE(proxy));
382
        }
383 384
        else
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
P
Paul Brook 已提交
385 386
        break;
    case VIRTIO_PCI_QUEUE_SEL:
387
        if (val < VIRTIO_QUEUE_MAX)
P
Paul Brook 已提交
388 389 390
            vdev->queue_sel = val;
        break;
    case VIRTIO_PCI_QUEUE_NOTIFY:
391
        if (val < VIRTIO_QUEUE_MAX) {
392 393
            virtio_queue_notify(vdev, val);
        }
P
Paul Brook 已提交
394 395
        break;
    case VIRTIO_PCI_STATUS:
396 397 398 399
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
            virtio_pci_stop_ioeventfd(proxy);
        }

400
        virtio_set_status(vdev, val & 0xFF);
401 402 403 404 405

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

406
        if (vdev->status == 0) {
407
            virtio_pci_reset(DEVICE(proxy));
408
        }
409

410 411 412 413 414 415 416 417 418
        /* 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 已提交
419
        break;
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    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:
436 437
        error_report("%s: unexpected address 0x%x value 0x%x",
                     __func__, addr, val);
438
        break;
P
Paul Brook 已提交
439 440 441
    }
}

442
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
P
Paul Brook 已提交
443
{
P
Paolo Bonzini 已提交
444
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
P
Paul Brook 已提交
445 446 447 448
    uint32_t ret = 0xFFFFFFFF;

    switch (addr) {
    case VIRTIO_PCI_HOST_FEATURES:
C
Cornelia Huck 已提交
449
        ret = vdev->host_features;
P
Paul Brook 已提交
450 451
        break;
    case VIRTIO_PCI_GUEST_FEATURES:
452
        ret = vdev->guest_features;
P
Paul Brook 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
        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;
471
        pci_irq_deassert(&proxy->pci_dev);
P
Paul Brook 已提交
472
        break;
473 474 475 476 477 478
    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 已提交
479 480 481 482 483 484 485
    default:
        break;
    }

    return ret;
}

486 487
static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
                                       unsigned size)
P
Paul Brook 已提交
488 489
{
    VirtIOPCIProxy *proxy = opaque;
P
Paolo Bonzini 已提交
490
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
491
    uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
492
    uint64_t val = 0;
493
    if (addr < config) {
494
        return virtio_ioport_read(proxy, addr);
495 496
    }
    addr -= config;
P
Paul Brook 已提交
497

498 499
    switch (size) {
    case 1:
P
Paolo Bonzini 已提交
500
        val = virtio_config_readb(vdev, addr);
501 502
        break;
    case 2:
P
Paolo Bonzini 已提交
503
        val = virtio_config_readw(vdev, addr);
504
        if (virtio_is_big_endian(vdev)) {
505 506
            val = bswap16(val);
        }
507 508
        break;
    case 4:
P
Paolo Bonzini 已提交
509
        val = virtio_config_readl(vdev, addr);
510
        if (virtio_is_big_endian(vdev)) {
511 512
            val = bswap32(val);
        }
513
        break;
514
    }
515
    return val;
P
Paul Brook 已提交
516 517
}

518 519
static void virtio_pci_config_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned size)
P
Paul Brook 已提交
520 521
{
    VirtIOPCIProxy *proxy = opaque;
522
    uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
P
Paolo Bonzini 已提交
523
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
524 525 526 527 528
    if (addr < config) {
        virtio_ioport_write(proxy, addr, val);
        return;
    }
    addr -= config;
529 530 531 532 533 534
    /*
     * Virtio-PCI is odd. Ioports are LE but config space is target native
     * endian.
     */
    switch (size) {
    case 1:
P
Paolo Bonzini 已提交
535
        virtio_config_writeb(vdev, addr, val);
536 537
        break;
    case 2:
538
        if (virtio_is_big_endian(vdev)) {
539 540
            val = bswap16(val);
        }
P
Paolo Bonzini 已提交
541
        virtio_config_writew(vdev, addr, val);
542 543
        break;
    case 4:
544
        if (virtio_is_big_endian(vdev)) {
545 546
            val = bswap32(val);
        }
P
Paolo Bonzini 已提交
547
        virtio_config_writel(vdev, addr, val);
548
        break;
549
    }
P
Paul Brook 已提交
550 551
}

A
Avi Kivity 已提交
552
static const MemoryRegionOps virtio_pci_config_ops = {
553 554 555 556 557 558
    .read = virtio_pci_config_read,
    .write = virtio_pci_config_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 4,
    },
559
    .endianness = DEVICE_LITTLE_ENDIAN,
A
Avi Kivity 已提交
560
};
561

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 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
/* 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;
    }
}

639 640 641
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
                                uint32_t val, int len)
{
642
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
P
Paolo Bonzini 已提交
643
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
644
    struct virtio_pci_cfg_cap *cfg;
645

646 647 648
    pci_default_write_config(pci_dev, address, val, len);

    if (range_covers_byte(address, len, PCI_COMMAND) &&
649
        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
650
        virtio_pci_stop_ioeventfd(proxy);
651
        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
652
    }
653 654 655 656 657 658 659 660 661 662 663 664

    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);

665 666
        if (len == 1 || len == 2 || len == 4) {
            assert(len <= sizeof cfg->pci_cfg_data);
667 668
            virtio_address_space_write(&proxy->modern_as, off,
                                       cfg->pci_cfg_data, len);
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
        }
    }
}

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);

690 691
        if (len == 1 || len == 2 || len == 4) {
            assert(len <= sizeof cfg->pci_cfg_data);
692 693
            virtio_address_space_read(&proxy->modern_as, off,
                                      cfg->pci_cfg_data, len);
694 695 696 697
        }
    }

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

700 701 702 703 704 705
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];
706
    int ret;
707 708

    if (irqfd->users == 0) {
709
        ret = kvm_irqchip_add_msi_route(kvm_state, msg, &proxy->pci_dev);
710 711 712 713 714 715 716 717 718 719 720
        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)
721 722 723 724 725 726 727
{
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
    if (--irqfd->users == 0) {
        kvm_irqchip_release_virq(kvm_state, irqfd->virq);
    }
}

728 729 730 731 732
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 已提交
733 734
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
735
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
736
    return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
737 738 739 740 741
}

static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
                                      unsigned int queue_no,
                                      unsigned int vector)
742
{
P
Paolo Bonzini 已提交
743 744
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
745
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
746
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
747
    int ret;
748

749
    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
750
    assert(ret == 0);
751
}
752

753 754 755
static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
{
    PCIDevice *dev = &proxy->pci_dev;
P
Paolo Bonzini 已提交
756
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
757
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    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;
774
        }
775 776 777
        /* If guest supports masking, set up irqfd now.
         * Otherwise, delay until unmasked in the frontend.
         */
778
        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
779 780 781 782 783 784
            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
            if (ret < 0) {
                kvm_virtio_pci_vq_vector_release(proxy, vector);
                goto undo;
            }
        }
785 786
    }
    return 0;
787 788 789 790 791 792 793

undo:
    while (--queue_no >= 0) {
        vector = virtio_queue_vector(vdev, queue_no);
        if (vector >= msix_nr_vectors_allocated(dev)) {
            continue;
        }
794
        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
795
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
796
        }
797 798 799
        kvm_virtio_pci_vq_vector_release(proxy, vector);
    }
    return ret;
800 801
}

802 803 804
static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
{
    PCIDevice *dev = &proxy->pci_dev;
P
Paolo Bonzini 已提交
805
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
806 807
    unsigned int vector;
    int queue_no;
808
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
809 810 811 812 813 814 815 816 817

    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;
        }
818 819 820
        /* If guest supports masking, clean up irqfd now.
         * Otherwise, it was cleaned when masked in the frontend.
         */
821
        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
822
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
823
        }
824 825 826 827
        kvm_virtio_pci_vq_vector_release(proxy, vector);
    }
}

828 829 830 831
static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                       unsigned int queue_no,
                                       unsigned int vector,
                                       MSIMessage msg)
832
{
P
Paolo Bonzini 已提交
833 834 835
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
836
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
837
    VirtIOIRQFD *irqfd;
838
    int ret = 0;
839

840 841 842
    if (proxy->vector_irqfd) {
        irqfd = &proxy->vector_irqfd[vector];
        if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
843 844
            ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
                                               &proxy->pci_dev);
845 846 847
            if (ret < 0) {
                return ret;
            }
848 849 850
        }
    }

851 852 853
    /* If guest supports masking, irqfd is already setup, unmask it.
     * Otherwise, set it up now.
     */
854
    if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
855
        k->guest_notifier_mask(vdev, queue_no, false);
856
        /* Test after unmasking to avoid losing events. */
857
        if (k->guest_notifier_pending &&
P
Paolo Bonzini 已提交
858
            k->guest_notifier_pending(vdev, queue_no)) {
859 860 861 862
            event_notifier_set(n);
        }
    } else {
        ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
863
    }
864
    return ret;
865 866
}

867
static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
868 869 870
                                             unsigned int queue_no,
                                             unsigned int vector)
{
P
Paolo Bonzini 已提交
871 872
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
873

874 875 876
    /* If guest supports masking, keep irqfd but mask it.
     * Otherwise, clean it up now.
     */ 
877
    if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
878
        k->guest_notifier_mask(vdev, queue_no, true);
879
    } else {
880
        kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
881
    }
882 883
}

884 885
static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
                                    MSIMessage msg)
886 887
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
888
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
889 890
    VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
    int ret, index, unmasked = 0;
891

892 893 894
    while (vq) {
        index = virtio_get_queue_index(vq);
        if (!virtio_queue_get_num(vdev, index)) {
895 896
            break;
        }
897 898 899 900 901 902
        if (index < proxy->nvqs_with_notifiers) {
            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
            if (ret < 0) {
                goto undo;
            }
            ++unmasked;
903
        }
904
        vq = virtio_vector_next_queue(vq);
905
    }
906

907 908 909
    return 0;

undo:
910
    vq = virtio_vector_first_queue(vdev, vector);
911
    while (vq && unmasked >= 0) {
912
        index = virtio_get_queue_index(vq);
913 914 915 916
        if (index < proxy->nvqs_with_notifiers) {
            virtio_pci_vq_vector_mask(proxy, index, vector);
            --unmasked;
        }
917
        vq = virtio_vector_next_queue(vq);
918 919 920 921
    }
    return ret;
}

922
static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
923 924
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
925
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
926 927
    VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
    int index;
928

929 930 931
    while (vq) {
        index = virtio_get_queue_index(vq);
        if (!virtio_queue_get_num(vdev, index)) {
932 933
            break;
        }
934 935 936
        if (index < proxy->nvqs_with_notifiers) {
            virtio_pci_vq_vector_mask(proxy, index, vector);
        }
937
        vq = virtio_vector_next_queue(vq);
938 939 940
    }
}

941 942 943
static void virtio_pci_vector_poll(PCIDevice *dev,
                                   unsigned int vector_start,
                                   unsigned int vector_end)
944 945
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
946
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
947
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
948 949 950 951 952
    int queue_no;
    unsigned int vector;
    EventNotifier *notifier;
    VirtQueue *vq;

953
    for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
954 955 956 957 958 959 960 961 962 963
        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);
964 965
        if (k->guest_notifier_pending) {
            if (k->guest_notifier_pending(vdev, queue_no)) {
966 967 968
                msix_set_pending(dev, vector);
            }
        } else if (event_notifier_test_and_clear(notifier)) {
969 970 971 972 973 974 975
            msix_set_pending(dev, vector);
        }
    }
}

static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
                                         bool with_irqfd)
976
{
977
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
978 979 980
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
    VirtQueue *vq = virtio_get_queue(vdev, n);
981 982 983 984 985 986 987
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);

    if (assign) {
        int r = event_notifier_init(notifier, 0);
        if (r < 0) {
            return r;
        }
988
        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
989
    } else {
990
        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
991 992 993
        event_notifier_cleanup(notifier);
    }

994 995 996
    if (!msix_enabled(&proxy->pci_dev) &&
        vdev->use_guest_notifier_mask &&
        vdc->guest_notifier_mask) {
P
Paolo Bonzini 已提交
997
        vdc->guest_notifier_mask(vdev, n, !assign);
998 999
    }

1000 1001 1002
    return 0;
}

1003
static bool virtio_pci_query_guest_notifiers(DeviceState *d)
1004
{
1005
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1006 1007 1008
    return msix_enabled(&proxy->pci_dev);
}

1009
static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
1010
{
1011
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
1012
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1013
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1014
    int r, n;
1015 1016
    bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
        kvm_msi_via_irqfd_enabled();
1017

1018
    nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
1019 1020 1021 1022 1023 1024 1025 1026

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

    proxy->nvqs_with_notifiers = nvqs;

1027
    /* Must unset vector notifier while guest notifier is still assigned */
1028
    if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
1029
        msix_unset_vector_notifiers(&proxy->pci_dev);
1030 1031 1032 1033 1034
        if (proxy->vector_irqfd) {
            kvm_virtio_pci_vector_release(proxy, nvqs);
            g_free(proxy->vector_irqfd);
            proxy->vector_irqfd = NULL;
        }
1035 1036
    }

1037
    for (n = 0; n < nvqs; n++) {
1038 1039 1040 1041
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }

1042
        r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
1043 1044 1045 1046 1047
        if (r < 0) {
            goto assign_error;
        }
    }

1048
    /* Must set vector notifier after guest notifier has been assigned */
1049
    if ((with_irqfd || k->guest_notifier_mask) && assign) {
1050 1051 1052 1053 1054 1055 1056 1057
        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;
            }
1058
        }
1059
        r = msix_set_vector_notifiers(&proxy->pci_dev,
1060 1061 1062
                                      virtio_pci_vector_unmask,
                                      virtio_pci_vector_mask,
                                      virtio_pci_vector_poll);
1063
        if (r < 0) {
1064
            goto notifiers_error;
1065 1066 1067
        }
    }

1068 1069
    return 0;

1070
notifiers_error:
1071 1072 1073 1074
    if (with_irqfd) {
        assert(assign);
        kvm_virtio_pci_vector_release(proxy, nvqs);
    }
1075

1076 1077
assign_error:
    /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1078
    assert(assign);
1079
    while (--n >= 0) {
1080
        virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1081 1082 1083 1084
    }
    return r;
}

1085
static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1086
{
1087
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
1088
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1089 1090

    if (running) {
1091 1092 1093 1094 1095
        /* 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) &&
1096
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1097 1098 1099
            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
                                     proxy->pci_dev.config[PCI_COMMAND] |
                                     PCI_COMMAND_MASTER, 1);
1100
        }
1101
        virtio_pci_start_ioeventfd(proxy);
1102
    } else {
1103
        virtio_pci_stop_ioeventfd(proxy);
1104 1105 1106
    }
}

1107
#ifdef CONFIG_VIRTFS
1108
static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1109
{
1110 1111
    V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1112

1113
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1114
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1115 1116
}

1117 1118 1119
static Property virtio_9p_pci_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1120 1121 1122 1123
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

1124
static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1125 1126
{
    DeviceClass *dc = DEVICE_CLASS(klass);
1127 1128
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1129

1130
    k->realize = virtio_9p_pci_realize;
1131 1132 1133 1134
    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;
1135
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1136
    dc->props = virtio_9p_pci_properties;
1137 1138
}

1139 1140 1141
static void virtio_9p_pci_instance_init(Object *obj)
{
    V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1142 1143 1144

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_9P);
1145 1146 1147 1148 1149 1150 1151 1152
}

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,
1153
};
1154
#endif /* CONFIG_VIRTFS */
1155

1156 1157 1158 1159
/*
 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
 */

1160 1161 1162 1163 1164 1165 1166
static int virtio_pci_query_nvectors(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);

    return proxy->nvectors;
}

1167
static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
                                   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);
1179 1180

    return offset;
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
}

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) {
1197 1198
            val = (vdev->host_features & ~VIRTIO_LEGACY_FEATURES) >>
                (32 * proxy->dfselect);
1199 1200 1201 1202 1203 1204
        }
        break;
    case VIRTIO_PCI_COMMON_GFSELECT:
        val = proxy->gfselect;
        break;
    case VIRTIO_PCI_COMMON_GF:
G
Gonglei 已提交
1205
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
            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:
1223
        val = vdev->generation;
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
        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 已提交
1280
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
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
            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) {
1307
            virtio_pci_reset(DEVICE(proxy));
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
        }

        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 已提交
1339
        proxy->vqs[vdev->queue_sel].enabled = 1;
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
        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);
    }
}

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
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);
    }
}

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
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:
1419
        val = virtio_config_modern_readb(vdev, addr);
1420 1421
        break;
    case 2:
1422
        val = virtio_config_modern_readw(vdev, addr);
1423 1424
        break;
    case 4:
1425
        val = virtio_config_modern_readl(vdev, addr);
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
        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:
1437
        virtio_config_modern_writeb(vdev, addr, val);
1438 1439
        break;
    case 2:
1440
        virtio_config_modern_writew(vdev, addr, val);
1441 1442
        break;
    case 4:
1443
        virtio_config_modern_writel(vdev, addr, val);
1444 1445 1446 1447
        break;
    }
}

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
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,
    };
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
    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,
    };

1496 1497 1498 1499

    memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
                          &common_ops,
                          proxy,
1500 1501
                          "virtio-pci-common",
                          proxy->common.size);
1502

1503 1504 1505
    memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
                          &isr_ops,
                          proxy,
1506 1507
                          "virtio-pci-isr",
                          proxy->isr.size);
1508

1509 1510 1511
    memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
                          &device_ops,
                          virtio_bus_get_device(&proxy->bus),
1512 1513
                          "virtio-pci-device",
                          proxy->device.size);
1514

1515 1516 1517 1518
    memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
                          &notify_ops,
                          virtio_bus_get_device(&proxy->bus),
                          "virtio-pci-notify",
1519
                          proxy->notify.size);
1520 1521 1522 1523 1524 1525

    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);
1526 1527 1528
}

static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1529
                                         VirtIOPCIRegion *region,
1530 1531 1532
                                         struct virtio_pci_cap *cap,
                                         MemoryRegion *mr,
                                         uint8_t bar)
1533
{
1534
    memory_region_add_subregion(mr, region->offset, &region->mr);
1535

1536
    cap->cfg_type = region->type;
1537
    cap->bar = bar;
1538
    cap->offset = cpu_to_le32(region->offset);
1539
    cap->length = cpu_to_le32(region->size);
1540
    virtio_pci_add_mem_cap(proxy, cap);
1541 1542 1543 1544 1545 1546 1547 1548 1549

}

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);
1550
}
1551

1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
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)
1562 1563 1564 1565 1566
{
    memory_region_del_subregion(&proxy->modern_bar,
                                &region->mr);
}

1567 1568 1569 1570 1571 1572 1573
static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
                                              VirtIOPCIRegion *region)
{
    memory_region_del_subregion(&proxy->io_bar,
                                &region->mr);
}

1574
/* This is called by virtio-bus just after the device is plugged. */
J
Jason Wang 已提交
1575
static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1576 1577 1578
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
    VirtioBusState *bus = &proxy->bus;
1579 1580
    bool legacy = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_LEGACY);
    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1581
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1582 1583
    uint8_t *config;
    uint32_t size;
C
Cornelia Huck 已提交
1584
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1585 1586 1587 1588 1589

    config = proxy->pci_dev.config;
    if (proxy->class_code) {
        pci_config_set_class(config, proxy->class_code);
    }
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603

    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);
    }
1604 1605
    config[PCI_INTERRUPT_PIN] = 1;

1606

1607
    if (modern) {
1608 1609
        struct virtio_pci_cap cap = {
            .cap_len = sizeof cap,
1610 1611 1612 1613 1614 1615
        };
        struct virtio_pci_notify_cap notify = {
            .cap.cap_len = sizeof notify,
            .notify_off_multiplier =
                cpu_to_le32(QEMU_VIRTIO_PCI_QUEUE_MEM_MULT),
        };
1616 1617 1618 1619
        struct virtio_pci_cfg_cap cfg = {
            .cap.cap_len = sizeof cfg,
            .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
        };
1620 1621 1622 1623
        struct virtio_pci_notify_cap notify_pio = {
            .cap.cap_len = sizeof notify,
            .notify_off_multiplier = cpu_to_le32(0x0),
        };
1624

1625
        struct virtio_pci_cfg_cap *cfg_mask;
1626 1627

        virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
1628
        virtio_pci_modern_regions_init(proxy);
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644

        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);
        }
1645

1646
        pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar,
1647 1648 1649
                         PCI_BASE_ADDRESS_SPACE_MEMORY |
                         PCI_BASE_ADDRESS_MEM_PREFETCH |
                         PCI_BASE_ADDRESS_MEM_TYPE_64,
1650
                         &proxy->modern_bar);
1651 1652 1653 1654 1655 1656 1657

        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);
1658 1659
    }

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    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;
        }
1671 1672 1673
    }

    proxy->pci_dev.config_write = virtio_write_config;
1674
    proxy->pci_dev.config_read = virtio_read_config;
1675

1676 1677 1678
    if (legacy) {
        size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
            + virtio_bus_get_vdev_config_len(bus);
1679
        size = pow2ceil(size);
1680

1681 1682 1683
        memory_region_init_io(&proxy->bar, OBJECT(proxy),
                              &virtio_pci_config_ops,
                              proxy, "virtio-pci", size);
1684

1685
        pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar,
1686
                         PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1687
    }
1688 1689 1690 1691 1692

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

C
Cornelia Huck 已提交
1693
    virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
1694 1695
}

1696 1697 1698
static void virtio_pci_device_unplugged(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1699
    bool modern = !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN);
1700
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1701 1702

    virtio_pci_stop_ioeventfd(proxy);
1703 1704

    if (modern) {
1705 1706 1707 1708 1709 1710 1711
        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);
        }
1712
    }
1713 1714
}

1715
static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1716
{
1717
    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1718
    VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1719

1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
    /*
     * 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;
1731
    proxy->modern_io_bar  = 2;
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
    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;

1751 1752 1753 1754
    proxy->notify_pio.offset = 0x0;
    proxy->notify_pio.size = 0x4;
    proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;

1755 1756 1757 1758 1759
    /* 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);

1760 1761 1762 1763 1764 1765 1766 1767 1768
    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");

1769 1770
    if (pci_is_express(pci_dev) && pci_bus_is_express(pci_dev->bus) &&
        !pci_bus_is_root(pci_dev->bus)) {
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
        int pos;

        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);
1784 1785 1786 1787 1788 1789
    } else {
        /*
         * make future invocations of pci_is_express() return false
         * and pci_config_size() return PCI_CONFIG_SPACE_SIZE.
         */
        pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS;
1790 1791
    }

1792
    virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1793
    if (k->realize) {
1794
        k->realize(proxy, errp);
1795 1796 1797 1798 1799
    }
}

static void virtio_pci_exit(PCIDevice *pci_dev)
{
1800 1801
    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);

1802
    msix_uninit_exclusive_bar(pci_dev);
1803
    address_space_destroy(&proxy->modern_as);
1804 1805
}

1806
static void virtio_pci_reset(DeviceState *qdev)
1807 1808 1809
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
    VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
J
Jason Wang 已提交
1810 1811
    int i;

1812 1813 1814
    virtio_pci_stop_ioeventfd(proxy);
    virtio_bus_reset(bus);
    msix_unuse_all_vectors(&proxy->pci_dev);
J
Jason Wang 已提交
1815 1816 1817 1818

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

1821
static Property virtio_pci_properties[] = {
1822 1823
    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1824 1825 1826 1827
    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),
1828 1829
    DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1830 1831
    DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1832 1833
    DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1834 1835 1836
    DEFINE_PROP_END_OF_LIST(),
};

1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp)
{
    VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev);
    VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
    PCIDevice *pci_dev = &proxy->pci_dev;

    if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) &&
        !(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_MODERN)) {
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
    }

    vpciklass->parent_dc_realize(qdev, errp);
}

1851 1852 1853 1854
static void virtio_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1855
    VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1856

1857
    dc->props = virtio_pci_properties;
1858
    k->realize = virtio_pci_realize;
1859 1860 1861 1862
    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;
1863 1864
    vpciklass->parent_dc_realize = dc->realize;
    dc->realize = virtio_pci_dc_realize;
1865
    dc->reset = virtio_pci_reset;
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
}

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,
};

1877 1878 1879
/* virtio-blk-pci */

static Property virtio_blk_pci_properties[] = {
1880
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1881 1882 1883 1884 1885 1886
    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(),
};

1887
static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1888 1889 1890
{
    VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1891

1892
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1893
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1894 1895 1896 1897 1898 1899 1900 1901
}

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);

1902
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1903
    dc->props = virtio_blk_pci_properties;
1904
    k->realize = virtio_blk_pci_realize;
1905 1906 1907 1908 1909 1910 1911 1912 1913
    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);
1914 1915 1916

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BLK);
1917 1918
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread",
                              &error_abort);
1919 1920
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
}

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,
};

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
/* 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(),
};

1941
static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1942 1943 1944
{
    VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1945
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1946 1947
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
1948 1949

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

1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
    /*
     * 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);
    }

1963
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1964
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1965 1966 1967 1968 1969 1970 1971
}

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);
1972 1973

    k->realize = virtio_scsi_pci_realize;
1974
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
    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);
1985 1986 1987

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SCSI);
1988 1989
    object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread",
                              &error_abort);
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
}

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,
};

2000 2001 2002 2003 2004 2005 2006 2007 2008
/* 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(),
};

2009
static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
{
    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));
2020
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2021 2022 2023 2024 2025 2026 2027
}

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);
2028
    k->realize = vhost_scsi_pci_realize;
2029
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
    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);
2040 2041 2042

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_SCSI);
G
Gonglei 已提交
2043 2044
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
}

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

2056 2057 2058
/* virtio-balloon-pci */

static Property virtio_balloon_pci_properties[] = {
2059
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2060 2061 2062
    DEFINE_PROP_END_OF_LIST(),
};

2063
static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
{
    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));
2074
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2075 2076 2077 2078 2079 2080 2081
}

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);
2082
    k->realize = virtio_balloon_pci_realize;
2083
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
    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);
2094

2095 2096
    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BALLOON);
2097 2098 2099 2100 2101
    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);
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
}

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,
};

2112 2113
/* virtio-serial-pci */

2114
static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2115 2116 2117
{
    VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
2118 2119
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132

    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;
    }

2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
    /*
     * 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);
    }

2143
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2144
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2145 2146 2147 2148 2149 2150
}

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),
2151
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2152 2153 2154 2155 2156 2157 2158 2159
    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);
2160
    k->realize = virtio_serial_pci_realize;
2161
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
    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);
2172 2173 2174

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SERIAL);
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184
}

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,
};

2185 2186 2187 2188 2189 2190 2191 2192 2193
/* 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(),
};

2194
static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2195
{
2196
    DeviceState *qdev = DEVICE(vpci_dev);
2197 2198 2199
    VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

2200 2201
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
                                  object_get_typename(OBJECT(qdev)));
2202
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2203
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
}

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;
2217
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2218
    dc->props = virtio_net_properties;
2219
    vpciklass->realize = virtio_net_pci_realize;
2220 2221 2222 2223 2224
}

static void virtio_net_pci_instance_init(Object *obj)
{
    VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2225 2226 2227

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_NET);
2228 2229
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
}

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,
};

2240 2241
/* virtio-rng-pci */

2242
static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2243 2244 2245
{
    VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&vrng->vdev);
2246
    Error *err = NULL;
2247 2248

    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2249 2250 2251 2252
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
        return;
2253 2254 2255
    }

    object_property_set_link(OBJECT(vrng),
2256
                             OBJECT(vrng->vdev.conf.rng), "rng",
2257 2258 2259 2260 2261 2262 2263 2264 2265
                             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);

2266
    k->realize = virtio_rng_pci_realize;
2267
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277

    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);
2278 2279 2280

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_RNG);
2281 2282
    object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), "rng",
                              &error_abort);
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
}

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,
};

2293 2294
/* virtio-input-pci */

2295
static Property virtio_input_pci_properties[] = {
2296 2297 2298 2299
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
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);

2318
    dc->props = virtio_input_pci_properties;
2319 2320 2321 2322 2323 2324
    k->realize = virtio_input_pci_realize;
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);

    pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
}

2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
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);
2343 2344 2345

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_KEYBOARD);
2346 2347 2348 2349 2350
}

static void virtio_mouse_initfn(Object *obj)
{
    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2351 2352 2353

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_MOUSE);
2354 2355 2356 2357 2358
}

static void virtio_tablet_initfn(Object *obj)
{
    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2359 2360 2361

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_TABLET);
2362 2363
}

2364 2365 2366 2367 2368 2369 2370 2371
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,
};

2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
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,
};

2402 2403 2404 2405 2406 2407 2408 2409 2410
#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 已提交
2411 2412 2413 2414 2415 2416
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,
};
2417
#endif
G
Gerd Hoffmann 已提交
2418

2419 2420
/* virtio-pci-bus */

2421 2422
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtIOPCIProxy *dev)
2423 2424
{
    DeviceState *qdev = DEVICE(dev);
2425 2426
    char virtio_bus_name[] = "virtio-bus";

2427
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2428
                        virtio_bus_name);
2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
}

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;
2441 2442 2443
    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;
2444 2445 2446
    k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
    k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
    k->vmstate_change = virtio_pci_vmstate_change;
2447
    k->device_plugged = virtio_pci_device_plugged;
2448
    k->device_unplugged = virtio_pci_device_unplugged;
2449
    k->query_nvectors = virtio_pci_query_nvectors;
2450 2451 2452 2453 2454
    k->ioeventfd_started = virtio_pci_ioeventfd_started;
    k->ioeventfd_set_started = virtio_pci_ioeventfd_set_started;
    k->ioeventfd_disabled = virtio_pci_ioeventfd_disabled;
    k->ioeventfd_set_disabled = virtio_pci_ioeventfd_set_disabled;
    k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
2455 2456 2457 2458 2459 2460 2461 2462 2463
}

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 已提交
2464
static void virtio_pci_register_types(void)
P
Paul Brook 已提交
2465
{
2466
    type_register_static(&virtio_rng_pci_info);
2467
    type_register_static(&virtio_input_pci_info);
2468 2469 2470 2471
    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);
2472
#ifdef CONFIG_LINUX
G
Gerd Hoffmann 已提交
2473
    type_register_static(&virtio_host_pci_info);
2474
#endif
2475
    type_register_static(&virtio_pci_bus_info);
2476
    type_register_static(&virtio_pci_info);
2477
#ifdef CONFIG_VIRTFS
2478
    type_register_static(&virtio_9p_pci_info);
2479
#endif
2480
    type_register_static(&virtio_blk_pci_info);
2481
    type_register_static(&virtio_scsi_pci_info);
2482
    type_register_static(&virtio_balloon_pci_info);
2483
    type_register_static(&virtio_serial_pci_info);
2484
    type_register_static(&virtio_net_pci_info);
2485 2486 2487
#ifdef CONFIG_VHOST_SCSI
    type_register_static(&vhost_scsi_pci_info);
#endif
P
Paul Brook 已提交
2488 2489
}

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