qdev-clock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Device's clock input and output
  3. *
  4. * Copyright GreenSocs 2016-2020
  5. *
  6. * Authors:
  7. * Frederic Konrad
  8. * Damien Hedde
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qemu/error-report.h"
  15. #include "hw/qdev-clock.h"
  16. #include "hw/qdev-core.h"
  17. #include "qapi/error.h"
  18. /*
  19. * qdev_init_clocklist:
  20. * Add a new clock in a device
  21. */
  22. static NamedClockList *qdev_init_clocklist(DeviceState *dev, const char *name,
  23. bool output, Clock *clk)
  24. {
  25. NamedClockList *ncl;
  26. /*
  27. * Clock must be added before realize() so that we can compute the
  28. * clock's canonical path during device_realize().
  29. */
  30. assert(!dev->realized);
  31. /*
  32. * The ncl structure is freed by qdev_finalize_clocklist() which will
  33. * be called during @dev's device_finalize().
  34. */
  35. ncl = g_new0(NamedClockList, 1);
  36. ncl->name = g_strdup(name);
  37. ncl->output = output;
  38. ncl->alias = (clk != NULL);
  39. /*
  40. * Trying to create a clock whose name clashes with some other
  41. * clock or property is a bug in the caller and we will abort().
  42. */
  43. if (clk == NULL) {
  44. clk = CLOCK(object_new(TYPE_CLOCK));
  45. object_property_add_child(OBJECT(dev), name, OBJECT(clk));
  46. if (output) {
  47. /*
  48. * Remove object_new()'s initial reference.
  49. * Note that for inputs, the reference created by object_new()
  50. * will be deleted in qdev_finalize_clocklist().
  51. */
  52. object_unref(OBJECT(clk));
  53. }
  54. } else {
  55. object_property_add_link(OBJECT(dev), name,
  56. object_get_typename(OBJECT(clk)),
  57. (Object **) &ncl->clock,
  58. NULL, OBJ_PROP_LINK_STRONG);
  59. /*
  60. * Since the link property has the OBJ_PROP_LINK_STRONG flag, the clk
  61. * object reference count gets decremented on property deletion.
  62. * However object_property_add_link does not increment it since it
  63. * doesn't know the linked object. Increment it here to ensure the
  64. * aliased clock stays alive during this device life-time.
  65. */
  66. object_ref(OBJECT(clk));
  67. }
  68. ncl->clock = clk;
  69. QLIST_INSERT_HEAD(&dev->clocks, ncl, node);
  70. return ncl;
  71. }
  72. void qdev_finalize_clocklist(DeviceState *dev)
  73. {
  74. /* called by @dev's device_finalize() */
  75. NamedClockList *ncl, *ncl_next;
  76. QLIST_FOREACH_SAFE(ncl, &dev->clocks, node, ncl_next) {
  77. QLIST_REMOVE(ncl, node);
  78. if (!ncl->output && !ncl->alias) {
  79. /*
  80. * We kept a reference on the input clock to ensure it lives up to
  81. * this point so we can safely remove the callback.
  82. * It avoids having a callback to a deleted object if ncl->clock
  83. * is still referenced somewhere else (eg: by a clock output).
  84. */
  85. clock_clear_callback(ncl->clock);
  86. object_unref(OBJECT(ncl->clock));
  87. }
  88. g_free(ncl->name);
  89. g_free(ncl);
  90. }
  91. }
  92. Clock *qdev_init_clock_out(DeviceState *dev, const char *name)
  93. {
  94. NamedClockList *ncl;
  95. assert(name);
  96. ncl = qdev_init_clocklist(dev, name, true, NULL);
  97. return ncl->clock;
  98. }
  99. Clock *qdev_init_clock_in(DeviceState *dev, const char *name,
  100. ClockCallback *callback, void *opaque,
  101. unsigned int events)
  102. {
  103. NamedClockList *ncl;
  104. assert(name);
  105. ncl = qdev_init_clocklist(dev, name, false, NULL);
  106. if (callback) {
  107. clock_set_callback(ncl->clock, callback, opaque, events);
  108. }
  109. return ncl->clock;
  110. }
  111. void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks)
  112. {
  113. const struct ClockPortInitElem *elem;
  114. for (elem = &clocks[0]; elem->name != NULL; elem++) {
  115. Clock **clkp;
  116. /* offset cannot be inside the DeviceState part */
  117. assert(elem->offset > sizeof(DeviceState));
  118. clkp = ((void *)dev) + elem->offset;
  119. if (elem->is_output) {
  120. *clkp = qdev_init_clock_out(dev, elem->name);
  121. } else {
  122. *clkp = qdev_init_clock_in(dev, elem->name, elem->callback, dev,
  123. elem->callback_events);
  124. }
  125. }
  126. }
  127. static NamedClockList *qdev_get_clocklist(DeviceState *dev, const char *name)
  128. {
  129. NamedClockList *ncl;
  130. QLIST_FOREACH(ncl, &dev->clocks, node) {
  131. if (strcmp(name, ncl->name) == 0) {
  132. return ncl;
  133. }
  134. }
  135. return NULL;
  136. }
  137. Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
  138. {
  139. NamedClockList *ncl;
  140. assert(name);
  141. ncl = qdev_get_clocklist(dev, name);
  142. if (!ncl) {
  143. error_report("Can not find clock-in '%s' for device type '%s'",
  144. name, object_get_typename(OBJECT(dev)));
  145. abort();
  146. }
  147. assert(!ncl->output);
  148. return ncl->clock;
  149. }
  150. Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
  151. {
  152. NamedClockList *ncl;
  153. assert(name);
  154. ncl = qdev_get_clocklist(dev, name);
  155. if (!ncl) {
  156. error_report("Can not find clock-out '%s' for device type '%s'",
  157. name, object_get_typename(OBJECT(dev)));
  158. abort();
  159. }
  160. assert(ncl->output);
  161. return ncl->clock;
  162. }
  163. Clock *qdev_alias_clock(DeviceState *dev, const char *name,
  164. DeviceState *alias_dev, const char *alias_name)
  165. {
  166. NamedClockList *ncl;
  167. assert(name && alias_name);
  168. ncl = qdev_get_clocklist(dev, name);
  169. qdev_init_clocklist(alias_dev, alias_name, ncl->output, ncl->clock);
  170. return ncl->clock;
  171. }
  172. void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
  173. {
  174. assert(!dev->realized);
  175. clock_set_source(qdev_get_clock_in(dev, name), source);
  176. }