2
0

ptimer.c 14 KB

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