diff options
Diffstat (limited to 'hw')
-rw-r--r-- | hw/pc.c | 6 | ||||
-rw-r--r-- | hw/pci-hotplug.c | 2 | ||||
-rw-r--r-- | hw/pci.c | 2 | ||||
-rw-r--r-- | hw/ppc440_bamboo.c | 4 | ||||
-rw-r--r-- | hw/ppce500_mpc8544ds.c | 2 | ||||
-rw-r--r-- | hw/virtio-balloon.c | 20 | ||||
-rw-r--r-- | hw/virtio-blk.c | 23 | ||||
-rw-r--r-- | hw/virtio-blk.h | 1 | ||||
-rw-r--r-- | hw/virtio-console.c | 22 | ||||
-rw-r--r-- | hw/virtio-net.c | 23 | ||||
-rw-r--r-- | hw/virtio-pci.c | 349 | ||||
-rw-r--r-- | hw/virtio.c | 274 | ||||
-rw-r--r-- | hw/virtio.h | 43 |
13 files changed, 472 insertions, 299 deletions
@@ -1137,21 +1137,21 @@ static void pc_init1(ram_addr_t ram_size, int unit_id = 0; 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++; } } /* Add virtio balloon device */ if (pci_enabled) { - pci_create_simple(pci_bus, -1, "virtio-balloon"); + pci_create_simple(pci_bus, -1, "virtio-balloon-pci"); } /* Add virtio console devices */ if (pci_enabled) { for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { if (virtcon_hds[i]) { - pci_create_simple(pci_bus, -1, "virtio-console"); + pci_create_simple(pci_bus, -1, "virtio-console-pci"); } } } diff --git a/hw/pci-hotplug.c b/hw/pci-hotplug.c index acda2bb902..4d18ea2fca 100644 --- a/hw/pci-hotplug.c +++ b/hw/pci-hotplug.c @@ -120,7 +120,7 @@ static PCIDevice *qemu_pci_hot_add_storage(Monitor *mon, PCIBus *pci_bus, opaque = pci_create_simple(pci_bus, -1, "lsi53c895a"); break; 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); break; } @@ -806,7 +806,7 @@ static const char * const pci_nic_names[] = { "rtl8139", "e1000", "pcnet", - "virtio_net", + "virtio-net-pci", NULL }; diff --git a/hw/ppc440_bamboo.c b/hw/ppc440_bamboo.c index 606fc32194..aaf0a48a5f 100644 --- a/hw/ppc440_bamboo.c +++ b/hw/ppc440_bamboo.c @@ -111,14 +111,14 @@ static void bamboo_init(ram_addr_t ram_size, /* Add virtio block devices. */ 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++; } /* Add virtio console devices */ for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { if (virtcon_hds[i]) { - pci_create_simple(pcibus, -1, "virtio-console"); + pci_create_simple(pcibus, -1, "virtio-console-pci"); } } diff --git a/hw/ppce500_mpc8544ds.c b/hw/ppce500_mpc8544ds.c index a5de61ce5f..b6303157be 100644 --- a/hw/ppce500_mpc8544ds.c +++ b/hw/ppce500_mpc8544ds.c @@ -220,7 +220,7 @@ static void mpc8544ds_init(ram_addr_t ram_size, /* Add virtio block devices. */ 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++; } diff --git a/hw/virtio-balloon.c b/hw/virtio-balloon.c index 7fa7e47f7f..cfd3b413ff 100644 --- a/hw/virtio-balloon.c +++ b/hw/virtio-balloon.c @@ -169,17 +169,13 @@ static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id) return 0; } -static void virtio_balloon_init(PCIDevice *pci_dev) +VirtIODevice *virtio_balloon_init(DeviceState *dev) { VirtIOBalloon *s; - s = (VirtIOBalloon *)virtio_init_pci(pci_dev, "virtio-balloon", - PCI_VENDOR_ID_REDHAT_QUMRANET, - PCI_DEVICE_ID_VIRTIO_BALLOON, - PCI_VENDOR_ID_REDHAT_QUMRANET, - VIRTIO_ID_BALLOON, - PCI_CLASS_MEMORY_RAM, 0x00, - 8); + s = (VirtIOBalloon *)virtio_common_init("virtio-balloon", + VIRTIO_ID_BALLOON, + 8, sizeof(VirtIOBalloon)); s->vdev.get_config = virtio_balloon_get_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); register_savevm("virtio-balloon", -1, 1, virtio_balloon_save, virtio_balloon_load, s); -} -static void virtio_balloon_register_devices(void) -{ - pci_qdev_register("virtio-balloon", sizeof(VirtIOBalloon), - virtio_balloon_init); + return &s->vdev; } - -device_init(virtio_balloon_register_devices) diff --git a/hw/virtio-blk.c b/hw/virtio-blk.c index ef091131b8..8dd3c7ac52 100644 --- a/hw/virtio-blk.c +++ b/hw/virtio-blk.c @@ -348,28 +348,24 @@ static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) return 0; } -static void virtio_blk_init(PCIDevice *pci_dev) +VirtIODevice *virtio_blk_init(DeviceState *dev) { VirtIOBlock *s; int cylinders, heads, secs; static int virtio_blk_id; BlockDriverState *bs; - s = (VirtIOBlock *)virtio_init_pci(pci_dev, "virtio-blk", - PCI_VENDOR_ID_REDHAT_QUMRANET, - PCI_DEVICE_ID_VIRTIO_BLOCK, - PCI_VENDOR_ID_REDHAT_QUMRANET, - VIRTIO_ID_BLOCK, - PCI_CLASS_STORAGE_OTHER, 0x00, - sizeof(struct virtio_blk_config)); + s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK, + sizeof(struct virtio_blk_config), + sizeof(VirtIOBlock)); - 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_features = virtio_blk_get_features; s->vdev.reset = virtio_blk_reset; s->bs = bs; s->rq = NULL; - bs->private = &s->vdev.pci_dev; + bs->private = dev; bdrv_guess_geometry(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); register_savevm("virtio-blk", virtio_blk_id++, 2, virtio_blk_save, virtio_blk_load, s); -} -static void virtio_blk_register_devices(void) -{ - pci_qdev_register("virtio-blk", sizeof(VirtIOBlock), virtio_blk_init); + return &s->vdev; } - -device_init(virtio_blk_register_devices) diff --git a/hw/virtio-blk.h b/hw/virtio-blk.h index e3686083a2..dff3e0c979 100644 --- a/hw/virtio-blk.h +++ b/hw/virtio-blk.h @@ -16,7 +16,6 @@ #include "virtio.h" #include "block.h" -#include "pci.h" /* from Linux's linux/virtio_blk.h */ diff --git a/hw/virtio-console.c b/hw/virtio-console.c index 7f3efa0129..04f8873b3b 100644 --- a/hw/virtio-console.c +++ b/hw/virtio-console.c @@ -123,31 +123,21 @@ static int virtio_console_load(QEMUFile *f, void *opaque, int version_id) return 0; } -static void virtio_console_init(PCIDevice *pci_dev) +VirtIODevice *virtio_console_init(DeviceState *dev) { VirtIOConsole *s; - s = (VirtIOConsole *)virtio_init_pci(pci_dev, "virtio-console", - PCI_VENDOR_ID_REDHAT_QUMRANET, - PCI_DEVICE_ID_VIRTIO_CONSOLE, - PCI_VENDOR_ID_REDHAT_QUMRANET, - VIRTIO_ID_CONSOLE, - PCI_CLASS_DISPLAY_OTHER, 0x00, - 0); + s = (VirtIOConsole *)virtio_common_init("virtio-console", + VIRTIO_ID_CONSOLE, + 0, sizeof(VirtIOConsole)); s->vdev.get_features = virtio_console_get_features; 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->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); register_savevm("virtio-console", -1, 1, virtio_console_save, virtio_console_load, s); -} -static void virtio_console_register_devices(void) -{ - pci_qdev_register("virtio-console", sizeof(VirtIOConsole), - virtio_console_init); + return &s->vdev; } - -device_init(virtio_console_register_devices) diff --git a/hw/virtio-net.c b/hw/virtio-net.c index 3764170437..60aa6dab1b 100644 --- a/hw/virtio-net.c +++ b/hw/virtio-net.c @@ -585,18 +585,14 @@ static void virtio_net_cleanup(VLANClientState *vc) virtio_cleanup(&n->vdev); } -static void virtio_net_init(PCIDevice *pci_dev) +VirtIODevice *virtio_net_init(DeviceState *dev) { VirtIONet *n; static int virtio_net_id; - n = (VirtIONet *)virtio_init_pci(pci_dev, "virtio-net", - PCI_VENDOR_ID_REDHAT_QUMRANET, - PCI_DEVICE_ID_VIRTIO_NET, - PCI_VENDOR_ID_REDHAT_QUMRANET, - VIRTIO_ID_NET, - PCI_CLASS_NETWORK_ETHERNET, 0x00, - sizeof(struct virtio_net_config)); + n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET, + sizeof(struct virtio_net_config), + sizeof(VirtIONet)); n->vdev.get_config = virtio_net_get_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->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); - qdev_get_macaddr(&pci_dev->qdev, n->mac); + qdev_get_macaddr(dev, n->mac); 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_can_receive, 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, virtio_net_save, virtio_net_load, n); -} -static void virtio_net_register_devices(void) -{ - pci_qdev_register("virtio_net", sizeof(VirtIONet), virtio_net_init); + return &n->vdev; } - -device_init(virtio_net_register_devices) diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c new file mode 100644 index 0000000000..ebc84cc2fe --- /dev/null +++ b/hw/virtio-pci.c @@ -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) diff --git a/hw/virtio.c b/hw/virtio.c index 21108c5c2b..463d74b394 100644 --- a/hw/virtio.c +++ b/hw/virtio.c @@ -16,43 +16,6 @@ #include "virtio.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. * x86 pagesize again. */ #define VIRTIO_PCI_VRING_ALIGN 4096 @@ -102,7 +65,7 @@ typedef struct VRing struct VirtQueue { VRing vring; - uint32_t pfn; + target_phys_addr_t pa; uint16_t last_avail_idx; int inuse; void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq); @@ -111,8 +74,10 @@ struct VirtQueue #define VIRTIO_PCI_QUEUE_MAX 16 /* 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.avail = pa + vq->vring.num * sizeof(VRingDesc); vq->vring.used = vring_align(vq->vring.avail + @@ -409,17 +374,14 @@ int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem) /* virtio device */ -static VirtIODevice *to_virtio_device(PCIDevice *pci_dev) +void virtio_update_irq(VirtIODevice *vdev) { - return (VirtIODevice *)pci_dev; -} - -static void virtio_update_irq(VirtIODevice *vdev) -{ - qemu_set_irq(vdev->pci_dev.irq[0], vdev->isr & 1); + if (vdev->binding->update_irq) { + vdev->binding->update_irq(vdev->binding_opaque); + } } -static void virtio_reset(void *opaque) +void virtio_reset(void *opaque) { VirtIODevice *vdev = opaque; int i; @@ -438,103 +400,16 @@ static void virtio_reset(void *opaque) vdev->vq[i].vring.avail = 0; vdev->vq[i].vring.used = 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) -{ - 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) +uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) { - VirtIODevice *vdev = opaque; uint8_t val; vdev->get_config(vdev, vdev->config); - addr -= vdev->addr + VIRTIO_PCI_CONFIG; if (addr > (vdev->config_len - sizeof(val))) return (uint32_t)-1; @@ -542,14 +417,12 @@ static uint32_t virtio_config_readb(void *opaque, uint32_t addr) 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; vdev->get_config(vdev, vdev->config); - addr -= vdev->addr + VIRTIO_PCI_CONFIG; if (addr > (vdev->config_len - sizeof(val))) return (uint32_t)-1; @@ -557,14 +430,12 @@ static uint32_t virtio_config_readw(void *opaque, uint32_t addr) 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; vdev->get_config(vdev, vdev->config); - addr -= vdev->addr + VIRTIO_PCI_CONFIG; if (addr > (vdev->config_len - sizeof(val))) return (uint32_t)-1; @@ -572,12 +443,10 @@ static uint32_t virtio_config_readl(void *opaque, uint32_t addr) 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; - addr -= vdev->addr + VIRTIO_PCI_CONFIG; if (addr > (vdev->config_len - sizeof(val))) return; @@ -587,12 +456,10 @@ static void virtio_config_writeb(void *opaque, uint32_t addr, uint32_t data) 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; - addr -= vdev->addr + VIRTIO_PCI_CONFIG; if (addr > (vdev->config_len - sizeof(val))) return; @@ -602,12 +469,10 @@ static void virtio_config_writew(void *opaque, uint32_t addr, uint32_t data) 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; - addr -= vdev->addr + VIRTIO_PCI_CONFIG; if (addr > (vdev->config_len - sizeof(val))) return; @@ -617,33 +482,30 @@ static void virtio_config_writel(void *opaque, uint32_t addr, uint32_t data) vdev->set_config(vdev, vdev->config); } -static void virtio_map(PCIDevice *pci_dev, int region_num, - uint32_t addr, uint32_t size, int type) +void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr) { - VirtIODevice *vdev = to_virtio_device(pci_dev); - int i; - - vdev->addr = addr; - for (i = 0; i < 3; i++) { - register_ioport_write(addr, 20, 1 << i, virtio_ioport_write, vdev); - register_ioport_read(addr, 20, 1 << i, virtio_ioport_read, vdev); + if (addr == 0) { + virtio_reset(vdev); + } else { + vdev->vq[n].pa = addr; + virtqueue_init(&vdev->vq[n]); } +} + +target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n) +{ + return vdev->vq[n].pa; +} + +int virtio_queue_get_num(VirtIODevice *vdev, int n) +{ + return vdev->vq[n].vring.num; +} - if (vdev->config_len) { - register_ioport_write(addr + 20, vdev->config_len, 1, - virtio_config_writeb, vdev); - 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); +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; - 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->isr); qemu_put_be16s(f, &vdev->queue_sel); @@ -713,7 +575,7 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f) break; 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); } } @@ -722,9 +584,9 @@ void virtio_load(VirtIODevice *vdev, QEMUFile *f) { 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->isr); qemu_get_be16s(f, &vdev->queue_sel); @@ -736,14 +598,11 @@ void virtio_load(VirtIODevice *vdev, QEMUFile *f) for (i = 0; i < num; i++) { 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); - if (vdev->vq[i].pfn) { - target_phys_addr_t pa; - - pa = (ram_addr_t)vdev->vq[i].pfn << VIRTIO_PCI_QUEUE_ADDR_SHIFT; - virtqueue_init(&vdev->vq[i], pa); + if (vdev->vq[i].pa) { + virtqueue_init(&vdev->vq[i]); } } @@ -757,40 +616,19 @@ void virtio_cleanup(VirtIODevice *vdev) qemu_free(vdev->vq); } -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) +VirtIODevice *virtio_common_init(const char *name, uint16_t device_id, + size_t config_size, size_t struct_size) { 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->isr = 0; vdev->queue_sel = 0; 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->config_len = config_size; if (vdev->config_len) @@ -798,13 +636,13 @@ VirtIODevice *virtio_init_pci(PCIDevice *pci_dev, const char *name, else 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); - return vdev; } + +void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding, + void *opaque) +{ + vdev->binding = binding; + vdev->binding_opaque = opaque; +} diff --git a/hw/virtio.h b/hw/virtio.h index 99d8d72a03..425727e559 100644 --- a/hw/virtio.h +++ b/hw/virtio.h @@ -15,7 +15,7 @@ #define _QEMU_VIRTIO_H #include "hw.h" -#include "pci.h" +#include "qdev.h" /* from Linux's linux/virtio_config.h */ @@ -70,13 +70,15 @@ typedef struct VirtQueueElement struct iovec out_sg[VIRTQUEUE_MAX_SIZE]; } VirtQueueElement; +typedef struct { + void (*update_irq)(void * opaque); +} VirtIOBindings; + #define VIRTIO_PCI_QUEUE_MAX 16 struct VirtIODevice { - PCIDevice pci_dev; const char *name; - uint32_t addr; uint8_t status; uint8_t isr; uint16_t queue_sel; @@ -90,14 +92,11 @@ struct VirtIODevice void (*set_config)(VirtIODevice *vdev, const uint8_t *config); void (*reset)(VirtIODevice *vdev); 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, void (*handle_output)(VirtIODevice *, VirtQueue *)); @@ -127,4 +126,30 @@ int virtio_queue_ready(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 |