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. */
36 /* Use a bottom-half routine to avoid reentrancy issues. */
37 static void ptimer_trigger(ptimer_state
*s
)
40 replay_bh_schedule_event(s
->bh
);
44 static void ptimer_reload(ptimer_state
*s
, int delta_adjust
)
46 uint32_t period_frac
= s
->period_frac
;
47 uint64_t period
= s
->period
;
48 uint64_t delta
= s
->delta
;
49 bool suppress_trigger
= false;
52 * Note that if delta_adjust is 0 then we must be here because of
53 * a count register write or timer start, not because of timer expiry.
54 * In that case the policy might require us to suppress the timer trigger
55 * that we would otherwise generate for a zero delta.
57 if (delta_adjust
== 0 &&
58 (s
->policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
)) {
59 suppress_trigger
= true;
61 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)
62 && !suppress_trigger
) {
66 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
67 delta
= s
->delta
= s
->limit
;
71 if (!qtest_enabled()) {
72 fprintf(stderr
, "Timer with period zero, disabling\n");
79 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
80 if (delta_adjust
!= DELTA_NO_ADJUST
) {
81 delta
+= delta_adjust
;
85 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_CONTINUOUS_TRIGGER
)) {
86 if (s
->enabled
== 1 && s
->limit
== 0) {
91 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
92 if (delta_adjust
!= DELTA_NO_ADJUST
) {
97 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
98 if (s
->enabled
== 1 && s
->limit
!= 0) {
104 if (!qtest_enabled()) {
105 fprintf(stderr
, "Timer with delta zero, disabling\n");
113 * Artificially limit timeout rate to something
114 * achievable under QEMU. Otherwise, QEMU spends all
115 * its time generating timer interrupts, and there
116 * is no forward progress.
117 * About ten microseconds is the fastest that really works
118 * on the current generation of host machines.
121 if (s
->enabled
== 1 && (delta
* period
< 10000) && !use_icount
) {
122 period
= 10000 / delta
;
126 s
->last_event
= s
->next_event
;
127 s
->next_event
= s
->last_event
+ delta
* period
;
129 s
->next_event
+= ((int64_t)period_frac
* delta
) >> 32;
131 timer_mod(s
->timer
, s
->next_event
);
134 static void ptimer_tick(void *opaque
)
136 ptimer_state
*s
= (ptimer_state
*)opaque
;
139 if (s
->enabled
== 2) {
143 int delta_adjust
= DELTA_ADJUST
;
145 if (s
->delta
== 0 || s
->limit
== 0) {
146 /* If a "continuous trigger" policy is not used and limit == 0,
147 we should error out. delta == 0 means that this tick is
148 caused by a "no immediate reload" policy, so it shouldn't
150 delta_adjust
= DELTA_NO_ADJUST
;
153 if (!(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
154 /* Avoid re-trigger on deferred reload if "no immediate trigger"
155 policy isn't used. */
156 trigger
= (delta_adjust
== DELTA_ADJUST
);
161 ptimer_reload(s
, delta_adjust
);
169 uint64_t ptimer_get_count(ptimer_state
*s
)
173 if (s
->enabled
&& s
->delta
!= 0) {
174 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
175 int64_t next
= s
->next_event
;
176 int64_t last
= s
->last_event
;
177 bool expired
= (now
- next
>= 0);
178 bool oneshot
= (s
->enabled
== 2);
180 /* Figure out the current counter value. */
182 /* Prevent timer underflowing if it should already have
190 uint32_t period_frac
= s
->period_frac
;
191 uint64_t period
= s
->period
;
193 if (!oneshot
&& (s
->delta
* period
< 10000) && !use_icount
) {
194 period
= 10000 / s
->delta
;
198 /* We need to divide time by period, where time is stored in
199 rem (64-bit integer) and period is stored in period/period_frac
202 Doing full precision division is hard, so scale values and
203 do a 64-bit division. The result should be rounded down,
204 so that the rounding error never causes the timer to go
213 shift
= clz1
< clz2
? clz1
: clz2
;
218 div
|= ((uint64_t)period_frac
<< (shift
- 32));
221 div
|= (period_frac
>> (32 - shift
));
222 /* Look at remaining bits of period_frac and round div up if
224 if ((uint32_t)(period_frac
<< shift
))
229 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
230 /* Before wrapping around, timer should stay with counter = 0
232 if (!oneshot
&& s
->delta
== s
->limit
) {
234 /* Counter == delta here, check whether it was
235 adjusted and if it was, then right now it is
236 that "one period". */
237 if (counter
== s
->limit
+ DELTA_ADJUST
) {
240 } else if (counter
== s
->limit
) {
241 /* Since the counter is rounded down and now != last,
242 the counter == limit means that delta was adjusted
243 by +1 and right now it is that adjusted period. */
250 if (s
->policy_mask
& PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
) {
251 /* If now == last then delta == limit, i.e. the counter already
252 represents the correct value. It would be rounded down a 1ns
264 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
268 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
273 void ptimer_run(ptimer_state
*s
, int oneshot
)
275 bool was_disabled
= !s
->enabled
;
277 if (was_disabled
&& s
->period
== 0) {
278 if (!qtest_enabled()) {
279 fprintf(stderr
, "Timer with period zero, disabling\n");
283 s
->enabled
= oneshot
? 2 : 1;
285 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
290 /* Pause a timer. Note that this may cause it to "lose" time, even if it
291 is immediately restarted. */
292 void ptimer_stop(ptimer_state
*s
)
297 s
->delta
= ptimer_get_count(s
);
302 /* Set counter increment interval in nanoseconds. */
303 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
305 s
->delta
= ptimer_get_count(s
);
309 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
314 /* Set counter frequency in Hz. */
315 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
317 s
->delta
= ptimer_get_count(s
);
318 s
->period
= 1000000000ll / freq
;
319 s
->period_frac
= (1000000000ll << 32) / freq
;
321 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
326 /* Set the initial countdown value. If reload is nonzero then also set
328 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
333 if (s
->enabled
&& reload
) {
334 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
339 uint64_t ptimer_get_limit(ptimer_state
*s
)
344 const VMStateDescription vmstate_ptimer
= {
347 .minimum_version_id
= 1,
348 .fields
= (VMStateField
[]) {
349 VMSTATE_UINT8(enabled
, ptimer_state
),
350 VMSTATE_UINT64(limit
, ptimer_state
),
351 VMSTATE_UINT64(delta
, ptimer_state
),
352 VMSTATE_UINT32(period_frac
, ptimer_state
),
353 VMSTATE_INT64(period
, ptimer_state
),
354 VMSTATE_INT64(last_event
, ptimer_state
),
355 VMSTATE_INT64(next_event
, ptimer_state
),
356 VMSTATE_TIMER_PTR(timer
, ptimer_state
),
357 VMSTATE_END_OF_LIST()
361 ptimer_state
*ptimer_init(QEMUBH
*bh
, uint8_t policy_mask
)
365 s
= (ptimer_state
*)g_malloc0(sizeof(ptimer_state
));
367 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);
368 s
->policy_mask
= policy_mask
;
371 * These two policies are incompatible -- trigger-on-decrement implies
372 * a timer trigger when the count becomes 0, but no-immediate-trigger
373 * implies a trigger when the count stops being 0.
375 assert(!((policy_mask
& PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT
) &&
376 (policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)));
380 void ptimer_free(ptimer_state
*s
)
382 qemu_bh_delete(s
->bh
);
383 timer_free(s
->timer
);