kern: Add cpucounter which returns 64bit monotonic counter.
[dragonfly.git] / sys / kern / kern_cputimer.c
blobdb6532202599aed68d57ef79c7857c37794b7181
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:
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.
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.
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 extern void pcpu_timer_process(void);
49 extern void pcpu_timer_process_frame(struct intrframe *);
51 static uint64_t dummy_cpucounter_count(void);
53 static sysclock_t dummy_cputimer_count(void);
55 static struct cputimer dummy_cputimer = {
56 .next = SLIST_ENTRY_INITIALIZER,
57 .name = "dummy",
58 .pri = CPUTIMER_PRI_DUMMY,
59 .type = CPUTIMER_DUMMY,
60 .count = dummy_cputimer_count,
61 .fromhz = cputimer_default_fromhz,
62 .fromus = cputimer_default_fromus,
63 .construct = cputimer_default_construct,
64 .destruct = cputimer_default_destruct,
65 .freq = 1000000,
66 .freq64_usec = (1000000LL << 32) / 1000000,
67 .freq64_nsec = (1000000000LL << 32) / 1000000
70 static struct cpucounter dummy_cpucounter = {
71 .freq = 1000000ULL,
72 .count = dummy_cpucounter_count,
73 .flags = CPUCOUNTER_FLAG_MPSYNC,
74 .prio = CPUCOUNTER_PRIO_DUMMY,
75 .type = CPUCOUNTER_DUMMY
78 struct cputimer *sys_cputimer = &dummy_cputimer;
79 SLIST_HEAD(, cputimer) cputimerhead = SLIST_HEAD_INITIALIZER(&cputimerhead);
81 static SLIST_HEAD(, cpucounter) cpucounterhead =
82 SLIST_HEAD_INITIALIZER(cpucounterhead);
84 static int cputimer_intr_ps_reqs;
85 static struct lwkt_serialize cputimer_intr_ps_slize =
86 LWKT_SERIALIZE_INITIALIZER;
89 * Generic cputimer API
91 void
92 cputimer_select(struct cputimer *timer, int pri)
94 sysclock_t oldclock;
97 * Calculate helper fields
99 cputimer_set_frequency(timer, timer->freq);
102 * Install a new cputimer if its priority allows it. If timer is
103 * passed as NULL we deinstall the current timer and revert to our
104 * dummy.
106 if (pri == 0)
107 pri = timer->pri;
108 if (timer == NULL || pri >= sys_cputimer->pri) {
109 oldclock = sys_cputimer->count();
110 sys_cputimer->destruct(sys_cputimer);
111 sys_cputimer = &dummy_cputimer;
112 if (timer) {
113 sys_cputimer = timer;
114 timer->construct(timer, oldclock);
115 cputimer_intr_config(timer);
121 * Register a timer. If the timer has already been registered, do nothing.
123 void
124 cputimer_register(struct cputimer *timer)
126 struct cputimer *scan;
129 * Initialize dummy_cputimer if the slist is empty, it does not get
130 * registered the normal way.
132 if (SLIST_EMPTY(&cputimerhead))
133 SLIST_FIRST(&cputimerhead) = &dummy_cputimer;
134 SLIST_FOREACH(scan, &cputimerhead, next) {
135 if (scan == timer)
136 return;
138 SLIST_INSERT_HEAD(&cputimerhead, timer, next);
142 * Deregister a timer. If the timer has already been deregistered, do nothing.
144 void
145 cputimer_deregister(struct cputimer *timer)
147 struct cputimer *scan;
148 struct cputimer *best;
151 * Locate and remove the timer. If the timer is our currently active
152 * timer, revert to the dummy timer.
154 SLIST_FOREACH(scan, &cputimerhead, next) {
155 if (timer == scan) {
156 if (timer == sys_cputimer)
157 cputimer_select(&dummy_cputimer, 0x7FFFFFFF);
158 SLIST_REMOVE(&cputimerhead, timer, cputimer, next);
159 break;
164 * If sys_cputimer reverted to the dummy, select the best one
166 if (sys_cputimer == &dummy_cputimer) {
167 best = NULL;
168 SLIST_FOREACH(scan, &cputimerhead, next) {
169 if (best == NULL || scan->pri > best->pri)
170 best = scan;
172 if (best)
173 cputimer_select(best, 0x7FFFFFFF);
178 * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
180 * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
181 * or 3000 nsec / tick x (1 << 32)
183 void
184 cputimer_set_frequency(struct cputimer *timer, sysclock_t freq)
186 timer->freq = freq;
187 timer->freq64_usec = (1000000LL << 32) / freq;
188 timer->freq64_nsec = (1000000000LL << 32) / freq;
189 if (timer == sys_cputimer)
190 cputimer_intr_config(timer);
193 sysclock_t
194 cputimer_default_fromhz(int freq)
196 return(sys_cputimer->freq / freq + 1);
199 sysclock_t
200 cputimer_default_fromus(int us)
202 return((int64_t)sys_cputimer->freq * us / 1000000);
206 * Dummy counter implementation
208 static
209 sysclock_t
210 dummy_cputimer_count(void)
212 return(++dummy_cputimer.base);
215 void
216 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
218 cputimer->base = oldclock;
221 void
222 cputimer_default_destruct(struct cputimer *cputimer)
226 /************************************************************************
227 * SYSCTL SUPPORT *
228 ************************************************************************
230 * Note: the ability to change the systimer is not currently enabled
231 * because it will mess up systimer calculations. You have to live
232 * with what is configured at boot.
234 static int
235 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
237 struct cputimer *scan;
238 int error = 0;
239 int loop = 0;
242 * Build a list of available timers
244 SLIST_FOREACH(scan, &cputimerhead, next) {
245 if (error == 0 && loop)
246 error = SYSCTL_OUT(req, " ", 1);
247 if (error == 0)
248 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
249 ++loop;
251 return (error);
254 static int
255 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
257 int error;
259 error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
260 return (error);
263 static int
264 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
266 sysclock_t clock;
267 int error;
269 clock = sys_cputimer->count();
270 error = SYSCTL_OUT(req, &clock, sizeof(clock));
271 return (error);
274 static int
275 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
277 int error;
279 error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
280 return (error);
283 SYSCTL_DECL(_kern_cputimer);
284 SYSCTL_NODE(_kern, OID_AUTO, cputimer, CTLFLAG_RW, NULL, "cputimer");
286 SYSCTL_PROC(_kern_cputimer, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RD,
287 NULL, 0, sysctl_cputimer_reglist, "A", "");
288 SYSCTL_PROC(_kern_cputimer, OID_AUTO, name, CTLTYPE_STRING|CTLFLAG_RD,
289 NULL, 0, sysctl_cputimer_name, "A", "");
290 SYSCTL_PROC(_kern_cputimer, OID_AUTO, clock, CTLTYPE_UINT|CTLFLAG_RD,
291 NULL, 0, sysctl_cputimer_clock, "IU", "");
292 SYSCTL_PROC(_kern_cputimer, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
293 NULL, 0, sysctl_cputimer_freq, "I", "");
295 static struct cputimer_intr *sys_cputimer_intr;
296 static uint32_t cputimer_intr_caps;
297 SLIST_HEAD(, cputimer_intr) cputimer_intr_head =
298 SLIST_HEAD_INITIALIZER(&cputimer_intr_head);
300 void
301 cputimer_intr_register(struct cputimer_intr *cti)
303 struct cputimer_intr *scan;
305 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
306 if (scan == cti)
307 return;
309 cti->config(cti, sys_cputimer);
310 SLIST_INSERT_HEAD(&cputimer_intr_head, cti, next);
313 void
314 cputimer_intr_deregister(struct cputimer_intr *cti)
316 KKASSERT(cti != sys_cputimer_intr);
317 SLIST_REMOVE(&cputimer_intr_head, cti, cputimer_intr, next);
321 cputimer_intr_select(struct cputimer_intr *cti, int prio)
323 KKASSERT(cti != NULL);
325 if (prio == 0)
326 prio = cti->prio;
328 if (sys_cputimer_intr == NULL) {
329 KKASSERT(cputimer_intr_caps == 0);
330 sys_cputimer_intr = cti;
331 return 0;
334 if ((cti->caps & cputimer_intr_caps) == cputimer_intr_caps) {
335 if (prio > sys_cputimer_intr->prio) {
336 sys_cputimer_intr = cti;
337 return 0;
338 } else {
339 return EBUSY;
341 } else {
342 return EOPNOTSUPP;
346 void
347 cputimer_intr_default_enable(struct cputimer_intr *cti __unused)
351 void
352 cputimer_intr_default_restart(struct cputimer_intr *cti)
354 cti->reload(cti, 0);
357 void
358 cputimer_intr_default_config(struct cputimer_intr *cti __unused,
359 const struct cputimer *timer __unused)
363 void
364 cputimer_intr_default_pmfixup(struct cputimer_intr *cti __unused)
368 void
369 cputimer_intr_default_initclock(struct cputimer_intr *cti __unused,
370 boolean_t selected __unused)
374 void
375 cputimer_intr_enable(void)
377 struct cputimer_intr *cti;
379 SLIST_FOREACH(cti, &cputimer_intr_head, next)
380 cti->enable(cti);
383 void
384 cputimer_intr_config(const struct cputimer *timer)
386 struct cputimer_intr *cti;
388 SLIST_FOREACH(cti, &cputimer_intr_head, next)
389 cti->config(cti, timer);
392 void
393 cputimer_intr_pmfixup(void)
395 struct cputimer_intr *cti;
397 SLIST_FOREACH(cti, &cputimer_intr_head, next)
398 cti->pmfixup(cti);
401 void
402 cputimer_intr_reload(sysclock_t reload)
404 struct cputimer_intr *cti = sys_cputimer_intr;
406 cti->reload(cti, reload);
409 void
410 cputimer_intr_restart(void)
412 struct cputimer_intr *cti = sys_cputimer_intr;
414 cti->restart(cti);
418 cputimer_intr_select_caps(uint32_t caps)
420 struct cputimer_intr *cti, *maybe;
421 int error;
423 maybe = NULL;
424 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
425 if ((cti->caps & caps) == caps) {
426 if (maybe == NULL)
427 maybe = cti;
428 else if (cti->prio > maybe->prio)
429 maybe = cti;
432 if (maybe == NULL)
433 return ENOENT;
435 if (sys_cputimer_intr == maybe)
436 return 0;
438 cputimer_intr_caps = caps;
439 error = cputimer_intr_select(maybe, CPUTIMER_INTR_PRIO_MAX);
440 KKASSERT(!error);
442 return ERESTART;
445 static void
446 cputimer_intr_initclocks(void)
448 struct cputimer_intr *cti, *ncti;
451 * An interrupt cputimer may deregister itself,
452 * so use SLIST_FOREACH_MUTABLE here.
454 SLIST_FOREACH_MUTABLE(cti, &cputimer_intr_head, next, ncti) {
455 boolean_t selected = FALSE;
457 if (cti == sys_cputimer_intr)
458 selected = TRUE;
459 cti->initclock(cti, selected);
462 /* NOTE: Must be SECOND to allow platform initialization to go first */
463 SYSINIT(cputimer_intr, SI_BOOT2_CLOCKREG, SI_ORDER_SECOND,
464 cputimer_intr_initclocks, NULL);
466 static int
467 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS)
469 struct cputimer_intr *scan;
470 int error = 0;
471 int loop = 0;
474 * Build a list of available interrupt cputimers
476 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
477 if (error == 0 && loop)
478 error = SYSCTL_OUT(req, " ", 1);
479 if (error == 0)
480 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
481 ++loop;
483 return (error);
486 static int
487 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS)
489 int error;
491 error = SYSCTL_OUT(req, &sys_cputimer_intr->freq,
492 sizeof(sys_cputimer_intr->freq));
493 return (error);
496 static int
497 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS)
499 struct cputimer_intr *cti;
500 char name[32];
501 int error;
503 ksnprintf(name, sizeof(name), "%s", sys_cputimer_intr->name);
504 error = sysctl_handle_string(oidp, name, sizeof(name), req);
505 if (error != 0 || req->newptr == NULL)
506 return error;
508 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
509 if (strcmp(cti->name, name) == 0)
510 break;
512 if (cti == NULL)
513 return ENOENT;
514 if (cti == sys_cputimer_intr)
515 return 0;
517 error = cputimer_intr_select(cti, CPUTIMER_INTR_PRIO_MAX);
518 if (!error)
519 cputimer_intr_restart();
520 return error;
523 SYSCTL_NODE(_kern_cputimer, OID_AUTO, intr, CTLFLAG_RW, NULL,
524 "interrupt cputimer");
526 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, reglist, CTLTYPE_STRING|CTLFLAG_RD,
527 NULL, 0, sysctl_cputimer_intr_reglist, "A", "");
528 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
529 NULL, 0, sysctl_cputimer_intr_freq, "I", "");
530 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RW,
531 NULL, 0, sysctl_cputimer_intr_select, "A", "");
534 cputimer_intr_powersave_addreq(void)
536 int error = 0;
538 lwkt_serialize_enter(&cputimer_intr_ps_slize);
540 ++cputimer_intr_ps_reqs;
541 if (cputimer_intr_ps_reqs == 1) {
543 * Upon the first power saving request, switch to an one shot
544 * timer, which would not stop in the any power saving state.
546 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_PS);
547 if (error == ERESTART) {
548 error = 0;
549 if (bootverbose)
550 kprintf("cputimer: first power save request, restart\n");
551 cputimer_intr_restart();
552 } else if (error) {
553 kprintf("no suitable intr cputimer found\n");
554 --cputimer_intr_ps_reqs;
555 } else if (bootverbose) {
556 kprintf("cputimer: first power save request\n");
560 lwkt_serialize_exit(&cputimer_intr_ps_slize);
562 return error;
565 void
566 cputimer_intr_powersave_remreq(void)
568 lwkt_serialize_enter(&cputimer_intr_ps_slize);
570 KASSERT(cputimer_intr_ps_reqs > 0,
571 ("invalid # of powersave reqs %d", cputimer_intr_ps_reqs));
572 --cputimer_intr_ps_reqs;
573 if (cputimer_intr_ps_reqs == 0) {
574 int error;
576 /* No one needs power saving, use a better one shot timer. */
577 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_NONE);
578 KKASSERT(!error || error == ERESTART);
579 if (error == ERESTART) {
580 if (bootverbose)
581 kprintf("cputimer: no powser save request, restart\n");
582 cputimer_intr_restart();
583 } else if (bootverbose) {
584 kprintf("cputimer: no power save request\n");
588 lwkt_serialize_exit(&cputimer_intr_ps_slize);
591 static __inline void
592 cputimer_intr_pcpuhand(void)
594 struct cputimer_intr *cti = sys_cputimer_intr;
596 if (cti->pcpuhand != NULL)
597 cti->pcpuhand(cti);
600 static void
601 pcpu_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
603 sysclock_t count;
605 cputimer_intr_pcpuhand();
607 gd->gd_timer_running = 0;
609 count = sys_cputimer->count();
610 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
611 systimer_intr(&count, 0, frame);
614 void
615 pcpu_timer_process(void)
617 pcpu_timer_process_oncpu(mycpu, NULL);
620 void
621 pcpu_timer_process_frame(struct intrframe *frame)
623 pcpu_timer_process_oncpu(mycpu, frame);
626 static uint64_t
627 dummy_cpucounter_count(void)
629 struct timeval tv;
631 microuptime(&tv);
632 return ((tv.tv_sec * 1000000ULL) + tv.tv_usec);
635 const struct cpucounter *
636 cpucounter_find_pcpu(void)
638 const struct cpucounter *cc, *ret;
640 ret = &dummy_cpucounter;
641 SLIST_FOREACH(cc, &cpucounterhead, link) {
642 if (cc->prio > ret->prio)
643 ret = cc;
645 return (ret);
648 const struct cpucounter *
649 cpucounter_find(void)
651 const struct cpucounter *cc, *ret;
653 ret = &dummy_cpucounter;
654 SLIST_FOREACH(cc, &cpucounterhead, link) {
655 if ((cc->flags & CPUCOUNTER_FLAG_MPSYNC) &&
656 cc->prio > ret->prio)
657 ret = cc;
659 KASSERT(ret->flags & CPUCOUNTER_FLAG_MPSYNC,
660 ("cpucounter %u is not MPsync", ret->type));
661 return (ret);
664 void
665 cpucounter_register(struct cpucounter *cc)
668 SLIST_INSERT_HEAD(&cpucounterhead, cc, link);