virtio-pci.c 87.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
    else {
        VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
P
Paolo Bonzini 已提交
76
        pci_set_irq(&proxy->pci_dev, atomic_read(&vdev->isr) & 1);
P
Paolo Bonzini 已提交
77
    }
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
static const VMStateDescription vmstate_virtio_pci_modern_queue_state = {
    .name = "virtio_pci/modern_queue_state",
    .version_id = 1,
    .minimum_version_id = 1,
    .fields = (VMStateField[]) {
        VMSTATE_UINT16(num, VirtIOPCIQueue),
        VMSTATE_UNUSED(1), /* enabled was stored as be16 */
        VMSTATE_BOOL(enabled, VirtIOPCIQueue),
        VMSTATE_UINT32_ARRAY(desc, VirtIOPCIQueue, 2),
        VMSTATE_UINT32_ARRAY(avail, VirtIOPCIQueue, 2),
        VMSTATE_UINT32_ARRAY(used, VirtIOPCIQueue, 2),
        VMSTATE_END_OF_LIST()
103 104 105 106 107 108 109
    }
};

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

110
    return virtio_pci_modern(proxy);
111 112
}

113
static const VMStateDescription vmstate_virtio_pci_modern_state_sub = {
114 115 116 117 118
    .name = "virtio_pci/modern_state",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = &virtio_pci_modern_state_needed,
    .fields = (VMStateField[]) {
119 120 121 122 123 124
        VMSTATE_UINT32(dfselect, VirtIOPCIProxy),
        VMSTATE_UINT32(gfselect, VirtIOPCIProxy),
        VMSTATE_UINT32_ARRAY(guest_features, VirtIOPCIProxy, 2),
        VMSTATE_STRUCT_ARRAY(vqs, VirtIOPCIProxy, VIRTIO_QUEUE_MAX, 0,
                             vmstate_virtio_pci_modern_queue_state,
                             VirtIOPCIQueue),
125 126 127 128 129 130 131 132 133 134 135 136 137
        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*[]) {
138
        &vmstate_virtio_pci_modern_state_sub,
139 140 141 142
        NULL
    }
};

143 144 145 146 147 148 149
static bool virtio_pci_has_extra_state(DeviceState *d)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163
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);
}

164
static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
165
{
166
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
167 168
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

169
    if (msix_present(&proxy->pci_dev))
P
Paolo Bonzini 已提交
170
        qemu_put_be16(f, virtio_queue_vector(vdev, n));
171 172
}

173
static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
174
{
175
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
176 177
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

178 179
    int ret;
    ret = pci_device_load(&proxy->pci_dev, f);
180
    if (ret) {
181
        return ret;
182
    }
183
    msix_unuse_all_vectors(&proxy->pci_dev);
184
    msix_load(&proxy->pci_dev, f);
185
    if (msix_present(&proxy->pci_dev)) {
P
Paolo Bonzini 已提交
186
        qemu_get_be16s(f, &vdev->config_vector);
187
    } else {
P
Paolo Bonzini 已提交
188
        vdev->config_vector = VIRTIO_NO_VECTOR;
189
    }
P
Paolo Bonzini 已提交
190 191
    if (vdev->config_vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, vdev->config_vector);
192
    }
193 194 195
    return 0;
}

196
static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
197
{
198
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
199 200
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

201
    uint16_t vector;
202 203 204 205 206
    if (msix_present(&proxy->pci_dev)) {
        qemu_get_be16s(f, &vector);
    } else {
        vector = VIRTIO_NO_VECTOR;
    }
P
Paolo Bonzini 已提交
207
    virtio_queue_set_vector(vdev, n, vector);
208 209 210
    if (vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, vector);
    }
211

212 213 214
    return 0;
}

215
static bool virtio_pci_ioeventfd_enabled(DeviceState *d)
216 217 218
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

219
    return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0;
220 221
}

222 223
#define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000

224 225 226 227 228 229
static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy)
{
    return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ?
        QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4;
}

230 231
static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier,
                                       int n, bool assign)
232
{
233
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
234 235
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, n);
236 237
    bool legacy = virtio_pci_legacy(proxy);
    bool modern = virtio_pci_modern(proxy);
238
    bool fast_mmio = kvm_ioeventfd_any_length_enabled();
239
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
240
    MemoryRegion *modern_mr = &proxy->notify.mr;
241
    MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr;
242
    MemoryRegion *legacy_mr = &proxy->bar;
243
    hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) *
244 245
                         virtio_get_queue_index(vq);
    hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY;
A
Avi Kivity 已提交
246

247
    if (assign) {
248
        if (modern) {
249 250 251 252 253 254 255
            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);
            }
256 257 258 259
            if (modern_pio) {
                memory_region_add_eventfd(modern_notify_mr, 0, 2,
                                              true, n, notifier);
            }
260 261 262 263 264
        }
        if (legacy) {
            memory_region_add_eventfd(legacy_mr, legacy_addr, 2,
                                      true, n, notifier);
        }
265
    } else {
266
        if (modern) {
267 268 269 270 271 272 273
            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);
            }
274 275 276 277
            if (modern_pio) {
                memory_region_del_eventfd(modern_notify_mr, 0, 2,
                                          true, n, notifier);
            }
278 279 280 281 282
        }
        if (legacy) {
            memory_region_del_eventfd(legacy_mr, legacy_addr, 2,
                                      true, n, notifier);
        }
283
    }
284
    return 0;
285 286
}

287
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
288
{
289
    virtio_bus_start_ioeventfd(&proxy->bus);
290 291
}

292
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
293
{
294
    virtio_bus_stop_ioeventfd(&proxy->bus);
295 296
}

P
Paul Brook 已提交
297 298 299
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
P
Paolo Bonzini 已提交
300
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
A
Avi Kivity 已提交
301
    hwaddr pa;
