2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $DragonFly: src/sys/kern/kern_systimer.c,v 1.3 2004/05/06 18:09:29 dillon Exp $
30 * WARNING! THE SYSTIMER MODULE DOES NOT OPERATE OR DISPATCH WITH THE
31 * MP LOCK HELD. ALL CODE USING THIS MODULE MUST BE MP-SAFE.
33 * This code implements a fine-grained per-cpu system timer which is
34 * ultimately based on a hardware timer. The hardware timer abstraction
35 * is sufficiently disconnected from this code to support both per-cpu
36 * hardware timers or a single system-wide hardware timer.
38 * Notes on machine-dependant code (in arch/arch/systimer.c)
40 * cputimer_intr_reload() Reload the one-shot (per-cpu basis)
42 * cputimer_count() Get the current absolute sysclock_t value.
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48 #include <sys/thread.h>
49 #include <sys/globaldata.h>
50 #include <sys/systimer.h>
51 #include <sys/thread2.h>
54 * Execute ready systimers. Called directly from the platform-specific
55 * one-shot timer clock interrupt (e.g. clkintr()). Systimer functions are
56 * responsible for calling hardclock, statclock, and other finely-timed
60 systimer_intr(sysclock_t *timep, struct intrframe *frame)
62 globaldata_t gd = mycpu;
63 sysclock_t time = *timep;
71 while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
73 * If we haven't reached the requested time, tell the cputimer
74 * how much is left and break out.
76 if ((int)(info->time - time) > 0) {
77 cputimer_intr_reload(info->time - time);
84 info->flags &= ~SYSTF_ONQUEUE;
85 TAILQ_REMOVE(info->queue, info, node);
87 info->func(info, frame);
91 * Reinstall if periodic
94 info->time += info->periodic;
99 gd->gd_nextclock = info->time;
101 gd->gd_nextclock = 0;
107 systimer_add(systimer_t info)
109 struct globaldata *gd = mycpu;
111 KKASSERT((info->flags & (SYSTF_ONQUEUE|SYSTF_IPIRUNNING)) == 0);
113 if (info->gd == gd) {
116 scan1 = TAILQ_FIRST(&gd->gd_systimerq);
117 if (scan1 == NULL || (int)(scan1->time - info->time) > 0) {
118 gd->gd_nextclock = info->time;
119 cputimer_intr_reload(info->time - cputimer_count());
120 TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
122 scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
125 TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
128 if ((int)(scan1->time - info->time) > 0) {
129 TAILQ_INSERT_BEFORE(scan1, info, node);
132 if ((int)(scan2->time - info->time) <= 0) {
133 TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node);
136 scan1 = TAILQ_NEXT(scan1, node);
137 scan2 = TAILQ_PREV(scan2, systimerq, node);
140 info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
141 info->queue = &gd->gd_systimerq;
143 info->flags |= SYSTF_IPIRUNNING;
144 lwkt_send_ipiq(info->gd, (ipifunc_t)systimer_add, info);
152 * Delete a system timer. Only the owning cpu can delete a timer.
155 systimer_del(systimer_t info)
157 KKASSERT(info->gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
159 if (info->flags & SYSTF_ONQUEUE) {
160 TAILQ_REMOVE(info->queue, info, node);
161 info->flags &= ~SYSTF_ONQUEUE;
167 * systimer_init_periodic()
169 * Initialize a periodic timer at the specified frequency and add
170 * it to the system. The frequency is uncompensated and approximate.
172 * Try to synchronize multi registrations of the same or similar
173 * frequencies so the hardware interrupt is able to dispatch several
174 * at together by adjusting the phase of the initial interrupt. This
175 * helps SMP. Note that we are not attempting to synchronize to
176 * the realtime clock.
179 systimer_init_periodic(systimer_t info, void *func, void *data, int hz)
181 sysclock_t base_count;
183 bzero(info, sizeof(struct systimer));
184 info->periodic = cputimer_fromhz(hz);
185 base_count = cputimer_count();
186 base_count = base_count - (base_count % info->periodic);
187 info->time = base_count + info->periodic;
195 * systimer_init_oneshot()
197 * Initialize a periodic timer at the specified frequency and add
198 * it to the system. The frequency is uncompensated and approximate.
201 systimer_init_oneshot(systimer_t info, void *func, void *data, int us)
203 bzero(info, sizeof(struct systimer));
204 info->time = cputimer_count() + cputimer_fromus(us);