xref: /qemu/include/hw/ptimer.h (revision a7a305aee1e9a74a3f4b263aa98babed795b1f0a)
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 
1149d4d9b6SPaolo Bonzini #include "qemu-common.h"
121de7afc9SPaolo Bonzini #include "qemu/timer.h"
13caf71f86SPaolo Bonzini #include "migration/vmstate.h"
1449d4d9b6SPaolo Bonzini 
15*a7a305aeSPeter Maydell /* The ptimer API implements a simple periodic countdown timer.
16*a7a305aeSPeter Maydell  * The countdown timer has a value (which can be read and written via
17*a7a305aeSPeter Maydell  * ptimer_get_count() and ptimer_set_count()). When it is enabled
18*a7a305aeSPeter Maydell  * using ptimer_run(), the value will count downwards at the frequency
19*a7a305aeSPeter Maydell  * which has been configured using ptimer_set_period() or ptimer_set_freq().
20*a7a305aeSPeter Maydell  * When it reaches zero it will trigger a QEMU bottom half handler, and
21*a7a305aeSPeter Maydell  * can be set to either reload itself from a specified limit value
22*a7a305aeSPeter Maydell  * and keep counting down, or to stop (as a one-shot timer).
23*a7a305aeSPeter Maydell  *
24*a7a305aeSPeter Maydell  * Forgetting to set the period/frequency (or setting it to zero) is a
25*a7a305aeSPeter Maydell  * bug in the QEMU device and will cause warning messages to be printed
26*a7a305aeSPeter Maydell  * to stderr when the guest attempts to enable the timer.
27*a7a305aeSPeter Maydell  */
28*a7a305aeSPeter Maydell 
29e7ea81c3SDmitry Osipenko /* The default ptimer policy retains backward compatibility with the legacy
30e7ea81c3SDmitry Osipenko  * timers. Custom policies are adjusting the default one. Consider providing
31e7ea81c3SDmitry Osipenko  * a correct policy for your timer.
32e7ea81c3SDmitry Osipenko  *
33e7ea81c3SDmitry Osipenko  * The rough edges of the default policy:
34e7ea81c3SDmitry Osipenko  *  - Starting to run with a period = 0 emits error message and stops the
35e7ea81c3SDmitry Osipenko  *    timer without a trigger.
36e7ea81c3SDmitry Osipenko  *
37e7ea81c3SDmitry Osipenko  *  - Setting period to 0 of the running timer emits error message and
38e7ea81c3SDmitry Osipenko  *    stops the timer without a trigger.
39e7ea81c3SDmitry Osipenko  *
40e7ea81c3SDmitry Osipenko  *  - Starting to run with counter = 0 or setting it to "0" while timer
41e7ea81c3SDmitry Osipenko  *    is running causes a trigger and reloads counter with a limit value.
42e7ea81c3SDmitry Osipenko  *    If limit = 0, ptimer emits error message and stops the timer.
43e7ea81c3SDmitry Osipenko  *
44e7ea81c3SDmitry Osipenko  *  - Counter value of the running timer is one less than the actual value.
45e7ea81c3SDmitry Osipenko  *
46e7ea81c3SDmitry Osipenko  *  - Changing period/frequency of the running timer loses time elapsed
47e7ea81c3SDmitry Osipenko  *    since the last period, effectively restarting the timer with a
48e7ea81c3SDmitry Osipenko  *    counter = counter value at the moment of change (.i.e. one less).
49e7ea81c3SDmitry Osipenko  */
50e7ea81c3SDmitry Osipenko #define PTIMER_POLICY_DEFAULT               0
51e7ea81c3SDmitry Osipenko 
522b5c0322SDmitry Osipenko /* Periodic timer counter stays with "0" for a one period before wrapping
532b5c0322SDmitry Osipenko  * around.  */
542b5c0322SDmitry Osipenko #define PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD (1 << 0)
552b5c0322SDmitry Osipenko 
56ef0a9984SDmitry Osipenko /* Running periodic timer that has counter = limit = 0 would continuously
57ef0a9984SDmitry Osipenko  * re-trigger every period.  */
58ef0a9984SDmitry Osipenko #define PTIMER_POLICY_CONTINUOUS_TRIGGER    (1 << 1)
59ef0a9984SDmitry Osipenko 
6022471b8aSDmitry Osipenko /* Starting to run with/setting counter to "0" won't trigger immediately,
6122471b8aSDmitry Osipenko  * but after a one period for both oneshot and periodic modes.  */
6222471b8aSDmitry Osipenko #define PTIMER_POLICY_NO_IMMEDIATE_TRIGGER  (1 << 2)
6322471b8aSDmitry Osipenko 
643f6e6a13SDmitry Osipenko /* Starting to run with/setting counter to "0" won't re-load counter
653f6e6a13SDmitry Osipenko  * immediately, but after a one period.  */
663f6e6a13SDmitry Osipenko #define PTIMER_POLICY_NO_IMMEDIATE_RELOAD   (1 << 3)
673f6e6a13SDmitry Osipenko 
685580ea45SDmitry Osipenko /* Make counter value of the running timer represent the actual value and
695580ea45SDmitry Osipenko  * not the one less.  */
705580ea45SDmitry Osipenko #define PTIMER_POLICY_NO_COUNTER_ROUND_DOWN (1 << 4)
715580ea45SDmitry Osipenko 
7249d4d9b6SPaolo Bonzini /* ptimer.c */
7349d4d9b6SPaolo Bonzini typedef struct ptimer_state ptimer_state;
7449d4d9b6SPaolo Bonzini typedef void (*ptimer_cb)(void *opaque);
7549d4d9b6SPaolo Bonzini 
76*a7a305aeSPeter Maydell /**
77*a7a305aeSPeter Maydell  * ptimer_init - Allocate and return a new ptimer
78*a7a305aeSPeter Maydell  * @bh: QEMU bottom half which is run on timer expiry
79*a7a305aeSPeter Maydell  * @policy: PTIMER_POLICY_* bits specifying behaviour
80*a7a305aeSPeter Maydell  *
81*a7a305aeSPeter Maydell  * The ptimer returned must be freed using ptimer_free().
82*a7a305aeSPeter Maydell  * The ptimer takes ownership of @bh and will delete it
83*a7a305aeSPeter Maydell  * when the ptimer is eventually freed.
84*a7a305aeSPeter Maydell  */
85e7ea81c3SDmitry Osipenko ptimer_state *ptimer_init(QEMUBH *bh, uint8_t policy_mask);
86*a7a305aeSPeter Maydell 
87*a7a305aeSPeter Maydell /**
88*a7a305aeSPeter Maydell  * ptimer_free - Free a ptimer
89*a7a305aeSPeter Maydell  * @s: timer to free
90*a7a305aeSPeter Maydell  *
91*a7a305aeSPeter Maydell  * Free a ptimer created using ptimer_init() (including
92*a7a305aeSPeter Maydell  * deleting the bottom half which it is using).
93*a7a305aeSPeter Maydell  */
94072bdb07SMarc-André Lureau void ptimer_free(ptimer_state *s);
95*a7a305aeSPeter Maydell 
96*a7a305aeSPeter Maydell /**
97*a7a305aeSPeter Maydell  * ptimer_set_period - Set counter increment interval in nanoseconds
98*a7a305aeSPeter Maydell  * @s: ptimer to configure
99*a7a305aeSPeter Maydell  * @period: period of the counter in nanoseconds
100*a7a305aeSPeter Maydell  *
101*a7a305aeSPeter Maydell  * Note that if your counter behaviour is specified as having a
102*a7a305aeSPeter Maydell  * particular frequency rather than a period then ptimer_set_freq()
103*a7a305aeSPeter Maydell  * may be more appropriate.
104*a7a305aeSPeter Maydell  */
10549d4d9b6SPaolo Bonzini void ptimer_set_period(ptimer_state *s, int64_t period);
106*a7a305aeSPeter Maydell 
107*a7a305aeSPeter Maydell /**
108*a7a305aeSPeter Maydell  * ptimer_set_freq - Set counter frequency in Hz
109*a7a305aeSPeter Maydell  * @s: ptimer to configure
110*a7a305aeSPeter Maydell  * @freq: counter frequency in Hz
111*a7a305aeSPeter Maydell  *
112*a7a305aeSPeter Maydell  * This does the same thing as ptimer_set_period(), so you only
113*a7a305aeSPeter Maydell  * need to call one of them. If the counter behaviour is specified
114*a7a305aeSPeter Maydell  * as setting the frequency then this function is more appropriate,
115*a7a305aeSPeter Maydell  * because it allows specifying an effective period which is
116*a7a305aeSPeter Maydell  * precise to fractions of a nanosecond, avoiding rounding errors.
117*a7a305aeSPeter Maydell  */
11849d4d9b6SPaolo Bonzini void ptimer_set_freq(ptimer_state *s, uint32_t freq);
119*a7a305aeSPeter Maydell 
120*a7a305aeSPeter Maydell /**
121*a7a305aeSPeter Maydell  * ptimer_get_limit - Get the configured limit of the ptimer
122*a7a305aeSPeter Maydell  * @s: ptimer to query
123*a7a305aeSPeter Maydell  *
124*a7a305aeSPeter Maydell  * This function returns the current limit (reload) value
125*a7a305aeSPeter Maydell  * of the down-counter; that is, the value which it will be
126*a7a305aeSPeter Maydell  * reset to when it hits zero.
127*a7a305aeSPeter Maydell  *
128*a7a305aeSPeter Maydell  * Generally timer devices using ptimers should be able to keep
129*a7a305aeSPeter Maydell  * their reload register state inside the ptimer using the get
130*a7a305aeSPeter Maydell  * and set limit functions rather than needing to also track it
131*a7a305aeSPeter Maydell  * in their own state structure.
132*a7a305aeSPeter Maydell  */
133578c4b2fSDmitry Osipenko uint64_t ptimer_get_limit(ptimer_state *s);
134*a7a305aeSPeter Maydell 
135*a7a305aeSPeter Maydell /**
136*a7a305aeSPeter Maydell  * ptimer_set_limit - Set the limit of the ptimer
137*a7a305aeSPeter Maydell  * @s: ptimer
138*a7a305aeSPeter Maydell  * @limit: initial countdown value
139*a7a305aeSPeter Maydell  * @reload: if nonzero, then reset the counter to the new limit
140*a7a305aeSPeter Maydell  *
141*a7a305aeSPeter Maydell  * Set the limit value of the down-counter. The @reload flag can
142*a7a305aeSPeter Maydell  * be used to emulate the behaviour of timers which immediately
143*a7a305aeSPeter Maydell  * reload the counter when their reload register is written to.
144*a7a305aeSPeter Maydell  */
14549d4d9b6SPaolo Bonzini void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
146*a7a305aeSPeter Maydell 
147*a7a305aeSPeter Maydell /**
148*a7a305aeSPeter Maydell  * ptimer_get_count - Get the current value of the ptimer
149*a7a305aeSPeter Maydell  * @s: ptimer
150*a7a305aeSPeter Maydell  *
151*a7a305aeSPeter Maydell  * Return the current value of the down-counter. This will
152*a7a305aeSPeter Maydell  * return the correct value whether the counter is enabled or
153*a7a305aeSPeter Maydell  * disabled.
154*a7a305aeSPeter Maydell  */
15549d4d9b6SPaolo Bonzini uint64_t ptimer_get_count(ptimer_state *s);
156*a7a305aeSPeter Maydell 
157*a7a305aeSPeter Maydell /**
158*a7a305aeSPeter Maydell  * ptimer_set_count - Set the current value of the ptimer
159*a7a305aeSPeter Maydell  * @s: ptimer
160*a7a305aeSPeter Maydell  * @count: count value to set
161*a7a305aeSPeter Maydell  *
162*a7a305aeSPeter Maydell  * Set the value of the down-counter. If the counter is currently
163*a7a305aeSPeter Maydell  * enabled this will arrange for a timer callback at the appropriate
164*a7a305aeSPeter Maydell  * point in the future.
165*a7a305aeSPeter Maydell  */
16649d4d9b6SPaolo Bonzini void ptimer_set_count(ptimer_state *s, uint64_t count);
167*a7a305aeSPeter Maydell 
168*a7a305aeSPeter Maydell /**
169*a7a305aeSPeter Maydell  * ptimer_run - Start a ptimer counting
170*a7a305aeSPeter Maydell  * @s: ptimer
171*a7a305aeSPeter Maydell  * @oneshot: non-zero if this timer should only count down once
172*a7a305aeSPeter Maydell  *
173*a7a305aeSPeter Maydell  * Start a ptimer counting down; when it reaches zero the bottom half
174*a7a305aeSPeter Maydell  * passed to ptimer_init() will be invoked. If the @oneshot argument is zero,
175*a7a305aeSPeter Maydell  * the counter value will then be reloaded from the limit and it will
176*a7a305aeSPeter Maydell  * start counting down again. If @oneshot is non-zero, then the counter
177*a7a305aeSPeter Maydell  * will disable itself when it reaches zero.
178*a7a305aeSPeter Maydell  */
17949d4d9b6SPaolo Bonzini void ptimer_run(ptimer_state *s, int oneshot);
180*a7a305aeSPeter Maydell 
181*a7a305aeSPeter Maydell /**
182*a7a305aeSPeter Maydell  * ptimer_stop - Stop a ptimer counting
183*a7a305aeSPeter Maydell  * @s: ptimer
184*a7a305aeSPeter Maydell  *
185*a7a305aeSPeter Maydell  * Pause a timer (the count stays at its current value until ptimer_run()
186*a7a305aeSPeter Maydell  * is called to start it counting again).
187*a7a305aeSPeter Maydell  *
188*a7a305aeSPeter Maydell  * Note that this can cause it to "lose" time, even if it is immediately
189*a7a305aeSPeter Maydell  * restarted.
190*a7a305aeSPeter Maydell  */
19149d4d9b6SPaolo Bonzini void ptimer_stop(ptimer_state *s);
19249d4d9b6SPaolo Bonzini 
193701a8f76SPaolo Bonzini extern const VMStateDescription vmstate_ptimer;
194701a8f76SPaolo Bonzini 
19520bcf73fSPeter Maydell #define VMSTATE_PTIMER(_field, _state) \
19620bcf73fSPeter Maydell     VMSTATE_STRUCT_POINTER_V(_field, _state, 1, vmstate_ptimer, ptimer_state)
197701a8f76SPaolo Bonzini 
198a1f05e79SPeter Maydell #define VMSTATE_PTIMER_ARRAY(_f, _s, _n)                                \
199a1f05e79SPeter Maydell     VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, 0,                   \
200a1f05e79SPeter Maydell                                        vmstate_ptimer, ptimer_state)
201a1f05e79SPeter Maydell 
20249d4d9b6SPaolo Bonzini #endif
203