P
Paul Brook 已提交
302 303 304

    switch (addr) {
    case VIRTIO_PCI_GUEST_FEATURES:
305 306 307 308
        /* 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);
        }
309
        virtio_set_features(vdev, val);
P
Paul Brook 已提交
310 311
        break;
    case VIRTIO_PCI_QUEUE_PFN:
A
Avi Kivity 已提交
312
        pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
313
        if (pa == 0) {
314
            virtio_pci_reset(DEVICE(proxy));
315
        }
316 317
        else
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
P
Paul Brook 已提交
318 319
        break;
    case VIRTIO_PCI_QUEUE_SEL:
320
        if (val < VIRTIO_QUEUE_MAX)
P
Paul Brook 已提交
321 322 323
            vdev->queue_sel = val;
        break;
    case VIRTIO_PCI_QUEUE_NOTIFY:
324
        if (val < VIRTIO_QUEUE_MAX) {
325 326
            virtio_queue_notify(vdev, val);
        }
P
Paul Brook 已提交
327 328
        break;
    case VIRTIO_PCI_STATUS:
329 330 331 332
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
            virtio_pci_stop_ioeventfd(proxy);
        }

333
        virtio_set_status(vdev, val & 0xFF);
334 335 336 337 338

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

339
        if (vdev->status == 0) {
340
            virtio_pci_reset(DEVICE(proxy));
341
        }
342

343 344 345 346 347 348 349 350 351
        /* 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 已提交
352
        break;
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    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:
369 370
        error_report("%s: unexpected address 0x%x value 0x%x",
                     __func__, addr, val);
371
        break;
P
Paul Brook 已提交
372 373 374
    }
}

375
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
P
Paul Brook 已提交
376
{
P
Paolo Bonzini 已提交
377
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
P
Paul Brook 已提交
378 379 380 381
    uint32_t ret = 0xFFFFFFFF;

    switch (addr) {
    case VIRTIO_PCI_HOST_FEATURES:
C
Cornelia Huck 已提交
382
        ret = vdev->host_features;
P
Paul Brook 已提交
383 384
        break;
    case VIRTIO_PCI_GUEST_FEATURES:
385
        ret = vdev->guest_features;
P
Paul Brook 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
        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. */
P
Paolo Bonzini 已提交
402
        ret = atomic_xchg(&vdev->isr, 0);
403
        pci_irq_deassert(&proxy->pci_dev);
P
Paul Brook 已提交
404
        break;
405 406 407 408 409 410
    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 已提交
411 412 413 414 415 416 417
    default:
        break;
    }

    return ret;
}

418 419
static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
                                       unsigned size)
P
Paul Brook 已提交
420 421
{
    VirtIOPCIProxy *proxy = opaque;
P
Paolo Bonzini 已提交
422
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
423
    uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
424
    uint64_t val = 0;
425
    if (addr < config) {
426
        return virtio_ioport_read(proxy, addr);
427 428
    }
    addr -= config;
P
Paul Brook 已提交
429

430 431
    switch (size) {
    case 1:
P
Paolo Bonzini 已提交
432
        val = virtio_config_readb(vdev, addr);
433 434
        break;
    case 2:
P
Paolo Bonzini 已提交
435
        val = virtio_config_readw(vdev, addr);
436
        if (virtio_is_big_endian(vdev)) {
437 438
            val = bswap16(val);
        }
439 440
        break;
    case 4:
P
Paolo Bonzini 已提交
441
        val = virtio_config_readl(vdev, addr);
442
        if (virtio_is_big_endian(vdev)) {
443 444
            val = bswap32(val);
        }
445
        break;
446
    }
447
    return val;
P
Paul Brook 已提交
448 449
}

450 451
static void virtio_pci_config_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned size)
P
Paul Brook 已提交
452 453
{
    VirtIOPCIProxy *proxy = opaque;
454
    uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev);
P
Paolo Bonzini 已提交
455
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
456 457 458 459 460
    if (addr < config) {
        virtio_ioport_write(proxy, addr, val);
        return;
    }
    addr -= config;
461 462 463 464 465 466
    /*
     * Virtio-PCI is odd. Ioports are LE but config space is target native
     * endian.
     */
    switch (size) {
    case 1:
P
Paolo Bonzini 已提交
467
        virtio_config_writeb(vdev, addr, val);
468 469
        break;
    case 2:
470
        if (virtio_is_big_endian(vdev)) {
471 472
            val = bswap16(val);
        }
P
Paolo Bonzini 已提交
473
        virtio_config_writew(vdev, addr, val);
474 475
        break;
    case 4:
476
        if (virtio_is_big_endian(vdev)) {
477 478
            val = bswap32(val);
        }
P
Paolo Bonzini 已提交
479
        virtio_config_writel(vdev, addr, val);
480
        break;
481
    }
P
Paul Brook 已提交
482 483
}

A
Avi Kivity 已提交
484
static const MemoryRegionOps virtio_pci_config_ops = {
485 486 487 488 489 490
    .read = virtio_pci_config_read,
    .write = virtio_pci_config_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 4,
    },
491
    .endianness = DEVICE_LITTLE_ENDIAN,
A
Avi Kivity 已提交
492
};
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
static MemoryRegion *virtio_address_space_lookup(VirtIOPCIProxy *proxy,
                                                 hwaddr *off, int len)
{
    int i;
    VirtIOPCIRegion *reg;

    for (i = 0; i < ARRAY_SIZE(proxy->regs); ++i) {
        reg = &proxy->regs[i];
        if (*off >= reg->offset &&
            *off + len <= reg->offset + reg->size) {
            *off -= reg->offset;
            return &reg->mr;
        }
    }

    return NULL;
}

512 513 514 515 516 517 518 519 520 521 522 523 524
/* 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
525
void virtio_address_space_write(VirtIOPCIProxy *proxy, hwaddr addr,
526 527
                                const uint8_t *buf, int len)
{
528 529
    uint64_t val;
    MemoryRegion *mr;
530 531 532 533 534 535

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

536 537 538 539 540
    mr = virtio_address_space_lookup(proxy, &addr, len);
    if (!mr) {
        return;
    }

541 542 543 544 545 546 547 548
    /* Make sure caller aligned buf properly */
    assert(!(((uintptr_t)buf) & (len - 1)));

    switch (len) {
    case 1:
        val = pci_get_byte(buf);
        break;
    case 2:
549
        val = cpu_to_le16(pci_get_word(buf));
550 551
        break;
    case 4:
552
        val = cpu_to_le32(pci_get_long(buf));
553 554 555
        break;
    default:
        /* As length is under guest control, handle illegal values. */
556
        return;
557
    }
