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

38
#define VIRTIO_PCI_REGION_SIZE(dev)     VIRTIO_PCI_CONFIG_OFF(msix_present(dev))
39

40 41
#undef VIRTIO_PCI_CONFIG

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

46 47
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtIOPCIProxy *dev);
48
static void virtio_pci_reset(DeviceState *qdev);
49

P
Paul Brook 已提交
50
/* virtio device */
51 52 53 54 55
/* 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 已提交
56

57 58 59 60
/* 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 已提交
61
{
62 63 64 65 66 67
    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 已提交
68

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

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

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

88 89 90 91 92 93 94 95 96 97 98 99
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()
100 101 102 103 104 105 106
    }
};

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

107
    return virtio_pci_modern(proxy);
108 109
}

110
static const VMStateDescription vmstate_virtio_pci_modern_state_sub = {
111 112 113 114 115
    .name = "virtio_pci/modern_state",
    .version_id = 1,
    .minimum_version_id = 1,
    .needed = &virtio_pci_modern_state_needed,
    .fields = (VMStateField[]) {
116 117 118 119 120 121
        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),
122 123 124 125 126 127 128 129 130 131 132 133 134
        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*[]) {
135
        &vmstate_virtio_pci_modern_state_sub,
136 137 138 139
        NULL
    }
};

140 141 142 143 144 145 146
static bool virtio_pci_has_extra_state(DeviceState *d)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

    return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA;
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160
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);
}

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

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

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

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

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

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

209 210 211
    return 0;
}

212
static bool virtio_pci_ioeventfd_enabled(DeviceState *d)
213 214 215
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);

216
    return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0;
217 218
}

219 220
#define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000

221 222 223 224 225 226
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;
}

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

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

284
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
285
{
286
    virtio_bus_start_ioeventfd(&proxy->bus);
287 288
}

289
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
290
{
291
    virtio_bus_stop_ioeventfd(&proxy->bus);
292 293
}

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

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

330
        virtio_set_status(vdev, val & 0xFF);
331 332 333 334 335

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

336
        if (vdev->status == 0) {
337
            virtio_pci_reset(DEVICE(proxy));
338
        }
339

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

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

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

    return ret;
}

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

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

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

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

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
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;
}

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

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

533 534 535 536 537
    mr = virtio_address_space_lookup(proxy, &addr, len);
    if (!mr) {
        return;
    }

538 539 540 541 542 543 544 545
    /* Make sure caller aligned buf properly */
    assert(!(((uintptr_t)buf) & (len - 1)));

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

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

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

570 571 572 573 574
    mr = virtio_address_space_lookup(proxy, &addr, len);
    if (!mr) {
        return;
    }

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

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

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

602 603 604
    pci_default_write_config(pci_dev, address, val, len);

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

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

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

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

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

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

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

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

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

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

702
    ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq);
703
    assert(ret == 0);
704
}
705

706 707 708
static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
{
    PCIDevice *dev = &proxy->pci_dev;
P
Paolo Bonzini 已提交
709
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
710
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
711 712 713 714 715 716 717 718 719 720 721
    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;
        }
722
        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector);
723 724
        if (ret < 0) {
            goto undo;
725
        }
726 727 728
        /* If guest supports masking, set up irqfd now.
         * Otherwise, delay until unmasked in the frontend.
         */
729
        if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) {
730 731 732 733 734 735
            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
            if (ret < 0) {
                kvm_virtio_pci_vq_vector_release(proxy, vector);
                goto undo;
            }
        }
736 737
    }
    return 0;
738 739 740 741 742 743 744

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

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

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

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

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

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

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

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

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

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

859 860 861
    return 0;

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

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

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

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

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

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

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

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

952 953 954
    return 0;
}

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

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

970
    nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX);
971 972 973 974 975 976 977 978

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

    proxy->nvqs_with_notifiers = nvqs;

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

989
    for (n = 0; n < nvqs; n++) {
990 991 992 993
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }

994
        r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
995 996 997 998 999
        if (r < 0) {
            goto assign_error;
        }
    }

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

1020 1021
    return 0;

1022
notifiers_error:
1023 1024 1025 1026
    if (with_irqfd) {
        assert(assign);
        kvm_virtio_pci_vector_release(proxy, nvqs);
    }
