| 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.1 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 "qgraph.h" |
| 23 | #include "virtio-rng.h" |
| 24 | |
| 25 | /* virtio-rng-device */ |
| 26 | static void *qvirtio_rng_get_driver(QVirtioRng *v_rng, |
| 27 | const char *interface) |
| 28 | { |
| 29 | if (!g_strcmp0(interface, "virtio-rng")) { |
| 30 | return v_rng; |
| 31 | } |
| 32 | if (!g_strcmp0(interface, "virtio")) { |
| 33 | return v_rng->vdev; |
| 34 | } |
| 35 | |
| 36 | fprintf(stderr, "%s not present in virtio-rng-device\n", interface); |
| 37 | g_assert_not_reached(); |
| 38 | } |
| 39 | |
| 40 | static void *qvirtio_rng_device_get_driver(void *object, |
| 41 | const char *interface) |
| 42 | { |
| 43 | QVirtioRngDevice *v_rng = object; |
| 44 | return qvirtio_rng_get_driver(&v_rng->rng, interface); |
| 45 | } |
| 46 | |
| 47 | static void *virtio_rng_device_create(void *virtio_dev, |
| 48 | QGuestAllocator *t_alloc, |
| 49 | void *addr) |
| 50 | { |
| 51 | QVirtioRngDevice *virtio_rdevice = g_new0(QVirtioRngDevice, 1); |
| 52 | QVirtioRng *interface = &virtio_rdevice->rng; |
| 53 | |
| 54 | interface->vdev = virtio_dev; |
| 55 | |
| 56 | virtio_rdevice->obj.get_driver = qvirtio_rng_device_get_driver; |
| 57 | |
| 58 | return &virtio_rdevice->obj; |
| 59 | } |
| 60 | |
| 61 | /* virtio-rng-pci */ |
| 62 | static void *qvirtio_rng_pci_get_driver(void *object, const char *interface) |
| 63 | { |
| 64 | QVirtioRngPCI *v_rng = object; |
| 65 | if (!g_strcmp0(interface, "pci-device")) { |
| 66 | return v_rng->pci_vdev.pdev; |
| 67 | } |
| 68 | return qvirtio_rng_get_driver(&v_rng->rng, interface); |
| 69 | } |
| 70 | |
| 71 | static void *virtio_rng_pci_create(void *pci_bus, QGuestAllocator *t_alloc, |
| 72 | void *addr) |
| 73 | { |
| 74 | QVirtioRngPCI *virtio_rpci = g_new0(QVirtioRngPCI, 1); |
| 75 | QVirtioRng *interface = &virtio_rpci->rng; |
| 76 | QOSGraphObject *obj = &virtio_rpci->pci_vdev.obj; |
| 77 | |
| 78 | virtio_pci_init(&virtio_rpci->pci_vdev, pci_bus, addr); |
| 79 | interface->vdev = &virtio_rpci->pci_vdev.vdev; |
| 80 | |
| 81 | obj->get_driver = qvirtio_rng_pci_get_driver; |
| 82 | |
| 83 | return obj; |
| 84 | } |
| 85 | |
| 86 | static void virtio_rng_register_nodes(void) |
| 87 | { |
| 88 | QPCIAddress addr = { |
| 89 | .devfn = QPCI_DEVFN(4, 0), |
| 90 | }; |
| 91 | |
| 92 | QOSGraphEdgeOptions opts = { |
| 93 | .extra_device_opts = "addr=04.0", |
| 94 | }; |
| 95 | |
| 96 | /* virtio-rng-device */ |
| 97 | qos_node_create_driver("virtio-rng-device", virtio_rng_device_create); |
| 98 | qos_node_consumes("virtio-rng-device", "virtio-bus", NULL); |
| 99 | qos_node_produces("virtio-rng-device", "virtio"); |
| 100 | qos_node_produces("virtio-rng-device", "virtio-rng"); |
| 101 | |
| 102 | /* virtio-rng-pci */ |
| 103 | add_qpci_address(&opts, &addr); |
| 104 | qos_node_create_driver("virtio-rng-pci", virtio_rng_pci_create); |
| 105 | qos_node_consumes("virtio-rng-pci", "pci-bus", &opts); |
| 106 | qos_node_produces("virtio-rng-pci", "pci-device"); |
| 107 | qos_node_produces("virtio-rng-pci", "virtio"); |
| 108 | qos_node_produces("virtio-rng-pci", "virtio-rng"); |
| 109 | } |
| 110 | |
| 111 | libqos_init(virtio_rng_register_nodes); |