qdev-clock.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 alias, 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->alias = alias;
  38. ncl->output = output;
  39. ncl->clock = clk;
  40. QLIST_INSERT_HEAD(&dev->clocks, ncl, node);
  41. return ncl;
  42. }
  43. void qdev_finalize_clocklist(DeviceState *dev)
  44. {
  45. /* called by @dev's device_finalize() */
  46. NamedClockList *ncl, *ncl_next;
  47. QLIST_FOREACH_SAFE(ncl, &dev->clocks, node, ncl_next) {
  48. QLIST_REMOVE(ncl, node);
  49. if (!ncl->alias) {
  50. /*
  51. * We kept a reference on the input clock to ensure it lives up to
  52. * this point; it is used by the monitor to show the frequency.
  53. */
  54. object_unref(OBJECT(ncl->clock));
  55. }
  56. g_free(ncl->name);
  57. g_free(ncl);
  58. }
  59. }
  60. Clock *qdev_init_clock_out(DeviceState *dev, const char *name)
  61. {
  62. Clock *clk = CLOCK(object_new(TYPE_CLOCK));
  63. object_property_add_child(OBJECT(dev), name, OBJECT(clk));
  64. qdev_init_clocklist(dev, name, false, true, clk);
  65. return clk;
  66. }
  67. Clock *qdev_init_clock_in(DeviceState *dev, const char *name,
  68. ClockCallback *callback, void *opaque,
  69. unsigned int events)
  70. {
  71. Clock *clk = CLOCK(object_new(TYPE_CLOCK));
  72. object_property_add_child(OBJECT(dev), name, OBJECT(clk));
  73. qdev_init_clocklist(dev, name, false, false, clk);
  74. if (callback) {
  75. clock_set_callback(clk, callback, opaque, events);
  76. }
  77. return clk;
  78. }
  79. void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks)
  80. {
  81. const struct ClockPortInitElem *elem;
  82. for (elem = &clocks[0]; elem->name != NULL; elem++) {
  83. Clock **clkp;
  84. /* offset cannot be inside the DeviceState part */
  85. assert(elem->offset > sizeof(DeviceState));
  86. clkp = ((void *)dev) + elem->offset;
  87. if (elem->is_output) {
  88. *clkp = qdev_init_clock_out(dev, elem->name);
  89. } else {
  90. *clkp = qdev_init_clock_in(dev, elem->name, elem->callback, dev,
  91. elem->callback_events);
  92. }
  93. }
  94. }
  95. static NamedClockList *qdev_get_clocklist(DeviceState *dev, const char *name)
  96. {
  97. NamedClockList *ncl;
  98. QLIST_FOREACH(ncl, &dev->clocks, node) {
  99. if (strcmp(name, ncl->name) == 0) {
  100. return ncl;
  101. }
  102. }
  103. return NULL;
  104. }
  105. Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
  106. {
  107. NamedClockList *ncl;
  108. assert(name);
  109. ncl = qdev_get_clocklist(dev, name);
  110. if (!ncl) {
  111. error_report("Can not find clock-in '%s' for device type '%s'",
  112. name, object_get_typename(OBJECT(dev)));
  113. abort();
  114. }
  115. assert(!ncl->output);
  116. return ncl->clock;
  117. }
  118. Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
  119. {
  120. NamedClockList *ncl;
  121. assert(name);
  122. ncl = qdev_get_clocklist(dev, name);
  123. if (!ncl) {
  124. error_report("Can not find clock-out '%s' for device type '%s'",
  125. name, object_get_typename(OBJECT(dev)));
  126. abort();
  127. }
  128. assert(ncl->output);
  129. return ncl->clock;
  130. }
  131. Clock *qdev_alias_clock(DeviceState *dev, const char *name,
  132. DeviceState *alias_dev, const char *alias_name)
  133. {
  134. NamedClockList *ncl = qdev_get_clocklist(dev, name);
  135. Clock *clk = ncl->clock;
  136. ncl = qdev_init_clocklist(alias_dev, alias_name, true, ncl->output, clk);
  137. object_property_add_link(OBJECT(alias_dev), alias_name,
  138. TYPE_CLOCK,
  139. (Object **) &ncl->clock,
  140. NULL, OBJ_PROP_LINK_STRONG);
  141. /*
  142. * Since the link property has the OBJ_PROP_LINK_STRONG flag, the clk
  143. * object reference count gets decremented on property deletion.
  144. * However object_property_add_link does not increment it since it
  145. * doesn't know the linked object. Increment it here to ensure the
  146. * aliased clock stays alive during this device life-time.
  147. */
  148. object_ref(OBJECT(clk));
  149. return clk;
  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. }