virtio-pci.c 28.2 KB
Newer Older
P
Paul Brook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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.
 *
 */

#include <inttypes.h>

#include "virtio.h"
19 20
#include "virtio-blk.h"
#include "virtio-net.h"
21
#include "virtio-serial.h"
P
Paul Brook 已提交
22
#include "pci.h"
23
#include "qemu-error.h"
24
#include "msix.h"
25
#include "net.h"
26
#include "loader.h"
27
#include "kvm.h"
B
Blue Swirl 已提交
28
#include "blockdev.h"
29
#include "virtio-pci.h"
P
Paul Brook 已提交
30 31 32 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

/* 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

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/* 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 已提交
77 78 79 80 81

/* 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

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

85 86 87 88 89
/* Performance improves when virtqueue kick processing is decoupled from the
 * vcpu thread using ioeventfd for some devices. */
#define VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT 1
#define VIRTIO_PCI_FLAG_USE_IOEVENTFD   (1 << VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT)

P
Paul Brook 已提交
90 91 92 93 94 95 96 97
/* 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)

/* virtio device */

98
static void virtio_pci_notify(void *opaque, uint16_t vector)
P
Paul Brook 已提交
99 100
{
    VirtIOPCIProxy *proxy = opaque;
101 102 103 104
    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 已提交
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
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);
128
    if (ret) {
129
        return ret;
130
    }
131
    msix_load(&proxy->pci_dev, f);
132
    if (msix_present(&proxy->pci_dev)) {
133
        qemu_get_be16s(f, &proxy->vdev->config_vector);
134 135 136 137 138 139
    } 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);
    }
140 141 142 143 144 145 146
    return 0;
}

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

159 160 161 162 163 164 165 166 167
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);
    int r;
    if (assign) {
        r = event_notifier_init(notifier, 1);
        if (r < 0) {
168 169
            error_report("%s: unable to init event notifier: %d",
                         __func__, r);
170 171 172 173 174 175
            return r;
        }
        r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
                                       proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
                                       n, assign);
        if (r < 0) {
176 177
            error_report("%s: unable to map ioeventfd: %d",
                         __func__, r);
178 179 180 181 182 183 184
            event_notifier_cleanup(notifier);
        }
    } else {
        r = kvm_set_ioeventfd_pio_word(event_notifier_get_fd(notifier),
                                       proxy->addr + VIRTIO_PCI_QUEUE_NOTIFY,
                                       n, assign);
        if (r < 0) {
185 186
            error_report("%s: unable to unmap ioeventfd: %d",
                         __func__, r);
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 214 215 216 217 218 219 220 221 222 223 224
            return r;
        }

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

225
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
226 227 228 229 230 231
{
    int n, r;

    if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
        proxy->ioeventfd_disabled ||
        proxy->ioeventfd_started) {
232
        return;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    }

    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;
248
    return;
249 250 251 252 253 254 255 256

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

        virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
257 258
        r = virtio_pci_set_host_notifier_internal(proxy, n, false);
        assert(r >= 0);
259 260
    }
    proxy->ioeventfd_started = false;
261
    error_report("%s: failed. Fallback to a userspace (slower).", __func__);
262 263
}

264
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
265
{
266
    int r;
267 268 269
    int n;

    if (!proxy->ioeventfd_started) {
270
        return;
271 272 273 274 275 276 277 278
    }

    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);
279 280
        r = virtio_pci_set_host_notifier_internal(proxy, n, false);
        assert(r >= 0);
281 282 283 284
    }
    proxy->ioeventfd_started = false;
}

285
static void virtio_pci_reset(DeviceState *d)
286
{
287
    VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
288
    virtio_pci_stop_ioeventfd(proxy);
289
    virtio_reset(proxy->vdev);
290
    msix_reset(&proxy->pci_dev);
291
    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
292 293
}

P
Paul Brook 已提交
294 295 296 297
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = proxy->vdev;
A
Anthony Liguori 已提交
298
    target_phys_addr_t pa;
