xref: /openbmc/qemu/hw/core/ptimer.c (revision ce437484)
1 /*
2  * General purpose implementation of a simple periodic countdown timer.
3  *
4  * Copyright (c) 2007 CodeSourcery.
5  *
6  * This code is licensed under the GNU LGPL.
7  */
8 
9 #include "qemu/osdep.h"
10 #include "hw/ptimer.h"
11 #include "migration/vmstate.h"
12 #include "qemu/host-utils.h"
13 #include "sysemu/replay.h"
14 #include "sysemu/cpu-timers.h"
15 #include "sysemu/qtest.h"
16 #include "block/aio.h"
17 #include "sysemu/cpus.h"
18 
19 #define DELTA_ADJUST     1
20 #define DELTA_NO_ADJUST -1
21 
22 struct ptimer_state
23 {
24     uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot.  */
25     uint64_t limit;
26     uint64_t delta;
27     uint32_t period_frac;
28     int64_t period;
29     int64_t last_event;
30     int64_t next_event;
31     uint8_t policy_mask;
32     QEMUTimer *timer;
33     ptimer_cb callback;
34     void *callback_opaque;
35     /*
36      * These track whether we're in a transaction block, and if we
37      * need to do a timer reload when the block finishes. They don't
38      * need to be migrated because migration can never happen in the
39      * middle of a transaction block.
40      */
41     bool in_transaction;
42     bool need_reload;
43 };
44 
45 /* Use a bottom-half routine to avoid reentrancy issues.  */
46 static void ptimer_trigger(ptimer_state *s)
47 {
48     s->callback(s->callback_opaque);
49 }
50 
51 static void ptimer_reload(ptimer_state *s, int delta_adjust)
52 {
53     uint32_t period_frac;
54     uint64_t period;
55     uint64_t delta;
56     bool suppress_trigger = false;
57 
58     /*
59      * Note that if delta_adjust is 0 then we must be here because of
60      * a count register write or timer start, not because of timer expiry.
61      * In that case the policy might require us to suppress the timer trigger
62      * that we would otherwise generate for a zero delta.
63      */
64     if (delta_adjust == 0 &&
65         (s->policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT)) {
66         suppress_trigger = true;
67     }
68     if (s->delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)
69         && !suppress_trigger) {
70         ptimer_trigger(s);
71     }
72 
73     /*
74      * Note that ptimer_trigger() might call the device callback function,
75      * which can then modify timer state, so we must not cache any fields
76      * from ptimer_state until after we have called it.
77      */
78     delta = s->delta;
79     period = s->period;
80     period_frac = s->period_frac;
81 
82     if (delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) {
83         delta = s->delta = s->limit;
84     }
85 
86     if (s->period == 0) {
87         if (!qtest_enabled()) {
88             fprintf(stderr, "Timer with period zero, disabling\n");
89         }
90         timer_del(s->timer);
91         s->enabled = 0;
92         return;
93     }
94 
95     if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) {
96         if (delta_adjust != DELTA_NO_ADJUST) {
97             delta += delta_adjust;
98         }
99     }
100 
101     if (delta == 0 && (s->policy_mask & PTIMER_POLICY_CONTINUOUS_TRIGGER)) {
102         if (s->enabled == 1 && s->limit == 0) {
103             delta = 1;
104         }
105     }
106 
107     if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
108         if (delta_adjust != DELTA_NO_ADJUST) {
109             delta = 1;
110         }
111     }
112 
113     if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) {
114         if (s->enabled == 1 && s->limit != 0) {
115             delta = 1;
116         }
117     }
118 
119     if (delta == 0) {
120         if (s->enabled == 0) {
121             /* trigger callback disabled the timer already */
122             return;
123         }
124         if (!qtest_enabled()) {
125             fprintf(stderr, "Timer with delta zero, disabling\n");
126         }
127         timer_del(s->timer);
128         s->enabled = 0;
129         return;
130     }
131 
132     /*
133      * Artificially limit timeout rate to something
134      * achievable under QEMU.  Otherwise, QEMU spends all
135      * its time generating timer interrupts, and there
136      * is no forward progress.
137      * About ten microseconds is the fastest that really works
138      * on the current generation of host machines.
139      */
140 
141     if (s->enabled == 1 && (delta * period < 10000) &&
142         !icount_enabled() && !qtest_enabled()) {
143         period = 10000 / delta;
144         period_frac = 0;
145     }
146 
147     s->last_event = s->next_event;
148     s->next_event = s->last_event + delta * period;
149     if (period_frac) {
150         s->next_event += ((int64_t)period_frac * delta) >> 32;
151     }
152     timer_mod(s->timer, s->next_event);
153 }
154 
155 static void ptimer_tick(void *opaque)
156 {
157     ptimer_state *s = (ptimer_state *)opaque;
158     bool trigger = true;
159 
160     /*
161      * We perform all the tick actions within a begin/commit block
162      * because the callback function that ptimer_trigger() calls
163      * might make calls into the ptimer APIs that provoke another
164      * trigger, and we want that to cause the callback function
165      * to be called iteratively, not recursively.
166      */
167     ptimer_transaction_begin(s);
168 
169     if (s->enabled == 2) {
170         s->delta = 0;
171         s->enabled = 0;
172     } else {
173         int delta_adjust = DELTA_ADJUST;
174 
175         if (s->delta == 0 || s->limit == 0) {
176             /* If a "continuous trigger" policy is not used and limit == 0,
177                we should error out. delta == 0 means that this tick is
178                caused by a "no immediate reload" policy, so it shouldn't
179                be adjusted.  */
180             delta_adjust = DELTA_NO_ADJUST;
181         }
182 
183         if (!(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
184             /* Avoid re-trigger on deferred reload if "no immediate trigger"
185                policy isn't used.  */
186             trigger = (delta_adjust == DELTA_ADJUST);
187         }
188 
189         s->delta = s->limit;
190 
191         ptimer_reload(s, delta_adjust);
192     }
193 
194     if (trigger) {
195         ptimer_trigger(s);
196     }
197 
198     ptimer_transaction_commit(s);
199 }
200 
201 uint64_t ptimer_get_count(ptimer_state *s)
202 {
203     uint64_t counter;
204 
205     if (s->enabled && s->delta != 0) {
206         int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
207         int64_t next = s->next_event;
208         int64_t last = s->last_event;
209         bool expired = (now - next >= 0);
210         bool oneshot = (s->enabled == 2);
211 
212         /* Figure out the current counter value.  */
213         if (expired) {
214             /* Prevent timer underflowing if it should already have
215                triggered.  */
216             counter = 0;
217         } else {
218             uint64_t rem;
219             uint64_t div;
220             int clz1, clz2;
221             int shift;
222             uint32_t period_frac = s->period_frac;
223             uint64_t period = s->period;
224 
225             if (!oneshot && (s->delta * period < 10000) &&
226                 !icount_enabled() && !qtest_enabled()) {
227                 period = 10000 / s->delta;
228                 period_frac = 0;
229             }
230 
231             /* We need to divide time by period, where time is stored in
232                rem (64-bit integer) and period is stored in period/period_frac
233                (64.32 fixed point).
234 
235                Doing full precision division is hard, so scale values and
236                do a 64-bit division.  The result should be rounded down,
237                so that the rounding error never causes the timer to go
238                backwards.
239             */
240 
241             rem = next - now;
242             div = period;
243 
244             clz1 = clz64(rem);
245             clz2 = clz64(div);
246             shift = clz1 < clz2 ? clz1 : clz2;
247 
248             rem <<= shift;
249             div <<= shift;
250             if (shift >= 32) {
251                 div |= ((uint64_t)period_frac << (shift - 32));
252             } else {
253                 if (shift != 0)
254                     div |= (period_frac >> (32 - shift));
255                 /* Look at remaining bits of period_frac and round div up if
256                    necessary.  */
257                 if ((uint32_t)(period_frac << shift))
258                     div += 1;
259             }
260             counter = rem / div;
261 
262             if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) {
263                 /* Before wrapping around, timer should stay with counter = 0
264                    for a one period.  */
265                 if (!oneshot && s->delta == s->limit) {
266                     if (now == last) {
267                         /* Counter == delta here, check whether it was
268                            adjusted and if it was, then right now it is
269                            that "one period".  */
270                         if (counter == s->limit + DELTA_ADJUST) {
271                             return 0;
272                         }
273                     } else if (counter == s->limit) {
274                         /* Since the counter is rounded down and now != last,
275                            the counter == limit means that delta was adjusted
276                            by +1 and right now it is that adjusted period.  */
277                         return 0;
278                     }
279                 }
280             }
281         }
282 
283         if (s->policy_mask & PTIMER_POLICY_NO_COUNTER_ROUND_DOWN) {
284             /* If now == last then delta == limit, i.e. the counter already
285                represents the correct value. It would be rounded down a 1ns
286                later.  */
287             if (now != last) {
288                 counter += 1;
289             }
290         }
291     } else {
292         counter = s->delta;
293     }
294     return counter;
295 }
296 
297 void ptimer_set_count(ptimer_state *s, uint64_t count)
298 {
299     assert(s->in_transaction);
300     s->delta = count;
301     if (s->enabled) {
302         s->need_reload = true;
303     }
304 }
305 
306 void ptimer_run(ptimer_state *s, int oneshot)
307 {
308     bool was_disabled = !s->enabled;
309 
310     assert(s->in_transaction);
311 
312     if (was_disabled && s->period == 0) {
313         if (!qtest_enabled()) {
314             fprintf(stderr, "Timer with period zero, disabling\n");
315         }
316         return;
317     }
318     s->enabled = oneshot ? 2 : 1;
319     if (was_disabled) {
320         s->need_reload = true;
321     }
322 }
323 
324 /* Pause a timer.  Note that this may cause it to "lose" time, even if it
325    is immediately restarted.  */
326 void ptimer_stop(ptimer_state *s)
327 {
328     assert(s->in_transaction);
329 
330     if (!s->enabled)
331         return;
332 
333     s->delta = ptimer_get_count(s);
334     timer_del(s->timer);
335     s->enabled = 0;
336     s->need_reload = false;
337 }
338 
339 /* Set counter increment interval in nanoseconds.  */
340 void ptimer_set_period(ptimer_state *s, int64_t period)
341 {
342     assert(s->in_transaction);
343     s->delta = ptimer_get_count(s);
344     s->period = period;
345     s->period_frac = 0;
346     if (s->enabled) {
347         s->need_reload = true;
348     }
349 }
350 
351 /* Set counter frequency in Hz.  */
352 void ptimer_set_freq(ptimer_state *s, uint32_t freq)
353 {
354     assert(s->in_transaction);
355     s->delta = ptimer_get_count(s);
356     s->period = 1000000000ll / freq;
357     s->period_frac = (1000000000ll << 32) / freq;
358     if (s->enabled) {
359         s->need_reload = true;
360     }
361 }
362 
363 /* Set the initial countdown value.  If reload is nonzero then also set
364    count = limit.  */
365 void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
366 {
367     assert(s->in_transaction);
368     s->limit = limit;
369     if (reload)
370         s->delta = limit;
371     if (s->enabled && reload) {
372         s->need_reload = true;
373     }
374 }
375 
376 uint64_t ptimer_get_limit(ptimer_state *s)
377 {
378     return s->limit;
379 }
380 
381 void ptimer_transaction_begin(ptimer_state *s)
382 {
383     assert(!s->in_transaction);
384     s->in_transaction = true;
385     s->need_reload = false;
386 }
387 
388 void ptimer_transaction_commit(ptimer_state *s)
389 {
390     assert(s->in_transaction);
391     /*
392      * We must loop here because ptimer_reload() can call the callback
393      * function, which might then update ptimer state in a way that
394      * means we need to do another reload and possibly another callback.
395      * A disabled timer never needs reloading (and if we don't check
396      * this then we loop forever if ptimer_reload() disables the timer).
397      */
398     while (s->need_reload && s->enabled) {
399         s->need_reload = false;
400         s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
401         ptimer_reload(s, 0);
402     }
403     /* Now we've finished reload we can leave the transaction block. */
404     s->in_transaction = false;
405 }
406 
407 const VMStateDescription vmstate_ptimer = {
408     .name = "ptimer",
409     .version_id = 1,
410     .minimum_version_id = 1,
411     .fields = (VMStateField[]) {
412         VMSTATE_UINT8(enabled, ptimer_state),
413         VMSTATE_UINT64(limit, ptimer_state),
414         VMSTATE_UINT64(delta, ptimer_state),
415         VMSTATE_UINT32(period_frac, ptimer_state),
416         VMSTATE_INT64(period, ptimer_state),
417         VMSTATE_INT64(last_event, ptimer_state),
418         VMSTATE_INT64(next_event, ptimer_state),
419         VMSTATE_TIMER_PTR(timer, ptimer_state),
420         VMSTATE_END_OF_LIST()
421     }
422 };
423 
424 ptimer_state *ptimer_init(ptimer_cb callback, void *callback_opaque,
425                           uint8_t policy_mask)
426 {
427     ptimer_state *s;
428 
429     /* The callback function is mandatory. */
430     assert(callback);
431 
432     s = g_new0(ptimer_state, 1);
433     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s);
434     s->policy_mask = policy_mask;
435     s->callback = callback;
436     s->callback_opaque = callback_opaque;
437 
438     /*
439      * These two policies are incompatible -- trigger-on-decrement implies
440      * a timer trigger when the count becomes 0, but no-immediate-trigger
441      * implies a trigger when the count stops being 0.
442      */
443     assert(!((policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) &&
444              (policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)));
445     return s;
446 }
447 
448 void ptimer_free(ptimer_state *s)
449 {
450     timer_free(s->timer);
451     g_free(s);
452 }
453