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

#include <inttypes.h>

#include "virtio.h"
21 22
#include "virtio-blk.h"
#include "virtio-net.h"
23
#include "virtio-serial.h"
P
Paul Brook 已提交
24
#include "pci.h"
25
#include "qemu-error.h"
26
#include "msix.h"
27
#include "net.h"
28
#include "loader.h"
29
#include "kvm.h"
B
Blue Swirl 已提交
30
#include "blockdev.h"
31
#include "virtio-pci.h"
32
#include "range.h"
P
Paul Brook 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

/* from Linux's linux/virtio_pci.h */

/* A 32-bit r/o bitmask of the features supported by the host */
#define VIRTIO_PCI_HOST_FEATURES        0

/* A 32-bit r/w bitmask of features activated by the guest */
#define VIRTIO_PCI_GUEST_FEATURES       4

/* A 32-bit r/w PFN for the currently selected queue */
#define VIRTIO_PCI_QUEUE_PFN            8

/* A 16-bit r/o queue size for the currently selected queue */
#define VIRTIO_PCI_QUEUE_NUM            12

/* A 16-bit r/w queue selector */
#define VIRTIO_PCI_QUEUE_SEL            14

/* A 16-bit r/w queue notifier */
#define VIRTIO_PCI_QUEUE_NOTIFY         16

/* An 8-bit device status register.  */
#define VIRTIO_PCI_STATUS               18

/* An 8-bit r/o interrupt status register.  Reading the value will return the
 * current contents of the ISR and will also clear it.  This is effectively
 * a read-and-acknowledge. */
#define VIRTIO_PCI_ISR                  19

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/* MSI-X registers: only enabled if MSI-X is enabled. */
/* A 16-bit vector for configuration changes. */
#define VIRTIO_MSI_CONFIG_VECTOR        20
/* A 16-bit vector for selected queue notifications. */
#define VIRTIO_MSI_QUEUE_VECTOR         22

/* Config space size */
#define VIRTIO_PCI_CONFIG_NOMSI         20
#define VIRTIO_PCI_CONFIG_MSI           24
#define VIRTIO_PCI_REGION_SIZE(dev)     (msix_present(dev) ? \
                                         VIRTIO_PCI_CONFIG_MSI : \
                                         VIRTIO_PCI_CONFIG_NOMSI)

/* The remaining space is defined by each driver as the per-driver
 * configuration space */
#define VIRTIO_PCI_CONFIG(dev)          (msix_enabled(dev) ? \
                                         VIRTIO_PCI_CONFIG_MSI : \
                                         VIRTIO_PCI_CONFIG_NOMSI)
P
Paul Brook 已提交
80 81 82 83 84

/* How many bits to shift physical queue address written to QUEUE_PFN.
 * 12 is historical, and due to x86 page size. */
#define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12

85 86
/* Flags track per-device state like workarounds for quirks in older guests. */
#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
87

P
Paul Brook 已提交
88 89 90 91 92 93
/* QEMU doesn't strictly need write barriers since everything runs in
 * lock-step.  We'll leave the calls to wmb() in though to make it obvious for
 * KVM or if kqemu gets SMP support.
 */
#define wmb() do { } while (0)

94 95 96
/* HACK for virtio to determine if it's running a big endian guest */
bool virtio_is_big_endian(void);

P
Paul Brook 已提交
97 98
/* virtio device */

