xref: /openbmc/qemu/hw/virtio/virtio-balloon-pci.c (revision 354908ce)
1 /*
2  * Virtio balloon PCI Bindings
3  *
4  * Copyright IBM, Corp. 2007
5  * Copyright (c) 2009 CodeSourcery
6  *
7  * Authors:
8  *  Anthony Liguori   <aliguori@us.ibm.com>
9  *  Paul Brook        <paul@codesourcery.com>
10  *
11  * Contributions after 2012-01-13 are licensed under the terms of the
12  * GNU GPL, version 2 or (at your option) any later version.
13  */
14 
15 #include "qemu/osdep.h"
16 
17 #include "virtio-pci.h"
18 #include "hw/qdev-properties.h"
19 #include "hw/virtio/virtio-balloon.h"
20 #include "qapi/error.h"
21 #include "qemu/module.h"
22 
23 typedef struct VirtIOBalloonPCI VirtIOBalloonPCI;
24 
25 /*
26  * virtio-balloon-pci: This extends VirtioPCIProxy.
27  */
28 #define TYPE_VIRTIO_BALLOON_PCI "virtio-balloon-pci-base"
29 #define VIRTIO_BALLOON_PCI(obj) \
30         OBJECT_CHECK(VirtIOBalloonPCI, (obj), TYPE_VIRTIO_BALLOON_PCI)
31 
32 struct VirtIOBalloonPCI {
33     VirtIOPCIProxy parent_obj;
34     VirtIOBalloon vdev;
35 };
36 static Property virtio_balloon_pci_properties[] = {
37     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
38     DEFINE_PROP_END_OF_LIST(),
39 };
40 
41 static void virtio_balloon_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
42 {
43     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
44     DeviceState *vdev = DEVICE(&dev->vdev);
45 
46     if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
47         vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
48         vpci_dev->class_code = PCI_CLASS_OTHERS;
49     }
50 
51     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
52 }
53 
54 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
55 {
56     DeviceClass *dc = DEVICE_CLASS(klass);
57     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
58     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
59     k->realize = virtio_balloon_pci_realize;
60     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
61     device_class_set_props(dc, virtio_balloon_pci_properties);
62     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
63     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
64     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
65     pcidev_k->class_id = PCI_CLASS_OTHERS;
66 }
67 
68 static void virtio_balloon_pci_instance_init(Object *obj)
69 {
70     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
71 
72     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
73                                 TYPE_VIRTIO_BALLOON);
74     object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev),
75                                   "guest-stats");
76     object_property_add_alias(obj, "guest-stats-polling-interval",
77                               OBJECT(&dev->vdev),
78                               "guest-stats-polling-interval");
79 }
80 
81 static const VirtioPCIDeviceTypeInfo virtio_balloon_pci_info = {
82     .base_name             = TYPE_VIRTIO_BALLOON_PCI,
83     .generic_name          = "virtio-balloon-pci",
84     .transitional_name     = "virtio-balloon-pci-transitional",
85     .non_transitional_name = "virtio-balloon-pci-non-transitional",
86     .instance_size = sizeof(VirtIOBalloonPCI),
87     .instance_init = virtio_balloon_pci_instance_init,
88     .class_init    = virtio_balloon_pci_class_init,
89 };
90 
91 static void virtio_balloon_pci_register(void)
92 {
93     virtio_pci_types_register(&virtio_balloon_pci_info);
94 }
95 
96 type_init(virtio_balloon_pci_register)
97