558
    memory_region_dispatch_write(mr, addr, val, len, MEMTXATTRS_UNSPECIFIED);
559 560 561
}

static void
562 563
virtio_address_space_read(VirtIOPCIProxy *proxy, hwaddr addr,
                          uint8_t *buf, int len)
564
{
565 566
    uint64_t val;
    MemoryRegion *mr;
567 568 569 570 571 572

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

573 574 575 576 577
    mr = virtio_address_space_lookup(proxy, &addr, len);
    if (!mr) {
        return;
    }

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

581
    memory_region_dispatch_read(mr, addr, &val, len, MEMTXATTRS_UNSPECIFIED);
582 583 584 585 586
    switch (len) {
    case 1:
        pci_set_byte(buf, val);
        break;
    case 2:
587
        pci_set_word(buf, le16_to_cpu(val));
588 589
        break;
    case 4:
590
        pci_set_long(buf, le32_to_cpu(val));
591 592 593 594 595 596 597
        break;
    default:
        /* As length is under guest control, handle illegal values. */
        break;
    }
}

598 599 600
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
                                uint32_t val, int len)
{
601
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
P
Paolo Bonzini 已提交
602
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
603
    struct virtio_pci_cfg_cap *cfg;
604

605 606 607
    pci_default_write_config(pci_dev, address, val, len);

    if (range_covers_byte(address, len, PCI_COMMAND) &&
608
        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
609
        virtio_pci_stop_ioeventfd(proxy);
610
        virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
611
    }
612 613 614 615 616 617 618 619 620 621 622 623

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

624 625
        if (len == 1 || len == 2 || len == 4) {
            assert(len <= sizeof cfg->pci_cfg_data);
626
            virtio_address_space_write(proxy, off, cfg->pci_cfg_data, len);
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
        }
    }
}

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

648 649
        if (len == 1 || len == 2 || len == 4) {
            assert(len <= sizeof cfg->pci_cfg_data);
650
            virtio_address_space_read(proxy, off, cfg->pci_cfg_data, len);
651 652 653 654
        }
    }

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

657 658
static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
                                        unsigned int queue_no,
659
                                        unsigned int vector)
660 661
{
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
662
    int ret;
663 664

    if (irqfd->users == 0) {
665
        ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev);
666 667 668 669 670 671 672 673 674 675 676
        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)
677 678 679 680 681 682 683
{
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
    if (--irqfd->users == 0) {
        kvm_irqchip_release_virq(kvm_state, irqfd->virq);
    }
}

684 685 686 687 688
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 已提交
689 690
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
691
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
692
    return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq);
693 694 695 696 697
}

static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
                                      unsigned int queue_no,
                                      unsigned int vector)
698
{
P
Paolo Bonzini 已提交
699 700
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
701
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
702
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
703
    int ret;
704

705
    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
706
    assert(ret == 0);
707
}
708

709 710 711
static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
{
    PCIDevice *dev = &proxy->pci_dev;
P
Paolo Bonzini 已提交
712
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
713
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
714 715 716 717 718 719 720 721 722 723 724
    unsigned int vector;
    int ret, queue_no;

    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;
        }
725
        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
726 727
        if (ret < 0) {
            goto undo;
728
        }
729 730 731
        /* If guest supports masking, set up irqfd now.
         * Otherwise, delay until unmasked in the frontend.
         */
732
        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
733 734 735 736 737 738
            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
            if (ret < 0) {
                kvm_virtio_pci_vq_vector_release(proxy, vector);
                goto undo;
            }
        }
739 740
    }
    return 0;
741 742 743 744 745 746 747

undo:
    while (--queue_no >= 0) {
        vector = virtio_queue_vector(vdev, queue_no);
        if (vector >= msix_nr_vectors_allocated(dev)) {
            continue;
        }
748
        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
749
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
750
        }
751 752 753
        kvm_virtio_pci_vq_vector_release(proxy, vector);
    }
    return ret;
754 755
}

756 757 758
static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
{
    PCIDevice *dev = &proxy->pci_dev;
P
Paolo Bonzini 已提交
759
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
760 761
    unsigned int vector;
    int queue_no;
762
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
763 764 765 766 767 768 769 770 771

    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;
        }
772 773 774
        /* If guest supports masking, clean up irqfd now.
         * Otherwise, it was cleaned when masked in the frontend.
         */
775
        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
776
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
777
        }
778 779 780 781
        kvm_virtio_pci_vq_vector_release(proxy, vector);
    }
}

782 783 784 785
static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
                                       unsigned int queue_no,
                                       unsigned int vector,
                                       MSIMessage msg)
786
{
P
Paolo Bonzini 已提交
787 788 789
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
    VirtQueue *vq = virtio_get_queue(vdev, queue_no);
790
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
791
    VirtIOIRQFD *irqfd;
792
    int ret = 0;
793

794 795 796
    if (proxy->vector_irqfd) {
        irqfd = &proxy->vector_irqfd[vector];
        if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
797 798
            ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg,
                                               &proxy->pci_dev);
799 800 801
            if (ret < 0) {
                return ret;
            }
802
            kvm_irqchip_commit_routes(kvm_state);
803 804 805
        }
    }

806 807 808
    /* If guest supports masking, irqfd is already setup, unmask it.
     * Otherwise, set it up now.
     */
809
    if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
810
        k->guest_notifier_mask(vdev, queue_no, false);
811
        /* Test after unmasking to avoid losing events. */
812
        if (k->guest_notifier_pending &&
P
Paolo Bonzini 已提交
813
            k->guest_notifier_pending(vdev, queue_no)) {
814 815 816 817
            event_notifier_set(n);
        }
    } else {
        ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
818
    }
