2009-05-18 15:51:59 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-01-13 17:44:23 +01:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2009-05-18 15:51:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include "virtio.h"
|
2010-01-10 12:52:53 +01:00
|
|
|
#include "virtio-blk.h"
|
|
|
|
#include "virtio-net.h"
|
2011-02-03 06:52:32 +01:00
|
|
|
#include "virtio-serial.h"
|
2011-02-11 09:40:59 +01:00
|
|
|
#include "virtio-scsi.h"
|
2012-12-12 13:24:50 +01:00
|
|
|
#include "pci/pci.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/error-report.h"
|
2012-12-12 13:24:50 +01:00
|
|
|
#include "pci/msi.h"
|
|
|
|
#include "pci/msix.h"
|
2009-10-21 15:25:35 +02:00
|
|
|
#include "loader.h"
|
2012-12-17 18:20:04 +01:00
|
|
|
#include "sysemu/kvm.h"
|
|
|
|
#include "sysemu/blockdev.h"
|
2011-06-01 09:05:13 +02:00
|
|
|
#include "virtio-pci.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/range.h"
|
2013-01-15 00:08:03 +01:00
|
|
|
#include "virtio-bus.h"
|
2009-05-18 15:51:59 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2009-06-21 18:50:26 +02:00
|
|
|
/* 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)
|
2009-05-18 15:51:59 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2010-12-17 13:01:49 +01:00
|
|
|
/* Flags track per-device state like workarounds for quirks in older guests. */
|
|
|
|
#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
|
2010-03-16 19:18:07 +01:00
|
|
|
|
2009-05-18 15:51:59 +02:00
|
|
|
/* 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)
|
|
|
|
|
2013-01-06 19:30:17 +01:00
|
|
|
/* HACK for virtio to determine if it's running a big endian guest */
|
|
|
|
bool virtio_is_big_endian(void);
|
|
|
|
|
2009-05-18 15:51:59 +02:00
|
|
|
/* virtio device */
|
2012-12-17 12:01:07 +01:00
|
|
|
/* 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);
|
|
|
|
}
|
2009-05-18 15:51:59 +02:00
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
/* 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)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
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);
|
2009-06-21 18:50:26 +02:00
|
|
|
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);
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
|
2009-06-21 18:50:40 +02:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2009-06-21 18:50:40 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
|
2009-06-21 18:50:40 +02:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2009-06-21 18:50:40 +02:00
|
|
|
if (msix_present(&proxy->pci_dev))
|
|
|
|
qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
|
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
|
2009-06-21 18:50:40 +02:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2009-06-21 18:50:40 +02:00
|
|
|
int ret;
|
|
|
|
ret = pci_device_load(&proxy->pci_dev, f);
|
2009-07-05 15:02:34 +02:00
|
|
|
if (ret) {
|
2009-06-21 18:50:40 +02:00
|
|
|
return ret;
|
2009-07-05 15:02:34 +02:00
|
|
|
}
|
2012-08-29 18:40:56 +02:00
|
|
|
msix_unuse_all_vectors(&proxy->pci_dev);
|
2009-06-21 18:50:40 +02:00
|
|
|
msix_load(&proxy->pci_dev, f);
|
2009-07-05 15:02:34 +02:00
|
|
|
if (msix_present(&proxy->pci_dev)) {
|
2009-06-21 18:50:40 +02:00
|
|
|
qemu_get_be16s(f, &proxy->vdev->config_vector);
|
2009-07-05 15:02:34 +02:00
|
|
|
} 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);
|
|
|
|
}
|
2009-06-21 18:50:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
|
2009-06-21 18:50:40 +02:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2009-06-21 18:50:40 +02:00
|
|
|
uint16_t vector;
|
2009-07-05 15:02:34 +02:00
|
|
|
if (msix_present(&proxy->pci_dev)) {
|
|
|
|
qemu_get_be16s(f, &vector);
|
|
|
|
} else {
|
|
|
|
vector = VIRTIO_NO_VECTOR;
|
|
|
|
}
|
2009-06-21 18:50:40 +02:00
|
|
|
virtio_queue_set_vector(proxy->vdev, n, vector);
|
2009-07-05 15:02:34 +02:00
|
|
|
if (vector != VIRTIO_NO_VECTOR) {
|
|
|
|
return msix_vector_use(&proxy->pci_dev, vector);
|
|
|
|
}
|
2009-06-21 18:50:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-17 13:01:50 +01:00
|
|
|
static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
|
2012-08-06 15:26:14 +02:00
|
|
|
int n, bool assign, bool set_handler)
|
2010-12-17 13:01:50 +01:00
|
|
|
{
|
|
|
|
VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
|
|
|
|
EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
|
2011-08-08 15:09:13 +02:00
|
|
|
int r = 0;
|
|
|
|
|
2010-12-17 13:01:50 +01:00
|
|
|
if (assign) {
|
|
|
|
r = event_notifier_init(notifier, 1);
|
|
|
|
if (r < 0) {
|
2011-01-11 13:10:15 +01:00
|
|
|
error_report("%s: unable to init event notifier: %d",
|
|
|
|
__func__, r);
|
2010-12-17 13:01:50 +01:00
|
|
|
return r;
|
|
|
|
}
|
2012-08-06 15:26:14 +02:00
|
|
|
virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
|
2011-08-08 15:09:13 +02:00
|
|
|
memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
|
2012-07-05 17:16:27 +02:00
|
|
|
true, n, notifier);
|
2010-12-17 13:01:50 +01:00
|
|
|
} else {
|
2011-08-08 15:09:13 +02:00
|
|
|
memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
|
2012-07-05 17:16:27 +02:00
|
|
|
true, n, notifier);
|
2012-08-06 15:26:14 +02:00
|
|
|
virtio_queue_set_host_notifier_fd_handler(vq, false, false);
|
2010-12-17 13:01:50 +01:00
|
|
|
event_notifier_cleanup(notifier);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-01-11 13:10:15 +01:00
|
|
|
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
|
2010-12-17 13:01:50 +01:00
|
|
|
{
|
|
|
|
int n, r;
|
|
|
|
|
|
|
|
if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
|
|
|
|
proxy->ioeventfd_disabled ||
|
|
|
|
proxy->ioeventfd_started) {
|
2011-01-11 13:10:15 +01:00
|
|
|
return;
|
2010-12-17 13:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
|
|
|
|
if (!virtio_queue_get_num(proxy->vdev, n)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-06 15:26:14 +02:00
|
|
|
r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
|
2010-12-17 13:01:50 +01:00
|
|
|
if (r < 0) {
|
|
|
|
goto assign_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
proxy->ioeventfd_started = true;
|
2011-01-11 13:10:15 +01:00
|
|
|
return;
|
2010-12-17 13:01:50 +01:00
|
|
|
|
|
|
|
assign_error:
|
|
|
|
while (--n >= 0) {
|
|
|
|
if (!virtio_queue_get_num(proxy->vdev, n)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-06 15:26:14 +02:00
|
|
|
r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
|
2011-01-11 13:10:15 +01:00
|
|
|
assert(r >= 0);
|
2010-12-17 13:01:50 +01:00
|
|
|
}
|
|
|
|
proxy->ioeventfd_started = false;
|
2011-01-11 13:10:15 +01:00
|
|
|
error_report("%s: failed. Fallback to a userspace (slower).", __func__);
|
2010-12-17 13:01:50 +01:00
|
|
|
}
|
|
|
|
|
2011-01-11 13:10:15 +01:00
|
|
|
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
|
2010-12-17 13:01:50 +01:00
|
|
|
{
|
2011-01-11 13:10:15 +01:00
|
|
|
int r;
|
2010-12-17 13:01:50 +01:00
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!proxy->ioeventfd_started) {
|
2011-01-11 13:10:15 +01:00
|
|
|
return;
|
2010-12-17 13:01:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
|
|
|
|
if (!virtio_queue_get_num(proxy->vdev, n)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-06 15:26:14 +02:00
|
|
|
r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
|
2011-01-11 13:10:15 +01:00
|
|
|
assert(r >= 0);
|
2010-12-17 13:01:50 +01:00
|
|
|
}
|
|
|
|
proxy->ioeventfd_started = false;
|
|
|
|
}
|
|
|
|
|
2011-12-04 18:05:28 +01:00
|
|
|
void virtio_pci_reset(DeviceState *d)
|
2009-06-21 18:50:13 +02:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2010-12-17 13:01:50 +01:00
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
2009-06-21 18:50:13 +02:00
|
|
|
virtio_reset(proxy->vdev);
|
2012-08-29 18:40:56 +02:00
|
|
|
msix_unuse_all_vectors(&proxy->pci_dev);
|
2010-12-17 13:01:50 +01:00
|
|
|
proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
|
2009-06-21 18:50:13 +02:00
|
|
|
}
|
|
|
|
|
2009-05-18 15:51:59 +02:00
|
|
|
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = opaque;
|
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr pa;
|
2009-05-18 15:51:59 +02:00
|
|
|
|
|
|
|
switch (addr) {
|
|
|
|
case VIRTIO_PCI_GUEST_FEATURES:
|
|
|
|
/* Guest does not negotiate properly? We have to assume nothing. */
|
|
|
|
if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
|
2011-11-24 13:28:52 +01:00
|
|
|
val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
2011-11-24 13:28:52 +01:00
|
|
|
virtio_set_features(vdev, val);
|
2009-05-18 15:51:59 +02:00
|
|
|
break;
|
|
|
|
case VIRTIO_PCI_QUEUE_PFN:
|
2012-10-23 12:30:10 +02:00
|
|
|
pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
|
2009-11-24 15:45:35 +01:00
|
|
|
if (pa == 0) {
|
2010-12-17 13:01:50 +01:00
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
2009-11-24 15:45:35 +01:00
|
|
|
virtio_reset(proxy->vdev);
|
|
|
|
msix_unuse_all_vectors(&proxy->pci_dev);
|
|
|
|
}
|
2009-06-21 18:50:13 +02:00
|
|
|
else
|
|
|
|
virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
|
2009-05-18 15:51:59 +02:00
|
|
|
break;
|
|
|
|
case VIRTIO_PCI_QUEUE_SEL:
|
|
|
|
if (val < VIRTIO_PCI_QUEUE_MAX)
|
|
|
|
vdev->queue_sel = val;
|
|
|
|
break;
|
|
|
|
case VIRTIO_PCI_QUEUE_NOTIFY:
|
2011-05-08 23:29:07 +02:00
|
|
|
if (val < VIRTIO_PCI_QUEUE_MAX) {
|
|
|
|
virtio_queue_notify(vdev, val);
|
|
|
|
}
|
2009-05-18 15:51:59 +02:00
|
|
|
break;
|
|
|
|
case VIRTIO_PCI_STATUS:
|
2010-12-17 13:01:50 +01:00
|
|
|
if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
|
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
|
|
|
}
|
|
|
|
|
2010-03-17 12:08:05 +01:00
|
|
|
virtio_set_status(vdev, val & 0xFF);
|
2010-12-17 13:01:50 +01:00
|
|
|
|
|
|
|
if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
|
|
|
|
virtio_pci_start_ioeventfd(proxy);
|
|
|
|
}
|
|
|
|
|
2009-11-24 15:45:35 +01:00
|
|
|
if (vdev->status == 0) {
|
|
|
|
virtio_reset(proxy->vdev);
|
|
|
|
msix_unuse_all_vectors(&proxy->pci_dev);
|
|
|
|
}
|
2010-03-16 19:18:07 +01:00
|
|
|
|
|
|
|
/* 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)) {
|
2010-12-17 13:01:49 +01:00
|
|
|
proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
|
2010-03-16 19:18:07 +01:00
|
|
|
}
|
2009-05-18 15:51:59 +02:00
|
|
|
break;
|
2009-06-21 18:50:26 +02:00
|
|
|
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:
|
2010-11-15 21:44:38 +01:00
|
|
|
error_report("%s: unexpected address 0x%x value 0x%x",
|
|
|
|
__func__, addr, val);
|
2009-06-21 18:50:26 +02:00
|
|
|
break;
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-21 18:50:26 +02:00
|
|
|
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
|
|
|
uint32_t ret = 0xFFFFFFFF;
|
|
|
|
|
|
|
|
switch (addr) {
|
|
|
|
case VIRTIO_PCI_HOST_FEATURES:
|
2010-01-10 12:52:53 +01:00
|
|
|
ret = proxy->host_features;
|
2009-05-18 15:51:59 +02:00
|
|
|
break;
|
|
|
|
case VIRTIO_PCI_GUEST_FEATURES:
|
2010-01-10 12:52:47 +01:00
|
|
|
ret = vdev->guest_features;
|
2009-05-18 15:51:59 +02:00
|
|
|
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;
|
2009-06-21 18:50:13 +02:00
|
|
|
qemu_set_irq(proxy->pci_dev.irq[0], 0);
|
2009-05-18 15:51:59 +02:00
|
|
|
break;
|
2009-06-21 18:50:26 +02:00
|
|
|
case VIRTIO_MSI_CONFIG_VECTOR:
|
|
|
|
ret = vdev->config_vector;
|
|
|
|
break;
|
|
|
|
case VIRTIO_MSI_QUEUE_VECTOR:
|
|
|
|
ret = virtio_queue_vector(vdev, vdev->queue_sel);
|
|
|
|
break;
|
2009-05-18 15:51:59 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-08 13:02:30 +02:00
|
|
|
static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
|
|
|
|
unsigned size)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = opaque;
|
2009-06-21 18:50:26 +02:00
|
|
|
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
|
2012-10-08 13:02:30 +02:00
|
|
|
uint64_t val = 0;
|
2009-06-21 18:50:26 +02:00
|
|
|
if (addr < config) {
|
2012-10-08 13:02:30 +02:00
|
|
|
return virtio_ioport_read(proxy, addr);
|
2009-06-21 18:50:26 +02:00
|
|
|
}
|
|
|
|
addr -= config;
|
2009-05-18 15:51:59 +02:00
|
|
|
|
2012-10-08 13:02:30 +02:00
|
|
|
switch (size) {
|
|
|
|
case 1:
|
|
|
|
val = virtio_config_readb(proxy->vdev, addr);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
val = virtio_config_readw(proxy->vdev, addr);
|
2013-01-06 19:30:17 +01:00
|
|
|
if (virtio_is_big_endian()) {
|
|
|
|
val = bswap16(val);
|
|
|
|
}
|
2012-10-08 13:02:30 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
val = virtio_config_readl(proxy->vdev, addr);
|
2013-01-06 19:30:17 +01:00
|
|
|
if (virtio_is_big_endian()) {
|
|
|
|
val = bswap32(val);
|
|
|
|
}
|
2012-10-08 13:02:30 +02:00
|
|
|
break;
|
2012-01-10 02:35:11 +01:00
|
|
|
}
|
2012-10-08 13:02:30 +02:00
|
|
|
return val;
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2012-10-08 13:02:30 +02:00
|
|
|
static void virtio_pci_config_write(void *opaque, hwaddr addr,
|
|
|
|
uint64_t val, unsigned size)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = opaque;
|
2009-06-21 18:50:26 +02:00
|
|
|
uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
|
|
|
|
if (addr < config) {
|
|
|
|
virtio_ioport_write(proxy, addr, val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
addr -= config;
|
2012-10-08 13:02:30 +02:00
|
|
|
/*
|
|
|
|
* Virtio-PCI is odd. Ioports are LE but config space is target native
|
|
|
|
* endian.
|
|
|
|
*/
|
|
|
|
switch (size) {
|
|
|
|
case 1:
|
|
|
|
virtio_config_writeb(proxy->vdev, addr, val);
|
|
|
|
break;
|
|
|
|
case 2:
|
2013-01-06 19:30:17 +01:00
|
|
|
if (virtio_is_big_endian()) {
|
|
|
|
val = bswap16(val);
|
|
|
|
}
|
2012-10-08 13:02:30 +02:00
|
|
|
virtio_config_writew(proxy->vdev, addr, val);
|
|
|
|
break;
|
|
|
|
case 4:
|
2013-01-06 19:30:17 +01:00
|
|
|
if (virtio_is_big_endian()) {
|
|
|
|
val = bswap32(val);
|
|
|
|
}
|
2012-10-08 13:02:30 +02:00
|
|
|
virtio_config_writel(proxy->vdev, addr, val);
|
|
|
|
break;
|
2012-01-10 02:35:11 +01:00
|
|
|
}
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2011-08-08 15:09:13 +02:00
|
|
|
static const MemoryRegionOps virtio_pci_config_ops = {
|
2012-10-08 13:02:30 +02:00
|
|
|
.read = virtio_pci_config_read,
|
|
|
|
.write = virtio_pci_config_write,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
2013-01-06 19:30:17 +01:00
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
2011-08-08 15:09:13 +02:00
|
|
|
};
|
2009-06-21 18:50:26 +02:00
|
|
|
|
|
|
|
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
|
|
|
|
uint32_t val, int len)
|
|
|
|
{
|
2009-09-08 16:49:41 +02:00
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
2011-07-27 10:08:20 +02:00
|
|
|
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);
|
2009-09-08 16:49:41 +02:00
|
|
|
}
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static unsigned virtio_pci_get_features(DeviceState *d)
|
2009-12-08 19:07:48 +01:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2010-01-10 12:52:53 +01:00
|
|
|
return proxy->host_features;
|
2009-12-08 19:07:48 +01:00
|
|
|
}
|
|
|
|
|
2012-05-17 15:32:39 +02:00
|
|
|
static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
|
|
|
|
unsigned int queue_no,
|
|
|
|
unsigned int vector,
|
|
|
|
MSIMessage msg)
|
|
|
|
{
|
|
|
|
VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
|
2012-07-05 17:16:30 +02:00
|
|
|
int ret;
|
2012-05-17 15:32:39 +02:00
|
|
|
|
|
|
|
if (irqfd->users == 0) {
|
|
|
|
ret = kvm_irqchip_add_msi_route(kvm_state, msg);
|
|
|
|
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)
|
2012-12-20 23:27:54 +01:00
|
|
|
{
|
|
|
|
VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
|
|
|
|
if (--irqfd->users == 0) {
|
|
|
|
kvm_irqchip_release_virq(kvm_state, irqfd->virq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:35:27 +01:00
|
|
|
static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
|
|
|
|
unsigned int queue_no,
|
|
|
|
unsigned int vector)
|
|
|
|
{
|
|
|
|
VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
|
|
|
|
VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
|
|
|
|
EventNotifier *n = virtio_queue_get_guest_notifier(vq);
|
|
|
|
int ret;
|
|
|
|
ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, irqfd->virq);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
|
|
|
|
unsigned int queue_no,
|
|
|
|
unsigned int vector)
|
2012-05-17 15:32:39 +02:00
|
|
|
{
|
|
|
|
VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
|
2012-07-05 17:16:30 +02:00
|
|
|
EventNotifier *n = virtio_queue_get_guest_notifier(vq);
|
2012-05-17 15:32:39 +02:00
|
|
|
VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
|
2012-07-05 17:16:30 +02:00
|
|
|
int ret;
|
2012-05-17 15:32:39 +02:00
|
|
|
|
2012-08-20 10:55:56 +02:00
|
|
|
ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
|
2012-05-17 15:32:39 +02:00
|
|
|
assert(ret == 0);
|
2012-12-24 16:35:27 +01:00
|
|
|
}
|
2012-05-17 15:32:39 +02:00
|
|
|
|
2012-12-20 23:27:54 +01:00
|
|
|
static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
|
|
|
|
{
|
|
|
|
PCIDevice *dev = &proxy->pci_dev;
|
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
|
|
|
unsigned int vector;
|
|
|
|
int ret, queue_no;
|
|
|
|
MSIMessage msg;
|
|
|
|
|
|
|
|
for (queue_no = 0; queue_no < nvqs; queue_no++) {
|
|
|
|
if (!virtio_queue_get_num(vdev, queue_no)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vector = virtio_queue_vector(vdev, queue_no);
|
|
|
|
if (vector >= msix_nr_vectors_allocated(dev)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
msg = msix_get_message(dev, vector);
|
|
|
|
ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto undo;
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
2012-12-24 16:35:27 +01:00
|
|
|
/* If guest supports masking, set up irqfd now.
|
|
|
|
* Otherwise, delay until unmasked in the frontend.
|
|
|
|
*/
|
|
|
|
if (proxy->vdev->guest_notifier_mask) {
|
|
|
|
ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
|
|
|
|
if (ret < 0) {
|
|
|
|
kvm_virtio_pci_vq_vector_release(proxy, vector);
|
|
|
|
goto undo;
|
|
|
|
}
|
|
|
|
}
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2012-12-20 23:27:54 +01:00
|
|
|
|
|
|
|
undo:
|
|
|
|
while (--queue_no >= 0) {
|
|
|
|
vector = virtio_queue_vector(vdev, queue_no);
|
|
|
|
if (vector >= msix_nr_vectors_allocated(dev)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-24 16:35:27 +01:00
|
|
|
if (proxy->vdev->guest_notifier_mask) {
|
2013-01-15 18:42:38 +01:00
|
|
|
kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
|
2012-12-24 16:35:27 +01:00
|
|
|
}
|
2012-12-20 23:27:54 +01:00
|
|
|
kvm_virtio_pci_vq_vector_release(proxy, vector);
|
|
|
|
}
|
|
|
|
return ret;
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
|
|
|
|
2012-12-20 23:27:54 +01:00
|
|
|
static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
|
|
|
|
{
|
|
|
|
PCIDevice *dev = &proxy->pci_dev;
|
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
|
|
|
unsigned int vector;
|
|
|
|
int 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;
|
|
|
|
}
|
2012-12-24 16:35:27 +01:00
|
|
|
/* If guest supports masking, clean up irqfd now.
|
|
|
|
* Otherwise, it was cleaned when masked in the frontend.
|
|
|
|
*/
|
|
|
|
if (proxy->vdev->guest_notifier_mask) {
|
2013-01-15 18:42:38 +01:00
|
|
|
kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
|
2012-12-24 16:35:27 +01:00
|
|
|
}
|
2012-12-20 23:27:54 +01:00
|
|
|
kvm_virtio_pci_vq_vector_release(proxy, vector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
|
|
|
|
unsigned int queue_no,
|
|
|
|
unsigned int vector,
|
|
|
|
MSIMessage msg)
|
|
|
|
{
|
|
|
|
VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
|
|
|
|
EventNotifier *n = virtio_queue_get_guest_notifier(vq);
|
|
|
|
VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
|
2013-01-14 20:20:12 +01:00
|
|
|
int ret = 0;
|
2012-12-20 23:27:54 +01:00
|
|
|
|
|
|
|
if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
|
|
|
|
ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:35:27 +01:00
|
|
|
/* If guest supports masking, irqfd is already setup, unmask it.
|
|
|
|
* Otherwise, set it up now.
|
|
|
|
*/
|
|
|
|
if (proxy->vdev->guest_notifier_mask) {
|
|
|
|
proxy->vdev->guest_notifier_mask(proxy->vdev, queue_no, false);
|
|
|
|
/* Test after unmasking to avoid losing events. */
|
|
|
|
if (proxy->vdev->guest_notifier_pending &&
|
|
|
|
proxy->vdev->guest_notifier_pending(proxy->vdev, queue_no)) {
|
|
|
|
event_notifier_set(n);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
2012-12-20 23:27:54 +01:00
|
|
|
return ret;
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
|
|
|
|
2012-12-20 23:27:54 +01:00
|
|
|
static void kvm_virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
|
2012-05-17 15:32:39 +02:00
|
|
|
unsigned int queue_no,
|
|
|
|
unsigned int vector)
|
|
|
|
{
|
2012-12-24 16:35:27 +01:00
|
|
|
/* If guest supports masking, keep irqfd but mask it.
|
|
|
|
* Otherwise, clean it up now.
|
|
|
|
*/
|
|
|
|
if (proxy->vdev->guest_notifier_mask) {
|
|
|
|
proxy->vdev->guest_notifier_mask(proxy->vdev, queue_no, true);
|
|
|
|
} else {
|
2013-01-15 18:42:38 +01:00
|
|
|
kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
|
2012-12-24 16:35:27 +01:00
|
|
|
}
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
|
|
|
|
2012-12-20 23:27:54 +01:00
|
|
|
static int kvm_virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
|
2012-05-17 15:32:39 +02:00
|
|
|
MSIMessage msg)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
|
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
|
|
|
int ret, queue_no;
|
|
|
|
|
2012-12-20 13:28:58 +01:00
|
|
|
for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
|
2012-05-17 15:32:39 +02:00
|
|
|
if (!virtio_queue_get_num(vdev, queue_no)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (virtio_queue_vector(vdev, queue_no) != vector) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-20 23:27:54 +01:00
|
|
|
ret = kvm_virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg);
|
2012-05-17 15:32:39 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
goto undo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
undo:
|
|
|
|
while (--queue_no >= 0) {
|
|
|
|
if (virtio_queue_vector(vdev, queue_no) != vector) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-20 23:27:54 +01:00
|
|
|
kvm_virtio_pci_vq_vector_mask(proxy, queue_no, vector);
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-20 23:27:54 +01:00
|
|
|
static void kvm_virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
|
2012-05-17 15:32:39 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
|
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
|
|
|
int queue_no;
|
|
|
|
|
2012-12-20 13:28:58 +01:00
|
|
|
for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
|
2012-05-17 15:32:39 +02:00
|
|
|
if (!virtio_queue_get_num(vdev, queue_no)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (virtio_queue_vector(vdev, queue_no) != vector) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-20 23:27:54 +01:00
|
|
|
kvm_virtio_pci_vq_vector_mask(proxy, queue_no, vector);
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 13:02:46 +01:00
|
|
|
static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
|
|
|
|
unsigned int vector_start,
|
|
|
|
unsigned int vector_end)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
|
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
|
|
|
int queue_no;
|
|
|
|
unsigned int vector;
|
|
|
|
EventNotifier *notifier;
|
|
|
|
VirtQueue *vq;
|
|
|
|
|
2012-12-20 13:28:58 +01:00
|
|
|
for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
|
2012-12-18 13:02:46 +01:00
|
|
|
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);
|
2012-12-24 16:35:27 +01:00
|
|
|
if (vdev->guest_notifier_pending) {
|
|
|
|
if (vdev->guest_notifier_pending(vdev, queue_no)) {
|
|
|
|
msix_set_pending(dev, vector);
|
|
|
|
}
|
|
|
|
} else if (event_notifier_test_and_clear(notifier)) {
|
2012-12-18 13:02:46 +01:00
|
|
|
msix_set_pending(dev, vector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
|
|
|
|
bool with_irqfd)
|
2010-03-17 12:08:13 +01:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2010-03-17 12:08:13 +01:00
|
|
|
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;
|
|
|
|
}
|
2012-12-18 13:02:46 +01:00
|
|
|
virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
|
2010-03-17 12:08:13 +01:00
|
|
|
} else {
|
2012-12-18 13:02:46 +01:00
|
|
|
virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
|
2010-03-17 12:08:13 +01:00
|
|
|
event_notifier_cleanup(notifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static bool virtio_pci_query_guest_notifiers(DeviceState *d)
|
2011-02-01 21:13:42 +01:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2011-02-01 21:13:42 +01:00
|
|
|
return msix_enabled(&proxy->pci_dev);
|
|
|
|
}
|
|
|
|
|
2012-12-20 13:28:58 +01:00
|
|
|
static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
|
2010-10-06 15:20:17 +02:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2010-10-06 15:20:17 +02:00
|
|
|
VirtIODevice *vdev = proxy->vdev;
|
|
|
|
int r, n;
|
2012-12-18 13:02:46 +01:00
|
|
|
bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
|
|
|
|
kvm_msi_via_irqfd_enabled();
|
2010-10-06 15:20:17 +02:00
|
|
|
|
2012-12-20 13:28:58 +01:00
|
|
|
nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX);
|
|
|
|
|
|
|
|
/* When deassigning, pass a consistent nvqs value
|
|
|
|
* to avoid leaking notifiers.
|
|
|
|
*/
|
|
|
|
assert(assign || nvqs == proxy->nvqs_with_notifiers);
|
|
|
|
|
|
|
|
proxy->nvqs_with_notifiers = nvqs;
|
|
|
|
|
2012-05-17 15:32:39 +02:00
|
|
|
/* Must unset vector notifier while guest notifier is still assigned */
|
2012-12-18 13:02:46 +01:00
|
|
|
if (proxy->vector_irqfd && !assign) {
|
2012-05-17 15:32:39 +02:00
|
|
|
msix_unset_vector_notifiers(&proxy->pci_dev);
|
2012-12-20 23:27:54 +01:00
|
|
|
kvm_virtio_pci_vector_release(proxy, nvqs);
|
2012-05-17 15:32:39 +02:00
|
|
|
g_free(proxy->vector_irqfd);
|
|
|
|
proxy->vector_irqfd = NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-20 13:28:58 +01:00
|
|
|
for (n = 0; n < nvqs; n++) {
|
2010-10-06 15:20:17 +02:00
|
|
|
if (!virtio_queue_get_num(vdev, n)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-18 13:02:46 +01:00
|
|
|
r = virtio_pci_set_guest_notifier(d, n, assign,
|
|
|
|
kvm_msi_via_irqfd_enabled());
|
2010-10-06 15:20:17 +02:00
|
|
|
if (r < 0) {
|
|
|
|
goto assign_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-17 15:32:39 +02:00
|
|
|
/* Must set vector notifier after guest notifier has been assigned */
|
2012-12-18 13:02:46 +01:00
|
|
|
if (with_irqfd && assign) {
|
2012-05-17 15:32:39 +02:00
|
|
|
proxy->vector_irqfd =
|
|
|
|
g_malloc0(sizeof(*proxy->vector_irqfd) *
|
|
|
|
msix_nr_vectors_allocated(&proxy->pci_dev));
|
2012-12-20 23:27:54 +01:00
|
|
|
r = kvm_virtio_pci_vector_use(proxy, nvqs);
|
|
|
|
if (r < 0) {
|
|
|
|
goto assign_error;
|
|
|
|
}
|
2012-05-17 15:32:39 +02:00
|
|
|
r = msix_set_vector_notifiers(&proxy->pci_dev,
|
2012-12-20 23:27:54 +01:00
|
|
|
kvm_virtio_pci_vector_unmask,
|
|
|
|
kvm_virtio_pci_vector_mask,
|
2012-12-18 13:02:46 +01:00
|
|
|
kvm_virtio_pci_vector_poll);
|
2012-05-17 15:32:39 +02:00
|
|
|
if (r < 0) {
|
2012-12-20 23:27:54 +01:00
|
|
|
goto notifiers_error;
|
2012-05-17 15:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-06 15:20:17 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-12-20 23:27:54 +01:00
|
|
|
notifiers_error:
|
|
|
|
assert(assign);
|
|
|
|
kvm_virtio_pci_vector_release(proxy, nvqs);
|
|
|
|
|
2010-10-06 15:20:17 +02:00
|
|
|
assign_error:
|
|
|
|
/* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
|
2012-05-17 15:32:39 +02:00
|
|
|
assert(assign);
|
2010-10-06 15:20:17 +02:00
|
|
|
while (--n >= 0) {
|
2012-12-18 13:02:46 +01:00
|
|
|
virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
|
2010-10-06 15:20:17 +02:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
|
2010-03-17 12:08:13 +01:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2010-12-17 13:01:50 +01:00
|
|
|
|
|
|
|
/* 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;
|
2010-03-17 12:08:13 +01:00
|
|
|
if (assign) {
|
2010-12-17 13:01:50 +01:00
|
|
|
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. */
|
2012-08-06 15:26:14 +02:00
|
|
|
return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
|
2010-12-17 13:01:50 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
static void virtio_pci_vmstate_change(DeviceState *d, bool running)
|
2010-12-17 13:01:50 +01:00
|
|
|
{
|
2012-12-17 12:01:07 +01:00
|
|
|
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
|
2010-12-17 13:01:50 +01:00
|
|
|
|
|
|
|
if (running) {
|
2011-03-19 18:28:19 +01:00
|
|
|
/* 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;
|
|
|
|
}
|
2010-12-17 13:01:50 +01:00
|
|
|
virtio_pci_start_ioeventfd(proxy);
|
2010-03-17 12:08:13 +01:00
|
|
|
} else {
|
2010-12-17 13:01:50 +01:00
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
2010-03-17 12:08:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 15:51:59 +02:00
|
|
|
static const VirtIOBindings virtio_pci_bindings = {
|
2009-06-21 18:50:40 +02:00
|
|
|
.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,
|
2009-12-08 19:07:48 +01:00
|
|
|
.get_features = virtio_pci_get_features,
|
2011-02-01 21:13:42 +01:00
|
|
|
.query_guest_notifiers = virtio_pci_query_guest_notifiers,
|
2010-03-17 12:08:13 +01:00
|
|
|
.set_host_notifier = virtio_pci_set_host_notifier,
|
2010-10-06 15:20:17 +02:00
|
|
|
.set_guest_notifiers = virtio_pci_set_guest_notifiers,
|
2010-12-17 13:01:50 +01:00
|
|
|
.vmstate_change = virtio_pci_vmstate_change,
|
2009-05-18 15:51:59 +02:00
|
|
|
};
|
|
|
|
|
2011-06-14 16:51:11 +02:00
|
|
|
void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
|
|
|
uint8_t *config;
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
proxy->vdev = vdev;
|
|
|
|
|
|
|
|
config = proxy->pci_dev.config;
|
|
|
|
|
2011-05-25 03:58:36 +02:00
|
|
|
if (proxy->class_code) {
|
|
|
|
pci_config_set_class(config, proxy->class_code);
|
|
|
|
}
|
2011-12-08 06:49:13 +01:00
|
|
|
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;
|
2009-05-18 15:51:59 +02:00
|
|
|
|
2012-06-14 20:16:10 +02:00
|
|
|
if (vdev->nvectors &&
|
|
|
|
msix_init_exclusive_bar(&proxy->pci_dev, vdev->nvectors, 1)) {
|
2009-06-21 18:50:26 +02:00
|
|
|
vdev->nvectors = 0;
|
2012-06-14 20:16:10 +02:00
|
|
|
}
|
2009-06-21 18:50:26 +02:00
|
|
|
|
2009-09-08 16:49:41 +02:00
|
|
|
proxy->pci_dev.config_write = virtio_write_config;
|
|
|
|
|
2009-06-21 18:50:26 +02:00
|
|
|
size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
|
2009-05-18 15:51:59 +02:00
|
|
|
if (size & (size-1))
|
|
|
|
size = 1 << qemu_fls(size);
|
|
|
|
|
2011-08-08 15:09:13 +02:00
|
|
|
memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
|
|
|
|
"virtio-pci", size);
|
2011-08-08 15:09:31 +02:00
|
|
|
pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
|
|
|
|
&proxy->bar);
|
2009-05-18 15:51:59 +02:00
|
|
|
|
2010-12-17 13:01:50 +01:00
|
|
|
if (!kvm_has_many_ioeventfds()) {
|
|
|
|
proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
|
|
|
|
}
|
|
|
|
|
2012-12-17 12:01:07 +01:00
|
|
|
virtio_bind_device(vdev, &virtio_pci_bindings, DEVICE(proxy));
|
2010-01-10 12:52:53 +01:00
|
|
|
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);
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2009-08-14 10:36:05 +02:00
|
|
|
static int virtio_blk_init_pci(PCIDevice *pci_dev)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
|
2009-07-15 13:48:23 +02:00
|
|
|
if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
|
|
|
|
proxy->class_code != PCI_CLASS_STORAGE_OTHER)
|
|
|
|
proxy->class_code = PCI_CLASS_STORAGE_SCSI;
|
2009-05-18 15:51:59 +02:00
|
|
|
|
2012-05-16 12:54:05 +02:00
|
|
|
vdev = virtio_blk_init(&pci_dev->qdev, &proxy->blk);
|
2010-07-06 14:37:42 +02:00
|
|
|
if (!vdev) {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-12 12:47:24 +02:00
|
|
|
vdev->nvectors = proxy->nvectors;
|
2011-05-25 03:58:36 +02:00
|
|
|
virtio_init_pci(proxy, vdev);
|
2009-08-12 12:47:24 +02:00
|
|
|
/* make the actual value visible */
|
|
|
|
proxy->nvectors = vdev->nvectors;
|
2009-08-14 10:36:05 +02:00
|
|
|
return 0;
|
2009-07-07 13:09:58 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 06:39:27 +02:00
|
|
|
static void virtio_exit_pci(PCIDevice *pci_dev)
|
2009-10-05 23:02:20 +02:00
|
|
|
{
|
2011-08-08 15:09:13 +02:00
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
|
|
|
memory_region_destroy(&proxy->bar);
|
2012-06-14 20:16:10 +02:00
|
|
|
msix_uninit_exclusive_bar(pci_dev);
|
2009-10-05 23:02:20 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 06:39:27 +02:00
|
|
|
static void virtio_blk_exit_pci(PCIDevice *pci_dev)
|
2009-09-25 21:42:46 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
2010-12-17 13:01:50 +01:00
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
2010-07-20 19:14:22 +02:00
|
|
|
virtio_blk_exit(proxy->vdev);
|
2012-07-04 06:39:27 +02:00
|
|
|
virtio_exit_pci(pci_dev);
|
2009-09-25 21:42:46 +02:00
|
|
|
}
|
|
|
|
|
virtio-console: qdev conversion, new virtio-serial-bus
This commit converts the virtio-console device to create a new
virtio-serial bus that can host console and generic serial ports. The
file hosting this code is now called virtio-serial-bus.c.
The virtio console is now a very simple qdev device that sits on the
virtio-serial-bus and communicates between the bus and qemu's chardevs.
This commit also includes a few changes to the virtio backing code for
pci and s390 to spawn the virtio-serial bus.
As a result of the qdev conversion, we get rid of a lot of legacy code.
The old-style way of instantiating a virtio console using
-virtioconsole ...
is maintained, but the new, preferred way is to use
-device virtio-serial -device virtconsole,chardev=...
With this commit, multiple devices as well as multiple ports with a
single device can be supported.
For multiple ports support, each port gets an IO vq pair. Since the
guest needs to know in advance how many vqs a particular device will
need, we have to set this number as a property of the virtio-serial
device and also as a config option.
In addition, we also spawn a pair of control IO vqs. This is an internal
channel meant for guest-host communication for things like port
open/close, sending port properties over to the guest, etc.
This commit is a part of a series of other commits to get the full
implementation of multiport support. Future commits will add other
support as well as ride on the savevm version that we bump up here.
Signed-off-by: Amit Shah <amit.shah@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2010-01-19 20:06:52 +01:00
|
|
|
static int virtio_serial_init_pci(PCIDevice *pci_dev)
|
2009-07-07 13:09:58 +02:00
|
|
|
{
|
2009-07-15 13:48:24 +02:00
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
2009-07-30 12:29:13 +02:00
|
|
|
VirtIODevice *vdev;
|
|
|
|
|
2009-07-15 13:48:24 +02:00
|
|
|
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;
|
|
|
|
|
2011-02-03 06:52:32 +01:00
|
|
|
vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
|
2009-09-29 12:21:04 +02:00
|
|
|
if (!vdev) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-25 12:54:44 +01:00
|
|
|
vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
|
2011-02-03 06:52:32 +01:00
|
|
|
? proxy->serial.max_virtserial_ports + 1
|
2010-02-25 12:54:44 +01:00
|
|
|
: proxy->nvectors;
|
2011-05-25 03:58:36 +02:00
|
|
|
virtio_init_pci(proxy, vdev);
|
2010-01-19 20:06:58 +01:00
|
|
|
proxy->nvectors = vdev->nvectors;
|
2009-08-14 10:36:05 +02:00
|
|
|
return 0;
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 06:39:27 +02:00
|
|
|
static void virtio_serial_exit_pci(PCIDevice *pci_dev)
|
2010-08-19 03:21:04 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
2011-02-17 08:01:10 +01:00
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
2010-08-19 03:21:04 +02:00
|
|
|
virtio_serial_exit(proxy->vdev);
|
2012-07-04 06:39:27 +02:00
|
|
|
virtio_exit_pci(pci_dev);
|
2010-08-19 03:21:04 +02:00
|
|
|
}
|
|
|
|
|
2009-08-14 10:36:05 +02:00
|
|
|
static int virtio_net_init_pci(PCIDevice *pci_dev)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
|
2010-09-02 17:00:50 +02:00
|
|
|
vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
|
2009-07-15 13:48:25 +02:00
|
|
|
|
2009-10-21 15:25:35 +02:00
|
|
|
vdev->nvectors = proxy->nvectors;
|
2011-05-25 03:58:36 +02:00
|
|
|
virtio_init_pci(proxy, vdev);
|
2009-07-15 13:48:25 +02:00
|
|
|
|
|
|
|
/* make the actual value visible */
|
|
|
|
proxy->nvectors = vdev->nvectors;
|
2009-08-14 10:36:05 +02:00
|
|
|
return 0;
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 06:39:27 +02:00
|
|
|
static void virtio_net_exit_pci(PCIDevice *pci_dev)
|
2009-10-21 15:25:35 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
2010-12-17 13:01:50 +01:00
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
2009-10-21 15:25:35 +02:00
|
|
|
virtio_net_exit(proxy->vdev);
|
2012-07-04 06:39:27 +02:00
|
|
|
virtio_exit_pci(pci_dev);
|
2009-10-21 15:25:35 +02:00
|
|
|
}
|
|
|
|
|
2009-08-14 10:36:05 +02:00
|
|
|
static int virtio_balloon_init_pci(PCIDevice *pci_dev)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
|
2012-04-03 16:24:11 +02:00
|
|
|
if (proxy->class_code != PCI_CLASS_OTHERS &&
|
|
|
|
proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
|
|
|
|
proxy->class_code = PCI_CLASS_OTHERS;
|
|
|
|
}
|
|
|
|
|
2009-05-18 15:51:59 +02:00
|
|
|
vdev = virtio_balloon_init(&pci_dev->qdev);
|
2011-07-27 08:59:33 +02:00
|
|
|
if (!vdev) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-25 03:58:36 +02:00
|
|
|
virtio_init_pci(proxy, vdev);
|
2009-08-14 10:36:05 +02:00
|
|
|
return 0;
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2012-07-04 06:39:27 +02:00
|
|
|
static void virtio_balloon_exit_pci(PCIDevice *pci_dev)
|
2011-07-27 10:20:41 +02:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
|
|
|
virtio_balloon_exit(proxy->vdev);
|
2012-07-04 06:39:27 +02:00
|
|
|
virtio_exit_pci(pci_dev);
|
2011-07-27 10:20:41 +02:00
|
|
|
}
|
|
|
|
|
2012-06-20 08:59:32 +02:00
|
|
|
static int virtio_rng_init_pci(PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
|
2012-10-26 19:05:49 +02:00
|
|
|
if (proxy->rng.rng == NULL) {
|
|
|
|
proxy->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
|
|
|
|
|
|
|
|
object_property_add_child(OBJECT(pci_dev),
|
|
|
|
"default-backend",
|
|
|
|
OBJECT(proxy->rng.default_backend),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
object_property_set_link(OBJECT(pci_dev),
|
|
|
|
OBJECT(proxy->rng.default_backend),
|
|
|
|
"rng", NULL);
|
|
|
|
}
|
|
|
|
|
2012-06-20 08:59:32 +02:00
|
|
|
vdev = virtio_rng_init(&pci_dev->qdev, &proxy->rng);
|
|
|
|
if (!vdev) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
virtio_init_pci(proxy, vdev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_rng_exit_pci(PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
|
|
|
virtio_rng_exit(proxy->vdev);
|
|
|
|
virtio_exit_pci(pci_dev);
|
|
|
|
}
|
|
|
|
|
2011-12-04 19:22:06 +01:00
|
|
|
static Property virtio_blk_properties[] = {
|
|
|
|
DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
|
2012-05-16 12:54:05 +02:00
|
|
|
DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, blk.conf),
|
2012-07-10 11:12:43 +02:00
|
|
|
DEFINE_BLOCK_CHS_PROPERTIES(VirtIOPCIProxy, blk.conf),
|
2012-05-16 12:54:05 +02:00
|
|
|
DEFINE_PROP_STRING("serial", VirtIOPCIProxy, blk.serial),
|
virtio-blk: always enable VIRTIO_BLK_F_SCSI
VIRTIO_BLK_F_SCSI is supposed to mean whether the host can *parse*
SCSI requests, not *execute* them. You could run QEMU with scsi=on
and a file-backed disk, and QEMU would fail all SCSI requests even
though it advertises VIRTIO_BLK_F_SCSI.
Because we need to do this to fix a migration compatibility problem
related to how QEMU is invoked by management, we must do this
unconditionally even on older machine types. This more or less assumes
that no one ever invoked QEMU with scsi=off.
Here is how testing goes:
- old QEMU, scsi=on -> new QEMU, scsi=on
- new QEMU, scsi=on -> old QEMU, scsi=on
- old QEMU, scsi=off -> new QEMU, scsi=on
- new QEMU, scsi=off -> old QEMU, scsi=on
ok (new QEMU has VIRTIO_BLK_F_SCSI, adding host features is fine)
- old QEMU, scsi=off -> new QEMU, scsi=off
ok (new QEMU has VIRTIO_BLK_F_SCSI, adding host features is fine)
- old QEMU, scsi=on -> new QEMU, scsi=off
ok, bug fixed
- new QEMU, scsi=on -> old QEMU, scsi=off
doesn't work (same as: old QEMU, scsi=on -> old QEMU, scsi=off)
- new QEMU, scsi=off -> old QEMU, scsi=off
broken by the patch
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-05-16 12:54:06 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
DEFINE_PROP_BIT("scsi", VirtIOPCIProxy, blk.scsi, 0, true),
|
|
|
|
#endif
|
2012-12-10 13:14:39 +01:00
|
|
|
DEFINE_PROP_BIT("config-wce", VirtIOPCIProxy, blk.config_wce, 0, true),
|
2011-12-04 19:22:06 +01:00
|
|
|
DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
|
2012-11-14 15:45:38 +01:00
|
|
|
#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
|
|
|
|
DEFINE_PROP_BIT("x-data-plane", VirtIOPCIProxy, blk.data_plane, 0, false),
|
|
|
|
#endif
|
2011-12-04 19:22:06 +01:00
|
|
|
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
|
|
|
|
DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
2011-12-07 02:32:44 +01:00
|
|
|
};
|
|
|
|
|
2011-12-04 19:22:06 +01:00
|
|
|
static void virtio_blk_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-04 19:22:06 +01:00
|
|
|
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;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->reset = virtio_pci_reset;
|
|
|
|
dc->props = virtio_blk_properties;
|
2011-12-04 19:22:06 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo virtio_blk_info = {
|
2011-12-08 04:34:16 +01:00
|
|
|
.name = "virtio-blk-pci",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(VirtIOPCIProxy),
|
|
|
|
.class_init = virtio_blk_class_init,
|
2011-12-04 19:22:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-04 19:22:06 +01:00
|
|
|
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;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->reset = virtio_pci_reset;
|
|
|
|
dc->props = virtio_net_properties;
|
2011-12-04 19:22:06 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo virtio_net_info = {
|
2011-12-08 04:34:16 +01:00
|
|
|
.name = "virtio-net-pci",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(VirtIOPCIProxy),
|
|
|
|
.class_init = virtio_net_class_init,
|
2011-12-07 02:32:44 +01:00
|
|
|
};
|
|
|
|
|
2011-12-04 19:22:06 +01:00
|
|
|
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(),
|
2011-12-07 02:32:44 +01:00
|
|
|
};
|
|
|
|
|
2011-12-04 19:22:06 +01:00
|
|
|
static void virtio_serial_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-04 19:22:06 +01:00
|
|
|
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;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->reset = virtio_pci_reset;
|
|
|
|
dc->props = virtio_serial_properties;
|
2011-12-04 19:22:06 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo virtio_serial_info = {
|
2011-12-08 04:34:16 +01:00
|
|
|
.name = "virtio-serial-pci",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(VirtIOPCIProxy),
|
|
|
|
.class_init = virtio_serial_class_init,
|
2011-12-04 19:22:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static Property virtio_balloon_properties[] = {
|
|
|
|
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
|
2012-04-03 16:24:11 +02:00
|
|
|
DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
|
2011-12-04 19:22:06 +01:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_balloon_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-04 19:22:06 +01:00
|
|
|
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;
|
2012-04-03 16:24:11 +02:00
|
|
|
k->class_id = PCI_CLASS_OTHERS;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->reset = virtio_pci_reset;
|
|
|
|
dc->props = virtio_balloon_properties;
|
2011-12-04 19:22:06 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo virtio_balloon_info = {
|
2011-12-08 04:34:16 +01:00
|
|
|
.name = "virtio-balloon-pci",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(VirtIOPCIProxy),
|
|
|
|
.class_init = virtio_balloon_class_init,
|
2009-06-30 14:12:07 +02:00
|
|
|
};
|
|
|
|
|
2012-06-20 08:59:32 +02:00
|
|
|
static void virtio_rng_initfn(Object *obj)
|
|
|
|
{
|
|
|
|
PCIDevice *pci_dev = PCI_DEVICE(obj);
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
|
|
|
object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
|
|
|
|
(Object **)&proxy->rng.rng, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property virtio_rng_properties[] = {
|
|
|
|
DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
|
2012-10-30 23:45:05 +01:00
|
|
|
/* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
|
|
|
|
you have an entropy source capable of generating more entropy than this
|
|
|
|
and you can pass it through via virtio-rng, then hats off to you. Until
|
|
|
|
then, this is unlimited for all practical purposes.
|
|
|
|
*/
|
|
|
|
DEFINE_PROP_UINT64("max-bytes", VirtIOPCIProxy, rng.max_bytes, INT64_MAX),
|
|
|
|
DEFINE_PROP_UINT32("period", VirtIOPCIProxy, rng.period_ms, 1 << 16),
|
2012-06-20 08:59:32 +02:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_rng_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = virtio_rng_init_pci;
|
|
|
|
k->exit = virtio_rng_exit_pci;
|
|
|
|
k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
|
|
|
|
k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
|
|
|
|
k->revision = VIRTIO_PCI_ABI_VERSION;
|
|
|
|
k->class_id = PCI_CLASS_OTHERS;
|
|
|
|
dc->reset = virtio_pci_reset;
|
|
|
|
dc->props = virtio_rng_properties;
|
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo virtio_rng_info = {
|
2012-06-20 08:59:32 +02:00
|
|
|
.name = "virtio-rng-pci",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(VirtIOPCIProxy),
|
|
|
|
.instance_init = virtio_rng_initfn,
|
|
|
|
.class_init = virtio_rng_class_init,
|
|
|
|
};
|
|
|
|
|
2011-02-11 09:40:59 +01:00
|
|
|
static int virtio_scsi_init_pci(PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
|
|
|
|
vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
|
|
|
|
if (!vdev) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-07-27 14:38:03 +02:00
|
|
|
vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
|
|
|
|
? proxy->scsi.num_queues + 3
|
|
|
|
: proxy->nvectors;
|
2011-02-11 09:40:59 +01:00
|
|
|
virtio_init_pci(proxy, vdev);
|
|
|
|
|
|
|
|
/* make the actual value visible */
|
|
|
|
proxy->nvectors = vdev->nvectors;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-04 06:39:27 +02:00
|
|
|
static void virtio_scsi_exit_pci(PCIDevice *pci_dev)
|
2011-02-11 09:40:59 +01:00
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
|
|
|
|
|
|
|
virtio_scsi_exit(proxy->vdev);
|
2012-07-04 06:39:27 +02:00
|
|
|
virtio_exit_pci(pci_dev);
|
2011-02-11 09:40:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Property virtio_scsi_properties[] = {
|
2012-07-27 14:36:07 +02:00
|
|
|
DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
|
2012-07-27 14:38:03 +02:00
|
|
|
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
|
2011-02-11 09:40:59 +01:00
|
|
|
DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_scsi_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = virtio_scsi_init_pci;
|
|
|
|
k->exit = virtio_scsi_exit_pci;
|
|
|
|
k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
|
|
|
|
k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
|
|
|
|
k->revision = 0x00;
|
|
|
|
k->class_id = PCI_CLASS_STORAGE_SCSI;
|
|
|
|
dc->reset = virtio_pci_reset;
|
|
|
|
dc->props = virtio_scsi_properties;
|
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo virtio_scsi_info = {
|
2011-02-11 09:40:59 +01:00
|
|
|
.name = "virtio-scsi-pci",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(VirtIOPCIProxy),
|
|
|
|
.class_init = virtio_scsi_class_init,
|
|
|
|
};
|
|
|
|
|
2013-01-15 00:08:04 +01:00
|
|
|
/*
|
|
|
|
* virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This is called by virtio-bus just after the device is plugged. */
|
|
|
|
static void virtio_pci_device_plugged(DeviceState *d)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
|
|
|
|
VirtioBusState *bus = &proxy->bus;
|
|
|
|
uint8_t *config;
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
proxy->vdev = bus->vdev;
|
|
|
|
|
|
|
|
config = proxy->pci_dev.config;
|
|
|
|
if (proxy->class_code) {
|
|
|
|
pci_config_set_class(config, proxy->class_code);
|
|
|
|
}
|
|
|
|
pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
|
|
|
|
pci_get_word(config + PCI_VENDOR_ID));
|
|
|
|
pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
|
|
|
|
config[PCI_INTERRUPT_PIN] = 1;
|
|
|
|
|
|
|
|
if (proxy->nvectors &&
|
|
|
|
msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
|
|
|
|
proxy->nvectors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy->pci_dev.config_write = virtio_write_config;
|
|
|
|
|
|
|
|
size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
|
|
|
|
+ virtio_bus_get_vdev_config_len(bus);
|
|
|
|
if (size & (size - 1)) {
|
|
|
|
size = 1 << qemu_fls(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
|
|
|
|
"virtio-pci", size);
|
|
|
|
pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
|
|
|
|
&proxy->bar);
|
|
|
|
|
|
|
|
if (!kvm_has_many_ioeventfds()) {
|
|
|
|
proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
|
|
|
|
proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
|
|
|
|
proxy->host_features = virtio_bus_get_vdev_features(bus,
|
|
|
|
proxy->host_features);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is called by virtio-bus just before the device is unplugged. */
|
|
|
|
static void virtio_pci_device_unplug(DeviceState *d)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *dev = VIRTIO_PCI(d);
|
|
|
|
virtio_pci_stop_ioeventfd(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virtio_pci_init(PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
|
|
|
|
VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
|
|
|
|
virtio_pci_bus_new(&dev->bus, dev);
|
|
|
|
if (k->init != NULL) {
|
|
|
|
return k->init(dev);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_pci_exit(PCIDevice *pci_dev)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
|
|
|
|
VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
|
|
|
|
BusState *qbus = BUS(&proxy->bus);
|
|
|
|
virtio_bus_destroy_device(bus);
|
|
|
|
qbus_free(qbus);
|
|
|
|
virtio_exit_pci(pci_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This will be renamed virtio_pci_reset at the end of the series.
|
|
|
|
* virtio_pci_reset is still in use at this moment.
|
|
|
|
*/
|
|
|
|
static void virtio_pci_rst(DeviceState *qdev)
|
|
|
|
{
|
|
|
|
VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
|
|
|
|
VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
|
|
|
|
virtio_pci_stop_ioeventfd(proxy);
|
|
|
|
virtio_bus_reset(bus);
|
|
|
|
msix_unuse_all_vectors(&proxy->pci_dev);
|
|
|
|
proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_pci_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = virtio_pci_init;
|
|
|
|
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;
|
|
|
|
dc->reset = virtio_pci_rst;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2013-01-15 00:08:03 +01:00
|
|
|
/* virtio-pci-bus */
|
|
|
|
|
|
|
|
void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev)
|
|
|
|
{
|
|
|
|
DeviceState *qdev = DEVICE(dev);
|
|
|
|
BusState *qbus;
|
|
|
|
qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_PCI_BUS, qdev, NULL);
|
|
|
|
qbus = BUS(bus);
|
|
|
|
qbus->allow_hotplug = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
k->get_features = virtio_pci_get_features;
|
|
|
|
k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
|
|
|
|
k->set_host_notifier = virtio_pci_set_host_notifier;
|
|
|
|
k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
|
|
|
|
k->vmstate_change = virtio_pci_vmstate_change;
|
2013-01-15 00:08:04 +01:00
|
|
|
k->device_plugged = virtio_pci_device_plugged;
|
|
|
|
k->device_unplug = virtio_pci_device_unplug;
|
2013-01-15 00:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void virtio_pci_register_types(void)
|
2009-05-18 15:51:59 +02:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&virtio_blk_info);
|
|
|
|
type_register_static(&virtio_net_info);
|
|
|
|
type_register_static(&virtio_serial_info);
|
|
|
|
type_register_static(&virtio_balloon_info);
|
2011-02-11 09:40:59 +01:00
|
|
|
type_register_static(&virtio_scsi_info);
|
2012-06-20 08:59:32 +02:00
|
|
|
type_register_static(&virtio_rng_info);
|
2013-01-15 00:08:03 +01:00
|
|
|
type_register_static(&virtio_pci_bus_info);
|
2013-01-15 00:08:04 +01:00
|
|
|
type_register_static(&virtio_pci_info);
|
2009-05-18 15:51:59 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(virtio_pci_register_types)
|