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 "hw/ptimer.h"
11 #include "migration/vmstate.h"
12 #include "qemu/host-utils.h"
13 #include "sysemu/replay.h"
14 #include "sysemu/cpu-timers.h"
15 #include "sysemu/qtest.h"
16 #include "block/aio.h"
17 #include "sysemu/cpus.h"
20 #define DELTA_ADJUST 1
21 #define DELTA_NO_ADJUST -1
25 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
)
49 s
->callback(s
->callback_opaque
);
52 static void ptimer_reload(ptimer_state
*s
, int delta_adjust
)
57 bool suppress_trigger
= false;
60 * Note that if delta_adjust is 0 then we must be here because of
61 * a count register write or timer start, not because of timer expiry.
62 * In that case the policy might require us to suppress the timer trigger
63 * that we would otherwise generate for a zero delta.
65 if (delta_adjust
== 0 &&
66 (s
->policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
)) {
67 suppress_trigger
= true;
69 if (s
->delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)
70 && !suppress_trigger
) {
75 * Note that ptimer_trigger() might call the device callback function,
76 * which can then modify timer state, so we must not cache any fields
77 * from ptimer_state until after we have called it.
81 period_frac
= s
->period_frac
;
83 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
84 delta
= s
->delta
= s
->limit
;
88 if (!qtest_enabled()) {
89 fprintf(stderr
, "Timer with period zero, disabling\n");
96 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
97 if (delta_adjust
!= DELTA_NO_ADJUST
) {
98 delta
+= delta_adjust
;
102 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_CONTINUOUS_TRIGGER
)) {
103 if (s
->enabled
== 1 && s
->limit
== 0) {
108 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
109 if (delta_adjust
!= DELTA_NO_ADJUST
) {
114 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
115 if (s
->enabled
== 1 && s
->limit
!= 0) {
121 if (s
->enabled
== 0) {
122 /* trigger callback disabled the timer already */
125 if (!qtest_enabled()) {
126 fprintf(stderr
, "Timer with delta zero, disabling\n");
134 * Artificially limit timeout rate to something
135 * achievable under QEMU. Otherwise, QEMU spends all
136 * its time generating timer interrupts, and there
137 * is no forward progress.
138 * About ten microseconds is the fastest that really works
139 * on the current generation of host machines.
142 if (s
->enabled
== 1 && (delta
* period
< 10000) &&
143 !icount_enabled() && !qtest_enabled()) {
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) &&
227 !icount_enabled() && !qtest_enabled()) {
228 period
= 10000 / s
->delta
;
232 /* We need to divide time by period, where time is stored in
233 rem (64-bit integer) and period is stored in period/period_frac
236 Doing full precision division is hard, so scale values and
237 do a 64-bit division. The result should be rounded down,
238 so that the rounding error never causes the timer to go
247 shift
= clz1
< clz2
? clz1
: clz2
;
252 div
|= ((uint64_t)period_frac
<< (shift
- 32));
255 div
|= (period_frac
>> (32 - shift
));
256 /* Look at remaining bits of period_frac and round div up if
258 if ((uint32_t)(period_frac
<< shift
))
263 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
264 /* Before wrapping around, timer should stay with counter = 0
266 if (!oneshot
&& s
->delta
== s
->limit
) {
268 /* Counter == delta here, check whether it was
269 adjusted and if it was, then right now it is
270 that "one period". */
271 if (counter
== s
->limit
+ DELTA_ADJUST
) {
274 } else if (counter
== s
->limit
) {
275 /* Since the counter is rounded down and now != last,
276 the counter == limit means that delta was adjusted
277 by +1 and right now it is that adjusted period. */
284 if (s
->policy_mask
& PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
) {
285 /* If now == last then delta == limit, i.e. the counter already
286 represents the correct value. It would be rounded down a 1ns
298 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
300 assert(s
->in_transaction
);
303 s
->need_reload
= true;
307 void ptimer_run(ptimer_state
*s
, int oneshot
)
309 bool was_disabled
= !s
->enabled
;
311 assert(s
->in_transaction
);
313 if (was_disabled
&& s
->period
== 0) {
314 if (!qtest_enabled()) {
315 fprintf(stderr
, "Timer with period zero, disabling\n");
319 s
->enabled
= oneshot
? 2 : 1;
321 s
->need_reload
= true;
325 /* Pause a timer. Note that this may cause it to "lose" time, even if it
326 is immediately restarted. */
327 void ptimer_stop(ptimer_state
*s
)
329 assert(s
->in_transaction
);
334 s
->delta
= ptimer_get_count(s
);
337 s
->need_reload
= false;
340 /* Set counter increment interval in nanoseconds. */
341 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
343 assert(s
->in_transaction
);
344 s
->delta
= ptimer_get_count(s
);
348 s
->need_reload
= true;
352 /* Set counter increment interval from a Clock */
353 void ptimer_set_period_from_clock(ptimer_state
*s
, const Clock
*clk
,
354 unsigned int divisor
)
357 * The raw clock period is a 64-bit value in units of 2^-32 ns;
358 * put another way it's a 32.32 fixed-point ns value. Our internal
359 * representation of the period is 64.32 fixed point ns, so
360 * the conversion is simple.
362 uint64_t raw_period
= clock_get(clk
);
363 uint64_t period_frac
;
365 assert(s
->in_transaction
);
366 s
->delta
= ptimer_get_count(s
);
367 s
->period
= extract64(raw_period
, 32, 32);
368 period_frac
= extract64(raw_period
, 0, 32);
370 * divisor specifies a possible frequency divisor between the
371 * clock and the timer, so it is a multiplier on the period.
372 * We do the multiply after splitting the raw period out into
373 * period and frac to avoid having to do a 32*64->96 multiply.
375 s
->period
*= divisor
;
376 period_frac
*= divisor
;
377 s
->period
+= extract64(period_frac
, 32, 32);
378 s
->period_frac
= (uint32_t)period_frac
;
381 s
->need_reload
= true;
385 /* Set counter frequency in Hz. */
386 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
388 assert(s
->in_transaction
);
389 s
->delta
= ptimer_get_count(s
);
390 s
->period
= 1000000000ll / freq
;
391 s
->period_frac
= (1000000000ll << 32) / freq
;
393 s
->need_reload
= true;
397 /* Set the initial countdown value. If reload is nonzero then also set
399 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
401 assert(s
->in_transaction
);
405 if (s
->enabled
&& reload
) {
406 s
->need_reload
= true;
410 uint64_t ptimer_get_limit(ptimer_state
*s
)
415 void ptimer_transaction_begin(ptimer_state
*s
)
417 assert(!s
->in_transaction
);
418 s
->in_transaction
= true;
419 s
->need_reload
= false;
422 void ptimer_transaction_commit(ptimer_state
*s
)
424 assert(s
->in_transaction
);
426 * We must loop here because ptimer_reload() can call the callback
427 * function, which might then update ptimer state in a way that
428 * means we need to do another reload and possibly another callback.
429 * A disabled timer never needs reloading (and if we don't check
430 * this then we loop forever if ptimer_reload() disables the timer).
432 while (s
->need_reload
&& s
->enabled
) {
433 s
->need_reload
= false;
434 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
437 /* Now we've finished reload we can leave the transaction block. */
438 s
->in_transaction
= false;
441 const VMStateDescription vmstate_ptimer
= {
444 .minimum_version_id
= 1,
445 .fields
= (VMStateField
[]) {
446 VMSTATE_UINT8(enabled
, ptimer_state
),
447 VMSTATE_UINT64(limit
, ptimer_state
),
448 VMSTATE_UINT64(delta
, ptimer_state
),
449 VMSTATE_UINT32(period_frac
, ptimer_state
),
450 VMSTATE_INT64(period
, ptimer_state
),
451 VMSTATE_INT64(last_event
, ptimer_state
),
452 VMSTATE_INT64(next_event
, ptimer_state
),
453 VMSTATE_TIMER_PTR(timer
, ptimer_state
),
454 VMSTATE_END_OF_LIST()
458 ptimer_state
*ptimer_init(ptimer_cb callback
, void *callback_opaque
,
463 /* The callback function is mandatory. */
466 s
= g_new0(ptimer_state
, 1);
467 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);
468 s
->policy_mask
= policy_mask
;
469 s
->callback
= callback
;
470 s
->callback_opaque
= callback_opaque
;
473 * These two policies are incompatible -- trigger-on-decrement implies
474 * a timer trigger when the count becomes 0, but no-immediate-trigger
475 * implies a trigger when the count stops being 0.
477 assert(!((policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
) &&
478 (policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)));
482 void ptimer_free(ptimer_state
*s
)
484 timer_free(s
->timer
);