819
    return ret;
820 821
}

822
static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
823 824 825
                                             unsigned int queue_no,
                                             unsigned int vector)
{
P
Paolo Bonzini 已提交
826 827
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
828

829 830 831
    /* If guest supports masking, keep irqfd but mask it.
     * Otherwise, clean it up now.
     */ 
832
    if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
P
Paolo Bonzini 已提交
833
        k->guest_notifier_mask(vdev, queue_no, true);
834
    } else {
835
        kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
836
    }
837 838
}

839 840
static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
                                    MSIMessage msg)
841 842
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
843
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
844 845
    VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
    int ret, index, unmasked = 0;
846

847 848 849
    while (vq) {
        index = virtio_get_queue_index(vq);
        if (!virtio_queue_get_num(vdev, index)) {
850 851
            break;
        }
852 853 854 855 856 857
        if (index < proxy->nvqs_with_notifiers) {
            ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg);
            if (ret < 0) {
                goto undo;
            }
            ++unmasked;
858
        }
859
        vq = virtio_vector_next_queue(vq);
860
    }
861

862 863 864
    return 0;

undo:
865
    vq = virtio_vector_first_queue(vdev, vector);
866
    while (vq && unmasked >= 0) {
867
        index = virtio_get_queue_index(vq);
868 869 870 871
        if (index < proxy->nvqs_with_notifiers) {
            virtio_pci_vq_vector_mask(proxy, index, vector);
            --unmasked;
        }
872
        vq = virtio_vector_next_queue(vq);
873 874 875 876
    }
    return ret;
}

877
static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
878 879
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
880
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
881 882
    VirtQueue *vq = virtio_vector_first_queue(vdev, vector);
    int index;
883

884 885 886
    while (vq) {
        index = virtio_get_queue_index(vq);
        if (!virtio_queue_get_num(vdev, index)) {
887 888
            break;
        }
889 890 891
        if (index < proxy->nvqs_with_notifiers) {
            virtio_pci_vq_vector_mask(proxy, index, vector);
        }
892
        vq = virtio_vector_next_queue(vq);
893 894 895
    }
}

896 897 898
static void virtio_pci_vector_poll(PCIDevice *dev,
                                   unsigned int vector_start,
                                   unsigned int vector_end)
899 900
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
P
Paolo Bonzini 已提交
901
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
902
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
903 904 905 906 907
    int queue_no;
    unsigned int vector;
    EventNotifier *notifier;
    VirtQueue *vq;

908
    for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
909 910 911 912 913 914 915 916 917 918
        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);
919 920
        if (k->guest_notifier_pending) {
            if (k->guest_notifier_pending(vdev, queue_no)) {
921 922 923
                msix_set_pending(dev, vector);
            }
        } else if (event_notifier_test_and_clear(notifier)) {
924 925 926 927 928 929 930
            msix_set_pending(dev, vector);
        }
    }
}

static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
                                         bool with_irqfd)
931
{
932
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
933 934 935
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
    VirtQueue *vq = virtio_get_queue(vdev, n);
936 937 938 939 940 941 942
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);

    if (assign) {
        int r = event_notifier_init(notifier, 0);
        if (r < 0) {
            return r;
        }
943
        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
944
    } else {
945
        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
946 947 948
        event_notifier_cleanup(notifier);
    }

949 950 951
    if (!msix_enabled(&proxy->pci_dev) &&
        vdev->use_guest_notifier_mask &&
        vdc->guest_notifier_mask) {
P
Paolo Bonzini 已提交
952
        vdc->guest_notifier_mask(vdev, n, !assign);
953 954
    }

955 956 957
    return 0;
}

958
static bool virtio_pci_query_guest_notifiers(DeviceState *d)
959
{
960
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
961 962 963
    return msix_enabled(&proxy->pci_dev);
}

964
static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
965
{
966
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
967
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
968
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
969
    int r, n;
970 971
    bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
        kvm_msi_via_irqfd_enabled();
972

973
    nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
974 975 976 977 978 979 980 981

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

    proxy->nvqs_with_notifiers = nvqs;

982
    /* Must unset vector notifier while guest notifier is still assigned */
983
    if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
984
        msix_unset_vector_notifiers(&proxy->pci_dev);
985 986 987 988 989
        if (proxy->vector_irqfd) {
            kvm_virtio_pci_vector_release(proxy, nvqs);
            g_free(proxy->vector_irqfd);
            proxy->vector_irqfd = NULL;
        }
990 991
    }

992
    for (n = 0; n < nvqs; n++) {
993 994 995 996
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }

997
        r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
998 999 1000 1001 1002
        if (r < 0) {
            goto assign_error;
        }
    }

1003
    /* Must set vector notifier after guest notifier has been assigned */
1004
    if ((with_irqfd || k->guest_notifier_mask) && assign) {
1005 1006 1007 1008 1009 1010 1011 1012
        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;
            }
1013
        }
1014
        r = msix_set_vector_notifiers(&proxy->pci_dev,
1015 1016 1017
                                      virtio_pci_vector_unmask,
                                      virtio_pci_vector_mask,
                                      virtio_pci_vector_poll);
1018
        if (r < 0) {
1019
            goto notifiers_error;
1020 1021 1022
        }
    }

1023 1024
    return 0;

1025
notifiers_error:
1026 1027 1028 1029
    if (with_irqfd) {
        assert(assign);
        kvm_virtio_pci_vector_release(proxy, nvqs);
    }
1030

1031 1032
assign_error:
    /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
1033
    assert(assign);
1034
    while (--n >= 0) {
1035
        virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
1036 1037 1038 1039
    }
    return r;
}

