xref: /dragonfly/sys/kern/kern_systimer.c (revision e8364298)
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $DragonFly: src/sys/kern/kern_systimer.c,v 1.3 2004/05/06 18:09:29 dillon Exp $
27  */
28 
29 /*
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.
32  *
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.
37  *
38  * Notes on machine-dependant code (in arch/arch/systimer.c)
39  *
40  * cputimer_intr_reload()	Reload the one-shot (per-cpu basis)
41  *
42  * cputimer_count()		Get the current absolute sysclock_t value.
43  */
44 
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>
52 
53 /*
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
57  * routines.
58  */
59 void
60 systimer_intr(sysclock_t *timep, struct intrframe *frame)
61 {
62     globaldata_t gd = mycpu;
63     sysclock_t time = *timep;
64     systimer_t info;
65 
66     if (gd->gd_syst_nest)
67 	return;
68 
69     crit_enter();
70     ++gd->gd_syst_nest;
71     while ((info = TAILQ_FIRST(&gd->gd_systimerq)) != NULL) {
72 	/*
73 	 * If we haven't reached the requested time, tell the cputimer
74 	 * how much is left and break out.
75 	 */
76 	if ((int)(info->time - time) > 0) {
77 	    cputimer_intr_reload(info->time - time);
78 	    break;
79 	}
80 
81 	/*
82 	 * Dequeue and execute
83 	 */
84 	info->flags &= ~SYSTF_ONQUEUE;
85 	TAILQ_REMOVE(info->queue, info, node);
86 	crit_exit();
87 	info->func(info, frame);
88 	crit_enter();
89 
90 	/*
91 	 * Reinstall if periodic
92 	 */
93 	if (info->periodic) {
94 	    info->time += info->periodic;
95 	    systimer_add(info);
96 	}
97     }
98     if (info)
99 	gd->gd_nextclock = info->time;
100     else
101 	gd->gd_nextclock = 0;
102     --gd->gd_syst_nest;
103     crit_exit();
104 }
105 
106 void
107 systimer_add(systimer_t info)
108 {
109     struct globaldata *gd = mycpu;
110 
111     KKASSERT((info->flags & (SYSTF_ONQUEUE|SYSTF_IPIRUNNING)) == 0);
112     crit_enter();
113     if (info->gd == gd) {
114 	systimer_t scan1;
115 	systimer_t scan2;
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);
121 	} else {
122 	    scan2 = TAILQ_LAST(&gd->gd_systimerq, systimerq);
123 	    for (;;) {
124 		if (scan1 == NULL) {
125 		    TAILQ_INSERT_TAIL(&gd->gd_systimerq, info, node);
126 		    break;
127 		}
128 		if ((int)(scan1->time - info->time) > 0) {
129 		    TAILQ_INSERT_BEFORE(scan1, info, node);
130 		    break;
131 		}
132 		if ((int)(scan2->time - info->time) <= 0) {
133 		    TAILQ_INSERT_AFTER(&gd->gd_systimerq, scan2, info, node);
134 		    break;
135 		}
136 		scan1 = TAILQ_NEXT(scan1, node);
137 		scan2 = TAILQ_PREV(scan2, systimerq, node);
138 	    }
139 	}
140 	info->flags = (info->flags | SYSTF_ONQUEUE) & ~SYSTF_IPIRUNNING;
141 	info->queue = &gd->gd_systimerq;
142     } else {
143 	info->flags |= SYSTF_IPIRUNNING;
144 	lwkt_send_ipiq(info->gd, (ipifunc_t)systimer_add, info);
145     }
146     crit_exit();
147 }
148 
149 /*
150  * systimer_del()
151  *
152  *	Delete a system timer.  Only the owning cpu can delete a timer.
153  */
154 void
155 systimer_del(systimer_t info)
156 {
157     KKASSERT(info->gd == mycpu && (info->flags & SYSTF_IPIRUNNING) == 0);
158     crit_enter();
159     if (info->flags & SYSTF_ONQUEUE) {
160 	TAILQ_REMOVE(info->queue, info, node);
161 	info->flags &= ~SYSTF_ONQUEUE;
162     }
163     crit_exit();
164 }
165 
166 /*
167  * systimer_init_periodic()
168  *
169  *	Initialize a periodic timer at the specified frequency and add
170  *	it to the system.  The frequency is uncompensated and approximate.
171  *
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.
177  */
178 void
179 systimer_init_periodic(systimer_t info, void *func, void *data, int hz)
180 {
181     sysclock_t base_count;
182 
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;
188     info->func = func;
189     info->data = data;
190     info->gd = mycpu;
191     systimer_add(info);
192 }
193 
194 /*
195  * systimer_init_oneshot()
196  *
197  *	Initialize a periodic timer at the specified frequency and add
198  *	it to the system.  The frequency is uncompensated and approximate.
199  */
200 void
201 systimer_init_oneshot(systimer_t info, void *func, void *data, int us)
202 {
203     bzero(info, sizeof(struct systimer));
204     info->time = cputimer_count() + cputimer_fromus(us);
205     info->func = func;
206     info->data = data;
207     info->gd = mycpu;
208     systimer_add(info);
209 }
210 
211