2
0

qdev-clock.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "hw/qdev-clock.h"
  15. #include "hw/qdev-core.h"
  16. #include "qapi/error.h"
  17. /*
  18. * qdev_init_clocklist:
  19. * Add a new clock in a device
  20. */
  21. static NamedClockList *qdev_init_clocklist(DeviceState *dev, const char *name,
  22. bool output, Clock *clk)
  23. {
  24. NamedClockList *ncl;
  25. /*
  26. * Clock must be added before realize() so that we can compute the
  27. * clock's canonical path during device_realize().
  28. */
  29. assert(!dev->realized);
  30. /*
  31. * The ncl structure is freed by qdev_finalize_clocklist() which will
  32. * be called during @dev's device_finalize().
  33. */
  34. ncl = g_new0(NamedClockList, 1);
  35. ncl->name = g_strdup(name);
  36. ncl->output = output;
  37. ncl->alias = (clk != NULL);
  38. /*
  39. * Trying to create a clock whose name clashes with some other
  40. * clock or property is a bug in the caller and we will abort().
  41. */
  42. if (clk == NULL) {
  43. clk = CLOCK(object_new(TYPE_CLOCK));
  44. object_property_add_child(OBJECT(dev), name, OBJECT(clk));
  45. if (output) {
  46. /*
  47. * Remove object_new()'s initial reference.
  48. * Note that for inputs, the reference created by object_new()
  49. * will be deleted in qdev_finalize_clocklist().
  50. */
  51. object_unref(OBJECT(clk));
  52. }
  53. } else {
  54. object_property_add_link(OBJECT(dev), name,
  55. object_get_typename(OBJECT(clk)),
  56. (Object **) &ncl->clock,
  57. NULL, OBJ_PROP_LINK_STRONG);
  58. }
  59. ncl->clock = clk;
  60. QLIST_INSERT_HEAD(&dev->clocks, ncl, node);
  61. return ncl;
  62. }
  63. void qdev_finalize_clocklist(DeviceState *dev)
  64. {
  65. /* called by @dev's device_finalize() */
  66. NamedClockList *ncl, *ncl_next;
  67. QLIST_FOREACH_SAFE(ncl, &dev->clocks, node, ncl_next) {
  68. QLIST_REMOVE(ncl, node);
  69. if (!ncl->output && !ncl->alias) {
  70. /*
  71. * We kept a reference on the input clock to ensure it lives up to
  72. * this point so we can safely remove the callback.
  73. * It avoids having a callback to a deleted object if ncl->clock
  74. * is still referenced somewhere else (eg: by a clock output).
  75. */
  76. clock_clear_callback(ncl->clock);
  77. object_unref(OBJECT(ncl->clock));
  78. }
  79. g_free(ncl->name);
  80. g_free(ncl);
  81. }
  82. }
  83. Clock *qdev_init_clock_out(DeviceState *dev, const char *name)
  84. {
  85. NamedClockList *ncl;
  86. assert(name);
  87. ncl = qdev_init_clocklist(dev, name, true, NULL);
  88. return ncl->clock;
  89. }
  90. Clock *qdev_init_clock_in(DeviceState *dev, const char *name,
  91. ClockCallback *callback, void *opaque)
  92. {
  93. NamedClockList *ncl;
  94. assert(name);
  95. ncl = qdev_init_clocklist(dev, name, false, NULL);
  96. if (callback) {
  97. clock_set_callback(ncl->clock, callback, opaque);
  98. }
  99. return ncl->clock;
  100. }
  101. void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks)
  102. {
  103. const struct ClockPortInitElem *elem;
  104. for (elem = &clocks[0]; elem->name != NULL; elem++) {
  105. Clock **clkp;
  106. /* offset cannot be inside the DeviceState part */
  107. assert(elem->offset > sizeof(DeviceState));
  108. clkp = (Clock **)(((void *) dev) + elem->offset);
  109. if (elem->is_output) {
  110. *clkp = qdev_init_clock_out(dev, elem->name);
  111. } else {
  112. *clkp = qdev_init_clock_in(dev, elem->name, elem->callback, dev);
  113. }
  114. }
  115. }
  116. static NamedClockList *qdev_get_clocklist(DeviceState *dev, const char *name)
  117. {
  118. NamedClockList *ncl;
  119. QLIST_FOREACH(ncl, &dev->clocks, node) {
  120. if (strcmp(name, ncl->name) == 0) {
  121. return ncl;
  122. }
  123. }
  124. return NULL;
  125. }
  126. Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
  127. {
  128. NamedClockList *ncl;
  129. assert(name);
  130. ncl = qdev_get_clocklist(dev, name);
  131. assert(!ncl->output);
  132. return ncl->clock;
  133. }
  134. Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
  135. {
  136. NamedClockList *ncl;
  137. assert(name);
  138. ncl = qdev_get_clocklist(dev, name);
  139. assert(ncl->output);
  140. return ncl->clock;
  141. }
  142. Clock *qdev_alias_clock(DeviceState *dev, const char *name,
  143. DeviceState *alias_dev, const char *alias_name)
  144. {
  145. NamedClockList *ncl;
  146. assert(name && alias_name);
  147. ncl = qdev_get_clocklist(dev, name);
  148. qdev_init_clocklist(alias_dev, alias_name, ncl->output, ncl->clock);
  149. return ncl->clock;
  150. }
  151. void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source)
  152. {
  153. assert(!dev->realized);
  154. clock_set_source(qdev_get_clock_in(dev, name), source);
  155. }