Store the frequency and cputimer used to initialize a periodic systimer.
[dragonfly/vkernel-mp.git] / sys / kern / kern_systimer.c
bloba341dc97f84c8350317d9c20073454ad3a69568b
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:
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.
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.
34 * $DragonFly: src/sys/kern/kern_systimer.c,v 1.9 2007/04/30 06:57:38 dillon Exp $
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.
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.
46 * WARNING! During early boot if a new system timer is selected, existing
47 * timeouts will not be effected and will thus occur slower or faster.
48 * periodic timers will be adjusted at the next periodic load.
50 * Notes on machine-dependant code (in arch/arch/systimer.c)
52 * cputimer_intr_reload() Reload the one-shot (per-cpu basis)
55 #include <sys/param.h>
56 #include <sys/kernel.h>
57 #include <sys/systm.h>
58 #include <sys/thread.h>
59 #include <sys/globaldata.h>
60 #include <sys/systimer.h>
61 #include <sys/thread2.h>
64 * Execute ready systimers. Called directly from the platform-specific
65 * one-shot timer clock interrupt (e.g. clkintr()) or via an IPI. May
66 * be called simultaniously on multiple cpus and always operations on
67 * the current cpu's queue. Systimer functions are responsible for calling
68 * hardclock, statclock, and other finely-timed routines.
70 void
71 systimer_intr(sysclock_t *timep, int dummy, struct intrframe *frame)
73 globaldata_t gd = mycpu;
74 sysclock_t time = *timep;
75 systimer_t info;
77 if (gd->gd_syst_nest)
78 return;
80 crit_enter();
81 ++gd->gd_syst_nest;
82 while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
84 * If we haven't reached the requested time, tell the cputimer
85 * how much is left and break out.
87 if ((int)(info->time - time) > 0) {
88 cputimer_intr_reload(info->time - time);
89 break;
93 * Dequeue and execute
95 info->flags &= ~SYSTF_ONQUEUE;
96 TAILQ_REMOVE(info->queue, info, node);
97 crit_exit();
98 info->func(info, frame);
99 crit_enter();
102 * Reinstall if periodic. If this is a non-queued periodic
103 * interrupt do not allow multiple events to build up (used
104 * for things like the callout timer to prevent premature timeouts
105 * due to long interrupt disablements, BIOS 8254 glitching, and so
106 * forth). However, we still want to keep things synchronized between
107 * cpus for efficient handling of the timer interrupt so jump in
108 * multiples of the periodic rate.
110 if (info->periodic) {
111 if (info->which != sys_cputimer) {
112 info->periodic = sys_cputimer->fromhz(info->freq);
113 info->which = sys_cputimer;
114 kprintf("readjusted systimer freq %p\n", info);
116 info->time += info->periodic;
117 if ((info->flags & SYSTF_NONQUEUED) &&
118 (int)(info->time - time) <= 0
120 info->time += ((time - info->time + info->periodic - 1) /
121 info->periodic) * info->periodic;
123 systimer_add(info);
126 --gd->gd_syst_nest;
127 crit_exit();
130 void
131 systimer_add(systimer_t info)
133 struct globaldata *gd = mycpu;
135 KKASSERT((info->flags & (SYSTF_ONQUEUE|SYSTF_IPIRUNNING)) == 0);
136 crit_enter();
137 if (info->gd == gd) {
138 systimer_t scan1;
139 systimer_t scan2;
140 scan1 = TAILQ_FIRST(&gd->gd_systimerq);
141 if (scan1 == NULL || (int)(scan1->time - info->time) > 0) {
142 cputimer_intr_reload(info->time - sys_cputimer->count());
143 TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
144 } else {
145 scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
146 for (;;) {
147 if (scan1 == NULL) {
148 TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
149 break;
151 if ((int)(scan1->time - info->time) > 0) {
152 TAILQ_INSERT_BEFORE(scan1, info, node);
153 break;
155 if ((int)(scan2->time - info->time) <= 0) {
156 TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node);
157 break;
159 scan1 = TAILQ_NEXT(scan1, node);
160 scan2 = TAILQ_PREV(scan2, systimerq, node);
163 info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
164 info->queue = &gd->gd_systimerq;
165 } else {
166 #ifdef SMP
167 info->flags |= SYSTF_IPIRUNNING;
168 lwkt_send_ipiq(info->gd, (ipifunc1_t)systimer_add, info);
169 #else
170 panic("systimer_add: bad gd in info %p", info);
171 #endif
173 crit_exit();
177 * systimer_del()
179 * Delete a system timer. Only the owning cpu can delete a timer.
181 void
182 systimer_del(systimer_t info)
184 KKASSERT(info->gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
185 crit_enter();
186 if (info->flags & SYSTF_ONQUEUE) {
187 TAILQ_REMOVE(info->queue, info, node);
188 info->flags &= ~SYSTF_ONQUEUE;
190 crit_exit();
194 * systimer_init_periodic()
196 * Initialize a periodic timer at the specified frequency and add
197 * it to the system. The frequency is uncompensated and approximate.
199 * Try to synchronize multi registrations of the same or similar
200 * frequencies so the hardware interrupt is able to dispatch several
201 * at together by adjusting the phase of the initial interrupt. This
202 * helps SMP. Note that we are not attempting to synchronize to
203 * the realtime clock.
205 void
206 systimer_init_periodic(systimer_t info, void *func, void *data, int hz)
208 sysclock_t base_count;
210 bzero(info, sizeof(struct systimer));
211 info->periodic = sys_cputimer->fromhz(hz);
212 base_count = sys_cputimer->count();
213 base_count = base_count - (base_count % info->periodic);
214 info->time = base_count + info->periodic;
215 info->func = func;
216 info->data = data;
217 info->freq = hz;
218 info->which = sys_cputimer;
219 info->gd = mycpu;
220 systimer_add(info);
223 void
224 systimer_init_periodic_nq(systimer_t info, void *func, void *data, int hz)
226 sysclock_t base_count;
228 bzero(info, sizeof(struct systimer));
229 info->periodic = sys_cputimer->fromhz(hz);
230 base_count = sys_cputimer->count();
231 base_count = base_count - (base_count % info->periodic);
232 info->time = base_count + info->periodic;
233 info->func = func;
234 info->data = data;
235 info->freq = hz;
236 info->which = sys_cputimer;
237 info->gd = mycpu;
238 info->flags |= SYSTF_NONQUEUED;
239 systimer_add(info);
243 * Adjust the periodic interval for a periodic timer which is already
244 * running. The current timeout is not effected.
246 void
247 systimer_adjust_periodic(systimer_t info, int hz)
249 crit_enter();
250 info->periodic = sys_cputimer->fromhz(hz);
251 info->freq = hz;
252 info->which = sys_cputimer;
253 crit_exit();
257 * systimer_init_oneshot()
259 * Initialize a periodic timer at the specified frequency and add
260 * it to the system. The frequency is uncompensated and approximate.
262 void
263 systimer_init_oneshot(systimer_t info, void *func, void *data, int us)
265 bzero(info, sizeof(struct systimer));
266 info->time = sys_cputimer->count() + sys_cputimer->fromus(us);
267 info->func = func;
268 info->data = data;
269 info->which = sys_cputimer;
270 info->gd = mycpu;
271 systimer_add(info);