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