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.2 2004/02/12 06:57:48 dillon Exp $
30 * This code implements a fine-grained per-cpu system timer which is
31 * ultimately based on a hardware timer. The hardware timer abstraction
32 * is sufficiently disconnected from this code to support both per-cpu
33 * hardware timers or a single system-wide hardware timer.
35 * Notes on machine-dependant code (in arch/arch/systimer.c)
37 * cputimer_intr_reload() Reload the one-shot (per-cpu basis)
39 * cputimer_count() Get the current absolute sysclock_t value.
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/systm.h>
45 #include <sys/thread.h>
46 #include <sys/globaldata.h>
47 #include <sys/systimer.h>
48 #include <sys/thread2.h>
51 * Execute ready systimers. Called directly from the platform-specific
52 * one-shot timer clock interrupt (e.g. clkintr()). Systimer functions are
53 * responsible for calling hardclock, statclock, and other finely-timed
57 systimer_intr(sysclock_t *timep, struct intrframe *frame)
59 globaldata_t gd = mycpu;
60 sysclock_t time = *timep;
68 while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
70 * If we haven't reached the requested time, tell the cputimer
71 * how much is left and break out.
73 if ((int)(info->time - time) > 0) {
74 cputimer_intr_reload(info->time - time);
81 info->flags &= ~SYSTF_ONQUEUE;
82 TAILQ_REMOVE(info->queue, info, node);
84 info->func(info, frame);
88 * Reinstall if periodic
91 info->time += info->periodic;
96 gd->gd_nextclock = info->time;
104 systimer_add(systimer_t info)
106 struct globaldata *gd = mycpu;
108 KKASSERT((info->flags & (SYSTF_ONQUEUE|SYSTF_IPIRUNNING)) == 0);
110 if (info->gd == gd) {
113 scan1 = TAILQ_FIRST(&gd->gd_systimerq);
114 if (scan1 == NULL || (int)(scan1->time - info->time) > 0) {
115 gd->gd_nextclock = info->time;
116 cputimer_intr_reload(info->time - cputimer_count());
117 TAILQ_INSERT_HEAD(&gd->gd_systimerq, info, node);
119 scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
122 TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
125 if ((int)(scan1->time - info->time) > 0) {
126 TAILQ_INSERT_BEFORE(scan1, info, node);
129 if ((int)(scan2->time - info->time) <= 0) {
130 TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node);
133 scan1 = TAILQ_NEXT(scan1, node);
134 scan2 = TAILQ_PREV(scan2, systimerq, node);
137 info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
138 info->queue = &gd->gd_systimerq;
140 info->flags |= SYSTF_IPIRUNNING;
141 lwkt_send_ipiq(info->gd, (ipifunc_t)systimer_add, info);
149 * Delete a system timer. Only the owning cpu can delete a timer.
152 systimer_del(systimer_t info)
154 KKASSERT(info->gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
156 if (info->flags & SYSTF_ONQUEUE) {
157 TAILQ_REMOVE(info->queue, info, node);
158 info->flags &= ~SYSTF_ONQUEUE;
164 * systimer_init_periodic()
166 * Initialize a periodic timer at the specified frequency and add
167 * it to the system. The frequency is uncompensated and approximate.
169 * Try to synchronize multi registrations of the same or similar
170 * frequencies so the hardware interrupt is able to dispatch several
171 * at together by adjusting the phase of the initial interrupt. This
172 * helps SMP. Note that we are not attempting to synchronize to
173 * the realtime clock.
176 systimer_init_periodic(systimer_t info, void *func, void *data, int hz)
178 sysclock_t base_count;
180 bzero(info, sizeof(struct systimer));
181 info->periodic = cputimer_fromhz(hz);
182 base_count = cputimer_count();
183 base_count = base_count - (base_count % info->periodic);
184 info->time = base_count + info->periodic;
192 * systimer_init_oneshot()
194 * Initialize a periodic timer at the specified frequency and add
195 * it to the system. The frequency is uncompensated and approximate.
198 systimer_init_oneshot(systimer_t info, void *func, void *data, int us)
200 bzero(info, sizeof(struct systimer));
201 info->time = cputimer_count() + cputimer_fromus(us);