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 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
,
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
,
66 .freq64_usec
= (1000000LL << 32) / 1000000,
67 .freq64_nsec
= (1000000000LL << 32) / 1000000
70 static struct cpucounter dummy_cpucounter
= {
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
92 cputimer_select(struct cputimer
*timer
, int pri
)
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
108 if (timer
== NULL
|| pri
>= sys_cputimer
->pri
) {
109 oldclock
= sys_cputimer
->count();
110 sys_cputimer
->destruct(sys_cputimer
);
111 sys_cputimer
= &dummy_cputimer
;
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.
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
) {
138 SLIST_INSERT_HEAD(&cputimerhead
, timer
, next
);
142 * Deregister a timer. If the timer has already been deregistered, do nothing.
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
) {
156 if (timer
== sys_cputimer
)
157 cputimer_select(&dummy_cputimer
, 0x7FFFFFFF);
158 SLIST_REMOVE(&cputimerhead
, timer
, cputimer
, next
);
164 * If sys_cputimer reverted to the dummy, select the best one
166 if (sys_cputimer
== &dummy_cputimer
) {
168 SLIST_FOREACH(scan
, &cputimerhead
, next
) {
169 if (best
== NULL
|| scan
->pri
> best
->pri
)
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)
184 cputimer_set_frequency(struct cputimer
*timer
, sysclock_t 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
);
194 cputimer_default_fromhz(int freq
)
196 return(sys_cputimer
->freq
/ freq
+ 1);
200 cputimer_default_fromus(int us
)
202 return((int64_t)sys_cputimer
->freq
* us
/ 1000000);
206 * Dummy counter implementation
210 dummy_cputimer_count(void)
212 return(++dummy_cputimer
.base
);
216 cputimer_default_construct(struct cputimer
*cputimer
, sysclock_t oldclock
)
218 cputimer
->base
= oldclock
;
222 cputimer_default_destruct(struct cputimer
*cputimer
)
226 /************************************************************************
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.
235 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS
)
237 struct cputimer
*scan
;
242 * Build a list of available timers
244 SLIST_FOREACH(scan
, &cputimerhead
, next
) {
245 if (error
== 0 && loop
)
246 error
= SYSCTL_OUT(req
, " ", 1);
248 error
= SYSCTL_OUT(req
, scan
->name
, strlen(scan
->name
));
255 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS
)
259 error
= SYSCTL_OUT(req
, sys_cputimer
->name
, strlen(sys_cputimer
->name
));
264 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS
)
269 clock
= sys_cputimer
->count();
270 error
= SYSCTL_OUT(req
, &clock
, sizeof(clock
));
275 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS
)
279 error
= SYSCTL_OUT(req
, &sys_cputimer
->freq
, sizeof(sys_cputimer
->freq
));
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
);
301 cputimer_intr_register(struct cputimer_intr
*cti
)
303 struct cputimer_intr
*scan
;
305 SLIST_FOREACH(scan
, &cputimer_intr_head
, next
) {
309 cti
->config(cti
, sys_cputimer
);
310 SLIST_INSERT_HEAD(&cputimer_intr_head
, cti
, next
);
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
);
328 if (sys_cputimer_intr
== NULL
) {
329 KKASSERT(cputimer_intr_caps
== 0);
330 sys_cputimer_intr
= cti
;
334 if ((cti
->caps
& cputimer_intr_caps
) == cputimer_intr_caps
) {
335 if (prio
> sys_cputimer_intr
->prio
) {
336 sys_cputimer_intr
= cti
;
347 cputimer_intr_default_enable(struct cputimer_intr
*cti __unused
)
352 cputimer_intr_default_restart(struct cputimer_intr
*cti
)
358 cputimer_intr_default_config(struct cputimer_intr
*cti __unused
,
359 const struct cputimer
*timer __unused
)
364 cputimer_intr_default_pmfixup(struct cputimer_intr
*cti __unused
)
369 cputimer_intr_default_initclock(struct cputimer_intr
*cti __unused
,
370 boolean_t selected __unused
)
375 cputimer_intr_enable(void)
377 struct cputimer_intr
*cti
;
379 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
)
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
);
393 cputimer_intr_pmfixup(void)
395 struct cputimer_intr
*cti
;
397 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
)
402 cputimer_intr_reload(sysclock_t reload
)
404 struct cputimer_intr
*cti
= sys_cputimer_intr
;
406 cti
->reload(cti
, reload
);
410 cputimer_intr_restart(void)
412 struct cputimer_intr
*cti
= sys_cputimer_intr
;
418 cputimer_intr_select_caps(uint32_t caps
)
420 struct cputimer_intr
*cti
, *maybe
;
424 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
) {
425 if ((cti
->caps
& caps
) == caps
) {
428 else if (cti
->prio
> maybe
->prio
)
435 if (sys_cputimer_intr
== maybe
)
438 cputimer_intr_caps
= caps
;
439 error
= cputimer_intr_select(maybe
, CPUTIMER_INTR_PRIO_MAX
);
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
)
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
);
467 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS
)
469 struct cputimer_intr
*scan
;
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);
480 error
= SYSCTL_OUT(req
, scan
->name
, strlen(scan
->name
));
487 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS
)
491 error
= SYSCTL_OUT(req
, &sys_cputimer_intr
->freq
,
492 sizeof(sys_cputimer_intr
->freq
));
497 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS
)
499 struct cputimer_intr
*cti
;
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
)
508 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
) {
509 if (strcmp(cti
->name
, name
) == 0)
514 if (cti
== sys_cputimer_intr
)
517 error
= cputimer_intr_select(cti
, CPUTIMER_INTR_PRIO_MAX
);
519 cputimer_intr_restart();
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)
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
) {
550 kprintf("cputimer: first power save request, restart\n");
551 cputimer_intr_restart();
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
);
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) {
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
) {
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
);
592 cputimer_intr_pcpuhand(void)
594 struct cputimer_intr
*cti
= sys_cputimer_intr
;
596 if (cti
->pcpuhand
!= NULL
)
601 pcpu_timer_process_oncpu(struct globaldata
*gd
, struct intrframe
*frame
)
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
);
615 pcpu_timer_process(void)
617 pcpu_timer_process_oncpu(mycpu
, NULL
);
621 pcpu_timer_process_frame(struct intrframe
*frame
)
623 pcpu_timer_process_oncpu(mycpu
, frame
);
627 dummy_cpucounter_count(void)
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
)
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
)
659 KASSERT(ret
->flags
& CPUCOUNTER_FLAG_MPSYNC
,
660 ("cpucounter %u is not MPsync", ret
->type
));
665 cpucounter_register(struct cpucounter
*cc
)
668 SLIST_INSERT_HEAD(&cpucounterhead
, cc
, link
);