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