1027

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

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
static int virtio_pci_set_host_notifier_mr(DeviceState *d, int n,
                                           MemoryRegion *mr, bool assign)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
    int offset;

    if (n >= VIRTIO_QUEUE_MAX || !virtio_pci_modern(proxy) ||
        virtio_pci_queue_mem_mult(proxy) != memory_region_size(mr)) {
        return -1;
    }

    if (assign) {
        offset = virtio_pci_queue_mem_mult(proxy) * n;
        memory_region_add_subregion_overlap(&proxy->notify.mr, offset, mr, 1);
    } else {
        memory_region_del_subregion(&proxy->notify.mr, mr);
    }

    return 0;
}

1058
static void virtio_pci_vmstate_change(DeviceState *d, bool running)
1059
{
1060
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
P
Paolo Bonzini 已提交
1061
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1062 1063

    if (running) {
1064 1065 1066 1067 1068
        /* 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) &&
1069
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1070 1071 1072
            pci_default_write_config(&proxy->pci_dev, PCI_COMMAND,
                                     proxy->pci_dev.config[PCI_COMMAND] |
                                     PCI_COMMAND_MASTER, 1);
1073
        }
1074
        virtio_pci_start_ioeventfd(proxy);
1075
    } else {
1076
        virtio_pci_stop_ioeventfd(proxy);
1077 1078 1079
    }
}

1080 1081 1082 1083
/*
 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
 */

1084 1085 1086 1087 1088 1089 1090
static int virtio_pci_query_nvectors(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);

    return proxy->nvectors;
}

J
Jason Wang 已提交
1091 1092 1093 1094 1095
static AddressSpace *virtio_pci_get_dma_as(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
    PCIDevice *dev = &proxy->pci_dev;

1096
    return pci_get_address_space(dev);
J
Jason Wang 已提交
1097 1098
}

1099
static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
1100 1101 1102 1103 1104
                                   struct virtio_pci_cap *cap)
{
    PCIDevice *dev = &proxy->pci_dev;
    int offset;

1105 1106
    offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0,
                                cap->cap_len, &error_abort);
1107 1108 1109 1110

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

    return offset;
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
}

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) {
1129 1130 1131
            VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);

            val = (vdev->host_features & ~vdc->legacy_features) >>
1132
                (32 * proxy->dfselect);
1133 1134 1135 1136 1137 1138
        }
        break;
    case VIRTIO_PCI_COMMON_GFSELECT:
        val = proxy->gfselect;
        break;
    case VIRTIO_PCI_COMMON_GF:
G
Gonglei 已提交
1139
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
            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:
1157
        val = vdev->generation;
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 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
        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 已提交
1214
        if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) {
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
            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) {
1241
            virtio_pci_reset(DEVICE(proxy));
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        }

        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 已提交
1272
        proxy->vqs[vdev->queue_sel].enabled = 1;
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
        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;
1308 1309
    VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent);
    unsigned queue = addr / virtio_pci_queue_mem_mult(proxy);
1310 1311 1312 1313 1314 1315

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

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
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);
    }
}

1327 1328 1329 1330 1331
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 已提交
1332
    uint64_t val = atomic_xchg(&vdev->isr, 0);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
    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:
1351
        val = virtio_config_modern_readb(vdev, addr);
1352 1353
        break;
    case 2:
1354
        val = virtio_config_modern_readw(vdev, addr);
1355 1356
        break;
    case 4:
1357
        val = virtio_config_modern_readl(vdev, addr);
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
        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:
1369
        virtio_config_modern_writeb(vdev, addr, val);
1370 1371
        break;
    case 2:
1372
        virtio_config_modern_writew(vdev, addr, val);
1373 1374
        break;
    case 4:
1375
        virtio_config_modern_writel(vdev, addr, val);
1376 1377 1378 1379
        break;
    }
}

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
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,
    };
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    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,
    };

1428 1429 1430 1431

    memory_region_init_io(&proxy->common.mr, OBJECT(proxy),
                          &common_ops,
                          proxy,
1432 1433
                          "virtio-pci-common",
                          proxy->common.size);
1434

1435 1436 1437
    memory_region_init_io(&proxy->isr.mr, OBJECT(proxy),
                          &isr_ops,
                          proxy,
1438 1439
                          "virtio-pci-isr",
                          proxy->isr.size);
1440

1441 1442 1443
    memory_region_init_io(&proxy->device.mr, OBJECT(proxy),
                          &device_ops,
                          virtio_bus_get_device(&proxy->bus),
1444 1445
                          "virtio-pci-device",
                          proxy->device.size);
