11cf4323eSThomas Huth /*
21cf4323eSThomas Huth  * libqos driver framework
31cf4323eSThomas Huth  *
41cf4323eSThomas Huth  * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
51cf4323eSThomas Huth  *
61cf4323eSThomas Huth  * This library is free software; you can redistribute it and/or
71cf4323eSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8dc0ad02dSThomas Huth  * License version 2.1 as published by the Free Software Foundation.
91cf4323eSThomas Huth  *
101cf4323eSThomas Huth  * This library is distributed in the hope that it will be useful,
111cf4323eSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
121cf4323eSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
131cf4323eSThomas Huth  * Lesser General Public License for more details.
141cf4323eSThomas Huth  *
151cf4323eSThomas Huth  * You should have received a copy of the GNU Lesser General Public
161cf4323eSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>
171cf4323eSThomas Huth  */
181cf4323eSThomas Huth 
191cf4323eSThomas Huth #include "qemu/osdep.h"
20*907b5105SMarc-André Lureau #include "../libqtest.h"
211cf4323eSThomas Huth #include "qemu/module.h"
22a2ce7dbdSPaolo Bonzini #include "qgraph.h"
23a2ce7dbdSPaolo Bonzini #include "virtio-balloon.h"
241cf4323eSThomas Huth 
251cf4323eSThomas Huth /* virtio-balloon-device */
qvirtio_balloon_get_driver(QVirtioBalloon * v_balloon,const char * interface)261cf4323eSThomas Huth static void *qvirtio_balloon_get_driver(QVirtioBalloon *v_balloon,
271cf4323eSThomas Huth                                         const char *interface)
281cf4323eSThomas Huth {
291cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio-balloon")) {
301cf4323eSThomas Huth         return v_balloon;
311cf4323eSThomas Huth     }
321cf4323eSThomas Huth     if (!g_strcmp0(interface, "virtio")) {
331cf4323eSThomas Huth         return v_balloon->vdev;
341cf4323eSThomas Huth     }
351cf4323eSThomas Huth 
361cf4323eSThomas Huth     fprintf(stderr, "%s not present in virtio-balloon-device\n", interface);
371cf4323eSThomas Huth     g_assert_not_reached();
381cf4323eSThomas Huth }
391cf4323eSThomas Huth 
qvirtio_balloon_device_get_driver(void * object,const char * interface)401cf4323eSThomas Huth static void *qvirtio_balloon_device_get_driver(void *object,
411cf4323eSThomas Huth                                                const char *interface)
421cf4323eSThomas Huth {
431cf4323eSThomas Huth     QVirtioBalloonDevice *v_balloon = object;
441cf4323eSThomas Huth     return qvirtio_balloon_get_driver(&v_balloon->balloon, interface);
451cf4323eSThomas Huth }
461cf4323eSThomas Huth 
virtio_balloon_device_create(void * virtio_dev,QGuestAllocator * t_alloc,void * addr)471cf4323eSThomas Huth static void *virtio_balloon_device_create(void *virtio_dev,
481cf4323eSThomas Huth                                           QGuestAllocator *t_alloc,
491cf4323eSThomas Huth                                           void *addr)
501cf4323eSThomas Huth {
511cf4323eSThomas Huth     QVirtioBalloonDevice *virtio_bdevice = g_new0(QVirtioBalloonDevice, 1);
521cf4323eSThomas Huth     QVirtioBalloon *interface = &virtio_bdevice->balloon;
531cf4323eSThomas Huth 
541cf4323eSThomas Huth     interface->vdev = virtio_dev;
551cf4323eSThomas Huth 
561cf4323eSThomas Huth     virtio_bdevice->obj.get_driver = qvirtio_balloon_device_get_driver;
571cf4323eSThomas Huth 
581cf4323eSThomas Huth     return &virtio_bdevice->obj;
591cf4323eSThomas Huth }
601cf4323eSThomas Huth 
611cf4323eSThomas Huth /* virtio-balloon-pci */
qvirtio_balloon_pci_get_driver(void * object,const char * interface)621cf4323eSThomas Huth static void *qvirtio_balloon_pci_get_driver(void *object,
631cf4323eSThomas Huth                                             const char *interface)
641cf4323eSThomas Huth {
651cf4323eSThomas Huth     QVirtioBalloonPCI *v_balloon = object;
661cf4323eSThomas Huth     if (!g_strcmp0(interface, "pci-device")) {
671cf4323eSThomas Huth         return v_balloon->pci_vdev.pdev;
681cf4323eSThomas Huth     }
691cf4323eSThomas Huth     return qvirtio_balloon_get_driver(&v_balloon->balloon, interface);
701cf4323eSThomas Huth }
711cf4323eSThomas Huth 
virtio_balloon_pci_create(void * pci_bus,QGuestAllocator * t_alloc,void * addr)721cf4323eSThomas Huth static void *virtio_balloon_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
731cf4323eSThomas Huth                                   void *addr)
741cf4323eSThomas Huth {
751cf4323eSThomas Huth     QVirtioBalloonPCI *virtio_bpci = g_new0(QVirtioBalloonPCI, 1);
761cf4323eSThomas Huth     QVirtioBalloon *interface = &virtio_bpci->balloon;
771cf4323eSThomas Huth     QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
781cf4323eSThomas Huth 
791cf4323eSThomas Huth 
801cf4323eSThomas Huth     virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
811cf4323eSThomas Huth     interface->vdev = &virtio_bpci->pci_vdev.vdev;
821cf4323eSThomas Huth 
831cf4323eSThomas Huth     obj->get_driver = qvirtio_balloon_pci_get_driver;
841cf4323eSThomas Huth 
851cf4323eSThomas Huth     return obj;
861cf4323eSThomas Huth }
871cf4323eSThomas Huth 
virtio_balloon_register_nodes(void)881cf4323eSThomas Huth static void virtio_balloon_register_nodes(void)
891cf4323eSThomas Huth {
901cf4323eSThomas Huth     QPCIAddress addr = {
911cf4323eSThomas Huth         .devfn = QPCI_DEVFN(4, 0),
921cf4323eSThomas Huth     };
931cf4323eSThomas Huth 
941cf4323eSThomas Huth     QOSGraphEdgeOptions opts = {
951cf4323eSThomas Huth         .extra_device_opts = "addr=04.0",
961cf4323eSThomas Huth     };
971cf4323eSThomas Huth 
981cf4323eSThomas Huth     /* virtio-balloon-device */
991cf4323eSThomas Huth     qos_node_create_driver("virtio-balloon-device",
1001cf4323eSThomas Huth                             virtio_balloon_device_create);
1011cf4323eSThomas Huth     qos_node_consumes("virtio-balloon-device", "virtio-bus", NULL);
1021cf4323eSThomas Huth     qos_node_produces("virtio-balloon-device", "virtio");
1031cf4323eSThomas Huth     qos_node_produces("virtio-balloon-device", "virtio-balloon");
1041cf4323eSThomas Huth 
1051cf4323eSThomas Huth     /* virtio-balloon-pci */
1061cf4323eSThomas Huth     add_qpci_address(&opts, &addr);
1071cf4323eSThomas Huth     qos_node_create_driver("virtio-balloon-pci", virtio_balloon_pci_create);
1081cf4323eSThomas Huth     qos_node_consumes("virtio-balloon-pci", "pci-bus", &opts);
1091cf4323eSThomas Huth     qos_node_produces("virtio-balloon-pci", "pci-device");
1101cf4323eSThomas Huth     qos_node_produces("virtio-balloon-pci", "virtio");
1111cf4323eSThomas Huth     qos_node_produces("virtio-balloon-pci", "virtio-balloon");
1121cf4323eSThomas Huth }
1131cf4323eSThomas Huth 
1141cf4323eSThomas Huth libqos_init(virtio_balloon_register_nodes);
115