Separate virtio PCI code
Split the PCI host bindings from the VRing transport implementation. Signed-off-by: Paul Brook <paul@codesourcery.com>
This commit is contained in:
parent
d8ee7665bf
commit
53c25cea7d
|
@ -489,7 +489,8 @@ OBJS=vl.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o dma-helpers.o \
|
||||||
gdbstub.o gdbstub-xml.o sysbus.o
|
gdbstub.o gdbstub-xml.o sysbus.o
|
||||||
# virtio has to be here due to weird dependency between PCI and virtio-net.
|
# virtio has to be here due to weird dependency between PCI and virtio-net.
|
||||||
# need to fix this properly
|
# need to fix this properly
|
||||||
OBJS+=virtio.o virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o
|
OBJS+=virtio.o virtio-pci.o
|
||||||
|
OBJS+=virtio-blk.o virtio-balloon.o virtio-net.o virtio-console.o
|
||||||
OBJS+=fw_cfg.o
|
OBJS+=fw_cfg.o
|
||||||
ifdef CONFIG_KVM
|
ifdef CONFIG_KVM
|
||||||
OBJS+=kvm.o kvm-all.o
|
OBJS+=kvm.o kvm-all.o
|
||||||
|
|
6
hw/pc.c
6
hw/pc.c
|
@ -1137,21 +1137,21 @@ static void pc_init1(ram_addr_t ram_size,
|
||||||
int unit_id = 0;
|
int unit_id = 0;
|
||||||
|
|
||||||
while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
|
while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
|
||||||
pci_create_simple(pci_bus, -1, "virtio-blk");
|
pci_create_simple(pci_bus, -1, "virtio-blk-pci");
|
||||||
unit_id++;
|
unit_id++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add virtio balloon device */
|
/* Add virtio balloon device */
|
||||||
if (pci_enabled) {
|
if (pci_enabled) {
|
||||||
pci_create_simple(pci_bus, -1, "virtio-balloon");
|
pci_create_simple(pci_bus, -1, "virtio-balloon-pci");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add virtio console devices */
|
/* Add virtio console devices */
|
||||||
if (pci_enabled) {
|
if (pci_enabled) {
|
||||||
for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
|
for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
|
||||||
if (virtcon_hds[i]) {
|
if (virtcon_hds[i]) {
|
||||||
pci_create_simple(pci_bus, -1, "virtio-console");
|
pci_create_simple(pci_bus, -1, "virtio-console-pci");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,7 +120,7 @@ static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus,
|
||||||
opaque = pci_create_simple(pci_bus, -1, "lsi53c895a");
|
opaque = pci_create_simple(pci_bus, -1, "lsi53c895a");
|
||||||
break;
|
break;
|
||||||
case IF_VIRTIO:
|
case IF_VIRTIO:
|
||||||
opaque = pci_create_simple(pci_bus, -1, "virtio-blk");
|
opaque = pci_create_simple(pci_bus, -1, "virtio-blk-pci");
|
||||||
qdev_init(opaque);
|
qdev_init(opaque);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
2
hw/pci.c
2
hw/pci.c
|
@ -806,7 +806,7 @@ static const char * const pci_nic_names[] = {
|
||||||
"rtl8139",
|
"rtl8139",
|
||||||
"e1000",
|
"e1000",
|
||||||
"pcnet",
|
"pcnet",
|
||||||
"virtio_net",
|
"virtio-net-pci",
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -111,14 +111,14 @@ static void bamboo_init(ram_addr_t ram_size,
|
||||||
|
|
||||||
/* Add virtio block devices. */
|
/* Add virtio block devices. */
|
||||||
while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
|
while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
|
||||||
pci_create_simple(pcibus, -1, "virtio-blk");
|
pci_create_simple(pcibus, -1, "virtio-blk-pci");
|
||||||
unit_id++;
|
unit_id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add virtio console devices */
|
/* Add virtio console devices */
|
||||||
for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
|
for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
|
||||||
if (virtcon_hds[i]) {
|
if (virtcon_hds[i]) {
|
||||||
pci_create_simple(pcibus, -1, "virtio-console");
|
pci_create_simple(pcibus, -1, "virtio-console-pci");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -220,7 +220,7 @@ static void mpc8544ds_init(ram_addr_t ram_size,
|
||||||
|
|
||||||
/* Add virtio block devices. */
|
/* Add virtio block devices. */
|
||||||
while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
|
while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
|
||||||
pci_create_simple(pci_bus, -1, "virtio-blk");
|
pci_create_simple(pci_bus, -1, "virtio-blk-pci");
|
||||||
unit_id++;
|
unit_id++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,17 +169,13 @@ static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_balloon_init(PCIDevice *pci_dev)
|
VirtIODevice *virtio_balloon_init(DeviceState *dev)
|
||||||
{
|
{
|
||||||
VirtIOBalloon *s;
|
VirtIOBalloon *s;
|
||||||
|
|
||||||
s = (VirtIOBalloon *)virtio_init_pci(pci_dev, "virtio-balloon",
|
s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
VIRTIO_ID_BALLOON,
|
||||||
PCI_DEVICE_ID_VIRTIO_BALLOON,
|
8, sizeof(VirtIOBalloon));
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
|
||||||
VIRTIO_ID_BALLOON,
|
|
||||||
PCI_CLASS_MEMORY_RAM, 0x00,
|
|
||||||
8);
|
|
||||||
|
|
||||||
s->vdev.get_config = virtio_balloon_get_config;
|
s->vdev.get_config = virtio_balloon_get_config;
|
||||||
s->vdev.set_config = virtio_balloon_set_config;
|
s->vdev.set_config = virtio_balloon_set_config;
|
||||||
|
@ -191,12 +187,6 @@ static void virtio_balloon_init(PCIDevice *pci_dev)
|
||||||
qemu_add_balloon_handler(virtio_balloon_to_target, s);
|
qemu_add_balloon_handler(virtio_balloon_to_target, s);
|
||||||
|
|
||||||
register_savevm("virtio-balloon", -1, 1, virtio_balloon_save, virtio_balloon_load, s);
|
register_savevm("virtio-balloon", -1, 1, virtio_balloon_save, virtio_balloon_load, s);
|
||||||
}
|
|
||||||
|
|
||||||
static void virtio_balloon_register_devices(void)
|
return &s->vdev;
|
||||||
{
|
|
||||||
pci_qdev_register("virtio-balloon", sizeof(VirtIOBalloon),
|
|
||||||
virtio_balloon_init);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device_init(virtio_balloon_register_devices)
|
|
||||||
|
|
|
@ -348,28 +348,24 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_blk_init(PCIDevice *pci_dev)
|
VirtIODevice *virtio_blk_init(DeviceState *dev)
|
||||||
{
|
{
|
||||||
VirtIOBlock *s;
|
VirtIOBlock *s;
|
||||||
int cylinders, heads, secs;
|
int cylinders, heads, secs;
|
||||||
static int virtio_blk_id;
|
static int virtio_blk_id;
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
|
|
||||||
s = (VirtIOBlock *)virtio_init_pci(pci_dev, "virtio-blk",
|
s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
sizeof(struct virtio_blk_config),
|
||||||
PCI_DEVICE_ID_VIRTIO_BLOCK,
|
sizeof(VirtIOBlock));
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
|
||||||
VIRTIO_ID_BLOCK,
|
|
||||||
PCI_CLASS_STORAGE_OTHER, 0x00,
|
|
||||||
sizeof(struct virtio_blk_config));
|
|
||||||
|
|
||||||
bs = qdev_init_bdrv(&pci_dev->qdev, IF_VIRTIO);
|
bs = qdev_init_bdrv(dev, IF_VIRTIO);
|
||||||
s->vdev.get_config = virtio_blk_update_config;
|
s->vdev.get_config = virtio_blk_update_config;
|
||||||
s->vdev.get_features = virtio_blk_get_features;
|
s->vdev.get_features = virtio_blk_get_features;
|
||||||
s->vdev.reset = virtio_blk_reset;
|
s->vdev.reset = virtio_blk_reset;
|
||||||
s->bs = bs;
|
s->bs = bs;
|
||||||
s->rq = NULL;
|
s->rq = NULL;
|
||||||
bs->private = &s->vdev.pci_dev;
|
bs->private = dev;
|
||||||
bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
|
bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs);
|
||||||
bdrv_set_geometry_hint(s->bs, cylinders, heads, secs);
|
bdrv_set_geometry_hint(s->bs, cylinders, heads, secs);
|
||||||
|
|
||||||
|
@ -378,11 +374,6 @@ static void virtio_blk_init(PCIDevice *pci_dev)
|
||||||
qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
|
qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s);
|
||||||
register_savevm("virtio-blk", virtio_blk_id++, 2,
|
register_savevm("virtio-blk", virtio_blk_id++, 2,
|
||||||
virtio_blk_save, virtio_blk_load, s);
|
virtio_blk_save, virtio_blk_load, s);
|
||||||
}
|
|
||||||
|
|
||||||
static void virtio_blk_register_devices(void)
|
return &s->vdev;
|
||||||
{
|
|
||||||
pci_qdev_register("virtio-blk", sizeof(VirtIOBlock), virtio_blk_init);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device_init(virtio_blk_register_devices)
|
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
|
|
||||||
#include "virtio.h"
|
#include "virtio.h"
|
||||||
#include "block.h"
|
#include "block.h"
|
||||||
#include "pci.h"
|
|
||||||
|
|
||||||
/* from Linux's linux/virtio_blk.h */
|
/* from Linux's linux/virtio_blk.h */
|
||||||
|
|
||||||
|
|
|
@ -123,31 +123,21 @@ static int virtio_console_load(QEMUFile *f, void *opaque, int version_id)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_console_init(PCIDevice *pci_dev)
|
VirtIODevice *virtio_console_init(DeviceState *dev)
|
||||||
{
|
{
|
||||||
VirtIOConsole *s;
|
VirtIOConsole *s;
|
||||||
s = (VirtIOConsole *)virtio_init_pci(pci_dev, "virtio-console",
|
s = (VirtIOConsole *)virtio_common_init("virtio-console",
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
VIRTIO_ID_CONSOLE,
|
||||||
PCI_DEVICE_ID_VIRTIO_CONSOLE,
|
0, sizeof(VirtIOConsole));
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
|
||||||
VIRTIO_ID_CONSOLE,
|
|
||||||
PCI_CLASS_DISPLAY_OTHER, 0x00,
|
|
||||||
0);
|
|
||||||
s->vdev.get_features = virtio_console_get_features;
|
s->vdev.get_features = virtio_console_get_features;
|
||||||
|
|
||||||
s->ivq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_input);
|
s->ivq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_input);
|
||||||
s->dvq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_output);
|
s->dvq = virtio_add_queue(&s->vdev, 128, virtio_console_handle_output);
|
||||||
|
|
||||||
s->chr = qdev_init_chardev(&pci_dev->qdev);
|
s->chr = qdev_init_chardev(dev);
|
||||||
qemu_chr_add_handlers(s->chr, vcon_can_read, vcon_read, vcon_event, s);
|
qemu_chr_add_handlers(s->chr, vcon_can_read, vcon_read, vcon_event, s);
|
||||||
|
|
||||||
register_savevm("virtio-console", -1, 1, virtio_console_save, virtio_console_load, s);
|
register_savevm("virtio-console", -1, 1, virtio_console_save, virtio_console_load, s);
|
||||||
}
|
|
||||||
|
|
||||||
static void virtio_console_register_devices(void)
|
return &s->vdev;
|
||||||
{
|
|
||||||
pci_qdev_register("virtio-console", sizeof(VirtIOConsole),
|
|
||||||
virtio_console_init);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device_init(virtio_console_register_devices)
|
|
||||||
|
|
|
@ -585,18 +585,14 @@ static void virtio_net_cleanup(VLANClientState *vc)
|
||||||
virtio_cleanup(&n->vdev);
|
virtio_cleanup(&n->vdev);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_net_init(PCIDevice *pci_dev)
|
VirtIODevice *virtio_net_init(DeviceState *dev)
|
||||||
{
|
{
|
||||||
VirtIONet *n;
|
VirtIONet *n;
|
||||||
static int virtio_net_id;
|
static int virtio_net_id;
|
||||||
|
|
||||||
n = (VirtIONet *)virtio_init_pci(pci_dev, "virtio-net",
|
n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
sizeof(struct virtio_net_config),
|
||||||
PCI_DEVICE_ID_VIRTIO_NET,
|
sizeof(VirtIONet));
|
||||||
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
|
||||||
VIRTIO_ID_NET,
|
|
||||||
PCI_CLASS_NETWORK_ETHERNET, 0x00,
|
|
||||||
sizeof(struct virtio_net_config));
|
|
||||||
|
|
||||||
n->vdev.get_config = virtio_net_get_config;
|
n->vdev.get_config = virtio_net_get_config;
|
||||||
n->vdev.set_config = virtio_net_set_config;
|
n->vdev.set_config = virtio_net_set_config;
|
||||||
|
@ -607,9 +603,9 @@ static void virtio_net_init(PCIDevice *pci_dev)
|
||||||
n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
|
n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
|
||||||
n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
|
n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
|
||||||
n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
|
n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
|
||||||
qdev_get_macaddr(&pci_dev->qdev, n->mac);
|
qdev_get_macaddr(dev, n->mac);
|
||||||
n->status = VIRTIO_NET_S_LINK_UP;
|
n->status = VIRTIO_NET_S_LINK_UP;
|
||||||
n->vc = qdev_get_vlan_client(&pci_dev->qdev,
|
n->vc = qdev_get_vlan_client(dev,
|
||||||
virtio_net_receive,
|
virtio_net_receive,
|
||||||
virtio_net_can_receive,
|
virtio_net_can_receive,
|
||||||
virtio_net_cleanup, n);
|
virtio_net_cleanup, n);
|
||||||
|
@ -628,11 +624,6 @@ static void virtio_net_init(PCIDevice *pci_dev)
|
||||||
|
|
||||||
register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
|
register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
|
||||||
virtio_net_save, virtio_net_load, n);
|
virtio_net_save, virtio_net_load, n);
|
||||||
}
|
|
||||||
|
|
||||||
static void virtio_net_register_devices(void)
|
return &n->vdev;
|
||||||
{
|
|
||||||
pci_qdev_register("virtio_net", sizeof(VirtIONet), virtio_net_init);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
device_init(virtio_net_register_devices)
|
|
||||||
|
|
|
@ -0,0 +1,349 @@
|
||||||
|
/*
|
||||||
|
* 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"
|
||||||
|
#include "pci.h"
|
||||||
|
#include "sysemu.h"
|
||||||
|
|
||||||
|
/* 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
|
||||||
|
|
||||||
|
#define VIRTIO_PCI_CONFIG 20
|
||||||
|
|
||||||
|
/* Virtio ABI version, if we increment this, we break the guest driver. */
|
||||||
|
#define VIRTIO_PCI_ABI_VERSION 0
|
||||||
|
|
||||||
|
/* 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
|
||||||
|
|
||||||
|
/* 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)
|
||||||
|
|
||||||
|
/* PCI bindings. */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PCIDevice pci_dev;
|
||||||
|
VirtIODevice *vdev;
|
||||||
|
uint32_t addr;
|
||||||
|
|
||||||
|
uint16_t vendor;
|
||||||
|
uint16_t device;
|
||||||
|
uint16_t subvendor;
|
||||||
|
uint16_t class_code;
|
||||||
|
uint8_t pif;
|
||||||
|
} VirtIOPCIProxy;
|
||||||
|
|
||||||
|
/* virtio device */
|
||||||
|
|
||||||
|
static void virtio_pci_update_irq(void *opaque)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
|
||||||
|
qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
VirtIODevice *vdev = proxy->vdev;
|
||||||
|
target_phys_addr_t pa;
|
||||||
|
|
||||||
|
addr -= proxy->addr;
|
||||||
|
|
||||||
|
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)
|
||||||
|
val = vdev->bad_features(vdev);
|
||||||
|
else
|
||||||
|
val = 0;
|
||||||
|
}
|
||||||
|
if (vdev->set_features)
|
||||||
|
vdev->set_features(vdev, val);
|
||||||
|
vdev->features = val;
|
||||||
|
break;
|
||||||
|
case VIRTIO_PCI_QUEUE_PFN:
|
||||||
|
pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
|
||||||
|
virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
|
||||||
|
break;
|
||||||
|
case VIRTIO_PCI_QUEUE_SEL:
|
||||||
|
if (val < VIRTIO_PCI_QUEUE_MAX)
|
||||||
|
vdev->queue_sel = val;
|
||||||
|
break;
|
||||||
|
case VIRTIO_PCI_QUEUE_NOTIFY:
|
||||||
|
virtio_queue_notify(vdev, val);
|
||||||
|
break;
|
||||||
|
case VIRTIO_PCI_STATUS:
|
||||||
|
vdev->status = val & 0xFF;
|
||||||
|
if (vdev->status == 0)
|
||||||
|
virtio_reset(vdev);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t virtio_ioport_read(void *opaque, uint32_t addr)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
VirtIODevice *vdev = proxy->vdev;
|
||||||
|
uint32_t ret = 0xFFFFFFFF;
|
||||||
|
|
||||||
|
addr -= proxy->addr;
|
||||||
|
|
||||||
|
switch (addr) {
|
||||||
|
case VIRTIO_PCI_HOST_FEATURES:
|
||||||
|
ret = vdev->get_features(vdev);
|
||||||
|
ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY) | (1 << VIRTIO_F_BAD_FEATURE);
|
||||||
|
break;
|
||||||
|
case VIRTIO_PCI_GUEST_FEATURES:
|
||||||
|
ret = vdev->features;
|
||||||
|
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;
|
||||||
|
virtio_update_irq(vdev);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
addr -= proxy->addr + VIRTIO_PCI_CONFIG;
|
||||||
|
return virtio_config_readb(proxy->vdev, addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
addr -= proxy->addr + VIRTIO_PCI_CONFIG;
|
||||||
|
return virtio_config_readw(proxy->vdev, addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
addr -= proxy->addr + VIRTIO_PCI_CONFIG;
|
||||||
|
return virtio_config_readl(proxy->vdev, addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
addr -= proxy->addr + VIRTIO_PCI_CONFIG;
|
||||||
|
virtio_config_writeb(proxy->vdev, addr, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
addr -= proxy->addr + VIRTIO_PCI_CONFIG;
|
||||||
|
virtio_config_writew(proxy->vdev, addr, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = opaque;
|
||||||
|
addr -= proxy->addr + VIRTIO_PCI_CONFIG;
|
||||||
|
virtio_config_writel(proxy->vdev, addr, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_map(PCIDevice *pci_dev, int region_num,
|
||||||
|
uint32_t addr, uint32_t size, int type)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
|
||||||
|
VirtIODevice *vdev = proxy->vdev;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
proxy->addr = addr;
|
||||||
|
for (i = 0; i < 3; i++) {
|
||||||
|
register_ioport_write(addr, VIRTIO_PCI_CONFIG, 1 << i,
|
||||||
|
virtio_ioport_write, proxy);
|
||||||
|
register_ioport_read(addr, VIRTIO_PCI_CONFIG, 1 << i,
|
||||||
|
virtio_ioport_read, proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vdev->config_len) {
|
||||||
|
register_ioport_write(addr + VIRTIO_PCI_CONFIG, vdev->config_len, 1,
|
||||||
|
virtio_pci_config_writeb, proxy);
|
||||||
|
register_ioport_write(addr + VIRTIO_PCI_CONFIG, vdev->config_len, 2,
|
||||||
|
virtio_pci_config_writew, proxy);
|
||||||
|
register_ioport_write(addr + VIRTIO_PCI_CONFIG, vdev->config_len, 4,
|
||||||
|
virtio_pci_config_writel, proxy);
|
||||||
|
register_ioport_read(addr + VIRTIO_PCI_CONFIG, vdev->config_len, 1,
|
||||||
|
virtio_pci_config_readb, proxy);
|
||||||
|
register_ioport_read(addr + VIRTIO_PCI_CONFIG, vdev->config_len, 2,
|
||||||
|
virtio_pci_config_readw, proxy);
|
||||||
|
register_ioport_read(addr + VIRTIO_PCI_CONFIG, vdev->config_len, 4,
|
||||||
|
virtio_pci_config_readl, proxy);
|
||||||
|
|
||||||
|
vdev->get_config(vdev, vdev->config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static const VirtIOBindings virtio_pci_bindings = {
|
||||||
|
.update_irq = virtio_pci_update_irq
|
||||||
|
};
|
||||||
|
|
||||||
|
static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
|
||||||
|
uint16_t vendor, uint16_t device,
|
||||||
|
uint16_t class_code, uint8_t pif)
|
||||||
|
{
|
||||||
|
uint8_t *config;
|
||||||
|
uint32_t size;
|
||||||
|
|
||||||
|
proxy->vdev = vdev;
|
||||||
|
|
||||||
|
config = proxy->pci_dev.config;
|
||||||
|
pci_config_set_vendor_id(config, vendor);
|
||||||
|
pci_config_set_device_id(config, device);
|
||||||
|
|
||||||
|
config[0x08] = VIRTIO_PCI_ABI_VERSION;
|
||||||
|
|
||||||
|
config[0x09] = pif;
|
||||||
|
pci_config_set_class(config, class_code);
|
||||||
|
config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
|
||||||
|
|
||||||
|
config[0x2c] = vendor & 0xFF;
|
||||||
|
config[0x2d] = (vendor >> 8) & 0xFF;
|
||||||
|
config[0x2e] = vdev->device_id & 0xFF;
|
||||||
|
config[0x2f] = (vdev->device_id >> 8) & 0xFF;
|
||||||
|
|
||||||
|
config[0x3d] = 1;
|
||||||
|
|
||||||
|
size = 20 + vdev->config_len;
|
||||||
|
if (size & (size-1))
|
||||||
|
size = 1 << qemu_fls(size);
|
||||||
|
|
||||||
|
pci_register_io_region(&proxy->pci_dev, 0, size, PCI_ADDRESS_SPACE_IO,
|
||||||
|
virtio_map);
|
||||||
|
|
||||||
|
virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_blk_init_pci(PCIDevice *pci_dev)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
||||||
|
VirtIODevice *vdev;
|
||||||
|
|
||||||
|
vdev = virtio_blk_init(&pci_dev->qdev);
|
||||||
|
virtio_init_pci(proxy, vdev,
|
||||||
|
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
||||||
|
PCI_DEVICE_ID_VIRTIO_BLOCK,
|
||||||
|
PCI_CLASS_STORAGE_OTHER,
|
||||||
|
0x00);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_console_init_pci(PCIDevice *pci_dev)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
||||||
|
VirtIODevice *vdev;
|
||||||
|
|
||||||
|
vdev = virtio_console_init(&pci_dev->qdev);
|
||||||
|
virtio_init_pci(proxy, vdev,
|
||||||
|
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
||||||
|
PCI_DEVICE_ID_VIRTIO_CONSOLE,
|
||||||
|
PCI_CLASS_DISPLAY_OTHER,
|
||||||
|
0x00);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_net_init_pci(PCIDevice *pci_dev)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
||||||
|
VirtIODevice *vdev;
|
||||||
|
|
||||||
|
vdev = virtio_net_init(&pci_dev->qdev);
|
||||||
|
virtio_init_pci(proxy, vdev,
|
||||||
|
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
||||||
|
PCI_DEVICE_ID_VIRTIO_NET,
|
||||||
|
PCI_CLASS_NETWORK_ETHERNET,
|
||||||
|
0x00);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_balloon_init_pci(PCIDevice *pci_dev)
|
||||||
|
{
|
||||||
|
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
|
||||||
|
VirtIODevice *vdev;
|
||||||
|
|
||||||
|
vdev = virtio_balloon_init(&pci_dev->qdev);
|
||||||
|
virtio_init_pci(proxy, vdev,
|
||||||
|
PCI_VENDOR_ID_REDHAT_QUMRANET,
|
||||||
|
PCI_DEVICE_ID_VIRTIO_BALLOON,
|
||||||
|
PCI_CLASS_MEMORY_RAM,
|
||||||
|
0x00);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void virtio_pci_register_devices(void)
|
||||||
|
{
|
||||||
|
pci_qdev_register("virtio-blk-pci", sizeof(VirtIOPCIProxy),
|
||||||
|
virtio_blk_init_pci);
|
||||||
|
pci_qdev_register("virtio-net-pci", sizeof(VirtIOPCIProxy),
|
||||||
|
virtio_net_init_pci);
|
||||||
|
pci_qdev_register("virtio-console-pci", sizeof(VirtIOPCIProxy),
|
||||||
|
virtio_console_init_pci);
|
||||||
|
pci_qdev_register("virtio-balloon-pci", sizeof(VirtIOPCIProxy),
|
||||||
|
virtio_balloon_init_pci);
|
||||||
|
}
|
||||||
|
|
||||||
|
device_init(virtio_pci_register_devices)
|
272
hw/virtio.c
272
hw/virtio.c
|
@ -16,43 +16,6 @@
|
||||||
#include "virtio.h"
|
#include "virtio.h"
|
||||||
#include "sysemu.h"
|
#include "sysemu.h"
|
||||||
|
|
||||||
/* 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
|
|
||||||
|
|
||||||
#define VIRTIO_PCI_CONFIG 20
|
|
||||||
|
|
||||||
/* Virtio ABI version, if we increment this, we break the guest driver. */
|
|
||||||
#define VIRTIO_PCI_ABI_VERSION 0
|
|
||||||
|
|
||||||
/* 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
|
|
||||||
|
|
||||||
/* The alignment to use between consumer and producer parts of vring.
|
/* The alignment to use between consumer and producer parts of vring.
|
||||||
* x86 pagesize again. */
|
* x86 pagesize again. */
|
||||||
#define VIRTIO_PCI_VRING_ALIGN 4096
|
#define VIRTIO_PCI_VRING_ALIGN 4096
|
||||||
|
@ -102,7 +65,7 @@ typedef struct VRing
|
||||||
struct VirtQueue
|
struct VirtQueue
|
||||||
{
|
{
|
||||||
VRing vring;
|
VRing vring;
|
||||||
uint32_t pfn;
|
target_phys_addr_t pa;
|
||||||
uint16_t last_avail_idx;
|
uint16_t last_avail_idx;
|
||||||
int inuse;
|
int inuse;
|
||||||
void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
|
void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq);
|
||||||
|
@ -111,8 +74,10 @@ struct VirtQueue
|
||||||
#define VIRTIO_PCI_QUEUE_MAX 16
|
#define VIRTIO_PCI_QUEUE_MAX 16
|
||||||
|
|
||||||
/* virt queue functions */
|
/* virt queue functions */
|
||||||
static void virtqueue_init(VirtQueue *vq, target_phys_addr_t pa)
|
static void virtqueue_init(VirtQueue *vq)
|
||||||
{
|
{
|
||||||
|
target_phys_addr_t pa = vq->pa;
|
||||||
|
|
||||||
vq->vring.desc = pa;
|
vq->vring.desc = pa;
|
||||||
vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
|
vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc);
|
||||||
vq->vring.used = vring_align(vq->vring.avail +
|
vq->vring.used = vring_align(vq->vring.avail +
|
||||||
|
@ -409,17 +374,14 @@ int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem)
|
||||||
|
|
||||||
/* virtio device */
|
/* virtio device */
|
||||||
|
|
||||||
static VirtIODevice *to_virtio_device(PCIDevice *pci_dev)
|
void virtio_update_irq(VirtIODevice *vdev)
|
||||||
{
|
{
|
||||||
return (VirtIODevice *)pci_dev;
|
if (vdev->binding->update_irq) {
|
||||||
|
vdev->binding->update_irq(vdev->binding_opaque);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_update_irq(VirtIODevice *vdev)
|
void virtio_reset(void *opaque)
|
||||||
{
|
|
||||||
qemu_set_irq(vdev->pci_dev.irq[0], vdev->isr & 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void virtio_reset(void *opaque)
|
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = opaque;
|
VirtIODevice *vdev = opaque;
|
||||||
int i;
|
int i;
|
||||||
|
@ -438,103 +400,16 @@ static void virtio_reset(void *opaque)
|
||||||
vdev->vq[i].vring.avail = 0;
|
vdev->vq[i].vring.avail = 0;
|
||||||
vdev->vq[i].vring.used = 0;
|
vdev->vq[i].vring.used = 0;
|
||||||
vdev->vq[i].last_avail_idx = 0;
|
vdev->vq[i].last_avail_idx = 0;
|
||||||
vdev->vq[i].pfn = 0;
|
vdev->vq[i].pa = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
|
uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = to_virtio_device(opaque);
|
|
||||||
ram_addr_t pa;
|
|
||||||
|
|
||||||
addr -= vdev->addr;
|
|
||||||
|
|
||||||
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)
|
|
||||||
val = vdev->bad_features(vdev);
|
|
||||||
else
|
|
||||||
val = 0;
|
|
||||||
}
|
|
||||||
if (vdev->set_features)
|
|
||||||
vdev->set_features(vdev, val);
|
|
||||||
vdev->features = val;
|
|
||||||
break;
|
|
||||||
case VIRTIO_PCI_QUEUE_PFN:
|
|
||||||
pa = (ram_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
|
|
||||||
vdev->vq[vdev->queue_sel].pfn = val;
|
|
||||||
if (pa == 0) {
|
|
||||||
virtio_reset(vdev);
|
|
||||||
} else {
|
|
||||||
virtqueue_init(&vdev->vq[vdev->queue_sel], pa);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case VIRTIO_PCI_QUEUE_SEL:
|
|
||||||
if (val < VIRTIO_PCI_QUEUE_MAX)
|
|
||||||
vdev->queue_sel = val;
|
|
||||||
break;
|
|
||||||
case VIRTIO_PCI_QUEUE_NOTIFY:
|
|
||||||
if (val < VIRTIO_PCI_QUEUE_MAX && vdev->vq[val].vring.desc)
|
|
||||||
vdev->vq[val].handle_output(vdev, &vdev->vq[val]);
|
|
||||||
break;
|
|
||||||
case VIRTIO_PCI_STATUS:
|
|
||||||
vdev->status = val & 0xFF;
|
|
||||||
if (vdev->status == 0)
|
|
||||||
virtio_reset(vdev);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32_t virtio_ioport_read(void *opaque, uint32_t addr)
|
|
||||||
{
|
|
||||||
VirtIODevice *vdev = to_virtio_device(opaque);
|
|
||||||
uint32_t ret = 0xFFFFFFFF;
|
|
||||||
|
|
||||||
addr -= vdev->addr;
|
|
||||||
|
|
||||||
switch (addr) {
|
|
||||||
case VIRTIO_PCI_HOST_FEATURES:
|
|
||||||
ret = vdev->get_features(vdev);
|
|
||||||
ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY) | (1 << VIRTIO_F_BAD_FEATURE);
|
|
||||||
break;
|
|
||||||
case VIRTIO_PCI_GUEST_FEATURES:
|
|
||||||
ret = vdev->features;
|
|
||||||
break;
|
|
||||||
case VIRTIO_PCI_QUEUE_PFN:
|
|
||||||
ret = vdev->vq[vdev->queue_sel].pfn;
|
|
||||||
break;
|
|
||||||
case VIRTIO_PCI_QUEUE_NUM:
|
|
||||||
ret = vdev->vq[vdev->queue_sel].vring.num;
|
|
||||||
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;
|
|
||||||
virtio_update_irq(vdev);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32_t virtio_config_readb(void *opaque, uint32_t addr)
|
|
||||||
{
|
|
||||||
VirtIODevice *vdev = opaque;
|
|
||||||
uint8_t val;
|
uint8_t val;
|
||||||
|
|
||||||
vdev->get_config(vdev, vdev->config);
|
vdev->get_config(vdev, vdev->config);
|
||||||
|
|
||||||
addr -= vdev->addr + VIRTIO_PCI_CONFIG;
|
|
||||||
if (addr > (vdev->config_len - sizeof(val)))
|
if (addr > (vdev->config_len - sizeof(val)))
|
||||||
return (uint32_t)-1;
|
return (uint32_t)-1;
|
||||||
|
|
||||||
|
@ -542,14 +417,12 @@ static uint32_t virtio_config_readb(void *opaque, uint32_t addr)
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t virtio_config_readw(void *opaque, uint32_t addr)
|
uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = opaque;
|
|
||||||
uint16_t val;
|
uint16_t val;
|
||||||
|
|
||||||
vdev->get_config(vdev, vdev->config);
|
vdev->get_config(vdev, vdev->config);
|
||||||
|
|
||||||
addr -= vdev->addr + VIRTIO_PCI_CONFIG;
|
|
||||||
if (addr > (vdev->config_len - sizeof(val)))
|
if (addr > (vdev->config_len - sizeof(val)))
|
||||||
return (uint32_t)-1;
|
return (uint32_t)-1;
|
||||||
|
|
||||||
|
@ -557,14 +430,12 @@ static uint32_t virtio_config_readw(void *opaque, uint32_t addr)
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t virtio_config_readl(void *opaque, uint32_t addr)
|
uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = opaque;
|
|
||||||
uint32_t val;
|
uint32_t val;
|
||||||
|
|
||||||
vdev->get_config(vdev, vdev->config);
|
vdev->get_config(vdev, vdev->config);
|
||||||
|
|
||||||
addr -= vdev->addr + VIRTIO_PCI_CONFIG;
|
|
||||||
if (addr > (vdev->config_len - sizeof(val)))
|
if (addr > (vdev->config_len - sizeof(val)))
|
||||||
return (uint32_t)-1;
|
return (uint32_t)-1;
|
||||||
|
|
||||||
|
@ -572,12 +443,10 @@ static uint32_t virtio_config_readl(void *opaque, uint32_t addr)
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_config_writeb(void *opaque, uint32_t addr, uint32_t data)
|
void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = opaque;
|
|
||||||
uint8_t val = data;
|
uint8_t val = data;
|
||||||
|
|
||||||
addr -= vdev->addr + VIRTIO_PCI_CONFIG;
|
|
||||||
if (addr > (vdev->config_len - sizeof(val)))
|
if (addr > (vdev->config_len - sizeof(val)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -587,12 +456,10 @@ static void virtio_config_writeb(void *opaque, uint32_t addr, uint32_t data)
|
||||||
vdev->set_config(vdev, vdev->config);
|
vdev->set_config(vdev, vdev->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_config_writew(void *opaque, uint32_t addr, uint32_t data)
|
void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = opaque;
|
|
||||||
uint16_t val = data;
|
uint16_t val = data;
|
||||||
|
|
||||||
addr -= vdev->addr + VIRTIO_PCI_CONFIG;
|
|
||||||
if (addr > (vdev->config_len - sizeof(val)))
|
if (addr > (vdev->config_len - sizeof(val)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -602,12 +469,10 @@ static void virtio_config_writew(void *opaque, uint32_t addr, uint32_t data)
|
||||||
vdev->set_config(vdev, vdev->config);
|
vdev->set_config(vdev, vdev->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_config_writel(void *opaque, uint32_t addr, uint32_t data)
|
void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = opaque;
|
|
||||||
uint32_t val = data;
|
uint32_t val = data;
|
||||||
|
|
||||||
addr -= vdev->addr + VIRTIO_PCI_CONFIG;
|
|
||||||
if (addr > (vdev->config_len - sizeof(val)))
|
if (addr > (vdev->config_len - sizeof(val)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -617,33 +482,30 @@ static void virtio_config_writel(void *opaque, uint32_t addr, uint32_t data)
|
||||||
vdev->set_config(vdev, vdev->config);
|
vdev->set_config(vdev, vdev->config);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void virtio_map(PCIDevice *pci_dev, int region_num,
|
void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr)
|
||||||
uint32_t addr, uint32_t size, int type)
|
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev = to_virtio_device(pci_dev);
|
if (addr == 0) {
|
||||||
int i;
|
virtio_reset(vdev);
|
||||||
|
} else {
|
||||||
vdev->addr = addr;
|
vdev->vq[n].pa = addr;
|
||||||
for (i = 0; i < 3; i++) {
|
virtqueue_init(&vdev->vq[n]);
|
||||||
register_ioport_write(addr, 20, 1 << i, virtio_ioport_write, vdev);
|
|
||||||
register_ioport_read(addr, 20, 1 << i, virtio_ioport_read, vdev);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (vdev->config_len) {
|
target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n)
|
||||||
register_ioport_write(addr + 20, vdev->config_len, 1,
|
{
|
||||||
virtio_config_writeb, vdev);
|
return vdev->vq[n].pa;
|
||||||
register_ioport_write(addr + 20, vdev->config_len, 2,
|
}
|
||||||
virtio_config_writew, vdev);
|
|
||||||
register_ioport_write(addr + 20, vdev->config_len, 4,
|
|
||||||
virtio_config_writel, vdev);
|
|
||||||
register_ioport_read(addr + 20, vdev->config_len, 1,
|
|
||||||
virtio_config_readb, vdev);
|
|
||||||
register_ioport_read(addr + 20, vdev->config_len, 2,
|
|
||||||
virtio_config_readw, vdev);
|
|
||||||
register_ioport_read(addr + 20, vdev->config_len, 4,
|
|
||||||
virtio_config_readl, vdev);
|
|
||||||
|
|
||||||
vdev->get_config(vdev, vdev->config);
|
int virtio_queue_get_num(VirtIODevice *vdev, int n)
|
||||||
|
{
|
||||||
|
return vdev->vq[n].vring.num;
|
||||||
|
}
|
||||||
|
|
||||||
|
void virtio_queue_notify(VirtIODevice *vdev, int n)
|
||||||
|
{
|
||||||
|
if (n < VIRTIO_PCI_QUEUE_MAX && vdev->vq[n].vring.desc) {
|
||||||
|
vdev->vq[n].handle_output(vdev, &vdev->vq[n]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -691,9 +553,9 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
pci_device_save(&vdev->pci_dev, f);
|
/* FIXME: load/save binding. */
|
||||||
|
//pci_device_save(&vdev->pci_dev, f);
|
||||||
|
|
||||||
qemu_put_be32s(f, &vdev->addr);
|
|
||||||
qemu_put_8s(f, &vdev->status);
|
qemu_put_8s(f, &vdev->status);
|
||||||
qemu_put_8s(f, &vdev->isr);
|
qemu_put_8s(f, &vdev->isr);
|
||||||
qemu_put_be16s(f, &vdev->queue_sel);
|
qemu_put_be16s(f, &vdev->queue_sel);
|
||||||
|
@ -713,7 +575,7 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
qemu_put_be32(f, vdev->vq[i].vring.num);
|
qemu_put_be32(f, vdev->vq[i].vring.num);
|
||||||
qemu_put_be32s(f, &vdev->vq[i].pfn);
|
qemu_put_be64(f, vdev->vq[i].pa);
|
||||||
qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
|
qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -722,9 +584,9 @@ void virtio_load(VirtIODevice *vdev, QEMUFile *f)
|
||||||
{
|
{
|
||||||
int num, i;
|
int num, i;
|
||||||
|
|
||||||
pci_device_load(&vdev->pci_dev, f);
|
/* FIXME: load/save binding. */
|
||||||
|
//pci_device_load(&vdev->pci_dev, f);
|
||||||
|
|
||||||
qemu_get_be32s(f, &vdev->addr);
|
|
||||||
qemu_get_8s(f, &vdev->status);
|
qemu_get_8s(f, &vdev->status);
|
||||||
qemu_get_8s(f, &vdev->isr);
|
qemu_get_8s(f, &vdev->isr);
|
||||||
qemu_get_be16s(f, &vdev->queue_sel);
|
qemu_get_be16s(f, &vdev->queue_sel);
|
||||||
|
@ -736,14 +598,11 @@ void virtio_load(VirtIODevice *vdev, QEMUFile *f)
|
||||||
|
|
||||||
for (i = 0; i < num; i++) {
|
for (i = 0; i < num; i++) {
|
||||||
vdev->vq[i].vring.num = qemu_get_be32(f);
|
vdev->vq[i].vring.num = qemu_get_be32(f);
|
||||||
qemu_get_be32s(f, &vdev->vq[i].pfn);
|
vdev->vq[i].pa = qemu_get_be64(f);
|
||||||
qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
|
qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
|
||||||
|
|
||||||
if (vdev->vq[i].pfn) {
|
if (vdev->vq[i].pa) {
|
||||||
target_phys_addr_t pa;
|
virtqueue_init(&vdev->vq[i]);
|
||||||
|
|
||||||
pa = (ram_addr_t)vdev->vq[i].pfn << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
|
|
||||||
virtqueue_init(&vdev->vq[i], pa);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -757,40 +616,19 @@ void virtio_cleanup(VirtIODevice *vdev)
|
||||||
qemu_free(vdev->vq);
|
qemu_free(vdev->vq);
|
||||||
}
|
}
|
||||||
|
|
||||||
VirtIODevice *virtio_init_pci(PCIDevice *pci_dev, const char *name,
|
VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
|
||||||
uint16_t vendor, uint16_t device,
|
size_t config_size, size_t struct_size)
|
||||||
uint16_t subvendor, uint16_t subdevice,
|
|
||||||
uint16_t class_code, uint8_t pif,
|
|
||||||
size_t config_size)
|
|
||||||
{
|
{
|
||||||
VirtIODevice *vdev;
|
VirtIODevice *vdev;
|
||||||
uint8_t *config;
|
|
||||||
uint32_t size;
|
|
||||||
|
|
||||||
vdev = to_virtio_device(pci_dev);
|
vdev = qemu_mallocz(struct_size);
|
||||||
|
|
||||||
|
vdev->device_id = device_id;
|
||||||
vdev->status = 0;
|
vdev->status = 0;
|
||||||
vdev->isr = 0;
|
vdev->isr = 0;
|
||||||
vdev->queue_sel = 0;
|
vdev->queue_sel = 0;
|
||||||
vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
|
vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
|
||||||
|
|
||||||
config = pci_dev->config;
|
|
||||||
pci_config_set_vendor_id(config, vendor);
|
|
||||||
pci_config_set_device_id(config, device);
|
|
||||||
|
|
||||||
config[0x08] = VIRTIO_PCI_ABI_VERSION;
|
|
||||||
|
|
||||||
config[0x09] = pif;
|
|
||||||
pci_config_set_class(config, class_code);
|
|
||||||
config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
|
|
||||||
|
|
||||||
config[0x2c] = subvendor & 0xFF;
|
|
||||||
config[0x2d] = (subvendor >> 8) & 0xFF;
|
|
||||||
config[0x2e] = subdevice & 0xFF;
|
|
||||||
config[0x2f] = (subdevice >> 8) & 0xFF;
|
|
||||||
|
|
||||||
config[0x3d] = 1;
|
|
||||||
|
|
||||||
vdev->name = name;
|
vdev->name = name;
|
||||||
vdev->config_len = config_size;
|
vdev->config_len = config_size;
|
||||||
if (vdev->config_len)
|
if (vdev->config_len)
|
||||||
|
@ -798,13 +636,13 @@ VirtIODevice *virtio_init_pci(PCIDevice *pci_dev, const char *name,
|
||||||
else
|
else
|
||||||
vdev->config = NULL;
|
vdev->config = NULL;
|
||||||
|
|
||||||
size = 20 + config_size;
|
|
||||||
if (size & (size-1))
|
|
||||||
size = 1 << qemu_fls(size);
|
|
||||||
|
|
||||||
pci_register_io_region(pci_dev, 0, size, PCI_ADDRESS_SPACE_IO,
|
|
||||||
virtio_map);
|
|
||||||
qemu_register_reset(virtio_reset, vdev);
|
qemu_register_reset(virtio_reset, vdev);
|
||||||
|
|
||||||
return vdev;
|
return vdev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
|
||||||
|
void *opaque)
|
||||||
|
{
|
||||||
|
vdev->binding = binding;
|
||||||
|
vdev->binding_opaque = opaque;
|
||||||
|
}
|
||||||
|
|
43
hw/virtio.h
43
hw/virtio.h
|
@ -15,7 +15,7 @@
|
||||||
#define _QEMU_VIRTIO_H
|
#define _QEMU_VIRTIO_H
|
||||||
|
|
||||||
#include "hw.h"
|
#include "hw.h"
|
||||||
#include "pci.h"
|
#include "qdev.h"
|
||||||
|
|
||||||
/* from Linux's linux/virtio_config.h */
|
/* from Linux's linux/virtio_config.h */
|
||||||
|
|
||||||
|
@ -70,13 +70,15 @@ typedef struct VirtQueueElement
|
||||||
struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
|
struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
|
||||||
} VirtQueueElement;
|
} VirtQueueElement;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void (*update_irq)(void * opaque);
|
||||||
|
} VirtIOBindings;
|
||||||
|
|
||||||
#define VIRTIO_PCI_QUEUE_MAX 16
|
#define VIRTIO_PCI_QUEUE_MAX 16
|
||||||
|
|
||||||
struct VirtIODevice
|
struct VirtIODevice
|
||||||
{
|
{
|
||||||
PCIDevice pci_dev;
|
|
||||||
const char *name;
|
const char *name;
|
||||||
uint32_t addr;
|
|
||||||
uint8_t status;
|
uint8_t status;
|
||||||
uint8_t isr;
|
uint8_t isr;
|
||||||
uint16_t queue_sel;
|
uint16_t queue_sel;
|
||||||
|
@ -90,14 +92,11 @@ struct VirtIODevice
|
||||||
void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
|
void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
|
||||||
void (*reset)(VirtIODevice *vdev);
|
void (*reset)(VirtIODevice *vdev);
|
||||||
VirtQueue *vq;
|
VirtQueue *vq;
|
||||||
|
const VirtIOBindings *binding;
|
||||||
|
void *binding_opaque;
|
||||||
|
uint16_t device_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
VirtIODevice *virtio_init_pci(PCIDevice *pci_dev, const char *name,
|
|
||||||
uint16_t vendor, uint16_t device,
|
|
||||||
uint16_t subvendor, uint16_t subdevice,
|
|
||||||
uint16_t class_code, uint8_t pif,
|
|
||||||
size_t config_size);
|
|
||||||
|
|
||||||
VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
|
VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
|
||||||
void (*handle_output)(VirtIODevice *,
|
void (*handle_output)(VirtIODevice *,
|
||||||
VirtQueue *));
|
VirtQueue *));
|
||||||
|
@ -127,4 +126,30 @@ int virtio_queue_ready(VirtQueue *vq);
|
||||||
|
|
||||||
int virtio_queue_empty(VirtQueue *vq);
|
int virtio_queue_empty(VirtQueue *vq);
|
||||||
|
|
||||||
|
/* Host binding interface. */
|
||||||
|
|
||||||
|
VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
|
||||||
|
size_t config_size, size_t struct_size);
|
||||||
|
uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr);
|
||||||
|
uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr);
|
||||||
|
uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr);
|
||||||
|
void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data);
|
||||||
|
void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data);
|
||||||
|
void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data);
|
||||||
|
void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr);
|
||||||
|
target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n);
|
||||||
|
int virtio_queue_get_num(VirtIODevice *vdev, int n);
|
||||||
|
void virtio_queue_notify(VirtIODevice *vdev, int n);
|
||||||
|
void virtio_reset(void *opaque);
|
||||||
|
void virtio_update_irq(VirtIODevice *vdev);
|
||||||
|
|
||||||
|
void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
|
||||||
|
void *opaque);
|
||||||
|
|
||||||
|
/* Base devices. */
|
||||||
|
VirtIODevice *virtio_blk_init(DeviceState *dev);
|
||||||
|
VirtIODevice *virtio_net_init(DeviceState *dev);
|
||||||
|
VirtIODevice *virtio_console_init(DeviceState *dev);
|
||||||
|
VirtIODevice *virtio_balloon_init(DeviceState *dev);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Reference in New Issue