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
;
49 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
53 if (delta
== 0 && !(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
54 delta
= s
->delta
= s
->limit
;
58 if (!qtest_enabled()) {
59 fprintf(stderr
, "Timer with period zero, disabling\n");
66 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
67 if (delta_adjust
!= DELTA_NO_ADJUST
) {
68 delta
+= delta_adjust
;
72 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_CONTINUOUS_TRIGGER
)) {
73 if (s
->enabled
== 1 && s
->limit
== 0) {
78 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
79 if (delta_adjust
!= DELTA_NO_ADJUST
) {
84 if (delta
== 0 && (s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_RELOAD
)) {
85 if (s
->enabled
== 1 && s
->limit
!= 0) {
91 if (!qtest_enabled()) {
92 fprintf(stderr
, "Timer with delta zero, disabling\n");
100 * Artificially limit timeout rate to something
101 * achievable under QEMU. Otherwise, QEMU spends all
102 * its time generating timer interrupts, and there
103 * is no forward progress.
104 * About ten microseconds is the fastest that really works
105 * on the current generation of host machines.
108 if (s
->enabled
== 1 && (delta
* period
< 10000) && !use_icount
) {
109 period
= 10000 / delta
;
113 s
->last_event
= s
->next_event
;
114 s
->next_event
= s
->last_event
+ delta
* period
;
116 s
->next_event
+= ((int64_t)period_frac
* delta
) >> 32;
118 timer_mod(s
->timer
, s
->next_event
);
121 static void ptimer_tick(void *opaque
)
123 ptimer_state
*s
= (ptimer_state
*)opaque
;
126 if (s
->enabled
== 2) {
130 int delta_adjust
= DELTA_ADJUST
;
132 if (s
->delta
== 0 || s
->limit
== 0) {
133 /* If a "continuous trigger" policy is not used and limit == 0,
134 we should error out. delta == 0 means that this tick is
135 caused by a "no immediate reload" policy, so it shouldn't
137 delta_adjust
= DELTA_NO_ADJUST
;
140 if (!(s
->policy_mask
& PTIMER_POLICY_NO_IMMEDIATE_TRIGGER
)) {
141 /* Avoid re-trigger on deferred reload if "no immediate trigger"
142 policy isn't used. */
143 trigger
= (delta_adjust
== DELTA_ADJUST
);
148 ptimer_reload(s
, delta_adjust
);
156 uint64_t ptimer_get_count(ptimer_state
*s
)
160 if (s
->enabled
&& s
->delta
!= 0) {
161 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
162 int64_t next
= s
->next_event
;
163 int64_t last
= s
->last_event
;
164 bool expired
= (now
- next
>= 0);
165 bool oneshot
= (s
->enabled
== 2);
167 /* Figure out the current counter value. */
169 /* Prevent timer underflowing if it should already have
177 uint32_t period_frac
= s
->period_frac
;
178 uint64_t period
= s
->period
;
180 if (!oneshot
&& (s
->delta
* period
< 10000) && !use_icount
) {
181 period
= 10000 / s
->delta
;
185 /* We need to divide time by period, where time is stored in
186 rem (64-bit integer) and period is stored in period/period_frac
189 Doing full precision division is hard, so scale values and
190 do a 64-bit division. The result should be rounded down,
191 so that the rounding error never causes the timer to go
200 shift
= clz1
< clz2
? clz1
: clz2
;
205 div
|= ((uint64_t)period_frac
<< (shift
- 32));
208 div
|= (period_frac
>> (32 - shift
));
209 /* Look at remaining bits of period_frac and round div up if
211 if ((uint32_t)(period_frac
<< shift
))
216 if (s
->policy_mask
& PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD
) {
217 /* Before wrapping around, timer should stay with counter = 0
219 if (!oneshot
&& s
->delta
== s
->limit
) {
221 /* Counter == delta here, check whether it was
222 adjusted and if it was, then right now it is
223 that "one period". */
224 if (counter
== s
->limit
+ DELTA_ADJUST
) {
227 } else if (counter
== s
->limit
) {
228 /* Since the counter is rounded down and now != last,
229 the counter == limit means that delta was adjusted
230 by +1 and right now it is that adjusted period. */
237 if (s
->policy_mask
& PTIMER_POLICY_NO_COUNTER_ROUND_DOWN
) {
238 /* If now == last then delta == limit, i.e. the counter already
239 represents the correct value. It would be rounded down a 1ns
251 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
255 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
260 void ptimer_run(ptimer_state
*s
, int oneshot
)
262 bool was_disabled
= !s
->enabled
;
264 if (was_disabled
&& s
->period
== 0) {
265 if (!qtest_enabled()) {
266 fprintf(stderr
, "Timer with period zero, disabling\n");
270 s
->enabled
= oneshot
? 2 : 1;
272 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
277 /* Pause a timer. Note that this may cause it to "lose" time, even if it
278 is immediately restarted. */
279 void ptimer_stop(ptimer_state
*s
)
284 s
->delta
= ptimer_get_count(s
);
289 /* Set counter increment interval in nanoseconds. */
290 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
292 s
->delta
= ptimer_get_count(s
);
296 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
301 /* Set counter frequency in Hz. */
302 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
304 s
->delta
= ptimer_get_count(s
);
305 s
->period
= 1000000000ll / freq
;
306 s
->period_frac
= (1000000000ll << 32) / freq
;
308 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
313 /* Set the initial countdown value. If reload is nonzero then also set
315 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
320 if (s
->enabled
&& reload
) {
321 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
326 uint64_t ptimer_get_limit(ptimer_state
*s
)
331 const VMStateDescription vmstate_ptimer
= {
334 .minimum_version_id
= 1,
335 .fields
= (VMStateField
[]) {
336 VMSTATE_UINT8(enabled
, ptimer_state
),
337 VMSTATE_UINT64(limit
, ptimer_state
),
338 VMSTATE_UINT64(delta
, ptimer_state
),
339 VMSTATE_UINT32(period_frac
, ptimer_state
),
340 VMSTATE_INT64(period
, ptimer_state
),
341 VMSTATE_INT64(last_event
, ptimer_state
),
342 VMSTATE_INT64(next_event
, ptimer_state
),
343 VMSTATE_TIMER_PTR(timer
, ptimer_state
),
344 VMSTATE_END_OF_LIST()
348 ptimer_state
*ptimer_init(QEMUBH
*bh
, uint8_t policy_mask
)
352 s
= (ptimer_state
*)g_malloc0(sizeof(ptimer_state
));
354 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);
355 s
->policy_mask
= policy_mask
;
359 void ptimer_free(ptimer_state
*s
)
361 qemu_bh_delete(s
->bh
);
362 timer_free(s
->timer
);