P
Paul Brook 已提交
299 300 301 302 303 304

    switch (addr) {
    case VIRTIO_PCI_GUEST_FEATURES:
	/* Guest does not negotiate properly?  We have to assume nothing. */
	if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
	    if (vdev->bad_features)
305
		val = proxy->host_features & vdev->bad_features(vdev);
P
Paul Brook 已提交
306 307 308 309 310
	    else
		val = 0;
	}
        if (vdev->set_features)
            vdev->set_features(vdev, val);
311
        vdev->guest_features = val;
P
Paul Brook 已提交
312 313
        break;
    case VIRTIO_PCI_QUEUE_PFN:
A
Anthony Liguori 已提交
314
        pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
315
        if (pa == 0) {
316
            virtio_pci_stop_ioeventfd(proxy);
317 318 319
            virtio_reset(proxy->vdev);
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
320 321
        else
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
P
Paul Brook 已提交
322 323 324 325 326 327
        break;
    case VIRTIO_PCI_QUEUE_SEL:
        if (val < VIRTIO_PCI_QUEUE_MAX)
            vdev->queue_sel = val;
        break;
    case VIRTIO_PCI_QUEUE_NOTIFY:
328 329 330
        if (val < VIRTIO_PCI_QUEUE_MAX) {
            virtio_queue_notify(vdev, val);
        }
P
Paul Brook 已提交
331 332
        break;
    case VIRTIO_PCI_STATUS:
333 334 335 336
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
            virtio_pci_stop_ioeventfd(proxy);
        }

337
        virtio_set_status(vdev, val & 0xFF);
338 339 340 341 342

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