99
static void virtio_pci_notify(void *opaque, uint16_t vector)
P
Paul Brook 已提交
100 101
{
    VirtIOPCIProxy *proxy = opaque;
102 103 104 105
    if (msix_enabled(&proxy->pci_dev))
        msix_notify(&proxy->pci_dev, vector);
    else
        qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
P
Paul Brook 已提交
106 107
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static void virtio_pci_save_config(void * opaque, QEMUFile *f)
{
    VirtIOPCIProxy *proxy = opaque;
    pci_device_save(&proxy->pci_dev, f);
    msix_save(&proxy->pci_dev, f);
    if (msix_present(&proxy->pci_dev))
        qemu_put_be16(f, proxy->vdev->config_vector);
}

static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
{
    VirtIOPCIProxy *proxy = opaque;
    if (msix_present(&proxy->pci_dev))
        qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
}

static int virtio_pci_load_config(void * opaque, QEMUFile *f)
{
    VirtIOPCIProxy *proxy = opaque;
    int ret;
    ret = pci_device_load(&proxy->pci_dev, f);
129
    if (ret) {
130
        return ret;
131
    }
132
    msix_load(&proxy->pci_dev, f);
133
    if (msix_present(&proxy->pci_dev)) {
134
        qemu_get_be16s(f, &proxy->vdev->config_vector);
135 136 137 138 139 140
    } else {
        proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
    }
    if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
    }
141 142 143 144 145 146 147
    return 0;
}

static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
{
    VirtIOPCIProxy *proxy = opaque;
    uint16_t vector;
148 149 150 151 152
    if (msix_present(&proxy->pci_dev)) {
        qemu_get_be16s(f, &vector);
    } else {
        vector = VIRTIO_NO_VECTOR;
    }
153
    virtio_queue_set_vector(proxy->vdev, n, vector);
154 155 156
    if (vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, vector);
    }
157 158 159
    return 0;
}

160 161 162 163 164
static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
                                                 int n, bool assign)
{
    VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
A
Avi Kivity 已提交
165 166
    int r = 0;

167 168 169
    if (assign) {
        r = event_notifier_init(notifier, 1);
        if (r < 0) {
170 171
            error_report("%s: unable to init event notifier: %d",
                         __func__, r);
172 173
            return r;
        }
A
Avi Kivity 已提交
174 175
        memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
                                  true, n, event_notifier_get_fd(notifier));
176
    } else {
A
Avi Kivity 已提交
177 178
        memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
                                  true, n, event_notifier_get_fd(notifier));
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        /* Handle the race condition where the guest kicked and we deassigned
         * before we got around to handling the kick.
         */
        if (event_notifier_test_and_clear(notifier)) {
            virtio_queue_notify_vq(vq);
        }

        event_notifier_cleanup(notifier);
    }
    return r;
}

static void virtio_pci_host_notifier_read(void *opaque)
{
    VirtQueue *vq = opaque;
    EventNotifier *n = virtio_queue_get_host_notifier(vq);
    if (event_notifier_test_and_clear(n)) {
        virtio_queue_notify_vq(vq);
    }
}

static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
                                                    int n, bool assign)
{
    VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
    if (assign) {
        qemu_set_fd_handler(event_notifier_get_fd(notifier),
                            virtio_pci_host_notifier_read, NULL, vq);
    } else {
        qemu_set_fd_handler(event_notifier_get_fd(notifier),
                            NULL, NULL, NULL);
    }
}

214
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
215 216 217 218 219 220
{
    int n, r;

    if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
        proxy->ioeventfd_disabled ||
        proxy->ioeventfd_started) {
221
        return;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    }

    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(proxy->vdev, n)) {
            continue;
        }

        r = virtio_pci_set_host_notifier_internal(proxy, n, true);
        if (r < 0) {
            goto assign_error;
        }

        virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
    }
    proxy->ioeventfd_started = true;
237
    return;
238 239 240 241 242 243 244 245

assign_error:
    while (--n >= 0) {
        if (!virtio_queue_get_num(proxy->vdev, n)) {
            continue;
        }

        virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
246 247
        r = virtio_pci_set_host_notifier_internal(proxy, n, false);
        assert(r >= 0);
248 249
    }
    proxy->ioeventfd_started = false;
250
    error_report("%s: failed. Fallback to a userspace (slower).", __func__);
251 252
}

