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-rng.h"
241cf4323eSThomas Huth
251cf4323eSThomas Huth /* virtio-rng-device */
qvirtio_rng_get_driver(QVirtioRng * v_rng,const char * interface)261cf4323eSThomas Huth static void *qvirtio_rng_get_driver(QVirtioRng *v_rng,
271cf4323eSThomas Huth const char *interface)
281cf4323eSThomas Huth {
291cf4323eSThomas Huth if (!g_strcmp0(interface, "virtio-rng")) {
301cf4323eSThomas Huth return v_rng;
311cf4323eSThomas Huth }
321cf4323eSThomas Huth if (!g_strcmp0(interface, "virtio")) {
331cf4323eSThomas Huth return v_rng->vdev;
341cf4323eSThomas Huth }
351cf4323eSThomas Huth
361cf4323eSThomas Huth fprintf(stderr, "%s not present in virtio-rng-device\n", interface);
371cf4323eSThomas Huth g_assert_not_reached();
381cf4323eSThomas Huth }
391cf4323eSThomas Huth
qvirtio_rng_device_get_driver(void * object,const char * interface)401cf4323eSThomas Huth static void *qvirtio_rng_device_get_driver(void *object,
411cf4323eSThomas Huth const char *interface)
421cf4323eSThomas Huth {
431cf4323eSThomas Huth QVirtioRngDevice *v_rng = object;
441cf4323eSThomas Huth return qvirtio_rng_get_driver(&v_rng->rng, interface);
451cf4323eSThomas Huth }
461cf4323eSThomas Huth
virtio_rng_device_create(void * virtio_dev,QGuestAllocator * t_alloc,void * addr)471cf4323eSThomas Huth static void *virtio_rng_device_create(void *virtio_dev,
481cf4323eSThomas Huth QGuestAllocator *t_alloc,
491cf4323eSThomas Huth void *addr)
501cf4323eSThomas Huth {
511cf4323eSThomas Huth QVirtioRngDevice *virtio_rdevice = g_new0(QVirtioRngDevice, 1);
521cf4323eSThomas Huth QVirtioRng *interface = &virtio_rdevice->rng;
531cf4323eSThomas Huth
541cf4323eSThomas Huth interface->vdev = virtio_dev;
551cf4323eSThomas Huth
561cf4323eSThomas Huth virtio_rdevice->obj.get_driver = qvirtio_rng_device_get_driver;
571cf4323eSThomas Huth
581cf4323eSThomas Huth return &virtio_rdevice->obj;
591cf4323eSThomas Huth }
601cf4323eSThomas Huth
611cf4323eSThomas Huth /* virtio-rng-pci */
qvirtio_rng_pci_get_driver(void * object,const char * interface)621cf4323eSThomas Huth static void *qvirtio_rng_pci_get_driver(void *object, const char *interface)
631cf4323eSThomas Huth {
641cf4323eSThomas Huth QVirtioRngPCI *v_rng = object;
651cf4323eSThomas Huth if (!g_strcmp0(interface, "pci-device")) {
661cf4323eSThomas Huth return v_rng->pci_vdev.pdev;
671cf4323eSThomas Huth }
681cf4323eSThomas Huth return qvirtio_rng_get_driver(&v_rng->rng, interface);
691cf4323eSThomas Huth }
701cf4323eSThomas Huth
virtio_rng_pci_create(void * pci_bus,QGuestAllocator * t_alloc,void * addr)711cf4323eSThomas Huth static void *virtio_rng_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
721cf4323eSThomas Huth void *addr)
731cf4323eSThomas Huth {
741cf4323eSThomas Huth QVirtioRngPCI *virtio_rpci = g_new0(QVirtioRngPCI, 1);
751cf4323eSThomas Huth QVirtioRng *interface = &virtio_rpci->rng;
761cf4323eSThomas Huth QOSGraphObject *obj = &virtio_rpci->pci_vdev.obj;
771cf4323eSThomas Huth
781cf4323eSThomas Huth virtio_pci_init(&virtio_rpci->pci_vdev, pci_bus, addr);
791cf4323eSThomas Huth interface->vdev = &virtio_rpci->pci_vdev.vdev;
801cf4323eSThomas Huth
811cf4323eSThomas Huth obj->get_driver = qvirtio_rng_pci_get_driver;
821cf4323eSThomas Huth
831cf4323eSThomas Huth return obj;
841cf4323eSThomas Huth }
851cf4323eSThomas Huth
virtio_rng_register_nodes(void)861cf4323eSThomas Huth static void virtio_rng_register_nodes(void)
871cf4323eSThomas Huth {
881cf4323eSThomas Huth QPCIAddress addr = {
891cf4323eSThomas Huth .devfn = QPCI_DEVFN(4, 0),
901cf4323eSThomas Huth };
911cf4323eSThomas Huth
921cf4323eSThomas Huth QOSGraphEdgeOptions opts = {
931cf4323eSThomas Huth .extra_device_opts = "addr=04.0",
941cf4323eSThomas Huth };
951cf4323eSThomas Huth
961cf4323eSThomas Huth /* virtio-rng-device */
971cf4323eSThomas Huth qos_node_create_driver("virtio-rng-device", virtio_rng_device_create);
981cf4323eSThomas Huth qos_node_consumes("virtio-rng-device", "virtio-bus", NULL);
991cf4323eSThomas Huth qos_node_produces("virtio-rng-device", "virtio");
1001cf4323eSThomas Huth qos_node_produces("virtio-rng-device", "virtio-rng");
1011cf4323eSThomas Huth
1021cf4323eSThomas Huth /* virtio-rng-pci */
1031cf4323eSThomas Huth add_qpci_address(&opts, &addr);
1041cf4323eSThomas Huth qos_node_create_driver("virtio-rng-pci", virtio_rng_pci_create);
1051cf4323eSThomas Huth qos_node_consumes("virtio-rng-pci", "pci-bus", &opts);
1061cf4323eSThomas Huth qos_node_produces("virtio-rng-pci", "pci-device");
1071cf4323eSThomas Huth qos_node_produces("virtio-rng-pci", "virtio");
1081cf4323eSThomas Huth qos_node_produces("virtio-rng-pci", "virtio-rng");
1091cf4323eSThomas Huth }
1101cf4323eSThomas Huth
1111cf4323eSThomas Huth libqos_init(virtio_rng_register_nodes);
112