xref: /dflybsd-src/sys/kern/kern_systimer.c (revision 45de427dfd4e754c9dbd161d9e5638d5a97962bf)
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 extern void	pcpu_timer_process(void);
62 extern void	pcpu_timer_process_frame(struct intrframe *);
63 
64 /*
65  * Execute ready systimers.  Called directly from the platform-specific
66  * one-shot timer clock interrupt (e.g. clkintr()) or via an IPI.  May
67  * be called simultaniously on multiple cpus and always operations on
68  * the current cpu's queue.  Systimer functions are responsible for calling
69  * hardclock, statclock, and other finely-timed routines.
70  */
71 void
72 systimer_intr(sysclock_t *timep, int in_ipi, struct intrframe *frame)
73 {
74     globaldata_t gd = mycpu;
75     sysclock_t time = *timep;
76     systimer_t info;
77 
78     if (gd->gd_syst_nest)
79 	return;
80 
81     crit_enter();
82     ++gd->gd_syst_nest;
83     while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
84 	/*
85 	 * If we haven't reached the requested time, tell the cputimer
86 	 * how much is left and break out.
87 	 */
88 	if ((int)(info->time - time) > 0) {
89 	    cputimer_intr_reload(info->time - time);
90 	    break;
91 	}
92 
93 	/*
94 	 * Dequeue and execute, detect a loss of the systimer.  Note
95 	 * that the in-progress systimer pointer can only be used to
96 	 * detect a loss of the systimer, it is only useful within
97 	 * this code sequence and becomes stale otherwise.
98 	 */
99 	info->flags &= ~SYSTF_ONQUEUE;
100 	TAILQ_REMOVE(info->queue, info, node);
101 	gd->gd_systimer_inprog = info;
102 	crit_exit();
103 	info->func(info, in_ipi, frame);
104 	crit_enter();
105 
106 	/*
107 	 * The caller may deleted or even re-queue the systimer itself
108 	 * with a delete/add sequence.  If the caller does not mess with
109 	 * the systimer we will requeue the periodic interval automatically.
110 	 *
111 	 * If this is a non-queued periodic interrupt, do not allow multiple
112 	 * events to build up (used for things like the callout timer to
113 	 * prevent premature timeouts due to long interrupt disablements,
114 	 * BIOS 8254 glitching, and so forth).  However, we still want to
115 	 * keep things synchronized between cpus for efficient handling of
116 	 * the timer interrupt so jump in multiples of the periodic rate.
117 	 */
118 	if (gd->gd_systimer_inprog == info && info->periodic) {
119 	    if (info->which != sys_cputimer) {
120 		info->periodic = sys_cputimer->fromhz(info->freq);
121 		info->which = sys_cputimer;
122 	    }
123 	    info->time += info->periodic;
124 	    if ((info->flags & SYSTF_NONQUEUED) &&
125 		(int)(info->time - time) <= 0
126 	    ) {
127 		info->time += roundup(time - info->time, info->periodic);
128 	    }
129 	    systimer_add(info);
130 	}
131 	gd->gd_systimer_inprog = NULL;
132     }
133     --gd->gd_syst_nest;
134     crit_exit();
135 }
136 
137 void
138 systimer_intr_enable(void)
139 {
140     cputimer_intr_enable();
141 }
142 
143 /*
144  * MPSAFE
145  */
146 void
147 systimer_add(systimer_t info)
148 {
149     struct globaldata *gd = mycpu;
150 
151     KKASSERT((info->flags & SYSTF_ONQUEUE) == 0);
152     crit_enter();
153     if (info->gd == gd) {
154 	systimer_t scan1;
155 	systimer_t scan2;
156 	scan1 = TAILQ_FIRST(&gd->gd_systimerq);
157 	if (scan1 == NULL || (int)(scan1->time - info->time) > 0) {
158 	    cputimer_intr_reload(info->time - sys_cputimer->count());
159 	    TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
160 	} else {
161 	    scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
162 	    for (;;) {
163 		if (scan1 == NULL) {
164 		    TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
165 		    break;
166 		}
167 		if ((int)(scan1->time - info->time) > 0) {
168 		    TAILQ_INSERT_BEFORE(scan1, info, node);
169 		    break;
170 		}
171 		if ((int)(scan2->time - info->time) <= 0) {
172 		    TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node);
173 		    break;
174 		}
175 		scan1 = TAILQ_NEXT(scan1, node);
176 		scan2 = TAILQ_PREV(scan2, systimerq, node);
177 	    }
178 	}
179 	info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
180 	info->queue = &gd->gd_systimerq;
181     } else {
182 	KKASSERT((info->flags & SYSTF_IPIRUNNING) == 0);
183 	info->flags |= SYSTF_IPIRUNNING;
184 	lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info);
185     }
186     crit_exit();
187 }
188 
189 /*
190  * systimer_del()
191  *
192  *	Delete a system timer.  Only the owning cpu can delete a timer.
193  *
194  * MPSAFE
195  */
196 void
197 systimer_del(systimer_t info)
198 {
199     struct globaldata *gd = info->gd;
200 
201     KKASSERT(gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
202 
203     crit_enter();
204 
205     if (info->flags & SYSTF_ONQUEUE) {
206 	TAILQ_REMOVE(info->queue, info, node);
207 	info->flags &= ~SYSTF_ONQUEUE;
208     }
209 
210     /*
211      * Deal with dispatch races by clearing the in-progress systimer
212      * pointer.  Only a direct pointer comparison can be used, the
213      * actual contents of the structure gd_systimer_inprog points to,
214      * if not equal to info, may be stale.
215      */
216     if (gd->gd_systimer_inprog == info)
217 	gd->gd_systimer_inprog = NULL;
218 
219     crit_exit();
220 }
221 
222 /*
223  * systimer_init_periodic()
224  *
225  *	Initialize a periodic timer at the specified frequency and add
226  *	it to the system.  The frequency is uncompensated and approximate.
227  *
228  *	Try to synchronize multi registrations of the same or similar
229  *	frequencies so the hardware interrupt is able to dispatch several
230  *	at together by adjusting the phase of the initial interrupt.  This
231  *	helps SMP.  Note that we are not attempting to synchronize to
232  *	the realtime clock.
233  */
234 void
235 systimer_init_periodic(systimer_t info, systimer_func_t func, void *data,
236     int hz)
237 {
238     sysclock_t base_count;
239 
240     bzero(info, sizeof(struct systimer));
241     info->periodic = sys_cputimer->fromhz(hz);
242     base_count = sys_cputimer->count();
243     base_count = base_count - (base_count % info->periodic);
244     info->time = base_count + info->periodic;
245     info->func = func;
246     info->data = data;
247     info->freq = hz;
248     info->which = sys_cputimer;
249     info->gd = mycpu;
250     systimer_add(info);
251 }
252 
253 void
254 systimer_init_periodic_nq(systimer_t info, systimer_func_t func, void *data,
255     int hz)
256 {
257     sysclock_t base_count;
258 
259     bzero(info, sizeof(struct systimer));
260     info->periodic = sys_cputimer->fromhz(hz);
261     base_count = sys_cputimer->count();
262     base_count = base_count - (base_count % info->periodic);
263     info->time = base_count + info->periodic;
264     info->func = func;
265     info->data = data;
266     info->freq = hz;
267     info->which = sys_cputimer;
268     info->gd = mycpu;
269     info->flags |= SYSTF_NONQUEUED;
270     systimer_add(info);
271 }
272 
273 /*
274  * Adjust the periodic interval for a periodic timer which is already
275  * running.  The current timeout is not effected.
276  */
277 void
278 systimer_adjust_periodic(systimer_t info, int hz)
279 {
280     crit_enter();
281     info->periodic = sys_cputimer->fromhz(hz);
282     info->freq = hz;
283     info->which = sys_cputimer;
284     crit_exit();
285 }
286 
287 /*
288  * systimer_init_oneshot()
289  *
290  *	Initialize a periodic timer at the specified frequency and add
291  *	it to the system.  The frequency is uncompensated and approximate.
292  */
293 void
294 systimer_init_oneshot(systimer_t info, systimer_func_t func, void *data, int us)
295 {
296     bzero(info, sizeof(struct systimer));
297     info->time = sys_cputimer->count() + sys_cputimer->fromus(us);
298     info->func = func;
299     info->data = data;
300     info->which = sys_cputimer;
301     info->gd = mycpu;
302     systimer_add(info);
303 }
304 
305 static void
306 pcpu_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
307 {
308 	sysclock_t count;
309 
310 	gd->gd_timer_running = 0;
311 
312 	count = sys_cputimer->count();
313 	if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
314 		systimer_intr(&count, 0, frame);
315 }
316 
317 void
318 pcpu_timer_process(void)
319 {
320 	pcpu_timer_process_oncpu(mycpu, NULL);
321 }
322 
323 void
324 pcpu_timer_process_frame(struct intrframe *frame)
325 {
326 	pcpu_timer_process_oncpu(mycpu, frame);
327 }
328