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/osdep.h"
10 #include "qemu/timer.h"
11 #include "hw/ptimer.h"
12 #include "migration/vmstate.h"
13 #include "qemu/host-utils.h"
14 #include "sysemu/replay.h"
15 #include "sysemu/qtest.h"
16 #include "block/aio.h"
17 #include "sysemu/cpus.h"
19 #define DELTA_ADJUST 1
20 #define DELTA_NO_ADJUST -1
24 uint8_t enabled
; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
35 void *callback_opaque
;
37 * These track whether we're in a transaction block, and if we
38 * need to do a timer reload when the block finishes. They don't
39 * need to be migrated because migration can never happen in the
40 * middle of a transaction block.
46 /* Use a bottom-half routine to avoid reentrancy issues. */
47 static void ptimer_trigger(ptimer_state
*s
)
50 replay_bh_schedule_event(s
->bh
);
53 s
->callback(s
->callback_opaque
);
57 static void ptimer_reload(ptimer_state
*s
, int delta_adjust
)
62 bool suppress_trigger
= false;
65 * Note that if delta_adjust is 0 then we must be here because of
66 * a count register write or timer start, not because of timer expiry.
67 * In that case the policy might require us to suppress the timer trigger
68 * that we would otherwise generate for a zero delta.
70 if (delta_adjust
== 0 &&
71 (s
->policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
)) {
72 suppress_trigger
= true;
74 if (s
->delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)
75 && !suppress_trigger
) {
80 * Note that ptimer_trigger() might call the device callback function,
81 * which can then modify timer state, so we must not cache any fields
82 * from ptimer_state until after we have called it.
86 period_frac
= s
->period_frac
;
88 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
89 delta
= s
->delta
= s
->limit
;
93 if (!qtest_enabled()) {
94 fprintf(stderr
, "Timer with period zero, disabling\n");
101 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
102 if (delta_adjust
!= DELTA_NO_ADJUST
) {
103 delta
+= delta_adjust
;
107 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_CONTINUOUS_TRIGGER
)) {
108 if (s
->enabled
== 1 && s
->limit
== 0) {
113 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
114 if (delta_adjust
!= DELTA_NO_ADJUST
) {
119 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
120 if (s
->enabled
== 1 && s
->limit
!= 0) {
126 if (!qtest_enabled()) {
127 fprintf(stderr
, "Timer with delta zero, disabling\n");
135 * Artificially limit timeout rate to something
136 * achievable under QEMU. Otherwise, QEMU spends all
137 * its time generating timer interrupts, and there
138 * is no forward progress.
139 * About ten microseconds is the fastest that really works
140 * on the current generation of host machines.
143 if (s
->enabled
== 1 && (delta
* period
< 10000) && !use_icount
) {
144 period
= 10000 / delta
;
148 s
->last_event
= s
->next_event
;
149 s
->next_event
= s
->last_event
+ delta
* period
;
151 s
->next_event
+= ((int64_t)period_frac
* delta
) >> 32;
153 timer_mod(s
->timer
, s
->next_event
);
156 static void ptimer_tick(void *opaque
)
158 ptimer_state
*s
= (ptimer_state
*)opaque
;
162 * We perform all the tick actions within a begin/commit block
163 * because the callback function that ptimer_trigger() calls
164 * might make calls into the ptimer APIs that provoke another
165 * trigger, and we want that to cause the callback function
166 * to be called iteratively, not recursively.
168 ptimer_transaction_begin(s
);
170 if (s
->enabled
== 2) {
174 int delta_adjust
= DELTA_ADJUST
;
176 if (s
->delta
== 0 || s
->limit
== 0) {
177 /* If a "continuous trigger" policy is not used and limit == 0,
178 we should error out. delta == 0 means that this tick is
179 caused by a "no immediate reload" policy, so it shouldn't
181 delta_adjust
= DELTA_NO_ADJUST
;
184 if (!(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
185 /* Avoid re-trigger on deferred reload if "no immediate trigger"
186 policy isn't used. */
187 trigger
= (delta_adjust
== DELTA_ADJUST
);
192 ptimer_reload(s
, delta_adjust
);
199 ptimer_transaction_commit(s
);
202 uint64_t ptimer_get_count(ptimer_state
*s
)
206 if (s
->enabled
&& s
->delta
!= 0) {
207 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
208 int64_t next
= s
->next_event
;
209 int64_t last
= s
->last_event
;
210 bool expired
= (now
- next
>= 0);
211 bool oneshot
= (s
->enabled
== 2);
213 /* Figure out the current counter value. */
215 /* Prevent timer underflowing if it should already have
223 uint32_t period_frac
= s
->period_frac
;
224 uint64_t period
= s
->period
;
226 if (!oneshot
&& (s
->delta
* period
< 10000) && !use_icount
) {
227 period
= 10000 / s
->delta
;
231 /* We need to divide time by period, where time is stored in
232 rem (64-bit integer) and period is stored in period/period_frac
235 Doing full precision division is hard, so scale values and
236 do a 64-bit division. The result should be rounded down,
237 so that the rounding error never causes the timer to go
246 shift
= clz1
< clz2
? clz1
: clz2
;
251 div
|= ((uint64_t)period_frac
<< (shift
- 32));
254 div
|= (period_frac
>> (32 - shift
));
255 /* Look at remaining bits of period_frac and round div up if
257 if ((uint32_t)(period_frac
<< shift
))
262 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
263 /* Before wrapping around, timer should stay with counter = 0
265 if (!oneshot
&& s
->delta
== s
->limit
) {
267 /* Counter == delta here, check whether it was
268 adjusted and if it was, then right now it is
269 that "one period". */
270 if (counter
== s
->limit
+ DELTA_ADJUST
) {
273 } else if (counter
== s
->limit
) {
274 /* Since the counter is rounded down and now != last,
275 the counter == limit means that delta was adjusted
276 by +1 and right now it is that adjusted period. */
283 if (s
->policy_mask
& PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
) {
284 /* If now == last then delta == limit, i.e. the counter already
285 represents the correct value. It would be rounded down a 1ns
297 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
299 assert(s
->in_transaction
|| !s
->callback
);
303 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
306 s
->need_reload
= true;
311 void ptimer_run(ptimer_state
*s
, int oneshot
)
313 bool was_disabled
= !s
->enabled
;
315 assert(s
->in_transaction
|| !s
->callback
);
317 if (was_disabled
&& s
->period
== 0) {
318 if (!qtest_enabled()) {
319 fprintf(stderr
, "Timer with period zero, disabling\n");
323 s
->enabled
= oneshot
? 2 : 1;
326 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
329 s
->need_reload
= true;
334 /* Pause a timer. Note that this may cause it to "lose" time, even if it
335 is immediately restarted. */
336 void ptimer_stop(ptimer_state
*s
)
338 assert(s
->in_transaction
|| !s
->callback
);
343 s
->delta
= ptimer_get_count(s
);
347 s
->need_reload
= false;
351 /* Set counter increment interval in nanoseconds. */
352 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
354 assert(s
->in_transaction
|| !s
->callback
);
355 s
->delta
= ptimer_get_count(s
);
360 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
363 s
->need_reload
= true;
368 /* Set counter frequency in Hz. */
369 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
371 assert(s
->in_transaction
|| !s
->callback
);
372 s
->delta
= ptimer_get_count(s
);
373 s
->period
= 1000000000ll / freq
;
374 s
->period_frac
= (1000000000ll << 32) / freq
;
377 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
380 s
->need_reload
= true;
385 /* Set the initial countdown value. If reload is nonzero then also set
387 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
389 assert(s
->in_transaction
|| !s
->callback
);
393 if (s
->enabled
&& reload
) {
395 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
398 s
->need_reload
= true;
403 uint64_t ptimer_get_limit(ptimer_state
*s
)
408 void ptimer_transaction_begin(ptimer_state
*s
)
410 assert(!s
->in_transaction
|| !s
->callback
);
411 s
->in_transaction
= true;
412 s
->need_reload
= false;
415 void ptimer_transaction_commit(ptimer_state
*s
)
417 assert(s
->in_transaction
);
419 * We must loop here because ptimer_reload() can call the callback
420 * function, which might then update ptimer state in a way that
421 * means we need to do another reload and possibly another callback.
422 * A disabled timer never needs reloading (and if we don't check
423 * this then we loop forever if ptimer_reload() disables the timer).
425 while (s
->need_reload
&& s
->enabled
) {
426 s
->need_reload
= false;
427 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
430 /* Now we've finished reload we can leave the transaction block. */
431 s
->in_transaction
= false;
434 const VMStateDescription vmstate_ptimer
= {
437 .minimum_version_id
= 1,
438 .fields
= (VMStateField
[]) {
439 VMSTATE_UINT8(enabled
, ptimer_state
),
440 VMSTATE_UINT64(limit
, ptimer_state
),
441 VMSTATE_UINT64(delta
, ptimer_state
),
442 VMSTATE_UINT32(period_frac
, ptimer_state
),
443 VMSTATE_INT64(period
, ptimer_state
),
444 VMSTATE_INT64(last_event
, ptimer_state
),
445 VMSTATE_INT64(next_event
, ptimer_state
),
446 VMSTATE_TIMER_PTR(timer
, ptimer_state
),
447 VMSTATE_END_OF_LIST()
451 ptimer_state
*ptimer_init_with_bh(QEMUBH
*bh
, uint8_t policy_mask
)
455 s
= (ptimer_state
*)g_malloc0(sizeof(ptimer_state
));
457 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);
458 s
->policy_mask
= policy_mask
;
461 * These two policies are incompatible -- trigger-on-decrement implies
462 * a timer trigger when the count becomes 0, but no-immediate-trigger
463 * implies a trigger when the count stops being 0.
465 assert(!((policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
) &&
466 (policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)));
470 ptimer_state
*ptimer_init(ptimer_cb callback
, void *callback_opaque
,
476 * The callback function is mandatory; so we use it to distinguish
477 * old-style QEMUBH ptimers from new transaction API ptimers.
478 * (ptimer_init_with_bh() allows a NULL bh pointer and at least
479 * one device (digic-timer) passes NULL, so it's not the case
480 * that either s->bh != NULL or s->callback != NULL.)
484 s
= g_new0(ptimer_state
, 1);
485 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);
486 s
->policy_mask
= policy_mask
;
487 s
->callback
= callback
;
488 s
->callback_opaque
= callback_opaque
;
491 * These two policies are incompatible -- trigger-on-decrement implies
492 * a timer trigger when the count becomes 0, but no-immediate-trigger
493 * implies a trigger when the count stops being 0.
495 assert(!((policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
) &&
496 (policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)));
500 void ptimer_free(ptimer_state
*s
)
503 qemu_bh_delete(s
->bh
);
505 timer_free(s
->timer
);