1040
static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1041
{
1042
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
1043
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1044 1045

    if (running) {
1046 1047 1048 1049 1050
        /* 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) &&
1051
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1052 1053 1054
            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
                                     proxy->pci_dev.config[PCI_COMMAND] |
                                     PCI_COMMAND_MASTER, 1);
1055
        }
1056
        virtio_pci_start_ioeventfd(proxy);
1057
    } else {
1058
        virtio_pci_stop_ioeventfd(proxy);
1059 1060 1061
    }
}

1062
#ifdef CONFIG_VIRTFS
1063
static void virtio_9p_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1064
{
1065 1066
    V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1067

1068
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1069
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1070 1071
}

1072 1073 1074
static Property virtio_9p_pci_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1075 1076 1077 1078
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

1079
static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
1080 1081
{
    DeviceClass *dc = DEVICE_CLASS(klass);
1082 1083
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1084

1085
    k->realize = virtio_9p_pci_realize;
1086 1087 1088 1089
    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;
1090
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1091
    dc->props = virtio_9p_pci_properties;
1092 1093
}

1094 1095 1096
static void virtio_9p_pci_instance_init(Object *obj)
{
    V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
1097 1098 1099

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_9P);
1100 1101 1102 1103 1104 1105 1106 1107
}

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,
1108
};
1109
#endif /* CONFIG_VIRTFS */
1110

1111 1112 1113 1114
/*
 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
 */

1115 1116 1117 1118 1119 1120 1121
static int virtio_pci_query_nvectors(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);

    return proxy->nvectors;
}

J
Jason Wang 已提交
1122 1123 1124 1125 1126
static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
    PCIDevice *dev = &proxy->pci_dev;

1127
    return pci_get_address_space(dev);
J
Jason Wang 已提交
1128 1129
}

1130
static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1131 1132 1133 1134 1135
                                   struct virtio_pci_cap *cap)
{
    PCIDevice *dev = &proxy->pci_dev;
    int offset;

1136 1137
    offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0,
                                cap->cap_len, &error_abort);
1138 1139 1140 1141

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

    return offset;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
}

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) {
1160 1161 1162
            VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);

            val = (vdev->host_features & ~vdc->legacy_features) >>
1163
                (32 * proxy->dfselect);
1164 1165 1166 1167 1168 1169
        }
        break;
    case VIRTIO_PCI_COMMON_GFSELECT:
        val = proxy->gfselect;
        break;
    case VIRTIO_PCI_COMMON_GF:
G
Gonglei 已提交
1170
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
            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:
1188
        val = vdev->generation;
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
        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 已提交
1245
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
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
            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) {
1272
            virtio_pci_reset(DEVICE(proxy));
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
        }

        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:
        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 已提交
1303
        proxy->vqs[vdev->queue_sel].enabled = 1;
1304 1305 1306 1307 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_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;
1339 1340
    VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent);
    unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1341 1342 1343 1344 1345 1346

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

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
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);
    }
}

1358 1359 1360 1361 1362
static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr,
                                    unsigned size)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
P
Paolo Bonzini 已提交
1363
    uint64_t val = atomic_xchg(&vdev->isr, 0);
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
    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:
1382
        val = virtio_config_modern_readb(vdev, addr);
1383 1384
        break;
    case 2:
1385
        val = virtio_config_modern_readw(vdev, addr);
1386 1387
        break;
    case 4:
1388
        val = virtio_config_modern_readl(vdev, addr);
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
        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:
1400
        virtio_config_modern_writeb(vdev, addr, val);
1401 1402
        break;
    case 2:
1403
        virtio_config_modern_writew(vdev, addr, val);
1404 1405
        break;
    case 4:
1406
        virtio_config_modern_writel(vdev, addr, val);
1407 1408 1409 1410
        break;
    }
}

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
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,
    };
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
    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,
    };

1459 1460 1461 1462

    memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
                          &common_ops,
                          proxy,
1463 1464
                          "virtio-pci-common",
                          proxy->common.size);
1465

1466 1467 1468
    memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
                          &isr_ops,
                          proxy,
1469 1470
                          "virtio-pci-isr",
                          proxy->isr.size);
1471

1472 1473 1474
    memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
                          &device_ops,
                          virtio_bus_get_device(&proxy->bus),
1475 1476
                          "virtio-pci-device",
                          proxy->device.size);
1477

1478 1479 1480 1481
    memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
                          &notify_ops,
                          virtio_bus_get_device(&proxy->bus),
                          "virtio-pci-notify",
1482
                          proxy->notify.size);
1483 1484 1485 1486 1487

    memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
                          &notify_pio_ops,
                          virtio_bus_get_device(&proxy->bus),
                          "virtio-pci-notify-pio",
1488
                          proxy->notify_pio.size);
1489 1490 1491
}

static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1492
                                         VirtIOPCIRegion *region,
1493 1494 1495
                                         struct virtio_pci_cap *cap,
                                         MemoryRegion *mr,
                                         uint8_t bar)
1496
{
1497
    memory_region_add_subregion(mr, region->offset, &region->mr);
1498

1499
    cap->cfg_type = region->type;
1500
    cap->bar = bar;
1501
    cap->offset = cpu_to_le32(region->offset);
1502
    cap->length = cpu_to_le32(region->size);
1503
    virtio_pci_add_mem_cap(proxy, cap);
1504 1505 1506 1507 1508 1509 1510 1511

}

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,
1512
                                 &proxy->modern_bar, proxy->modern_mem_bar_idx);
1513
}
1514

1515 1516 1517 1518 1519
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,
1520
                                 &proxy->io_bar, proxy->modern_io_bar_idx);
1521 1522 1523 1524
}

static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
                                               VirtIOPCIRegion *region)
1525 1526 1527 1528 1529
{
    memory_region_del_subregion(&proxy->modern_bar,
                                &region->mr);
}

1530 1531 1532 1533 1534 1535 1536
static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
                                              VirtIOPCIRegion *region)
{
    memory_region_del_subregion(&proxy->io_bar,
                                &region->mr);
}

1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
static void virtio_pci_pre_plugged(DeviceState *d, Error **errp)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);

    if (virtio_pci_modern(proxy)) {
        virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1);
    }

    virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE);
}

1549
/* This is called by virtio-bus just after the device is plugged. */
J
Jason Wang 已提交
1550
static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1551 1552 1553
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
    VirtioBusState *bus = &proxy->bus;
