2 * General purpose implementation of a simple periodic countdown timer.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GNU LGPL.
9 #include "qemu/timer.h"
10 #include "hw/ptimer.h"
11 #include "qemu/host-utils.h"
15 uint8_t enabled
; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
26 /* Use a bottom-half routine to avoid reentrancy issues. */
27 static void ptimer_trigger(ptimer_state
*s
)
30 qemu_bh_schedule(s
->bh
);
34 static void ptimer_reload(ptimer_state
*s
)
40 if (s
->delta
== 0 || s
->period
== 0) {
41 fprintf(stderr
, "Timer with period zero, disabling\n");
46 s
->last_event
= s
->next_event
;
47 s
->next_event
= s
->last_event
+ s
->delta
* s
->period
;
49 s
->next_event
+= ((int64_t)s
->period_frac
* s
->delta
) >> 32;
51 timer_mod(s
->timer
, s
->next_event
);
54 static void ptimer_tick(void *opaque
)
56 ptimer_state
*s
= (ptimer_state
*)opaque
;
59 if (s
->enabled
== 2) {
66 uint64_t ptimer_get_count(ptimer_state
*s
)
72 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
73 /* Figure out the current counter value. */
74 if (now
- s
->next_event
> 0
76 /* Prevent timer underflowing if it should already have
85 /* We need to divide time by period, where time is stored in
86 rem (64-bit integer) and period is stored in period/period_frac
89 Doing full precision division is hard, so scale values and
90 do a 64-bit division. The result should be rounded down,
91 so that the rounding error never causes the timer to go
95 rem
= s
->next_event
- now
;
100 shift
= clz1
< clz2
? clz1
: clz2
;
105 div
|= ((uint64_t)s
->period_frac
<< (shift
- 32));
108 div
|= (s
->period_frac
>> (32 - shift
));
109 /* Look at remaining bits of period_frac and round div up if
111 if ((uint32_t)(s
->period_frac
<< shift
))
122 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
126 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
131 void ptimer_run(ptimer_state
*s
, int oneshot
)
136 if (s
->period
== 0) {
137 fprintf(stderr
, "Timer with period zero, disabling\n");
140 s
->enabled
= oneshot
? 2 : 1;
141 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
145 /* Pause a timer. Note that this may cause it to "lose" time, even if it
146 is immediately restarted. */
147 void ptimer_stop(ptimer_state
*s
)
152 s
->delta
= ptimer_get_count(s
);
157 /* Set counter increment interval in nanoseconds. */
158 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
163 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
168 /* Set counter frequency in Hz. */
169 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
171 s
->period
= 1000000000ll / freq
;
172 s
->period_frac
= (1000000000ll << 32) / freq
;
174 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
179 /* Set the initial countdown value. If reload is nonzero then also set
181 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
184 * Artificially limit timeout rate to something
185 * achievable under QEMU. Otherwise, QEMU spends all
186 * its time generating timer interrupts, and there
187 * is no forward progress.
188 * About ten microseconds is the fastest that really works
189 * on the current generation of host machines.
192 if (limit
* s
->period
< 10000 && s
->period
) {
193 limit
= 10000 / s
->period
;
199 if (s
->enabled
&& reload
) {
200 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
205 const VMStateDescription vmstate_ptimer
= {
208 .minimum_version_id
= 1,
209 .fields
= (VMStateField
[]) {
210 VMSTATE_UINT8(enabled
, ptimer_state
),
211 VMSTATE_UINT64(limit
, ptimer_state
),
212 VMSTATE_UINT64(delta
, ptimer_state
),
213 VMSTATE_UINT32(period_frac
, ptimer_state
),
214 VMSTATE_INT64(period
, ptimer_state
),
215 VMSTATE_INT64(last_event
, ptimer_state
),
216 VMSTATE_INT64(next_event
, ptimer_state
),
217 VMSTATE_TIMER_PTR(timer
, ptimer_state
),
218 VMSTATE_END_OF_LIST()
222 ptimer_state
*ptimer_init(QEMUBH
*bh
)
226 s
= (ptimer_state
*)g_malloc0(sizeof(ptimer_state
));
228 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);