blob: b1517592c6bd092a3175bcfdafa866ed0e3e426b [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook423f0742007-05-23 00:06:54 +00002 * General purpose implementation of a simple periodic countdown timer.
3 *
4 * Copyright (c) 2007 CodeSourcery.
5 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10006 * This code is licensed under the GNU LGPL.
pbrook423f0742007-05-23 00:06:54 +00007 */
Markus Armbrusterd6454272019-08-12 07:23:45 +02008
Peter Maydell18c86e22016-01-26 18:17:29 +00009#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010010#include "hw/ptimer.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020011#include "migration/vmstate.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010012#include "qemu/host-utils.h"
Philippe Mathieu-Daudé5b5968c2022-12-19 18:09:43 +010013#include "exec/replay-core.h"
Claudio Fontana740b1752020-08-19 13:17:19 +020014#include "sysemu/cpu-timers.h"
Dmitry Osipenko2a8b5872016-09-22 18:13:07 +010015#include "sysemu/qtest.h"
Marc-André Lureau072bdb02017-01-27 12:55:51 +040016#include "block/aio.h"
Peter Maydellad140da2021-01-28 11:41:21 +000017#include "hw/clock.h"
pbrook423f0742007-05-23 00:06:54 +000018
Dmitry Osipenko22471b82016-10-24 16:26:51 +010019#define DELTA_ADJUST 1
20#define DELTA_NO_ADJUST -1
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +010021
pbrook423f0742007-05-23 00:06:54 +000022struct ptimer_state
23{
Juan Quintela852f7712010-12-01 23:51:14 +010024 uint8_t enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot. */
blueswir18d05ea82007-05-24 19:48:41 +000025 uint64_t limit;
26 uint64_t delta;
pbrook423f0742007-05-23 00:06:54 +000027 uint32_t period_frac;
28 int64_t period;
29 int64_t last_event;
30 int64_t next_event;
Dmitry Osipenkoe7ea81c2016-09-22 18:13:06 +010031 uint8_t policy_mask;
pbrook423f0742007-05-23 00:06:54 +000032 QEMUTimer *timer;
Peter Maydell78b6eaa2019-10-08 18:17:21 +010033 ptimer_cb callback;
34 void *callback_opaque;
35 /*
36 * These track whether we're in a transaction block, and if we
37 * need to do a timer reload when the block finishes. They don't
38 * need to be migrated because migration can never happen in the
39 * middle of a transaction block.
40 */
41 bool in_transaction;
42 bool need_reload;
pbrook423f0742007-05-23 00:06:54 +000043};
44
45/* Use a bottom-half routine to avoid reentrancy issues. */
46static void ptimer_trigger(ptimer_state *s)
47{
Peter Maydellaf2a5802019-11-11 13:44:16 +000048 s->callback(s->callback_opaque);
pbrook423f0742007-05-23 00:06:54 +000049}
50
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +010051static void ptimer_reload(ptimer_state *s, int delta_adjust)
pbrook423f0742007-05-23 00:06:54 +000052{
Peter Maydell78b6eaa2019-10-08 18:17:21 +010053 uint32_t period_frac;
54 uint64_t period;
55 uint64_t delta;
Peter Maydell086ede32018-07-09 14:51:34 +010056 bool suppress_trigger = false;
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +010057
Peter Maydell086ede32018-07-09 14:51:34 +010058 /*
59 * Note that if delta_adjust is 0 then we must be here because of
60 * a count register write or timer start, not because of timer expiry.
61 * In that case the policy might require us to suppress the timer trigger
62 * that we would otherwise generate for a zero delta.
63 */
64 if (delta_adjust == 0 &&
65 (s->policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT)) {
66 suppress_trigger = true;
67 }
Peter Maydell78b6eaa2019-10-08 18:17:21 +010068 if (s->delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)
Peter Maydell086ede32018-07-09 14:51:34 +010069 && !suppress_trigger) {
pbrook423f0742007-05-23 00:06:54 +000070 ptimer_trigger(s);
Dmitry Osipenko22471b82016-10-24 16:26:51 +010071 }
72
Peter Maydell78b6eaa2019-10-08 18:17:21 +010073 /*
74 * Note that ptimer_trigger() might call the device callback function,
75 * which can then modify timer state, so we must not cache any fields
76 * from ptimer_state until after we have called it.
77 */
78 delta = s->delta;
79 period = s->period;
80 period_frac = s->period_frac;
81
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +010082 if (delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) {
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +010083 delta = s->delta = s->limit;
pbrook423f0742007-05-23 00:06:54 +000084 }
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +010085
86 if (s->period == 0) {
Dmitry Osipenko2a8b5872016-09-22 18:13:07 +010087 if (!qtest_enabled()) {
88 fprintf(stderr, "Timer with period zero, disabling\n");
89 }
Dmitry Osipenko780d23e2016-09-22 18:13:06 +010090 timer_del(s->timer);
pbrook423f0742007-05-23 00:06:54 +000091 s->enabled = 0;
92 return;
93 }
94
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +010095 if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) {
Dmitry Osipenko22471b82016-10-24 16:26:51 +010096 if (delta_adjust != DELTA_NO_ADJUST) {
97 delta += delta_adjust;
98 }
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +010099 }
100
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100101 if (delta == 0 && (s->policy_mask & PTIMER_POLICY_CONTINUOUS_TRIGGER)) {
102 if (s->enabled == 1 && s->limit == 0) {
103 delta = 1;
104 }
105 }
106
Dmitry Osipenko22471b82016-10-24 16:26:51 +0100107 if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
108 if (delta_adjust != DELTA_NO_ADJUST) {
109 delta = 1;
110 }
111 }
112
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +0100113 if (delta == 0 && (s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_RELOAD)) {
114 if (s->enabled == 1 && s->limit != 0) {
115 delta = 1;
116 }
117 }
118
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100119 if (delta == 0) {
Peter Maydell68d59c62020-10-15 16:18:28 +0100120 if (s->enabled == 0) {
121 /* trigger callback disabled the timer already */
122 return;
123 }
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100124 if (!qtest_enabled()) {
125 fprintf(stderr, "Timer with delta zero, disabling\n");
126 }
127 timer_del(s->timer);
128 s->enabled = 0;
129 return;
130 }
131
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100132 /*
133 * Artificially limit timeout rate to something
134 * achievable under QEMU. Otherwise, QEMU spends all
135 * its time generating timer interrupts, and there
136 * is no forward progress.
137 * About ten microseconds is the fastest that really works
138 * on the current generation of host machines.
139 */
140
Claudio Fontana740b1752020-08-19 13:17:19 +0200141 if (s->enabled == 1 && (delta * period < 10000) &&
142 !icount_enabled() && !qtest_enabled()) {
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +0100143 period = 10000 / delta;
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100144 period_frac = 0;
145 }
146
pbrook423f0742007-05-23 00:06:54 +0000147 s->last_event = s->next_event;
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +0100148 s->next_event = s->last_event + delta * period;
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100149 if (period_frac) {
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +0100150 s->next_event += ((int64_t)period_frac * delta) >> 32;
pbrook423f0742007-05-23 00:06:54 +0000151 }
Alex Blighbc72ad62013-08-21 16:03:08 +0100152 timer_mod(s->timer, s->next_event);
pbrook423f0742007-05-23 00:06:54 +0000153}
154
155static void ptimer_tick(void *opaque)
156{
157 ptimer_state *s = (ptimer_state *)opaque;
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +0100158 bool trigger = true;
159
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100160 /*
161 * We perform all the tick actions within a begin/commit block
162 * because the callback function that ptimer_trigger() calls
163 * might make calls into the ptimer APIs that provoke another
164 * trigger, and we want that to cause the callback function
165 * to be called iteratively, not recursively.
166 */
167 ptimer_transaction_begin(s);
168
pbrook423f0742007-05-23 00:06:54 +0000169 if (s->enabled == 2) {
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +0100170 s->delta = 0;
pbrook423f0742007-05-23 00:06:54 +0000171 s->enabled = 0;
172 } else {
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100173 int delta_adjust = DELTA_ADJUST;
174
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +0100175 if (s->delta == 0 || s->limit == 0) {
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100176 /* If a "continuous trigger" policy is not used and limit == 0,
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +0100177 we should error out. delta == 0 means that this tick is
178 caused by a "no immediate reload" policy, so it shouldn't
179 be adjusted. */
Dmitry Osipenko22471b82016-10-24 16:26:51 +0100180 delta_adjust = DELTA_NO_ADJUST;
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100181 }
182
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +0100183 if (!(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
184 /* Avoid re-trigger on deferred reload if "no immediate trigger"
185 policy isn't used. */
186 trigger = (delta_adjust == DELTA_ADJUST);
187 }
188
189 s->delta = s->limit;
190
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100191 ptimer_reload(s, delta_adjust);
pbrook423f0742007-05-23 00:06:54 +0000192 }
Dmitry Osipenko3f6e6a12016-10-24 16:26:52 +0100193
194 if (trigger) {
195 ptimer_trigger(s);
196 }
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100197
198 ptimer_transaction_commit(s);
pbrook423f0742007-05-23 00:06:54 +0000199}
200
blueswir18d05ea82007-05-24 19:48:41 +0000201uint64_t ptimer_get_count(ptimer_state *s)
pbrook423f0742007-05-23 00:06:54 +0000202{
blueswir18d05ea82007-05-24 19:48:41 +0000203 uint64_t counter;
pbrook423f0742007-05-23 00:06:54 +0000204
Dmitry Osipenkoef0a9982016-10-24 16:26:51 +0100205 if (s->enabled && s->delta != 0) {
Dmitry Osipenko5a503072016-06-06 16:59:30 +0100206 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
207 int64_t next = s->next_event;
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +0100208 int64_t last = s->last_event;
Dmitry Osipenko5a503072016-06-06 16:59:30 +0100209 bool expired = (now - next >= 0);
210 bool oneshot = (s->enabled == 2);
211
pbrook423f0742007-05-23 00:06:54 +0000212 /* Figure out the current counter value. */
Dmitry Osipenko56215da2016-07-14 16:51:36 +0100213 if (expired) {
pbrook423f0742007-05-23 00:06:54 +0000214 /* Prevent timer underflowing if it should already have
215 triggered. */
216 counter = 0;
217 } else {
blueswir18d05ea82007-05-24 19:48:41 +0000218 uint64_t rem;
219 uint64_t div;
pbrookd0a981b2009-03-31 14:34:24 +0000220 int clz1, clz2;
221 int shift;
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100222 uint32_t period_frac = s->period_frac;
223 uint64_t period = s->period;
224
Claudio Fontana740b1752020-08-19 13:17:19 +0200225 if (!oneshot && (s->delta * period < 10000) &&
226 !icount_enabled() && !qtest_enabled()) {
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100227 period = 10000 / s->delta;
228 period_frac = 0;
229 }
pbrookd0a981b2009-03-31 14:34:24 +0000230
231 /* We need to divide time by period, where time is stored in
232 rem (64-bit integer) and period is stored in period/period_frac
233 (64.32 fixed point).
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +0100234
pbrookd0a981b2009-03-31 14:34:24 +0000235 Doing full precision division is hard, so scale values and
236 do a 64-bit division. The result should be rounded down,
237 so that the rounding error never causes the timer to go
238 backwards.
239 */
pbrook423f0742007-05-23 00:06:54 +0000240
Dmitry Osipenko56215da2016-07-14 16:51:36 +0100241 rem = next - now;
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100242 div = period;
pbrookd0a981b2009-03-31 14:34:24 +0000243
244 clz1 = clz64(rem);
245 clz2 = clz64(div);
246 shift = clz1 < clz2 ? clz1 : clz2;
247
248 rem <<= shift;
249 div <<= shift;
250 if (shift >= 32) {
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100251 div |= ((uint64_t)period_frac << (shift - 32));
pbrookd0a981b2009-03-31 14:34:24 +0000252 } else {
253 if (shift != 0)
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100254 div |= (period_frac >> (32 - shift));
pbrookd0a981b2009-03-31 14:34:24 +0000255 /* Look at remaining bits of period_frac and round div up if
256 necessary. */
Dmitry Osipenkoe91171e2016-06-06 16:59:30 +0100257 if ((uint32_t)(period_frac << shift))
pbrookd0a981b2009-03-31 14:34:24 +0000258 div += 1;
259 }
pbrook423f0742007-05-23 00:06:54 +0000260 counter = rem / div;
Dmitry Osipenko2b5c0322016-10-24 16:26:50 +0100261
262 if (s->policy_mask & PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD) {
263 /* Before wrapping around, timer should stay with counter = 0
264 for a one period. */
265 if (!oneshot && s->delta == s->limit) {
266 if (now == last) {
267 /* Counter == delta here, check whether it was
268 adjusted and if it was, then right now it is
269 that "one period". */
270 if (counter == s->limit + DELTA_ADJUST) {
271 return 0;
272 }
273 } else if (counter == s->limit) {
274 /* Since the counter is rounded down and now != last,
275 the counter == limit means that delta was adjusted
276 by +1 and right now it is that adjusted period. */
277 return 0;
278 }
279 }
280 }
pbrook423f0742007-05-23 00:06:54 +0000281 }
Dmitry Osipenko5580ea42016-10-24 16:26:52 +0100282
283 if (s->policy_mask & PTIMER_POLICY_NO_COUNTER_ROUND_DOWN) {
284 /* If now == last then delta == limit, i.e. the counter already
285 represents the correct value. It would be rounded down a 1ns
286 later. */
287 if (now != last) {
288 counter += 1;
289 }
290 }
pbrook423f0742007-05-23 00:06:54 +0000291 } else {
292 counter = s->delta;
293 }
294 return counter;
295}
296
blueswir18d05ea82007-05-24 19:48:41 +0000297void ptimer_set_count(ptimer_state *s, uint64_t count)
pbrook423f0742007-05-23 00:06:54 +0000298{
Peter Maydellaf2a5802019-11-11 13:44:16 +0000299 assert(s->in_transaction);
pbrook423f0742007-05-23 00:06:54 +0000300 s->delta = count;
301 if (s->enabled) {
Peter Maydellaf2a5802019-11-11 13:44:16 +0000302 s->need_reload = true;
pbrook423f0742007-05-23 00:06:54 +0000303 }
304}
305
306void ptimer_run(ptimer_state *s, int oneshot)
307{
Dmitry Osipenko869e92b2016-06-06 16:59:31 +0100308 bool was_disabled = !s->enabled;
309
Peter Maydellaf2a5802019-11-11 13:44:16 +0000310 assert(s->in_transaction);
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100311
Dmitry Osipenko869e92b2016-06-06 16:59:31 +0100312 if (was_disabled && s->period == 0) {
Dmitry Osipenko2a8b5872016-09-22 18:13:07 +0100313 if (!qtest_enabled()) {
314 fprintf(stderr, "Timer with period zero, disabling\n");
315 }
pbrook423f0742007-05-23 00:06:54 +0000316 return;
317 }
318 s->enabled = oneshot ? 2 : 1;
Dmitry Osipenko869e92b2016-06-06 16:59:31 +0100319 if (was_disabled) {
Peter Maydellaf2a5802019-11-11 13:44:16 +0000320 s->need_reload = true;
Dmitry Osipenko869e92b2016-06-06 16:59:31 +0100321 }
pbrook423f0742007-05-23 00:06:54 +0000322}
323
blueswir18d05ea82007-05-24 19:48:41 +0000324/* Pause a timer. Note that this may cause it to "lose" time, even if it
pbrook423f0742007-05-23 00:06:54 +0000325 is immediately restarted. */
326void ptimer_stop(ptimer_state *s)
327{
Peter Maydellaf2a5802019-11-11 13:44:16 +0000328 assert(s->in_transaction);
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100329
pbrook423f0742007-05-23 00:06:54 +0000330 if (!s->enabled)
331 return;
332
333 s->delta = ptimer_get_count(s);
Alex Blighbc72ad62013-08-21 16:03:08 +0100334 timer_del(s->timer);
pbrook423f0742007-05-23 00:06:54 +0000335 s->enabled = 0;
Peter Maydellaf2a5802019-11-11 13:44:16 +0000336 s->need_reload = false;
pbrook423f0742007-05-23 00:06:54 +0000337}
338
339/* Set counter increment interval in nanoseconds. */
340void ptimer_set_period(ptimer_state *s, int64_t period)
341{
Peter Maydellaf2a5802019-11-11 13:44:16 +0000342 assert(s->in_transaction);
Dmitry Osipenko7ef6e3c2016-06-06 16:59:30 +0100343 s->delta = ptimer_get_count(s);
pbrook423f0742007-05-23 00:06:54 +0000344 s->period = period;
345 s->period_frac = 0;
blueswir18d05ea82007-05-24 19:48:41 +0000346 if (s->enabled) {
Peter Maydellaf2a5802019-11-11 13:44:16 +0000347 s->need_reload = true;
blueswir18d05ea82007-05-24 19:48:41 +0000348 }
pbrook423f0742007-05-23 00:06:54 +0000349}
350
Peter Maydellad140da2021-01-28 11:41:21 +0000351/* Set counter increment interval from a Clock */
352void ptimer_set_period_from_clock(ptimer_state *s, const Clock *clk,
353 unsigned int divisor)
354{
355 /*
356 * The raw clock period is a 64-bit value in units of 2^-32 ns;
357 * put another way it's a 32.32 fixed-point ns value. Our internal
358 * representation of the period is 64.32 fixed point ns, so
359 * the conversion is simple.
360 */
361 uint64_t raw_period = clock_get(clk);
362 uint64_t period_frac;
363
364 assert(s->in_transaction);
365 s->delta = ptimer_get_count(s);
366 s->period = extract64(raw_period, 32, 32);
367 period_frac = extract64(raw_period, 0, 32);
368 /*
369 * divisor specifies a possible frequency divisor between the
370 * clock and the timer, so it is a multiplier on the period.
371 * We do the multiply after splitting the raw period out into
372 * period and frac to avoid having to do a 32*64->96 multiply.
373 */
374 s->period *= divisor;
375 period_frac *= divisor;
376 s->period += extract64(period_frac, 32, 32);
377 s->period_frac = (uint32_t)period_frac;
378
379 if (s->enabled) {
380 s->need_reload = true;
381 }
382}
383
pbrook423f0742007-05-23 00:06:54 +0000384/* Set counter frequency in Hz. */
385void ptimer_set_freq(ptimer_state *s, uint32_t freq)
386{
Peter Maydellaf2a5802019-11-11 13:44:16 +0000387 assert(s->in_transaction);
Dmitry Osipenko7ef6e3c2016-06-06 16:59:30 +0100388 s->delta = ptimer_get_count(s);
pbrook423f0742007-05-23 00:06:54 +0000389 s->period = 1000000000ll / freq;
390 s->period_frac = (1000000000ll << 32) / freq;
blueswir18d05ea82007-05-24 19:48:41 +0000391 if (s->enabled) {
Peter Maydellaf2a5802019-11-11 13:44:16 +0000392 s->need_reload = true;
blueswir18d05ea82007-05-24 19:48:41 +0000393 }
pbrook423f0742007-05-23 00:06:54 +0000394}
395
396/* Set the initial countdown value. If reload is nonzero then also set
397 count = limit. */
blueswir18d05ea82007-05-24 19:48:41 +0000398void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload)
pbrook423f0742007-05-23 00:06:54 +0000399{
Peter Maydellaf2a5802019-11-11 13:44:16 +0000400 assert(s->in_transaction);
pbrook423f0742007-05-23 00:06:54 +0000401 s->limit = limit;
402 if (reload)
403 s->delta = limit;
pbrook62ea5b02007-06-03 10:44:47 +0000404 if (s->enabled && reload) {
Peter Maydellaf2a5802019-11-11 13:44:16 +0000405 s->need_reload = true;
blueswir18d05ea82007-05-24 19:48:41 +0000406 }
407}
408
Dmitry Osipenko578c4b22016-06-06 16:59:31 +0100409uint64_t ptimer_get_limit(ptimer_state *s)
410{
411 return s->limit;
412}
413
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100414void ptimer_transaction_begin(ptimer_state *s)
415{
Peter Maydellaf2a5802019-11-11 13:44:16 +0000416 assert(!s->in_transaction);
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100417 s->in_transaction = true;
418 s->need_reload = false;
419}
420
421void ptimer_transaction_commit(ptimer_state *s)
422{
423 assert(s->in_transaction);
424 /*
425 * We must loop here because ptimer_reload() can call the callback
426 * function, which might then update ptimer state in a way that
427 * means we need to do another reload and possibly another callback.
428 * A disabled timer never needs reloading (and if we don't check
429 * this then we loop forever if ptimer_reload() disables the timer).
430 */
431 while (s->need_reload && s->enabled) {
432 s->need_reload = false;
433 s->next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
434 ptimer_reload(s, 0);
435 }
436 /* Now we've finished reload we can leave the transaction block. */
437 s->in_transaction = false;
438}
439
Juan Quintela852f7712010-12-01 23:51:14 +0100440const VMStateDescription vmstate_ptimer = {
Blue Swirl55a6e51f2009-08-31 19:30:15 +0000441 .name = "ptimer",
Juan Quintela852f7712010-12-01 23:51:14 +0100442 .version_id = 1,
443 .minimum_version_id = 1,
Richard Hendersonf55f1a42023-12-21 14:16:00 +1100444 .fields = (const VMStateField[]) {
Juan Quintela852f7712010-12-01 23:51:14 +0100445 VMSTATE_UINT8(enabled, ptimer_state),
446 VMSTATE_UINT64(limit, ptimer_state),
447 VMSTATE_UINT64(delta, ptimer_state),
448 VMSTATE_UINT32(period_frac, ptimer_state),
449 VMSTATE_INT64(period, ptimer_state),
450 VMSTATE_INT64(last_event, ptimer_state),
451 VMSTATE_INT64(next_event, ptimer_state),
Paolo Bonzinie7206772015-01-08 10:18:59 +0100452 VMSTATE_TIMER_PTR(timer, ptimer_state),
Juan Quintela852f7712010-12-01 23:51:14 +0100453 VMSTATE_END_OF_LIST()
454 }
Blue Swirl55a6e51f2009-08-31 19:30:15 +0000455};
456
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100457ptimer_state *ptimer_init(ptimer_cb callback, void *callback_opaque,
458 uint8_t policy_mask)
459{
460 ptimer_state *s;
461
Peter Maydellaf2a5802019-11-11 13:44:16 +0000462 /* The callback function is mandatory. */
Peter Maydell78b6eaa2019-10-08 18:17:21 +0100463 assert(callback);
464
465 s = g_new0(ptimer_state, 1);
466 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s);
467 s->policy_mask = policy_mask;
468 s->callback = callback;
469 s->callback_opaque = callback_opaque;
470
471 /*
472 * These two policies are incompatible -- trigger-on-decrement implies
473 * a timer trigger when the count becomes 0, but no-immediate-trigger
474 * implies a trigger when the count stops being 0.
475 */
476 assert(!((policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) &&
477 (policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)));
478 return s;
479}
480
Marc-André Lureau072bdb02017-01-27 12:55:51 +0400481void ptimer_free(ptimer_state *s)
482{
Marc-André Lureau072bdb02017-01-27 12:55:51 +0400483 timer_free(s->timer);
484 g_free(s);
485}