xref: /openbmc/qemu/include/hw/ptimer.h (revision b91a0fa7)
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 #ifndef PTIMER_H
9 #define PTIMER_H
10 
11 #include "qemu/timer.h"
12 
13 /*
14  * The ptimer API implements a simple periodic countdown timer.
15  * The countdown timer has a value (which can be read and written via
16  * ptimer_get_count() and ptimer_set_count()). When it is enabled
17  * using ptimer_run(), the value will count downwards at the frequency
18  * which has been configured using ptimer_set_period() or ptimer_set_freq().
19  * When it reaches zero it will trigger a callback function, and
20  * can be set to either reload itself from a specified limit value
21  * and keep counting down, or to stop (as a one-shot timer).
22  *
23  * A transaction-based API is used for modifying ptimer state: all calls
24  * to functions which modify ptimer state must be between matched calls to
25  * ptimer_transaction_begin() and ptimer_transaction_commit().
26  * When ptimer_transaction_commit() is called it will evaluate the state
27  * of the timer after all the changes in the transaction, and call the
28  * callback if necessary. (See the ptimer_init() documentation for the full
29  * list of state-modifying functions and detailed semantics of the callback.)
30  *
31  * Forgetting to set the period/frequency (or setting it to zero) is a
32  * bug in the QEMU device and will cause warning messages to be printed
33  * to stderr when the guest attempts to enable the timer.
34  */
35 
36 /* The default ptimer policy retains backward compatibility with the legacy
37  * timers. Custom policies are adjusting the default one. Consider providing
38  * a correct policy for your timer.
39  *
40  * The rough edges of the default policy:
41  *  - Starting to run with a period = 0 emits error message and stops the
42  *    timer without a trigger.
43  *
44  *  - Setting period to 0 of the running timer emits error message and
45  *    stops the timer without a trigger.
46  *
47  *  - Starting to run with counter = 0 or setting it to "0" while timer
48  *    is running causes a trigger and reloads counter with a limit value.
49  *    If limit = 0, ptimer emits error message and stops the timer.
50  *
51  *  - Counter value of the running timer is one less than the actual value.
52  *
53  *  - Changing period/frequency of the running timer loses time elapsed
54  *    since the last period, effectively restarting the timer with a
55  *    counter = counter value at the moment of change (.i.e. one less).
56  */
57 #define PTIMER_POLICY_DEFAULT               0
58 
59 /* Periodic timer counter stays with "0" for a one period before wrapping
60  * around.  */
61 #define PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD (1 << 0)
62 
63 /* Running periodic timer that has counter = limit = 0 would continuously
64  * re-trigger every period.  */
65 #define PTIMER_POLICY_CONTINUOUS_TRIGGER    (1 << 1)
66 
67 /* Starting to run with/setting counter to "0" won't trigger immediately,
68  * but after a one period for both oneshot and periodic modes.  */
69 #define PTIMER_POLICY_NO_IMMEDIATE_TRIGGER  (1 << 2)
70 
71 /* Starting to run with/setting counter to "0" won't re-load counter
72  * immediately, but after a one period.  */
73 #define PTIMER_POLICY_NO_IMMEDIATE_RELOAD   (1 << 3)
74 
75 /* Make counter value of the running timer represent the actual value and
76  * not the one less.  */
77 #define PTIMER_POLICY_NO_COUNTER_ROUND_DOWN (1 << 4)
78 
79 /*
80  * Starting to run with a zero counter, or setting the counter to "0" via
81  * ptimer_set_count() or ptimer_set_limit() will not trigger the timer
82  * (though it will cause a reload). Only a counter decrement to "0"
83  * will cause a trigger. Not compatible with NO_IMMEDIATE_TRIGGER;
84  * ptimer_init() will assert() that you don't set both.
85  */
86 #define PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT (1 << 5)
87 
88 /* ptimer.c */
89 typedef struct ptimer_state ptimer_state;
90 typedef void (*ptimer_cb)(void *opaque);
91 
92 /**
93  * ptimer_init - Allocate and return a new ptimer
94  * @callback: function to call on ptimer expiry
95  * @callback_opaque: opaque pointer passed to @callback
96  * @policy: PTIMER_POLICY_* bits specifying behaviour
97  *
98  * The ptimer returned must be freed using ptimer_free().
99  *
100  * If a ptimer is created using this API then will use the
101  * transaction-based API for modifying ptimer state: all calls
102  * to functions which modify ptimer state:
103  *  - ptimer_set_period()
104  *  - ptimer_set_freq()
105  *  - ptimer_set_limit()
106  *  - ptimer_set_count()
107  *  - ptimer_run()
108  *  - ptimer_stop()
109  * must be between matched calls to ptimer_transaction_begin()
110  * and ptimer_transaction_commit(). When ptimer_transaction_commit()
111  * is called it will evaluate the state of the timer after all the
112  * changes in the transaction, and call the callback if necessary.
113  *
114  * The callback function is always called from within a transaction
115  * begin/commit block, so the callback should not call the
116  * ptimer_transaction_begin() function itself. If the callback changes
117  * the ptimer state such that another ptimer expiry is triggered, then
118  * the callback will be called a second time after the first call returns.
119  */
120 ptimer_state *ptimer_init(ptimer_cb callback,
121                           void *callback_opaque,
122                           uint8_t policy_mask);
123 
124 /**
125  * ptimer_free - Free a ptimer
126  * @s: timer to free
127  *
128  * Free a ptimer created using ptimer_init().
129  */
130 void ptimer_free(ptimer_state *s);
131 
132 /**
133  * ptimer_transaction_begin() - Start a ptimer modification transaction
134  *
135  * This function must be called before making any calls to functions
136  * which modify the ptimer's state (see the ptimer_init() documentation
137  * for a list of these), and must always have a matched call to
138  * ptimer_transaction_commit().
139  * It is an error to call this function for a BH-based ptimer;
140  * attempting to do this will trigger an assert.
141  */
142 void ptimer_transaction_begin(ptimer_state *s);
143 
144 /**
145  * ptimer_transaction_commit() - Commit a ptimer modification transaction
146  *
147  * This function must be called after calls to functions which modify
148  * the ptimer's state, and completes the update of the ptimer. If the
149  * ptimer state now means that we should trigger the timer expiry
150  * callback, it will be called directly.
151  */
152 void ptimer_transaction_commit(ptimer_state *s);
153 
154 /**
155  * ptimer_set_period - Set counter increment interval in nanoseconds
156  * @s: ptimer to configure
157  * @period: period of the counter in nanoseconds
158  *
159  * Note that if your counter behaviour is specified as having a
160  * particular frequency rather than a period then ptimer_set_freq()
161  * may be more appropriate.
162  *
163  * This function will assert if it is called outside a
164  * ptimer_transaction_begin/commit block.
165  */
166 void ptimer_set_period(ptimer_state *s, int64_t period);
167 
168 /**
169  * ptimer_set_period_from_clock - Set counter increment from a Clock
170  * @s: ptimer to configure
171  * @clk: pointer to Clock object to take period from
172  * @divisor: value to scale the clock frequency down by
173  *
174  * If the ptimer is being driven from a Clock, this is the preferred
175  * way to tell the ptimer about the period, because it avoids any
176  * possible rounding errors that might happen if the internal
177  * representation of the Clock period was converted to either a period
178  * in ns or a frequency in Hz.
179  *
180  * If the ptimer should run at the same frequency as the clock,
181  * pass 1 as the @divisor; if the ptimer should run at half the
182  * frequency, pass 2, and so on.
183  *
184  * This function will assert if it is called outside a
185  * ptimer_transaction_begin/commit block.
186  */
187 void ptimer_set_period_from_clock(ptimer_state *s, const Clock *clock,
188                                   unsigned int divisor);
189 
190 /**
191  * ptimer_set_freq - Set counter frequency in Hz
192  * @s: ptimer to configure
193  * @freq: counter frequency in Hz
194  *
195  * This does the same thing as ptimer_set_period(), so you only
196  * need to call one of them. If the counter behaviour is specified
197  * as setting the frequency then this function is more appropriate,
198  * because it allows specifying an effective period which is
199  * precise to fractions of a nanosecond, avoiding rounding errors.
200  *
201  * This function will assert if it is called outside a
202  * ptimer_transaction_begin/commit block.
203  */
204 void ptimer_set_freq(ptimer_state *s, uint32_t freq);
205 
206 /**
207  * ptimer_get_limit - Get the configured limit of the ptimer
208  * @s: ptimer to query
209  *
210  * This function returns the current limit (reload) value
211  * of the down-counter; that is, the value which it will be
212  * reset to when it hits zero.
213  *
214  * Generally timer devices using ptimers should be able to keep
215  * their reload register state inside the ptimer using the get
216  * and set limit functions rather than needing to also track it
217  * in their own state structure.
218  */
219 uint64_t ptimer_get_limit(ptimer_state *s);
220 
221 /**
222  * ptimer_set_limit - Set the limit of the ptimer
223  * @s: ptimer
224  * @limit: initial countdown value
225  * @reload: if nonzero, then reset the counter to the new limit
226  *
227  * Set the limit value of the down-counter. The @reload flag can
228  * be used to emulate the behaviour of timers which immediately
229  * reload the counter when their reload register is written to.
230  *
231  * This function will assert if it is called outside a
232  * ptimer_transaction_begin/commit block.
233  */
234 void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
235 
236 /**
237  * ptimer_get_count - Get the current value of the ptimer
238  * @s: ptimer
239  *
240  * Return the current value of the down-counter. This will
241  * return the correct value whether the counter is enabled or
242  * disabled.
243  */
244 uint64_t ptimer_get_count(ptimer_state *s);
245 
246 /**
247  * ptimer_set_count - Set the current value of the ptimer
248  * @s: ptimer
249  * @count: count value to set
250  *
251  * Set the value of the down-counter. If the counter is currently
252  * enabled this will arrange for a timer callback at the appropriate
253  * point in the future.
254  *
255  * This function will assert if it is called outside a
256  * ptimer_transaction_begin/commit block.
257  */
258 void ptimer_set_count(ptimer_state *s, uint64_t count);
259 
260 /**
261  * ptimer_run - Start a ptimer counting
262  * @s: ptimer
263  * @oneshot: non-zero if this timer should only count down once
264  *
265  * Start a ptimer counting down; when it reaches zero the callback function
266  * passed to ptimer_init() will be invoked.
267  * If the @oneshot argument is zero,
268  * the counter value will then be reloaded from the limit and it will
269  * start counting down again. If @oneshot is non-zero, then the counter
270  * will disable itself when it reaches zero.
271  *
272  * This function will assert if it is called outside a
273  * ptimer_transaction_begin/commit block.
274  */
275 void ptimer_run(ptimer_state *s, int oneshot);
276 
277 /**
278  * ptimer_stop - Stop a ptimer counting
279  * @s: ptimer
280  *
281  * Pause a timer (the count stays at its current value until ptimer_run()
282  * is called to start it counting again).
283  *
284  * Note that this can cause it to "lose" time, even if it is immediately
285  * restarted.
286  *
287  * This function will assert if it is called outside a
288  * ptimer_transaction_begin/commit block.
289  */
290 void ptimer_stop(ptimer_state *s);
291 
292 extern const VMStateDescription vmstate_ptimer;
293 
294 #define VMSTATE_PTIMER(_field, _state) \
295     VMSTATE_STRUCT_POINTER_V(_field, _state, 1, vmstate_ptimer, ptimer_state)
296 
297 #define VMSTATE_PTIMER_ARRAY(_f, _s, _n)                                \
298     VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, 0,                   \
299                                        vmstate_ptimer, ptimer_state)
300 
301 #endif
302