253
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
254
{
255
    int r;
256 257 258
    int n;

    if (!proxy->ioeventfd_started) {
259
        return;
260 261 262 263 264 265 266 267
    }

    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(proxy->vdev, n)) {
            continue;
        }

        virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
268 269
        r = virtio_pci_set_host_notifier_internal(proxy, n, false);
        assert(r >= 0);
270 271 272 273
    }
    proxy->ioeventfd_started = false;
}

274
void virtio_pci_reset(DeviceState *d)
275
{
276
    VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
277
    virtio_pci_stop_ioeventfd(proxy);
278
    virtio_reset(proxy->vdev);
279
    msix_reset(&proxy->pci_dev);
280
    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
281 282
}

P
Paul Brook 已提交
283 284 285 286
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = proxy->vdev;
A
Anthony Liguori 已提交
287
    target_phys_addr_t pa;
P
Paul Brook 已提交
288 289 290 291 292

    switch (addr) {
    case VIRTIO_PCI_GUEST_FEATURES:
	/* Guest does not negotiate properly?  We have to assume nothing. */
	if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
293
            val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
P
Paul Brook 已提交
294
	}
295
        virtio_set_features(vdev, val);
P
Paul Brook 已提交
296 297
        break;
    case VIRTIO_PCI_QUEUE_PFN:
A
Anthony Liguori 已提交
298
        pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
299
        if (pa == 0) {
300
            virtio_pci_stop_ioeventfd(proxy);
301 302 303
            virtio_reset(proxy->vdev);
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
304 305
        else
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
P
Paul Brook 已提交
306 307 308 309 310 311
        break;
    case VIRTIO_PCI_QUEUE_SEL:
        if (val < VIRTIO_PCI_QUEUE_MAX)
            vdev->queue_sel = val;
        break;
    case VIRTIO_PCI_QUEUE_NOTIFY:
312 313 314
        if (val < VIRTIO_PCI_QUEUE_MAX) {
            virtio_queue_notify(vdev, val);
        }
P
Paul Brook 已提交
315 316
        break;
    case VIRTIO_PCI_STATUS:
317 318 319 320
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
            virtio_pci_stop_ioeventfd(proxy);
        }

321
        virtio_set_status(vdev, val & 0xFF);
322 323 324 325 326

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

327 328 329 330
        if (vdev->status == 0) {
            virtio_reset(proxy->vdev);
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
331 332 333 334 335 336

        /* Linux before 2.6.34 sets the device as OK without enabling
           the PCI device bus master bit. In this case we need to disable
           some safety checks. */
        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
337
            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
338
        }
P
Paul Brook 已提交
339
        break;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    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:
356 357
        error_report("%s: unexpected address 0x%x value 0x%x",
                     __func__, addr, val);
358
        break;
P
Paul Brook 已提交
359 360 361
    }
}

362
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
P
Paul Brook 已提交
363 364 365 366 367 368
{
    VirtIODevice *vdev = proxy->vdev;
    uint32_t ret = 0xFFFFFFFF;

    switch (addr) {
    case VIRTIO_PCI_HOST_FEATURES:
369
        ret = proxy->host_features;
P
Paul Brook 已提交
370 371
        break;
    case VIRTIO_PCI_GUEST_FEATURES:
372
        ret = vdev->guest_features;
P
Paul Brook 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
        break;
    case VIRTIO_PCI_QUEUE_PFN:
        ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
              >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
        break;
    case VIRTIO_PCI_QUEUE_NUM:
        ret = virtio_queue_get_num(vdev, vdev->queue_sel);
        break;
    case VIRTIO_PCI_QUEUE_SEL:
        ret = vdev->queue_sel;
        break;
    case VIRTIO_PCI_STATUS:
        ret = vdev->status;
        break;
    case VIRTIO_PCI_ISR:
        /* reading from the ISR also clears it. */
        ret = vdev->isr;
        vdev->isr = 0;
391
        qemu_set_irq(proxy->pci_dev.irq[0], 0);
P
Paul Brook 已提交
392
        break;
393 394 395 396 397 398
    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 已提交
399 400 401 402 403 404 405 406 407 408
    default:
        break;
    }

    return ret;
}

