xref: /dragonfly/sys/kern/kern_systimer.c (revision 8fbc264d)
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * WARNING!  THE SYSTIMER MODULE DOES NOT OPERATE OR DISPATCH WITH THE
37  * MP LOCK HELD.  ALL CODE USING THIS MODULE MUST BE MP-SAFE.
38  *
39  * This code implements a fine-grained per-cpu system timer which is
40  * ultimately based on a hardware timer.  The hardware timer abstraction
41  * is sufficiently disconnected from this code to support both per-cpu
42  * hardware timers or a single system-wide hardware timer.
43  *
44  * WARNING!  During early boot if a new system timer is selected, existing
45  * timeouts will not be effected and will thus occur slower or faster.
46  * periodic timers will be adjusted at the next periodic load.
47  *
48  * Notes on machine-dependant code (in arch/arch/systimer.c)
49  *
50  * cputimer_intr_reload()	Reload the one-shot (per-cpu basis)
51  */
52 
53 #include <sys/param.h>
54 #include <sys/kernel.h>
55 #include <sys/systm.h>
56 #include <sys/thread.h>
57 #include <sys/globaldata.h>
58 #include <sys/systimer.h>
59 #include <sys/thread2.h>
60 
61 /*
62  * Execute ready systimers.  Called directly from the platform-specific
63  * one-shot timer clock interrupt (e.g. clkintr()) or via an IPI.  May
64  * be called simultaniously on multiple cpus and always operations on
65  * the current cpu's queue.  Systimer functions are responsible for calling
66  * hardclock, statclock, and other finely-timed routines.
67  */
68 void
systimer_intr(sysclock_t * timep,int in_ipi,struct intrframe * frame)69 systimer_intr(sysclock_t *timep, int in_ipi, struct intrframe *frame)
70 {
71     globaldata_t gd = mycpu;
72     sysclock_t time = *timep;
73     systimer_t info;
74 
75     if (gd->gd_syst_nest)
76 	return;
77 
78     crit_enter();
79     ++gd->gd_syst_nest;
80     while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
81 	/*
82 	 * If we haven't reached the requested time, tell the cputimer
83 	 * how much is left and break out.
84 	 */
85 	if ((ssysclock_t)(info->time - time) > 0) {
86 	    cputimer_intr_reload(info->time - time);
87 	    break;
88 	}
89 
90 	/*
91 	 * Dequeue and execute, detect a loss of the systimer.  Note
92 	 * that the in-progress systimer pointer can only be used to
93 	 * detect a loss of the systimer, it is only useful within
94 	 * this code sequence and becomes stale otherwise.
95 	 */
96 	info->flags &= ~SYSTF_ONQUEUE;
97 	TAILQ_REMOVE(info->queue, info, node);
98 	gd->gd_systimer_inprog = info;
99 	crit_exit();
100 	info->func(info, in_ipi, frame);
101 	crit_enter();
102 
103 	/*
104 	 * The caller may deleted or even re-queue the systimer itself
105 	 * with a delete/add sequence.  If the caller does not mess with
106 	 * the systimer we will requeue the periodic interval automatically.
107 	 *
108 	 * If this is a non-queued periodic interrupt, do not allow multiple
109 	 * events to build up (used for things like the callout timer to
110 	 * prevent premature timeouts due to long interrupt disablements,
111 	 * BIOS 8254 glitching, and so forth).  However, we still want to
112 	 * keep things synchronized between cpus for efficient handling of
113 	 * the timer interrupt so jump in multiples of the periodic rate.
114 	 */
115 	if (gd->gd_systimer_inprog == info && info->periodic) {
116 	    if (info->which != sys_cputimer) {
117 		info->periodic = sys_cputimer->fromhz(info->freq);
118 		info->which = sys_cputimer;
119 	    }
120 	    info->time += info->periodic;
121 	    if ((info->flags & SYSTF_NONQUEUED) &&
122 		(ssysclock_t)(info->time - time) <= 0
123 	    ) {
124 		info->time += roundup(time - info->time, info->periodic);
125 	    }
126 	    systimer_add(info);
127 	}
128 	gd->gd_systimer_inprog = NULL;
129     }
130     --gd->gd_syst_nest;
131     crit_exit();
132 }
133 
134 void
systimer_intr_enable(void)135 systimer_intr_enable(void)
136 {
137     cputimer_intr_enable();
138 }
139 
140 /*
141  * MPSAFE
142  */
143 void
systimer_add(systimer_t info)144 systimer_add(systimer_t info)
145 {
146     struct globaldata *gd = mycpu;
147 
148     KKASSERT((info->flags & SYSTF_ONQUEUE) == 0);
149     crit_enter();
150     if (info->gd == gd) {
151 	systimer_t scan1;
152 	systimer_t scan2;
153 	scan1 = TAILQ_FIRST(&gd->gd_systimerq);
154 	if (scan1 == NULL || (ssysclock_t)(scan1->time - info->time) > 0) {
155 	    cputimer_intr_reload(info->time - sys_cputimer->count());
156 	    TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
157 	} else {
158 	    scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
159 	    for (;;) {
160 		if (scan1 == NULL) {
161 		    TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
162 		    break;
163 		}
164 		if (info->flags & SYSTF_FIRST) {
165 			/*
166 			 * When coincident events occur, the event being
167 			 * added wants to be placed before the others.
168 			 */
169 			if ((ssysclock_t)(scan1->time - info->time) >= 0) {
170 			    TAILQ_INSERT_BEFORE(scan1, info, node);
171 			    break;
172 			}
173 			if ((ssysclock_t)(scan2->time - info->time) < 0) {
174 			    TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2,
175 					       info, node);
176 			    break;
177 			}
178 		} else {
179 			/*
180 			 * When coincident events occur, the event being
181 			 * added should be placed after the others.  This
182 			 * is the default.
183 			 */
184 			if ((ssysclock_t)(scan1->time - info->time) > 0) {
185 			    TAILQ_INSERT_BEFORE(scan1, info, node);
186 			    break;
187 			}
188 			if ((ssysclock_t)(scan2->time - info->time) <= 0) {
189 			    TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2,
190 					        info, node);
191 			    break;
192 			}
193 		}
194 		scan1 = TAILQ_NEXT(scan1, node);
195 		scan2 = TAILQ_PREV(scan2, systimerq, node);
196 	    }
197 	}
198 	info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
199 	info->queue = &gd->gd_systimerq;
200     } else {
201 	KKASSERT((info->flags & SYSTF_IPIRUNNING) == 0);
202 	info->flags |= SYSTF_IPIRUNNING;
203 	lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info);
204     }
205     crit_exit();
206 }
207 
208 /*
209  * systimer_del()
210  *
211  *	Delete a system timer.  Only the owning cpu can delete a timer.
212  *
213  * MPSAFE
214  */
215 void
systimer_del(systimer_t info)216 systimer_del(systimer_t info)
217 {
218     struct globaldata *gd = info->gd;
219 
220     KKASSERT(gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
221 
222     crit_enter();
223 
224     if (info->flags & SYSTF_ONQUEUE) {
225 	TAILQ_REMOVE(info->queue, info, node);
226 	info->flags &= ~SYSTF_ONQUEUE;
227     }
228 
229     /*
230      * Deal with dispatch races by clearing the in-progress systimer
231      * pointer.  Only a direct pointer comparison can be used, the
232      * actual contents of the structure gd_systimer_inprog points to,
233      * if not equal to info, may be stale.
234      */
235     if (gd->gd_systimer_inprog == info)
236 	gd->gd_systimer_inprog = NULL;
237 
238     crit_exit();
239 }
240 
241 /*
242  * systimer_init_periodic*()
243  *
244  *	Initialize a periodic timer at the specified frequency and add
245  *	it to the system.  The frequency is uncompensated and approximate.
246  *
247  *	Try to synchronize multiple registrations of the same or similar
248  *	frequencies so the hardware interrupt is able to dispatch several
249  *	together.  We do this by adjusting the phase of the initial timeout.
250  *	This helps SMP.  Note that we are not attempting to synchronize to
251  *	the realtime clock.
252  *
253  *	This synchronization is also depended upon for statclock, hardclock,
254  *	and schedclock.
255  */
256 static __inline
257 void
_systimer_init_periodic(systimer_t info,systimer_func_t func,void * data,int64_t freq,int flags)258 _systimer_init_periodic(systimer_t info, systimer_func_t func, void *data,
259 			int64_t freq, int flags)
260 {
261     sysclock_t base_count;
262 
263     if (sys_cputimer->sync_base == 0)
264 	sys_cputimer->sync_base = sys_cputimer->count();
265 
266     bzero(info, sizeof(struct systimer));
267 
268     if ((flags & SYSTF_100KHZSYNC) && freq <= 100000)
269 	    info->periodic = sys_cputimer->fromhz(100000) * (100000 / freq);
270     if ((flags & SYSTF_MSSYNC) && freq <= 1000)
271 	    info->periodic = sys_cputimer->fromhz(1000) * (1000 / freq);
272     else
273 	    info->periodic = sys_cputimer->fromhz(freq);
274 
275     base_count = sys_cputimer->count();
276     base_count = base_count -
277 		 (base_count - sys_cputimer->sync_base) % info->periodic;
278     info->time = base_count + info->periodic;
279     if (flags & SYSTF_OFFSETCPU)
280 	    info->time += mycpu->gd_cpuid * info->periodic / ncpus;
281     if (flags & SYSTF_OFFSET50)
282 	    info->time += info->periodic / 2;
283     info->func = func;
284     info->data = data;
285     info->freq = freq;
286     info->which = sys_cputimer;
287     info->gd = mycpu;
288     info->flags |= flags;
289     systimer_add(info);
290 }
291 
292 void
systimer_init_periodic(systimer_t info,systimer_func_t func,void * data,int64_t freq)293 systimer_init_periodic(systimer_t info, systimer_func_t func, void *data,
294 		       int64_t freq)
295 {
296 	_systimer_init_periodic(info, func, data, freq, 0);
297 }
298 
299 void
systimer_init_periodic_nq(systimer_t info,systimer_func_t func,void * data,int64_t freq)300 systimer_init_periodic_nq(systimer_t info, systimer_func_t func, void *data,
301 			  int64_t freq)
302 {
303 	_systimer_init_periodic(info, func, data, freq, SYSTF_NONQUEUED);
304 }
305 
306 /*
307  * These provide systimers whos periods are in perfect multiples of 1ms
308  * or 0.1uS.  This is used in situations where the caller wants to gang
309  * multiple systimers together whos periods may have some coincident events,
310  * in order for those coincident events to generate only one interrupt.
311  *
312  * This also allows the caller to make event ordering assumptions for
313  * said coincident events.
314  */
315 void
systimer_init_periodic_nq1khz(systimer_t info,systimer_func_t func,void * data,int64_t freq)316 systimer_init_periodic_nq1khz(systimer_t info, systimer_func_t func,
317 			      void *data, int64_t freq)
318 {
319 	_systimer_init_periodic(info, func, data, freq,
320 				SYSTF_NONQUEUED | SYSTF_MSSYNC);
321 }
322 
323 void
systimer_init_periodic_nq100khz(systimer_t info,systimer_func_t func,void * data,int64_t freq)324 systimer_init_periodic_nq100khz(systimer_t info, systimer_func_t func,
325 				void *data, int64_t freq)
326 {
327 	_systimer_init_periodic(info, func, data, freq,
328 				SYSTF_NONQUEUED | SYSTF_100KHZSYNC);
329 }
330 
331 void
systimer_init_periodic_flags(systimer_t info,systimer_func_t func,void * data,int64_t freq,int flags)332 systimer_init_periodic_flags(systimer_t info, systimer_func_t func,
333 				void *data, int64_t freq, int flags)
334 {
335 	_systimer_init_periodic(info, func, data, freq, flags);
336 }
337 
338 
339 /*
340  * Adjust the periodic interval for a periodic timer which is already
341  * running.  The current timeout is not effected.
342  */
343 void
systimer_adjust_periodic(systimer_t info,int64_t freq)344 systimer_adjust_periodic(systimer_t info, int64_t freq)
345 {
346     crit_enter();
347     info->periodic = sys_cputimer->fromhz(freq);
348     info->freq = freq;
349     info->which = sys_cputimer;
350     crit_exit();
351 }
352 
353 /*
354  * systimer_init_oneshot()
355  *
356  *	Initialize a periodic timer at the specified frequency and add
357  *	it to the system.  The frequency is uncompensated and approximate.
358  */
359 void
systimer_init_oneshot(systimer_t info,systimer_func_t func,void * data,int64_t us)360 systimer_init_oneshot(systimer_t info, systimer_func_t func,
361 		      void *data, int64_t us)
362 {
363     bzero(info, sizeof(struct systimer));
364     info->time = sys_cputimer->count() + sys_cputimer->fromus(us);
365     info->func = func;
366     info->data = data;
367     info->which = sys_cputimer;
368     info->gd = mycpu;
369     info->us = us;
370     systimer_add(info);
371 }
372 
373 /*
374  * sys_cputimer was changed, recalculate all existing systimers and kick the
375  * new interrupt.
376  */
377 static void
systimer_changed_pcpu(void * arg __unused)378 systimer_changed_pcpu(void *arg __unused)
379 {
380     globaldata_t gd = mycpu;
381     systimer_t info;
382 
383     crit_enter();
384 again:
385     TAILQ_FOREACH(info, &gd->gd_systimerq, node) {
386 	if (info->which == sys_cputimer)
387 		continue;
388 	TAILQ_REMOVE(&gd->gd_systimerq, info, node);
389 	info->flags &= ~SYSTF_ONQUEUE;
390 	if (info->periodic) {
391 		_systimer_init_periodic(info, info->func, info->data,
392 					info->freq, info->flags);
393 	} else {
394 		info->time = sys_cputimer->count() +
395 			     sys_cputimer->fromus(info->us);
396 		systimer_add(info);
397 	}
398 	goto again;
399     }
400     cputimer_intr_reload(1);
401     crit_exit();
402 }
403 
404 void
systimer_changed(void)405 systimer_changed(void)
406 {
407     globaldata_t gd = mycpu;
408     int i;
409 
410     systimer_changed_pcpu(NULL);
411     for (i = 0; i < ncpus; ++i) {
412 	if (i != gd->gd_cpuid) {
413 		lwkt_send_ipiq(globaldata_find(i),
414 			       systimer_changed_pcpu, NULL);
415 	}
416     }
417 }
418