2
0

ptimer.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "hw.h"
  9. #include "qemu-timer.h"
  10. #include "host-utils.h"
  11. struct ptimer_state
  12. {
  13. uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
  14. uint64_t limit;
  15. uint64_t delta;
  16. uint32_t period_frac;
  17. int64_t period;
  18. int64_t last_event;
  19. int64_t next_event;
  20. QEMUBH *bh;
  21. QEMUTimer *timer;
  22. };
  23. /* Use a bottom-half routine to avoid reentrancy issues. */
  24. static void ptimer_trigger(ptimer_state *s)
  25. {
  26. if (s->bh) {
  27. qemu_bh_schedule(s->bh);
  28. }
  29. }
  30. static void ptimer_reload(ptimer_state *s)
  31. {
  32. if (s->delta == 0) {
  33. ptimer_trigger(s);
  34. s->delta = s->limit;
  35. }
  36. if (s->delta == 0 || s->period == 0) {
  37. fprintf(stderr, "Timer with period zero, disabling\n");
  38. s->enabled = 0;
  39. return;
  40. }
  41. s->last_event = s->next_event;
  42. s->next_event = s->last_event + s->delta * s->period;
  43. if (s->period_frac) {
  44. s->next_event += ((int64_t)s->period_frac * s->delta) >> 32;
  45. }
  46. qemu_mod_timer(s->timer, s->next_event);
  47. }
  48. static void ptimer_tick(void *opaque)
  49. {
  50. ptimer_state *s = (ptimer_state *)opaque;
  51. ptimer_trigger(s);
  52. s->delta = 0;
  53. if (s->enabled == 2) {
  54. s->enabled = 0;
  55. } else {
  56. ptimer_reload(s);
  57. }
  58. }
  59. uint64_t ptimer_get_count(ptimer_state *s)
  60. {
  61. int64_t now;
  62. uint64_t counter;
  63. if (s->enabled) {
  64. now = qemu_get_clock_ns(vm_clock);
  65. /* Figure out the current counter value. */
  66. if (now - s->next_event > 0
  67. || s->period == 0) {
  68. /* Prevent timer underflowing if it should already have
  69. triggered. */
  70. counter = 0;
  71. } else {
  72. uint64_t rem;
  73. uint64_t div;
  74. int clz1, clz2;
  75. int shift;
  76. /* We need to divide time by period, where time is stored in
  77. rem (64-bit integer) and period is stored in period/period_frac
  78. (64.32 fixed point).
  79. Doing full precision division is hard, so scale values and
  80. do a 64-bit division. The result should be rounded down,
  81. so that the rounding error never causes the timer to go
  82. backwards.
  83. */
  84. rem = s->next_event - now;
  85. div = s->period;
  86. clz1 = clz64(rem);
  87. clz2 = clz64(div);
  88. shift = clz1 < clz2 ? clz1 : clz2;
  89. rem <<= shift;
  90. div <<= shift;
  91. if (shift >= 32) {
  92. div |= ((uint64_t)s->period_frac << (shift - 32));
  93. } else {
  94. if (shift != 0)
  95. div |= (s->period_frac >> (32 - shift));
  96. /* Look at remaining bits of period_frac and round div up if
  97. necessary. */
  98. if ((uint32_t)(s->period_frac << shift))
  99. div += 1;
  100. }
  101. counter = rem / div;
  102. }
  103. } else {
  104. counter = s->delta;
  105. }
  106. return counter;
  107. }
  108. void ptimer_set_count(ptimer_state *s, uint64_t count)
  109. {
  110. s->delta = count;
  111. if (s->enabled) {
  112. s->next_event = qemu_get_clock_ns(vm_clock);
  113. ptimer_reload(s);
  114. }
  115. }
  116. void ptimer_run(ptimer_state *s, int oneshot)
  117. {
  118. if (s->enabled) {
  119. return;
  120. }
  121. if (s->period == 0) {
  122. fprintf(stderr, "Timer with period zero, disabling\n");
  123. return;
  124. }
  125. s->enabled = oneshot ? 2 : 1;
  126. s->next_event = qemu_get_clock_ns(vm_clock);
  127. ptimer_reload(s);
  128. }
  129. /* Pause a timer. Note that this may cause it to "lose" time, even if it
  130. is immediately restarted. */
  131. void ptimer_stop(ptimer_state *s)
  132. {
  133. if (!s->enabled)
  134. return;
  135. s->delta = ptimer_get_count(s);
  136. qemu_del_timer(s->timer);
  137. s->enabled = 0;
  138. }
  139. /* Set counter increment interval in nanoseconds. */
  140. void ptimer_set_period(ptimer_state *s, int64_t period)
  141. {
  142. s->period = period;
  143. s->period_frac = 0;
  144. if (s->enabled) {
  145. s->next_event = qemu_get_clock_ns(vm_clock);
  146. ptimer_reload(s);
  147. }
  148. }
  149. /* Set counter frequency in Hz. */
  150. void ptimer_set_freq(ptimer_state *s, uint32_t freq)
  151. {
  152. s->period = 1000000000ll / freq;
  153. s->period_frac = (1000000000ll << 32) / freq;
  154. if (s->enabled) {
  155. s->next_event = qemu_get_clock_ns(vm_clock);
  156. ptimer_reload(s);
  157. }
  158. }
  159. /* Set the initial countdown value. If reload is nonzero then also set
  160. count = limit. */
  161. void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
  162. {
  163. s->limit = limit;
  164. if (reload)
  165. s->delta = limit;
  166. if (s->enabled && reload) {
  167. s->next_event = qemu_get_clock_ns(vm_clock);
  168. ptimer_reload(s);
  169. }
  170. }
  171. const VMStateDescription vmstate_ptimer = {
  172. .name = "ptimer",
  173. .version_id = 1,
  174. .minimum_version_id = 1,
  175. .minimum_version_id_old = 1,
  176. .fields = (VMStateField[]) {
  177. VMSTATE_UINT8(enabled, ptimer_state),
  178. VMSTATE_UINT64(limit, ptimer_state),
  179. VMSTATE_UINT64(delta, ptimer_state),
  180. VMSTATE_UINT32(period_frac, ptimer_state),
  181. VMSTATE_INT64(period, ptimer_state),
  182. VMSTATE_INT64(last_event, ptimer_state),
  183. VMSTATE_INT64(next_event, ptimer_state),
  184. VMSTATE_TIMER(timer, ptimer_state),
  185. VMSTATE_END_OF_LIST()
  186. }
  187. };
  188. ptimer_state *ptimer_init(QEMUBH *bh)
  189. {
  190. ptimer_state *s;
  191. s = (ptimer_state *)qemu_mallocz(sizeof(ptimer_state));
  192. s->bh = bh;
  193. s->timer = qemu_new_timer_ns(vm_clock, ptimer_tick, s);
  194. return s;
  195. }