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 extern void pcpu_timer_process(void);
49 extern void pcpu_timer_process_frame(struct intrframe *);
51 static sysclock_t dummy_cputimer_count(void);
53 static struct cputimer dummy_cputimer = {
54 .next = SLIST_ENTRY_INITIALIZER,
56 .pri = CPUTIMER_PRI_DUMMY,
57 .type = CPUTIMER_DUMMY,
58 .count = dummy_cputimer_count,
59 .fromhz = cputimer_default_fromhz,
60 .fromus = cputimer_default_fromus,
61 .construct = cputimer_default_construct,
62 .destruct = cputimer_default_destruct,
64 .freq64_usec = (1000000LL << 32) / 1000000,
65 .freq64_nsec = (1000000000LL << 32) / 1000000
68 struct cputimer *sys_cputimer = &dummy_cputimer;
69 SLIST_HEAD(, cputimer) cputimerhead = SLIST_HEAD_INITIALIZER(&cputimerhead);
71 static int cputimer_intr_ps_reqs;
72 static struct lwkt_serialize cputimer_intr_ps_slize =
73 LWKT_SERIALIZE_INITIALIZER;
76 * Generic cputimer API
79 cputimer_select(struct cputimer *timer, int pri)
84 * Calculate helper fields
86 cputimer_set_frequency(timer, timer->freq);
89 * Install a new cputimer if its priority allows it. If timer is
90 * passed as NULL we deinstall the current timer and revert to our
95 if (timer == NULL || pri >= sys_cputimer->pri) {
96 oldclock = sys_cputimer->count();
97 sys_cputimer->destruct(sys_cputimer);
98 sys_cputimer = &dummy_cputimer;
100 sys_cputimer = timer;
101 timer->construct(timer, oldclock);
102 cputimer_intr_config(timer);
108 * Register a timer. If the timer has already been registered, do nothing.
111 cputimer_register(struct cputimer *timer)
113 struct cputimer *scan;
116 * Initialize dummy_cputimer if the slist is empty, it does not get
117 * registered the normal way.
119 if (SLIST_EMPTY(&cputimerhead))
120 SLIST_FIRST(&cputimerhead) = &dummy_cputimer;
121 SLIST_FOREACH(scan, &cputimerhead, next) {
125 SLIST_INSERT_HEAD(&cputimerhead, timer, next);
129 * Deregister a timer. If the timer has already been deregistered, do nothing.
132 cputimer_deregister(struct cputimer *timer)
134 struct cputimer *scan;
135 struct cputimer *best;
138 * Locate and remove the timer. If the timer is our currently active
139 * timer, revert to the dummy timer.
141 SLIST_FOREACH(scan, &cputimerhead, next) {
143 if (timer == sys_cputimer)
144 cputimer_select(&dummy_cputimer, 0x7FFFFFFF);
145 SLIST_REMOVE(&cputimerhead, timer, cputimer, next);
151 * If sys_cputimer reverted to the dummy, select the best one
153 if (sys_cputimer == &dummy_cputimer) {
155 SLIST_FOREACH(scan, &cputimerhead, next) {
156 if (best == NULL || scan->pri > best->pri)
160 cputimer_select(best, 0x7FFFFFFF);
165 * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
167 * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
168 * or 3000 nsec / tick x (1 << 32)
171 cputimer_set_frequency(struct cputimer *timer, sysclock_t freq)
174 timer->freq64_usec = (1000000LL << 32) / freq;
175 timer->freq64_nsec = (1000000000LL << 32) / freq;
176 if (timer == sys_cputimer)
177 cputimer_intr_config(timer);
181 cputimer_default_fromhz(int freq)
183 return(sys_cputimer->freq / freq + 1);
187 cputimer_default_fromus(int us)
189 return((int64_t)sys_cputimer->freq * us / 1000000);
193 * Dummy counter implementation
197 dummy_cputimer_count(void)
199 return(++dummy_cputimer.base);
203 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
205 cputimer->base = oldclock;
209 cputimer_default_destruct(struct cputimer *cputimer)
213 /************************************************************************
215 ************************************************************************
217 * Note: the ability to change the systimer is not currently enabled
218 * because it will mess up systimer calculations. You have to live
219 * with what is configured at boot.
222 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
224 struct cputimer *scan;
229 * Build a list of available timers
231 SLIST_FOREACH(scan, &cputimerhead, next) {
232 if (error == 0 && loop)
233 error = SYSCTL_OUT(req, " ", 1);
235 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
242 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
246 error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
251 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
256 clock = sys_cputimer->count();
257 error = SYSCTL_OUT(req, &clock, sizeof(clock));
262 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
266 error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
270 SYSCTL_DECL(_kern_cputimer);
271 SYSCTL_NODE(_kern, OID_AUTO, cputimer, CTLFLAG_RW, NULL, "cputimer");
273 SYSCTL_PROC(_kern_cputimer, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RD,
274 NULL, 0, sysctl_cputimer_reglist, "A", "");
275 SYSCTL_PROC(_kern_cputimer, OID_AUTO, name, CTLTYPE_STRING|CTLFLAG_RD,
276 NULL, 0, sysctl_cputimer_name, "A", "");
277 SYSCTL_PROC(_kern_cputimer, OID_AUTO, clock, CTLTYPE_UINT|CTLFLAG_RD,
278 NULL, 0, sysctl_cputimer_clock, "IU", "");
279 SYSCTL_PROC(_kern_cputimer, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
280 NULL, 0, sysctl_cputimer_freq, "I", "");
282 static struct cputimer_intr *sys_cputimer_intr;
283 static uint32_t cputimer_intr_caps;
284 SLIST_HEAD(, cputimer_intr) cputimer_intr_head =
285 SLIST_HEAD_INITIALIZER(&cputimer_intr_head);
288 cputimer_intr_register(struct cputimer_intr *cti)
290 struct cputimer_intr *scan;
292 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
296 cti->config(cti, sys_cputimer);
297 SLIST_INSERT_HEAD(&cputimer_intr_head, cti, next);
301 cputimer_intr_deregister(struct cputimer_intr *cti)
303 KKASSERT(cti != sys_cputimer_intr);
304 SLIST_REMOVE(&cputimer_intr_head, cti, cputimer_intr, next);
308 cputimer_intr_select(struct cputimer_intr *cti, int prio)
310 KKASSERT(cti != NULL);
315 if (sys_cputimer_intr == NULL) {
316 KKASSERT(cputimer_intr_caps == 0);
317 sys_cputimer_intr = cti;
321 if ((cti->caps & cputimer_intr_caps) == cputimer_intr_caps) {
322 if (prio > sys_cputimer_intr->prio) {
323 sys_cputimer_intr = cti;
334 cputimer_intr_default_enable(struct cputimer_intr *cti __unused)
339 cputimer_intr_default_restart(struct cputimer_intr *cti)
345 cputimer_intr_default_config(struct cputimer_intr *cti __unused,
346 const struct cputimer *timer __unused)
351 cputimer_intr_default_pmfixup(struct cputimer_intr *cti __unused)
356 cputimer_intr_default_initclock(struct cputimer_intr *cti __unused,
357 boolean_t selected __unused)
362 cputimer_intr_enable(void)
364 struct cputimer_intr *cti;
366 SLIST_FOREACH(cti, &cputimer_intr_head, next)
371 cputimer_intr_config(const struct cputimer *timer)
373 struct cputimer_intr *cti;
375 SLIST_FOREACH(cti, &cputimer_intr_head, next)
376 cti->config(cti, timer);
380 cputimer_intr_pmfixup(void)
382 struct cputimer_intr *cti;
384 SLIST_FOREACH(cti, &cputimer_intr_head, next)
389 cputimer_intr_reload(sysclock_t reload)
391 struct cputimer_intr *cti = sys_cputimer_intr;
393 cti->reload(cti, reload);
397 cputimer_intr_restart(void)
399 struct cputimer_intr *cti = sys_cputimer_intr;
405 cputimer_intr_select_caps(uint32_t caps)
407 struct cputimer_intr *cti, *maybe;
411 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
412 if ((cti->caps & caps) == caps) {
415 else if (cti->prio > maybe->prio)
422 if (sys_cputimer_intr == maybe)
425 cputimer_intr_caps = caps;
426 error = cputimer_intr_select(maybe, CPUTIMER_INTR_PRIO_MAX);
433 cputimer_intr_initclocks(void)
435 struct cputimer_intr *cti, *ncti;
438 * An interrupt cputimer may deregister itself,
439 * so use SLIST_FOREACH_MUTABLE here.
441 SLIST_FOREACH_MUTABLE(cti, &cputimer_intr_head, next, ncti) {
442 boolean_t selected = FALSE;
444 if (cti == sys_cputimer_intr)
446 cti->initclock(cti, selected);
449 /* NOTE: Must be SECOND to allow platform initialization to go first */
450 SYSINIT(cputimer_intr, SI_BOOT2_CLOCKREG, SI_ORDER_SECOND,
451 cputimer_intr_initclocks, NULL);
454 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS)
456 struct cputimer_intr *scan;
461 * Build a list of available interrupt cputimers
463 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
464 if (error == 0 && loop)
465 error = SYSCTL_OUT(req, " ", 1);
467 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
474 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS)
478 error = SYSCTL_OUT(req, &sys_cputimer_intr->freq,
479 sizeof(sys_cputimer_intr->freq));
484 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS)
486 struct cputimer_intr *cti;
490 ksnprintf(name, sizeof(name), "%s", sys_cputimer_intr->name);
491 error = sysctl_handle_string(oidp, name, sizeof(name), req);
492 if (error != 0 || req->newptr == NULL)
495 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
496 if (strcmp(cti->name, name) == 0)
501 if (cti == sys_cputimer_intr)
504 error = cputimer_intr_select(cti, CPUTIMER_INTR_PRIO_MAX);
506 cputimer_intr_restart();
510 SYSCTL_NODE(_kern_cputimer, OID_AUTO, intr, CTLFLAG_RW, NULL,
511 "interrupt cputimer");
513 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, reglist, CTLTYPE_STRING|CTLFLAG_RD,
514 NULL, 0, sysctl_cputimer_intr_reglist, "A", "");
515 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, freq, CTLTYPE_INT|CTLFLAG_RD,
516 NULL, 0, sysctl_cputimer_intr_freq, "I", "");
517 SYSCTL_PROC(_kern_cputimer_intr, OID_AUTO, select, CTLTYPE_STRING|CTLFLAG_RW,
518 NULL, 0, sysctl_cputimer_intr_select, "A", "");
521 cputimer_intr_powersave_addreq(void)
525 lwkt_serialize_enter(&cputimer_intr_ps_slize);
527 ++cputimer_intr_ps_reqs;
528 if (cputimer_intr_ps_reqs == 1) {
530 * Upon the first power saving request, switch to an one shot
531 * timer, which would not stop in the any power saving state.
533 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_PS);
534 if (error == ERESTART) {
537 kprintf("cputimer: first power save request, restart\n");
538 cputimer_intr_restart();
540 kprintf("no suitable intr cputimer found\n");
541 --cputimer_intr_ps_reqs;
542 } else if (bootverbose) {
543 kprintf("cputimer: first power save request\n");
547 lwkt_serialize_exit(&cputimer_intr_ps_slize);
553 cputimer_intr_powersave_remreq(void)
555 lwkt_serialize_enter(&cputimer_intr_ps_slize);
557 KASSERT(cputimer_intr_ps_reqs > 0,
558 ("invalid # of powersave reqs %d", cputimer_intr_ps_reqs));
559 --cputimer_intr_ps_reqs;
560 if (cputimer_intr_ps_reqs == 0) {
563 /* No one needs power saving, use a better one shot timer. */
564 error = cputimer_intr_select_caps(CPUTIMER_INTR_CAP_NONE);
565 KKASSERT(!error || error == ERESTART);
566 if (error == ERESTART) {
568 kprintf("cputimer: no powser save request, restart\n");
569 cputimer_intr_restart();
570 } else if (bootverbose) {
571 kprintf("cputimer: no power save request\n");
575 lwkt_serialize_exit(&cputimer_intr_ps_slize);
579 cputimer_intr_pcpuhand(void)
581 struct cputimer_intr *cti = sys_cputimer_intr;
583 if (cti->pcpuhand != NULL)
588 pcpu_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
592 cputimer_intr_pcpuhand();
594 gd->gd_timer_running = 0;
596 count = sys_cputimer->count();
597 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
598 systimer_intr(&count, 0, frame);
602 pcpu_timer_process(void)
604 pcpu_timer_process_oncpu(mycpu, NULL);
608 pcpu_timer_process_frame(struct intrframe *frame)
610 pcpu_timer_process_oncpu(mycpu, frame);