ptimer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * General purpose implementation of a simple periodic countdown timer.
  3. *
  4. * Copyright (c) 2007 CodeSourcery.
  5. *
  6. * This code is licensed under the GNU LGPL.
  7. */
  8. #ifndef PTIMER_H
  9. #define PTIMER_H
  10. #include "qemu/timer.h"
  11. /*
  12. * The ptimer API implements a simple periodic countdown timer.
  13. * The countdown timer has a value (which can be read and written via
  14. * ptimer_get_count() and ptimer_set_count()). When it is enabled
  15. * using ptimer_run(), the value will count downwards at the frequency
  16. * which has been configured using ptimer_set_period() or ptimer_set_freq().
  17. * When it reaches zero it will trigger a callback function, and
  18. * can be set to either reload itself from a specified limit value
  19. * and keep counting down, or to stop (as a one-shot timer).
  20. *
  21. * A transaction-based API is used for modifying ptimer state: all calls
  22. * to functions which modify ptimer state must be between matched calls to
  23. * ptimer_transaction_begin() and ptimer_transaction_commit().
  24. * When ptimer_transaction_commit() is called it will evaluate the state
  25. * of the timer after all the changes in the transaction, and call the
  26. * callback if necessary. (See the ptimer_init() documentation for the full
  27. * list of state-modifying functions and detailed semantics of the callback.)
  28. *
  29. * Forgetting to set the period/frequency (or setting it to zero) is a
  30. * bug in the QEMU device and will cause warning messages to be printed
  31. * to stderr when the guest attempts to enable the timer.
  32. */
  33. /*
  34. * The 'legacy' ptimer policy retains backward compatibility with the
  35. * traditional ptimer behaviour from before policy flags were introduced.
  36. * It has several weird behaviours which don't match typical hardware
  37. * timer behaviour. For a new device using ptimers, you should not
  38. * use PTIMER_POLICY_LEGACY, but instead check the actual behaviour
  39. * that you need and specify the right set of policy flags to get that.
  40. *
  41. * If you are overhauling an existing device that uses PTIMER_POLICY_LEGACY
  42. * and are in a position to check or test the real hardware behaviour,
  43. * consider updating it to specify the right policy flags.
  44. *
  45. * The rough edges of the default policy:
  46. * - Starting to run with a period = 0 emits error message and stops the
  47. * timer without a trigger.
  48. *
  49. * - Setting period to 0 of the running timer emits error message and
  50. * stops the timer without a trigger.
  51. *
  52. * - Starting to run with counter = 0 or setting it to "0" while timer
  53. * is running causes a trigger and reloads counter with a limit value.
  54. * If limit = 0, ptimer emits error message and stops the timer.
  55. *
  56. * - Counter value of the running timer is one less than the actual value.
  57. *
  58. * - Changing period/frequency of the running timer loses time elapsed
  59. * since the last period, effectively restarting the timer with a
  60. * counter = counter value at the moment of change (.i.e. one less).
  61. */
  62. #define PTIMER_POLICY_LEGACY 0
  63. /* Periodic timer counter stays with "0" for a one period before wrapping
  64. * around. */
  65. #define PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD (1 << 0)
  66. /* Running periodic timer that has counter = limit = 0 would continuously
  67. * re-trigger every period. */
  68. #define PTIMER_POLICY_CONTINUOUS_TRIGGER (1 << 1)
  69. /* Starting to run with/setting counter to "0" won't trigger immediately,
  70. * but after a one period for both oneshot and periodic modes. */
  71. #define PTIMER_POLICY_NO_IMMEDIATE_TRIGGER (1 << 2)
  72. /* Starting to run with/setting counter to "0" won't re-load counter
  73. * immediately, but after a one period. */
  74. #define PTIMER_POLICY_NO_IMMEDIATE_RELOAD (1 << 3)
  75. /* Make counter value of the running timer represent the actual value and
  76. * not the one less. */
  77. #define PTIMER_POLICY_NO_COUNTER_ROUND_DOWN (1 << 4)
  78. /*
  79. * Starting to run with a zero counter, or setting the counter to "0" via
  80. * ptimer_set_count() or ptimer_set_limit() will not trigger the timer
  81. * (though it will cause a reload). Only a counter decrement to "0"
  82. * will cause a trigger. Not compatible with NO_IMMEDIATE_TRIGGER;
  83. * ptimer_init() will assert() that you don't set both.
  84. */
  85. #define PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT (1 << 5)
  86. /* ptimer.c */
  87. typedef struct ptimer_state ptimer_state;
  88. typedef void (*ptimer_cb)(void *opaque);
  89. /**
  90. * ptimer_init - Allocate and return a new ptimer
  91. * @callback: function to call on ptimer expiry
  92. * @callback_opaque: opaque pointer passed to @callback
  93. * @policy: PTIMER_POLICY_* bits specifying behaviour
  94. *
  95. * The ptimer returned must be freed using ptimer_free().
  96. *
  97. * If a ptimer is created using this API then will use the
  98. * transaction-based API for modifying ptimer state: all calls
  99. * to functions which modify ptimer state:
  100. * - ptimer_set_period()
  101. * - ptimer_set_freq()
  102. * - ptimer_set_limit()
  103. * - ptimer_set_count()
  104. * - ptimer_run()
  105. * - ptimer_stop()
  106. * must be between matched calls to ptimer_transaction_begin()
  107. * and ptimer_transaction_commit(). When ptimer_transaction_commit()
  108. * is called it will evaluate the state of the timer after all the
  109. * changes in the transaction, and call the callback if necessary.
  110. *
  111. * The callback function is always called from within a transaction
  112. * begin/commit block, so the callback should not call the
  113. * ptimer_transaction_begin() function itself. If the callback changes
  114. * the ptimer state such that another ptimer expiry is triggered, then
  115. * the callback will be called a second time after the first call returns.
  116. */
  117. ptimer_state *ptimer_init(ptimer_cb callback,
  118. void *callback_opaque,
  119. uint8_t policy_mask);
  120. /**
  121. * ptimer_free - Free a ptimer
  122. * @s: timer to free
  123. *
  124. * Free a ptimer created using ptimer_init().
  125. */
  126. void ptimer_free(ptimer_state *s);
  127. /**
  128. * ptimer_transaction_begin() - Start a ptimer modification transaction
  129. *
  130. * This function must be called before making any calls to functions
  131. * which modify the ptimer's state (see the ptimer_init() documentation
  132. * for a list of these), and must always have a matched call to
  133. * ptimer_transaction_commit().
  134. * It is an error to call this function for a BH-based ptimer;
  135. * attempting to do this will trigger an assert.
  136. */
  137. void ptimer_transaction_begin(ptimer_state *s);
  138. /**
  139. * ptimer_transaction_commit() - Commit a ptimer modification transaction
  140. *
  141. * This function must be called after calls to functions which modify
  142. * the ptimer's state, and completes the update of the ptimer. If the
  143. * ptimer state now means that we should trigger the timer expiry
  144. * callback, it will be called directly.
  145. */
  146. void ptimer_transaction_commit(ptimer_state *s);
  147. /**
  148. * ptimer_set_period - Set counter increment interval in nanoseconds
  149. * @s: ptimer to configure
  150. * @period: period of the counter in nanoseconds
  151. *
  152. * Note that if your counter behaviour is specified as having a
  153. * particular frequency rather than a period then ptimer_set_freq()
  154. * may be more appropriate.
  155. *
  156. * This function will assert if it is called outside a
  157. * ptimer_transaction_begin/commit block.
  158. */
  159. void ptimer_set_period(ptimer_state *s, int64_t period);
  160. /**
  161. * ptimer_set_period_from_clock - Set counter increment from a Clock
  162. * @s: ptimer to configure
  163. * @clk: pointer to Clock object to take period from
  164. * @divisor: value to scale the clock frequency down by
  165. *
  166. * If the ptimer is being driven from a Clock, this is the preferred
  167. * way to tell the ptimer about the period, because it avoids any
  168. * possible rounding errors that might happen if the internal
  169. * representation of the Clock period was converted to either a period
  170. * in ns or a frequency in Hz.
  171. *
  172. * If the ptimer should run at the same frequency as the clock,
  173. * pass 1 as the @divisor; if the ptimer should run at half the
  174. * frequency, pass 2, and so on.
  175. *
  176. * This function will assert if it is called outside a
  177. * ptimer_transaction_begin/commit block.
  178. */
  179. void ptimer_set_period_from_clock(ptimer_state *s, const Clock *clock,
  180. unsigned int divisor);
  181. /**
  182. * ptimer_set_freq - Set counter frequency in Hz
  183. * @s: ptimer to configure
  184. * @freq: counter frequency in Hz
  185. *
  186. * This does the same thing as ptimer_set_period(), so you only
  187. * need to call one of them. If the counter behaviour is specified
  188. * as setting the frequency then this function is more appropriate,
  189. * because it allows specifying an effective period which is
  190. * precise to fractions of a nanosecond, avoiding rounding errors.
  191. *
  192. * This function will assert if it is called outside a
  193. * ptimer_transaction_begin/commit block.
  194. */
  195. void ptimer_set_freq(ptimer_state *s, uint32_t freq);
  196. /**
  197. * ptimer_get_limit - Get the configured limit of the ptimer
  198. * @s: ptimer to query
  199. *
  200. * This function returns the current limit (reload) value
  201. * of the down-counter; that is, the value which it will be
  202. * reset to when it hits zero.
  203. *
  204. * Generally timer devices using ptimers should be able to keep
  205. * their reload register state inside the ptimer using the get
  206. * and set limit functions rather than needing to also track it
  207. * in their own state structure.
  208. */
  209. uint64_t ptimer_get_limit(ptimer_state *s);
  210. /**
  211. * ptimer_set_limit - Set the limit of the ptimer
  212. * @s: ptimer
  213. * @limit: initial countdown value
  214. * @reload: if nonzero, then reset the counter to the new limit
  215. *
  216. * Set the limit value of the down-counter. The @reload flag can
  217. * be used to emulate the behaviour of timers which immediately
  218. * reload the counter when their reload register is written to.
  219. *
  220. * This function will assert if it is called outside a
  221. * ptimer_transaction_begin/commit block.
  222. */
  223. void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
  224. /**
  225. * ptimer_get_count - Get the current value of the ptimer
  226. * @s: ptimer
  227. *
  228. * Return the current value of the down-counter. This will
  229. * return the correct value whether the counter is enabled or
  230. * disabled.
  231. */
  232. uint64_t ptimer_get_count(ptimer_state *s);
  233. /**
  234. * ptimer_set_count - Set the current value of the ptimer
  235. * @s: ptimer
  236. * @count: count value to set
  237. *
  238. * Set the value of the down-counter. If the counter is currently
  239. * enabled this will arrange for a timer callback at the appropriate
  240. * point in the future.
  241. *
  242. * This function will assert if it is called outside a
  243. * ptimer_transaction_begin/commit block.
  244. */
  245. void ptimer_set_count(ptimer_state *s, uint64_t count);
  246. /**
  247. * ptimer_run - Start a ptimer counting
  248. * @s: ptimer
  249. * @oneshot: non-zero if this timer should only count down once
  250. *
  251. * Start a ptimer counting down; when it reaches zero the callback function
  252. * passed to ptimer_init() will be invoked.
  253. * If the @oneshot argument is zero,
  254. * the counter value will then be reloaded from the limit and it will
  255. * start counting down again. If @oneshot is non-zero, then the counter
  256. * will disable itself when it reaches zero.
  257. *
  258. * This function will assert if it is called outside a
  259. * ptimer_transaction_begin/commit block.
  260. */
  261. void ptimer_run(ptimer_state *s, int oneshot);
  262. /**
  263. * ptimer_stop - Stop a ptimer counting
  264. * @s: ptimer
  265. *
  266. * Pause a timer (the count stays at its current value until ptimer_run()
  267. * is called to start it counting again).
  268. *
  269. * Note that this can cause it to "lose" time, even if it is immediately
  270. * restarted.
  271. *
  272. * This function will assert if it is called outside a
  273. * ptimer_transaction_begin/commit block.
  274. */
  275. void ptimer_stop(ptimer_state *s);
  276. extern const VMStateDescription vmstate_ptimer;
  277. #define VMSTATE_PTIMER(_field, _state) \
  278. VMSTATE_STRUCT_POINTER_V(_field, _state, 1, vmstate_ptimer, ptimer_state)
  279. #define VMSTATE_PTIMER_ARRAY(_f, _s, _n) \
  280. VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, 0, \
  281. vmstate_ptimer, ptimer_state)
  282. #endif