xref: /dragonfly/sys/kern/kern_systimer.c (revision 8fbc264d)
188c4d2f6SMatthew Dillon /*
28c10bfcfSMatthew Dillon  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
38c10bfcfSMatthew Dillon  *
48c10bfcfSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
58c10bfcfSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
688c4d2f6SMatthew Dillon  *
788c4d2f6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
888c4d2f6SMatthew Dillon  * modification, are permitted provided that the following conditions
988c4d2f6SMatthew Dillon  * are met:
108c10bfcfSMatthew Dillon  *
1188c4d2f6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
1288c4d2f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
1388c4d2f6SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
148c10bfcfSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
158c10bfcfSMatthew Dillon  *    the documentation and/or other materials provided with the
168c10bfcfSMatthew Dillon  *    distribution.
178c10bfcfSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
188c10bfcfSMatthew Dillon  *    contributors may be used to endorse or promote products derived
198c10bfcfSMatthew Dillon  *    from this software without specific, prior written permission.
2088c4d2f6SMatthew Dillon  *
218c10bfcfSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
228c10bfcfSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
238c10bfcfSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
248c10bfcfSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
258c10bfcfSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
268c10bfcfSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
278c10bfcfSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
288c10bfcfSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
298c10bfcfSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
308c10bfcfSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
318c10bfcfSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3288c4d2f6SMatthew Dillon  * SUCH DAMAGE.
3388c4d2f6SMatthew Dillon  */
3488c4d2f6SMatthew Dillon 
3588c4d2f6SMatthew Dillon /*
367358cf5bSMatthew Dillon  * WARNING!  THE SYSTIMER MODULE DOES NOT OPERATE OR DISPATCH WITH THE
377358cf5bSMatthew Dillon  * MP LOCK HELD.  ALL CODE USING THIS MODULE MUST BE MP-SAFE.
387358cf5bSMatthew Dillon  *
3988c4d2f6SMatthew Dillon  * This code implements a fine-grained per-cpu system timer which is
4088c4d2f6SMatthew Dillon  * ultimately based on a hardware timer.  The hardware timer abstraction
4188c4d2f6SMatthew Dillon  * is sufficiently disconnected from this code to support both per-cpu
4288c4d2f6SMatthew Dillon  * hardware timers or a single system-wide hardware timer.
4388c4d2f6SMatthew Dillon  *
444b52d1afSMatthew Dillon  * WARNING!  During early boot if a new system timer is selected, existing
454b52d1afSMatthew Dillon  * timeouts will not be effected and will thus occur slower or faster.
464b52d1afSMatthew Dillon  * periodic timers will be adjusted at the next periodic load.
474b52d1afSMatthew Dillon  *
4888c4d2f6SMatthew Dillon  * Notes on machine-dependant code (in arch/arch/systimer.c)
4988c4d2f6SMatthew Dillon  *
5088c4d2f6SMatthew Dillon  * cputimer_intr_reload()	Reload the one-shot (per-cpu basis)
5188c4d2f6SMatthew Dillon  */
5288c4d2f6SMatthew Dillon 
5388c4d2f6SMatthew Dillon #include <sys/param.h>
5488c4d2f6SMatthew Dillon #include <sys/kernel.h>
5588c4d2f6SMatthew Dillon #include <sys/systm.h>
5688c4d2f6SMatthew Dillon #include <sys/thread.h>
5788c4d2f6SMatthew Dillon #include <sys/globaldata.h>
5888c4d2f6SMatthew Dillon #include <sys/systimer.h>
5988c4d2f6SMatthew Dillon #include <sys/thread2.h>
6088c4d2f6SMatthew Dillon 
6188c4d2f6SMatthew Dillon /*
6288c4d2f6SMatthew Dillon  * Execute ready systimers.  Called directly from the platform-specific
63774fc796SMatthew Dillon  * one-shot timer clock interrupt (e.g. clkintr()) or via an IPI.  May
64774fc796SMatthew Dillon  * be called simultaniously on multiple cpus and always operations on
65774fc796SMatthew Dillon  * the current cpu's queue.  Systimer functions are responsible for calling
66774fc796SMatthew Dillon  * hardclock, statclock, and other finely-timed routines.
6788c4d2f6SMatthew Dillon  */
6888c4d2f6SMatthew Dillon void
systimer_intr(sysclock_t * timep,int in_ipi,struct intrframe * frame)6996d52ac8SSepherosa Ziehau systimer_intr(sysclock_t *timep, int in_ipi, struct intrframe *frame)
7088c4d2f6SMatthew Dillon {
7188c4d2f6SMatthew Dillon     globaldata_t gd = mycpu;
7288c4d2f6SMatthew Dillon     sysclock_t time = *timep;
7388c4d2f6SMatthew Dillon     systimer_t info;
7488c4d2f6SMatthew Dillon 
7588c4d2f6SMatthew Dillon     if (gd->gd_syst_nest)
7688c4d2f6SMatthew Dillon 	return;
7788c4d2f6SMatthew Dillon 
7888c4d2f6SMatthew Dillon     crit_enter();
7988c4d2f6SMatthew Dillon     ++gd->gd_syst_nest;
8088c4d2f6SMatthew Dillon     while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
8188c4d2f6SMatthew Dillon 	/*
8288c4d2f6SMatthew Dillon 	 * If we haven't reached the requested time, tell the cputimer
8388c4d2f6SMatthew Dillon 	 * how much is left and break out.
8488c4d2f6SMatthew Dillon 	 */
85*8fbc264dSMatthew Dillon 	if ((ssysclock_t)(info->time - time) > 0) {
8688c4d2f6SMatthew Dillon 	    cputimer_intr_reload(info->time - time);
8788c4d2f6SMatthew Dillon 	    break;
8888c4d2f6SMatthew Dillon 	}
8988c4d2f6SMatthew Dillon 
9088c4d2f6SMatthew Dillon 	/*
91bdfb4a94SSepherosa Ziehau 	 * Dequeue and execute, detect a loss of the systimer.  Note
92bdfb4a94SSepherosa Ziehau 	 * that the in-progress systimer pointer can only be used to
93bdfb4a94SSepherosa Ziehau 	 * detect a loss of the systimer, it is only useful within
94bdfb4a94SSepherosa Ziehau 	 * this code sequence and becomes stale otherwise.
9588c4d2f6SMatthew Dillon 	 */
9688c4d2f6SMatthew Dillon 	info->flags &= ~SYSTF_ONQUEUE;
9788c4d2f6SMatthew Dillon 	TAILQ_REMOVE(info->queue, info, node);
98bdfb4a94SSepherosa Ziehau 	gd->gd_systimer_inprog = info;
9988c4d2f6SMatthew Dillon 	crit_exit();
10096d52ac8SSepherosa Ziehau 	info->func(info, in_ipi, frame);
10188c4d2f6SMatthew Dillon 	crit_enter();
10288c4d2f6SMatthew Dillon 
10388c4d2f6SMatthew Dillon 	/*
104bdfb4a94SSepherosa Ziehau 	 * The caller may deleted or even re-queue the systimer itself
105bdfb4a94SSepherosa Ziehau 	 * with a delete/add sequence.  If the caller does not mess with
106bdfb4a94SSepherosa Ziehau 	 * the systimer we will requeue the periodic interval automatically.
107bdfb4a94SSepherosa Ziehau 	 *
108bdfb4a94SSepherosa Ziehau 	 * If this is a non-queued periodic interrupt, do not allow multiple
109bdfb4a94SSepherosa Ziehau 	 * events to build up (used for things like the callout timer to
110bdfb4a94SSepherosa Ziehau 	 * prevent premature timeouts due to long interrupt disablements,
111bdfb4a94SSepherosa Ziehau 	 * BIOS 8254 glitching, and so forth).  However, we still want to
112bdfb4a94SSepherosa Ziehau 	 * keep things synchronized between cpus for efficient handling of
113bdfb4a94SSepherosa Ziehau 	 * the timer interrupt so jump in multiples of the periodic rate.
11488c4d2f6SMatthew Dillon 	 */
115bdfb4a94SSepherosa Ziehau 	if (gd->gd_systimer_inprog == info && info->periodic) {
1164b52d1afSMatthew Dillon 	    if (info->which != sys_cputimer) {
1174b52d1afSMatthew Dillon 		info->periodic = sys_cputimer->fromhz(info->freq);
1184b52d1afSMatthew Dillon 		info->which = sys_cputimer;
1194b52d1afSMatthew Dillon 	    }
12088c4d2f6SMatthew Dillon 	    info->time += info->periodic;
1210d1dffdfSMatthew Dillon 	    if ((info->flags & SYSTF_NONQUEUED) &&
122*8fbc264dSMatthew Dillon 		(ssysclock_t)(info->time - time) <= 0
1230d1dffdfSMatthew Dillon 	    ) {
124a77a893aSSascha Wildner 		info->time += roundup(time - info->time, info->periodic);
1250d1dffdfSMatthew Dillon 	    }
12688c4d2f6SMatthew Dillon 	    systimer_add(info);
12788c4d2f6SMatthew Dillon 	}
128bdfb4a94SSepherosa Ziehau 	gd->gd_systimer_inprog = NULL;
12988c4d2f6SMatthew Dillon     }
13088c4d2f6SMatthew Dillon     --gd->gd_syst_nest;
13188c4d2f6SMatthew Dillon     crit_exit();
13288c4d2f6SMatthew Dillon }
13388c4d2f6SMatthew Dillon 
13488c4d2f6SMatthew Dillon void
systimer_intr_enable(void)13543adde98SSepherosa Ziehau systimer_intr_enable(void)
13643adde98SSepherosa Ziehau {
13743adde98SSepherosa Ziehau     cputimer_intr_enable();
13843adde98SSepherosa Ziehau }
13943adde98SSepherosa Ziehau 
1403919ced0SMatthew Dillon /*
1413919ced0SMatthew Dillon  * MPSAFE
1423919ced0SMatthew Dillon  */
14343adde98SSepherosa Ziehau void
systimer_add(systimer_t info)14488c4d2f6SMatthew Dillon systimer_add(systimer_t info)
14588c4d2f6SMatthew Dillon {
14688c4d2f6SMatthew Dillon     struct globaldata *gd = mycpu;
14788c4d2f6SMatthew Dillon 
148f9e5a194SSepherosa Ziehau     KKASSERT((info->flags & SYSTF_ONQUEUE) == 0);
14988c4d2f6SMatthew Dillon     crit_enter();
15088c4d2f6SMatthew Dillon     if (info->gd == gd) {
15188c4d2f6SMatthew Dillon 	systimer_t scan1;
15288c4d2f6SMatthew Dillon 	systimer_t scan2;
15388c4d2f6SMatthew Dillon 	scan1 = TAILQ_FIRST(&gd->gd_systimerq);
154*8fbc264dSMatthew Dillon 	if (scan1 == NULL || (ssysclock_t)(scan1->time - info->time) > 0) {
155044ee7c4SMatthew Dillon 	    cputimer_intr_reload(info->time - sys_cputimer->count());
15688c4d2f6SMatthew Dillon 	    TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
15788c4d2f6SMatthew Dillon 	} else {
15888c4d2f6SMatthew Dillon 	    scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
15988c4d2f6SMatthew Dillon 	    for (;;) {
16088c4d2f6SMatthew Dillon 		if (scan1 == NULL) {
16188c4d2f6SMatthew Dillon 		    TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
16288c4d2f6SMatthew Dillon 		    break;
16388c4d2f6SMatthew Dillon 		}
164c6a766f4SMatthew Dillon 		if (info->flags & SYSTF_FIRST) {
165c6a766f4SMatthew Dillon 			/*
166c6a766f4SMatthew Dillon 			 * When coincident events occur, the event being
167c6a766f4SMatthew Dillon 			 * added wants to be placed before the others.
168c6a766f4SMatthew Dillon 			 */
169*8fbc264dSMatthew Dillon 			if ((ssysclock_t)(scan1->time - info->time) >= 0) {
170c6a766f4SMatthew Dillon 			    TAILQ_INSERT_BEFORE(scan1, info, node);
171c6a766f4SMatthew Dillon 			    break;
172c6a766f4SMatthew Dillon 			}
173*8fbc264dSMatthew Dillon 			if ((ssysclock_t)(scan2->time - info->time) < 0) {
174c6a766f4SMatthew Dillon 			    TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2,
175c6a766f4SMatthew Dillon 					       info, node);
176c6a766f4SMatthew Dillon 			    break;
177c6a766f4SMatthew Dillon 			}
178c6a766f4SMatthew Dillon 		} else {
179c6a766f4SMatthew Dillon 			/*
180c6a766f4SMatthew Dillon 			 * When coincident events occur, the event being
181c6a766f4SMatthew Dillon 			 * added should be placed after the others.  This
182c6a766f4SMatthew Dillon 			 * is the default.
183c6a766f4SMatthew Dillon 			 */
184*8fbc264dSMatthew Dillon 			if ((ssysclock_t)(scan1->time - info->time) > 0) {
18588c4d2f6SMatthew Dillon 			    TAILQ_INSERT_BEFORE(scan1, info, node);
18688c4d2f6SMatthew Dillon 			    break;
18788c4d2f6SMatthew Dillon 			}
188*8fbc264dSMatthew Dillon 			if ((ssysclock_t)(scan2->time - info->time) <= 0) {
189c6a766f4SMatthew Dillon 			    TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2,
190c6a766f4SMatthew Dillon 					        info, node);
19188c4d2f6SMatthew Dillon 			    break;
19288c4d2f6SMatthew Dillon 			}
193c6a766f4SMatthew Dillon 		}
19488c4d2f6SMatthew Dillon 		scan1 = TAILQ_NEXT(scan1, node);
19588c4d2f6SMatthew Dillon 		scan2 = TAILQ_PREV(scan2, systimerq, node);
19688c4d2f6SMatthew Dillon 	    }
19788c4d2f6SMatthew Dillon 	}
19888c4d2f6SMatthew Dillon 	info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
19988c4d2f6SMatthew Dillon 	info->queue = &gd->gd_systimerq;
20088c4d2f6SMatthew Dillon     } else {
201f9e5a194SSepherosa Ziehau 	KKASSERT((info->flags & SYSTF_IPIRUNNING) == 0);
20288c4d2f6SMatthew Dillon 	info->flags |= SYSTF_IPIRUNNING;
203b8a98473SMatthew Dillon 	lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info);
20488c4d2f6SMatthew Dillon     }
20588c4d2f6SMatthew Dillon     crit_exit();
20688c4d2f6SMatthew Dillon }
20788c4d2f6SMatthew Dillon 
20888c4d2f6SMatthew Dillon /*
20988c4d2f6SMatthew Dillon  * systimer_del()
21088c4d2f6SMatthew Dillon  *
21188c4d2f6SMatthew Dillon  *	Delete a system timer.  Only the owning cpu can delete a timer.
2123919ced0SMatthew Dillon  *
2133919ced0SMatthew Dillon  * MPSAFE
21488c4d2f6SMatthew Dillon  */
21588c4d2f6SMatthew Dillon void
systimer_del(systimer_t info)21688c4d2f6SMatthew Dillon systimer_del(systimer_t info)
21788c4d2f6SMatthew Dillon {
218bdfb4a94SSepherosa Ziehau     struct globaldata *gd = info->gd;
219bdfb4a94SSepherosa Ziehau 
220bdfb4a94SSepherosa Ziehau     KKASSERT(gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
221bdfb4a94SSepherosa Ziehau 
22288c4d2f6SMatthew Dillon     crit_enter();
223bdfb4a94SSepherosa Ziehau 
22488c4d2f6SMatthew Dillon     if (info->flags & SYSTF_ONQUEUE) {
22588c4d2f6SMatthew Dillon 	TAILQ_REMOVE(info->queue, info, node);
22688c4d2f6SMatthew Dillon 	info->flags &= ~SYSTF_ONQUEUE;
22788c4d2f6SMatthew Dillon     }
228bdfb4a94SSepherosa Ziehau 
229bdfb4a94SSepherosa Ziehau     /*
230bdfb4a94SSepherosa Ziehau      * Deal with dispatch races by clearing the in-progress systimer
231bdfb4a94SSepherosa Ziehau      * pointer.  Only a direct pointer comparison can be used, the
232bdfb4a94SSepherosa Ziehau      * actual contents of the structure gd_systimer_inprog points to,
233bdfb4a94SSepherosa Ziehau      * if not equal to info, may be stale.
234bdfb4a94SSepherosa Ziehau      */
235bdfb4a94SSepherosa Ziehau     if (gd->gd_systimer_inprog == info)
236bdfb4a94SSepherosa Ziehau 	gd->gd_systimer_inprog = NULL;
237bdfb4a94SSepherosa Ziehau 
23888c4d2f6SMatthew Dillon     crit_exit();
23988c4d2f6SMatthew Dillon }
24088c4d2f6SMatthew Dillon 
24188c4d2f6SMatthew Dillon /*
242c6a766f4SMatthew Dillon  * systimer_init_periodic*()
24388c4d2f6SMatthew Dillon  *
24488c4d2f6SMatthew Dillon  *	Initialize a periodic timer at the specified frequency and add
24588c4d2f6SMatthew Dillon  *	it to the system.  The frequency is uncompensated and approximate.
24688c4d2f6SMatthew Dillon  *
247c6a766f4SMatthew Dillon  *	Try to synchronize multiple registrations of the same or similar
24888c4d2f6SMatthew Dillon  *	frequencies so the hardware interrupt is able to dispatch several
249c6a766f4SMatthew Dillon  *	together.  We do this by adjusting the phase of the initial timeout.
250c6a766f4SMatthew Dillon  *	This helps SMP.  Note that we are not attempting to synchronize to
25188c4d2f6SMatthew Dillon  *	the realtime clock.
252c6a766f4SMatthew Dillon  *
253c6a766f4SMatthew Dillon  *	This synchronization is also depended upon for statclock, hardclock,
254c6a766f4SMatthew Dillon  *	and schedclock.
25588c4d2f6SMatthew Dillon  */
256c6a766f4SMatthew Dillon static __inline
25788c4d2f6SMatthew Dillon void
_systimer_init_periodic(systimer_t info,systimer_func_t func,void * data,int64_t freq,int flags)258c6a766f4SMatthew Dillon _systimer_init_periodic(systimer_t info, systimer_func_t func, void *data,
259*8fbc264dSMatthew Dillon 			int64_t freq, int flags)
26088c4d2f6SMatthew Dillon {
26188c4d2f6SMatthew Dillon     sysclock_t base_count;
26288c4d2f6SMatthew Dillon 
263c6a766f4SMatthew Dillon     if (sys_cputimer->sync_base == 0)
264c6a766f4SMatthew Dillon 	sys_cputimer->sync_base = sys_cputimer->count();
265c6a766f4SMatthew Dillon 
26688c4d2f6SMatthew Dillon     bzero(info, sizeof(struct systimer));
267c6a766f4SMatthew Dillon 
268c6a766f4SMatthew Dillon     if ((flags & SYSTF_100KHZSYNC) && freq <= 100000)
269c6a766f4SMatthew Dillon 	    info->periodic = sys_cputimer->fromhz(100000) * (100000 / freq);
270c6a766f4SMatthew Dillon     if ((flags & SYSTF_MSSYNC) && freq <= 1000)
271c6a766f4SMatthew Dillon 	    info->periodic = sys_cputimer->fromhz(1000) * (1000 / freq);
272c6a766f4SMatthew Dillon     else
273c6a766f4SMatthew Dillon 	    info->periodic = sys_cputimer->fromhz(freq);
274c6a766f4SMatthew Dillon 
275044ee7c4SMatthew Dillon     base_count = sys_cputimer->count();
276c6a766f4SMatthew Dillon     base_count = base_count -
277c6a766f4SMatthew Dillon 		 (base_count - sys_cputimer->sync_base) % info->periodic;
27888c4d2f6SMatthew Dillon     info->time = base_count + info->periodic;
27991dc43ddSMatthew Dillon     if (flags & SYSTF_OFFSETCPU)
28091dc43ddSMatthew Dillon 	    info->time += mycpu->gd_cpuid * info->periodic / ncpus;
28191dc43ddSMatthew Dillon     if (flags & SYSTF_OFFSET50)
28291dc43ddSMatthew Dillon 	    info->time += info->periodic / 2;
28388c4d2f6SMatthew Dillon     info->func = func;
28488c4d2f6SMatthew Dillon     info->data = data;
285c6a766f4SMatthew Dillon     info->freq = freq;
2864b52d1afSMatthew Dillon     info->which = sys_cputimer;
28788c4d2f6SMatthew Dillon     info->gd = mycpu;
288c6a766f4SMatthew Dillon     info->flags |= flags;
28988c4d2f6SMatthew Dillon     systimer_add(info);
29088c4d2f6SMatthew Dillon }
29188c4d2f6SMatthew Dillon 
2920d1dffdfSMatthew Dillon void
systimer_init_periodic(systimer_t info,systimer_func_t func,void * data,int64_t freq)293c6a766f4SMatthew Dillon systimer_init_periodic(systimer_t info, systimer_func_t func, void *data,
294*8fbc264dSMatthew Dillon 		       int64_t freq)
295c6a766f4SMatthew Dillon {
296c6a766f4SMatthew Dillon 	_systimer_init_periodic(info, func, data, freq, 0);
297c6a766f4SMatthew Dillon }
298c6a766f4SMatthew Dillon 
299c6a766f4SMatthew Dillon void
systimer_init_periodic_nq(systimer_t info,systimer_func_t func,void * data,int64_t freq)30096d52ac8SSepherosa Ziehau systimer_init_periodic_nq(systimer_t info, systimer_func_t func, void *data,
301*8fbc264dSMatthew Dillon 			  int64_t freq)
3020d1dffdfSMatthew Dillon {
303c6a766f4SMatthew Dillon 	_systimer_init_periodic(info, func, data, freq, SYSTF_NONQUEUED);
3040d1dffdfSMatthew Dillon }
3050d1dffdfSMatthew Dillon 
30688c4d2f6SMatthew Dillon /*
307c6a766f4SMatthew Dillon  * These provide systimers whos periods are in perfect multiples of 1ms
308c6a766f4SMatthew Dillon  * or 0.1uS.  This is used in situations where the caller wants to gang
309c6a766f4SMatthew Dillon  * multiple systimers together whos periods may have some coincident events,
310c6a766f4SMatthew Dillon  * in order for those coincident events to generate only one interrupt.
311c6a766f4SMatthew Dillon  *
312c6a766f4SMatthew Dillon  * This also allows the caller to make event ordering assumptions for
313c6a766f4SMatthew Dillon  * said coincident events.
314c6a766f4SMatthew Dillon  */
315c6a766f4SMatthew Dillon void
systimer_init_periodic_nq1khz(systimer_t info,systimer_func_t func,void * data,int64_t freq)316c6a766f4SMatthew Dillon systimer_init_periodic_nq1khz(systimer_t info, systimer_func_t func,
317*8fbc264dSMatthew Dillon 			      void *data, int64_t freq)
318c6a766f4SMatthew Dillon {
319c6a766f4SMatthew Dillon 	_systimer_init_periodic(info, func, data, freq,
320c6a766f4SMatthew Dillon 				SYSTF_NONQUEUED | SYSTF_MSSYNC);
321c6a766f4SMatthew Dillon }
322c6a766f4SMatthew Dillon 
323c6a766f4SMatthew Dillon void
systimer_init_periodic_nq100khz(systimer_t info,systimer_func_t func,void * data,int64_t freq)324c6a766f4SMatthew Dillon systimer_init_periodic_nq100khz(systimer_t info, systimer_func_t func,
325*8fbc264dSMatthew Dillon 				void *data, int64_t freq)
326c6a766f4SMatthew Dillon {
327c6a766f4SMatthew Dillon 	_systimer_init_periodic(info, func, data, freq,
328c6a766f4SMatthew Dillon 				SYSTF_NONQUEUED | SYSTF_100KHZSYNC);
329c6a766f4SMatthew Dillon }
330c6a766f4SMatthew Dillon 
331c6a766f4SMatthew Dillon void
systimer_init_periodic_flags(systimer_t info,systimer_func_t func,void * data,int64_t freq,int flags)332c6a766f4SMatthew Dillon systimer_init_periodic_flags(systimer_t info, systimer_func_t func,
333*8fbc264dSMatthew Dillon 				void *data, int64_t freq, int flags)
334c6a766f4SMatthew Dillon {
335c6a766f4SMatthew Dillon 	_systimer_init_periodic(info, func, data, freq, flags);
336c6a766f4SMatthew Dillon }
337c6a766f4SMatthew Dillon 
338c6a766f4SMatthew Dillon 
339c6a766f4SMatthew Dillon /*
3404b52d1afSMatthew Dillon  * Adjust the periodic interval for a periodic timer which is already
3414b52d1afSMatthew Dillon  * running.  The current timeout is not effected.
3424b52d1afSMatthew Dillon  */
3434b52d1afSMatthew Dillon void
systimer_adjust_periodic(systimer_t info,int64_t freq)344*8fbc264dSMatthew Dillon systimer_adjust_periodic(systimer_t info, int64_t freq)
3454b52d1afSMatthew Dillon {
3464b52d1afSMatthew Dillon     crit_enter();
347c6a766f4SMatthew Dillon     info->periodic = sys_cputimer->fromhz(freq);
348c6a766f4SMatthew Dillon     info->freq = freq;
3494b52d1afSMatthew Dillon     info->which = sys_cputimer;
3504b52d1afSMatthew Dillon     crit_exit();
3514b52d1afSMatthew Dillon }
3524b52d1afSMatthew Dillon 
3534b52d1afSMatthew Dillon /*
35488c4d2f6SMatthew Dillon  * systimer_init_oneshot()
35588c4d2f6SMatthew Dillon  *
35688c4d2f6SMatthew Dillon  *	Initialize a periodic timer at the specified frequency and add
35788c4d2f6SMatthew Dillon  *	it to the system.  The frequency is uncompensated and approximate.
35888c4d2f6SMatthew Dillon  */
35988c4d2f6SMatthew Dillon void
systimer_init_oneshot(systimer_t info,systimer_func_t func,void * data,int64_t us)360*8fbc264dSMatthew Dillon systimer_init_oneshot(systimer_t info, systimer_func_t func,
361*8fbc264dSMatthew Dillon 		      void *data, int64_t us)
36288c4d2f6SMatthew Dillon {
36388c4d2f6SMatthew Dillon     bzero(info, sizeof(struct systimer));
364044ee7c4SMatthew Dillon     info->time = sys_cputimer->count() + sys_cputimer->fromus(us);
36588c4d2f6SMatthew Dillon     info->func = func;
36688c4d2f6SMatthew Dillon     info->data = data;
3674b52d1afSMatthew Dillon     info->which = sys_cputimer;
36888c4d2f6SMatthew Dillon     info->gd = mycpu;
369*8fbc264dSMatthew Dillon     info->us = us;
37088c4d2f6SMatthew Dillon     systimer_add(info);
37188c4d2f6SMatthew Dillon }
372*8fbc264dSMatthew Dillon 
373*8fbc264dSMatthew Dillon /*
374*8fbc264dSMatthew Dillon  * sys_cputimer was changed, recalculate all existing systimers and kick the
375*8fbc264dSMatthew Dillon  * new interrupt.
376*8fbc264dSMatthew Dillon  */
377*8fbc264dSMatthew Dillon static void
systimer_changed_pcpu(void * arg __unused)378*8fbc264dSMatthew Dillon systimer_changed_pcpu(void *arg __unused)
379*8fbc264dSMatthew Dillon {
380*8fbc264dSMatthew Dillon     globaldata_t gd = mycpu;
381*8fbc264dSMatthew Dillon     systimer_t info;
382*8fbc264dSMatthew Dillon 
383*8fbc264dSMatthew Dillon     crit_enter();
384*8fbc264dSMatthew Dillon again:
385*8fbc264dSMatthew Dillon     TAILQ_FOREACH(info, &gd->gd_systimerq, node) {
386*8fbc264dSMatthew Dillon 	if (info->which == sys_cputimer)
387*8fbc264dSMatthew Dillon 		continue;
388*8fbc264dSMatthew Dillon 	TAILQ_REMOVE(&gd->gd_systimerq, info, node);
389*8fbc264dSMatthew Dillon 	info->flags &= ~SYSTF_ONQUEUE;
390*8fbc264dSMatthew Dillon 	if (info->periodic) {
391*8fbc264dSMatthew Dillon 		_systimer_init_periodic(info, info->func, info->data,
392*8fbc264dSMatthew Dillon 					info->freq, info->flags);
393*8fbc264dSMatthew Dillon 	} else {
394*8fbc264dSMatthew Dillon 		info->time = sys_cputimer->count() +
395*8fbc264dSMatthew Dillon 			     sys_cputimer->fromus(info->us);
396*8fbc264dSMatthew Dillon 		systimer_add(info);
397*8fbc264dSMatthew Dillon 	}
398*8fbc264dSMatthew Dillon 	goto again;
399*8fbc264dSMatthew Dillon     }
400*8fbc264dSMatthew Dillon     cputimer_intr_reload(1);
401*8fbc264dSMatthew Dillon     crit_exit();
402*8fbc264dSMatthew Dillon }
403*8fbc264dSMatthew Dillon 
404*8fbc264dSMatthew Dillon void
systimer_changed(void)405*8fbc264dSMatthew Dillon systimer_changed(void)
406*8fbc264dSMatthew Dillon {
407*8fbc264dSMatthew Dillon     globaldata_t gd = mycpu;
408*8fbc264dSMatthew Dillon     int i;
409*8fbc264dSMatthew Dillon 
410*8fbc264dSMatthew Dillon     systimer_changed_pcpu(NULL);
411*8fbc264dSMatthew Dillon     for (i = 0; i < ncpus; ++i) {
412*8fbc264dSMatthew Dillon 	if (i != gd->gd_cpuid) {
413*8fbc264dSMatthew Dillon 		lwkt_send_ipiq(globaldata_find(i),
414*8fbc264dSMatthew Dillon 			       systimer_changed_pcpu, NULL);
415*8fbc264dSMatthew Dillon 	}
416*8fbc264dSMatthew Dillon     }
417*8fbc264dSMatthew Dillon }
418