1446

1447 1448 1449 1450
    memory_region_init_io(&proxy->notify.mr, OBJECT(proxy),
                          &notify_ops,
                          virtio_bus_get_device(&proxy->bus),
                          "virtio-pci-notify",
1451
                          proxy->notify.size);
1452 1453 1454 1455 1456

    memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy),
                          &notify_pio_ops,
                          virtio_bus_get_device(&proxy->bus),
                          "virtio-pci-notify-pio",
1457
                          proxy->notify_pio.size);
1458 1459 1460
}

static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy,
1461
                                         VirtIOPCIRegion *region,
1462 1463 1464
                                         struct virtio_pci_cap *cap,
                                         MemoryRegion *mr,
                                         uint8_t bar)
1465
{
1466
    memory_region_add_subregion(mr, region->offset, &region->mr);
1467

1468
    cap->cfg_type = region->type;
1469
    cap->bar = bar;
1470
    cap->offset = cpu_to_le32(region->offset);
1471
    cap->length = cpu_to_le32(region->size);
1472
    virtio_pci_add_mem_cap(proxy, cap);
1473 1474 1475 1476 1477 1478 1479 1480

}

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,
1481
                                 &proxy->modern_bar, proxy->modern_mem_bar_idx);
1482
}
1483

1484 1485 1486 1487 1488
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,
1489
                                 &proxy->io_bar, proxy->modern_io_bar_idx);
1490 1491 1492 1493
}

static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy,
                                               VirtIOPCIRegion *region)
1494 1495 1496 1497 1498
{
    memory_region_del_subregion(&proxy->modern_bar,
                                &region->mr);
}

1499 1500 1501 1502 1503 1504 1505
static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy,
                                              VirtIOPCIRegion *region)
{
    memory_region_del_subregion(&proxy->io_bar,
                                &region->mr);
}

1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
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);
}

1518
/* This is called by virtio-bus just after the device is plugged. */
J
Jason Wang 已提交
1519
static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
1520 1521 1522
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
    VirtioBusState *bus = &proxy->bus;
1523
    bool legacy = virtio_pci_legacy(proxy);
1524
    bool modern;
1525
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1526 1527
    uint8_t *config;
    uint32_t size;
C
Cornelia Huck 已提交
1528
    VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1529

1530 1531 1532 1533
    /*
     * Virtio capabilities present without
     * VIRTIO_F_VERSION_1 confuses guests
     */
1534 1535
    if (!proxy->ignore_backend_features &&
            !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) {
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
        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);

1549 1550 1551 1552
    config = proxy->pci_dev.config;
    if (proxy->class_code) {
        pci_config_set_class(config, proxy->class_code);
    }
1553 1554

    if (legacy) {
J
Jason Wang 已提交
1555 1556
        if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
            error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by"
1557
                       " neither legacy nor transitional device");
J
Jason Wang 已提交
1558 1559
            return ;
        }
1560 1561 1562 1563 1564
        /*
         * 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.
         */
1565 1566 1567 1568 1569 1570 1571 1572 1573
        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);
    }
1574 1575
    config[PCI_INTERRUPT_PIN] = 1;

1576

1577
    if (modern) {
1578 1579
        struct virtio_pci_cap cap = {
            .cap_len = sizeof cap,
1580 1581 1582 1583
        };
        struct virtio_pci_notify_cap notify = {
            .cap.cap_len = sizeof notify,
            .notify_off_multiplier =
1584
                cpu_to_le32(virtio_pci_queue_mem_mult(proxy)),
1585
        };
1586 1587 1588 1589
        struct virtio_pci_cfg_cap cfg = {
            .cap.cap_len = sizeof cfg,
            .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG,
        };
1590 1591 1592 1593
        struct virtio_pci_notify_cap notify_pio = {
            .cap.cap_len = sizeof notify,
            .notify_off_multiplier = cpu_to_le32(0x0),
        };
1594

1595
        struct virtio_pci_cfg_cap *cfg_mask;
1596

1597
        virtio_pci_modern_regions_init(proxy);
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607

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

1608
            pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx,
1609 1610 1611 1612 1613
                             PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar);

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

1615
        pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx,
1616 1617 1618
                         PCI_BASE_ADDRESS_SPACE_MEMORY |
                         PCI_BASE_ADDRESS_MEM_PREFETCH |
                         PCI_BASE_ADDRESS_MEM_TYPE_64,