343 344 345 346
        if (vdev->status == 0) {
            virtio_reset(proxy->vdev);
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
347 348 349 350 351 352

        /* 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)) {
353
            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
354
        }
P
Paul Brook 已提交
355
        break;
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    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:
372 373
        error_report("%s: unexpected address 0x%x value 0x%x",
                     __func__, addr, val);
374
        break;
P
Paul Brook 已提交
375 376 377
    }
}

378
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
P
Paul Brook 已提交
379 380 381 382 383 384
{
    VirtIODevice *vdev = proxy->vdev;
    uint32_t ret = 0xFFFFFFFF;

    switch (addr) {
    case VIRTIO_PCI_HOST_FEATURES:
385
        ret = proxy->host_features;
P
Paul Brook 已提交
386 387
        break;
    case VIRTIO_PCI_GUEST_FEATURES:
388
        ret = vdev->guest_features;
P
Paul Brook 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        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;
407
        qemu_set_irq(proxy->pci_dev.irq[0], 0);
P
Paul Brook 已提交
408
        break;
409 410 411 412 413 414
    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 已提交
415 416 417 418 419 420 421 422 423 424
    default:
        break;
    }

    return ret;
}

static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
{
    VirtIOPCIProxy *proxy = opaque;
425 426 427 428 429
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    addr -= proxy->addr;
    if (addr < config)
        return virtio_ioport_read(proxy, addr);
    addr -= config;
P
Paul Brook 已提交
430 431 432 433 434 435
    return virtio_config_readb(proxy->vdev, addr);
}

static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
{
    VirtIOPCIProxy *proxy = opaque;
436 437 438 439 440
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    addr -= proxy->addr;
    if (addr < config)
        return virtio_ioport_read(proxy, addr);
    addr -= config;
P
Paul Brook 已提交
441 442 443 444 445 446
    return virtio_config_readw(proxy->vdev, addr);
}

static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
{
    VirtIOPCIProxy *proxy = opaque;
447 448 449 450 451
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    addr -= proxy->addr;
    if (addr < config)
        return virtio_ioport_read(proxy, addr);
    addr -= config;
P
Paul Brook 已提交
452 453 454 455 456 457
    return virtio_config_readl(proxy->vdev, addr);
}

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

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

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

static void virtio_map(PCIDevice *pci_dev, int region_num,
495
                       pcibus_t addr, pcibus_t size, int type)
P
Paul Brook 已提交
496 497 498
{
    VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
    VirtIODevice *vdev = proxy->vdev;
499
    unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
P
Paul Brook 已提交
500 501 502

    proxy->addr = addr;

503 504 505 506 507 508
    register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
    register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
    register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
    register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
    register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
    register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
P
Paul Brook 已提交
509

510
    if (vdev->config_len)
P
Paul Brook 已提交
511
        vdev->get_config(vdev, vdev->config);
512 513 514 515 516
}

static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
                                uint32_t val, int len)
{
517 518 519 520
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    if (PCI_COMMAND == address) {
        if (!(val & PCI_COMMAND_MASTER)) {
521
            if (!(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
522
                virtio_pci_stop_ioeventfd(proxy);
523 524
                virtio_set_status(proxy->vdev,
                                  proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
525
            }
526 527 528
        }
    }

529
    pci_default_write_config(pci_dev, address, val, len);
530
    msix_write_config(pci_dev, address, val, len);
P
Paul Brook 已提交
531 532
}

533 534
static unsigned virtio_pci_get_features(void *opaque)
{
535 536
    VirtIOPCIProxy *proxy = opaque;
    return proxy->host_features;
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 563 564 565 566 567 568 569
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;
}

570 571 572 573 574 575
static bool virtio_pci_query_guest_notifiers(void *opaque)
{
    VirtIOPCIProxy *proxy = opaque;
    return msix_enabled(&proxy->pci_dev);
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
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;
}

603 604 605
static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
{
    VirtIOPCIProxy *proxy = opaque;
606 607 608 609 610

    /* 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;
611
    if (assign) {
612 613 614 615 616 617 618 619 620 621 622 623 624 625
        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) {
626 627 628 629 630 631
        /* 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;
        }
632
        virtio_pci_start_ioeventfd(proxy);
633
    } else {
634
        virtio_pci_stop_ioeventfd(proxy);
635 636 637
    }
}

P
Paul Brook 已提交
638
static const VirtIOBindings virtio_pci_bindings = {
639 640 641 642 643
    .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,
644
    .get_features = virtio_pci_get_features,
645
    .query_guest_notifiers = virtio_pci_query_guest_notifiers,
646
    .set_host_notifier = virtio_pci_set_host_notifier,
647
    .set_guest_notifiers = virtio_pci_set_guest_notifiers,
648
    .vmstate_change = virtio_pci_vmstate_change,
P
Paul Brook 已提交
649 650
};

651
void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
P
Paul Brook 已提交
652 653 654 655 656 657 658 659
{
    uint8_t *config;
    uint32_t size;

    proxy->vdev = vdev;

    config = proxy->pci_dev.config;

660 661 662 663 664
    if (proxy->class_code) {
        pci_config_set_class(config, proxy->class_code);
    }
    pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID));
    pci_set_word(config + 0x2e, vdev->device_id);
P
Paul Brook 已提交
665 666
    config[0x3d] = 1;

667
    if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
668 669
        pci_register_bar(&proxy->pci_dev, 1,
                         msix_bar_size(&proxy->pci_dev),
670
                         PCI_BASE_ADDRESS_SPACE_MEMORY,
671 672 673 674
                         msix_mmio_map);
    } else
        vdev->nvectors = 0;

675 676
    proxy->pci_dev.config_write = virtio_write_config;

677
    size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
P
Paul Brook 已提交
678 679 680
    if (size & (size-1))
        size = 1 << qemu_fls(size);

681
    pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
P
Paul Brook 已提交
682 683
                           virtio_map);

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

P
Paul Brook 已提交
688
    virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
689 690 691
    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 已提交
692 693
}

694
static int virtio_blk_init_pci(PCIDevice *pci_dev)
P
Paul Brook 已提交
695 696 697 698
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

699 700 701
    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 已提交
702

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

715 716 717 718 719
static int virtio_exit_pci(PCIDevice *pci_dev)
{
    return msix_uninit(pci_dev);
}

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

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

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

735 736 737 738 739
    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;

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

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

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

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

766
    vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
767

768
    vdev->nvectors = proxy->nvectors;
769
    virtio_init_pci(proxy, vdev);
770 771 772

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

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

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

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

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

798 799 800
static PCIDeviceInfo virtio_info[] = {
    {
        .qdev.name = "virtio-blk-pci",
801
        .qdev.alias = "virtio-blk",
802 803
        .qdev.size = sizeof(VirtIOPCIProxy),
        .init      = virtio_blk_init_pci,
G
Gerd Hoffmann 已提交
804
        .exit      = virtio_blk_exit_pci,
805 806 807 808
        .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
        .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK,
        .revision  = VIRTIO_PCI_ABI_VERSION,
        .class_id  = PCI_CLASS_STORAGE_SCSI,
809
        .qdev.props = (Property[]) {
810
            DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
811
            DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block),
812
            DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial),
813 814
            DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                            VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
G
Gerd Hoffmann 已提交
815
            DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
816
            DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
817
            DEFINE_PROP_END_OF_LIST(),
818
        },
819
        .qdev.reset = virtio_pci_reset,
820
    },{
821
        .qdev.name  = "virtio-net-pci",
822
        .qdev.alias = "virtio-net",
823 824
        .qdev.size  = sizeof(VirtIOPCIProxy),
        .init       = virtio_net_init_pci,
825
        .exit       = virtio_net_exit_pci,
826
        .romfile    = "pxe-virtio.rom",
827 828 829 830
        .vendor_id  = PCI_VENDOR_ID_REDHAT_QUMRANET,
        .device_id  = PCI_DEVICE_ID_VIRTIO_NET,
        .revision   = VIRTIO_PCI_ABI_VERSION,
        .class_id   = PCI_CLASS_NETWORK_ETHERNET,
831
        .qdev.props = (Property[]) {
832 833
            DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                            VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
834
            DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
835
            DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
836
            DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
837 838
            DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
                               net.txtimer, TX_TIMER_INTERVAL),
839 840
            DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy,
                              net.txburst, TX_BURST),
841
            DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
842
            DEFINE_PROP_END_OF_LIST(),
843
        },
844
        .qdev.reset = virtio_pci_reset,
845
    },{
846 847
        .qdev.name = "virtio-serial-pci",
        .qdev.alias = "virtio-serial",
848
        .qdev.size = sizeof(VirtIOPCIProxy),
849
        .init      = virtio_serial_init_pci,
850
        .exit      = virtio_serial_exit_pci,
851 852 853 854
        .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
        .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE,
        .revision  = VIRTIO_PCI_ABI_VERSION,
        .class_id  = PCI_CLASS_COMMUNICATION_OTHER,
855
        .qdev.props = (Property[]) {
A
Amit Shah 已提交
856 857
            DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
                            VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
858 859
            DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
                               DEV_NVECTORS_UNSPECIFIED),
860
            DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
861
            DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
862 863
            DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy,
                               serial.max_virtserial_ports, 31),
864
            DEFINE_PROP_END_OF_LIST(),
865
        },
866
        .qdev.reset = virtio_pci_reset,
867 868
    },{
        .qdev.name = "virtio-balloon-pci",
869
        .qdev.alias = "virtio-balloon",
870 871
        .qdev.size = sizeof(VirtIOPCIProxy),
        .init      = virtio_balloon_init_pci,
872
        .exit      = virtio_exit_pci,
873 874 875 876
        .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
        .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON,
        .revision  = VIRTIO_PCI_ABI_VERSION,
        .class_id  = PCI_CLASS_MEMORY_RAM,
877 878 879 880
        .qdev.props = (Property[]) {
            DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
            DEFINE_PROP_END_OF_LIST(),
        },
881
        .qdev.reset = virtio_pci_reset,
882 883 884 885 886
    },{
        /* end of list */
    }
};

P
Paul Brook 已提交
887 888
static void virtio_pci_register_devices(void)
{
889
    pci_qdev_register_many(virtio_info);
P
Paul Brook 已提交
890 891 892
}

device_init(virtio_pci_register_devices)