Merge from vendor branch FILE:
[dragonfly.git] / sys / kern / kern_systimer.c
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  * $DragonFly: src/sys/kern/kern_systimer.c,v 1.8 2005/10/25 17:26:54 dillon Exp $
35  */
36
37 /*
38  * WARNING!  THE SYSTIMER MODULE DOES NOT OPERATE OR DISPATCH WITH THE
39  * MP LOCK HELD.  ALL CODE USING THIS MODULE MUST BE MP-SAFE.
40  *
41  * This code implements a fine-grained per-cpu system timer which is
42  * ultimately based on a hardware timer.  The hardware timer abstraction
43  * is sufficiently disconnected from this code to support both per-cpu
44  * hardware timers or a single system-wide hardware timer.
45  *
46  * Notes on machine-dependant code (in arch/arch/systimer.c)
47  *
48  * cputimer_intr_reload()       Reload the one-shot (per-cpu basis)
49  */
50
51 #include <sys/param.h>
52 #include <sys/kernel.h>
53 #include <sys/systm.h>
54 #include <sys/thread.h>
55 #include <sys/globaldata.h>
56 #include <sys/systimer.h>
57 #include <sys/thread2.h>
58
59 /*
60  * Execute ready systimers.  Called directly from the platform-specific
61  * one-shot timer clock interrupt (e.g. clkintr()) or via an IPI.  May
62  * be called simultaniously on multiple cpus and always operations on 
63  * the current cpu's queue.  Systimer functions are responsible for calling
64  * hardclock, statclock, and other finely-timed routines.
65  */
66 void
67 systimer_intr(sysclock_t *timep, int dummy, struct intrframe *frame)
68 {
69     globaldata_t gd = mycpu;
70     sysclock_t time = *timep;
71     systimer_t info;
72
73     if (gd->gd_syst_nest)
74         return;
75
76     crit_enter();
77     ++gd->gd_syst_nest;
78     while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
79         /*
80          * If we haven't reached the requested time, tell the cputimer
81          * how much is left and break out.
82          */
83         if ((int)(info->time - time) > 0) {
84             cputimer_intr_reload(info->time - time);
85             break;
86         }
87
88         /*
89          * Dequeue and execute
90          */
91         info->flags &= ~SYSTF_ONQUEUE;
92         TAILQ_REMOVE(info->queue, info, node);
93         crit_exit();
94         info->func(info, frame);
95         crit_enter();
96
97         /*
98          * Reinstall if periodic.  If this is a non-queued periodic
99          * interrupt do not allow multiple events to build up (used
100          * for things like the callout timer to prevent premature timeouts
101          * due to long interrupt disablements, BIOS 8254 glitching, and so
102          * forth).  However, we still want to keep things synchronized between
103          * cpus for efficient handling of the timer interrupt so jump in
104          * multiples of the periodic rate.
105          */
106         if (info->periodic) {
107             info->time += info->periodic;
108             if ((info->flags & SYSTF_NONQUEUED) &&
109                 (int)(info->time - time) <= 0
110             ) {
111                 info->time += ((time - info->time + info->periodic - 1) / 
112                                 info->periodic) * info->periodic;
113             }
114             systimer_add(info);
115         }
116     }
117     --gd->gd_syst_nest;
118     crit_exit();
119 }
120
121 void
122 systimer_add(systimer_t info)
123 {
124     struct globaldata *gd = mycpu;
125
126     KKASSERT((info->flags & (SYSTF_ONQUEUE|SYSTF_IPIRUNNING)) == 0);
127     crit_enter();
128     if (info->gd == gd) {
129         systimer_t scan1;
130         systimer_t scan2;
131         scan1 = TAILQ_FIRST(&gd->gd_systimerq);
132         if (scan1 == NULL || (int)(scan1->time - info->time) > 0) {
133             cputimer_intr_reload(info->time - sys_cputimer->count());
134             TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
135         } else {
136             scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
137             for (;;) {
138                 if (scan1 == NULL) {
139                     TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
140                     break;
141                 }
142                 if ((int)(scan1->time - info->time) > 0) {
143                     TAILQ_INSERT_BEFORE(scan1, info, node);
144                     break;
145                 }
146                 if ((int)(scan2->time - info->time) <= 0) {
147                     TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node);
148                     break;
149                 }
150                 scan1 = TAILQ_NEXT(scan1, node);
151                 scan2 = TAILQ_PREV(scan2, systimerq, node);
152             }
153         }
154         info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
155         info->queue = &gd->gd_systimerq;
156     } else {
157 #ifdef SMP
158         info->flags |= SYSTF_IPIRUNNING;
159         lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info);
160 #else
161         panic("systimer_add: bad gd in info %p", info);
162 #endif
163     }
164     crit_exit();
165 }
166
167 /*
168  * systimer_del()
169  *
170  *      Delete a system timer.  Only the owning cpu can delete a timer.
171  */
172 void
173 systimer_del(systimer_t info)
174 {
175     KKASSERT(info->gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
176     crit_enter();
177     if (info->flags & SYSTF_ONQUEUE) {
178         TAILQ_REMOVE(info->queue, info, node);
179         info->flags &= ~SYSTF_ONQUEUE;
180     }
181     crit_exit();
182 }
183
184 /*
185  * systimer_init_periodic()
186  *
187  *      Initialize a periodic timer at the specified frequency and add
188  *      it to the system.  The frequency is uncompensated and approximate.
189  *
190  *      Try to synchronize multi registrations of the same or similar
191  *      frequencies so the hardware interrupt is able to dispatch several
192  *      at together by adjusting the phase of the initial interrupt.  This
193  *      helps SMP.  Note that we are not attempting to synchronize to 
194  *      the realtime clock.
195  */
196 void
197 systimer_init_periodic(systimer_t info, void *func, void *data, int hz)
198 {
199     sysclock_t base_count;
200
201     bzero(info, sizeof(struct systimer));
202     info->periodic = sys_cputimer->fromhz(hz);
203     base_count = sys_cputimer->count();
204     base_count = base_count - (base_count % info->periodic);
205     info->time = base_count + info->periodic;
206     info->func = func;
207     info->data = data;
208     info->gd = mycpu;
209     systimer_add(info);
210 }
211
212 void
213 systimer_init_periodic_nq(systimer_t info, void *func, void *data, int hz)
214 {
215     sysclock_t base_count;
216
217     bzero(info, sizeof(struct systimer));
218     info->periodic = sys_cputimer->fromhz(hz);
219     base_count = sys_cputimer->count();
220     base_count = base_count - (base_count % info->periodic);
221     info->time = base_count + info->periodic;
222     info->func = func;
223     info->data = data;
224     info->gd = mycpu;
225     info->flags |= SYSTF_NONQUEUED;
226     systimer_add(info);
227 }
228
229 /*
230  * systimer_init_oneshot()
231  *
232  *      Initialize a periodic timer at the specified frequency and add
233  *      it to the system.  The frequency is uncompensated and approximate.
234  */
235 void
236 systimer_init_oneshot(systimer_t info, void *func, void *data, int us)
237 {
238     bzero(info, sizeof(struct systimer));
239     info->time = sys_cputimer->count() + sys_cputimer->fromus(us);
240     info->func = func;
241     info->data = data;
242     info->gd = mycpu;
243     systimer_add(info);
244 }
245