bus.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Dynamic device configuration and creation -- buses.
  3. *
  4. * Copyright (c) 2009 CodeSourcery
  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 as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/qdev-properties.h"
  21. #include "qemu/ctype.h"
  22. #include "qemu/module.h"
  23. #include "qapi/error.h"
  24. void qbus_set_hotplug_handler(BusState *bus, Object *handler)
  25. {
  26. object_property_set_link(OBJECT(bus), QDEV_HOTPLUG_HANDLER_PROPERTY,
  27. handler, &error_abort);
  28. }
  29. void qbus_set_bus_hotplug_handler(BusState *bus)
  30. {
  31. qbus_set_hotplug_handler(bus, OBJECT(bus));
  32. }
  33. int qbus_walk_children(BusState *bus,
  34. qdev_walkerfn *pre_devfn, qbus_walkerfn *pre_busfn,
  35. qdev_walkerfn *post_devfn, qbus_walkerfn *post_busfn,
  36. void *opaque)
  37. {
  38. BusChild *kid;
  39. int err;
  40. if (pre_busfn) {
  41. err = pre_busfn(bus, opaque);
  42. if (err) {
  43. return err;
  44. }
  45. }
  46. WITH_RCU_READ_LOCK_GUARD() {
  47. QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
  48. err = qdev_walk_children(kid->child,
  49. pre_devfn, pre_busfn,
  50. post_devfn, post_busfn, opaque);
  51. if (err < 0) {
  52. return err;
  53. }
  54. }
  55. }
  56. if (post_busfn) {
  57. err = post_busfn(bus, opaque);
  58. if (err) {
  59. return err;
  60. }
  61. }
  62. return 0;
  63. }
  64. void bus_cold_reset(BusState *bus)
  65. {
  66. resettable_reset(OBJECT(bus), RESET_TYPE_COLD);
  67. }
  68. bool bus_is_in_reset(BusState *bus)
  69. {
  70. return resettable_is_in_reset(OBJECT(bus));
  71. }
  72. static ResettableState *bus_get_reset_state(Object *obj)
  73. {
  74. BusState *bus = BUS(obj);
  75. return &bus->reset;
  76. }
  77. static void bus_reset_child_foreach(Object *obj, ResettableChildCallback cb,
  78. void *opaque, ResetType type)
  79. {
  80. BusState *bus = BUS(obj);
  81. BusChild *kid;
  82. WITH_RCU_READ_LOCK_GUARD() {
  83. QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
  84. cb(OBJECT(kid->child), opaque, type);
  85. }
  86. }
  87. }
  88. static void qbus_init_internal(BusState *bus, DeviceState *parent,
  89. const char *name)
  90. {
  91. const char *typename = object_get_typename(OBJECT(bus));
  92. BusClass *bc;
  93. int i, bus_id;
  94. bus->parent = parent;
  95. if (name) {
  96. bus->name = g_strdup(name);
  97. } else if (bus->parent && bus->parent->id) {
  98. /* parent device has id -> use it plus parent-bus-id for bus name */
  99. bus_id = bus->parent->num_child_bus;
  100. bus->name = g_strdup_printf("%s.%d", bus->parent->id, bus_id);
  101. } else {
  102. /* no id -> use lowercase bus type plus global bus-id for bus name */
  103. bc = BUS_GET_CLASS(bus);
  104. bus_id = bc->automatic_ids++;
  105. bus->name = g_strdup_printf("%s.%d", typename, bus_id);
  106. for (i = 0; bus->name[i]; i++) {
  107. bus->name[i] = qemu_tolower(bus->name[i]);
  108. }
  109. }
  110. if (bus->parent) {
  111. QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
  112. bus->parent->num_child_bus++;
  113. object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus));
  114. object_unref(OBJECT(bus));
  115. } else {
  116. /* The only bus without a parent is the main system bus */
  117. assert(bus == sysbus_get_default());
  118. }
  119. }
  120. static void bus_unparent(Object *obj)
  121. {
  122. BusState *bus = BUS(obj);
  123. BusChild *kid;
  124. /* Only the main system bus has no parent, and that bus is never freed */
  125. assert(bus->parent);
  126. while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
  127. DeviceState *dev = kid->child;
  128. object_unparent(OBJECT(dev));
  129. }
  130. QLIST_REMOVE(bus, sibling);
  131. bus->parent->num_child_bus--;
  132. bus->parent = NULL;
  133. }
  134. void qbus_init(void *bus, size_t size, const char *typename,
  135. DeviceState *parent, const char *name)
  136. {
  137. object_initialize(bus, size, typename);
  138. qbus_init_internal(bus, parent, name);
  139. }
  140. BusState *qbus_new(const char *typename, DeviceState *parent, const char *name)
  141. {
  142. BusState *bus;
  143. bus = BUS(object_new(typename));
  144. qbus_init_internal(bus, parent, name);
  145. return bus;
  146. }
  147. bool qbus_realize(BusState *bus, Error **errp)
  148. {
  149. return object_property_set_bool(OBJECT(bus), "realized", true, errp);
  150. }
  151. void qbus_unrealize(BusState *bus)
  152. {
  153. object_property_set_bool(OBJECT(bus), "realized", false, &error_abort);
  154. }
  155. static bool bus_get_realized(Object *obj, Error **errp)
  156. {
  157. BusState *bus = BUS(obj);
  158. return bus->realized;
  159. }
  160. static void bus_set_realized(Object *obj, bool value, Error **errp)
  161. {
  162. BusState *bus = BUS(obj);
  163. BusClass *bc = BUS_GET_CLASS(bus);
  164. BusChild *kid;
  165. if (value && !bus->realized) {
  166. if (bc->realize) {
  167. bc->realize(bus, errp);
  168. }
  169. /* TODO: recursive realization */
  170. } else if (!value && bus->realized) {
  171. WITH_RCU_READ_LOCK_GUARD() {
  172. QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
  173. DeviceState *dev = kid->child;
  174. qdev_unrealize(dev);
  175. }
  176. }
  177. if (bc->unrealize) {
  178. bc->unrealize(bus);
  179. }
  180. }
  181. bus->realized = value;
  182. }
  183. static void qbus_initfn(Object *obj)
  184. {
  185. BusState *bus = BUS(obj);
  186. QTAILQ_INIT(&bus->children);
  187. object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
  188. TYPE_HOTPLUG_HANDLER,
  189. (Object **)&bus->hotplug_handler,
  190. object_property_allow_set_link,
  191. 0);
  192. object_property_add_bool(obj, "realized",
  193. bus_get_realized, bus_set_realized);
  194. }
  195. static char *default_bus_get_fw_dev_path(DeviceState *dev)
  196. {
  197. return g_strdup(object_get_typename(OBJECT(dev)));
  198. }
  199. static void bus_class_init(ObjectClass *class, void *data)
  200. {
  201. BusClass *bc = BUS_CLASS(class);
  202. ResettableClass *rc = RESETTABLE_CLASS(class);
  203. class->unparent = bus_unparent;
  204. bc->get_fw_dev_path = default_bus_get_fw_dev_path;
  205. rc->get_state = bus_get_reset_state;
  206. rc->child_foreach = bus_reset_child_foreach;
  207. }
  208. static void qbus_finalize(Object *obj)
  209. {
  210. BusState *bus = BUS(obj);
  211. g_free(bus->name);
  212. }
  213. static const TypeInfo bus_info = {
  214. .name = TYPE_BUS,
  215. .parent = TYPE_OBJECT,
  216. .instance_size = sizeof(BusState),
  217. .abstract = true,
  218. .class_size = sizeof(BusClass),
  219. .instance_init = qbus_initfn,
  220. .instance_finalize = qbus_finalize,
  221. .class_init = bus_class_init,
  222. .interfaces = (InterfaceInfo[]) {
  223. { TYPE_RESETTABLE_INTERFACE },
  224. { }
  225. },
  226. };
  227. static void bus_register_types(void)
  228. {
  229. type_register_static(&bus_info);
  230. }
  231. type_init(bus_register_types)