static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
{
    VirtIOPCIProxy *proxy = opaque;
409 410 411 412
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    if (addr < config)
        return virtio_ioport_read(proxy, addr);
    addr -= config;
P
Paul Brook 已提交
413 414 415 416 417 418
    return virtio_config_readb(proxy->vdev, addr);
}

static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
{
    VirtIOPCIProxy *proxy = opaque;
419
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
420
    uint16_t val;
421 422 423
    if (addr < config)
        return virtio_ioport_read(proxy, addr);
    addr -= config;
424 425 426 427 428 429 430 431 432 433
    val = virtio_config_readw(proxy->vdev, addr);
    if (virtio_is_big_endian()) {
        /*
         * virtio is odd, ioports are LE but config space is target native
         * endian. However, in qemu, all PIO is LE, so we need to re-swap
         * on BE targets
         */
        val = bswap16(val);
    }
    return val;
P
Paul Brook 已提交
434 435 436 437 438
}

static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
{
    VirtIOPCIProxy *proxy = opaque;
439
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
440
    uint32_t val;
441 442 443
    if (addr < config)
        return virtio_ioport_read(proxy, addr);
    addr -= config;
444 445 446 447 448
    val = virtio_config_readl(proxy->vdev, addr);
    if (virtio_is_big_endian()) {
        val = bswap32(val);
    }
    return val;
P
Paul Brook 已提交
449 450 451 452 453
}

static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
454 455 456 457 458 459
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    if (addr < config) {
        virtio_ioport_write(proxy, addr, val);
        return;
    }
    addr -= config;
P
Paul Brook 已提交
460 461 462 463 464 465
    virtio_config_writeb(proxy->vdev, addr, val);
}

static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
466 467 468 469 470 471
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    if (addr < config) {
        virtio_ioport_write(proxy, addr, val);
        return;
    }
    addr -= config;
472 473 474
    if (virtio_is_big_endian()) {
        val = bswap16(val);
    }
P
Paul Brook 已提交
475 476 477 478 479 480
    virtio_config_writew(proxy->vdev, addr, val);
}

static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
481 482 483 484 485 486
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    if (addr < config) {
        virtio_ioport_write(proxy, addr, val);
        return;
    }
    addr -= config;
487 488 489
    if (virtio_is_big_endian()) {
        val = bswap32(val);
    }
P
Paul Brook 已提交
490 491 492
    virtio_config_writel(proxy->vdev, addr, val);
}

