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
35 * Generic cputimer - access to a reliable, free-running counter.
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/thread.h>
42 #include <sys/globaldata.h>
43 #include <sys/serialize.h>
44 #include <sys/systimer.h>
45 #include <sys/sysctl.h>
46 #include <sys/thread2.h>
48 static sysclock_t dummy_cputimer_count(void);
50 static struct cputimer dummy_cputimer = {
51 SLIST_ENTRY_INITIALIZER,
56 cputimer_default_fromhz,
57 cputimer_default_fromus,
58 cputimer_default_construct,
59 cputimer_default_destruct,
61 (1000000LL << 32) / 1000000,
62 (1000000000LL << 32) / 1000000,
66 struct cputimer *sys_cputimer = &dummy_cputimer;
67 SLIST_HEAD(, cputimer) cputimerhead = SLIST_HEAD_INITIALIZER(&cputimerhead);
69 static int cputimer_intr_ps_reqs;
70 static struct lwkt_serialize cputimer_intr_ps_slize =
71 LWKT_SERIALIZE_INITIALIZER;
74 * Generic cputimer API
77 cputimer_select(struct cputimer *timer, int pri)
82 * Calculate helper fields
84 cputimer_set_frequency(timer, timer->freq);
87 * Install a new cputimer if its priority allows it. If timer is
88 * passed as NULL we deinstall the current timer and revert to our
93 if (timer == NULL || pri >= sys_cputimer->pri) {
94 oldclock = sys_cputimer->count();
95 sys_cputimer->destruct(sys_cputimer);
96 sys_cputimer = &dummy_cputimer;
99 timer->construct(timer, oldclock);
100 cputimer_intr_config(timer);
106 * Register a timer. If the timer has already been registered, do nothing.
109 cputimer_register(struct cputimer *timer)
111 struct cputimer *scan;
114 * Initialize dummy_cputimer if the slist is empty, it does not get
115 * registered the normal way.
117 if (SLIST_EMPTY(&cputimerhead))
118 SLIST_FIRST(&cputimerhead) = &dummy_cputimer;
119 SLIST_FOREACH(scan, &cputimerhead, next) {
123 SLIST_INSERT_HEAD(&cputimerhead, timer, next);
127 * Deregister a timer. If the timer has already been deregistered, do nothing.
130 cputimer_deregister(struct cputimer *timer)
132 struct cputimer *scan;
133 struct cputimer *best;
136 * Locate and remove the timer. If the timer is our currently active
137 * timer, revert to the dummy timer.
139 SLIST_FOREACH(scan, &cputimerhead, next) {
141 if (timer == sys_cputimer)
142 cputimer_select(&dummy_cputimer, 0x7FFFFFFF);
143 SLIST_REMOVE(&cputimerhead, timer, cputimer, next);
149 * If sys_cputimer reverted to the dummy, select the best one
151 if (sys_cputimer == &dummy_cputimer) {
153 SLIST_FOREACH(scan, &cputimerhead, next) {
154 if (best == NULL || scan->pri > best->pri)
158 cputimer_select(best, 0x7FFFFFFF);
163 * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
165 * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
166 * or 3000 nsec / tick x (1 << 32)
169 cputimer_set_frequency(struct cputimer *timer, sysclock_t freq)
172 timer->freq64_usec = (1000000LL << 32) / freq;
173 timer->freq64_nsec = (1000000000LL << 32) / freq;
174 if (timer == sys_cputimer)
175 cputimer_intr_config(timer);
179 cputimer_default_fromhz(int freq)
181 return(sys_cputimer->freq / freq + 1);
185 cputimer_default_fromus(int us)
187 return((int64_t)sys_cputimer->freq * us / 1000000);
191 * Dummy counter implementation
195 dummy_cputimer_count(void)
197 return(++dummy_cputimer.base);
201 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
203 cputimer->base = oldclock;
207 cputimer_default_destruct(struct cputimer *cputimer)
211 /************************************************************************
213 ************************************************************************
215 * Note: the ability to change the systimer is not currently enabled
216 * because it will mess up systimer calculations. You have to live
217 * with what is configured at boot.
220 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
222 struct cputimer *scan;
227 * Build a list of available timers
229 SLIST_FOREACH(scan, &cputimerhead, next) {
230 if (error == 0 && loop)
231 error = SYSCTL_OUT(req, " ", 1);
233 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
240 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
244 error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
249 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
254 clock = sys_cputimer->count();
255 error = SYSCTL_OUT(req, &clock, sizeof(clock));
260 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
264 error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
268 SYSCTL_DECL(_kern_cputimer);
269 SYSCTL_NODE(_kern, OID_AUTO, cputimer, CTLFLAG_RW, NULL, "cputimer");
271 SYSCTL_PROC(_kern_cputimer, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RD,
272 NULL, 0, sysctl_cputimer_reglist, "A", "");
273 SYSCTL_PROC(_kern_cputimer, OID_AUTO, name, CTLTYPE_STRING|CTLFLAG_RD,
274 NULL, 0, sysctl_cputimer_name, "A", "");
275 SYSCTL_PROC(_kern_cputimer, OID_AUTO, clock, CTLTYPE_UINT|CTLFLAG_RD,
276 NULL, 0, sysctl_cputimer_clock, "IU", "");
277 SYSCTL_PROC(_kern_cputimer, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
278 NULL, 0, sysctl_cputimer_freq, "I", "");
280 static struct cputimer_intr *sys_cputimer_intr;
281 static uint32_t cputimer_intr_caps;
282 SLIST_HEAD(, cputimer_intr) cputimer_intr_head =
283 SLIST_HEAD_INITIALIZER(&cputimer_intr_head);
286 cputimer_intr_register(struct cputimer_intr *cti)
288 struct cputimer_intr *scan;
290 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
294 cti->config(cti, sys_cputimer);
295 SLIST_INSERT_HEAD(&cputimer_intr_head, cti, next);
299 cputimer_intr_deregister(struct cputimer_intr *cti)
301 KKASSERT(cti != sys_cputimer_intr);
302 SLIST_REMOVE(&cputimer_intr_head, cti, cputimer_intr, next);
306 cputimer_intr_select(struct cputimer_intr *cti, int prio)
308 KKASSERT(cti != NULL);
313 if (sys_cputimer_intr == NULL) {
314 KKASSERT(cputimer_intr_caps == 0);
315 sys_cputimer_intr = cti;
319 if ((cti->caps & cputimer_intr_caps) == cputimer_intr_caps) {
320 if (prio > sys_cputimer_intr->prio) {
321 sys_cputimer_intr = cti;
332 cputimer_intr_default_enable(struct cputimer_intr *cti __unused)
337 cputimer_intr_default_restart(struct cputimer_intr *cti)
343 cputimer_intr_default_config(struct cputimer_intr *cti __unused,
344 const struct cputimer *timer __unused)
349 cputimer_intr_default_pmfixup(struct cputimer_intr *cti __unused)
354 cputimer_intr_default_initclock(struct cputimer_intr *cti __unused,
355 boolean_t selected __unused)
360 cputimer_intr_enable(void)
362 struct cputimer_intr *cti;
364 SLIST_FOREACH(cti, &cputimer_intr_head, next)
369 cputimer_intr_config(const struct cputimer *timer)
371 struct cputimer_intr *cti;
373 SLIST_FOREACH(cti, &cputimer_intr_head, next)
374 cti->config(cti, timer);
378 cputimer_intr_pmfixup(void)
380 struct cputimer_intr *cti;
382 SLIST_FOREACH(cti, &cputimer_intr_head, next)
387 cputimer_intr_reload(sysclock_t reload)
389 struct cputimer_intr *cti = sys_cputimer_intr;
391 cti->reload(cti, reload);
395 cputimer_intr_restart(void)
397 struct cputimer_intr *cti = sys_cputimer_intr;
403 cputimer_intr_select_caps(uint32_t caps)
405 struct cputimer_intr *cti, *maybe;
409 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
410 if ((cti->caps & caps) == caps) {
413 else if (cti->prio > maybe->prio)
420 if (sys_cputimer_intr == maybe)
423 cputimer_intr_caps = caps;
424 error = cputimer_intr_select(maybe, CPUTIMER_INTR_PRIO_MAX);
431 cputimer_intr_initclocks(void)
433 struct cputimer_intr *cti, *ncti;
436 * An interrupt cputimer may deregister itself,
437 * so use SLIST_FOREACH_MUTABLE here.
439 SLIST_FOREACH_MUTABLE(cti, &cputimer_intr_head, next, ncti) {
440 boolean_t selected = FALSE;
442 if (cti == sys_cputimer_intr)
444 cti->initclock(cti, selected);
447 /* NOTE: Must be SECOND to allow platform initialization to go first */
448 SYSINIT(cputimer_intr, SI_BOOT2_CLOCKREG, SI_ORDER_SECOND,
449 cputimer_intr_initclocks, NULL);
452 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS)
454 struct cputimer_intr *scan;
459 * Build a list of available interrupt cputimers
461 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
462 if (error == 0 && loop)
463 error = SYSCTL_OUT(req, " ", 1);
465 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
472 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS)
476 error = SYSCTL_OUT(req, &sys_cputimer_intr->freq,
477 sizeof(sys_cputimer_intr->freq));
482 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS)
484 struct cputimer_intr *cti;
488 ksnprintf(name, sizeof(name), "%s", sys_cputimer_intr->name);
489 error = sysctl_handle_string(oidp, name, sizeof(name), req);
490 if (error != 0 || req->newptr == NULL)
493 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
494 if (strcmp(cti->name, name) == 0)
499 if (cti == sys_cputimer_intr)
502 error = cputimer_intr_select(cti, CPUTIMER_INTR_PRIO_MAX);
504 cputimer_intr_restart();
508 SYSCTL_NODE(_kern_cputimer, OID_AUTO, intr, CTLFLAG_RW, NULL,
509 "interrupt cputimer");
511 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, reglist, CTLTYPE_STRING|CTLFLAG_RD,
512 NULL, 0, sysctl_cputimer_intr_reglist, "A", "");
513 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
514 NULL, 0, sysctl_cputimer_intr_freq, "I", "");
515 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RW,
516 NULL, 0, sysctl_cputimer_intr_select, "A", "");
519 cputimer_intr_powersave_addreq(void)
523 lwkt_serialize_enter(&cputimer_intr_ps_slize);
525 ++cputimer_intr_ps_reqs;
526 if (cputimer_intr_ps_reqs == 1) {
528 * Upon the first power saving request, switch to an one shot
529 * timer, which would not stop in the any power saving state.
531 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_PS);
532 if (error == ERESTART) {
535 kprintf("cputimer: first power save request, restart\n");
536 cputimer_intr_restart();
538 kprintf("no suitable intr cputimer found\n");
539 --cputimer_intr_ps_reqs;
540 } else if (bootverbose) {
541 kprintf("cputimer: first power save request\n");
545 lwkt_serialize_exit(&cputimer_intr_ps_slize);
551 cputimer_intr_powersave_remreq(void)
553 lwkt_serialize_enter(&cputimer_intr_ps_slize);
555 KASSERT(cputimer_intr_ps_reqs > 0,
556 ("invalid # of powersave reqs %d", cputimer_intr_ps_reqs));
557 --cputimer_intr_ps_reqs;
558 if (cputimer_intr_ps_reqs == 0) {
561 /* No one needs power saving, use a better one shot timer. */
562 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_NONE);
563 KKASSERT(!error || error == ERESTART);
564 if (error == ERESTART) {
566 kprintf("cputimer: no powser save request, restart\n");
567 cputimer_intr_restart();
568 } else if (bootverbose) {
569 kprintf("cputimer: no power save request\n");
573 lwkt_serialize_exit(&cputimer_intr_ps_slize);