kernel: Make SMP support default (and non-optional).
[dragonfly.git] / sys / kern / kern_systimer.c
CommitLineData
88c4d2f6 1/*
8c10bfcf
MD
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 *
88c4d2f6
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
8c10bfcf 10 *
88c4d2f6
MD
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
8c10bfcf
MD
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
88c4d2f6 32 * SUCH DAMAGE.
88c4d2f6
MD
33 */
34
35/*
7358cf5b
MD
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 *
88c4d2f6
MD
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 *
4b52d1af
MD
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 *
88c4d2f6
MD
48 * Notes on machine-dependant code (in arch/arch/systimer.c)
49 *
50 * cputimer_intr_reload() Reload the one-shot (per-cpu basis)
88c4d2f6
MD
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
774fc796
MD
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.
88c4d2f6
MD
67 */
68void
96d52ac8 69systimer_intr(sysclock_t *timep, int in_ipi, struct intrframe *frame)
88c4d2f6
MD
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 ((int)(info->time - time) > 0) {
86 cputimer_intr_reload(info->time - time);
87 break;
88 }
89
90 /*
bdfb4a94
SZ
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.
88c4d2f6
MD
95 */
96 info->flags &= ~SYSTF_ONQUEUE;
97 TAILQ_REMOVE(info->queue, info, node);
bdfb4a94 98 gd->gd_systimer_inprog = info;
88c4d2f6 99 crit_exit();
96d52ac8 100 info->func(info, in_ipi, frame);
88c4d2f6
MD
101 crit_enter();
102
103 /*
bdfb4a94
SZ
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.
88c4d2f6 114 */
bdfb4a94 115 if (gd->gd_systimer_inprog == info && info->periodic) {
4b52d1af
MD
116 if (info->which != sys_cputimer) {
117 info->periodic = sys_cputimer->fromhz(info->freq);
118 info->which = sys_cputimer;
4b52d1af 119 }
88c4d2f6 120 info->time += info->periodic;
0d1dffdf
MD
121 if ((info->flags & SYSTF_NONQUEUED) &&
122 (int)(info->time - time) <= 0
123 ) {
124 info->time += ((time - info->time + info->periodic - 1) /
125 info->periodic) * info->periodic;
126 }
88c4d2f6
MD
127 systimer_add(info);
128 }
bdfb4a94 129 gd->gd_systimer_inprog = NULL;
88c4d2f6 130 }
88c4d2f6
MD
131 --gd->gd_syst_nest;
132 crit_exit();
133}
134
43adde98
SZ
135void
136systimer_intr_enable(void)
137{
138 cputimer_intr_enable();
139}
140
3919ced0
MD
141/*
142 * MPSAFE
143 */
88c4d2f6
MD
144void
145systimer_add(systimer_t info)
146{
147 struct globaldata *gd = mycpu;
148
f9e5a194 149 KKASSERT((info->flags & SYSTF_ONQUEUE) == 0);
88c4d2f6
MD
150 crit_enter();
151 if (info->gd == gd) {
152 systimer_t scan1;
153 systimer_t scan2;
154 scan1 = TAILQ_FIRST(&gd->gd_systimerq);
155 if (scan1 == NULL || (int)(scan1->time - info->time) > 0) {
044ee7c4 156 cputimer_intr_reload(info->time - sys_cputimer->count());
88c4d2f6
MD
157 TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
158 } else {
159 scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
160 for (;;) {
161 if (scan1 == NULL) {
162 TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
163 break;
164 }
165 if ((int)(scan1->time - info->time) > 0) {
166 TAILQ_INSERT_BEFORE(scan1, info, node);
167 break;
168 }
169 if ((int)(scan2->time - info->time) <= 0) {
170 TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node);
171 break;
172 }
173 scan1 = TAILQ_NEXT(scan1, node);
174 scan2 = TAILQ_PREV(scan2, systimerq, node);
175 }
176 }
177 info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
178 info->queue = &gd->gd_systimerq;
179 } else {
f9e5a194 180 KKASSERT((info->flags & SYSTF_IPIRUNNING) == 0);
88c4d2f6 181 info->flags |= SYSTF_IPIRUNNING;
b8a98473 182 lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info);
88c4d2f6
MD
183 }
184 crit_exit();
185}
186
187/*
188 * systimer_del()
189 *
190 * Delete a system timer. Only the owning cpu can delete a timer.
3919ced0
MD
191 *
192 * MPSAFE
88c4d2f6
MD
193 */
194void
195systimer_del(systimer_t info)
196{
bdfb4a94
SZ
197 struct globaldata *gd = info->gd;
198
199 KKASSERT(gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
200
88c4d2f6 201 crit_enter();
bdfb4a94 202
88c4d2f6
MD
203 if (info->flags & SYSTF_ONQUEUE) {
204 TAILQ_REMOVE(info->queue, info, node);
205 info->flags &= ~SYSTF_ONQUEUE;
206 }
bdfb4a94
SZ
207
208 /*
209 * Deal with dispatch races by clearing the in-progress systimer
210 * pointer. Only a direct pointer comparison can be used, the
211 * actual contents of the structure gd_systimer_inprog points to,
212 * if not equal to info, may be stale.
213 */
214 if (gd->gd_systimer_inprog == info)
215 gd->gd_systimer_inprog = NULL;
216
88c4d2f6
MD
217 crit_exit();
218}
219
220/*
221 * systimer_init_periodic()
222 *
223 * Initialize a periodic timer at the specified frequency and add
224 * it to the system. The frequency is uncompensated and approximate.
225 *
226 * Try to synchronize multi registrations of the same or similar
227 * frequencies so the hardware interrupt is able to dispatch several
228 * at together by adjusting the phase of the initial interrupt. This
229 * helps SMP. Note that we are not attempting to synchronize to
230 * the realtime clock.
231 */
232void
96d52ac8
SZ
233systimer_init_periodic(systimer_t info, systimer_func_t func, void *data,
234 int hz)
88c4d2f6
MD
235{
236 sysclock_t base_count;
237
238 bzero(info, sizeof(struct systimer));
044ee7c4
MD
239 info->periodic = sys_cputimer->fromhz(hz);
240 base_count = sys_cputimer->count();
88c4d2f6
MD
241 base_count = base_count - (base_count % info->periodic);
242 info->time = base_count + info->periodic;
243 info->func = func;
244 info->data = data;
4b52d1af
MD
245 info->freq = hz;
246 info->which = sys_cputimer;
88c4d2f6
MD
247 info->gd = mycpu;
248 systimer_add(info);
249}
250
0d1dffdf 251void
96d52ac8
SZ
252systimer_init_periodic_nq(systimer_t info, systimer_func_t func, void *data,
253 int hz)
0d1dffdf
MD
254{
255 sysclock_t base_count;
256
257 bzero(info, sizeof(struct systimer));
044ee7c4
MD
258 info->periodic = sys_cputimer->fromhz(hz);
259 base_count = sys_cputimer->count();
0d1dffdf
MD
260 base_count = base_count - (base_count % info->periodic);
261 info->time = base_count + info->periodic;
262 info->func = func;
263 info->data = data;
4b52d1af
MD
264 info->freq = hz;
265 info->which = sys_cputimer;
0d1dffdf
MD
266 info->gd = mycpu;
267 info->flags |= SYSTF_NONQUEUED;
268 systimer_add(info);
269}
270
4b52d1af
MD
271/*
272 * Adjust the periodic interval for a periodic timer which is already
273 * running. The current timeout is not effected.
274 */
275void
276systimer_adjust_periodic(systimer_t info, int hz)
277{
278 crit_enter();
279 info->periodic = sys_cputimer->fromhz(hz);
280 info->freq = hz;
281 info->which = sys_cputimer;
282 crit_exit();
283}
284
88c4d2f6
MD
285/*
286 * systimer_init_oneshot()
287 *
288 * Initialize a periodic timer at the specified frequency and add
289 * it to the system. The frequency is uncompensated and approximate.
290 */
291void
96d52ac8 292systimer_init_oneshot(systimer_t info, systimer_func_t func, void *data, int us)
88c4d2f6
MD
293{
294 bzero(info, sizeof(struct systimer));
044ee7c4 295 info->time = sys_cputimer->count() + sys_cputimer->fromus(us);
88c4d2f6
MD
296 info->func = func;
297 info->data = data;
4b52d1af 298 info->which = sys_cputimer;
88c4d2f6
MD
299 info->gd = mycpu;
300 systimer_add(info);
301}