A
Avi Kivity 已提交
493 494 495 496 497 498 499 500 501
const MemoryRegionPortio virtio_portio[] = {
    { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
    { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
    { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
    { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
    { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
    { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
    PORTIO_END_OF_LIST()
};
P
Paul Brook 已提交
502

A
Avi Kivity 已提交
503 504 505 506
static const MemoryRegionOps virtio_pci_config_ops = {
    .old_portio = virtio_portio,
    .endianness = DEVICE_LITTLE_ENDIAN,
};
507 508 509 510

static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
                                uint32_t val, int len)
{
511 512
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

513 514 515 516 517 518 519 520
    pci_default_write_config(pci_dev, address, val, len);

    if (range_covers_byte(address, len, PCI_COMMAND) &&
        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
        virtio_pci_stop_ioeventfd(proxy);
        virtio_set_status(proxy->vdev,
                          proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
521 522
    }

523
    msix_write_config(pci_dev, address, val, len);
P
Paul Brook 已提交
524 525
}

526 527
static unsigned virtio_pci_get_features(void *opaque)
{
528 529
    VirtIOPCIProxy *proxy = opaque;
    return proxy->host_features;
530 531
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
static void virtio_pci_guest_notifier_read(void *opaque)
{
    VirtQueue *vq = opaque;
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
    if (event_notifier_test_and_clear(n)) {
        virtio_irq(vq);
    }
}

static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);

    if (assign) {
        int r = event_notifier_init(notifier, 0);
        if (r < 0) {
            return r;
        }
        qemu_set_fd_handler(event_notifier_get_fd(notifier),
                            virtio_pci_guest_notifier_read, NULL, vq);
    } else {
        qemu_set_fd_handler(event_notifier_get_fd(notifier),
                            NULL, NULL, NULL);
        event_notifier_cleanup(notifier);
    }

    return 0;
}

563 564 565 566 567 568
static bool virtio_pci_query_guest_notifiers(void *opaque)
{
    VirtIOPCIProxy *proxy = opaque;
    return msix_enabled(&proxy->pci_dev);
}

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = proxy->vdev;
    int r, n;

    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }

        r = virtio_pci_set_guest_notifier(opaque, n, assign);
        if (r < 0) {
            goto assign_error;
        }
    }

    return 0;

assign_error:
    /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
    while (--n >= 0) {
        virtio_pci_set_guest_notifier(opaque, n, !assign);
    }
    return r;
}

596 597 598
static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
{
    VirtIOPCIProxy *proxy = opaque;
599 600 601 602 603

    /* Stop using ioeventfd for virtqueue kick if the device starts using host
     * notifiers.  This makes it easy to avoid stepping on each others' toes.
     */
    proxy->ioeventfd_disabled = assign;
604
    if (assign) {
605 606 607 608 609 610 611 612 613 614 615 616 617 618
        virtio_pci_stop_ioeventfd(proxy);
    }
    /* We don't need to start here: it's not needed because backend
     * currently only stops on status change away from ok,
     * reset, vmstop and such. If we do add code to start here,
     * need to check vmstate, device state etc. */
    return virtio_pci_set_host_notifier_internal(proxy, n, assign);
}

static void virtio_pci_vmstate_change(void *opaque, bool running)
{
    VirtIOPCIProxy *proxy = opaque;

    if (running) {
619 620 621 622 623 624
        /* Try to find out if the guest has bus master disabled, but is
           in ready state. Then we have a buggy guest OS. */
        if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
        }
625
        virtio_pci_start_ioeventfd(proxy);
626
    } else {
627
        virtio_pci_stop_ioeventfd(proxy);
628 629 630
    }
}

P
Paul Brook 已提交
631
static const VirtIOBindings virtio_pci_bindings = {
632 633 634 635 636
    .notify = virtio_pci_notify,
    .save_config = virtio_pci_save_config,
    .load_config = virtio_pci_load_config,
    .save_queue = virtio_pci_save_queue,
    .load_queue = virtio_pci_load_queue,
637
    .get_features = virtio_pci_get_features,
638
    .query_guest_notifiers = virtio_pci_query_guest_notifiers,
639
    .set_host_notifier = virtio_pci_set_host_notifier,
640
    .set_guest_notifiers = virtio_pci_set_guest_notifiers,
641
    .vmstate_change = virtio_pci_vmstate_change,
P
Paul Brook 已提交
642 643
};

644
void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
P
Paul Brook 已提交
645 646 647 648 649 650 651 652
{
    uint8_t *config;
    uint32_t size;

    proxy->vdev = vdev;

    config = proxy->pci_dev.config;

653 654 655
    if (proxy->class_code) {
        pci_config_set_class(config, proxy->class_code);
    }
H
Hui Kai Ran 已提交
656 657 658 659
    pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
                 pci_get_word(config + PCI_VENDOR_ID));
    pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
    config[PCI_INTERRUPT_PIN] = 1;
P
Paul Brook 已提交
660

A
Avi Kivity 已提交
661 662 663
    memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
    if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
                                     &proxy->msix_bar, 1, 0)) {
664 665
        pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
                         &proxy->msix_bar);