1619
                         &proxy->modern_bar);
1620 1621 1622 1623 1624 1625 1626

        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);
1627 1628
    }

1629 1630
    if (proxy->nvectors) {
        int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors,
1631
                                          proxy->msix_bar_idx, NULL);
1632
        if (err) {
1633
            /* Notice when a system that supports MSIx can't initialize it */
1634
            if (err != -ENOTSUP) {
1635 1636
                warn_report("unable to init msix vectors to %" PRIu32,
                            proxy->nvectors);
1637 1638 1639
            }
            proxy->nvectors = 0;
        }
1640 1641 1642
    }

    proxy->pci_dev.config_write = virtio_write_config;
1643
    proxy->pci_dev.config_read = virtio_read_config;
1644

1645 1646 1647
    if (legacy) {
        size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
            + virtio_bus_get_vdev_config_len(bus);
1648
        size = pow2ceil(size);
1649

1650 1651 1652
        memory_region_init_io(&proxy->bar, OBJECT(proxy),
                              &virtio_pci_config_ops,
                              proxy, "virtio-pci", size);
1653

1654
        pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx,
1655
                         PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
1656
    }
1657 1658
}

1659 1660 1661
static void virtio_pci_device_unplugged(DeviceState *d)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1662
    bool modern = virtio_pci_modern(proxy);
1663
    bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
1664 1665

    virtio_pci_stop_ioeventfd(proxy);
1666 1667

    if (modern) {
1668 1669 1670 1671 1672 1673 1674
        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);
        }
1675
    }
1676 1677
}

1678
static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
1679
{
1680
    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1681
    VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
1682 1683
    bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) &&
                     !pci_bus_is_root(pci_get_bus(pci_dev));
1684

1685
    if (kvm_enabled() && !kvm_has_many_ioeventfds()) {
1686 1687 1688
        proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
    }

1689 1690 1691 1692 1693 1694 1695 1696 1697
    /*
     * 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
     *
     */
1698 1699 1700 1701
    proxy->legacy_io_bar_idx  = 0;
    proxy->msix_bar_idx       = 1;
    proxy->modern_io_bar_idx  = 2;
    proxy->modern_mem_bar_idx = 4;
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715

    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;
1716
    proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX;
1717 1718
    proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG;

1719 1720 1721 1722
    proxy->notify_pio.offset = 0x0;
    proxy->notify_pio.size = 0x4;
    proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG;

1723 1724
    /* subclasses can enforce modern, so do this unconditionally */
    memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci",
1725 1726
                       /* PCI BAR regions must be powers of 2 */
                       pow2ceil(proxy->notify.offset + proxy->notify.size));
1727

1728 1729 1730 1731
    if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) {
        proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
    }

1732
    if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) {
1733 1734 1735 1736 1737 1738 1739
        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;
    }

1740
    if (pcie_port && pci_is_express(pci_dev)) {
1741 1742 1743 1744 1745
        int pos;

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

1746 1747 1748 1749 1750 1751
        pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0,
                                 PCI_PM_SIZEOF, errp);
        if (pos < 0) {
            return;
        }

1752
        pci_dev->exp.pm_cap = pos;
1753 1754 1755 1756 1757 1758

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

1760 1761 1762 1763 1764
        if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) {
            /* Init error enabling flags */
            pcie_cap_deverr_init(pci_dev);
        }

1765 1766 1767 1768 1769
        if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) {
            /* Init Link Control Register */
            pcie_cap_lnkctl_init(pci_dev);
        }

1770 1771 1772 1773 1774 1775
        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);
        }

1776 1777 1778 1779
        if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
            pcie_ats_init(pci_dev, 256);
        }

1780 1781 1782 1783 1784 1785
    } 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;
1786 1787
    }

1788
    virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy);
1789
    if (k->realize) {
1790
        k->realize(proxy, errp);
1791 1792 1793 1794 1795
    }
}

static void virtio_pci_exit(PCIDevice *pci_dev)
{
1796
    msix_uninit_exclusive_bar(pci_dev);
1797 1798
}

1799
static void virtio_pci_reset(DeviceState *qdev)
1800 1801 1802
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
    VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1803
    PCIDevice *dev = PCI_DEVICE(qdev);
J
Jason Wang 已提交
1804 1805
    int i;

