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 SLIST_ENTRY_INITIALIZER
,
59 cputimer_default_fromhz
,
60 cputimer_default_fromus
,
61 cputimer_default_construct
,
62 cputimer_default_destruct
,
64 (1000000LL << 32) / 1000000,
65 (1000000000LL << 32) / 1000000,
69 struct cputimer
*sys_cputimer
= &dummy_cputimer
;
70 SLIST_HEAD(, cputimer
) cputimerhead
= SLIST_HEAD_INITIALIZER(&cputimerhead
);
72 static int cputimer_intr_ps_reqs
;
73 static struct lwkt_serialize cputimer_intr_ps_slize
=
74 LWKT_SERIALIZE_INITIALIZER
;
77 * Generic cputimer API
80 cputimer_select(struct cputimer
*timer
, int pri
)
85 * Calculate helper fields
87 cputimer_set_frequency(timer
, timer
->freq
);
90 * Install a new cputimer if its priority allows it. If timer is
91 * passed as NULL we deinstall the current timer and revert to our
96 if (timer
== NULL
|| pri
>= sys_cputimer
->pri
) {
97 oldclock
= sys_cputimer
->count();
98 sys_cputimer
->destruct(sys_cputimer
);
99 sys_cputimer
= &dummy_cputimer
;
101 sys_cputimer
= timer
;
102 timer
->construct(timer
, oldclock
);
103 cputimer_intr_config(timer
);
109 * Register a timer. If the timer has already been registered, do nothing.
112 cputimer_register(struct cputimer
*timer
)
114 struct cputimer
*scan
;
117 * Initialize dummy_cputimer if the slist is empty, it does not get
118 * registered the normal way.
120 if (SLIST_EMPTY(&cputimerhead
))
121 SLIST_FIRST(&cputimerhead
) = &dummy_cputimer
;
122 SLIST_FOREACH(scan
, &cputimerhead
, next
) {
126 SLIST_INSERT_HEAD(&cputimerhead
, timer
, next
);
130 * Deregister a timer. If the timer has already been deregistered, do nothing.
133 cputimer_deregister(struct cputimer
*timer
)
135 struct cputimer
*scan
;
136 struct cputimer
*best
;
139 * Locate and remove the timer. If the timer is our currently active
140 * timer, revert to the dummy timer.
142 SLIST_FOREACH(scan
, &cputimerhead
, next
) {
144 if (timer
== sys_cputimer
)
145 cputimer_select(&dummy_cputimer
, 0x7FFFFFFF);
146 SLIST_REMOVE(&cputimerhead
, timer
, cputimer
, next
);
152 * If sys_cputimer reverted to the dummy, select the best one
154 if (sys_cputimer
== &dummy_cputimer
) {
156 SLIST_FOREACH(scan
, &cputimerhead
, next
) {
157 if (best
== NULL
|| scan
->pri
> best
->pri
)
161 cputimer_select(best
, 0x7FFFFFFF);
166 * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
168 * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
169 * or 3000 nsec / tick x (1 << 32)
172 cputimer_set_frequency(struct cputimer
*timer
, sysclock_t freq
)
175 timer
->freq64_usec
= (1000000LL << 32) / freq
;
176 timer
->freq64_nsec
= (1000000000LL << 32) / freq
;
177 if (timer
== sys_cputimer
)
178 cputimer_intr_config(timer
);
182 cputimer_default_fromhz(int freq
)
184 return(sys_cputimer
->freq
/ freq
+ 1);
188 cputimer_default_fromus(int us
)
190 return((int64_t)sys_cputimer
->freq
* us
/ 1000000);
194 * Dummy counter implementation
198 dummy_cputimer_count(void)
200 return(++dummy_cputimer
.base
);
204 cputimer_default_construct(struct cputimer
*cputimer
, sysclock_t oldclock
)
206 cputimer
->base
= oldclock
;
210 cputimer_default_destruct(struct cputimer
*cputimer
)
214 /************************************************************************
216 ************************************************************************
218 * Note: the ability to change the systimer is not currently enabled
219 * because it will mess up systimer calculations. You have to live
220 * with what is configured at boot.
223 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS
)
225 struct cputimer
*scan
;
230 * Build a list of available timers
232 SLIST_FOREACH(scan
, &cputimerhead
, next
) {
233 if (error
== 0 && loop
)
234 error
= SYSCTL_OUT(req
, " ", 1);
236 error
= SYSCTL_OUT(req
, scan
->name
, strlen(scan
->name
));
243 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS
)
247 error
= SYSCTL_OUT(req
, sys_cputimer
->name
, strlen(sys_cputimer
->name
));
252 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS
)
257 clock
= sys_cputimer
->count();
258 error
= SYSCTL_OUT(req
, &clock
, sizeof(clock
));
263 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS
)
267 error
= SYSCTL_OUT(req
, &sys_cputimer
->freq
, sizeof(sys_cputimer
->freq
));
271 SYSCTL_DECL(_kern_cputimer
);
272 SYSCTL_NODE(_kern
, OID_AUTO
, cputimer
, CTLFLAG_RW
, NULL
, "cputimer");
274 SYSCTL_PROC(_kern_cputimer
, OID_AUTO
, select
, CTLTYPE_STRING
|CTLFLAG_RD
,
275 NULL
, 0, sysctl_cputimer_reglist
, "A", "");
276 SYSCTL_PROC(_kern_cputimer
, OID_AUTO
, name
, CTLTYPE_STRING
|CTLFLAG_RD
,
277 NULL
, 0, sysctl_cputimer_name
, "A", "");
278 SYSCTL_PROC(_kern_cputimer
, OID_AUTO
, clock
, CTLTYPE_UINT
|CTLFLAG_RD
,
279 NULL
, 0, sysctl_cputimer_clock
, "IU", "");
280 SYSCTL_PROC(_kern_cputimer
, OID_AUTO
, freq
, CTLTYPE_INT
|CTLFLAG_RD
,
281 NULL
, 0, sysctl_cputimer_freq
, "I", "");
283 static struct cputimer_intr
*sys_cputimer_intr
;
284 static uint32_t cputimer_intr_caps
;
285 SLIST_HEAD(, cputimer_intr
) cputimer_intr_head
=
286 SLIST_HEAD_INITIALIZER(&cputimer_intr_head
);
289 cputimer_intr_register(struct cputimer_intr
*cti
)
291 struct cputimer_intr
*scan
;
293 SLIST_FOREACH(scan
, &cputimer_intr_head
, next
) {
297 cti
->config(cti
, sys_cputimer
);
298 SLIST_INSERT_HEAD(&cputimer_intr_head
, cti
, next
);
302 cputimer_intr_deregister(struct cputimer_intr
*cti
)
304 KKASSERT(cti
!= sys_cputimer_intr
);
305 SLIST_REMOVE(&cputimer_intr_head
, cti
, cputimer_intr
, next
);
309 cputimer_intr_select(struct cputimer_intr
*cti
, int prio
)
311 KKASSERT(cti
!= NULL
);
316 if (sys_cputimer_intr
== NULL
) {
317 KKASSERT(cputimer_intr_caps
== 0);
318 sys_cputimer_intr
= cti
;
322 if ((cti
->caps
& cputimer_intr_caps
) == cputimer_intr_caps
) {
323 if (prio
> sys_cputimer_intr
->prio
) {
324 sys_cputimer_intr
= cti
;
335 cputimer_intr_default_enable(struct cputimer_intr
*cti __unused
)
340 cputimer_intr_default_restart(struct cputimer_intr
*cti
)
346 cputimer_intr_default_config(struct cputimer_intr
*cti __unused
,
347 const struct cputimer
*timer __unused
)
352 cputimer_intr_default_pmfixup(struct cputimer_intr
*cti __unused
)
357 cputimer_intr_default_initclock(struct cputimer_intr
*cti __unused
,
358 boolean_t selected __unused
)
363 cputimer_intr_enable(void)
365 struct cputimer_intr
*cti
;
367 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
)
372 cputimer_intr_config(const struct cputimer
*timer
)
374 struct cputimer_intr
*cti
;
376 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
)
377 cti
->config(cti
, timer
);
381 cputimer_intr_pmfixup(void)
383 struct cputimer_intr
*cti
;
385 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
)
390 cputimer_intr_reload(sysclock_t reload
)
392 struct cputimer_intr
*cti
= sys_cputimer_intr
;
394 cti
->reload(cti
, reload
);
398 cputimer_intr_restart(void)
400 struct cputimer_intr
*cti
= sys_cputimer_intr
;
406 cputimer_intr_select_caps(uint32_t caps
)
408 struct cputimer_intr
*cti
, *maybe
;
412 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
) {
413 if ((cti
->caps
& caps
) == caps
) {
416 else if (cti
->prio
> maybe
->prio
)
423 if (sys_cputimer_intr
== maybe
)
426 cputimer_intr_caps
= caps
;
427 error
= cputimer_intr_select(maybe
, CPUTIMER_INTR_PRIO_MAX
);
434 cputimer_intr_initclocks(void)
436 struct cputimer_intr
*cti
, *ncti
;
439 * An interrupt cputimer may deregister itself,
440 * so use SLIST_FOREACH_MUTABLE here.
442 SLIST_FOREACH_MUTABLE(cti
, &cputimer_intr_head
, next
, ncti
) {
443 boolean_t selected
= FALSE
;
445 if (cti
== sys_cputimer_intr
)
447 cti
->initclock(cti
, selected
);
450 /* NOTE: Must be SECOND to allow platform initialization to go first */
451 SYSINIT(cputimer_intr
, SI_BOOT2_CLOCKREG
, SI_ORDER_SECOND
,
452 cputimer_intr_initclocks
, NULL
);
455 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS
)
457 struct cputimer_intr
*scan
;
462 * Build a list of available interrupt cputimers
464 SLIST_FOREACH(scan
, &cputimer_intr_head
, next
) {
465 if (error
== 0 && loop
)
466 error
= SYSCTL_OUT(req
, " ", 1);
468 error
= SYSCTL_OUT(req
, scan
->name
, strlen(scan
->name
));
475 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS
)
479 error
= SYSCTL_OUT(req
, &sys_cputimer_intr
->freq
,
480 sizeof(sys_cputimer_intr
->freq
));
485 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS
)
487 struct cputimer_intr
*cti
;
491 ksnprintf(name
, sizeof(name
), "%s", sys_cputimer_intr
->name
);
492 error
= sysctl_handle_string(oidp
, name
, sizeof(name
), req
);
493 if (error
!= 0 || req
->newptr
== NULL
)
496 SLIST_FOREACH(cti
, &cputimer_intr_head
, next
) {
497 if (strcmp(cti
->name
, name
) == 0)
502 if (cti
== sys_cputimer_intr
)
505 error
= cputimer_intr_select(cti
, CPUTIMER_INTR_PRIO_MAX
);
507 cputimer_intr_restart();
511 SYSCTL_NODE(_kern_cputimer
, OID_AUTO
, intr
, CTLFLAG_RW
, NULL
,
512 "interrupt cputimer");
514 SYSCTL_PROC(_kern_cputimer_intr
, OID_AUTO
, reglist
, CTLTYPE_STRING
|CTLFLAG_RD
,
515 NULL
, 0, sysctl_cputimer_intr_reglist
, "A", "");
516 SYSCTL_PROC(_kern_cputimer_intr
, OID_AUTO
, freq
, CTLTYPE_INT
|CTLFLAG_RD
,
517 NULL
, 0, sysctl_cputimer_intr_freq
, "I", "");
518 SYSCTL_PROC(_kern_cputimer_intr
, OID_AUTO
, select
, CTLTYPE_STRING
|CTLFLAG_RW
,
519 NULL
, 0, sysctl_cputimer_intr_select
, "A", "");
522 cputimer_intr_powersave_addreq(void)
526 lwkt_serialize_enter(&cputimer_intr_ps_slize
);
528 ++cputimer_intr_ps_reqs
;
529 if (cputimer_intr_ps_reqs
== 1) {
531 * Upon the first power saving request, switch to an one shot
532 * timer, which would not stop in the any power saving state.
534 error
= cputimer_intr_select_caps(CPUTIMER_INTR_CAP_PS
);
535 if (error
== ERESTART
) {
538 kprintf("cputimer: first power save request, restart\n");
539 cputimer_intr_restart();
541 kprintf("no suitable intr cputimer found\n");
542 --cputimer_intr_ps_reqs
;
543 } else if (bootverbose
) {
544 kprintf("cputimer: first power save request\n");
548 lwkt_serialize_exit(&cputimer_intr_ps_slize
);
554 cputimer_intr_powersave_remreq(void)
556 lwkt_serialize_enter(&cputimer_intr_ps_slize
);
558 KASSERT(cputimer_intr_ps_reqs
> 0,
559 ("invalid # of powersave reqs %d", cputimer_intr_ps_reqs
));
560 --cputimer_intr_ps_reqs
;
561 if (cputimer_intr_ps_reqs
== 0) {
564 /* No one needs power saving, use a better one shot timer. */
565 error
= cputimer_intr_select_caps(CPUTIMER_INTR_CAP_NONE
);
566 KKASSERT(!error
|| error
== ERESTART
);
567 if (error
== ERESTART
) {
569 kprintf("cputimer: no powser save request, restart\n");
570 cputimer_intr_restart();
571 } else if (bootverbose
) {
572 kprintf("cputimer: no power save request\n");
576 lwkt_serialize_exit(&cputimer_intr_ps_slize
);
580 cputimer_intr_pcpuhand(void)
582 struct cputimer_intr
*cti
= sys_cputimer_intr
;
584 if (cti
->pcpuhand
!= NULL
)
589 pcpu_timer_process_oncpu(struct globaldata
*gd
, struct intrframe
*frame
)
593 cputimer_intr_pcpuhand();
595 gd
->gd_timer_running
= 0;
597 count
= sys_cputimer
->count();
598 if (TAILQ_FIRST(&gd
->gd_systimerq
) != NULL
)
599 systimer_intr(&count
, 0, frame
);
603 pcpu_timer_process(void)
605 pcpu_timer_process_oncpu(mycpu
, NULL
);
609 pcpu_timer_process_frame(struct intrframe
*frame
)
611 pcpu_timer_process_oncpu(mycpu
, frame
);