blob: 47964a67e1644ebcd4c413b89985a6749acf5eb0 [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook423f0742007-05-23 00:06:54 +00002 * General purpose implementation of a simple periodic countdown timer.
3 *
4 * Copyright (c) 2007 CodeSourcery.
5 *
6 * This code is licenced under the GNU LGPL.
7 */
pbrook87ecb682007-11-17 17:14:51 +00008#include "hw.h"
9#include "qemu-timer.h"
pbrookd0a981b2009-03-31 14:34:24 +000010#include "host-utils.h"
pbrook423f0742007-05-23 00:06:54 +000011
12struct ptimer_state
13{
Juan Quintela852f7712010-12-01 23:51:14 +010014 uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
blueswir18d05ea82007-05-24 19:48:41 +000015 uint64_t limit;
16 uint64_t delta;
pbrook423f0742007-05-23 00:06:54 +000017 uint32_t period_frac;
18 int64_t period;
19 int64_t last_event;
20 int64_t next_event;
21 QEMUBH *bh;
22 QEMUTimer *timer;
23};
24
25/* Use a bottom-half routine to avoid reentrancy issues. */
26static void ptimer_trigger(ptimer_state *s)
27{
28 if (s->bh) {
29 qemu_bh_schedule(s->bh);
30 }
31}
32
33static void ptimer_reload(ptimer_state *s)
34{
35 if (s->delta == 0) {
36 ptimer_trigger(s);
37 s->delta = s->limit;
38 }
39 if (s->delta == 0 || s->period == 0) {
40 fprintf(stderr, "Timer with period zero, disabling\n");
41 s->enabled = 0;
42 return;
43 }
44
45 s->last_event = s->next_event;
46 s->next_event = s->last_event + s->delta * s->period;
47 if (s->period_frac) {
48 s->next_event += ((int64_t)s->period_frac * s->delta) >> 32;
49 }
50 qemu_mod_timer(s->timer, s->next_event);
51}
52
53static void ptimer_tick(void *opaque)
54{
55 ptimer_state *s = (ptimer_state *)opaque;
56 ptimer_trigger(s);
57 s->delta = 0;
58 if (s->enabled == 2) {
59 s->enabled = 0;
60 } else {
61 ptimer_reload(s);
62 }
63}
64
blueswir18d05ea82007-05-24 19:48:41 +000065uint64_t ptimer_get_count(ptimer_state *s)
pbrook423f0742007-05-23 00:06:54 +000066{
67 int64_t now;
blueswir18d05ea82007-05-24 19:48:41 +000068 uint64_t counter;
pbrook423f0742007-05-23 00:06:54 +000069
70 if (s->enabled) {
Paolo Bonzini74475452011-03-11 16:47:48 +010071 now = qemu_get_clock_ns(vm_clock);
pbrook423f0742007-05-23 00:06:54 +000072 /* Figure out the current counter value. */
73 if (now - s->next_event > 0
74 || s->period == 0) {
75 /* Prevent timer underflowing if it should already have
76 triggered. */
77 counter = 0;
78 } else {
blueswir18d05ea82007-05-24 19:48:41 +000079 uint64_t rem;
80 uint64_t div;
pbrookd0a981b2009-03-31 14:34:24 +000081 int clz1, clz2;
82 int shift;
83
84 /* We need to divide time by period, where time is stored in
85 rem (64-bit integer) and period is stored in period/period_frac
86 (64.32 fixed point).
87
88 Doing full precision division is hard, so scale values and
89 do a 64-bit division. The result should be rounded down,
90 so that the rounding error never causes the timer to go
91 backwards.
92 */
pbrook423f0742007-05-23 00:06:54 +000093
94 rem = s->next_event - now;
95 div = s->period;
pbrookd0a981b2009-03-31 14:34:24 +000096
97 clz1 = clz64(rem);
98 clz2 = clz64(div);
99 shift = clz1 < clz2 ? clz1 : clz2;
100
101 rem <<= shift;
102 div <<= shift;
103 if (shift >= 32) {
104 div |= ((uint64_t)s->period_frac << (shift - 32));
105 } else {
106 if (shift != 0)
107 div |= (s->period_frac >> (32 - shift));
108 /* Look at remaining bits of period_frac and round div up if
109 necessary. */
110 if ((uint32_t)(s->period_frac << shift))
111 div += 1;
112 }
pbrook423f0742007-05-23 00:06:54 +0000113 counter = rem / div;
114 }
115 } else {
116 counter = s->delta;
117 }
118 return counter;
119}
120
blueswir18d05ea82007-05-24 19:48:41 +0000121void ptimer_set_count(ptimer_state *s, uint64_t count)
pbrook423f0742007-05-23 00:06:54 +0000122{
123 s->delta = count;
124 if (s->enabled) {
Paolo Bonzini74475452011-03-11 16:47:48 +0100125 s->next_event = qemu_get_clock_ns(vm_clock);
pbrook423f0742007-05-23 00:06:54 +0000126 ptimer_reload(s);
127 }
128}
129
130void ptimer_run(ptimer_state *s, int oneshot)
131{
pbrook98fc5612008-05-25 14:05:47 +0000132 if (s->enabled) {
133 return;
134 }
pbrook423f0742007-05-23 00:06:54 +0000135 if (s->period == 0) {
136 fprintf(stderr, "Timer with period zero, disabling\n");
137 return;
138 }
139 s->enabled = oneshot ? 2 : 1;
Paolo Bonzini74475452011-03-11 16:47:48 +0100140 s->next_event = qemu_get_clock_ns(vm_clock);
pbrook423f0742007-05-23 00:06:54 +0000141 ptimer_reload(s);
142}
143
blueswir18d05ea82007-05-24 19:48:41 +0000144/* Pause a timer. Note that this may cause it to "lose" time, even if it
pbrook423f0742007-05-23 00:06:54 +0000145 is immediately restarted. */
146void ptimer_stop(ptimer_state *s)
147{
148 if (!s->enabled)
149 return;
150
151 s->delta = ptimer_get_count(s);
152 qemu_del_timer(s->timer);
153 s->enabled = 0;
154}
155
156/* Set counter increment interval in nanoseconds. */
157void ptimer_set_period(ptimer_state *s, int64_t period)
158{
pbrook423f0742007-05-23 00:06:54 +0000159 s->period = period;
160 s->period_frac = 0;
blueswir18d05ea82007-05-24 19:48:41 +0000161 if (s->enabled) {
Paolo Bonzini74475452011-03-11 16:47:48 +0100162 s->next_event = qemu_get_clock_ns(vm_clock);
blueswir18d05ea82007-05-24 19:48:41 +0000163 ptimer_reload(s);
164 }
pbrook423f0742007-05-23 00:06:54 +0000165}
166
167/* Set counter frequency in Hz. */
168void ptimer_set_freq(ptimer_state *s, uint32_t freq)
169{
pbrook423f0742007-05-23 00:06:54 +0000170 s->period = 1000000000ll / freq;
171 s->period_frac = (1000000000ll << 32) / freq;
blueswir18d05ea82007-05-24 19:48:41 +0000172 if (s->enabled) {
Paolo Bonzini74475452011-03-11 16:47:48 +0100173 s->next_event = qemu_get_clock_ns(vm_clock);
blueswir18d05ea82007-05-24 19:48:41 +0000174 ptimer_reload(s);
175 }
pbrook423f0742007-05-23 00:06:54 +0000176}
177
178/* Set the initial countdown value. If reload is nonzero then also set
179 count = limit. */
blueswir18d05ea82007-05-24 19:48:41 +0000180void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
pbrook423f0742007-05-23 00:06:54 +0000181{
pbrook423f0742007-05-23 00:06:54 +0000182 s->limit = limit;
183 if (reload)
184 s->delta = limit;
pbrook62ea5b02007-06-03 10:44:47 +0000185 if (s->enabled && reload) {
Paolo Bonzini74475452011-03-11 16:47:48 +0100186 s->next_event = qemu_get_clock_ns(vm_clock);
blueswir18d05ea82007-05-24 19:48:41 +0000187 ptimer_reload(s);
188 }
189}
190
Juan Quintela852f7712010-12-01 23:51:14 +0100191const VMStateDescription vmstate_ptimer = {
Blue Swirl55a6e51f2009-08-31 19:30:15 +0000192 .name = "ptimer",
Juan Quintela852f7712010-12-01 23:51:14 +0100193 .version_id = 1,
194 .minimum_version_id = 1,
195 .minimum_version_id_old = 1,
196 .fields = (VMStateField[]) {
197 VMSTATE_UINT8(enabled, ptimer_state),
198 VMSTATE_UINT64(limit, ptimer_state),
199 VMSTATE_UINT64(delta, ptimer_state),
200 VMSTATE_UINT32(period_frac, ptimer_state),
201 VMSTATE_INT64(period, ptimer_state),
202 VMSTATE_INT64(last_event, ptimer_state),
203 VMSTATE_INT64(next_event, ptimer_state),
204 VMSTATE_TIMER(timer, ptimer_state),
205 VMSTATE_END_OF_LIST()
206 }
Blue Swirl55a6e51f2009-08-31 19:30:15 +0000207};
208
pbrook423f0742007-05-23 00:06:54 +0000209ptimer_state *ptimer_init(QEMUBH *bh)
210{
211 ptimer_state *s;
212
213 s = (ptimer_state *)qemu_mallocz(sizeof(ptimer_state));
214 s->bh = bh;
Paolo Bonzini74475452011-03-11 16:47:48 +0100215 s->timer = qemu_new_timer_ns(vm_clock, ptimer_tick, s);
pbrook423f0742007-05-23 00:06:54 +0000216 return s;
217}