1806 1807 1808
    virtio_pci_stop_ioeventfd(proxy);
    virtio_bus_reset(bus);
    msix_unuse_all_vectors(&proxy->pci_dev);
J
Jason Wang 已提交
1809 1810 1811

    for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
        proxy->vqs[i].enabled = 0;
1812 1813 1814 1815
        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 已提交
1816
    }
1817 1818 1819

    if (pci_is_express(dev)) {
        pcie_cap_deverr_reset(dev);
1820
        pcie_cap_lnkctl_reset(dev);
1821 1822

        pci_set_word(dev->config + dev->exp.pm_cap + PCI_PM_CTRL, 0);
1823
    }
1824 1825
}

1826
static Property virtio_pci_properties[] = {
1827 1828
    DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false),
1829 1830
    DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true),
1831 1832
    DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false),
1833 1834
    DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false),
1835 1836
    DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),
1837 1838
    DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy,
                     ignore_backend_features, false),
1839 1840
    DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_ATS_BIT, false),
1841 1842
    DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, true),
1843 1844
    DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true),
1845 1846
    DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_INIT_PM_BIT, true),
1847 1848 1849
    DEFINE_PROP_END_OF_LIST(),
};

1850 1851 1852 1853 1854 1855 1856
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) &&
1857
        virtio_pci_modern(proxy)) {
1858 1859 1860 1861 1862 1863
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
    }

    vpciklass->parent_dc_realize(qdev, errp);
}

1864 1865 1866 1867
static void virtio_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1868
    VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1869

1870
    dc->props = virtio_pci_properties;
1871
    k->realize = virtio_pci_realize;
1872 1873 1874 1875
    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;
1876 1877
    device_class_set_parent_realize(dc, virtio_pci_dc_realize,
                                    &vpciklass->parent_dc_realize);
1878
    dc->reset = virtio_pci_reset;
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
}

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

1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
static Property virtio_pci_generic_properties[] = {
    DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy,
                            ON_OFF_AUTO_AUTO),
    DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false),
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_pci_base_class_init(ObjectClass *klass, void *data)
{
    const VirtioPCIDeviceTypeInfo *t = data;
    if (t->class_init) {
        t->class_init(klass, NULL);
    }
}

static void virtio_pci_generic_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->props = virtio_pci_generic_properties;
}

/* Used when the generic type and the base type is the same */
static void virtio_pci_generic_base_class_init(ObjectClass *klass, void *data)
{
    virtio_pci_base_class_init(klass, data);
    virtio_pci_generic_class_init(klass, NULL);
}

static void virtio_pci_transitional_instance_init(Object *obj)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(obj);

    proxy->disable_legacy = ON_OFF_AUTO_OFF;
    proxy->disable_modern = false;
}

static void virtio_pci_non_transitional_instance_init(Object *obj)
{
    VirtIOPCIProxy *proxy = VIRTIO_PCI(obj);

    proxy->disable_legacy = ON_OFF_AUTO_ON;
    proxy->disable_modern = false;
}

void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t)
{
    TypeInfo base_type_info = {
        .name          = t->base_name,
        .parent        = t->parent ? t->parent : TYPE_VIRTIO_PCI,
        .instance_size = t->instance_size,
        .instance_init = t->instance_init,
        .class_init    = virtio_pci_base_class_init,
        .class_data    = (void *)t,
        .abstract      = true,
    };
    TypeInfo generic_type_info = {
        .name = t->generic_name,
        .parent = base_type_info.name,
        .class_init = virtio_pci_generic_class_init,
        .interfaces = (InterfaceInfo[]) {
            { INTERFACE_PCIE_DEVICE },
            { INTERFACE_CONVENTIONAL_PCI_DEVICE },
            { }
        },
    };

    if (!base_type_info.name) {
        /* No base type -> register a single generic device type */
        base_type_info.name = t->generic_name;
        base_type_info.class_init = virtio_pci_generic_base_class_init;
        base_type_info.interfaces = generic_type_info.interfaces;
        base_type_info.abstract = false;
        generic_type_info.name = NULL;
        assert(!t->non_transitional_name);
        assert(!t->transitional_name);
    }

    type_register(&base_type_info);
    if (generic_type_info.name) {
        type_register(&generic_type_info);
    }

    if (t->non_transitional_name) {
        const TypeInfo non_transitional_type_info = {
            .name          = t->non_transitional_name,
            .parent        = base_type_info.name,
            .instance_init = virtio_pci_non_transitional_instance_init,
            .interfaces = (InterfaceInfo[]) {
                { INTERFACE_PCIE_DEVICE },
                { INTERFACE_CONVENTIONAL_PCI_DEVICE },
                { }
            },
        };
        type_register(&non_transitional_type_info);
    }

    if (t->transitional_name) {
        const TypeInfo transitional_type_info = {
            .name          = t->transitional_name,
            .parent        = base_type_info.name,
            .instance_init = virtio_pci_transitional_instance_init,
            .interfaces = (InterfaceInfo[]) {
                /*
                 * Transitional virtio devices work only as Conventional PCI
                 * devices because they require PIO ports.
                 */
                { INTERFACE_CONVENTIONAL_PCI_DEVICE },
                { }
            },
        };
        type_register(&transitional_type_info);
    }
}

