virtio-bus.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * VirtioBus
  3. *
  4. * Copyright (C) 2012 : GreenSocs Ltd
  5. * http://www.greensocs.com/ , email: info@greensocs.com
  6. *
  7. * Developed by :
  8. * Frederic Konrad <fred.konrad@greensocs.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. #include "hw.h"
  25. #include "qemu/error-report.h"
  26. #include "qdev.h"
  27. #include "virtio-bus.h"
  28. #include "virtio.h"
  29. /* #define DEBUG_VIRTIO_BUS */
  30. #ifdef DEBUG_VIRTIO_BUS
  31. #define DPRINTF(fmt, ...) \
  32. do { printf("virtio_bus: " fmt , ## __VA_ARGS__); } while (0)
  33. #else
  34. #define DPRINTF(fmt, ...) do { } while (0)
  35. #endif
  36. /* Plug the VirtIODevice */
  37. int virtio_bus_plug_device(VirtIODevice *vdev)
  38. {
  39. DeviceState *qdev = DEVICE(vdev);
  40. BusState *qbus = BUS(qdev_get_parent_bus(qdev));
  41. VirtioBusState *bus = VIRTIO_BUS(qbus);
  42. VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
  43. DPRINTF("%s: plug device.\n", qbus->name);
  44. bus->vdev = vdev;
  45. /*
  46. * The lines below will disappear when we drop VirtIOBindings, at the end
  47. * of the series.
  48. */
  49. bus->bindings.notify = klass->notify;
  50. bus->bindings.save_config = klass->save_config;
  51. bus->bindings.save_queue = klass->save_queue;
  52. bus->bindings.load_config = klass->load_config;
  53. bus->bindings.load_queue = klass->load_queue;
  54. bus->bindings.load_done = klass->load_done;
  55. bus->bindings.get_features = klass->get_features;
  56. bus->bindings.query_guest_notifiers = klass->query_guest_notifiers;
  57. bus->bindings.set_guest_notifiers = klass->set_guest_notifiers;
  58. bus->bindings.set_host_notifier = klass->set_host_notifier;
  59. bus->bindings.vmstate_change = klass->vmstate_change;
  60. virtio_bind_device(bus->vdev, &bus->bindings, qbus->parent);
  61. if (klass->device_plugged != NULL) {
  62. klass->device_plugged(qbus->parent);
  63. }
  64. return 0;
  65. }
  66. /* Reset the virtio_bus */
  67. void virtio_bus_reset(VirtioBusState *bus)
  68. {
  69. DPRINTF("%s: reset device.\n", qbus->name);
  70. if (bus->vdev != NULL) {
  71. virtio_reset(bus->vdev);
  72. }
  73. }
  74. /* Destroy the VirtIODevice */
  75. void virtio_bus_destroy_device(VirtioBusState *bus)
  76. {
  77. DeviceState *qdev;
  78. BusState *qbus = BUS(bus);
  79. VirtioBusClass *klass = VIRTIO_BUS_GET_CLASS(bus);
  80. DPRINTF("%s: remove device.\n", qbus->name);
  81. if (bus->vdev != NULL) {
  82. if (klass->device_unplug != NULL) {
  83. klass->device_unplug(qbus->parent);
  84. }
  85. qdev = DEVICE(bus->vdev);
  86. qdev_free(qdev);
  87. bus->vdev = NULL;
  88. }
  89. }
  90. /* Get the device id of the plugged device. */
  91. uint16_t virtio_bus_get_vdev_id(VirtioBusState *bus)
  92. {
  93. assert(bus->vdev != NULL);
  94. return bus->vdev->device_id;
  95. }
  96. /* Get the config_len field of the plugged device. */
  97. size_t virtio_bus_get_vdev_config_len(VirtioBusState *bus)
  98. {
  99. assert(bus->vdev != NULL);
  100. return bus->vdev->config_len;
  101. }
  102. /* Get the features of the plugged device. */
  103. uint32_t virtio_bus_get_vdev_features(VirtioBusState *bus,
  104. uint32_t requested_features)
  105. {
  106. VirtioDeviceClass *k;
  107. assert(bus->vdev != NULL);
  108. k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
  109. assert(k->get_features != NULL);
  110. return k->get_features(bus->vdev, requested_features);
  111. }
  112. /* Get bad features of the plugged device. */
  113. uint32_t virtio_bus_get_vdev_bad_features(VirtioBusState *bus)
  114. {
  115. VirtioDeviceClass *k;
  116. assert(bus->vdev != NULL);
  117. k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
  118. if (k->bad_features != NULL) {
  119. return k->bad_features(bus->vdev);
  120. } else {
  121. return 0;
  122. }
  123. }
  124. /* Get config of the plugged device. */
  125. void virtio_bus_get_vdev_config(VirtioBusState *bus, uint8_t *config)
  126. {
  127. VirtioDeviceClass *k;
  128. assert(bus->vdev != NULL);
  129. k = VIRTIO_DEVICE_GET_CLASS(bus->vdev);
  130. if (k->get_config != NULL) {
  131. k->get_config(bus->vdev, config);
  132. }
  133. }
  134. static const TypeInfo virtio_bus_info = {
  135. .name = TYPE_VIRTIO_BUS,
  136. .parent = TYPE_BUS,
  137. .instance_size = sizeof(VirtioBusState),
  138. .abstract = true,
  139. .class_size = sizeof(VirtioBusClass),
  140. };
  141. static void virtio_register_types(void)
  142. {
  143. type_register_static(&virtio_bus_info);
  144. }
  145. type_init(virtio_register_types)