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>
47 extern void pcpu_timer_process(void);
48 extern void pcpu_timer_process_frame(struct intrframe *);
50 static uint64_t dummy_cpucounter_count(void);
52 static sysclock_t dummy_cputimer_count(void);
54 static struct cputimer dummy_cputimer = {
55 .next = SLIST_ENTRY_INITIALIZER,
57 .pri = CPUTIMER_PRI_DUMMY,
58 .type = CPUTIMER_DUMMY,
59 .count = dummy_cputimer_count,
60 .fromhz = cputimer_default_fromhz,
61 .fromus = cputimer_default_fromus,
62 .construct = cputimer_default_construct,
63 .destruct = cputimer_default_destruct,
65 .freq64_usec = (1000000LL << 32) / 1000000,
66 .freq64_nsec = (1000000000LL << 32) / 1000000
69 static struct cpucounter dummy_cpucounter = {
71 .count = dummy_cpucounter_count,
72 .flags = CPUCOUNTER_FLAG_MPSYNC,
73 .prio = CPUCOUNTER_PRIO_DUMMY,
74 .type = CPUCOUNTER_DUMMY
77 struct cputimer *sys_cputimer = &dummy_cputimer;
78 SLIST_HEAD(, cputimer) cputimerhead = SLIST_HEAD_INITIALIZER(&cputimerhead);
80 static SLIST_HEAD(, cpucounter) cpucounterhead =
81 SLIST_HEAD_INITIALIZER(cpucounterhead);
83 static int cputimer_intr_ps_reqs;
84 static struct lwkt_serialize cputimer_intr_ps_slize =
85 LWKT_SERIALIZE_INITIALIZER;
88 * Generic cputimer API
91 cputimer_select(struct cputimer *timer, int pri)
96 * Calculate helper fields
98 cputimer_set_frequency(timer, timer->freq);
101 * Install a new cputimer if its priority allows it. If timer is
102 * passed as NULL we deinstall the current timer and revert to our
107 if (timer == NULL || pri >= sys_cputimer->pri) {
108 oldclock = sys_cputimer->count();
109 sys_cputimer->destruct(sys_cputimer);
110 sys_cputimer = &dummy_cputimer;
112 sys_cputimer = timer;
113 timer->construct(timer, oldclock);
114 cputimer_intr_config(timer);
120 * Register a timer. If the timer has already been registered, do nothing.
123 cputimer_register(struct cputimer *timer)
125 struct cputimer *scan;
128 * Initialize dummy_cputimer if the slist is empty, it does not get
129 * registered the normal way.
131 if (SLIST_EMPTY(&cputimerhead))
132 SLIST_FIRST(&cputimerhead) = &dummy_cputimer;
133 SLIST_FOREACH(scan, &cputimerhead, next) {
137 SLIST_INSERT_HEAD(&cputimerhead, timer, next);
141 * Deregister a timer. If the timer has already been deregistered, do nothing.
144 cputimer_deregister(struct cputimer *timer)
146 struct cputimer *scan;
147 struct cputimer *best;
150 * Locate and remove the timer. If the timer is our currently active
151 * timer, revert to the dummy timer.
153 SLIST_FOREACH(scan, &cputimerhead, next) {
155 if (timer == sys_cputimer)
156 cputimer_select(&dummy_cputimer, 0x7FFFFFFF);
157 SLIST_REMOVE(&cputimerhead, timer, cputimer, next);
163 * If sys_cputimer reverted to the dummy, select the best one
165 if (sys_cputimer == &dummy_cputimer) {
167 SLIST_FOREACH(scan, &cputimerhead, next) {
168 if (best == NULL || scan->pri > best->pri)
172 cputimer_select(best, 0x7FFFFFFF);
177 * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
179 * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
180 * or 3000 nsec / tick x (1 << 32)
183 cputimer_set_frequency(struct cputimer *timer, sysclock_t freq)
186 timer->freq64_usec = (1000000LL << 32) / freq;
187 timer->freq64_nsec = (1000000000LL << 32) / freq;
188 if (timer == sys_cputimer)
189 cputimer_intr_config(timer);
193 cputimer_default_fromhz(int freq)
195 return(sys_cputimer->freq / freq + 1);
199 cputimer_default_fromus(int us)
201 return((int64_t)sys_cputimer->freq * us / 1000000);
205 * Dummy counter implementation
209 dummy_cputimer_count(void)
211 return(++dummy_cputimer.base);
215 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
217 cputimer->base = oldclock;
221 cputimer_default_destruct(struct cputimer *cputimer)
225 /************************************************************************
227 ************************************************************************
229 * Note: the ability to change the systimer is not currently enabled
230 * because it will mess up systimer calculations. You have to live
231 * with what is configured at boot.
234 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
236 struct cputimer *scan;
241 * Build a list of available timers
243 SLIST_FOREACH(scan, &cputimerhead, next) {
244 if (error == 0 && loop)
245 error = SYSCTL_OUT(req, " ", 1);
247 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
254 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
258 error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
263 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
268 clock = sys_cputimer->count();
269 error = SYSCTL_OUT(req, &clock, sizeof(clock));
274 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
278 error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
282 SYSCTL_DECL(_kern_cputimer);
283 SYSCTL_NODE(_kern, OID_AUTO, cputimer, CTLFLAG_RW, NULL, "cputimer");
285 SYSCTL_PROC(_kern_cputimer, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RD,
286 NULL, 0, sysctl_cputimer_reglist, "A", "");
287 SYSCTL_PROC(_kern_cputimer, OID_AUTO, name, CTLTYPE_STRING|CTLFLAG_RD,
288 NULL, 0, sysctl_cputimer_name, "A", "");
289 SYSCTL_PROC(_kern_cputimer, OID_AUTO, clock, CTLTYPE_UINT|CTLFLAG_RD,
290 NULL, 0, sysctl_cputimer_clock, "IU", "");
291 SYSCTL_PROC(_kern_cputimer, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
292 NULL, 0, sysctl_cputimer_freq, "I", "");
294 static struct cputimer_intr *sys_cputimer_intr;
295 static uint32_t cputimer_intr_caps;
296 SLIST_HEAD(, cputimer_intr) cputimer_intr_head =
297 SLIST_HEAD_INITIALIZER(&cputimer_intr_head);
300 cputimer_intr_register(struct cputimer_intr *cti)
302 struct cputimer_intr *scan;
304 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
308 cti->config(cti, sys_cputimer);
309 SLIST_INSERT_HEAD(&cputimer_intr_head, cti, next);
313 cputimer_intr_deregister(struct cputimer_intr *cti)
315 KKASSERT(cti != sys_cputimer_intr);
316 SLIST_REMOVE(&cputimer_intr_head, cti, cputimer_intr, next);
320 cputimer_intr_select(struct cputimer_intr *cti, int prio)
322 KKASSERT(cti != NULL);
327 if (sys_cputimer_intr == NULL) {
328 KKASSERT(cputimer_intr_caps == 0);
329 sys_cputimer_intr = cti;
333 if ((cti->caps & cputimer_intr_caps) == cputimer_intr_caps) {
334 if (prio > sys_cputimer_intr->prio) {
335 sys_cputimer_intr = cti;
346 cputimer_intr_default_enable(struct cputimer_intr *cti __unused)
351 cputimer_intr_default_restart(struct cputimer_intr *cti)
357 cputimer_intr_default_config(struct cputimer_intr *cti __unused,
358 const struct cputimer *timer __unused)
363 cputimer_intr_default_pmfixup(struct cputimer_intr *cti __unused)
368 cputimer_intr_default_initclock(struct cputimer_intr *cti __unused,
369 boolean_t selected __unused)
374 cputimer_intr_enable(void)
376 struct cputimer_intr *cti;
378 SLIST_FOREACH(cti, &cputimer_intr_head, next)
383 cputimer_intr_config(const struct cputimer *timer)
385 struct cputimer_intr *cti;
387 SLIST_FOREACH(cti, &cputimer_intr_head, next)
388 cti->config(cti, timer);
392 cputimer_intr_pmfixup(void)
394 struct cputimer_intr *cti;
396 SLIST_FOREACH(cti, &cputimer_intr_head, next)
401 cputimer_intr_reload(sysclock_t reload)
403 struct cputimer_intr *cti = sys_cputimer_intr;
405 cti->reload(cti, reload);
409 cputimer_intr_restart(void)
411 struct cputimer_intr *cti = sys_cputimer_intr;
417 cputimer_intr_select_caps(uint32_t caps)
419 struct cputimer_intr *cti, *maybe;
423 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
424 if ((cti->caps & caps) == caps) {
427 else if (cti->prio > maybe->prio)
434 if (sys_cputimer_intr == maybe)
437 cputimer_intr_caps = caps;
438 error = cputimer_intr_select(maybe, CPUTIMER_INTR_PRIO_MAX);
445 cputimer_intr_initclocks(void)
447 struct cputimer_intr *cti, *ncti;
450 * An interrupt cputimer may deregister itself,
451 * so use SLIST_FOREACH_MUTABLE here.
453 SLIST_FOREACH_MUTABLE(cti, &cputimer_intr_head, next, ncti) {
454 boolean_t selected = FALSE;
456 if (cti == sys_cputimer_intr)
458 cti->initclock(cti, selected);
461 /* NOTE: Must be SECOND to allow platform initialization to go first */
462 SYSINIT(cputimer_intr, SI_BOOT2_CLOCKREG, SI_ORDER_SECOND,
463 cputimer_intr_initclocks, NULL);
466 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS)
468 struct cputimer_intr *scan;
473 * Build a list of available interrupt cputimers
475 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
476 if (error == 0 && loop)
477 error = SYSCTL_OUT(req, " ", 1);
479 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
486 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS)
490 error = SYSCTL_OUT(req, &sys_cputimer_intr->freq,
491 sizeof(sys_cputimer_intr->freq));
496 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS)
498 struct cputimer_intr *cti;
502 ksnprintf(name, sizeof(name), "%s", sys_cputimer_intr->name);
503 error = sysctl_handle_string(oidp, name, sizeof(name), req);
504 if (error != 0 || req->newptr == NULL)
507 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
508 if (strcmp(cti->name, name) == 0)
513 if (cti == sys_cputimer_intr)
516 error = cputimer_intr_select(cti, CPUTIMER_INTR_PRIO_MAX);
518 cputimer_intr_restart();
522 SYSCTL_NODE(_kern_cputimer, OID_AUTO, intr, CTLFLAG_RW, NULL,
523 "interrupt cputimer");
525 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, reglist, CTLTYPE_STRING|CTLFLAG_RD,
526 NULL, 0, sysctl_cputimer_intr_reglist, "A", "");
527 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
528 NULL, 0, sysctl_cputimer_intr_freq, "I", "");
529 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RW,
530 NULL, 0, sysctl_cputimer_intr_select, "A", "");
533 cputimer_intr_powersave_addreq(void)
537 lwkt_serialize_enter(&cputimer_intr_ps_slize);
539 ++cputimer_intr_ps_reqs;
540 if (cputimer_intr_ps_reqs == 1) {
542 * Upon the first power saving request, switch to an one shot
543 * timer, which would not stop in the any power saving state.
545 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_PS);
546 if (error == ERESTART) {
549 kprintf("cputimer: first power save request, restart\n");
550 cputimer_intr_restart();
552 kprintf("no suitable intr cputimer found\n");
553 --cputimer_intr_ps_reqs;
554 } else if (bootverbose) {
555 kprintf("cputimer: first power save request\n");
559 lwkt_serialize_exit(&cputimer_intr_ps_slize);
565 cputimer_intr_powersave_remreq(void)
567 lwkt_serialize_enter(&cputimer_intr_ps_slize);
569 KASSERT(cputimer_intr_ps_reqs > 0,
570 ("invalid # of powersave reqs %d", cputimer_intr_ps_reqs));
571 --cputimer_intr_ps_reqs;
572 if (cputimer_intr_ps_reqs == 0) {
575 /* No one needs power saving, use a better one shot timer. */
576 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_NONE);
577 KKASSERT(!error || error == ERESTART);
578 if (error == ERESTART) {
580 kprintf("cputimer: no powser save request, restart\n");
581 cputimer_intr_restart();
582 } else if (bootverbose) {
583 kprintf("cputimer: no power save request\n");
587 lwkt_serialize_exit(&cputimer_intr_ps_slize);
591 cputimer_intr_pcpuhand(void)
593 struct cputimer_intr *cti = sys_cputimer_intr;
595 if (cti->pcpuhand != NULL)
600 pcpu_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
604 cputimer_intr_pcpuhand();
606 gd->gd_timer_running = 0;
608 count = sys_cputimer->count();
609 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
610 systimer_intr(&count, 0, frame);
614 pcpu_timer_process(void)
616 pcpu_timer_process_oncpu(mycpu, NULL);
620 pcpu_timer_process_frame(struct intrframe *frame)
622 pcpu_timer_process_oncpu(mycpu, frame);
626 dummy_cpucounter_count(void)
631 return ((tv.tv_sec * 1000000ULL) + tv.tv_usec);
634 const struct cpucounter *
635 cpucounter_find_pcpu(void)
637 const struct cpucounter *cc, *ret;
639 ret = &dummy_cpucounter;
640 SLIST_FOREACH(cc, &cpucounterhead, link) {
641 if (cc->prio > ret->prio)
647 const struct cpucounter *
648 cpucounter_find(void)
650 const struct cpucounter *cc, *ret;
652 ret = &dummy_cpucounter;
653 SLIST_FOREACH(cc, &cpucounterhead, link) {
654 if ((cc->flags & CPUCOUNTER_FLAG_MPSYNC) &&
655 cc->prio > ret->prio)
658 KASSERT(ret->flags & CPUCOUNTER_FLAG_MPSYNC,
659 ("cpucounter %u is not MPsync", ret->type));
664 cpucounter_register(struct cpucounter *cc)
667 SLIST_INSERT_HEAD(&cpucounterhead, cc, link);