666 667 668
    } else
        vdev->nvectors = 0;

669 670
    proxy->pci_dev.config_write = virtio_write_config;

671
    size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
P
Paul Brook 已提交
672 673 674
    if (size & (size-1))
        size = 1 << qemu_fls(size);

A
Avi Kivity 已提交
675 676
    memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
                          "virtio-pci", size);
677 678
    pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
                     &proxy->bar);
P
Paul Brook 已提交
679

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

P
Paul Brook 已提交
684
    virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
685 686 687
    proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
    proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
    proxy->host_features = vdev->get_features(vdev, proxy->host_features);
P
Paul Brook 已提交
688 689
}

690
static int virtio_blk_init_pci(PCIDevice *pci_dev)
P
Paul Brook 已提交
691 692 693 694
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

695 696 697
    if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
        proxy->class_code != PCI_CLASS_STORAGE_OTHER)
        proxy->class_code = PCI_CLASS_STORAGE_SCSI;
P
Paul Brook 已提交
698

699 700
    vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block,
                           &proxy->block_serial);
701 702 703
    if (!vdev) {
        return -1;
    }
G
Gerd Hoffmann 已提交
704
    vdev->nvectors = proxy->nvectors;
705
    virtio_init_pci(proxy, vdev);
G
Gerd Hoffmann 已提交
706 707
    /* make the actual value visible */
    proxy->nvectors = vdev->nvectors;
708
    return 0;
709 710
}

711 712
static int virtio_exit_pci(PCIDevice *pci_dev)
{
A
Avi Kivity 已提交
713
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
A
Avi Kivity 已提交
714
    int r;
A
Avi Kivity 已提交
715 716

    memory_region_destroy(&proxy->bar);
A
Avi Kivity 已提交
717 718 719
    r = msix_uninit(pci_dev, &proxy->msix_bar);
    memory_region_destroy(&proxy->msix_bar);
    return r;
720 721
}

G
Gerd Hoffmann 已提交
722 723 724 725
static int virtio_blk_exit_pci(PCIDevice *pci_dev)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

726
    virtio_pci_stop_ioeventfd(proxy);
727
    virtio_blk_exit(proxy->vdev);
728
    blockdev_mark_auto_del(proxy->block.bs);
729
    return virtio_exit_pci(pci_dev);
G
Gerd Hoffmann 已提交
730 731
}

732
static int virtio_serial_init_pci(PCIDevice *pci_dev)
733
{
734
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
735 736
    VirtIODevice *vdev;

737 738 739 740 741
    if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
        proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
        proxy->class_code != PCI_CLASS_OTHERS)          /* qemu-kvm  */
        proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;

742
    vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
743 744 745
    if (!vdev) {
        return -1;
    }
746
    vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
747
                                        ? proxy->serial.max_virtserial_ports + 1
748
                                        : proxy->nvectors;
749
    virtio_init_pci(proxy, vdev);
750
    proxy->nvectors = vdev->nvectors;
751
    return 0;
P
Paul Brook 已提交
752 753
}

754 755 756 757
static int virtio_serial_exit_pci(PCIDevice *pci_dev)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

A
Amit Shah 已提交
758
    virtio_pci_stop_ioeventfd(proxy);
759 760 761 762
    virtio_serial_exit(proxy->vdev);
    return virtio_exit_pci(pci_dev);
}

763
static int virtio_net_init_pci(PCIDevice *pci_dev)
P
Paul Brook 已提交
764 765 766 767
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

768
    vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
769

770
    vdev->nvectors = proxy->nvectors;
771
    virtio_init_pci(proxy, vdev);
772 773 774

    /* make the actual value visible */
    proxy->nvectors = vdev->nvectors;
775
    return 0;
P
Paul Brook 已提交
776 777
}

778 779 780 781
static int virtio_net_exit_pci(PCIDevice *pci_dev)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

