clock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Hardware Clocks
  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 QEMU_HW_CLOCK_H
  14. #define QEMU_HW_CLOCK_H
  15. #include "qom/object.h"
  16. #include "qemu/queue.h"
  17. #include "qemu/host-utils.h"
  18. #include "qemu/bitops.h"
  19. #define TYPE_CLOCK "clock"
  20. OBJECT_DECLARE_SIMPLE_TYPE(Clock, CLOCK)
  21. /*
  22. * Argument to ClockCallback functions indicating why the callback
  23. * has been called. A mask of these values logically ORed together
  24. * is used to specify which events are interesting when the callback
  25. * is registered, so these values must all be different bit values.
  26. */
  27. typedef enum ClockEvent {
  28. ClockUpdate = 1, /* Clock period has just updated */
  29. ClockPreUpdate = 2, /* Clock period is about to update */
  30. } ClockEvent;
  31. typedef void ClockCallback(void *opaque, ClockEvent event);
  32. /*
  33. * clock store a value representing the clock's period in 2^-32ns unit.
  34. * It can represent:
  35. * + periods from 2^-32ns up to 4seconds
  36. * + frequency from ~0.25Hz 2e10Ghz
  37. * Resolution of frequency representation decreases with frequency:
  38. * + at 100MHz, resolution is ~2mHz
  39. * + at 1Ghz, resolution is ~0.2Hz
  40. * + at 10Ghz, resolution is ~20Hz
  41. */
  42. #define CLOCK_PERIOD_1SEC (1000000000llu << 32)
  43. /*
  44. * macro helpers to convert to hertz / nanosecond
  45. */
  46. #define CLOCK_PERIOD_FROM_NS(ns) ((ns) * (CLOCK_PERIOD_1SEC / 1000000000llu))
  47. #define CLOCK_PERIOD_FROM_HZ(hz) (((hz) != 0) ? CLOCK_PERIOD_1SEC / (hz) : 0u)
  48. #define CLOCK_PERIOD_TO_HZ(per) (((per) != 0) ? CLOCK_PERIOD_1SEC / (per) : 0u)
  49. /**
  50. * Clock:
  51. * @parent_obj: parent class
  52. * @period: unsigned integer representing the period of the clock
  53. * @canonical_path: clock path string cache (used for trace purpose)
  54. * @callback: called when clock changes
  55. * @callback_opaque: argument for @callback
  56. * @callback_events: mask of events when callback should be called
  57. * @source: source (or parent in clock tree) of the clock
  58. * @children: list of clocks connected to this one (it is their source)
  59. * @sibling: structure used to form a clock list
  60. */
  61. struct Clock {
  62. /*< private >*/
  63. Object parent_obj;
  64. /* all fields are private and should not be modified directly */
  65. /* fields */
  66. uint64_t period;
  67. char *canonical_path;
  68. ClockCallback *callback;
  69. void *callback_opaque;
  70. unsigned int callback_events;
  71. /* Ratio of the parent clock to run the child clocks at */
  72. uint32_t multiplier;
  73. uint32_t divider;
  74. /* Clocks are organized in a clock tree */
  75. Clock *source;
  76. QLIST_HEAD(, Clock) children;
  77. QLIST_ENTRY(Clock) sibling;
  78. };
  79. /*
  80. * vmstate description entry to be added in device vmsd.
  81. */
  82. extern const VMStateDescription vmstate_clock;
  83. #define VMSTATE_CLOCK(field, state) \
  84. VMSTATE_CLOCK_V(field, state, 0)
  85. #define VMSTATE_CLOCK_V(field, state, version) \
  86. VMSTATE_STRUCT_POINTER_V(field, state, version, vmstate_clock, Clock)
  87. #define VMSTATE_ARRAY_CLOCK(field, state, num) \
  88. VMSTATE_ARRAY_CLOCK_V(field, state, num, 0)
  89. #define VMSTATE_ARRAY_CLOCK_V(field, state, num, version) \
  90. VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(field, state, num, version, \
  91. vmstate_clock, Clock)
  92. /**
  93. * clock_setup_canonical_path:
  94. * @clk: clock
  95. *
  96. * compute the canonical path of the clock (used by log messages)
  97. */
  98. void clock_setup_canonical_path(Clock *clk);
  99. /**
  100. * clock_new:
  101. * @parent: the clock parent
  102. * @name: the clock object name
  103. *
  104. * Helper function to create a new clock and parent it to @parent. There is no
  105. * need to call clock_setup_canonical_path on the returned clock as it is done
  106. * by this function.
  107. *
  108. * @return the newly created clock
  109. */
  110. Clock *clock_new(Object *parent, const char *name);
  111. /**
  112. * clock_set_callback:
  113. * @clk: the clock to register the callback into
  114. * @cb: the callback function
  115. * @opaque: the argument to the callback
  116. * @events: the events the callback should be called for
  117. * (logical OR of ClockEvent enum values)
  118. *
  119. * Register a callback called on every clock update.
  120. * Note that a clock has only one callback: you cannot register
  121. * different callback functions for different events.
  122. */
  123. void clock_set_callback(Clock *clk, ClockCallback *cb,
  124. void *opaque, unsigned int events);
  125. /**
  126. * clock_set_source:
  127. * @clk: the clock.
  128. * @src: the source clock
  129. *
  130. * Setup @src as the clock source of @clk. The current @src period
  131. * value is also copied to @clk and its subtree but no callback is
  132. * called.
  133. * Further @src update will be propagated to @clk and its subtree.
  134. */
  135. void clock_set_source(Clock *clk, Clock *src);
  136. /**
  137. * clock_has_source:
  138. * @clk: the clock
  139. *
  140. * Returns true if the clock has a source clock connected to it.
  141. * This is useful for devices which have input clocks which must
  142. * be connected by the board/SoC code which creates them. The
  143. * device code can use this to check in its realize method that
  144. * the clock has been connected.
  145. */
  146. static inline bool clock_has_source(const Clock *clk)
  147. {
  148. return clk->source != NULL;
  149. }
  150. /**
  151. * clock_set:
  152. * @clk: the clock to initialize.
  153. * @value: the clock's value, 0 means unclocked
  154. *
  155. * Set the local cached period value of @clk to @value.
  156. *
  157. * @return: true if the clock is changed.
  158. */
  159. bool clock_set(Clock *clk, uint64_t value);
  160. static inline bool clock_set_hz(Clock *clk, unsigned hz)
  161. {
  162. return clock_set(clk, CLOCK_PERIOD_FROM_HZ(hz));
  163. }
  164. static inline bool clock_set_ns(Clock *clk, unsigned ns)
  165. {
  166. return clock_set(clk, CLOCK_PERIOD_FROM_NS(ns));
  167. }
  168. /**
  169. * clock_propagate:
  170. * @clk: the clock
  171. *
  172. * Propagate the clock period that has been previously configured using
  173. * @clock_set(). This will update recursively all connected clocks.
  174. * It is an error to call this function on a clock which has a source.
  175. * Note: this function must not be called during device initialization
  176. * or migration.
  177. */
  178. void clock_propagate(Clock *clk);
  179. /**
  180. * clock_update:
  181. * @clk: the clock to update.
  182. * @value: the new clock's value, 0 means unclocked
  183. *
  184. * Update the @clk to the new @value. All connected clocks will be informed
  185. * of this update. This is equivalent to call @clock_set() then
  186. * @clock_propagate().
  187. */
  188. static inline void clock_update(Clock *clk, uint64_t value)
  189. {
  190. if (clock_set(clk, value)) {
  191. clock_propagate(clk);
  192. }
  193. }
  194. static inline void clock_update_hz(Clock *clk, unsigned hz)
  195. {
  196. clock_update(clk, CLOCK_PERIOD_FROM_HZ(hz));
  197. }
  198. static inline void clock_update_ns(Clock *clk, unsigned ns)
  199. {
  200. clock_update(clk, CLOCK_PERIOD_FROM_NS(ns));
  201. }
  202. /**
  203. * clock_get:
  204. * @clk: the clk to fetch the clock
  205. *
  206. * @return: the current period.
  207. */
  208. static inline uint64_t clock_get(const Clock *clk)
  209. {
  210. return clk->period;
  211. }
  212. static inline unsigned clock_get_hz(Clock *clk)
  213. {
  214. return CLOCK_PERIOD_TO_HZ(clock_get(clk));
  215. }
  216. /**
  217. * clock_ticks_to_ns:
  218. * @clk: the clock to query
  219. * @ticks: number of ticks
  220. *
  221. * Returns the length of time in nanoseconds for this clock
  222. * to tick @ticks times. Because a clock can have a period
  223. * which is not a whole number of nanoseconds, it is important
  224. * to use this function when calculating things like timer
  225. * expiry deadlines, rather than attempting to obtain a "period
  226. * in nanoseconds" value and then multiplying that by a number
  227. * of ticks.
  228. *
  229. * The result could in theory be too large to fit in a 64-bit
  230. * value if the number of ticks and the clock period are both
  231. * large; to avoid overflow the result will be saturated to INT64_MAX
  232. * (because this is the largest valid input to the QEMUTimer APIs).
  233. * Since INT64_MAX nanoseconds is almost 300 years, anything with
  234. * an expiry later than that is in the "will never happen" category
  235. * and callers can reasonably not special-case the saturated result.
  236. */
  237. static inline uint64_t clock_ticks_to_ns(const Clock *clk, uint64_t ticks)
  238. {
  239. uint64_t ns_low, ns_high;
  240. /*
  241. * clk->period is the period in units of 2^-32 ns, so
  242. * (clk->period * ticks) is the required length of time in those
  243. * units, and we can convert to nanoseconds by multiplying by
  244. * 2^32, which is the same as shifting the 128-bit multiplication
  245. * result right by 32.
  246. */
  247. mulu64(&ns_low, &ns_high, clk->period, ticks);
  248. if (ns_high & MAKE_64BIT_MASK(31, 33)) {
  249. return INT64_MAX;
  250. }
  251. return ns_low >> 32 | ns_high << 32;
  252. }
  253. /**
  254. * clock_ns_to_ticks:
  255. * @clk: the clock to query
  256. * @ns: duration in nanoseconds
  257. *
  258. * Returns the number of ticks this clock would make in the given
  259. * number of nanoseconds. Because a clock can have a period which
  260. * is not a whole number of nanoseconds, it is important to use this
  261. * function rather than attempting to obtain a "period in nanoseconds"
  262. * value and then dividing the duration by that value.
  263. *
  264. * If the clock is stopped (ie it has period zero), returns 0.
  265. *
  266. * For some inputs the result could overflow a 64-bit value (because
  267. * the clock's period is short and the duration is long). In these
  268. * cases we truncate the result to a 64-bit value. This is on the
  269. * assumption that generally the result is going to be used to report
  270. * a 32-bit or 64-bit guest register value, so wrapping either cannot
  271. * happen or is the desired behaviour.
  272. */
  273. static inline uint64_t clock_ns_to_ticks(const Clock *clk, uint64_t ns)
  274. {
  275. /*
  276. * ticks = duration_in_ns / period_in_ns
  277. * = ns / (period / 2^32)
  278. * = (ns * 2^32) / period
  279. * The hi, lo inputs to divu128() are (ns << 32) as a 128 bit value.
  280. */
  281. uint64_t lo = ns << 32;
  282. uint64_t hi = ns >> 32;
  283. if (clk->period == 0) {
  284. return 0;
  285. }
  286. divu128(&lo, &hi, clk->period);
  287. return lo;
  288. }
  289. /**
  290. * clock_is_enabled:
  291. * @clk: a clock
  292. *
  293. * @return: true if the clock is running.
  294. */
  295. static inline bool clock_is_enabled(const Clock *clk)
  296. {
  297. return clock_get(clk) != 0;
  298. }
  299. /**
  300. * clock_display_freq: return human-readable representation of clock frequency
  301. * @clk: clock
  302. *
  303. * Return a string which has a human-readable representation of the
  304. * clock's frequency, e.g. "33.3 MHz". This is intended for debug
  305. * and display purposes.
  306. *
  307. * The caller is responsible for freeing the string with g_free().
  308. */
  309. char *clock_display_freq(Clock *clk);
  310. /**
  311. * clock_set_mul_div: set multiplier/divider for child clocks
  312. * @clk: clock
  313. * @multiplier: multiplier value
  314. * @divider: divider value
  315. *
  316. * @return: true if the clock is changed.
  317. *
  318. * By default, a Clock's children will all run with the same period
  319. * as their parent. This function allows you to adjust the multiplier
  320. * and divider used to derive the child clock frequency.
  321. * For example, setting a multiplier of 2 and a divider of 3
  322. * will run child clocks with a period 2/3 of the parent clock,
  323. * so if the parent clock is an 8MHz clock the children will
  324. * be 12MHz.
  325. *
  326. * Setting the multiplier to 0 will stop the child clocks.
  327. * Setting the divider to 0 is a programming error (diagnosed with
  328. * an assertion failure).
  329. * Setting a multiplier value that results in the child period
  330. * overflowing is not diagnosed.
  331. *
  332. * Note that this function does not call clock_propagate(); the
  333. * caller should do that if necessary.
  334. */
  335. bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
  336. #endif /* QEMU_HW_CLOCK_H */