ptimer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. #include "qemu/osdep.h"
  9. #include "qemu/timer.h"
  10. #include "hw/ptimer.h"
  11. #include "migration/vmstate.h"
  12. #include "qemu/host-utils.h"
  13. #include "sysemu/replay.h"
  14. #include "sysemu/qtest.h"
  15. #include "block/aio.h"
  16. #include "sysemu/cpus.h"
  17. #define DELTA_ADJUST 1
  18. #define DELTA_NO_ADJUST -1
  19. struct ptimer_state
  20. {
  21. uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
  22. uint64_t limit;
  23. uint64_t delta;
  24. uint32_t period_frac;
  25. int64_t period;
  26. int64_t last_event;
  27. int64_t next_event;
  28. uint8_t policy_mask;
  29. QEMUTimer *timer;
  30. ptimer_cb callback;
  31. void *callback_opaque;
  32. /*
  33. * These track whether we're in a transaction block, and if we
  34. * need to do a timer reload when the block finishes. They don't
  35. * need to be migrated because migration can never happen in the
  36. * middle of a transaction block.
  37. */
  38. bool in_transaction;
  39. bool need_reload;
  40. };
  41. /* Use a bottom-half routine to avoid reentrancy issues. */
  42. static void ptimer_trigger(ptimer_state *s)
  43. {
  44. s->callback(s->callback_opaque);
  45. }
  46. static void ptimer_reload(ptimer_state *s, int delta_adjust)
  47. {
  48. uint32_t period_frac;
  49. uint64_t period;
  50. uint64_t delta;
  51. bool suppress_trigger = false;
  52. /*
  53. * Note that if delta_adjust is 0 then we must be here because of
  54. * a count register write or timer start, not because of timer expiry.
  55. * In that case the policy might require us to suppress the timer trigger
  56. * that we would otherwise generate for a zero delta.
  57. */
  58. if (delta_adjust == 0 &&
  59. (s->policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT)) {
  60. suppress_trigger = true;
  61. }
  62. if (s->delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)
  63. && !suppress_trigger) {
  64. ptimer_trigger(s);
  65. }
  66. /*
  67. * Note that ptimer_trigger() might call the device callback function,
  68. * which can then modify timer state, so we must not cache any fields
  69. * from ptimer_state until after we have called it.
  70. */
  71. delta = s->delta;
  72. period = s->period;
  73. period_frac = s->period_frac;
  74. if (delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) {
  75. delta = s->delta = s->limit;
  76. }
  77. if (s->period == 0) {
  78. if (!qtest_enabled()) {
  79. fprintf(stderr, "Timer with period zero, disabling\n");
  80. }
  81. timer_del(s->timer);
  82. s->enabled = 0;
  83. return;
  84. }
  85. if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) {
  86. if (delta_adjust != DELTA_NO_ADJUST) {
  87. delta += delta_adjust;
  88. }
  89. }
  90. if (delta == 0 && (s->policy_mask & PTIMER_POLICY_CONTINUOUS_TRIGGER)) {
  91. if (s->enabled == 1 && s->limit == 0) {
  92. delta = 1;
  93. }
  94. }
  95. if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
  96. if (delta_adjust != DELTA_NO_ADJUST) {
  97. delta = 1;
  98. }
  99. }
  100. if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) {
  101. if (s->enabled == 1 && s->limit != 0) {
  102. delta = 1;
  103. }
  104. }
  105. if (delta == 0) {
  106. if (!qtest_enabled()) {
  107. fprintf(stderr, "Timer with delta zero, disabling\n");
  108. }
  109. timer_del(s->timer);
  110. s->enabled = 0;
  111. return;
  112. }
  113. /*
  114. * Artificially limit timeout rate to something
  115. * achievable under QEMU. Otherwise, QEMU spends all
  116. * its time generating timer interrupts, and there
  117. * is no forward progress.
  118. * About ten microseconds is the fastest that really works
  119. * on the current generation of host machines.
  120. */
  121. if (s->enabled == 1 && (delta * period < 10000) && !use_icount) {
  122. period = 10000 / delta;
  123. period_frac = 0;
  124. }
  125. s->last_event = s->next_event;
  126. s->next_event = s->last_event + delta * period;
  127. if (period_frac) {
  128. s->next_event += ((int64_t)period_frac * delta) >> 32;
  129. }
  130. timer_mod(s->timer, s->next_event);
  131. }
  132. static void ptimer_tick(void *opaque)
  133. {
  134. ptimer_state *s = (ptimer_state *)opaque;
  135. bool trigger = true;
  136. /*
  137. * We perform all the tick actions within a begin/commit block
  138. * because the callback function that ptimer_trigger() calls
  139. * might make calls into the ptimer APIs that provoke another
  140. * trigger, and we want that to cause the callback function
  141. * to be called iteratively, not recursively.
  142. */
  143. ptimer_transaction_begin(s);
  144. if (s->enabled == 2) {
  145. s->delta = 0;
  146. s->enabled = 0;
  147. } else {
  148. int delta_adjust = DELTA_ADJUST;
  149. if (s->delta == 0 || s->limit == 0) {
  150. /* If a "continuous trigger" policy is not used and limit == 0,
  151. we should error out. delta == 0 means that this tick is
  152. caused by a "no immediate reload" policy, so it shouldn't
  153. be adjusted. */
  154. delta_adjust = DELTA_NO_ADJUST;
  155. }
  156. if (!(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
  157. /* Avoid re-trigger on deferred reload if "no immediate trigger"
  158. policy isn't used. */
  159. trigger = (delta_adjust == DELTA_ADJUST);
  160. }
  161. s->delta = s->limit;
  162. ptimer_reload(s, delta_adjust);
  163. }
  164. if (trigger) {
  165. ptimer_trigger(s);
  166. }
  167. ptimer_transaction_commit(s);
  168. }
  169. uint64_t ptimer_get_count(ptimer_state *s)
  170. {
  171. uint64_t counter;
  172. if (s->enabled && s->delta != 0) {
  173. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  174. int64_t next = s->next_event;
  175. int64_t last = s->last_event;
  176. bool expired = (now - next >= 0);
  177. bool oneshot = (s->enabled == 2);
  178. /* Figure out the current counter value. */
  179. if (expired) {
  180. /* Prevent timer underflowing if it should already have
  181. triggered. */
  182. counter = 0;
  183. } else {
  184. uint64_t rem;
  185. uint64_t div;
  186. int clz1, clz2;
  187. int shift;
  188. uint32_t period_frac = s->period_frac;
  189. uint64_t period = s->period;
  190. if (!oneshot && (s->delta * period < 10000) && !use_icount) {
  191. period = 10000 / s->delta;
  192. period_frac = 0;
  193. }
  194. /* We need to divide time by period, where time is stored in
  195. rem (64-bit integer) and period is stored in period/period_frac
  196. (64.32 fixed point).
  197. Doing full precision division is hard, so scale values and
  198. do a 64-bit division. The result should be rounded down,
  199. so that the rounding error never causes the timer to go
  200. backwards.
  201. */
  202. rem = next - now;
  203. div = period;
  204. clz1 = clz64(rem);
  205. clz2 = clz64(div);
  206. shift = clz1 < clz2 ? clz1 : clz2;
  207. rem <<= shift;
  208. div <<= shift;
  209. if (shift >= 32) {
  210. div |= ((uint64_t)period_frac << (shift - 32));
  211. } else {
  212. if (shift != 0)
  213. div |= (period_frac >> (32 - shift));
  214. /* Look at remaining bits of period_frac and round div up if
  215. necessary. */
  216. if ((uint32_t)(period_frac << shift))
  217. div += 1;
  218. }
  219. counter = rem / div;
  220. if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) {
  221. /* Before wrapping around, timer should stay with counter = 0
  222. for a one period. */
  223. if (!oneshot && s->delta == s->limit) {
  224. if (now == last) {
  225. /* Counter == delta here, check whether it was
  226. adjusted and if it was, then right now it is
  227. that "one period". */
  228. if (counter == s->limit + DELTA_ADJUST) {
  229. return 0;
  230. }
  231. } else if (counter == s->limit) {
  232. /* Since the counter is rounded down and now != last,
  233. the counter == limit means that delta was adjusted
  234. by +1 and right now it is that adjusted period. */
  235. return 0;
  236. }
  237. }
  238. }
  239. }
  240. if (s->policy_mask & PTIMER_POLICY_NO_COUNTER_ROUND_DOWN) {
  241. /* If now == last then delta == limit, i.e. the counter already
  242. represents the correct value. It would be rounded down a 1ns
  243. later. */
  244. if (now != last) {
  245. counter += 1;
  246. }
  247. }
  248. } else {
  249. counter = s->delta;
  250. }
  251. return counter;
  252. }
  253. void ptimer_set_count(ptimer_state *s, uint64_t count)
  254. {
  255. assert(s->in_transaction);
  256. s->delta = count;
  257. if (s->enabled) {
  258. s->need_reload = true;
  259. }
  260. }
  261. void ptimer_run(ptimer_state *s, int oneshot)
  262. {
  263. bool was_disabled = !s->enabled;
  264. assert(s->in_transaction);
  265. if (was_disabled && s->period == 0) {
  266. if (!qtest_enabled()) {
  267. fprintf(stderr, "Timer with period zero, disabling\n");
  268. }
  269. return;
  270. }
  271. s->enabled = oneshot ? 2 : 1;
  272. if (was_disabled) {
  273. s->need_reload = true;
  274. }
  275. }
  276. /* Pause a timer. Note that this may cause it to "lose" time, even if it
  277. is immediately restarted. */
  278. void ptimer_stop(ptimer_state *s)
  279. {
  280. assert(s->in_transaction);
  281. if (!s->enabled)
  282. return;
  283. s->delta = ptimer_get_count(s);
  284. timer_del(s->timer);
  285. s->enabled = 0;
  286. s->need_reload = false;
  287. }
  288. /* Set counter increment interval in nanoseconds. */
  289. void ptimer_set_period(ptimer_state *s, int64_t period)
  290. {
  291. assert(s->in_transaction);
  292. s->delta = ptimer_get_count(s);
  293. s->period = period;
  294. s->period_frac = 0;
  295. if (s->enabled) {
  296. s->need_reload = true;
  297. }
  298. }
  299. /* Set counter frequency in Hz. */
  300. void ptimer_set_freq(ptimer_state *s, uint32_t freq)
  301. {
  302. assert(s->in_transaction);
  303. s->delta = ptimer_get_count(s);
  304. s->period = 1000000000ll / freq;
  305. s->period_frac = (1000000000ll << 32) / freq;
  306. if (s->enabled) {
  307. s->need_reload = true;
  308. }
  309. }
  310. /* Set the initial countdown value. If reload is nonzero then also set
  311. count = limit. */
  312. void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
  313. {
  314. assert(s->in_transaction);
  315. s->limit = limit;
  316. if (reload)
  317. s->delta = limit;
  318. if (s->enabled && reload) {
  319. s->need_reload = true;
  320. }
  321. }
  322. uint64_t ptimer_get_limit(ptimer_state *s)
  323. {
  324. return s->limit;
  325. }
  326. void ptimer_transaction_begin(ptimer_state *s)
  327. {
  328. assert(!s->in_transaction);
  329. s->in_transaction = true;
  330. s->need_reload = false;
  331. }
  332. void ptimer_transaction_commit(ptimer_state *s)
  333. {
  334. assert(s->in_transaction);
  335. /*
  336. * We must loop here because ptimer_reload() can call the callback
  337. * function, which might then update ptimer state in a way that
  338. * means we need to do another reload and possibly another callback.
  339. * A disabled timer never needs reloading (and if we don't check
  340. * this then we loop forever if ptimer_reload() disables the timer).
  341. */
  342. while (s->need_reload && s->enabled) {
  343. s->need_reload = false;
  344. s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  345. ptimer_reload(s, 0);
  346. }
  347. /* Now we've finished reload we can leave the transaction block. */
  348. s->in_transaction = false;
  349. }
  350. const VMStateDescription vmstate_ptimer = {
  351. .name = "ptimer",
  352. .version_id = 1,
  353. .minimum_version_id = 1,
  354. .fields = (VMStateField[]) {
  355. VMSTATE_UINT8(enabled, ptimer_state),
  356. VMSTATE_UINT64(limit, ptimer_state),
  357. VMSTATE_UINT64(delta, ptimer_state),
  358. VMSTATE_UINT32(period_frac, ptimer_state),
  359. VMSTATE_INT64(period, ptimer_state),
  360. VMSTATE_INT64(last_event, ptimer_state),
  361. VMSTATE_INT64(next_event, ptimer_state),
  362. VMSTATE_TIMER_PTR(timer, ptimer_state),
  363. VMSTATE_END_OF_LIST()
  364. }
  365. };
  366. ptimer_state *ptimer_init(ptimer_cb callback, void *callback_opaque,
  367. uint8_t policy_mask)
  368. {
  369. ptimer_state *s;
  370. /* The callback function is mandatory. */
  371. assert(callback);
  372. s = g_new0(ptimer_state, 1);
  373. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s);
  374. s->policy_mask = policy_mask;
  375. s->callback = callback;
  376. s->callback_opaque = callback_opaque;
  377. /*
  378. * These two policies are incompatible -- trigger-on-decrement implies
  379. * a timer trigger when the count becomes 0, but no-immediate-trigger
  380. * implies a trigger when the count stops being 0.
  381. */
  382. assert(!((policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) &&
  383. (policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)));
  384. return s;
  385. }
  386. void ptimer_free(ptimer_state *s)
  387. {
  388. timer_free(s->timer);
  389. g_free(s);
  390. }