782
    virtio_pci_stop_ioeventfd(proxy);
783 784 785 786
    virtio_net_exit(proxy->vdev);
    return virtio_exit_pci(pci_dev);
}

787
static int virtio_balloon_init_pci(PCIDevice *pci_dev)
P
Paul Brook 已提交
788 789 790 791 792
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

    vdev = virtio_balloon_init(&pci_dev->qdev);
793 794 795
    if (!vdev) {
        return -1;
    }
796
    virtio_init_pci(proxy, vdev);
797
    return 0;
P
Paul Brook 已提交
798 799
}

800 801 802 803 804 805 806 807 808
static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    virtio_pci_stop_ioeventfd(proxy);
    virtio_balloon_exit(proxy->vdev);
    return virtio_exit_pci(pci_dev);
}

809 810 811 812 813 814 815 816
static Property virtio_blk_properties[] = {
    DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
    DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
    DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
    DEFINE_PROP_END_OF_LIST(),
817 818
};

819 820
static void virtio_blk_class_init(ObjectClass *klass, void *data)
{
821
    DeviceClass *dc = DEVICE_CLASS(klass);
822 823 824 825 826 827 828 829
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_blk_init_pci;
    k->exit = virtio_blk_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_STORAGE_SCSI;
830 831
    dc->reset = virtio_pci_reset;
    dc->props = virtio_blk_properties;
832 833
}

834 835 836 837 838
static TypeInfo virtio_blk_info = {
    .name          = "virtio-blk-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_blk_class_init,
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
};

static Property virtio_net_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
    DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
    DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
    DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL),
    DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST),
    DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_net_class_init(ObjectClass *klass, void *data)
{
854
    DeviceClass *dc = DEVICE_CLASS(klass);
855 856 857 858 859 860 861 862 863
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_net_init_pci;
    k->exit = virtio_net_exit_pci;
    k->romfile = "pxe-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;
864 865
    dc->reset = virtio_pci_reset;
    dc->props = virtio_net_properties;
866 867
}

868 869 870 871 872
static TypeInfo virtio_net_info = {
    .name          = "virtio-net-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_net_class_init,
873 874
};

875 876 877 878 879 880 881
static Property virtio_serial_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_HEX32("class", VirtIOPCIProxy, class_code, 0),
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
    DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31),
    DEFINE_PROP_END_OF_LIST(),
882 883
};

884 885
static void virtio_serial_class_init(ObjectClass *klass, void *data)
{
886
    DeviceClass *dc = DEVICE_CLASS(klass);
887 888 889 890 891 892 893 894
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_serial_init_pci;
    k->exit = virtio_serial_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
895 896
    dc->reset = virtio_pci_reset;
    dc->props = virtio_serial_properties;
897 898
}

899 900 901 902 903
static TypeInfo virtio_serial_info = {
    .name          = "virtio-serial-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_serial_class_init,
904 905 906 907 908 909 910 911 912
};

static Property virtio_balloon_properties[] = {
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_balloon_class_init(ObjectClass *klass, void *data)
{
913
    DeviceClass *dc = DEVICE_CLASS(klass);
914 915 916 917 918 919 920 921
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_balloon_init_pci;
    k->exit = virtio_balloon_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_MEMORY_RAM;
922 923
    dc->reset = virtio_pci_reset;
    dc->props = virtio_balloon_properties;
924 925
}

926 927 928 929 930
static TypeInfo virtio_balloon_info = {
    .name          = "virtio-balloon-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_balloon_class_init,
931 932
};

P
Paul Brook 已提交
933 934
static void virtio_pci_register_devices(void)
{
935 936 937 938
    type_register_static(&virtio_blk_info);
    type_register_static(&virtio_net_info);
    type_register_static(&virtio_serial_info);
    type_register_static(&virtio_balloon_info);
P
Paul Brook 已提交
939 940 941
}

device_init(virtio_pci_register_devices)