1 /*
2  * libqos driver framework
3  *
4  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License version 2 as published by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, see <http://www.gnu.org/licenses/>
17  */
18 
19 #include "qemu/osdep.h"
20 #include "libqtest.h"
21 #include "qemu/module.h"
22 #include "libqos/qgraph.h"
23 #include "libqos/virtio-balloon.h"
24 
25 /* virtio-balloon-device */
26 static void *qvirtio_balloon_get_driver(QVirtioBalloon *v_balloon,
27                                         const char *interface)
28 {
29     if (!g_strcmp0(interface, "virtio-balloon")) {
30         return v_balloon;
31     }
32     if (!g_strcmp0(interface, "virtio")) {
33         return v_balloon->vdev;
34     }
35 
36     fprintf(stderr, "%s not present in virtio-balloon-device\n", interface);
37     g_assert_not_reached();
38 }
39 
40 static void *qvirtio_balloon_device_get_driver(void *object,
41                                                const char *interface)
42 {
43     QVirtioBalloonDevice *v_balloon = object;
44     return qvirtio_balloon_get_driver(&v_balloon->balloon, interface);
45 }
46 
47 static void *virtio_balloon_device_create(void *virtio_dev,
48                                           QGuestAllocator *t_alloc,
49                                           void *addr)
50 {
51     QVirtioBalloonDevice *virtio_bdevice = g_new0(QVirtioBalloonDevice, 1);
52     QVirtioBalloon *interface = &virtio_bdevice->balloon;
53 
54     interface->vdev = virtio_dev;
55 
56     virtio_bdevice->obj.get_driver = qvirtio_balloon_device_get_driver;
57 
58     return &virtio_bdevice->obj;
59 }
60 
61 /* virtio-balloon-pci */
62 static void *qvirtio_balloon_pci_get_driver(void *object,
63                                             const char *interface)
64 {
65     QVirtioBalloonPCI *v_balloon = object;
66     if (!g_strcmp0(interface, "pci-device")) {
67         return v_balloon->pci_vdev.pdev;
68     }
69     return qvirtio_balloon_get_driver(&v_balloon->balloon, interface);
70 }
71 
72 static void *virtio_balloon_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
73                                   void *addr)
74 {
75     QVirtioBalloonPCI *virtio_bpci = g_new0(QVirtioBalloonPCI, 1);
76     QVirtioBalloon *interface = &virtio_bpci->balloon;
77     QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
78 
79 
80     virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
81     interface->vdev = &virtio_bpci->pci_vdev.vdev;
82 
83     obj->get_driver = qvirtio_balloon_pci_get_driver;
84 
85     return obj;
86 }
87 
88 static void virtio_balloon_register_nodes(void)
89 {
90     QPCIAddress addr = {
91         .devfn = QPCI_DEVFN(4, 0),
92     };
93 
94     QOSGraphEdgeOptions opts = {
95         .extra_device_opts = "addr=04.0",
96     };
97 
98     /* virtio-balloon-device */
99     qos_node_create_driver("virtio-balloon-device",
100                             virtio_balloon_device_create);
101     qos_node_consumes("virtio-balloon-device", "virtio-bus", NULL);
102     qos_node_produces("virtio-balloon-device", "virtio");
103     qos_node_produces("virtio-balloon-device", "virtio-balloon");
104 
105     /* virtio-balloon-pci */
106     add_qpci_address(&opts, &addr);
107     qos_node_create_driver("virtio-balloon-pci", virtio_balloon_pci_create);
108     qos_node_consumes("virtio-balloon-pci", "pci-bus", &opts);
109     qos_node_produces("virtio-balloon-pci", "pci-device");
110     qos_node_produces("virtio-balloon-pci", "virtio");
111     qos_node_produces("virtio-balloon-pci", "virtio-balloon");
112 }
113 
114 libqos_init(virtio_balloon_register_nodes);
115