1554
    bool legacy = virtio_pci_legacy(proxy);
1555
    bool modern;
1556
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1557 1558
    uint8_t *config;
    uint32_t size;
C
Cornelia Huck 已提交
1559
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1560

1561 1562 1563 1564
    /*
     * Virtio capabilities present without
     * VIRTIO_F_VERSION_1 confuses guests
     */
1565 1566
    if (!proxy->ignore_backend_features &&
            !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
        virtio_pci_disable_modern(proxy);

        if (!legacy) {
            error_setg(errp, "Device doesn't support modern mode, and legacy"
                             " mode is disabled");
            error_append_hint(errp, "Set disable-legacy to off\n");

            return;
        }
    }

    modern = virtio_pci_modern(proxy);

1580 1581 1582 1583
    config = proxy->pci_dev.config;
    if (proxy->class_code) {
        pci_config_set_class(config, proxy->class_code);
    }
1584 1585

    if (legacy) {
J
Jason Wang 已提交
1586 1587 1588 1589 1590
        if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
            error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by"
                       "neither legacy nor transitional device.");
            return ;
        }
1591 1592 1593 1594 1595
        /*
         * Legacy and transitional devices use specific subsystem IDs.
         * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID)
         * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default.
         */
1596 1597 1598 1599 1600 1601 1602 1603 1604
        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);
    }
1605 1606
    config[PCI_INTERRUPT_PIN] = 1;

1607

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

1626
        struct virtio_pci_cfg_cap *cfg_mask;
1627

1628
        virtio_pci_modern_regions_init(proxy);
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638

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

1639
            pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
1640 1641 1642 1643 1644
                             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_idx,
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
    if (proxy->nvectors) {
        int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1662
                                          proxy->msix_bar_idx, NULL);
1663
        if (err) {
1664
            /* Notice when a system that supports MSIx can't initialize it */
1665 1666 1667 1668 1669 1670
            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_idx,
1686
                         PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1687
    }
1688 1689
}

1690 1691 1692
static void virtio_pci_device_unplugged(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1693
    bool modern = virtio_pci_modern(proxy);
1694
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1695 1696

    virtio_pci_stop_ioeventfd(proxy);
1697 1698

    if (modern) {
1699 1700 1701 1702 1703 1704 1705
        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);
        }
1706
    }
1707 1708
}

1709
static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1710
{
1711
    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1712
    VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1713 1714
    bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) &&
                     !pci_bus_is_root(pci_get_bus(pci_dev));
1715

1716
    if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1717 1718 1719
        proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
    }

1720 1721 1722 1723 1724 1725 1726 1727 1728
    /*
     * 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
     *
     */
1729 1730 1731 1732
    proxy->legacy_io_bar_idx  = 0;
    proxy->msix_bar_idx       = 1;
    proxy->modern_io_bar_idx  = 2;
    proxy->modern_mem_bar_idx = 4;
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

    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;
1747
    proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
1748 1749
    proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;

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

1754 1755
    /* subclasses can enforce modern, so do this unconditionally */
    memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1756 1757
                       /* PCI BAR regions must be powers of 2 */
                       pow2ceil(proxy->notify.offset + proxy->notify.size));
1758

1759 1760 1761 1762
    if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
        proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
    }

1763
    if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
1764 1765 1766 1767 1768 1769 1770
        error_setg(errp, "device cannot work as neither modern nor legacy mode"
                   " is enabled");
        error_append_hint(errp, "Set either disable-modern or disable-legacy"
                          " to off\n");
        return;
    }

1771
    if (pcie_port && pci_is_express(pci_dev)) {
1772 1773 1774 1775 1776
        int pos;

        pos = pcie_endpoint_cap_init(pci_dev, 0);
        assert(pos > 0);

1777 1778 1779 1780 1781 1782
        pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0,
                                 PCI_PM_SIZEOF, errp);
        if (pos < 0) {
            return;
        }

1783
        pci_dev->exp.pm_cap = pos;
1784 1785 1786 1787 1788 1789

        /*
         * 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);
1790

1791 1792 1793 1794 1795
        if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) {
            /* Init error enabling flags */
            pcie_cap_deverr_init(pci_dev);
        }

1796 1797 1798 1799 1800
        if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) {
            /* Init Link Control Register */
            pcie_cap_lnkctl_init(pci_dev);
        }

1801 1802 1803 1804 1805 1806
        if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) {
            /* Init Power Management Control Register */
            pci_set_word(pci_dev->wmask + pos + PCI_PM_CTRL,
                         PCI_PM_CTRL_STATE_MASK);
        }

1807 1808 1809 1810
        if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
            pcie_ats_init(pci_dev, 256);
        }

1811 1812 1813 1814 1815 1816
    } 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;
1817 1818
    }

1819
    virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1820
    if (k->realize) {
1821
        k->realize(proxy, errp);
1822 1823 1824 1825 1826
    }
}

static void virtio_pci_exit(PCIDevice *pci_dev)
{
1827
    msix_uninit_exclusive_bar(pci_dev);
1828 1829
}

1830
static void virtio_pci_reset(DeviceState *qdev)
1831 1832 1833
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
    VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1834
    PCIDevice *dev = PCI_DEVICE(qdev);
J
Jason Wang 已提交
1835 1836
    int i;

1837 1838 1839
    virtio_pci_stop_ioeventfd(proxy);
    virtio_bus_reset(bus);
    msix_unuse_all_vectors(&proxy->pci_dev);
J
Jason Wang 已提交
1840 1841 1842

    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        proxy->vqs[i].enabled = 0;
1843 1844 1845 1846
        proxy->vqs[i].num = 0;
        proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0;
        proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0;
        proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0;
J
Jason Wang 已提交
1847
    }
1848 1849 1850

    if (pci_is_express(dev)) {
        pcie_cap_deverr_reset(dev);
1851
        pcie_cap_lnkctl_reset(dev);
1852 1853

        pci_set_word(dev->config + dev->exp.pm_cap + PCI_PM_CTRL, 0);
1854
    }
