xref: /freebsd/share/man/man9/eventtimers.9 (revision cfc4b56b571f75e9b13eda2756bbd72a0c187a7a)
1f42acd0fSAlexander Motin.\"
2fdc5dd2dSAlexander Motin.\" Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org>
3f42acd0fSAlexander Motin.\" All rights reserved.
4f42acd0fSAlexander Motin.\"
5f42acd0fSAlexander Motin.\" Redistribution and use in source and binary forms, with or without
6f42acd0fSAlexander Motin.\" modification, are permitted provided that the following conditions
7f42acd0fSAlexander Motin.\" are met:
8f42acd0fSAlexander Motin.\" 1. Redistributions of source code must retain the above copyright
9f42acd0fSAlexander Motin.\"    notice, this list of conditions and the following disclaimer.
10f42acd0fSAlexander Motin.\" 2. Redistributions in binary form must reproduce the above copyright
11f42acd0fSAlexander Motin.\"    notice, this list of conditions and the following disclaimer in the
12f42acd0fSAlexander Motin.\"    documentation and/or other materials provided with the distribution.
13f42acd0fSAlexander Motin.\"
14f42acd0fSAlexander Motin.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
15f42acd0fSAlexander Motin.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16f42acd0fSAlexander Motin.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17f42acd0fSAlexander Motin.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
18f42acd0fSAlexander Motin.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19f42acd0fSAlexander Motin.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20f42acd0fSAlexander Motin.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21f42acd0fSAlexander Motin.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22f42acd0fSAlexander Motin.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23f42acd0fSAlexander Motin.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24f42acd0fSAlexander Motin.\"
25f42acd0fSAlexander Motin.\" $FreeBSD$
26f42acd0fSAlexander Motin.\"
27*cfc4b56bSIan Lepore.Dd April 2, 2014
28f42acd0fSAlexander Motin.Dt EVENTTIMERS 9
29f42acd0fSAlexander Motin.Os
30f42acd0fSAlexander Motin.Sh NAME
31f42acd0fSAlexander Motin.Nm eventtimers
32f42acd0fSAlexander Motin.Nd kernel event timers subsystem
33f42acd0fSAlexander Motin.Sh SYNOPSIS
34f42acd0fSAlexander Motin.In sys/timeet.h
35f42acd0fSAlexander Motin.Bd -literal
36f42acd0fSAlexander Motinstruct eventtimer;
37f42acd0fSAlexander Motin
38f42acd0fSAlexander Motintypedef int et_start_t(struct eventtimer *et,
39fdc5dd2dSAlexander Motin    sbintime_t first, sbintime_t period);
40f42acd0fSAlexander Motintypedef int et_stop_t(struct eventtimer *et);
41f42acd0fSAlexander Motintypedef void et_event_cb_t(struct eventtimer *et, void *arg);
42f42acd0fSAlexander Motintypedef int et_deregister_cb_t(struct eventtimer *et, void *arg);
43f42acd0fSAlexander Motin
44f42acd0fSAlexander Motinstruct eventtimer {
45f42acd0fSAlexander Motin	SLIST_ENTRY(eventtimer)	et_all;
46f42acd0fSAlexander Motin	char			*et_name;
47f42acd0fSAlexander Motin	int			et_flags;
48f42acd0fSAlexander Motin#define ET_FLAGS_PERIODIC	1
49f42acd0fSAlexander Motin#define ET_FLAGS_ONESHOT	2
50f42acd0fSAlexander Motin#define ET_FLAGS_PERCPU		4
51f42acd0fSAlexander Motin#define ET_FLAGS_C3STOP		8
52f42acd0fSAlexander Motin#define ET_FLAGS_POW2DIV	16
53f42acd0fSAlexander Motin	int			et_quality;
54f42acd0fSAlexander Motin	int			et_active;
556b99842aSEd Schouten	uint64_t		et_frequency;
56fdc5dd2dSAlexander Motin	sbintime_t		et_min_period;
57fdc5dd2dSAlexander Motin	sbintime_t		et_max_period;
58f42acd0fSAlexander Motin	et_start_t		*et_start;
59f42acd0fSAlexander Motin	et_stop_t		*et_stop;
60f42acd0fSAlexander Motin	et_event_cb_t		*et_event_cb;
61f42acd0fSAlexander Motin	et_deregister_cb_t	*et_deregister_cb;
62f42acd0fSAlexander Motin	void 			*et_arg;
63f42acd0fSAlexander Motin	void			*et_priv;
64f42acd0fSAlexander Motin	struct sysctl_oid	*et_sysctl;
65f42acd0fSAlexander Motin};
66f42acd0fSAlexander Motin.Ed
67f42acd0fSAlexander Motin.Ft int
68f42acd0fSAlexander Motin.Fn et_register "struct eventtimer *et"
69f42acd0fSAlexander Motin.Ft int
70f42acd0fSAlexander Motin.Fn et_deregister "struct eventtimer *et"
71*cfc4b56bSIan Lepore.Ft void
72*cfc4b56bSIan Lepore.Fn et_change_frequency "struct eventtimer *et" "uint64_t newfreq"
73f42acd0fSAlexander Motin.Fn ET_LOCK
74f42acd0fSAlexander Motin.Fn ET_UNLOCK
75f42acd0fSAlexander Motin.Ft struct eventtimer *
76f42acd0fSAlexander Motin.Fn et_find "const char *name" "int check" "int want"
77f42acd0fSAlexander Motin.Ft int
78f42acd0fSAlexander Motin.Fn et_init "struct eventtimer *et" "et_event_cb_t *event" "et_deregister_cb_t *deregister" "void *arg"
79f42acd0fSAlexander Motin.Ft int
80fdc5dd2dSAlexander Motin.Fn et_start "struct eventtimer *et" "sbintime_t first" "sbintime_t period"
81f42acd0fSAlexander Motin.Ft int
82f42acd0fSAlexander Motin.Fn et_stop "struct eventtimer *et"
83f42acd0fSAlexander Motin.Ft int
84f42acd0fSAlexander Motin.Fn et_ban "struct eventtimer *et"
85f42acd0fSAlexander Motin.Ft int
86f42acd0fSAlexander Motin.Fn et_free "struct eventtimer *et"
87f42acd0fSAlexander Motin.Sh DESCRIPTION
88f42acd0fSAlexander MotinEvent timers are responsible for generating interrupts at specified time
89f42acd0fSAlexander Motinor periodically, to run different time-based events.
90f42acd0fSAlexander MotinSubsystem consists of three main parts:
91ecb0bac9SGlen Barber.Bl -tag -width "Consumers"
92f42acd0fSAlexander Motin.It Drivers
93f42acd0fSAlexander MotinManage hardware to generate requested time events.
94f42acd0fSAlexander Motin.It Consumers
95f42acd0fSAlexander Motin.Pa sys/kern/kern_clocksource.c
96f42acd0fSAlexander Motinuses event timers to supply kernel with
97f42acd0fSAlexander Motin.Fn hardclock ,
98f42acd0fSAlexander Motin.Fn statclock
99f42acd0fSAlexander Motinand
100f42acd0fSAlexander Motin.Fn profclock
101f42acd0fSAlexander Motintime events.
102f42acd0fSAlexander Motin.It Glue code
103f42acd0fSAlexander Motin.Pa sys/sys/timeet.h ,
104f42acd0fSAlexander Motin.Pa sys/kern/kern_et.c
105f42acd0fSAlexander Motinprovide APIs for event timer drivers and consumers.
106f42acd0fSAlexander Motin.El
107f42acd0fSAlexander Motin.Sh DRIVER API
108f42acd0fSAlexander MotinDriver API is built around eventtimer structure.
109f42acd0fSAlexander MotinTo register its functionality driver allocates that structure and calls
110f42acd0fSAlexander Motin.Fn et_register .
111f42acd0fSAlexander MotinDriver should fill following fields there:
112ecb0bac9SGlen Barber.Bl -tag -width Va
113f42acd0fSAlexander Motin.It Va et_name
114f42acd0fSAlexander MotinUnique name of the event timer for management purposes.
115f42acd0fSAlexander Motin.It Va et_flags
116f42acd0fSAlexander MotinSet of flags, describing timer capabilities:
117f42acd0fSAlexander Motin.Bl -tag -width "ET_FLAGS_PERIODIC" -compact
118f42acd0fSAlexander Motin.It ET_FLAGS_PERIODIC
119f42acd0fSAlexander MotinPeriodic mode supported.
120f42acd0fSAlexander Motin.It ET_FLAGS_ONESHOT
121f42acd0fSAlexander MotinOne-shot mode supported.
122f42acd0fSAlexander Motin.It ET_FLAGS_PERCPU
123f42acd0fSAlexander MotinTimer is per-CPU.
124f42acd0fSAlexander Motin.It ET_FLAGS_C3STOP
125f42acd0fSAlexander MotinTimer may stop in CPU sleep state.
126f42acd0fSAlexander Motin.It ET_FLAGS_POW2DIV
127f42acd0fSAlexander MotinTimer supports only 2^n divisors.
128f42acd0fSAlexander Motin.El
129f42acd0fSAlexander Motin.It Va et_quality
130f42acd0fSAlexander MotinAbstract value to certify whether this timecounter is better than the others.
131f42acd0fSAlexander MotinHigher value means better.
132f42acd0fSAlexander Motin.It Va et_frequency
133f42acd0fSAlexander MotinTimer oscillator's base frequency, if applicable and known.
134f42acd0fSAlexander MotinUsed by consumers to predict set of possible frequencies that could be
135f42acd0fSAlexander Motinobtained by dividing it.
136f42acd0fSAlexander MotinShould be zero if not applicable or unknown.
137f42acd0fSAlexander Motin.It Va et_min_period , et_max_period
138f42acd0fSAlexander MotinMinimal and maximal reliably programmable time periods.
139f42acd0fSAlexander Motin.It Va et_start
140f42acd0fSAlexander MotinDriver's timer start function pointer.
141f42acd0fSAlexander Motin.It Va et_stop
142f42acd0fSAlexander MotinDriver's timer stop function pointer.
143f42acd0fSAlexander Motin.It Va et_priv
144f42acd0fSAlexander MotinDriver's private data storage.
145f42acd0fSAlexander Motin.El
146f42acd0fSAlexander Motin.Pp
147f42acd0fSAlexander MotinAfter the event timer functionality is registered, it is controlled via
148f42acd0fSAlexander Motin.Va et_start
149f42acd0fSAlexander Motinand
150f42acd0fSAlexander Motin.Va et_stop
151f42acd0fSAlexander Motinmethods.
152f42acd0fSAlexander Motin.Va et_start
153f42acd0fSAlexander Motinmethod is called to start the specified event timer.
154f42acd0fSAlexander MotinThe last two arguments are used to specify time when events should be
155f42acd0fSAlexander Motingenerated.
156f42acd0fSAlexander Motin.Va first
157f42acd0fSAlexander Motinargument specifies time period before the first event generated.
158f42acd0fSAlexander MotinIn periodic mode NULL value specifies that first period is equal to the
159f42acd0fSAlexander Motin.Va period
160f42acd0fSAlexander Motinargument value.
161f42acd0fSAlexander Motin.Va period
162f42acd0fSAlexander Motinargument specifies the time period between following events for the
163f42acd0fSAlexander Motinperiodic mode.
164f42acd0fSAlexander MotinThe NULL value there specifies the one-shot mode.
165f42acd0fSAlexander MotinAt least one of these two arguments should be not NULL.
166f42acd0fSAlexander MotinWhen event time arrive, driver should call
167f42acd0fSAlexander Motin.Va et_event_cb
168f42acd0fSAlexander Motincallback function, passing
169f42acd0fSAlexander Motin.Va et_arg
170f42acd0fSAlexander Motinas the second argument.
171f42acd0fSAlexander Motin.Va et_stop
172f42acd0fSAlexander Motinmethod is called to stop the specified event timer.
173f42acd0fSAlexander MotinFor the per-CPU event timers
174f42acd0fSAlexander Motin.Va et_start
175f42acd0fSAlexander Motinand
176f42acd0fSAlexander Motin.Va et_stop
177f42acd0fSAlexander Motinmethods control timers associated with the current CPU.
178f42acd0fSAlexander Motin.Pp
179f42acd0fSAlexander MotinDriver may deregister its functionality by calling
180f42acd0fSAlexander Motin.Fn et_deregister .
181*cfc4b56bSIan Lepore.Pp
182*cfc4b56bSIan LeporeIf the frequency of the clock hardware can change while it is
183*cfc4b56bSIan Leporerunning (for example, during power-saving modes), the driver must call
184*cfc4b56bSIan Lepore.Fn et_change_frequency
185*cfc4b56bSIan Leporeon each change.
186*cfc4b56bSIan LeporeIf the given event timer is the active timer,
187*cfc4b56bSIan Lepore.Fn et_change_frequency
188*cfc4b56bSIan Leporestops the timer on all CPUs, updates
189*cfc4b56bSIan Lepore.Va et->frequency ,
190*cfc4b56bSIan Leporethen restarts the timer on all CPUs so that all
191*cfc4b56bSIan Leporecurrent events are rescheduled using the new frequency.
192*cfc4b56bSIan LeporeIf the given timer is not currently active,
193*cfc4b56bSIan Lepore.Fn et_change_frequency
194*cfc4b56bSIan Leporesimply updates
195*cfc4b56bSIan Lepore.Va et->frequency .
196f42acd0fSAlexander Motin.Sh CONSUMER API
197f42acd0fSAlexander Motin.Fn et_find
198f42acd0fSAlexander Motinallows consumer to find available event timer, optionally matching specific
199f42acd0fSAlexander Motinname and/or capability flags.
200f42acd0fSAlexander MotinConsumer may read returned eventtimer structure, but should not modify it.
201f42acd0fSAlexander MotinWhen wanted event timer is found,
202f42acd0fSAlexander Motin.Fn et_init
203f42acd0fSAlexander Motinshould be called for it, submitting
204f42acd0fSAlexander Motin.Va event
205f42acd0fSAlexander Motinand optionally
206f42acd0fSAlexander Motin.Va deregister
207f42acd0fSAlexander Motincallbacks functions, and the opaque argument
208f42acd0fSAlexander Motin.Va arg .
209f42acd0fSAlexander MotinThat argument will be passed as argument to the callbacks.
210f42acd0fSAlexander MotinEvent callback function will be called on scheduled time events.
211f42acd0fSAlexander MotinIt is called from the hardware interrupt context, so no sleep is permitted
212f42acd0fSAlexander Motinthere.
213f42acd0fSAlexander MotinDeregister callback function may be called to report consumer that the event
214f42acd0fSAlexander Motintimer functionality is no longer available.
215f42acd0fSAlexander MotinOn this call, consumer should stop using event timer before the return.
216f42acd0fSAlexander Motin.Pp
217f42acd0fSAlexander MotinAfter the timer is found and initialized, it can be controlled via
218f42acd0fSAlexander Motin.Fn et_start
219f42acd0fSAlexander Motinand
220f42acd0fSAlexander Motin.Fn et_stop .
221f42acd0fSAlexander MotinThe arguments are the same as described in driver API.
222f42acd0fSAlexander MotinPer-CPU event timers can be controlled only from specific CPUs.
223f42acd0fSAlexander Motin.Pp
224f42acd0fSAlexander Motin.Fn et_ban
225f42acd0fSAlexander Motinallows consumer to mark event timer as broken via clearing both one-shot and
226f42acd0fSAlexander Motinperiodic capability flags, if it was somehow detected.
227f42acd0fSAlexander Motin.Fn et_free
228f42acd0fSAlexander Motinis the opposite to
229f42acd0fSAlexander Motin.Fn et_init .
230f42acd0fSAlexander MotinIt releases the event timer for other consumers use.
231f42acd0fSAlexander Motin.Pp
232f42acd0fSAlexander Motin.Fn ET_LOCK
233f42acd0fSAlexander Motinand
234f42acd0fSAlexander Motin.Fn ET_UNLOCK
235f42acd0fSAlexander Motinmacros should be used to manage
236f42acd0fSAlexander Motin.Xr mutex 9
237f42acd0fSAlexander Motinlock around
238f42acd0fSAlexander Motin.Fn et_find ,
239f42acd0fSAlexander Motin.Fn et_init
240f42acd0fSAlexander Motinand
241f42acd0fSAlexander Motin.Fn et_free
242f42acd0fSAlexander Motincalls to serialize access to the list of the registered event timers and the
243f42acd0fSAlexander Motinpointers returned by
244f42acd0fSAlexander Motin.Fn et_find .
245f42acd0fSAlexander Motin.Fn et_start
246f42acd0fSAlexander Motinand
247f42acd0fSAlexander Motin.Fn et_stop
248f42acd0fSAlexander Motincalls should be serialized in consumer's internal way to avoid concurrent
249f42acd0fSAlexander Motintimer hardware access.
250f42acd0fSAlexander Motin.Sh SEE ALSO
251f42acd0fSAlexander Motin.Xr eventtimers 4
252f42acd0fSAlexander Motin.Sh AUTHORS
253f42acd0fSAlexander Motin.An Alexander Motin Aq mav@FreeBSD.org
254