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"
17 uint8_t enabled
; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
28 /* Use a bottom-half routine to avoid reentrancy issues. */
29 static void ptimer_trigger(ptimer_state
*s
)
32 replay_bh_schedule_event(s
->bh
);
36 static void ptimer_reload(ptimer_state
*s
)
38 uint32_t period_frac
= s
->period_frac
;
39 uint64_t period
= s
->period
;
45 if (s
->delta
== 0 || s
->period
== 0) {
46 fprintf(stderr
, "Timer with period zero, disabling\n");
53 * Artificially limit timeout rate to something
54 * achievable under QEMU. Otherwise, QEMU spends all
55 * its time generating timer interrupts, and there
56 * is no forward progress.
57 * About ten microseconds is the fastest that really works
58 * on the current generation of host machines.
61 if (s
->enabled
== 1 && (s
->delta
* period
< 10000) && !use_icount
) {
62 period
= 10000 / s
->delta
;
66 s
->last_event
= s
->next_event
;
67 s
->next_event
= s
->last_event
+ s
->delta
* period
;
69 s
->next_event
+= ((int64_t)period_frac
* s
->delta
) >> 32;
71 timer_mod(s
->timer
, s
->next_event
);
74 static void ptimer_tick(void *opaque
)
76 ptimer_state
*s
= (ptimer_state
*)opaque
;
79 if (s
->enabled
== 2) {
86 uint64_t ptimer_get_count(ptimer_state
*s
)
91 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
92 int64_t next
= s
->next_event
;
93 bool expired
= (now
- next
>= 0);
94 bool oneshot
= (s
->enabled
== 2);
96 /* Figure out the current counter value. */
98 /* Prevent timer underflowing if it should already have
106 uint32_t period_frac
= s
->period_frac
;
107 uint64_t period
= s
->period
;
109 if (!oneshot
&& (s
->delta
* period
< 10000) && !use_icount
) {
110 period
= 10000 / s
->delta
;
114 /* We need to divide time by period, where time is stored in
115 rem (64-bit integer) and period is stored in period/period_frac
118 Doing full precision division is hard, so scale values and
119 do a 64-bit division. The result should be rounded down,
120 so that the rounding error never causes the timer to go
129 shift
= clz1
< clz2
? clz1
: clz2
;
134 div
|= ((uint64_t)period_frac
<< (shift
- 32));
137 div
|= (period_frac
>> (32 - shift
));
138 /* Look at remaining bits of period_frac and round div up if
140 if ((uint32_t)(period_frac
<< shift
))
151 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
155 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
160 void ptimer_run(ptimer_state
*s
, int oneshot
)
162 bool was_disabled
= !s
->enabled
;
164 if (was_disabled
&& s
->period
== 0) {
165 fprintf(stderr
, "Timer with period zero, disabling\n");
168 s
->enabled
= oneshot
? 2 : 1;
170 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
175 /* Pause a timer. Note that this may cause it to "lose" time, even if it
176 is immediately restarted. */
177 void ptimer_stop(ptimer_state
*s
)
182 s
->delta
= ptimer_get_count(s
);
187 /* Set counter increment interval in nanoseconds. */
188 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
190 s
->delta
= ptimer_get_count(s
);
194 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
199 /* Set counter frequency in Hz. */
200 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
202 s
->delta
= ptimer_get_count(s
);
203 s
->period
= 1000000000ll / freq
;
204 s
->period_frac
= (1000000000ll << 32) / freq
;
206 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
211 /* Set the initial countdown value. If reload is nonzero then also set
213 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
218 if (s
->enabled
&& reload
) {
219 s
->next_event
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
224 uint64_t ptimer_get_limit(ptimer_state
*s
)
229 const VMStateDescription vmstate_ptimer
= {
232 .minimum_version_id
= 1,
233 .fields
= (VMStateField
[]) {
234 VMSTATE_UINT8(enabled
, ptimer_state
),
235 VMSTATE_UINT64(limit
, ptimer_state
),
236 VMSTATE_UINT64(delta
, ptimer_state
),
237 VMSTATE_UINT32(period_frac
, ptimer_state
),
238 VMSTATE_INT64(period
, ptimer_state
),
239 VMSTATE_INT64(last_event
, ptimer_state
),
240 VMSTATE_INT64(next_event
, ptimer_state
),
241 VMSTATE_TIMER_PTR(timer
, ptimer_state
),
242 VMSTATE_END_OF_LIST()
246 ptimer_state
*ptimer_init(QEMUBH
*bh
)
250 s
= (ptimer_state
*)g_malloc0(sizeof(ptimer_state
));
252 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ptimer_tick
, s
);