libqos: read QVIRTIO_MMIO_VERSION register
[qemu/ar7.git] / tests / libqos / virtio-balloon.c
blob42a4c5831eada619f3c010288ed757be56a48ae0
1 /*
2 * libqos driver framework
4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
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.
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.
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/>
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"
25 /* virtio-balloon-device */
26 static void *qvirtio_balloon_get_driver(QVirtioBalloon *v_balloon,
27 const char *interface)
29 if (!g_strcmp0(interface, "virtio-balloon")) {
30 return v_balloon;
32 if (!g_strcmp0(interface, "virtio")) {
33 return v_balloon->vdev;
36 fprintf(stderr, "%s not present in virtio-balloon-device\n", interface);
37 g_assert_not_reached();
40 static void *qvirtio_balloon_device_get_driver(void *object,
41 const char *interface)
43 QVirtioBalloonDevice *v_balloon = object;
44 return qvirtio_balloon_get_driver(&v_balloon->balloon, interface);
47 static void *virtio_balloon_device_create(void *virtio_dev,
48 QGuestAllocator *t_alloc,
49 void *addr)
51 QVirtioBalloonDevice *virtio_bdevice = g_new0(QVirtioBalloonDevice, 1);
52 QVirtioBalloon *interface = &virtio_bdevice->balloon;
54 interface->vdev = virtio_dev;
56 virtio_bdevice->obj.get_driver = qvirtio_balloon_device_get_driver;
58 return &virtio_bdevice->obj;
61 /* virtio-balloon-pci */
62 static void *qvirtio_balloon_pci_get_driver(void *object,
63 const char *interface)
65 QVirtioBalloonPCI *v_balloon = object;
66 if (!g_strcmp0(interface, "pci-device")) {
67 return v_balloon->pci_vdev.pdev;
69 return qvirtio_balloon_get_driver(&v_balloon->balloon, interface);
72 static void *virtio_balloon_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
73 void *addr)
75 QVirtioBalloonPCI *virtio_bpci = g_new0(QVirtioBalloonPCI, 1);
76 QVirtioBalloon *interface = &virtio_bpci->balloon;
77 QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
80 virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
81 interface->vdev = &virtio_bpci->pci_vdev.vdev;
83 obj->get_driver = qvirtio_balloon_pci_get_driver;
85 return obj;
88 static void virtio_balloon_register_nodes(void)
90 QPCIAddress addr = {
91 .devfn = QPCI_DEVFN(4, 0),
94 QOSGraphEdgeOptions opts = {
95 .extra_device_opts = "addr=04.0",
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");
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");
114 libqos_init(virtio_balloon_register_nodes);