2 * General purpose implementation of a simple periodic countdown timer.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GNU LGPL.
11 #include "qemu-common.h"
12 #include "qemu/timer.h"
13 #include "migration/vmstate.h"
15 /* The default ptimer policy retains backward compatibility with the legacy
16 * timers. Custom policies are adjusting the default one. Consider providing
17 * a correct policy for your timer.
19 * The rough edges of the default policy:
20 * - Starting to run with a period = 0 emits error message and stops the
21 * timer without a trigger.
23 * - Setting period to 0 of the running timer emits error message and
24 * stops the timer without a trigger.
26 * - Starting to run with counter = 0 or setting it to "0" while timer
27 * is running causes a trigger and reloads counter with a limit value.
28 * If limit = 0, ptimer emits error message and stops the timer.
30 * - Counter value of the running timer is one less than the actual value.
32 * - Changing period/frequency of the running timer loses time elapsed
33 * since the last period, effectively restarting the timer with a
34 * counter = counter value at the moment of change (.i.e. one less).
36 #define PTIMER_POLICY_DEFAULT 0
39 typedef struct ptimer_state ptimer_state
;
40 typedef void (*ptimer_cb
)(void *opaque
);
42 ptimer_state
*ptimer_init(QEMUBH
*bh
, uint8_t policy_mask
);
43 void ptimer_set_period(ptimer_state
*s
, int64_t period
);
44 void ptimer_set_freq(ptimer_state
*s
, uint32_t freq
);
45 uint64_t ptimer_get_limit(ptimer_state
*s
);
46 void ptimer_set_limit(ptimer_state
*s
, uint64_t limit
, int reload
);
47 uint64_t ptimer_get_count(ptimer_state
*s
);
48 void ptimer_set_count(ptimer_state
*s
, uint64_t count
);
49 void ptimer_run(ptimer_state
*s
, int oneshot
);
50 void ptimer_stop(ptimer_state
*s
);
52 extern const VMStateDescription vmstate_ptimer
;
54 #define VMSTATE_PTIMER(_field, _state) \
55 VMSTATE_STRUCT_POINTER_V(_field, _state, 1, vmstate_ptimer, ptimer_state)
57 #define VMSTATE_PTIMER_ARRAY(_f, _s, _n) \
58 VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(_f, _s, _n, 0, \
59 vmstate_ptimer, ptimer_state)