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