]> git.proxmox.com Git - mirror_qemu.git/blame - include/hw/ptimer.h
Include qemu-common.h exactly where needed
[mirror_qemu.git] / include / hw / ptimer.h
CommitLineData
49d4d9b6
PB
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
1de7afc9 11#include "qemu/timer.h"
caf71f86 12#include "migration/vmstate.h"
49d4d9b6 13
a7a305ae
PM
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 QEMU bottom half handler, 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 * Forgetting to set the period/frequency (or setting it to zero) is a
24 * bug in the QEMU device and will cause warning messages to be printed
25 * to stderr when the guest attempts to enable the timer.
26 */
27
e7ea81c3
DO
28/* The default ptimer policy retains backward compatibility with the legacy
29 * timers. Custom policies are adjusting the default one. Consider providing
30 * a correct policy for your timer.
31 *
32 * The rough edges of the default policy:
33 * - Starting to run with a period = 0 emits error message and stops the
34 * timer without a trigger.
35 *
36 * - Setting period to 0 of the running timer emits error message and
37 * stops the timer without a trigger.
38 *
39 * - Starting to run with counter = 0 or setting it to "0" while timer
40 * is running causes a trigger and reloads counter with a limit value.
41 * If limit = 0, ptimer emits error message and stops the timer.
42 *
43 * - Counter value of the running timer is one less than the actual value.
44 *
45 * - Changing period/frequency of the running timer loses time elapsed
46 * since the last period, effectively restarting the timer with a
47 * counter = counter value at the moment of change (.i.e. one less).
48 */
49#define PTIMER_POLICY_DEFAULT 0
50
2b5c0322
DO
51/* Periodic timer counter stays with "0" for a one period before wrapping
52 * around. */
53#define PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD (1 << 0)
54
ef0a9984
DO
55/* Running periodic timer that has counter = limit = 0 would continuously
56 * re-trigger every period. */
57#define PTIMER_POLICY_CONTINUOUS_TRIGGER (1 << 1)
58
22471b8a
DO
59/* Starting to run with/setting counter to "0" won't trigger immediately,
60 * but after a one period for both oneshot and periodic modes. */
61#define PTIMER_POLICY_NO_IMMEDIATE_TRIGGER (1 << 2)
62
3f6e6a13
DO
63/* Starting to run with/setting counter to "0" won't re-load counter
64 * immediately, but after a one period. */
65#define PTIMER_POLICY_NO_IMMEDIATE_RELOAD (1 << 3)
66
5580ea45
DO
67/* Make counter value of the running timer represent the actual value and
68 * not the one less. */
69#define PTIMER_POLICY_NO_COUNTER_ROUND_DOWN (1 << 4)
70
086ede32
PM
71/*
72 * Starting to run with a zero counter, or setting the counter to "0" via
73 * ptimer_set_count() or ptimer_set_limit() will not trigger the timer
74 * (though it will cause a reload). Only a counter decrement to "0"
75 * will cause a trigger. Not compatible with NO_IMMEDIATE_TRIGGER;
76 * ptimer_init() will assert() that you don't set both.
77 */
78#define PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT (1 << 5)
79
49d4d9b6
PB
80/* ptimer.c */
81typedef struct ptimer_state ptimer_state;
82typedef void (*ptimer_cb)(void *opaque);
83
a7a305ae
PM
84/**
85 * ptimer_init - Allocate and return a new ptimer
86 * @bh: QEMU bottom half which is run on timer expiry
87 * @policy: PTIMER_POLICY_* bits specifying behaviour
88 *
89 * The ptimer returned must be freed using ptimer_free().
90 * The ptimer takes ownership of @bh and will delete it
91 * when the ptimer is eventually freed.
92 */
e7ea81c3 93ptimer_state *ptimer_init(QEMUBH *bh, uint8_t policy_mask);
a7a305ae
PM
94
95/**
96 * ptimer_free - Free a ptimer
97 * @s: timer to free
98 *
99 * Free a ptimer created using ptimer_init() (including
100 * deleting the bottom half which it is using).
101 */
072bdb07 102void ptimer_free(ptimer_state *s);
a7a305ae
PM
103
104/**
105 * ptimer_set_period - Set counter increment interval in nanoseconds
106 * @s: ptimer to configure
107 * @period: period of the counter in nanoseconds
108 *
109 * Note that if your counter behaviour is specified as having a
110 * particular frequency rather than a period then ptimer_set_freq()
111 * may be more appropriate.
112 */
49d4d9b6 113void ptimer_set_period(ptimer_state *s, int64_t period);
a7a305ae
PM
114
115/**
116 * ptimer_set_freq - Set counter frequency in Hz
117 * @s: ptimer to configure
118 * @freq: counter frequency in Hz
119 *
120 * This does the same thing as ptimer_set_period(), so you only
121 * need to call one of them. If the counter behaviour is specified
122 * as setting the frequency then this function is more appropriate,
123 * because it allows specifying an effective period which is
124 * precise to fractions of a nanosecond, avoiding rounding errors.
125 */
49d4d9b6 126void ptimer_set_freq(ptimer_state *s, uint32_t freq);
a7a305ae
PM
127
128/**
129 * ptimer_get_limit - Get the configured limit of the ptimer
130 * @s: ptimer to query
131 *
132 * This function returns the current limit (reload) value
133 * of the down-counter; that is, the value which it will be
134 * reset to when it hits zero.
135 *
136 * Generally timer devices using ptimers should be able to keep
137 * their reload register state inside the ptimer using the get
138 * and set limit functions rather than needing to also track it
139 * in their own state structure.
140 */
578c4b2f 141uint64_t ptimer_get_limit(ptimer_state *s);
a7a305ae
PM
142
143/**
144 * ptimer_set_limit - Set the limit of the ptimer
145 * @s: ptimer
146 * @limit: initial countdown value
147 * @reload: if nonzero, then reset the counter to the new limit
148 *
149 * Set the limit value of the down-counter. The @reload flag can
150 * be used to emulate the behaviour of timers which immediately
151 * reload the counter when their reload register is written to.
152 */
49d4d9b6 153void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
a7a305ae
PM
154
155/**
156 * ptimer_get_count - Get the current value of the ptimer
157 * @s: ptimer
158 *
159 * Return the current value of the down-counter. This will
160 * return the correct value whether the counter is enabled or
161 * disabled.
162 */
49d4d9b6 163uint64_t ptimer_get_count(ptimer_state *s);
a7a305ae
PM
164
165/**
166 * ptimer_set_count - Set the current value of the ptimer
167 * @s: ptimer
168 * @count: count value to set
169 *
170 * Set the value of the down-counter. If the counter is currently
171 * enabled this will arrange for a timer callback at the appropriate
172 * point in the future.
173 */
49d4d9b6 174void ptimer_set_count(ptimer_state *s, uint64_t count);
a7a305ae
PM
175
176/**
177 * ptimer_run - Start a ptimer counting
178 * @s: ptimer
179 * @oneshot: non-zero if this timer should only count down once
180 *
181 * Start a ptimer counting down; when it reaches zero the bottom half
182 * passed to ptimer_init() will be invoked. If the @oneshot argument is zero,
183 * the counter value will then be reloaded from the limit and it will
184 * start counting down again. If @oneshot is non-zero, then the counter
185 * will disable itself when it reaches zero.
186 */
49d4d9b6 187void ptimer_run(ptimer_state *s, int oneshot);
a7a305ae
PM
188
189/**
190 * ptimer_stop - Stop a ptimer counting
191 * @s: ptimer
192 *
193 * Pause a timer (the count stays at its current value until ptimer_run()
194 * is called to start it counting again).
195 *
196 * Note that this can cause it to "lose" time, even if it is immediately
197 * restarted.
198 */
49d4d9b6
PB
199void ptimer_stop(ptimer_state *s);
200
701a8f76
PB
201extern const VMStateDescription vmstate_ptimer;
202
20bcf73f
PM
203#define VMSTATE_PTIMER(_field, _state) \
204 VMSTATE_STRUCT_POINTER_V(_field, _state, 1, vmstate_ptimer, ptimer_state)
701a8f76 205
a1f05e79
PM
206#define VMSTATE_PTIMER_ARRAY(_f, _s, _n) \
207 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, 0, \
208 vmstate_ptimer, ptimer_state)
209
49d4d9b6 210#endif