xref: /qemu/include/hw/ptimer.h (revision 78b6eaa6f3ea83d12fa698a7f0ec15a45f802d74)
149d4d9b6SPaolo Bonzini /*
249d4d9b6SPaolo Bonzini  * General purpose implementation of a simple periodic countdown timer.
349d4d9b6SPaolo Bonzini  *
449d4d9b6SPaolo Bonzini  * Copyright (c) 2007 CodeSourcery.
549d4d9b6SPaolo Bonzini  *
649d4d9b6SPaolo Bonzini  * This code is licensed under the GNU LGPL.
749d4d9b6SPaolo Bonzini  */
849d4d9b6SPaolo Bonzini #ifndef PTIMER_H
949d4d9b6SPaolo Bonzini #define PTIMER_H
1049d4d9b6SPaolo Bonzini 
111de7afc9SPaolo Bonzini #include "qemu/timer.h"
1249d4d9b6SPaolo Bonzini 
13a7a305aeSPeter Maydell /* The ptimer API implements a simple periodic countdown timer.
14a7a305aeSPeter Maydell  * The countdown timer has a value (which can be read and written via
15a7a305aeSPeter Maydell  * ptimer_get_count() and ptimer_set_count()). When it is enabled
16a7a305aeSPeter Maydell  * using ptimer_run(), the value will count downwards at the frequency
17a7a305aeSPeter Maydell  * which has been configured using ptimer_set_period() or ptimer_set_freq().
18a7a305aeSPeter Maydell  * When it reaches zero it will trigger a QEMU bottom half handler, and
19a7a305aeSPeter Maydell  * can be set to either reload itself from a specified limit value
20a7a305aeSPeter Maydell  * and keep counting down, or to stop (as a one-shot timer).
21a7a305aeSPeter Maydell  *
22a7a305aeSPeter Maydell  * Forgetting to set the period/frequency (or setting it to zero) is a
23a7a305aeSPeter Maydell  * bug in the QEMU device and will cause warning messages to be printed
24a7a305aeSPeter Maydell  * to stderr when the guest attempts to enable the timer.
25a7a305aeSPeter Maydell  */
26a7a305aeSPeter Maydell 
27e7ea81c3SDmitry Osipenko /* The default ptimer policy retains backward compatibility with the legacy
28e7ea81c3SDmitry Osipenko  * timers. Custom policies are adjusting the default one. Consider providing
29e7ea81c3SDmitry Osipenko  * a correct policy for your timer.
30e7ea81c3SDmitry Osipenko  *
31e7ea81c3SDmitry Osipenko  * The rough edges of the default policy:
32e7ea81c3SDmitry Osipenko  *  - Starting to run with a period = 0 emits error message and stops the
33e7ea81c3SDmitry Osipenko  *    timer without a trigger.
34e7ea81c3SDmitry Osipenko  *
35e7ea81c3SDmitry Osipenko  *  - Setting period to 0 of the running timer emits error message and
36e7ea81c3SDmitry Osipenko  *    stops the timer without a trigger.
37e7ea81c3SDmitry Osipenko  *
38e7ea81c3SDmitry Osipenko  *  - Starting to run with counter = 0 or setting it to "0" while timer
39e7ea81c3SDmitry Osipenko  *    is running causes a trigger and reloads counter with a limit value.
40e7ea81c3SDmitry Osipenko  *    If limit = 0, ptimer emits error message and stops the timer.
41e7ea81c3SDmitry Osipenko  *
42e7ea81c3SDmitry Osipenko  *  - Counter value of the running timer is one less than the actual value.
43e7ea81c3SDmitry Osipenko  *
44e7ea81c3SDmitry Osipenko  *  - Changing period/frequency of the running timer loses time elapsed
45e7ea81c3SDmitry Osipenko  *    since the last period, effectively restarting the timer with a
46e7ea81c3SDmitry Osipenko  *    counter = counter value at the moment of change (.i.e. one less).
47e7ea81c3SDmitry Osipenko  */
48e7ea81c3SDmitry Osipenko #define PTIMER_POLICY_DEFAULT               0
49e7ea81c3SDmitry Osipenko 
502b5c0322SDmitry Osipenko /* Periodic timer counter stays with "0" for a one period before wrapping
512b5c0322SDmitry Osipenko  * around.  */
522b5c0322SDmitry Osipenko #define PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD (1 << 0)
532b5c0322SDmitry Osipenko 
54ef0a9984SDmitry Osipenko /* Running periodic timer that has counter = limit = 0 would continuously
55ef0a9984SDmitry Osipenko  * re-trigger every period.  */
56ef0a9984SDmitry Osipenko #define PTIMER_POLICY_CONTINUOUS_TRIGGER    (1 << 1)
57ef0a9984SDmitry Osipenko 
5822471b8aSDmitry Osipenko /* Starting to run with/setting counter to "0" won't trigger immediately,
5922471b8aSDmitry Osipenko  * but after a one period for both oneshot and periodic modes.  */
6022471b8aSDmitry Osipenko #define PTIMER_POLICY_NO_IMMEDIATE_TRIGGER  (1 << 2)
6122471b8aSDmitry Osipenko 
623f6e6a13SDmitry Osipenko /* Starting to run with/setting counter to "0" won't re-load counter
633f6e6a13SDmitry Osipenko  * immediately, but after a one period.  */
643f6e6a13SDmitry Osipenko #define PTIMER_POLICY_NO_IMMEDIATE_RELOAD   (1 << 3)
653f6e6a13SDmitry Osipenko 
665580ea45SDmitry Osipenko /* Make counter value of the running timer represent the actual value and
675580ea45SDmitry Osipenko  * not the one less.  */
685580ea45SDmitry Osipenko #define PTIMER_POLICY_NO_COUNTER_ROUND_DOWN (1 << 4)
695580ea45SDmitry Osipenko 
70086ede32SPeter Maydell /*
71086ede32SPeter Maydell  * Starting to run with a zero counter, or setting the counter to "0" via
72086ede32SPeter Maydell  * ptimer_set_count() or ptimer_set_limit() will not trigger the timer
73086ede32SPeter Maydell  * (though it will cause a reload). Only a counter decrement to "0"
74086ede32SPeter Maydell  * will cause a trigger. Not compatible with NO_IMMEDIATE_TRIGGER;
75b0142262SPeter Maydell  * ptimer_init_with_bh() will assert() that you don't set both.
76086ede32SPeter Maydell  */
77086ede32SPeter Maydell #define PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT (1 << 5)
78086ede32SPeter Maydell 
7949d4d9b6SPaolo Bonzini /* ptimer.c */
8049d4d9b6SPaolo Bonzini typedef struct ptimer_state ptimer_state;
8149d4d9b6SPaolo Bonzini typedef void (*ptimer_cb)(void *opaque);
8249d4d9b6SPaolo Bonzini 
83a7a305aeSPeter Maydell /**
84b0142262SPeter Maydell  * ptimer_init_with_bh - Allocate and return a new ptimer
85a7a305aeSPeter Maydell  * @bh: QEMU bottom half which is run on timer expiry
86a7a305aeSPeter Maydell  * @policy: PTIMER_POLICY_* bits specifying behaviour
87a7a305aeSPeter Maydell  *
88a7a305aeSPeter Maydell  * The ptimer returned must be freed using ptimer_free().
89a7a305aeSPeter Maydell  * The ptimer takes ownership of @bh and will delete it
90a7a305aeSPeter Maydell  * when the ptimer is eventually freed.
91a7a305aeSPeter Maydell  */
92b0142262SPeter Maydell ptimer_state *ptimer_init_with_bh(QEMUBH *bh, uint8_t policy_mask);
93a7a305aeSPeter Maydell 
94a7a305aeSPeter Maydell /**
95*78b6eaa6SPeter Maydell  * ptimer_init - Allocate and return a new ptimer
96*78b6eaa6SPeter Maydell  * @callback: function to call on ptimer expiry
97*78b6eaa6SPeter Maydell  * @callback_opaque: opaque pointer passed to @callback
98*78b6eaa6SPeter Maydell  * @policy: PTIMER_POLICY_* bits specifying behaviour
99*78b6eaa6SPeter Maydell  *
100*78b6eaa6SPeter Maydell  * The ptimer returned must be freed using ptimer_free().
101*78b6eaa6SPeter Maydell  *
102*78b6eaa6SPeter Maydell  * If a ptimer is created using this API then will use the
103*78b6eaa6SPeter Maydell  * transaction-based API for modifying ptimer state: all calls
104*78b6eaa6SPeter Maydell  * to functions which modify ptimer state:
105*78b6eaa6SPeter Maydell  *  - ptimer_set_period()
106*78b6eaa6SPeter Maydell  *  - ptimer_set_freq()
107*78b6eaa6SPeter Maydell  *  - ptimer_set_limit()
108*78b6eaa6SPeter Maydell  *  - ptimer_set_count()
109*78b6eaa6SPeter Maydell  *  - ptimer_run()
110*78b6eaa6SPeter Maydell  *  - ptimer_stop()
111*78b6eaa6SPeter Maydell  * must be between matched calls to ptimer_transaction_begin()
112*78b6eaa6SPeter Maydell  * and ptimer_transaction_commit(). When ptimer_transaction_commit()
113*78b6eaa6SPeter Maydell  * is called it will evaluate the state of the timer after all the
114*78b6eaa6SPeter Maydell  * changes in the transaction, and call the callback if necessary.
115*78b6eaa6SPeter Maydell  *
116*78b6eaa6SPeter Maydell  * The callback function is always called from within a transaction
117*78b6eaa6SPeter Maydell  * begin/commit block, so the callback should not call the
118*78b6eaa6SPeter Maydell  * ptimer_transaction_begin() function itself. If the callback changes
119*78b6eaa6SPeter Maydell  * the ptimer state such that another ptimer expiry is triggered, then
120*78b6eaa6SPeter Maydell  * the callback will be called a second time after the first call returns.
121*78b6eaa6SPeter Maydell  */
122*78b6eaa6SPeter Maydell ptimer_state *ptimer_init(ptimer_cb callback,
123*78b6eaa6SPeter Maydell                           void *callback_opaque,
124*78b6eaa6SPeter Maydell                           uint8_t policy_mask);
125*78b6eaa6SPeter Maydell 
126*78b6eaa6SPeter Maydell /**
127a7a305aeSPeter Maydell  * ptimer_free - Free a ptimer
128a7a305aeSPeter Maydell  * @s: timer to free
129a7a305aeSPeter Maydell  *
130b0142262SPeter Maydell  * Free a ptimer created using ptimer_init_with_bh() (including
131a7a305aeSPeter Maydell  * deleting the bottom half which it is using).
132a7a305aeSPeter Maydell  */
133072bdb07SMarc-André Lureau void ptimer_free(ptimer_state *s);
134a7a305aeSPeter Maydell 
135a7a305aeSPeter Maydell /**
136*78b6eaa6SPeter Maydell  * ptimer_transaction_begin() - Start a ptimer modification transaction
137*78b6eaa6SPeter Maydell  *
138*78b6eaa6SPeter Maydell  * This function must be called before making any calls to functions
139*78b6eaa6SPeter Maydell  * which modify the ptimer's state (see the ptimer_init() documentation
140*78b6eaa6SPeter Maydell  * for a list of these), and must always have a matched call to
141*78b6eaa6SPeter Maydell  * ptimer_transaction_commit().
142*78b6eaa6SPeter Maydell  * It is an error to call this function for a BH-based ptimer;
143*78b6eaa6SPeter Maydell  * attempting to do this will trigger an assert.
144*78b6eaa6SPeter Maydell  */
145*78b6eaa6SPeter Maydell void ptimer_transaction_begin(ptimer_state *s);
146*78b6eaa6SPeter Maydell 
147*78b6eaa6SPeter Maydell /**
148*78b6eaa6SPeter Maydell  * ptimer_transaction_commit() - Commit a ptimer modification transaction
149*78b6eaa6SPeter Maydell  *
150*78b6eaa6SPeter Maydell  * This function must be called after calls to functions which modify
151*78b6eaa6SPeter Maydell  * the ptimer's state, and completes the update of the ptimer. If the
152*78b6eaa6SPeter Maydell  * ptimer state now means that we should trigger the timer expiry
153*78b6eaa6SPeter Maydell  * callback, it will be called directly.
154*78b6eaa6SPeter Maydell  */
155*78b6eaa6SPeter Maydell void ptimer_transaction_commit(ptimer_state *s);
156*78b6eaa6SPeter Maydell 
157*78b6eaa6SPeter Maydell /**
158a7a305aeSPeter Maydell  * ptimer_set_period - Set counter increment interval in nanoseconds
159a7a305aeSPeter Maydell  * @s: ptimer to configure
160a7a305aeSPeter Maydell  * @period: period of the counter in nanoseconds
161a7a305aeSPeter Maydell  *
162a7a305aeSPeter Maydell  * Note that if your counter behaviour is specified as having a
163a7a305aeSPeter Maydell  * particular frequency rather than a period then ptimer_set_freq()
164a7a305aeSPeter Maydell  * may be more appropriate.
165*78b6eaa6SPeter Maydell  *
166*78b6eaa6SPeter Maydell  * This function will assert if it is called outside a
167*78b6eaa6SPeter Maydell  * ptimer_transaction_begin/commit block, unless this is a bottom-half ptimer.
168a7a305aeSPeter Maydell  */
16949d4d9b6SPaolo Bonzini void ptimer_set_period(ptimer_state *s, int64_t period);
170a7a305aeSPeter Maydell 
171a7a305aeSPeter Maydell /**
172a7a305aeSPeter Maydell  * ptimer_set_freq - Set counter frequency in Hz
173a7a305aeSPeter Maydell  * @s: ptimer to configure
174a7a305aeSPeter Maydell  * @freq: counter frequency in Hz
175a7a305aeSPeter Maydell  *
176a7a305aeSPeter Maydell  * This does the same thing as ptimer_set_period(), so you only
177a7a305aeSPeter Maydell  * need to call one of them. If the counter behaviour is specified
178a7a305aeSPeter Maydell  * as setting the frequency then this function is more appropriate,
179a7a305aeSPeter Maydell  * because it allows specifying an effective period which is
180a7a305aeSPeter Maydell  * precise to fractions of a nanosecond, avoiding rounding errors.
181*78b6eaa6SPeter Maydell  *
182*78b6eaa6SPeter Maydell  * This function will assert if it is called outside a
183*78b6eaa6SPeter Maydell  * ptimer_transaction_begin/commit block, unless this is a bottom-half ptimer.
184a7a305aeSPeter Maydell  */
18549d4d9b6SPaolo Bonzini void ptimer_set_freq(ptimer_state *s, uint32_t freq);
186a7a305aeSPeter Maydell 
187a7a305aeSPeter Maydell /**
188a7a305aeSPeter Maydell  * ptimer_get_limit - Get the configured limit of the ptimer
189a7a305aeSPeter Maydell  * @s: ptimer to query
190a7a305aeSPeter Maydell  *
191a7a305aeSPeter Maydell  * This function returns the current limit (reload) value
192a7a305aeSPeter Maydell  * of the down-counter; that is, the value which it will be
193a7a305aeSPeter Maydell  * reset to when it hits zero.
194a7a305aeSPeter Maydell  *
195a7a305aeSPeter Maydell  * Generally timer devices using ptimers should be able to keep
196a7a305aeSPeter Maydell  * their reload register state inside the ptimer using the get
197a7a305aeSPeter Maydell  * and set limit functions rather than needing to also track it
198a7a305aeSPeter Maydell  * in their own state structure.
199a7a305aeSPeter Maydell  */
200578c4b2fSDmitry Osipenko uint64_t ptimer_get_limit(ptimer_state *s);
201a7a305aeSPeter Maydell 
202a7a305aeSPeter Maydell /**
203a7a305aeSPeter Maydell  * ptimer_set_limit - Set the limit of the ptimer
204a7a305aeSPeter Maydell  * @s: ptimer
205a7a305aeSPeter Maydell  * @limit: initial countdown value
206a7a305aeSPeter Maydell  * @reload: if nonzero, then reset the counter to the new limit
207a7a305aeSPeter Maydell  *
208a7a305aeSPeter Maydell  * Set the limit value of the down-counter. The @reload flag can
209a7a305aeSPeter Maydell  * be used to emulate the behaviour of timers which immediately
210a7a305aeSPeter Maydell  * reload the counter when their reload register is written to.
211*78b6eaa6SPeter Maydell  *
212*78b6eaa6SPeter Maydell  * This function will assert if it is called outside a
213*78b6eaa6SPeter Maydell  * ptimer_transaction_begin/commit block, unless this is a bottom-half ptimer.
214a7a305aeSPeter Maydell  */
21549d4d9b6SPaolo Bonzini void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
216a7a305aeSPeter Maydell 
217a7a305aeSPeter Maydell /**
218a7a305aeSPeter Maydell  * ptimer_get_count - Get the current value of the ptimer
219a7a305aeSPeter Maydell  * @s: ptimer
220a7a305aeSPeter Maydell  *
221a7a305aeSPeter Maydell  * Return the current value of the down-counter. This will
222a7a305aeSPeter Maydell  * return the correct value whether the counter is enabled or
223a7a305aeSPeter Maydell  * disabled.
224a7a305aeSPeter Maydell  */
22549d4d9b6SPaolo Bonzini uint64_t ptimer_get_count(ptimer_state *s);
226a7a305aeSPeter Maydell 
227a7a305aeSPeter Maydell /**
228a7a305aeSPeter Maydell  * ptimer_set_count - Set the current value of the ptimer
229a7a305aeSPeter Maydell  * @s: ptimer
230a7a305aeSPeter Maydell  * @count: count value to set
231a7a305aeSPeter Maydell  *
232a7a305aeSPeter Maydell  * Set the value of the down-counter. If the counter is currently
233a7a305aeSPeter Maydell  * enabled this will arrange for a timer callback at the appropriate
234a7a305aeSPeter Maydell  * point in the future.
235*78b6eaa6SPeter Maydell  *
236*78b6eaa6SPeter Maydell  * This function will assert if it is called outside a
237*78b6eaa6SPeter Maydell  * ptimer_transaction_begin/commit block, unless this is a bottom-half ptimer.
238a7a305aeSPeter Maydell  */
23949d4d9b6SPaolo Bonzini void ptimer_set_count(ptimer_state *s, uint64_t count);
240a7a305aeSPeter Maydell 
241a7a305aeSPeter Maydell /**
242a7a305aeSPeter Maydell  * ptimer_run - Start a ptimer counting
243a7a305aeSPeter Maydell  * @s: ptimer
244a7a305aeSPeter Maydell  * @oneshot: non-zero if this timer should only count down once
245a7a305aeSPeter Maydell  *
246a7a305aeSPeter Maydell  * Start a ptimer counting down; when it reaches zero the bottom half
247b0142262SPeter Maydell  * passed to ptimer_init_with_bh() will be invoked.
248b0142262SPeter Maydell  * If the @oneshot argument is zero,
249a7a305aeSPeter Maydell  * the counter value will then be reloaded from the limit and it will
250a7a305aeSPeter Maydell  * start counting down again. If @oneshot is non-zero, then the counter
251a7a305aeSPeter Maydell  * will disable itself when it reaches zero.
252*78b6eaa6SPeter Maydell  *
253*78b6eaa6SPeter Maydell  * This function will assert if it is called outside a
254*78b6eaa6SPeter Maydell  * ptimer_transaction_begin/commit block, unless this is a bottom-half ptimer.
255a7a305aeSPeter Maydell  */
25649d4d9b6SPaolo Bonzini void ptimer_run(ptimer_state *s, int oneshot);
257a7a305aeSPeter Maydell 
258a7a305aeSPeter Maydell /**
259a7a305aeSPeter Maydell  * ptimer_stop - Stop a ptimer counting
260a7a305aeSPeter Maydell  * @s: ptimer
261a7a305aeSPeter Maydell  *
262a7a305aeSPeter Maydell  * Pause a timer (the count stays at its current value until ptimer_run()
263a7a305aeSPeter Maydell  * is called to start it counting again).
264a7a305aeSPeter Maydell  *
265a7a305aeSPeter Maydell  * Note that this can cause it to "lose" time, even if it is immediately
266a7a305aeSPeter Maydell  * restarted.
267*78b6eaa6SPeter Maydell  *
268*78b6eaa6SPeter Maydell  * This function will assert if it is called outside a
269*78b6eaa6SPeter Maydell  * ptimer_transaction_begin/commit block, unless this is a bottom-half ptimer.
270a7a305aeSPeter Maydell  */
27149d4d9b6SPaolo Bonzini void ptimer_stop(ptimer_state *s);
27249d4d9b6SPaolo Bonzini 
273701a8f76SPaolo Bonzini extern const VMStateDescription vmstate_ptimer;
274701a8f76SPaolo Bonzini 
27520bcf73fSPeter Maydell #define VMSTATE_PTIMER(_field, _state) \
27620bcf73fSPeter Maydell     VMSTATE_STRUCT_POINTER_V(_field, _state, 1, vmstate_ptimer, ptimer_state)
277701a8f76SPaolo Bonzini 
278a1f05e79SPeter Maydell #define VMSTATE_PTIMER_ARRAY(_f, _s, _n)                                \
279a1f05e79SPeter Maydell     VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, 0,                   \
280a1f05e79SPeter Maydell                                        vmstate_ptimer, ptimer_state)
281a1f05e79SPeter Maydell 
28249d4d9b6SPaolo Bonzini #endif
283