aboutsummaryrefslogtreecommitdiff
path: root/include/timer.h
blob: d19053277e2dfc97db56435d2c1aee7a344321e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
/* Copyright 2014-2015 IBM Corp. */

#ifndef __TIMER_H
#define __TIMER_H

#include <stdint.h>
#include <ccan/list/list.h>

struct timer;

typedef void (*timer_func_t)(struct timer *t, void *data, uint64_t now);

/* Structure exposed in order to be able to allocate it
 * statically but otherwise, use accessors, don't access
 * the fields directly
 *
 * WARNING: Do not free a timer object unless you have cancelled
 * it first or you know it won't reschedule itself and have done
 * a sync_timer() on it. The timer core *will* access the object
 * again after you return from the expiry callback so it must not
 * be freed from the callback itself.
 */
struct timer {
	struct list_node	link;
	uint64_t		target;
	timer_func_t		expiry;
	void *			user_data;
	void *			running;
	uint64_t		gen;
};

extern void init_timer(struct timer *t, timer_func_t expiry, void *data);

/* (re)schedule a timer. If already scheduled, it's expiry will be updated
 *
 * This doesn't synchronize so if the timer also reschedules itself there
 * is no telling which one "wins". The advantage is that this can be called
 * with any lock held or from the timer expiry itself.
 *
 * We support a magic expiry of TIMER_POLL which causes a given timer to
 * be called whenever OPAL main polling loop is run, which is often during
 * boot and occasionally while Linux is up. This can be used with both
 * schedule_timer() and schedule_timer_at()
 *
 * This is useful for a number of interrupt driven drivers to have a way
 * to crank their state machine at times when the interrupt isn't available
 * such as during early boot.
 *
 * Note: For convenience, schedule_timer() returns the current TB value
 */
#define TIMER_POLL	((uint64_t)-1)
extern uint64_t schedule_timer(struct timer *t, uint64_t how_long);
extern void schedule_timer_at(struct timer *t, uint64_t when);

/* Synchronization point with the timer. If the callback has started before
 * that function is called, it will be complete when this function returns.
 *
 * It might start *again* but at least anything before this will be visible
 * to any subsequent occurrence.
 *
 * The usual issue of such sync functions exist: don't call it while holding
 * a lock that the timer callback might take or from the timer expiry itself.
 */
extern void sync_timer(struct timer *t);

/* cancel_timer() will ensure the timer isn't concurrently running so
 * the cancellation is guaranteed even if the timer reschedules itself.
 *
 * This uses sync_timer() internally so don't call this while holding a
 * lock the timer might use.
 */
extern void cancel_timer(struct timer *t);

/* cancel_timer_async() allows to remove the timer from the schedule
 * list without trying to synchronize. This is useful if the cancellation
 * must happen while holding locks that would make the synchronization
 * impossible. The user is responsible of ensuring it deals with potentially
 * spurrious occurrences
 */
extern void cancel_timer_async(struct timer *t);

/* Run the timers */
extern void check_timers(bool from_interrupt);

/* Core init */
void late_init_timers(void);

#endif /* __TIMER_H */