1855 1856
}

1857
static Property virtio_pci_properties[] = {
1858 1859
    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1860 1861 1862
    DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
                            ON_OFF_AUTO_AUTO),
    DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
1863 1864
    DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1865 1866
    DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1867 1868
    DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1869 1870
    DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
1871 1872
    DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy,
                     ignore_backend_features, false),
1873 1874
    DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_ATS_BIT, false),
1875 1876
    DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, true),
1877 1878
    DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true),
1879 1880
    DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_INIT_PM_BIT, true),
1881 1882 1883
    DEFINE_PROP_END_OF_LIST(),
};

1884 1885 1886 1887 1888 1889 1890
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) &&
1891
        virtio_pci_modern(proxy)) {
1892 1893 1894 1895 1896 1897
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
    }

    vpciklass->parent_dc_realize(qdev, errp);
}

1898 1899 1900 1901
static void virtio_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1902
    VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1903

1904
    dc->props = virtio_pci_properties;
1905
    k->realize = virtio_pci_realize;
1906 1907 1908 1909
    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;
1910 1911
    device_class_set_parent_realize(dc, virtio_pci_dc_realize,
                                    &vpciklass->parent_dc_realize);
1912
    dc->reset = virtio_pci_reset;
1913 1914 1915 1916 1917 1918 1919 1920 1921
}

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,
1922 1923 1924 1925 1926
    .interfaces = (InterfaceInfo[]) {
        { INTERFACE_PCIE_DEVICE },
        { INTERFACE_CONVENTIONAL_PCI_DEVICE },
        { }
    },
1927 1928
};

1929 1930 1931
/* virtio-blk-pci */

static Property virtio_blk_pci_properties[] = {
1932
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
1933 1934 1935 1936 1937 1938
    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(),
};

1939
static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
1940 1941 1942
{
    VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
1943

1944
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1945
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
1946 1947 1948 1949 1950 1951 1952 1953
}

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

1954
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1955
    dc->props = virtio_blk_pci_properties;
1956
    k->realize = virtio_blk_pci_realize;
1957 1958 1959 1960 1961 1962 1963 1964 1965
    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);
1966 1967 1968

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BLK);
1969 1970
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
}

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

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
/* vhost-user-blk */

static Property vhost_user_blk_pci_properties[] = {
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

static void vhost_user_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
{
    VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

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

static void vhost_user_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);

    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
    dc->props = vhost_user_blk_pci_properties;
    k->realize = vhost_user_blk_pci_realize;
    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 vhost_user_blk_pci_instance_init(Object *obj)
{
    VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(obj);

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_USER_BLK);
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
}

static const TypeInfo vhost_user_blk_pci_info = {
    .name           = TYPE_VHOST_USER_BLK_PCI,
    .parent         = TYPE_VIRTIO_PCI,
    .instance_size  = sizeof(VHostUserBlkPCI),
    .instance_init  = vhost_user_blk_pci_instance_init,
    .class_init     = vhost_user_blk_pci_class_init,
};
#endif

2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
/* 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(),
};

2043
static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2044 2045 2046
{
    VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
2047
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2048 2049
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
2050 2051

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

2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
    /*
     * 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);
    }

2065
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2066
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2067 2068 2069 2070 2071 2072 2073
}

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

    k->realize = virtio_scsi_pci_realize;
2076
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2077 2078 2079 2080 2081 2082 2083 2084 2085 2086
    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);
2087 2088 2089

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SCSI);
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
}

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

2100 2101 2102 2103 2104 2105 2106 2107 2108
/* 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(),
};

2109
static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
{
    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));
2120
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2121 2122 2123 2124 2125 2126 2127
}

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);
2128
    k->realize = vhost_scsi_pci_realize;
2129
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
    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);
2140 2141 2142

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_SCSI);
G
Gonglei 已提交
2143 2144
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
}

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

2156
#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
/* vhost-user-scsi-pci */
static Property vhost_user_scsi_pci_properties[] = {
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
                       DEV_NVECTORS_UNSPECIFIED),
    DEFINE_PROP_END_OF_LIST(),
};

static void vhost_user_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
{
    VHostUserSCSIPCI *dev = VHOST_USER_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));
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
}

static void vhost_user_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);
    k->realize = vhost_user_scsi_pci_realize;
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
    dc->props = vhost_user_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_user_scsi_pci_instance_init(Object *obj)
{
    VHostUserSCSIPCI *dev = VHOST_USER_SCSI_PCI(obj);

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_USER_SCSI);
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
}

static const TypeInfo vhost_user_scsi_pci_info = {
    .name          = TYPE_VHOST_USER_SCSI_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VHostUserSCSIPCI),
    .instance_init = vhost_user_scsi_pci_instance_init,
    .class_init    = vhost_user_scsi_pci_class_init,
};
#endif

2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
/* vhost-vsock-pci */

#ifdef CONFIG_VHOST_VSOCK
static Property vhost_vsock_pci_properties[] = {
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
    DEFINE_PROP_END_OF_LIST(),
};

static void vhost_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
{
    VHostVSockPCI *dev = VHOST_VSOCK_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

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

static void vhost_vsock_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);
    k->realize = vhost_vsock_pci_realize;
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
    dc->props = vhost_vsock_pci_properties;
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
    pcidev_k->revision = 0x00;
    pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
}

static void vhost_vsock_pci_instance_init(Object *obj)
{
    VHostVSockPCI *dev = VHOST_VSOCK_PCI(obj);

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

static const TypeInfo vhost_vsock_pci_info = {
    .name          = TYPE_VHOST_VSOCK_PCI,
    .parent        = TYPE_VIRTIO_PCI,
    .instance_size = sizeof(VHostVSockPCI),
    .instance_init = vhost_vsock_pci_instance_init,
    .class_init    = vhost_vsock_pci_class_init,
};
#endif

2259 2260 2261
/* virtio-balloon-pci */

static Property virtio_balloon_pci_properties[] = {
2262
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2263 2264 2265
    DEFINE_PROP_END_OF_LIST(),
};

2266
static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
{
    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));
