ptimer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 "hw/ptimer.h"
  10. #include "migration/vmstate.h"
  11. #include "qemu/host-utils.h"
  12. #include "sysemu/replay.h"
  13. #include "sysemu/cpu-timers.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) &&
  122. !icount_enabled() && !qtest_enabled()) {
  123. period = 10000 / delta;
  124. period_frac = 0;
  125. }
  126. s->last_event = s->next_event;
  127. s->next_event = s->last_event + delta * period;
  128. if (period_frac) {
  129. s->next_event += ((int64_t)period_frac * delta) >> 32;
  130. }
  131. timer_mod(s->timer, s->next_event);
  132. }
  133. static void ptimer_tick(void *opaque)
  134. {
  135. ptimer_state *s = (ptimer_state *)opaque;
  136. bool trigger = true;
  137. /*
  138. * We perform all the tick actions within a begin/commit block
  139. * because the callback function that ptimer_trigger() calls
  140. * might make calls into the ptimer APIs that provoke another
  141. * trigger, and we want that to cause the callback function
  142. * to be called iteratively, not recursively.
  143. */
  144. ptimer_transaction_begin(s);
  145. if (s->enabled == 2) {
  146. s->delta = 0;
  147. s->enabled = 0;
  148. } else {
  149. int delta_adjust = DELTA_ADJUST;
  150. if (s->delta == 0 || s->limit == 0) {
  151. /* If a "continuous trigger" policy is not used and limit == 0,
  152. we should error out. delta == 0 means that this tick is
  153. caused by a "no immediate reload" policy, so it shouldn't
  154. be adjusted. */
  155. delta_adjust = DELTA_NO_ADJUST;
  156. }
  157. if (!(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
  158. /* Avoid re-trigger on deferred reload if "no immediate trigger"
  159. policy isn't used. */
  160. trigger = (delta_adjust == DELTA_ADJUST);
  161. }
  162. s->delta = s->limit;
  163. ptimer_reload(s, delta_adjust);
  164. }
  165. if (trigger) {
  166. ptimer_trigger(s);
  167. }
  168. ptimer_transaction_commit(s);
  169. }
  170. uint64_t ptimer_get_count(ptimer_state *s)
  171. {
  172. uint64_t counter;
  173. if (s->enabled && s->delta != 0) {
  174. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  175. int64_t next = s->next_event;
  176. int64_t last = s->last_event;
  177. bool expired = (now - next >= 0);
  178. bool oneshot = (s->enabled == 2);
  179. /* Figure out the current counter value. */
  180. if (expired) {
  181. /* Prevent timer underflowing if it should already have
  182. triggered. */
  183. counter = 0;
  184. } else {
  185. uint64_t rem;
  186. uint64_t div;
  187. int clz1, clz2;
  188. int shift;
  189. uint32_t period_frac = s->period_frac;
  190. uint64_t period = s->period;
  191. if (!oneshot && (s->delta * period < 10000) &&
  192. !icount_enabled() && !qtest_enabled()) {
  193. period = 10000 / s->delta;
  194. period_frac = 0;
  195. }
  196. /* We need to divide time by period, where time is stored in
  197. rem (64-bit integer) and period is stored in period/period_frac
  198. (64.32 fixed point).
  199. Doing full precision division is hard, so scale values and
  200. do a 64-bit division. The result should be rounded down,
  201. so that the rounding error never causes the timer to go
  202. backwards.
  203. */
  204. rem = next - now;
  205. div = period;
  206. clz1 = clz64(rem);
  207. clz2 = clz64(div);
  208. shift = clz1 < clz2 ? clz1 : clz2;
  209. rem <<= shift;
  210. div <<= shift;
  211. if (shift >= 32) {
  212. div |= ((uint64_t)period_frac << (shift - 32));
  213. } else {
  214. if (shift != 0)
  215. div |= (period_frac >> (32 - shift));
  216. /* Look at remaining bits of period_frac and round div up if
  217. necessary. */
  218. if ((uint32_t)(period_frac << shift))
  219. div += 1;
  220. }
  221. counter = rem / div;
  222. if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) {
  223. /* Before wrapping around, timer should stay with counter = 0
  224. for a one period. */
  225. if (!oneshot && s->delta == s->limit) {
  226. if (now == last) {
  227. /* Counter == delta here, check whether it was
  228. adjusted and if it was, then right now it is
  229. that "one period". */
  230. if (counter == s->limit + DELTA_ADJUST) {
  231. return 0;
  232. }
  233. } else if (counter == s->limit) {
  234. /* Since the counter is rounded down and now != last,
  235. the counter == limit means that delta was adjusted
  236. by +1 and right now it is that adjusted period. */
  237. return 0;
  238. }
  239. }
  240. }
  241. }
  242. if (s->policy_mask & PTIMER_POLICY_NO_COUNTER_ROUND_DOWN) {
  243. /* If now == last then delta == limit, i.e. the counter already
  244. represents the correct value. It would be rounded down a 1ns
  245. later. */
  246. if (now != last) {
  247. counter += 1;
  248. }
  249. }
  250. } else {
  251. counter = s->delta;
  252. }
  253. return counter;
  254. }
  255. void ptimer_set_count(ptimer_state *s, uint64_t count)
  256. {
  257. assert(s->in_transaction);
  258. s->delta = count;
  259. if (s->enabled) {
  260. s->need_reload = true;
  261. }
  262. }
  263. void ptimer_run(ptimer_state *s, int oneshot)
  264. {
  265. bool was_disabled = !s->enabled;
  266. assert(s->in_transaction);
  267. if (was_disabled && s->period == 0) {
  268. if (!qtest_enabled()) {
  269. fprintf(stderr, "Timer with period zero, disabling\n");
  270. }
  271. return;
  272. }
  273. s->enabled = oneshot ? 2 : 1;
  274. if (was_disabled) {
  275. s->need_reload = true;
  276. }
  277. }
  278. /* Pause a timer. Note that this may cause it to "lose" time, even if it
  279. is immediately restarted. */
  280. void ptimer_stop(ptimer_state *s)
  281. {
  282. assert(s->in_transaction);
  283. if (!s->enabled)
  284. return;
  285. s->delta = ptimer_get_count(s);
  286. timer_del(s->timer);
  287. s->enabled = 0;
  288. s->need_reload = false;
  289. }
  290. /* Set counter increment interval in nanoseconds. */
  291. void ptimer_set_period(ptimer_state *s, int64_t period)
  292. {
  293. assert(s->in_transaction);
  294. s->delta = ptimer_get_count(s);
  295. s->period = period;
  296. s->period_frac = 0;
  297. if (s->enabled) {
  298. s->need_reload = true;
  299. }
  300. }
  301. /* Set counter frequency in Hz. */
  302. void ptimer_set_freq(ptimer_state *s, uint32_t freq)
  303. {
  304. assert(s->in_transaction);
  305. s->delta = ptimer_get_count(s);
  306. s->period = 1000000000ll / freq;
  307. s->period_frac = (1000000000ll << 32) / freq;
  308. if (s->enabled) {
  309. s->need_reload = true;
  310. }
  311. }
  312. /* Set the initial countdown value. If reload is nonzero then also set
  313. count = limit. */
  314. void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
  315. {
  316. assert(s->in_transaction);
  317. s->limit = limit;
  318. if (reload)
  319. s->delta = limit;
  320. if (s->enabled && reload) {
  321. s->need_reload = true;
  322. }
  323. }
  324. uint64_t ptimer_get_limit(ptimer_state *s)
  325. {
  326. return s->limit;
  327. }
  328. void ptimer_transaction_begin(ptimer_state *s)
  329. {
  330. assert(!s->in_transaction);
  331. s->in_transaction = true;
  332. s->need_reload = false;
  333. }
  334. void ptimer_transaction_commit(ptimer_state *s)
  335. {
  336. assert(s->in_transaction);
  337. /*
  338. * We must loop here because ptimer_reload() can call the callback
  339. * function, which might then update ptimer state in a way that
  340. * means we need to do another reload and possibly another callback.
  341. * A disabled timer never needs reloading (and if we don't check
  342. * this then we loop forever if ptimer_reload() disables the timer).
  343. */
  344. while (s->need_reload && s->enabled) {
  345. s->need_reload = false;
  346. s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  347. ptimer_reload(s, 0);
  348. }
  349. /* Now we've finished reload we can leave the transaction block. */
  350. s->in_transaction = false;
  351. }
  352. const VMStateDescription vmstate_ptimer = {
  353. .name = "ptimer",
  354. .version_id = 1,
  355. .minimum_version_id = 1,
  356. .fields = (VMStateField[]) {
  357. VMSTATE_UINT8(enabled, ptimer_state),
  358. VMSTATE_UINT64(limit, ptimer_state),
  359. VMSTATE_UINT64(delta, ptimer_state),
  360. VMSTATE_UINT32(period_frac, ptimer_state),
  361. VMSTATE_INT64(period, ptimer_state),
  362. VMSTATE_INT64(last_event, ptimer_state),
  363. VMSTATE_INT64(next_event, ptimer_state),
  364. VMSTATE_TIMER_PTR(timer, ptimer_state),
  365. VMSTATE_END_OF_LIST()
  366. }
  367. };
  368. ptimer_state *ptimer_init(ptimer_cb callback, void *callback_opaque,
  369. uint8_t policy_mask)
  370. {
  371. ptimer_state *s;
  372. /* The callback function is mandatory. */
  373. assert(callback);
  374. s = g_new0(ptimer_state, 1);
  375. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s);
  376. s->policy_mask = policy_mask;
  377. s->callback = callback;
  378. s->callback_opaque = callback_opaque;
  379. /*
  380. * These two policies are incompatible -- trigger-on-decrement implies
  381. * a timer trigger when the count becomes 0, but no-immediate-trigger
  382. * implies a trigger when the count stops being 0.
  383. */
  384. assert(!((policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) &&
  385. (policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)));
  386. return s;
  387. }
  388. void ptimer_free(ptimer_state *s)
  389. {
  390. timer_free(s->timer);
  391. g_free(s);
  392. }