qdev-clock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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; it is used by the monitor to show the frequency.
  82. */
  83. object_unref(OBJECT(ncl->clock));
  84. }
  85. g_free(ncl->name);
  86. g_free(ncl);
  87. }
  88. }
  89. Clock *qdev_init_clock_out(DeviceState *dev, const char *name)
  90. {
  91. NamedClockList *ncl;
  92. assert(name);
  93. ncl = qdev_init_clocklist(dev, name, true, NULL);
  94. return ncl->clock;
  95. }
  96. Clock *qdev_init_clock_in(DeviceState *dev, const char *name,
  97. ClockCallback *callback, void *opaque,
  98. unsigned int events)
  99. {
  100. NamedClockList *ncl;
  101. assert(name);
  102. ncl = qdev_init_clocklist(dev, name, false, NULL);
  103. if (callback) {
  104. clock_set_callback(ncl->clock, callback, opaque, events);
  105. }
  106. return ncl->clock;
  107. }
  108. void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks)
  109. {
  110. const struct ClockPortInitElem *elem;
  111. for (elem = &clocks[0]; elem->name != NULL; elem++) {
  112. Clock **clkp;
  113. /* offset cannot be inside the DeviceState part */
  114. assert(elem->offset > sizeof(DeviceState));
  115. clkp = ((void *)dev) + elem->offset;
  116. if (elem->is_output) {
  117. *clkp = qdev_init_clock_out(dev, elem->name);
  118. } else {
  119. *clkp = qdev_init_clock_in(dev, elem->name, elem->callback, dev,
  120. elem->callback_events);
  121. }
  122. }
  123. }
  124. static NamedClockList *qdev_get_clocklist(DeviceState *dev, const char *name)
  125. {
  126. NamedClockList *ncl;
  127. QLIST_FOREACH(ncl, &dev->clocks, node) {
  128. if (strcmp(name, ncl->name) == 0) {
  129. return ncl;
  130. }
  131. }
  132. return NULL;
  133. }
  134. Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
  135. {
  136. NamedClockList *ncl;
  137. assert(name);
  138. ncl = qdev_get_clocklist(dev, name);
  139. if (!ncl) {
  140. error_report("Can not find clock-in '%s' for device type '%s'",
  141. name, object_get_typename(OBJECT(dev)));
  142. abort();
  143. }
  144. assert(!ncl->output);
  145. return ncl->clock;
  146. }
  147. Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
  148. {
  149. NamedClockList *ncl;
  150. assert(name);
  151. ncl = qdev_get_clocklist(dev, name);
  152. if (!ncl) {
  153. error_report("Can not find clock-out '%s' for device type '%s'",
  154. name, object_get_typename(OBJECT(dev)));
  155. abort();
  156. }
  157. assert(ncl->output);
  158. return ncl->clock;
  159. }
  160. Clock *qdev_alias_clock(DeviceState *dev, const char *name,
  161. DeviceState *alias_dev, const char *alias_name)
  162. {
  163. NamedClockList *ncl;
  164. assert(name && alias_name);
  165. ncl = qdev_get_clocklist(dev, name);
  166. qdev_init_clocklist(alias_dev, alias_name, ncl->output, ncl->clock);
  167. return ncl->clock;
  168. }
  169. void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
  170. {
  171. assert(!dev->realized);
  172. clock_set_source(qdev_get_clock_in(dev, name), source);
  173. }