More cleanups, add the API implementation to select the system clock.
[dragonfly.git] / sys / kern / kern_cputimer.c
1 /*
2  * Copyright (c) 2005 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:
10  * 
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.
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/kern/kern_cputimer.c,v 1.1 2005/06/01 20:19:47 dillon Exp $
35  */
36 /*
37  * Generic cputimer - access to a reliable, free-running counter.
38  */
39
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/thread.h>
44 #include <sys/globaldata.h>
45 #include <sys/systimer.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
48
49 static sysclock_t dummy_cputimer_count(void);
50
51 static struct cputimer dummy_cputimer = {
52     NULL,
53     "dummy",
54     CPUTIMER_PRI_DUMMY,
55     CPUTIMER_DUMMY,
56     dummy_cputimer_count,
57     cputimer_default_fromhz,
58     cputimer_default_fromus,
59     cputimer_default_construct,
60     cputimer_default_destruct,
61     1000000,
62     (1000000LL << 32) / 1000000,
63     (1000000000LL << 32) / 1000000,
64     0
65 };
66
67 static struct cputimer *cputimer_reg_base = &dummy_cputimer;
68 struct cputimer *sys_cputimer = &dummy_cputimer;
69
70 /*
71  * Generic cputimer API
72  */
73 void
74 cputimer_select(struct cputimer *timer, int pri)
75 {
76     sysclock_t oldclock;
77
78     /*
79      * Calculate helper fields
80      */
81     cputimer_set_frequency(timer, timer->freq);
82
83     /*
84      * Install a new cputimer if its priority allows it.  If timer is
85      * passed as NULL we deinstall the current timer and revert to our
86      * dummy.
87      */
88     if (pri == 0)
89         pri = timer->pri;
90     if (timer == NULL || pri >= sys_cputimer->pri) {
91         oldclock = sys_cputimer->count();
92         sys_cputimer->destruct(sys_cputimer);
93         sys_cputimer = &dummy_cputimer;
94         if (timer) {
95             sys_cputimer = timer;
96             timer->construct(timer, oldclock);
97         }
98     }
99 }
100
101 /*
102  * Register a timer.  If the timer has already been registered, do nothing.
103  */
104 void
105 cputimer_register(struct cputimer *timer)
106 {
107     struct cputimer **scanpp;
108     struct cputimer *scan;
109
110     for (scanpp = &cputimer_reg_base; 
111          (scan = *scanpp) != NULL;
112          scanpp = &scan->next
113     ) {
114         if (scan == timer)
115             return;
116     }
117     timer->next = NULL;
118     *scanpp = timer;
119 }
120
121 /*
122  * Deregister a timer.  If the timer has already been deregistered, do nothing.
123  */
124 void
125 cputimer_deregister(struct cputimer *timer)
126 {
127     struct cputimer **scanpp;
128     struct cputimer *scan;
129     struct cputimer *best;
130
131     /*
132      * Locate and remove the timer.  If the timer is our currently active
133      * timer, revert to the dummy timer.
134      */
135     scanpp = &cputimer_reg_base; 
136     while ((scan = *scanpp) != NULL) {
137         if (scan == timer) {
138             *scanpp = timer->next;
139             if (timer == sys_cputimer)
140                 cputimer_select(&dummy_cputimer, 0x7FFFFFFF);
141         } else {
142             scanpp = &scan->next;
143         }
144     }
145
146     /*
147      * If sys_cputimer reverted to the dummy, select the best one
148      */
149     if (sys_cputimer == &dummy_cputimer) {
150         best = NULL;
151         for (scan = cputimer_reg_base; scan; scan = scan->next) {
152             if (best == NULL || scan->pri > best->pri)
153                 best = scan;
154         }
155         if (best)
156             cputimer_select(best, 0x7FFFFFFF);
157     }
158 }
159
160 /*
161  * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
162  *
163  * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
164  * or 3000 nsec / tick x (1 << 32)
165  */
166 void
167 cputimer_set_frequency(struct cputimer *timer, int freq)
168 {
169     timer->freq = freq;
170     timer->freq64_usec = (1000000LL << 32) / freq;
171     timer->freq64_nsec = (1000000000LL << 32) / freq;
172 }
173
174 sysclock_t
175 cputimer_default_fromhz(int freq)
176 {
177     return(sys_cputimer->freq / freq + 1);
178 }
179
180 sysclock_t
181 cputimer_default_fromus(int us)
182 {
183     return((int64_t)sys_cputimer->freq * us / 1000000);
184 }
185
186 /*
187  * Dummy counter implementation
188  */
189 static
190 sysclock_t
191 dummy_cputimer_count(void)
192 {
193     return(++dummy_cputimer.base);
194 }
195
196 void
197 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
198 {
199     cputimer->base = oldclock;
200 }
201
202 void
203 cputimer_default_destruct(struct cputimer *cputimer)
204 {
205 }
206
207 /************************************************************************
208  *                              SYSCTL SUPPORT                          *
209  ************************************************************************/
210
211 static int
212 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
213 {
214     struct cputimer *scan;
215     struct cputimer *selected;
216     int error = 0;
217     int loop = 0;
218     char buf[32];
219
220     /*
221      * Writing to the reglist selects a timer
222      */
223     if (req->newptr != NULL) {
224         if (req->newlen < 0 || req->newlen >= sizeof(buf))
225             return (ENAMETOOLONG);
226         error = SYSCTL_IN(req, buf, req->newlen);
227         buf[req->newlen] = 0;
228     } else {
229         buf[0] = 0;
230     }
231
232     /*
233      * Build a list of available timers
234      */
235     selected = NULL;
236     for (scan = cputimer_reg_base; scan; scan = scan->next) {
237         if (error == 0 && loop)
238             error = SYSCTL_OUT(req, " ", 1);
239         if (error == 0)
240             error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
241         if (buf[0] && strcmp(buf, scan->name) == 0)
242             selected = scan;
243         ++loop;
244     }
245
246     /*
247      * If a particular timer is being selected by the sysctl, switch to it.
248      */
249     if (selected)
250         cputimer_select(selected, 0x7FFFFFFF);
251     if (error == 0 && buf[0] && selected == NULL)
252         error = ENOENT;
253     return (error);
254 }
255
256 static int
257 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
258 {
259     int error;
260
261     error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
262     return (error);
263 }
264
265 static int
266 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
267 {
268     sysclock_t clock;
269     int error;
270
271     clock = sys_cputimer->count();
272     error = SYSCTL_OUT(req, &clock, sizeof(clock));
273     return (error);
274 }
275
276 static int
277 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
278 {
279     int error;
280
281     error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
282     return (error);
283 }
284
285 SYSCTL_DECL(_kern_cputimer);
286 SYSCTL_NODE(_kern, OID_AUTO, cputimer, CTLFLAG_RW, NULL, "cputimer");
287
288 SYSCTL_PROC(_kern_cputimer, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RW,
289             NULL, NULL, sysctl_cputimer_reglist, "A", "");
290 SYSCTL_PROC(_kern_cputimer, OID_AUTO, name, CTLTYPE_STRING|CTLFLAG_RD,
291             NULL, NULL, sysctl_cputimer_name, "A", "");
292 SYSCTL_PROC(_kern_cputimer, OID_AUTO, clock, CTLTYPE_UINT|CTLFLAG_RD,
293             NULL, NULL, sysctl_cputimer_clock, "IU", "");
294 SYSCTL_PROC(_kern_cputimer, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
295             NULL, NULL, sysctl_cputimer_freq, "I", "");
296
297