2005 2006 2007
/* virtio-blk-pci */

static Property virtio_blk_pci_properties[] = {
2008
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2009 2010
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2011 2012
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
                       DEV_NVECTORS_UNSPECIFIED),
2013 2014 2015
    DEFINE_PROP_END_OF_LIST(),
};

2016
static void virtio_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2017 2018 2019
{
    VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
2020

2021 2022 2023 2024
    if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
        vpci_dev->nvectors = dev->vdev.conf.num_queues + 1;
    }

2025
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2026
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2027 2028 2029 2030 2031 2032 2033 2034
}

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

2035
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2036
    dc->props = virtio_blk_pci_properties;
2037
    k->realize = virtio_blk_pci_realize;
2038 2039 2040 2041 2042 2043 2044 2045 2046
    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);
2047 2048 2049

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_BLK);
2050 2051
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2052 2053
}

2054
static const VirtioPCIDeviceTypeInfo virtio_blk_pci_info = {
2055 2056 2057 2058
    .base_name              = TYPE_VIRTIO_BLK_PCI,
    .generic_name           = "virtio-blk-pci",
    .transitional_name      = "virtio-blk-pci-transitional",
    .non_transitional_name  = "virtio-blk-pci-non-transitional",
2059 2060 2061 2062 2063
    .instance_size = sizeof(VirtIOBlkPCI),
    .instance_init = virtio_blk_pci_instance_init,
    .class_init    = virtio_blk_pci_class_init,
};

2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
/* 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(),
};

2074
static void virtio_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2075 2076 2077
{
    VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
2078
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
2079 2080
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
2081 2082

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

2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
    /*
     * 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);
    }

2096
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2097
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2098 2099 2100 2101 2102 2103 2104
}

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

    k->realize = virtio_scsi_pci_realize;
2107
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
    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);
2118 2119 2120

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SCSI);
2121 2122
}

2123
static const VirtioPCIDeviceTypeInfo virtio_scsi_pci_info = {
2124 2125 2126 2127
    .base_name              = TYPE_VIRTIO_SCSI_PCI,
    .generic_name           = "virtio-scsi-pci",
    .transitional_name      = "virtio-scsi-pci-transitional",
    .non_transitional_name  = "virtio-scsi-pci-non-transitional",
2128 2129 2130 2131 2132
    .instance_size = sizeof(VirtIOSCSIPCI),
    .instance_init = virtio_scsi_pci_instance_init,
    .class_init    = virtio_scsi_pci_class_init,
};

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

2142
static void vhost_scsi_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152
{
    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));
2153
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2154 2155 2156 2157 2158 2159 2160
}

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);
2161
    k->realize = vhost_scsi_pci_realize;
2162
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
    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);
2173 2174 2175

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VHOST_SCSI);
G
Gonglei 已提交
2176 2177
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2178 2179
}

2180
static const VirtioPCIDeviceTypeInfo vhost_scsi_pci_info = {
2181 2182 2183 2184
    .base_name             = TYPE_VHOST_SCSI_PCI,
    .generic_name          = "vhost-scsi-pci",
    .transitional_name     = "vhost-scsi-pci-transitional",
    .non_transitional_name = "vhost-scsi-pci-non-transitional",
2185 2186 2187 2188 2189 2190
    .instance_size = sizeof(VHostSCSIPCI),
    .instance_init = vhost_scsi_pci_instance_init,
    .class_init    = vhost_scsi_pci_class_init,
};
#endif

2191 2192
/* virtio-serial-pci */

2193
static void virtio_serial_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2194 2195 2196
{
    VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);
