2 * General purpose implementation of a simple periodic countdown timer.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licenced under the GNU LGPL.
9 #include "qemu-timer.h"
14 int enabled
; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
25 /* Use a bottom-half routine to avoid reentrancy issues. */
26 static void ptimer_trigger(ptimer_state
*s
)
29 qemu_bh_schedule(s
->bh
);
33 static void ptimer_reload(ptimer_state
*s
)
39 if (s
->delta
== 0 || s
->period
== 0) {
40 fprintf(stderr
, "Timer with period zero, disabling\n");
45 s
->last_event
= s
->next_event
;
46 s
->next_event
= s
->last_event
+ s
->delta
* s
->period
;
48 s
->next_event
+= ((int64_t)s
->period_frac
* s
->delta
) >> 32;
50 qemu_mod_timer(s
->timer
, s
->next_event
);
53 static void ptimer_tick(void *opaque
)
55 ptimer_state
*s
= (ptimer_state
*)opaque
;
58 if (s
->enabled
== 2) {
65 uint64_t ptimer_get_count(ptimer_state
*s
)
71 now
= qemu_get_clock(vm_clock
);
72 /* Figure out the current counter value. */
73 if (now
- s
->next_event
> 0
75 /* Prevent timer underflowing if it should already have
82 rem
= s
->next_event
- now
;
92 void ptimer_set_count(ptimer_state
*s
, uint64_t count
)
96 s
->next_event
= qemu_get_clock(vm_clock
);
101 void ptimer_run(ptimer_state
*s
, int oneshot
)
103 if (s
->period
== 0) {
104 fprintf(stderr
, "Timer with period zero, disabling\n");
107 s
->enabled
= oneshot
? 2 : 1;
108 s
->next_event
= qemu_get_clock(vm_clock
);
112 /* Pause a timer. Note that this may cause it to "lose" time, even if it
113 is immediately restarted. */
114 void ptimer_stop(ptimer_state
*s
)
119 s
->delta
= ptimer_get_count(s
);
120 qemu_del_timer(s
->timer
);
124 /* Set counter increment interval in nanoseconds. */
125 void ptimer_set_period(ptimer_state
*s
, int64_t period
)
130 s
->next_event
= qemu_get_clock(vm_clock
);
135 /* Set counter frequency in Hz. */
136 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
)
138 s
->period
= 1000000000ll / freq
;
139 s
->period_frac
= (1000000000ll << 32) / freq
;
141 s
->next_event
= qemu_get_clock(vm_clock
);
146 /* Set the initial countdown value. If reload is nonzero then also set
148 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
)
153 if (s
->enabled
&& reload
) {
154 s
->next_event
= qemu_get_clock(vm_clock
);
159 void qemu_put_ptimer(QEMUFile
*f
, ptimer_state
*s
)
161 qemu_put_byte(f
, s
->enabled
);
162 qemu_put_be64s(f
, &s
->limit
);
163 qemu_put_be64s(f
, &s
->delta
);
164 qemu_put_be32s(f
, &s
->period_frac
);
165 qemu_put_be64s(f
, &s
->period
);
166 qemu_put_be64s(f
, &s
->last_event
);
167 qemu_put_be64s(f
, &s
->next_event
);
168 qemu_put_timer(f
, s
->timer
);
171 void qemu_get_ptimer(QEMUFile
*f
, ptimer_state
*s
)
173 s
->enabled
= qemu_get_byte(f
);
174 qemu_get_be64s(f
, &s
->limit
);
175 qemu_get_be64s(f
, &s
->delta
);
176 qemu_get_be32s(f
, &s
->period_frac
);
177 qemu_get_be64s(f
, &s
->period
);
178 qemu_get_be64s(f
, &s
->last_event
);
179 qemu_get_be64s(f
, &s
->next_event
);
180 qemu_get_timer(f
, s
->timer
);
183 ptimer_state
*ptimer_init(QEMUBH
*bh
)
187 s
= (ptimer_state
*)qemu_mallocz(sizeof(ptimer_state
));
189 s
->timer
= qemu_new_timer(vm_clock
, ptimer_tick
, s
);