virtio-rng.c 3.4 KB

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