2197 2198
    DeviceState *proxy = DEVICE(vpci_dev);
    char *bus_name;
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211

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

2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
    /*
     * 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);
    }

2222
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2223
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2224 2225 2226 2227 2228 2229
}

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),
2230
    DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
2231 2232 2233 2234 2235 2236 2237 2238
    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);
2239
    k->realize = virtio_serial_pci_realize;
2240
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
    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);
2251 2252 2253

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_SERIAL);
2254 2255
}

2256
static const VirtioPCIDeviceTypeInfo virtio_serial_pci_info = {
2257 2258 2259 2260
    .base_name             = TYPE_VIRTIO_SERIAL_PCI,
    .generic_name          = "virtio-serial-pci",
    .transitional_name     = "virtio-serial-pci-transitional",
    .non_transitional_name = "virtio-serial-pci-non-transitional",
2261 2262 2263 2264 2265
    .instance_size = sizeof(VirtIOSerialPCI),
    .instance_init = virtio_serial_pci_instance_init,
    .class_init    = virtio_serial_pci_class_init,
};

2266 2267 2268 2269
/* virtio-net-pci */

static Property virtio_net_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
2270
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
2271 2272 2273 2274
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
    DEFINE_PROP_END_OF_LIST(),
};

2275
static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
2276
{
2277
    DeviceState *qdev = DEVICE(vpci_dev);
2278 2279 2280
    VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
    DeviceState *vdev = DEVICE(&dev->vdev);

2281 2282
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
                                  object_get_typename(OBJECT(qdev)));
2283
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
2284
    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
}

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;
2298
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2299
    dc->props = virtio_net_properties;
2300
    vpciklass->realize = virtio_net_pci_realize;
2301 2302 2303 2304 2305
}

static void virtio_net_pci_instance_init(Object *obj)
{
    VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
2306 2307 2308

    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
                                TYPE_VIRTIO_NET);
2309 2310
    object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
                              "bootindex", &error_abort);
2311 2312
}

2313
static const VirtioPCIDeviceTypeInfo virtio_net_pci_info = {
2314 2315 2316 2317
    .base_name             = TYPE_VIRTIO_NET_PCI,
    .generic_name          = "virtio-net-pci",
    .transitional_name     = "virtio-net-pci-transitional",
    .non_transitional_name = "virtio-net-pci-non-transitional",
2318 2319 2320 2321 2322
    .instance_size = sizeof(VirtIONetPCI),
    .instance_init = virtio_net_pci_instance_init,
    .class_init    = virtio_net_pci_class_init,
};

2323 2324
/* virtio-pci-bus */

2325 2326
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
                               VirtIOPCIProxy *dev)
2327 2328
{
    DeviceState *qdev = DEVICE(dev);
2329 2330
    char virtio_bus_name[] = "virtio-bus";

2331
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
2332
                        virtio_bus_name);
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
}

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;
2345 2346 2347
    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;
2348 2349
    k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
    k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
2350
    k->set_host_notifier_mr = virtio_pci_set_host_notifier_mr;
2351
    k->vmstate_change = virtio_pci_vmstate_change;
2352
    k->pre_plugged = virtio_pci_pre_plugged;
2353
    k->device_plugged = virtio_pci_device_plugged;
2354
    k->device_unplugged = virtio_pci_device_unplugged;
2355
    k->query_nvectors = virtio_pci_query_nvectors;
2356
    k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled;
2357
    k->ioeventfd_assign = virtio_pci_ioeventfd_assign;
J
Jason Wang 已提交
2358
    k->get_dma_as = virtio_pci_get_dma_as;
2359 2360 2361 2362 2363 2364 2365 2366 2367
}

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 已提交
2368
static void virtio_pci_register_types(void)
P
Paul Brook 已提交
2369
{
2370 2371 2372 2373 2374 2375 2376 2377 2378
    /* Base types: */
    type_register_static(&virtio_pci_bus_info);
    type_register_static(&virtio_pci_info);

    /* Implementations: */
    virtio_pci_types_register(&virtio_blk_pci_info);
    virtio_pci_types_register(&virtio_scsi_pci_info);
    virtio_pci_types_register(&virtio_serial_pci_info);
    virtio_pci_types_register(&virtio_net_pci_info);
2379
#ifdef CONFIG_VHOST_SCSI
2380
    virtio_pci_types_register(&vhost_scsi_pci_info);
2381
#endif
P
Paul Brook 已提交
2382 2383
}

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