2277
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2278 2279 2280 2281 2282 2283 2284
}

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);
2285
    k->realize = virtio_balloon_pci_realize;
2286
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
    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);
2297

2298 2299
    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BALLOON);
2300 2301 2302 2303 2304
    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);
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314
}

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

2315 2316
/* virtio-serial-pci */

2317
static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2318 2319 2320
{
    VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
2321 2322
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335

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

2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
    /*
     * 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);
    }

2346
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2347
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2348 2349 2350 2351 2352 2353
}

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),
2354
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2355 2356 2357 2358 2359 2360 2361 2362
    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);
2363
    k->realize = virtio_serial_pci_realize;
2364
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
    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);
2375 2376 2377

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SERIAL);
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
}

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

2388 2389 2390 2391
/* virtio-net-pci */

static Property virtio_net_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2392
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2393 2394 2395 2396
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
    DEFINE_PROP_END_OF_LIST(),
};

2397
static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2398
{
2399
    DeviceState *qdev = DEVICE(vpci_dev);
2400 2401 2402
    VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

2403 2404
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
                                  object_get_typename(OBJECT(qdev)));
2405
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2406
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
}

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;
2420
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2421
    dc->props = virtio_net_properties;
2422
    vpciklass->realize = virtio_net_pci_realize;
2423 2424 2425 2426 2427
}

static void virtio_net_pci_instance_init(Object *obj)
{
    VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2428 2429 2430

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_NET);
2431 2432
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442
}

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

2443 2444
/* virtio-rng-pci */

2445
static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2446 2447 2448
{
    VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&vrng->vdev);
2449
    Error *err = NULL;
2450 2451

    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2452 2453 2454 2455
    object_property_set_bool(OBJECT(vdev), true, "realized", &err);
    if (err) {
        error_propagate(errp, err);
        return;
2456 2457 2458
    }

    object_property_set_link(OBJECT(vrng),
2459
                             OBJECT(vrng->vdev.conf.rng), "rng",
2460 2461 2462 2463 2464 2465 2466 2467 2468
                             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);

2469
    k->realize = virtio_rng_pci_realize;
2470
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
2471 2472 2473 2474 2475 2476 2477 2478 2479 2480

    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);
2481 2482 2483

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_RNG);
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493
}

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

2494 2495
/* virtio-input-pci */

2496
static Property virtio_input_pci_properties[] = {
2497 2498 2499 2500
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_PROP_END_OF_LIST(),
};

2501 2502 2503 2504 2505 2506
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));
2507
    virtio_pci_force_virtio_1(vpci_dev);
2508 2509 2510 2511 2512 2513 2514 2515 2516
    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);

2517
    dc->props = virtio_input_pci_properties;
2518 2519 2520 2521 2522 2523
    k->realize = virtio_input_pci_realize;
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);

    pcidev_k->class_id = PCI_CLASS_INPUT_OTHER;
}

2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
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);
2542 2543 2544

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_KEYBOARD);
2545 2546 2547 2548 2549
}

static void virtio_mouse_initfn(Object *obj)
{
    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2550 2551 2552

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_MOUSE);
2553 2554 2555 2556 2557
}

static void virtio_tablet_initfn(Object *obj)
{
    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
2558 2559 2560

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_TABLET);
2561 2562
}

2563 2564 2565 2566 2567 2568 2569 2570
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,
};

2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
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,
};

2601 2602 2603 2604 2605 2606 2607 2608 2609
#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 已提交
2610 2611 2612 2613 2614 2615
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,
};
2616
#endif
G
Gerd Hoffmann 已提交
2617

2618 2619
/* virtio-pci-bus */

2620 2621
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtIOPCIProxy *dev)
2622 2623
{
    DeviceState *qdev = DEVICE(dev);
2624 2625
    char virtio_bus_name[] = "virtio-bus";

2626
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2627
                        virtio_bus_name);
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
}

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;
2640 2641 2642
    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;
2643 2644 2645
    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;
2646
    k->pre_plugged = virtio_pci_pre_plugged;
2647
    k->device_plugged = virtio_pci_device_plugged;
2648
    k->device_unplugged = virtio_pci_device_unplugged;
2649
    k->query_nvectors = virtio_pci_query_nvectors;
2650
    k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled;
2651
    k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
J
Jason Wang 已提交
2652
    k->get_dma_as = virtio_pci_get_dma_as;
2653 2654 2655 2656 2657 2658 2659 2660 2661
}

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 已提交
2662
static void virtio_pci_register_types(void)
P
Paul Brook 已提交
2663
{
2664
    type_register_static(&virtio_rng_pci_info);
2665
    type_register_static(&virtio_input_pci_info);
2666 2667 2668 2669
    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);
2670
#ifdef CONFIG_LINUX
G
Gerd Hoffmann 已提交
2671
    type_register_static(&virtio_host_pci_info);
2672
#endif
2673
    type_register_static(&virtio_pci_bus_info);
2674
    type_register_static(&virtio_pci_info);
2675
#ifdef CONFIG_VIRTFS
2676
    type_register_static(&virtio_9p_pci_info);
2677
#endif
2678
    type_register_static(&virtio_blk_pci_info);
2679 2680 2681
#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
    type_register_static(&vhost_user_blk_pci_info);
#endif
2682
    type_register_static(&virtio_scsi_pci_info);
2683
    type_register_static(&virtio_balloon_pci_info);
2684
    type_register_static(&virtio_serial_pci_info);
2685
    type_register_static(&virtio_net_pci_info);
2686 2687 2688
#ifdef CONFIG_VHOST_SCSI
    type_register_static(&vhost_scsi_pci_info);
#endif
2689
#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2690 2691
    type_register_static(&vhost_user_scsi_pci_info);
#endif
2692 2693 2694
#ifdef CONFIG_VHOST_VSOCK
    type_register_static(&vhost_vsock_pci_info);
#endif
P
Paul Brook 已提交
2695 2696
}

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