2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sys/kern/kern_cputimer.c,v 1.3 2005/06/01 22:25:12 dillon Exp $
37 * Generic cputimer - access to a reliable, free-running counter.
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>
49 static sysclock_t dummy_cputimer_count(void);
51 static struct cputimer dummy_cputimer = {
57 cputimer_default_fromhz,
58 cputimer_default_fromus,
59 cputimer_default_construct,
60 cputimer_default_destruct,
62 (1000000LL << 32) / 1000000,
63 (1000000000LL << 32) / 1000000,
67 static struct cputimer *cputimer_reg_base = &dummy_cputimer;
68 struct cputimer *sys_cputimer = &dummy_cputimer;
71 * Generic cputimer API
74 cputimer_select(struct cputimer *timer, int pri)
79 * Calculate helper fields
81 cputimer_set_frequency(timer, timer->freq);
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
90 if (timer == NULL || pri >= sys_cputimer->pri) {
91 oldclock = sys_cputimer->count();
92 sys_cputimer->destruct(sys_cputimer);
93 sys_cputimer = &dummy_cputimer;
96 timer->construct(timer, oldclock);
97 cputimer_intr_config(timer);
103 * Register a timer. If the timer has already been registered, do nothing.
106 cputimer_register(struct cputimer *timer)
108 struct cputimer **scanpp;
109 struct cputimer *scan;
111 for (scanpp = &cputimer_reg_base;
112 (scan = *scanpp) != NULL;
123 * Deregister a timer. If the timer has already been deregistered, do nothing.
126 cputimer_deregister(struct cputimer *timer)
128 struct cputimer **scanpp;
129 struct cputimer *scan;
130 struct cputimer *best;
133 * Locate and remove the timer. If the timer is our currently active
134 * timer, revert to the dummy timer.
136 scanpp = &cputimer_reg_base;
137 while ((scan = *scanpp) != NULL) {
139 *scanpp = timer->next;
140 if (timer == sys_cputimer)
141 cputimer_select(&dummy_cputimer, 0x7FFFFFFF);
143 scanpp = &scan->next;
148 * If sys_cputimer reverted to the dummy, select the best one
150 if (sys_cputimer == &dummy_cputimer) {
152 for (scan = cputimer_reg_base; scan; scan = scan->next) {
153 if (best == NULL || scan->pri > best->pri)
157 cputimer_select(best, 0x7FFFFFFF);
162 * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
164 * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
165 * or 3000 nsec / tick x (1 << 32)
168 cputimer_set_frequency(struct cputimer *timer, int freq)
171 timer->freq64_usec = (1000000LL << 32) / freq;
172 timer->freq64_nsec = (1000000000LL << 32) / freq;
173 if (timer == sys_cputimer)
174 cputimer_intr_config(timer);
178 cputimer_default_fromhz(int freq)
180 return(sys_cputimer->freq / freq + 1);
184 cputimer_default_fromus(int us)
186 return((int64_t)sys_cputimer->freq * us / 1000000);
190 * Dummy counter implementation
194 dummy_cputimer_count(void)
196 return(++dummy_cputimer.base);
200 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
202 cputimer->base = oldclock;
206 cputimer_default_destruct(struct cputimer *cputimer)
210 /************************************************************************
212 ************************************************************************
214 * Note: the ability to change the systimer is not currently enabled
215 * because it will mess up systimer calculations. You have to live
216 * with what is configured at boot.
219 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
221 struct cputimer *scan;
226 * Build a list of available timers
228 for (scan = cputimer_reg_base; scan; scan = scan->next) {
229 if (error == 0 && loop)
230 error = SYSCTL_OUT(req, " ", 1);
232 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
239 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
243 error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
248 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
253 clock = sys_cputimer->count();
254 error = SYSCTL_OUT(req, &clock, sizeof(clock));
259 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
263 error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
267 SYSCTL_DECL(_kern_cputimer);
268 SYSCTL_NODE(_kern, OID_AUTO, cputimer, CTLFLAG_RW, NULL, "cputimer");
270 SYSCTL_PROC(_kern_cputimer, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RD,
271 NULL, NULL, sysctl_cputimer_reglist, "A", "");
272 SYSCTL_PROC(_kern_cputimer, OID_AUTO, name, CTLTYPE_STRING|CTLFLAG_RD,
273 NULL, NULL, sysctl_cputimer_name, "A", "");
274 SYSCTL_PROC(_kern_cputimer, OID_AUTO, clock, CTLTYPE_UINT|CTLFLAG_RD,
275 NULL, NULL, sysctl_cputimer_clock, "IU", "");
276 SYSCTL_PROC(_kern_cputimer, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
277 NULL, NULL, sysctl_cputimer_freq, "I", "");