cputimer: Add per-cpu handler and private data for interrupt cputimer.
[dragonfly.git] / sys / kern / kern_cputimer.c
blobe84a2bc37745a2fcd3c860215eb8b0b8701dcb82
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 sysclock_t dummy_cputimer_count(void);
53 static struct cputimer dummy_cputimer = {
54 SLIST_ENTRY_INITIALIZER,
55 "dummy",
56 CPUTIMER_PRI_DUMMY,
57 CPUTIMER_DUMMY,
58 dummy_cputimer_count,
59 cputimer_default_fromhz,
60 cputimer_default_fromus,
61 cputimer_default_construct,
62 cputimer_default_destruct,
63 1000000,
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
79 void
80 cputimer_select(struct cputimer *timer, int pri)
82 sysclock_t oldclock;
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
92 * dummy.
94 if (pri == 0)
95 pri = timer->pri;
96 if (timer == NULL || pri >= sys_cputimer->pri) {
97 oldclock = sys_cputimer->count();
98 sys_cputimer->destruct(sys_cputimer);
99 sys_cputimer = &dummy_cputimer;
100 if (timer) {
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.
111 void
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) {
123 if (scan == timer)
124 return;
126 SLIST_INSERT_HEAD(&cputimerhead, timer, next);
130 * Deregister a timer. If the timer has already been deregistered, do nothing.
132 void
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) {
143 if (timer == scan) {
144 if (timer == sys_cputimer)
145 cputimer_select(&dummy_cputimer, 0x7FFFFFFF);
146 SLIST_REMOVE(&cputimerhead, timer, cputimer, next);
147 break;
152 * If sys_cputimer reverted to the dummy, select the best one
154 if (sys_cputimer == &dummy_cputimer) {
155 best = NULL;
156 SLIST_FOREACH(scan, &cputimerhead, next) {
157 if (best == NULL || scan->pri > best->pri)
158 best = scan;
160 if (best)
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)
171 void
172 cputimer_set_frequency(struct cputimer *timer, sysclock_t freq)
174 timer->freq = 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);
181 sysclock_t
182 cputimer_default_fromhz(int freq)
184 return(sys_cputimer->freq / freq + 1);
187 sysclock_t
188 cputimer_default_fromus(int us)
190 return((int64_t)sys_cputimer->freq * us / 1000000);
194 * Dummy counter implementation
196 static
197 sysclock_t
198 dummy_cputimer_count(void)
200 return(++dummy_cputimer.base);
203 void
204 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
206 cputimer->base = oldclock;
209 void
210 cputimer_default_destruct(struct cputimer *cputimer)
214 /************************************************************************
215 * SYSCTL SUPPORT *
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.
222 static int
223 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
225 struct cputimer *scan;
226 int error = 0;
227 int loop = 0;
230 * Build a list of available timers
232 SLIST_FOREACH(scan, &cputimerhead, next) {
233 if (error == 0 && loop)
234 error = SYSCTL_OUT(req, " ", 1);
235 if (error == 0)
236 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
237 ++loop;
239 return (error);
242 static int
243 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
245 int error;
247 error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
248 return (error);
251 static int
252 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
254 sysclock_t clock;
255 int error;
257 clock = sys_cputimer->count();
258 error = SYSCTL_OUT(req, &clock, sizeof(clock));
259 return (error);
262 static int
263 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
265 int error;
267 error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
268 return (error);
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);
288 void
289 cputimer_intr_register(struct cputimer_intr *cti)
291 struct cputimer_intr *scan;
293 SLIST_FOREACH(scan, &cputimer_intr_head, next) {
294 if (scan == cti)
295 return;
297 cti->config(cti, sys_cputimer);
298 SLIST_INSERT_HEAD(&cputimer_intr_head, cti, next);
301 void
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);
313 if (prio == 0)
314 prio = cti->prio;
316 if (sys_cputimer_intr == NULL) {
317 KKASSERT(cputimer_intr_caps == 0);
318 sys_cputimer_intr = cti;
319 return 0;
322 if ((cti->caps & cputimer_intr_caps) == cputimer_intr_caps) {
323 if (prio > sys_cputimer_intr->prio) {
324 sys_cputimer_intr = cti;
325 return 0;
326 } else {
327 return EBUSY;
329 } else {
330 return EOPNOTSUPP;
334 void
335 cputimer_intr_default_enable(struct cputimer_intr *cti __unused)
339 void
340 cputimer_intr_default_restart(struct cputimer_intr *cti)
342 cti->reload(cti, 0);
345 void
346 cputimer_intr_default_config(struct cputimer_intr *cti __unused,
347 const struct cputimer *timer __unused)
351 void
352 cputimer_intr_default_pmfixup(struct cputimer_intr *cti __unused)
356 void
357 cputimer_intr_default_initclock(struct cputimer_intr *cti __unused,
358 boolean_t selected __unused)
362 void
363 cputimer_intr_enable(void)
365 struct cputimer_intr *cti;
367 SLIST_FOREACH(cti, &cputimer_intr_head, next)
368 cti->enable(cti);
371 void
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);
380 void
381 cputimer_intr_pmfixup(void)
383 struct cputimer_intr *cti;
385 SLIST_FOREACH(cti, &cputimer_intr_head, next)
386 cti->pmfixup(cti);
389 void
390 cputimer_intr_reload(sysclock_t reload)
392 struct cputimer_intr *cti = sys_cputimer_intr;
394 cti->reload(cti, reload);
397 void
398 cputimer_intr_restart(void)
400 struct cputimer_intr *cti = sys_cputimer_intr;
402 cti->restart(cti);
406 cputimer_intr_select_caps(uint32_t caps)
408 struct cputimer_intr *cti, *maybe;
409 int error;
411 maybe = NULL;
412 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
413 if ((cti->caps & caps) == caps) {
414 if (maybe == NULL)
415 maybe = cti;
416 else if (cti->prio > maybe->prio)
417 maybe = cti;
420 if (maybe == NULL)
421 return ENOENT;
423 if (sys_cputimer_intr == maybe)
424 return 0;
426 cputimer_intr_caps = caps;
427 error = cputimer_intr_select(maybe, CPUTIMER_INTR_PRIO_MAX);
428 KKASSERT(!error);
430 return ERESTART;
433 static void
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)
446 selected = TRUE;
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);
454 static int
455 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS)
457 struct cputimer_intr *scan;
458 int error = 0;
459 int loop = 0;
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);
467 if (error == 0)
468 error = SYSCTL_OUT(req, scan->name, strlen(scan->name));
469 ++loop;
471 return (error);
474 static int
475 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS)
477 int error;
479 error = SYSCTL_OUT(req, &sys_cputimer_intr->freq,
480 sizeof(sys_cputimer_intr->freq));
481 return (error);
484 static int
485 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS)
487 struct cputimer_intr *cti;
488 char name[32];
489 int error;
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)
494 return error;
496 SLIST_FOREACH(cti, &cputimer_intr_head, next) {
497 if (strcmp(cti->name, name) == 0)
498 break;
500 if (cti == NULL)
501 return ENOENT;
502 if (cti == sys_cputimer_intr)
503 return 0;
505 error = cputimer_intr_select(cti, CPUTIMER_INTR_PRIO_MAX);
506 if (!error)
507 cputimer_intr_restart();
508 return error;
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)
524 int error = 0;
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) {
536 error = 0;
537 if (bootverbose)
538 kprintf("cputimer: first power save request, restart\n");
539 cputimer_intr_restart();
540 } else if (error) {
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);
550 return error;
553 void
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) {
562 int error;
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) {
568 if (bootverbose)
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);
579 static __inline void
580 cputimer_intr_pcpuhand(void)
582 struct cputimer_intr *cti = sys_cputimer_intr;
584 if (cti->pcpuhand != NULL)
585 cti->pcpuhand(cti);
588 static void
589 pcpu_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
591 sysclock_t count;
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);
602 void
603 pcpu_timer_process(void)
605 pcpu_timer_process_oncpu(mycpu, NULL);
608 void
609 pcpu_timer_process_frame(struct intrframe *frame)
611 pcpu_timer_process_oncpu(mycpu, frame);