aboutsummaryrefslogtreecommitdiff
path: root/hw
diff options
context:
space:
mode:
Diffstat (limited to 'hw')
-rw-r--r--hw/arm/musicpal.c28
-rw-r--r--hw/milkymist-hw.h6
-rw-r--r--hw/milkymist-minimac2.c5
-rw-r--r--hw/milkymist-softusb.c23
-rw-r--r--hw/piix_pci.c30
-rw-r--r--hw/s390x/virtio-ccw.c25
-rw-r--r--hw/s390x/virtio-ccw.h11
-rw-r--r--hw/sysbus.c21
-rw-r--r--hw/sysbus.h5
-rw-r--r--hw/virtio-balloon.c110
-rw-r--r--hw/virtio-balloon.h7
-rw-r--r--hw/virtio-pci.c111
-rw-r--r--hw/virtio-pci.h14
13 files changed, 231 insertions, 165 deletions
diff --git a/hw/arm/musicpal.c b/hw/arm/musicpal.c
index edd528255f..ea8473db3e 100644
--- a/hw/arm/musicpal.c
+++ b/hw/arm/musicpal.c
@@ -1033,6 +1033,15 @@ static const TypeInfo mv88w8618_flashcfg_info = {
#define MP_BOARD_REVISION 0x31
+typedef struct {
+ SysBusDevice parent_obj;
+ MemoryRegion iomem;
+} MusicPalMiscState;
+
+#define TYPE_MUSICPAL_MISC "musicpal-misc"
+#define MUSICPAL_MISC(obj) \
+ OBJECT_CHECK(MusicPalMiscState, (obj), TYPE_MUSICPAL_MISC)
+
static uint64_t musicpal_misc_read(void *opaque, hwaddr offset,
unsigned size)
{
@@ -1056,15 +1065,23 @@ static const MemoryRegionOps musicpal_misc_ops = {
.endianness = DEVICE_NATIVE_ENDIAN,
};
-static void musicpal_misc_init(SysBusDevice *dev)
+static void musicpal_misc_init(Object *obj)
{
- MemoryRegion *iomem = g_new(MemoryRegion, 1);
+ SysBusDevice *sd = SYS_BUS_DEVICE(obj);
+ MusicPalMiscState *s = MUSICPAL_MISC(obj);
- memory_region_init_io(iomem, &musicpal_misc_ops, NULL,
+ memory_region_init_io(&s->iomem, &musicpal_misc_ops, NULL,
"musicpal-misc", MP_MISC_SIZE);
- sysbus_add_memory(dev, MP_MISC_BASE, iomem);
+ sysbus_init_mmio(sd, &s->iomem);
}
+static const TypeInfo musicpal_misc_info = {
+ .name = TYPE_MUSICPAL_MISC,
+ .parent = TYPE_SYS_BUS_DEVICE,
+ .instance_init = musicpal_misc_init,
+ .instance_size = sizeof(MusicPalMiscState),
+};
+
/* WLAN register offsets */
#define MP_WLAN_MAGIC1 0x11c
#define MP_WLAN_MAGIC2 0x124
@@ -1614,7 +1631,7 @@ static void musicpal_init(QEMUMachineInitArgs *args)
sysbus_create_simple("mv88w8618_wlan", MP_WLAN_BASE, NULL);
- musicpal_misc_init(SYS_BUS_DEVICE(dev));
+ sysbus_create_simple(TYPE_MUSICPAL_MISC, MP_MISC_BASE, NULL);
dev = sysbus_create_simple("musicpal_gpio", MP_GPIO_BASE, pic[MP_GPIO_IRQ]);
i2c_dev = sysbus_create_simple("gpio_i2c", -1, NULL);
@@ -1694,6 +1711,7 @@ static void musicpal_register_types(void)
type_register_static(&musicpal_lcd_info);
type_register_static(&musicpal_gpio_info);
type_register_static(&musicpal_key_info);
+ type_register_static(&musicpal_misc_info);
}
type_init(musicpal_register_types)
diff --git a/hw/milkymist-hw.h b/hw/milkymist-hw.h
index c047a70a9f..4e86c4e832 100644
--- a/hw/milkymist-hw.h
+++ b/hw/milkymist-hw.h
@@ -177,10 +177,10 @@ static inline DeviceState *milkymist_minimac2_create(hwaddr base,
qemu_check_nic_model(&nd_table[0], "minimac2");
dev = qdev_create(NULL, "milkymist-minimac2");
- qdev_prop_set_taddr(dev, "buffers_base", buffers_base);
qdev_set_nic_properties(dev, &nd_table[0]);
qdev_init_nofail(dev);
sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+ sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, buffers_base);
sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, rx_irq);
sysbus_connect_irq(SYS_BUS_DEVICE(dev), 1, tx_irq);
@@ -194,12 +194,12 @@ static inline DeviceState *milkymist_softusb_create(hwaddr base,
DeviceState *dev;
dev = qdev_create(NULL, "milkymist-softusb");
- qdev_prop_set_uint32(dev, "pmem_base", pmem_base);
qdev_prop_set_uint32(dev, "pmem_size", pmem_size);
- qdev_prop_set_uint32(dev, "dmem_base", dmem_base);
qdev_prop_set_uint32(dev, "dmem_size", dmem_size);
qdev_init_nofail(dev);
sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+ sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, pmem_base);
+ sysbus_mmio_map(SYS_BUS_DEVICE(dev), 2, dmem_base);
sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq);
return dev;
diff --git a/hw/milkymist-minimac2.c b/hw/milkymist-minimac2.c
index c20ff904ec..29618e8efa 100644
--- a/hw/milkymist-minimac2.c
+++ b/hw/milkymist-minimac2.c
@@ -96,7 +96,6 @@ struct MilkymistMinimac2State {
NICState *nic;
NICConf conf;
char *phy_model;
- hwaddr buffers_base;
MemoryRegion buffers;
MemoryRegion regs_region;
@@ -475,7 +474,7 @@ static int milkymist_minimac2_init(SysBusDevice *dev)
s->rx1_buf = s->rx0_buf + MINIMAC2_BUFFER_SIZE;
s->tx_buf = s->rx1_buf + MINIMAC2_BUFFER_SIZE;
- sysbus_add_memory(dev, s->buffers_base, &s->buffers);
+ sysbus_init_mmio(dev, &s->buffers);
qemu_macaddr_default_if_unset(&s->conf.macaddr);
s->nic = qemu_new_nic(&net_milkymist_minimac2_info, &s->conf,
@@ -517,8 +516,6 @@ static const VMStateDescription vmstate_milkymist_minimac2 = {
};
static Property milkymist_minimac2_properties[] = {
- DEFINE_PROP_TADDR("buffers_base", MilkymistMinimac2State,
- buffers_base, 0),
DEFINE_NIC_PROPERTIES(MilkymistMinimac2State, conf),
DEFINE_PROP_STRING("phy_model", MilkymistMinimac2State, phy_model),
DEFINE_PROP_END_OF_LIST(),
diff --git a/hw/milkymist-softusb.c b/hw/milkymist-softusb.c
index d911686de7..90a0ae5769 100644
--- a/hw/milkymist-softusb.c
+++ b/hw/milkymist-softusb.c
@@ -54,10 +54,11 @@ struct MilkymistSoftUsbState {
MemoryRegion dmem;
qemu_irq irq;
+ void *pmem_ptr;
+ void *dmem_ptr;
+
/* device properties */
- uint32_t pmem_base;
uint32_t pmem_size;
- uint32_t dmem_base;
uint32_t dmem_size;
/* device registers */
@@ -131,10 +132,11 @@ static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
if (offset + len >= s->dmem_size) {
error_report("milkymist_softusb: read dmem out of bounds "
"at offset 0x%x, len %d", offset, len);
+ memset(buf, 0, len);
return;
}
- cpu_physical_memory_read(s->dmem_base + offset, buf, len);
+ memcpy(buf, s->dmem_ptr + offset, len);
}
static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
@@ -146,7 +148,7 @@ static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
return;
}
- cpu_physical_memory_write(s->dmem_base + offset, buf, len);
+ memcpy(s->dmem_ptr + offset, buf, len);
}
static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
@@ -155,10 +157,11 @@ static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
if (offset + len >= s->pmem_size) {
error_report("milkymist_softusb: read pmem out of bounds "
"at offset 0x%x, len %d", offset, len);
+ memset(buf, 0, len);
return;
}
- cpu_physical_memory_read(s->pmem_base + offset, buf, len);
+ memcpy(buf, s->pmem_ptr + offset, len);
}
static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
@@ -170,7 +173,7 @@ static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
return;
}
- cpu_physical_memory_write(s->pmem_base + offset, buf, len);
+ memcpy(s->pmem_ptr + offset, buf, len);
}
static void softusb_mouse_changed(MilkymistSoftUsbState *s)
@@ -270,11 +273,13 @@ static int milkymist_softusb_init(SysBusDevice *dev)
memory_region_init_ram(&s->pmem, "milkymist-softusb.pmem",
s->pmem_size);
vmstate_register_ram_global(&s->pmem);
- sysbus_add_memory(dev, s->pmem_base, &s->pmem);
+ s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
+ sysbus_init_mmio(dev, &s->pmem);
memory_region_init_ram(&s->dmem, "milkymist-softusb.dmem",
s->dmem_size);
vmstate_register_ram_global(&s->dmem);
- sysbus_add_memory(dev, s->dmem_base, &s->dmem);
+ s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
+ sysbus_init_mmio(dev, &s->dmem);
hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
@@ -298,9 +303,7 @@ static const VMStateDescription vmstate_milkymist_softusb = {
};
static Property milkymist_softusb_properties[] = {
- DEFINE_PROP_UINT32("pmem_base", MilkymistSoftUsbState, pmem_base, 0xa0000000),
DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
- DEFINE_PROP_UINT32("dmem_base", MilkymistSoftUsbState, dmem_base, 0xa0020000),
DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
DEFINE_PROP_END_OF_LIST(),
};
diff --git a/hw/piix_pci.c b/hw/piix_pci.c
index ce397797fc..83fcfa497c 100644
--- a/hw/piix_pci.c
+++ b/hw/piix_pci.c
@@ -82,6 +82,10 @@ typedef struct PIIX3State {
MemoryRegion rcr_mem;
} PIIX3State;
+#define TYPE_I440FX_PCI_DEVICE "i440FX"
+#define I440FX_PCI_DEVICE(obj) \
+ OBJECT_CHECK(PCII440FXState, (obj), TYPE_I440FX_PCI_DEVICE)
+
struct PCII440FXState {
PCIDevice dev;
MemoryRegion *system_memory;
@@ -141,7 +145,7 @@ static void i440fx_set_smm(int val, void *arg)
static void i440fx_write_config(PCIDevice *dev,
uint32_t address, uint32_t val, int len)
{
- PCII440FXState *d = DO_UPCAST(PCII440FXState, dev, dev);
+ PCII440FXState *d = I440FX_PCI_DEVICE(dev);
/* XXX: implement SMRAM.D_LOCK */
pci_default_write_config(dev, address, val, len);
@@ -212,7 +216,7 @@ static int i440fx_pcihost_initfn(SysBusDevice *dev)
static int i440fx_initfn(PCIDevice *dev)
{
- PCII440FXState *d = DO_UPCAST(PCII440FXState, dev, dev);
+ PCII440FXState *d = I440FX_PCI_DEVICE(dev);
d->dev.config[I440FX_SMRAM] = 0x02;
@@ -251,7 +255,7 @@ static PCIBus *i440fx_common_init(const char *device_name,
qdev_init_nofail(dev);
d = pci_create_simple(b, 0, device_name);
- *pi440fx_state = DO_UPCAST(PCII440FXState, dev, d);
+ *pi440fx_state = I440FX_PCI_DEVICE(d);
f = *pi440fx_state;
f->system_memory = address_space_mem;
f->pci_address_space = pci_address_space;
@@ -296,8 +300,7 @@ static PCIBus *i440fx_common_init(const char *device_name,
pci_bus_set_route_irq_fn(b, piix3_route_intx_pin_to_irq);
}
piix3->pic = pic;
- *isa_bus = DO_UPCAST(ISABus, qbus,
- qdev_get_child_bus(&piix3->dev.qdev, "isa.0"));
+ *isa_bus = ISA_BUS(qdev_get_child_bus(DEVICE(piix3), "isa.0"));
*piix3_devfn = piix3->dev.devfn;
@@ -325,7 +328,8 @@ PCIBus *i440fx_init(PCII440FXState **pi440fx_state, int *piix3_devfn,
{
PCIBus *b;
- b = i440fx_common_init("i440FX", pi440fx_state, piix3_devfn, isa_bus, pic,
+ b = i440fx_common_init(TYPE_I440FX_PCI_DEVICE, pi440fx_state,
+ piix3_devfn, isa_bus, pic,
address_space_mem, address_space_io, ram_size,
pci_hole_start, pci_hole_size,
pci_hole64_start, pci_hole64_size,
@@ -421,10 +425,10 @@ static void piix3_reset(void *opaque)
PIIX3State *d = opaque;
uint8_t *pci_conf = d->dev.config;
- pci_conf[0x04] = 0x07; // master, memory and I/O
+ pci_conf[0x04] = 0x07; /* master, memory and I/O */
pci_conf[0x05] = 0x00;
pci_conf[0x06] = 0x00;
- pci_conf[0x07] = 0x02; // PCI_status_devsel_medium
+ pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
pci_conf[0x4c] = 0x4d;
pci_conf[0x4e] = 0x03;
pci_conf[0x4f] = 0x00;
@@ -543,7 +547,7 @@ static int piix3_initfn(PCIDevice *dev)
{
PIIX3State *d = DO_UPCAST(PIIX3State, dev, dev);
- isa_bus_new(&d->dev.qdev, pci_address_space_io(dev));
+ isa_bus_new(DEVICE(d), pci_address_space_io(dev));
memory_region_init_io(&d->rcr_mem, &rcr_ops, d, "piix3-reset-control", 1);
memory_region_add_subregion_overlap(pci_address_space_io(dev), RCR_IOPORT,
@@ -565,7 +569,8 @@ static void piix3_class_init(ObjectClass *klass, void *data)
k->init = piix3_initfn;
k->config_write = piix3_write_config;
k->vendor_id = PCI_VENDOR_ID_INTEL;
- k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0; // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
+ /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
+ k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0;
k->class_id = PCI_CLASS_BRIDGE_ISA;
}
@@ -588,7 +593,8 @@ static void piix3_xen_class_init(ObjectClass *klass, void *data)
k->init = piix3_initfn;
k->config_write = piix3_write_config_xen;
k->vendor_id = PCI_VENDOR_ID_INTEL;
- k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0; // 82371SB PIIX3 PCI-to-ISA bridge (Step A1)
+ /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
+ k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0;
k->class_id = PCI_CLASS_BRIDGE_ISA;
};
@@ -617,7 +623,7 @@ static void i440fx_class_init(ObjectClass *klass, void *data)
}
static const TypeInfo i440fx_info = {
- .name = "i440FX",
+ .name = TYPE_I440FX_PCI_DEVICE,
.parent = TYPE_PCI_DEVICE,
.instance_size = sizeof(PCII440FXState),
.class_init = i440fx_class_init,
diff --git a/hw/s390x/virtio-ccw.c b/hw/s390x/virtio-ccw.c
index 7e79c5795a..5dce791406 100644
--- a/hw/s390x/virtio-ccw.c
+++ b/hw/s390x/virtio-ccw.c
@@ -608,22 +608,24 @@ static int virtio_ccw_serial_exit(VirtioCcwDevice *dev)
return virtio_ccw_exit(dev);
}
-static int virtio_ccw_balloon_init(VirtioCcwDevice *dev)
+static int virtio_ccw_balloon_init(VirtioCcwDevice *ccw_dev)
{
- VirtIODevice *vdev;
+ VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev);
+ DeviceState *vdev = DEVICE(&dev->vdev);
- vdev = virtio_balloon_init((DeviceState *)dev);
- if (!vdev) {
+ qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus));
+ if (qdev_init(vdev) < 0) {
return -1;
}
- return virtio_ccw_device_init(dev, vdev);
+ return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev));
}
-static int virtio_ccw_balloon_exit(VirtioCcwDevice *dev)
+static void virtio_ccw_balloon_instance_init(Object *obj)
{
- virtio_balloon_exit(dev->vdev);
- return virtio_ccw_exit(dev);
+ VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj);
+ object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BALLOON);
+ object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
}
static int virtio_ccw_scsi_init(VirtioCcwDevice *ccw_dev)
@@ -820,15 +822,16 @@ static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data)
VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass);
k->init = virtio_ccw_balloon_init;
- k->exit = virtio_ccw_balloon_exit;
+ k->exit = virtio_ccw_exit;
dc->reset = virtio_ccw_reset;
dc->props = virtio_ccw_balloon_properties;
}
static const TypeInfo virtio_ccw_balloon = {
- .name = "virtio-balloon-ccw",
+ .name = TYPE_VIRTIO_BALLOON_CCW,
.parent = TYPE_VIRTIO_CCW_DEVICE,
- .instance_size = sizeof(VirtioCcwDevice),
+ .instance_size = sizeof(VirtIOBalloonCcw),
+ .instance_init = virtio_ccw_balloon_instance_init,
.class_init = virtio_ccw_balloon_class_init,
};
diff --git a/hw/s390x/virtio-ccw.h b/hw/s390x/virtio-ccw.h
index d9f73997df..d580510283 100644
--- a/hw/s390x/virtio-ccw.h
+++ b/hw/s390x/virtio-ccw.h
@@ -16,6 +16,7 @@
#include <hw/virtio-net.h>
#include <hw/virtio-serial.h>
#include <hw/virtio-scsi.h>
+#include "hw/virtio-balloon.h"
#include <hw/virtio-rng.h>
#include <hw/virtio-bus.h>
@@ -115,6 +116,16 @@ typedef struct VirtIOBlkCcw {
VirtIOBlkConf blk;
} VirtIOBlkCcw;
+/* virtio-balloon-ccw */
+
+#define TYPE_VIRTIO_BALLOON_CCW "virtio-balloon-ccw"
+#define VIRTIO_BALLOON_CCW(obj) \
+ OBJECT_CHECK(VirtIOBalloonCcw, (obj), TYPE_VIRTIO_BALLOON_CCW)
+
+typedef struct VirtIOBalloonCcw {
+ VirtioCcwDevice parent_obj;
+ VirtIOBalloon vdev;
+} VirtIOBalloonCcw;
VirtualCssBus *virtual_css_bus_init(void);
void virtio_ccw_device_update_status(SubchDev *sch);
diff --git a/hw/sysbus.c b/hw/sysbus.c
index 702fc728f4..9004d8c543 100644
--- a/hw/sysbus.c
+++ b/hw/sysbus.c
@@ -137,6 +137,9 @@ static int sysbus_device_init(DeviceState *dev)
SysBusDevice *sd = SYS_BUS_DEVICE(dev);
SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
+ if (!sbc->init) {
+ return 0;
+ }
return sbc->init(sd);
}
@@ -233,24 +236,6 @@ static char *sysbus_get_fw_dev_path(DeviceState *dev)
return g_strdup(path);
}
-void sysbus_add_memory(SysBusDevice *dev, hwaddr addr,
- MemoryRegion *mem)
-{
- memory_region_add_subregion(get_system_memory(), addr, mem);
-}
-
-void sysbus_add_memory_overlap(SysBusDevice *dev, hwaddr addr,
- MemoryRegion *mem, unsigned priority)
-{
- memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
- priority);
-}
-
-void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
-{
- memory_region_del_subregion(get_system_memory(), mem);
-}
-
void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
MemoryRegion *mem)
{
diff --git a/hw/sysbus.h b/hw/sysbus.h
index 5d90a52af5..7c2e3163fb 100644
--- a/hw/sysbus.h
+++ b/hw/sysbus.h
@@ -58,11 +58,6 @@ void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq);
void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr);
void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr,
unsigned priority);
-void sysbus_add_memory(SysBusDevice *dev, hwaddr addr,
- MemoryRegion *mem);
-void sysbus_add_memory_overlap(SysBusDevice *dev, hwaddr addr,
- MemoryRegion *mem, unsigned priority);
-void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem);
void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
MemoryRegion *mem);
void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem);
diff --git a/hw/virtio-balloon.c b/hw/virtio-balloon.c
index 54a43728a5..b382bd440d 100644
--- a/hw/virtio-balloon.c
+++ b/hw/virtio-balloon.c
@@ -29,10 +29,7 @@
#include <sys/mman.h>
#endif
-static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
-{
- return (VirtIOBalloon *)vdev;
-}
+#include "hw/virtio-bus.h"
static void balloon_page(void *addr, int deflate)
{
@@ -69,7 +66,8 @@ static inline void reset_stats(VirtIOBalloon *dev)
static bool balloon_stats_supported(const VirtIOBalloon *s)
{
- return s->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ);
+ VirtIODevice *vdev = VIRTIO_DEVICE(s);
+ return vdev->guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ);
}
static bool balloon_stats_enabled(const VirtIOBalloon *s)
@@ -95,6 +93,7 @@ static void balloon_stats_change_timer(VirtIOBalloon *s, int secs)
static void balloon_stats_poll_cb(void *opaque)
{
VirtIOBalloon *s = opaque;
+ VirtIODevice *vdev = VIRTIO_DEVICE(s);
if (!balloon_stats_supported(s)) {
/* re-schedule */
@@ -103,7 +102,7 @@ static void balloon_stats_poll_cb(void *opaque)
}
virtqueue_push(s->svq, &s->stats_vq_elem, s->stats_vq_offset);
- virtio_notify(&s->vdev, s->svq);
+ virtio_notify(vdev, s->svq);
}
static void balloon_stats_get_all(Object *obj, struct Visitor *v,
@@ -181,7 +180,7 @@ static void balloon_stats_set_poll_interval(Object *obj, struct Visitor *v,
static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
{
- VirtIOBalloon *s = to_virtio_balloon(vdev);
+ VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
VirtQueueElement elem;
MemoryRegionSection section;
@@ -215,7 +214,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
{
- VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
+ VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
VirtQueueElement *elem = &s->stats_vq_elem;
VirtIOBalloonStat stat;
size_t offset = 0;
@@ -257,7 +256,7 @@ out:
static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
{
- VirtIOBalloon *dev = to_virtio_balloon(vdev);
+ VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
struct virtio_balloon_config config;
config.num_pages = cpu_to_le32(dev->num_pages);
@@ -269,7 +268,7 @@ static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
static void virtio_balloon_set_config(VirtIODevice *vdev,
const uint8_t *config_data)
{
- VirtIOBalloon *dev = to_virtio_balloon(vdev);
+ VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
struct virtio_balloon_config config;
uint32_t oldactual = dev->actual;
memcpy(&config, config_data, 8);
@@ -295,22 +294,24 @@ static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
{
- VirtIOBalloon *dev = opaque;
+ VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
if (target > ram_size) {
target = ram_size;
}
if (target) {
dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
- virtio_notify_config(&dev->vdev);
+ virtio_notify_config(vdev);
}
}
static void virtio_balloon_save(QEMUFile *f, void *opaque)
{
- VirtIOBalloon *s = opaque;
+ VirtIOBalloon *s = VIRTIO_BALLOON(opaque);
+ VirtIODevice *vdev = VIRTIO_DEVICE(s);
- virtio_save(&s->vdev, f);
+ virtio_save(vdev, f);
qemu_put_be32(f, s->num_pages);
qemu_put_be32(f, s->actual);
@@ -318,13 +319,14 @@ static void virtio_balloon_save(QEMUFile *f, void *opaque)
static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
{
- VirtIOBalloon *s = opaque;
+ VirtIOBalloon *s = VIRTIO_BALLOON(opaque);
+ VirtIODevice *vdev = VIRTIO_DEVICE(s);
int ret;
if (version_id != 1)
return -EINVAL;
- ret = virtio_load(&s->vdev, f);
+ ret = virtio_load(vdev, f);
if (ret) {
return ret;
}
@@ -334,51 +336,81 @@ static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
return 0;
}
-VirtIODevice *virtio_balloon_init(DeviceState *dev)
+static int virtio_balloon_device_init(VirtIODevice *vdev)
{
- VirtIOBalloon *s;
+ DeviceState *qdev = DEVICE(vdev);
+ VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
int ret;
- s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
- VIRTIO_ID_BALLOON,
- 8, sizeof(VirtIOBalloon));
+ virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON, 8);
- s->vdev.get_config = virtio_balloon_get_config;
- s->vdev.set_config = virtio_balloon_set_config;
- s->vdev.get_features = virtio_balloon_get_features;
+ vdev->get_config = virtio_balloon_get_config;
+ vdev->set_config = virtio_balloon_set_config;
+ vdev->get_features = virtio_balloon_get_features;
ret = qemu_add_balloon_handler(virtio_balloon_to_target,
virtio_balloon_stat, s);
+
if (ret < 0) {
- virtio_cleanup(&s->vdev);
- return NULL;
+ virtio_common_cleanup(VIRTIO_DEVICE(s));
+ return -1;
}
- s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
- s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
- s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
+ s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
+ s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
+ s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
- s->qdev = dev;
- register_savevm(dev, "virtio-balloon", -1, 1,
+ register_savevm(qdev, "virtio-balloon", -1, 1,
virtio_balloon_save, virtio_balloon_load, s);
- object_property_add(OBJECT(dev), "guest-stats", "guest statistics",
+ object_property_add(OBJECT(qdev), "guest-stats", "guest statistics",
balloon_stats_get_all, NULL, NULL, s, NULL);
- object_property_add(OBJECT(dev), "guest-stats-polling-interval", "int",
+ object_property_add(OBJECT(qdev), "guest-stats-polling-interval", "int",
balloon_stats_get_poll_interval,
balloon_stats_set_poll_interval,
NULL, s, NULL);
-
- return &s->vdev;
+ return 0;
}
-void virtio_balloon_exit(VirtIODevice *vdev)
+static int virtio_balloon_device_exit(DeviceState *qdev)
{
- VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
+ VirtIOBalloon *s = VIRTIO_BALLOON(qdev);
+ VirtIODevice *vdev = VIRTIO_DEVICE(qdev);
balloon_stats_destroy_timer(s);
qemu_remove_balloon_handler(s);
- unregister_savevm(s->qdev, "virtio-balloon", s);
- virtio_cleanup(vdev);
+ unregister_savevm(qdev, "virtio-balloon", s);
+ virtio_common_cleanup(vdev);
+ return 0;
+}
+
+static Property virtio_balloon_properties[] = {
+ DEFINE_PROP_END_OF_LIST(),
+};
+
+static void virtio_balloon_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+ dc->exit = virtio_balloon_device_exit;
+ dc->props = virtio_balloon_properties;
+ vdc->init = virtio_balloon_device_init;
+ vdc->get_config = virtio_balloon_get_config;
+ vdc->set_config = virtio_balloon_set_config;
+ vdc->get_features = virtio_balloon_get_features;
+}
+
+static const TypeInfo virtio_balloon_info = {
+ .name = TYPE_VIRTIO_BALLOON,
+ .parent = TYPE_VIRTIO_DEVICE,
+ .instance_size = sizeof(VirtIOBalloon),
+ .class_init = virtio_balloon_class_init,
+};
+
+static void virtio_register_types(void)
+{
+ type_register_static(&virtio_balloon_info);
}
+
+type_init(virtio_register_types)
diff --git a/hw/virtio-balloon.h b/hw/virtio-balloon.h
index b0070421ca..d898315414 100644
--- a/hw/virtio-balloon.h
+++ b/hw/virtio-balloon.h
@@ -18,6 +18,10 @@
#include "hw/virtio.h"
#include "hw/pci/pci.h"
+#define TYPE_VIRTIO_BALLOON "virtio-balloon"
+#define VIRTIO_BALLOON(obj) \
+ OBJECT_CHECK(VirtIOBalloon, (obj), TYPE_VIRTIO_BALLOON)
+
/* from Linux's linux/virtio_balloon.h */
/* The ID for virtio_balloon */
@@ -53,7 +57,7 @@ typedef struct VirtIOBalloonStat {
} QEMU_PACKED VirtIOBalloonStat;
typedef struct VirtIOBalloon {
- VirtIODevice vdev;
+ VirtIODevice parent_obj;
VirtQueue *ivq, *dvq, *svq;
uint32_t num_pages;
uint32_t actual;
@@ -63,7 +67,6 @@ typedef struct VirtIOBalloon {
QEMUTimer *stats_timer;
int64_t stats_last_update;
int64_t stats_poll_interval;
- DeviceState *qdev;
} VirtIOBalloon;
#endif
diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c
index 736a9bf07d..fb20722eaa 100644
--- a/hw/virtio-pci.c
+++ b/hw/virtio-pci.c
@@ -22,6 +22,7 @@
#include "hw/virtio-net.h"
#include "hw/virtio-serial.h"
#include "hw/virtio-scsi.h"
+#include "hw/virtio-balloon.h"
#include "hw/pci/pci.h"
#include "qemu/error-report.h"
#include "hw/pci/msi.h"
@@ -1000,33 +1001,6 @@ static void virtio_net_exit_pci(PCIDevice *pci_dev)
virtio_exit_pci(pci_dev);
}
-static int virtio_balloon_init_pci(PCIDevice *pci_dev)
-{
- VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
- VirtIODevice *vdev;
-
- if (proxy->class_code != PCI_CLASS_OTHERS &&
- proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
- proxy->class_code = PCI_CLASS_OTHERS;
- }
-
- vdev = virtio_balloon_init(&pci_dev->qdev);
- if (!vdev) {
- return -1;
- }
- virtio_init_pci(proxy, vdev);
- return 0;
-}
-
-static void virtio_balloon_exit_pci(PCIDevice *pci_dev)
-{
- VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
-
- virtio_pci_stop_ioeventfd(proxy);
- virtio_balloon_exit(proxy->vdev);
- virtio_exit_pci(pci_dev);
-}
-
static int virtio_rng_init_pci(PCIDevice *pci_dev)
{
VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
@@ -1127,34 +1101,6 @@ static const TypeInfo virtio_serial_info = {
.class_init = virtio_serial_class_init,
};
-static Property virtio_balloon_properties[] = {
- DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
- DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
- DEFINE_PROP_END_OF_LIST(),
-};
-
-static void virtio_balloon_class_init(ObjectClass *klass, void *data)
-{
- DeviceClass *dc = DEVICE_CLASS(klass);
- PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
-
- k->init = virtio_balloon_init_pci;
- k->exit = virtio_balloon_exit_pci;
- k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
- k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
- k->revision = VIRTIO_PCI_ABI_VERSION;
- k->class_id = PCI_CLASS_OTHERS;
- dc->reset = virtio_pci_reset;
- dc->props = virtio_balloon_properties;
-}
-
-static const TypeInfo virtio_balloon_info = {
- .name = "virtio-balloon-pci",
- .parent = TYPE_PCI_DEVICE,
- .instance_size = sizeof(VirtIOPCIProxy),
- .class_init = virtio_balloon_class_init,
-};
-
static void virtio_rng_initfn(Object *obj)
{
PCIDevice *pci_dev = PCI_DEVICE(obj);
@@ -1461,6 +1407,59 @@ static const TypeInfo virtio_scsi_pci_info = {
.class_init = virtio_scsi_pci_class_init,
};
+/* virtio-balloon-pci */
+
+static Property virtio_balloon_pci_properties[] = {
+ DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
+ DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
+static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev)
+{
+ VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
+ DeviceState *vdev = DEVICE(&dev->vdev);
+
+ if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
+ vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
+ vpci_dev->class_code = PCI_CLASS_OTHERS;
+ }
+
+ qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
+ if (qdev_init(vdev) < 0) {
+ return -1;
+ }
+ return 0;
+}
+
+static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+ PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+ k->init = virtio_balloon_pci_init;
+ dc->props = virtio_balloon_pci_properties;
+ pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+ pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
+ pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
+ pcidev_k->class_id = PCI_CLASS_OTHERS;
+}
+
+static void virtio_balloon_pci_instance_init(Object *obj)
+{
+ VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
+ object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BALLOON);
+ object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
+}
+
+static const TypeInfo virtio_balloon_pci_info = {
+ .name = TYPE_VIRTIO_BALLOON_PCI,
+ .parent = TYPE_VIRTIO_PCI,
+ .instance_size = sizeof(VirtIOBalloonPCI),
+ .instance_init = virtio_balloon_pci_instance_init,
+ .class_init = virtio_balloon_pci_class_init,
+};
+
/* virtio-pci-bus */
void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev)
@@ -1501,7 +1500,6 @@ static void virtio_pci_register_types(void)
{
type_register_static(&virtio_net_info);
type_register_static(&virtio_serial_info);
- type_register_static(&virtio_balloon_info);
type_register_static(&virtio_rng_info);
type_register_static(&virtio_pci_bus_info);
type_register_static(&virtio_pci_info);
@@ -1510,6 +1508,7 @@ static void virtio_pci_register_types(void)
#endif
type_register_static(&virtio_blk_pci_info);
type_register_static(&virtio_scsi_pci_info);
+ type_register_static(&virtio_balloon_pci_info);
}
type_init(virtio_pci_register_types)
diff --git a/hw/virtio-pci.h b/hw/virtio-pci.h
index bfe7a8e1ca..f99f2eb80e 100644
--- a/hw/virtio-pci.h
+++ b/hw/virtio-pci.h
@@ -21,12 +21,14 @@
#include "hw/virtio-rng.h"
#include "hw/virtio-serial.h"
#include "hw/virtio-scsi.h"
+#include "hw/virtio-balloon.h"
#include "hw/virtio-bus.h"
#include "hw/9pfs/virtio-9p-device.h"
typedef struct VirtIOPCIProxy VirtIOPCIProxy;
typedef struct VirtIOBlkPCI VirtIOBlkPCI;
typedef struct VirtIOSCSIPCI VirtIOSCSIPCI;
+typedef struct VirtIOBalloonPCI VirtIOBalloonPCI;
/* virtio-pci-bus */
@@ -116,6 +118,18 @@ struct VirtIOBlkPCI {
VirtIOBlkConf blk;
};
+/*
+ * virtio-balloon-pci: This extends VirtioPCIProxy.
+ */
+#define TYPE_VIRTIO_BALLOON_PCI "virtio-balloon-pci"
+#define VIRTIO_BALLOON_PCI(obj) \
+ OBJECT_CHECK(VirtIOBalloonPCI, (obj), TYPE_VIRTIO_BALLOON_PCI)
+
+struct VirtIOBalloonPCI {
+ VirtIOPCIProxy parent_obj;
+ VirtIOBalloon vdev;
+};
+
void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev);
void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev);