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