qdev-clock.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef QDEV_CLOCK_H
  14. #define QDEV_CLOCK_H
  15. #include "hw/clock.h"
  16. /**
  17. * qdev_init_clock_in:
  18. * @dev: the device to add an input clock to
  19. * @name: the name of the clock (can't be NULL).
  20. * @callback: optional callback to be called on update or NULL.
  21. * @opaque: argument for the callback
  22. * @events: the events the callback should be called for
  23. * (logical OR of ClockEvent enum values)
  24. * @returns: a pointer to the newly added clock
  25. *
  26. * Add an input clock to device @dev as a clock named @name.
  27. * This adds a child<> property.
  28. * The callback will be called with @opaque as opaque parameter.
  29. */
  30. Clock *qdev_init_clock_in(DeviceState *dev, const char *name,
  31. ClockCallback *callback, void *opaque,
  32. unsigned int events);
  33. /**
  34. * qdev_init_clock_out:
  35. * @dev: the device to add an output clock to
  36. * @name: the name of the clock (can't be NULL).
  37. * @returns: a pointer to the newly added clock
  38. *
  39. * Add an output clock to device @dev as a clock named @name.
  40. * This adds a child<> property.
  41. */
  42. Clock *qdev_init_clock_out(DeviceState *dev, const char *name);
  43. /**
  44. * qdev_get_clock_in:
  45. * @dev: the device which has the clock
  46. * @name: the name of the clock (can't be NULL).
  47. * @returns: a pointer to the clock
  48. *
  49. * Get the input clock @name from @dev or NULL if does not exist.
  50. */
  51. Clock *qdev_get_clock_in(DeviceState *dev, const char *name);
  52. /**
  53. * qdev_get_clock_out:
  54. * @dev: the device which has the clock
  55. * @name: the name of the clock (can't be NULL).
  56. * @returns: a pointer to the clock
  57. *
  58. * Get the output clock @name from @dev or NULL if does not exist.
  59. */
  60. Clock *qdev_get_clock_out(DeviceState *dev, const char *name);
  61. /**
  62. * qdev_connect_clock_in:
  63. * @dev: a device
  64. * @name: the name of an input clock in @dev
  65. * @source: the source clock (an output clock of another device for example)
  66. *
  67. * Set the source clock of input clock @name of device @dev to @source.
  68. * @source period update will be propagated to @name clock.
  69. *
  70. * Must be called before @dev is realized.
  71. */
  72. void qdev_connect_clock_in(DeviceState *dev, const char *name, Clock *source);
  73. /**
  74. * qdev_alias_clock:
  75. * @dev: the device which has the clock
  76. * @name: the name of the clock in @dev (can't be NULL)
  77. * @alias_dev: the device to add the clock
  78. * @alias_name: the name of the clock in @container
  79. * @returns: a pointer to the clock
  80. *
  81. * Add a clock @alias_name in @alias_dev which is an alias of the clock @name
  82. * in @dev. The direction _in_ or _out_ will the same as the original.
  83. * An alias clock must not be modified or used by @alias_dev and should
  84. * typically be only only for device composition purpose.
  85. */
  86. Clock *qdev_alias_clock(DeviceState *dev, const char *name,
  87. DeviceState *alias_dev, const char *alias_name);
  88. /**
  89. * qdev_finalize_clocklist:
  90. * @dev: the device being finalized
  91. *
  92. * Clear the clocklist from @dev. Only used internally in qdev.
  93. */
  94. void qdev_finalize_clocklist(DeviceState *dev);
  95. /**
  96. * ClockPortInitElem:
  97. * @name: name of the clock (can't be NULL)
  98. * @output: indicates whether the clock is input or output
  99. * @callback: for inputs, optional callback to be called on clock's update
  100. * with device as opaque
  101. * @callback_events: mask of ClockEvent values for when callback is called
  102. * @offset: optional offset to store the ClockIn or ClockOut pointer in device
  103. * state structure (0 means unused)
  104. */
  105. struct ClockPortInitElem {
  106. const char *name;
  107. bool is_output;
  108. ClockCallback *callback;
  109. unsigned int callback_events;
  110. size_t offset;
  111. };
  112. #define clock_offset_value(devstate, field) \
  113. (offsetof(devstate, field) + \
  114. type_check(Clock *, typeof_field(devstate, field)))
  115. #define QDEV_CLOCK(out_not_in, devstate, field, cb, cbevents) { \
  116. .name = (stringify(field)), \
  117. .is_output = out_not_in, \
  118. .callback = cb, \
  119. .callback_events = cbevents, \
  120. .offset = clock_offset_value(devstate, field), \
  121. }
  122. /**
  123. * QDEV_CLOCK_(IN|OUT):
  124. * @devstate: structure type. @dev argument of qdev_init_clocks below must be
  125. * a pointer to that same type.
  126. * @field: a field in @_devstate (must be Clock*)
  127. * @callback: (for input only) callback (or NULL) to be called with the device
  128. * state as argument
  129. * @cbevents: (for input only) ClockEvent mask for when callback is called
  130. *
  131. * The name of the clock will be derived from @field
  132. */
  133. #define QDEV_CLOCK_IN(devstate, field, callback, cbevents) \
  134. QDEV_CLOCK(false, devstate, field, callback, cbevents)
  135. #define QDEV_CLOCK_OUT(devstate, field) \
  136. QDEV_CLOCK(true, devstate, field, NULL, 0)
  137. #define QDEV_CLOCK_END { .name = NULL }
  138. typedef struct ClockPortInitElem ClockPortInitArray[];
  139. /**
  140. * qdev_init_clocks:
  141. * @dev: the device to add clocks to
  142. * @clocks: a QDEV_CLOCK_END-terminated array which contains the
  143. * clocks information.
  144. */
  145. void qdev_init_clocks(DeviceState *dev, const ClockPortInitArray clocks);
  146. #endif /* QDEV_CLOCK_H */