bus.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 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(BusState *bus, DeviceState *parent, const char *name)
  89. {
  90. const char *typename = object_get_typename(OBJECT(bus));
  91. BusClass *bc;
  92. int i, bus_id;
  93. bus->parent = parent;
  94. if (name) {
  95. bus->name = g_strdup(name);
  96. } else if (bus->parent && bus->parent->id) {
  97. /* parent device has id -> use it plus parent-bus-id for bus name */
  98. bus_id = bus->parent->num_child_bus;
  99. bus->name = g_strdup_printf("%s.%d", bus->parent->id, bus_id);
  100. } else {
  101. /* no id -> use lowercase bus type plus global bus-id for bus name */
  102. bc = BUS_GET_CLASS(bus);
  103. bus_id = bc->automatic_ids++;
  104. bus->name = g_strdup_printf("%s.%d", typename, bus_id);
  105. for (i = 0; bus->name[i]; i++) {
  106. bus->name[i] = qemu_tolower(bus->name[i]);
  107. }
  108. }
  109. if (bus->parent) {
  110. QLIST_INSERT_HEAD(&bus->parent->child_bus, bus, sibling);
  111. bus->parent->num_child_bus++;
  112. object_property_add_child(OBJECT(bus->parent), bus->name, OBJECT(bus));
  113. object_unref(OBJECT(bus));
  114. } else {
  115. /* The only bus without a parent is the main system bus */
  116. assert(bus == sysbus_get_default());
  117. }
  118. }
  119. static void bus_unparent(Object *obj)
  120. {
  121. BusState *bus = BUS(obj);
  122. BusChild *kid;
  123. /* Only the main system bus has no parent, and that bus is never freed */
  124. assert(bus->parent);
  125. while ((kid = QTAILQ_FIRST(&bus->children)) != NULL) {
  126. DeviceState *dev = kid->child;
  127. object_unparent(OBJECT(dev));
  128. }
  129. QLIST_REMOVE(bus, sibling);
  130. bus->parent->num_child_bus--;
  131. bus->parent = NULL;
  132. }
  133. void qbus_create_inplace(void *bus, size_t size, const char *typename,
  134. DeviceState *parent, const char *name)
  135. {
  136. object_initialize(bus, size, typename);
  137. qbus_init(bus, parent, name);
  138. }
  139. BusState *qbus_create(const char *typename, DeviceState *parent, const char *name)
  140. {
  141. BusState *bus;
  142. bus = BUS(object_new(typename));
  143. qbus_init(bus, parent, name);
  144. return bus;
  145. }
  146. bool qbus_realize(BusState *bus, Error **errp)
  147. {
  148. return object_property_set_bool(OBJECT(bus), "realized", true, errp);
  149. }
  150. void qbus_unrealize(BusState *bus)
  151. {
  152. object_property_set_bool(OBJECT(bus), "realized", false, &error_abort);
  153. }
  154. static bool bus_get_realized(Object *obj, Error **errp)
  155. {
  156. BusState *bus = BUS(obj);
  157. return bus->realized;
  158. }
  159. static void bus_set_realized(Object *obj, bool value, Error **errp)
  160. {
  161. BusState *bus = BUS(obj);
  162. BusClass *bc = BUS_GET_CLASS(bus);
  163. BusChild *kid;
  164. if (value && !bus->realized) {
  165. if (bc->realize) {
  166. bc->realize(bus, errp);
  167. }
  168. /* TODO: recursive realization */
  169. } else if (!value && bus->realized) {
  170. WITH_RCU_READ_LOCK_GUARD() {
  171. QTAILQ_FOREACH_RCU(kid, &bus->children, sibling) {
  172. DeviceState *dev = kid->child;
  173. qdev_unrealize(dev);
  174. }
  175. }
  176. if (bc->unrealize) {
  177. bc->unrealize(bus);
  178. }
  179. }
  180. bus->realized = value;
  181. }
  182. static void qbus_initfn(Object *obj)
  183. {
  184. BusState *bus = BUS(obj);
  185. QTAILQ_INIT(&bus->children);
  186. object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
  187. TYPE_HOTPLUG_HANDLER,
  188. (Object **)&bus->hotplug_handler,
  189. object_property_allow_set_link,
  190. 0);
  191. object_property_add_bool(obj, "realized",
  192. bus_get_realized, bus_set_realized);
  193. }
  194. static char *default_bus_get_fw_dev_path(DeviceState *dev)
  195. {
  196. return g_strdup(object_get_typename(OBJECT(dev)));
  197. }
  198. /**
  199. * bus_phases_reset:
  200. * Transition reset method for buses to allow moving
  201. * smoothly from legacy reset method to multi-phases
  202. */
  203. static void bus_phases_reset(BusState *bus)
  204. {
  205. ResettableClass *rc = RESETTABLE_GET_CLASS(bus);
  206. if (rc->phases.enter) {
  207. rc->phases.enter(OBJECT(bus), RESET_TYPE_COLD);
  208. }
  209. if (rc->phases.hold) {
  210. rc->phases.hold(OBJECT(bus));
  211. }
  212. if (rc->phases.exit) {
  213. rc->phases.exit(OBJECT(bus));
  214. }
  215. }
  216. static void bus_transitional_reset(Object *obj)
  217. {
  218. BusClass *bc = BUS_GET_CLASS(obj);
  219. /*
  220. * This will call either @bus_phases_reset (for multi-phases transitioned
  221. * buses) or a bus's specific method for not-yet transitioned buses.
  222. * In both case, it does not reset children.
  223. */
  224. if (bc->reset) {
  225. bc->reset(BUS(obj));
  226. }
  227. }
  228. /**
  229. * bus_get_transitional_reset:
  230. * check if the bus's class is ready for multi-phase
  231. */
  232. static ResettableTrFunction bus_get_transitional_reset(Object *obj)
  233. {
  234. BusClass *dc = BUS_GET_CLASS(obj);
  235. if (dc->reset != bus_phases_reset) {
  236. /*
  237. * dc->reset has been overridden by a subclass,
  238. * the bus is not ready for multi phase yet.
  239. */
  240. return bus_transitional_reset;
  241. }
  242. return NULL;
  243. }
  244. static void bus_class_init(ObjectClass *class, void *data)
  245. {
  246. BusClass *bc = BUS_CLASS(class);
  247. ResettableClass *rc = RESETTABLE_CLASS(class);
  248. class->unparent = bus_unparent;
  249. bc->get_fw_dev_path = default_bus_get_fw_dev_path;
  250. rc->get_state = bus_get_reset_state;
  251. rc->child_foreach = bus_reset_child_foreach;
  252. /*
  253. * @bus_phases_reset is put as the default reset method below, allowing
  254. * to do the multi-phase transition from base classes to leaf classes. It
  255. * allows a legacy-reset Bus class to extend a multi-phases-reset
  256. * Bus class for the following reason:
  257. * + If a base class B has been moved to multi-phase, then it does not
  258. * override this default reset method and may have defined phase methods.
  259. * + A child class C (extending class B) which uses
  260. * bus_class_set_parent_reset() (or similar means) to override the
  261. * reset method will still work as expected. @bus_phases_reset function
  262. * will be registered as the parent reset method and effectively call
  263. * parent reset phases.
  264. */
  265. bc->reset = bus_phases_reset;
  266. rc->get_transitional_function = bus_get_transitional_reset;
  267. }
  268. static void qbus_finalize(Object *obj)
  269. {
  270. BusState *bus = BUS(obj);
  271. g_free(bus->name);
  272. }
  273. static const TypeInfo bus_info = {
  274. .name = TYPE_BUS,
  275. .parent = TYPE_OBJECT,
  276. .instance_size = sizeof(BusState),
  277. .abstract = true,
  278. .class_size = sizeof(BusClass),
  279. .instance_init = qbus_initfn,
  280. .instance_finalize = qbus_finalize,
  281. .class_init = bus_class_init,
  282. .interfaces = (InterfaceInfo[]) {
  283. { TYPE_RESETTABLE_INTERFACE },
  284. { }
  285. },
  286. };
  287. static void bus_register_types(void)
  288. {
  289. type_register_static(&bus_info);
  290. }
  291. type_init(bus_register_types)