2 * General purpose implementation of a simple periodic countdown timer.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GNU LGPL.
8 #include "qemu/osdep.h"
10 #include "qemu/timer.h"
11 #include "hw/ptimer.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"
18 #define DELTA_ADJUST 1
19 #define DELTA_NO_ADJUST -1
23 uint8_t enabled
; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
35 /* Use a bottom-half routine to avoid reentrancy issues. */
36 static void ptimer_trigger(ptimer_state
*s
)
39 replay_bh_schedule_event(s
->bh
);
43 static void ptimer_reload(ptimer_state
*s
, int delta_adjust
)
45 uint32_t period_frac
= s
->period_frac
;
46 uint64_t period
= s
->period
;
47 uint64_t delta
= s
->delta
;
48 bool suppress_trigger
= false;
51 * Note that if delta_adjust is 0 then we must be here because of
52 * a count register write or timer start, not because of timer expiry.
53 * In that case the policy might require us to suppress the timer trigger
54 * that we would otherwise generate for a zero delta.
56 if (delta_adjust
== 0 &&
57 (s
->policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
)) {
58 suppress_trigger
= true;
60 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)
61 && !suppress_trigger
) {
65 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
66 delta
= s
->delta
= s
->limit
;
70 if (!qtest_enabled()) {
71 fprintf(stderr
, "Timer with period zero, disabling\n");
78 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
79 if (delta_adjust
!= DELTA_NO_ADJUST
) {
80 delta
+= delta_adjust
;
84 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_CONTINUOUS_TRIGGER
)) {
85 if (s
->enabled
== 1 && s
->limit
== 0) {
90 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
91 if (delta_adjust
!= DELTA_NO_ADJUST
) {
96 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
97 if (s
->enabled
== 1 && s
->limit
!= 0) {
103 if (!qtest_enabled()) {
104 fprintf(stderr
, "Timer with delta zero, disabling\n");
112 * Artificially limit timeout rate to something
113 * achievable under QEMU. Otherwise, QEMU spends all
114 * its time generating timer interrupts, and there
115 * is no forward progress.
116 * About ten microseconds is the fastest that really works
117 * on the current generation of host machines.
120 if (s
->enabled
== 1 && (delta
* period
< 10000) && !use_icount
) {
121 period
= 10000 / delta
;
125 s
->last_event
= s
->next_event
;
126 s
->next_event
= s
->last_event
+ delta
* period
;
128 s
->next_event
+= ((int64_t)period_frac
* delta
) >> 32;
130 timer_mod(s
->timer
, s
->next_event
);
133 static void ptimer_tick(void *opaque
)
135 ptimer_state
*s
= (ptimer_state
*)opaque
;
138 if (s
->enabled
== 2) {
142 int delta_adjust
= DELTA_ADJUST
;
144 if (s
->delta
== 0 || s
->limit
== 0) {
145 /* If a "continuous trigger" policy is not used and limit == 0,
146 we should error out. delta == 0 means that this tick is
147 caused by a "no immediate reload" policy, so it shouldn't
149 delta_adjust
= DELTA_NO_ADJUST
;
152 if (!(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
153 /* Avoid re-trigger on deferred reload if "no immediate trigger"
154 policy isn't used. */
155 trigger
= (delta_adjust
== DELTA_ADJUST
);
160 ptimer_reload(s
, delta_adjust
);
168 uint64_t ptimer_get_count(ptimer_state
*s
)
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);
179 /* Figure out the current counter value. */
181 /* Prevent timer underflowing if it should already have
189 uint32_t period_frac
= s
->period_frac
;
190 uint64_t period
= s
->period
;
192 if (!oneshot
&& (s
->delta
* period
< 10000) && !use_icount
) {
193 period
= 10000 / s
->delta
;
197 /* We need to divide time by period, where time is stored in
198 rem (64-bit integer) and period is stored in period/period_frac
201 Doing full precision division is hard, so scale values and
202 do a 64-bit division. The result should be rounded down,
203 so that the rounding error never causes the timer to go
212 shift
= clz1
< clz2
? clz1
: clz2
;
217 div
|= ((uint64_t)period_frac
<< (shift
- 32));
220 div
|= (period_frac
>> (32 - shift
));
221 /* Look at remaining bits of period_frac and round div up if
223 if ((uint32_t)(period_frac
<< shift
))
228 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
229 /* Before wrapping around, timer should stay with counter = 0
231 if (!oneshot
&& s
->delta
== s
->limit
) {
233 /* Counter == delta here, check whether it was
234 adjusted and if it was, then right now it is
235 that "one period". */
236 if (counter
== s
->limit
+ DELTA_ADJUST
) {
239 } else if (counter
== s
->limit
) {
240 /* Since the counter is rounded down and now != last,
241 the counter == limit means that delta was adjusted
242 by +1 and right now it is that adjusted period. */
249 if (s
->policy_mask
& PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
) {
250 /* If now == last then delta == limit, i.e. the counter already
251 represents the correct value. It would be rounded down a 1ns
263 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
267 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
272 void ptimer_run(ptimer_state
*s
, int oneshot
)
274 bool was_disabled
= !s
->enabled
;
276 if (was_disabled
&& s
->period
== 0) {
277 if (!qtest_enabled()) {
278 fprintf(stderr
, "Timer with period zero, disabling\n");
282 s
->enabled
= oneshot
? 2 : 1;
284 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
289 /* Pause a timer. Note that this may cause it to "lose" time, even if it
290 is immediately restarted. */
291 void ptimer_stop(ptimer_state
*s
)
296 s
->delta
= ptimer_get_count(s
);
301 /* Set counter increment interval in nanoseconds. */
302 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
304 s
->delta
= ptimer_get_count(s
);
308 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
313 /* Set counter frequency in Hz. */
314 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
316 s
->delta
= ptimer_get_count(s
);
317 s
->period
= 1000000000ll / freq
;
318 s
->period_frac
= (1000000000ll << 32) / freq
;
320 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
325 /* Set the initial countdown value. If reload is nonzero then also set
327 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
332 if (s
->enabled
&& reload
) {
333 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
338 uint64_t ptimer_get_limit(ptimer_state
*s
)
343 const VMStateDescription vmstate_ptimer
= {
346 .minimum_version_id
= 1,
347 .fields
= (VMStateField
[]) {
348 VMSTATE_UINT8(enabled
, ptimer_state
),
349 VMSTATE_UINT64(limit
, ptimer_state
),
350 VMSTATE_UINT64(delta
, ptimer_state
),
351 VMSTATE_UINT32(period_frac
, ptimer_state
),
352 VMSTATE_INT64(period
, ptimer_state
),
353 VMSTATE_INT64(last_event
, ptimer_state
),
354 VMSTATE_INT64(next_event
, ptimer_state
),
355 VMSTATE_TIMER_PTR(timer
, ptimer_state
),
356 VMSTATE_END_OF_LIST()
360 ptimer_state
*ptimer_init(QEMUBH
*bh
, uint8_t policy_mask
)
364 s
= (ptimer_state
*)g_malloc0(sizeof(ptimer_state
));
366 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);
367 s
->policy_mask
= policy_mask
;
370 * These two policies are incompatible -- trigger-on-decrement implies
371 * a timer trigger when the count becomes 0, but no-immediate-trigger
372 * implies a trigger when the count stops being 0.
374 assert(!((policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
) &&
375 (policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)));
379 void ptimer_free(ptimer_state
*s
)
381 qemu_bh_delete(s
->bh
);
382 timer_free(s
->timer
);