root / hw / ptimer.c @ 814cd3ac
History | View | Annotate | Download (6 kB)
1 | 5fafdf24 | ths | /*
|
---|---|---|---|
2 | 423f0742 | pbrook | * General purpose implementation of a simple periodic countdown timer.
|
3 | 423f0742 | pbrook | *
|
4 | 423f0742 | pbrook | * Copyright (c) 2007 CodeSourcery.
|
5 | 423f0742 | pbrook | *
|
6 | 8e31bf38 | Matthew Fernandez | * This code is licensed under the GNU LGPL.
|
7 | 423f0742 | pbrook | */
|
8 | 87ecb68b | pbrook | #include "hw.h" |
9 | 87ecb68b | pbrook | #include "qemu-timer.h" |
10 | 49d4d9b6 | Paolo Bonzini | #include "ptimer.h" |
11 | d0a981b2 | pbrook | #include "host-utils.h" |
12 | 423f0742 | pbrook | |
13 | 423f0742 | pbrook | struct ptimer_state
|
14 | 423f0742 | pbrook | { |
15 | 852f771e | Juan Quintela | uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
|
16 | 8d05ea8a | blueswir1 | uint64_t limit; |
17 | 8d05ea8a | blueswir1 | uint64_t delta; |
18 | 423f0742 | pbrook | uint32_t period_frac; |
19 | 423f0742 | pbrook | int64_t period; |
20 | 423f0742 | pbrook | int64_t last_event; |
21 | 423f0742 | pbrook | int64_t next_event; |
22 | 423f0742 | pbrook | QEMUBH *bh; |
23 | 423f0742 | pbrook | QEMUTimer *timer; |
24 | 423f0742 | pbrook | }; |
25 | 423f0742 | pbrook | |
26 | 423f0742 | pbrook | /* Use a bottom-half routine to avoid reentrancy issues. */
|
27 | 423f0742 | pbrook | static void ptimer_trigger(ptimer_state *s) |
28 | 423f0742 | pbrook | { |
29 | 423f0742 | pbrook | if (s->bh) {
|
30 | 423f0742 | pbrook | qemu_bh_schedule(s->bh); |
31 | 423f0742 | pbrook | } |
32 | 423f0742 | pbrook | } |
33 | 423f0742 | pbrook | |
34 | 423f0742 | pbrook | static void ptimer_reload(ptimer_state *s) |
35 | 423f0742 | pbrook | { |
36 | 423f0742 | pbrook | if (s->delta == 0) { |
37 | 423f0742 | pbrook | ptimer_trigger(s); |
38 | 423f0742 | pbrook | s->delta = s->limit; |
39 | 423f0742 | pbrook | } |
40 | 423f0742 | pbrook | if (s->delta == 0 || s->period == 0) { |
41 | 423f0742 | pbrook | fprintf(stderr, "Timer with period zero, disabling\n");
|
42 | 423f0742 | pbrook | s->enabled = 0;
|
43 | 423f0742 | pbrook | return;
|
44 | 423f0742 | pbrook | } |
45 | 423f0742 | pbrook | |
46 | 423f0742 | pbrook | s->last_event = s->next_event; |
47 | 423f0742 | pbrook | s->next_event = s->last_event + s->delta * s->period; |
48 | 423f0742 | pbrook | if (s->period_frac) {
|
49 | 423f0742 | pbrook | s->next_event += ((int64_t)s->period_frac * s->delta) >> 32;
|
50 | 423f0742 | pbrook | } |
51 | 423f0742 | pbrook | qemu_mod_timer(s->timer, s->next_event); |
52 | 423f0742 | pbrook | } |
53 | 423f0742 | pbrook | |
54 | 423f0742 | pbrook | static void ptimer_tick(void *opaque) |
55 | 423f0742 | pbrook | { |
56 | 423f0742 | pbrook | ptimer_state *s = (ptimer_state *)opaque; |
57 | 423f0742 | pbrook | ptimer_trigger(s); |
58 | 423f0742 | pbrook | s->delta = 0;
|
59 | 423f0742 | pbrook | if (s->enabled == 2) { |
60 | 423f0742 | pbrook | s->enabled = 0;
|
61 | 423f0742 | pbrook | } else {
|
62 | 423f0742 | pbrook | ptimer_reload(s); |
63 | 423f0742 | pbrook | } |
64 | 423f0742 | pbrook | } |
65 | 423f0742 | pbrook | |
66 | 8d05ea8a | blueswir1 | uint64_t ptimer_get_count(ptimer_state *s) |
67 | 423f0742 | pbrook | { |
68 | 423f0742 | pbrook | int64_t now; |
69 | 8d05ea8a | blueswir1 | uint64_t counter; |
70 | 423f0742 | pbrook | |
71 | 423f0742 | pbrook | if (s->enabled) {
|
72 | 74475455 | Paolo Bonzini | now = qemu_get_clock_ns(vm_clock); |
73 | 423f0742 | pbrook | /* Figure out the current counter value. */
|
74 | 423f0742 | pbrook | if (now - s->next_event > 0 |
75 | 423f0742 | pbrook | || s->period == 0) {
|
76 | 423f0742 | pbrook | /* Prevent timer underflowing if it should already have
|
77 | 423f0742 | pbrook | triggered. */
|
78 | 423f0742 | pbrook | counter = 0;
|
79 | 423f0742 | pbrook | } else {
|
80 | 8d05ea8a | blueswir1 | uint64_t rem; |
81 | 8d05ea8a | blueswir1 | uint64_t div; |
82 | d0a981b2 | pbrook | int clz1, clz2;
|
83 | d0a981b2 | pbrook | int shift;
|
84 | d0a981b2 | pbrook | |
85 | d0a981b2 | pbrook | /* We need to divide time by period, where time is stored in
|
86 | d0a981b2 | pbrook | rem (64-bit integer) and period is stored in period/period_frac
|
87 | d0a981b2 | pbrook | (64.32 fixed point).
|
88 | d0a981b2 | pbrook |
|
89 | d0a981b2 | pbrook | Doing full precision division is hard, so scale values and
|
90 | d0a981b2 | pbrook | do a 64-bit division. The result should be rounded down,
|
91 | d0a981b2 | pbrook | so that the rounding error never causes the timer to go
|
92 | d0a981b2 | pbrook | backwards.
|
93 | d0a981b2 | pbrook | */
|
94 | 423f0742 | pbrook | |
95 | 423f0742 | pbrook | rem = s->next_event - now; |
96 | 423f0742 | pbrook | div = s->period; |
97 | d0a981b2 | pbrook | |
98 | d0a981b2 | pbrook | clz1 = clz64(rem); |
99 | d0a981b2 | pbrook | clz2 = clz64(div); |
100 | d0a981b2 | pbrook | shift = clz1 < clz2 ? clz1 : clz2; |
101 | d0a981b2 | pbrook | |
102 | d0a981b2 | pbrook | rem <<= shift; |
103 | d0a981b2 | pbrook | div <<= shift; |
104 | d0a981b2 | pbrook | if (shift >= 32) { |
105 | d0a981b2 | pbrook | div |= ((uint64_t)s->period_frac << (shift - 32));
|
106 | d0a981b2 | pbrook | } else {
|
107 | d0a981b2 | pbrook | if (shift != 0) |
108 | d0a981b2 | pbrook | div |= (s->period_frac >> (32 - shift));
|
109 | d0a981b2 | pbrook | /* Look at remaining bits of period_frac and round div up if
|
110 | d0a981b2 | pbrook | necessary. */
|
111 | d0a981b2 | pbrook | if ((uint32_t)(s->period_frac << shift))
|
112 | d0a981b2 | pbrook | div += 1;
|
113 | d0a981b2 | pbrook | } |
114 | 423f0742 | pbrook | counter = rem / div; |
115 | 423f0742 | pbrook | } |
116 | 423f0742 | pbrook | } else {
|
117 | 423f0742 | pbrook | counter = s->delta; |
118 | 423f0742 | pbrook | } |
119 | 423f0742 | pbrook | return counter;
|
120 | 423f0742 | pbrook | } |
121 | 423f0742 | pbrook | |
122 | 8d05ea8a | blueswir1 | void ptimer_set_count(ptimer_state *s, uint64_t count)
|
123 | 423f0742 | pbrook | { |
124 | 423f0742 | pbrook | s->delta = count; |
125 | 423f0742 | pbrook | if (s->enabled) {
|
126 | 74475455 | Paolo Bonzini | s->next_event = qemu_get_clock_ns(vm_clock); |
127 | 423f0742 | pbrook | ptimer_reload(s); |
128 | 423f0742 | pbrook | } |
129 | 423f0742 | pbrook | } |
130 | 423f0742 | pbrook | |
131 | 423f0742 | pbrook | void ptimer_run(ptimer_state *s, int oneshot) |
132 | 423f0742 | pbrook | { |
133 | 98fc5614 | pbrook | if (s->enabled) {
|
134 | 98fc5614 | pbrook | return;
|
135 | 98fc5614 | pbrook | } |
136 | 423f0742 | pbrook | if (s->period == 0) { |
137 | 423f0742 | pbrook | fprintf(stderr, "Timer with period zero, disabling\n");
|
138 | 423f0742 | pbrook | return;
|
139 | 423f0742 | pbrook | } |
140 | 423f0742 | pbrook | s->enabled = oneshot ? 2 : 1; |
141 | 74475455 | Paolo Bonzini | s->next_event = qemu_get_clock_ns(vm_clock); |
142 | 423f0742 | pbrook | ptimer_reload(s); |
143 | 423f0742 | pbrook | } |
144 | 423f0742 | pbrook | |
145 | 8d05ea8a | blueswir1 | /* Pause a timer. Note that this may cause it to "lose" time, even if it
|
146 | 423f0742 | pbrook | is immediately restarted. */
|
147 | 423f0742 | pbrook | void ptimer_stop(ptimer_state *s)
|
148 | 423f0742 | pbrook | { |
149 | 423f0742 | pbrook | if (!s->enabled)
|
150 | 423f0742 | pbrook | return;
|
151 | 423f0742 | pbrook | |
152 | 423f0742 | pbrook | s->delta = ptimer_get_count(s); |
153 | 423f0742 | pbrook | qemu_del_timer(s->timer); |
154 | 423f0742 | pbrook | s->enabled = 0;
|
155 | 423f0742 | pbrook | } |
156 | 423f0742 | pbrook | |
157 | 423f0742 | pbrook | /* Set counter increment interval in nanoseconds. */
|
158 | 423f0742 | pbrook | void ptimer_set_period(ptimer_state *s, int64_t period)
|
159 | 423f0742 | pbrook | { |
160 | 423f0742 | pbrook | s->period = period; |
161 | 423f0742 | pbrook | s->period_frac = 0;
|
162 | 8d05ea8a | blueswir1 | if (s->enabled) {
|
163 | 74475455 | Paolo Bonzini | s->next_event = qemu_get_clock_ns(vm_clock); |
164 | 8d05ea8a | blueswir1 | ptimer_reload(s); |
165 | 8d05ea8a | blueswir1 | } |
166 | 423f0742 | pbrook | } |
167 | 423f0742 | pbrook | |
168 | 423f0742 | pbrook | /* Set counter frequency in Hz. */
|
169 | 423f0742 | pbrook | void ptimer_set_freq(ptimer_state *s, uint32_t freq)
|
170 | 423f0742 | pbrook | { |
171 | 423f0742 | pbrook | s->period = 1000000000ll / freq;
|
172 | 423f0742 | pbrook | s->period_frac = (1000000000ll << 32) / freq; |
173 | 8d05ea8a | blueswir1 | if (s->enabled) {
|
174 | 74475455 | Paolo Bonzini | s->next_event = qemu_get_clock_ns(vm_clock); |
175 | 8d05ea8a | blueswir1 | ptimer_reload(s); |
176 | 8d05ea8a | blueswir1 | } |
177 | 423f0742 | pbrook | } |
178 | 423f0742 | pbrook | |
179 | 423f0742 | pbrook | /* Set the initial countdown value. If reload is nonzero then also set
|
180 | 423f0742 | pbrook | count = limit. */
|
181 | 8d05ea8a | blueswir1 | void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload) |
182 | 423f0742 | pbrook | { |
183 | cf36b31d | Peter Chubb | /*
|
184 | cf36b31d | Peter Chubb | * Artificially limit timeout rate to something
|
185 | cf36b31d | Peter Chubb | * achievable under QEMU. Otherwise, QEMU spends all
|
186 | cf36b31d | Peter Chubb | * its time generating timer interrupts, and there
|
187 | cf36b31d | Peter Chubb | * is no forward progress.
|
188 | cf36b31d | Peter Chubb | * About ten microseconds is the fastest that really works
|
189 | cf36b31d | Peter Chubb | * on the current generation of host machines.
|
190 | cf36b31d | Peter Chubb | */
|
191 | cf36b31d | Peter Chubb | |
192 | cf36b31d | Peter Chubb | if (limit * s->period < 10000 && s->period) { |
193 | cf36b31d | Peter Chubb | limit = 10000 / s->period;
|
194 | cf36b31d | Peter Chubb | } |
195 | cf36b31d | Peter Chubb | |
196 | 423f0742 | pbrook | s->limit = limit; |
197 | 423f0742 | pbrook | if (reload)
|
198 | 423f0742 | pbrook | s->delta = limit; |
199 | 62ea5b0b | pbrook | if (s->enabled && reload) {
|
200 | 74475455 | Paolo Bonzini | s->next_event = qemu_get_clock_ns(vm_clock); |
201 | 8d05ea8a | blueswir1 | ptimer_reload(s); |
202 | 8d05ea8a | blueswir1 | } |
203 | 8d05ea8a | blueswir1 | } |
204 | 8d05ea8a | blueswir1 | |
205 | 852f771e | Juan Quintela | const VMStateDescription vmstate_ptimer = {
|
206 | 55a6e51f | Blue Swirl | .name = "ptimer",
|
207 | 852f771e | Juan Quintela | .version_id = 1,
|
208 | 852f771e | Juan Quintela | .minimum_version_id = 1,
|
209 | 852f771e | Juan Quintela | .minimum_version_id_old = 1,
|
210 | 852f771e | Juan Quintela | .fields = (VMStateField[]) { |
211 | 852f771e | Juan Quintela | VMSTATE_UINT8(enabled, ptimer_state), |
212 | 852f771e | Juan Quintela | VMSTATE_UINT64(limit, ptimer_state), |
213 | 852f771e | Juan Quintela | VMSTATE_UINT64(delta, ptimer_state), |
214 | 852f771e | Juan Quintela | VMSTATE_UINT32(period_frac, ptimer_state), |
215 | 852f771e | Juan Quintela | VMSTATE_INT64(period, ptimer_state), |
216 | 852f771e | Juan Quintela | VMSTATE_INT64(last_event, ptimer_state), |
217 | 852f771e | Juan Quintela | VMSTATE_INT64(next_event, ptimer_state), |
218 | 852f771e | Juan Quintela | VMSTATE_TIMER(timer, ptimer_state), |
219 | 852f771e | Juan Quintela | VMSTATE_END_OF_LIST() |
220 | 852f771e | Juan Quintela | } |
221 | 55a6e51f | Blue Swirl | }; |
222 | 55a6e51f | Blue Swirl | |
223 | 423f0742 | pbrook | ptimer_state *ptimer_init(QEMUBH *bh) |
224 | 423f0742 | pbrook | { |
225 | 423f0742 | pbrook | ptimer_state *s; |
226 | 423f0742 | pbrook | |
227 | 7267c094 | Anthony Liguori | s = (ptimer_state *)g_malloc0(sizeof(ptimer_state));
|
228 | 423f0742 | pbrook | s->bh = bh; |
229 | 74475455 | Paolo Bonzini | s->timer = qemu_new_timer_ns(vm_clock, ptimer_tick, s); |
230 | 423f0742 | pbrook | return s;
|
231 | 423f0742 | pbrook | } |