Merge branch 'vendor/BMAKE'
[dragonfly.git] / sys / kern / kern_cputimer.c
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:
10  * 
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.
20  * 
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.
33  */
34 /*
35  * Generic cputimer - access to a reliable, free-running counter.
36  */
37
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>
47
48 extern void     pcpu_timer_process(void);
49 extern void     pcpu_timer_process_frame(struct intrframe *);
50
51 static sysclock_t dummy_cputimer_count(void);
52
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,
66     0
67 };
68
69 struct cputimer *sys_cputimer = &dummy_cputimer;
70 SLIST_HEAD(, cputimer) cputimerhead = SLIST_HEAD_INITIALIZER(&cputimerhead);
71
72 static int      cputimer_intr_ps_reqs;
73 static struct lwkt_serialize cputimer_intr_ps_slize =
74     LWKT_SERIALIZE_INITIALIZER;
75
76 /*
77  * Generic cputimer API
78  */
79 void
80 cputimer_select(struct cputimer *timer, int pri)
81 {
82     sysclock_t oldclock;
83
84     /*
85      * Calculate helper fields
86      */
87     cputimer_set_frequency(timer, timer->freq);
88
89     /*
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.
93      */
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);
104         }
105     }
106 }
107
108 /*
109  * Register a timer.  If the timer has already been registered, do nothing.
110  */
111 void
112 cputimer_register(struct cputimer *timer)
113 {
114     struct cputimer *scan;
115
116     /*
117      * Initialize dummy_cputimer if the slist is empty, it does not get
118      * registered the normal way.
119      */
120     if (SLIST_EMPTY(&cputimerhead))
121         SLIST_FIRST(&cputimerhead) = &dummy_cputimer;
122     SLIST_FOREACH(scan, &cputimerhead, next) {
123         if (scan == timer)
124             return;
125     }
126     SLIST_INSERT_HEAD(&cputimerhead, timer, next);
127 }
128
129 /*
130  * Deregister a timer.  If the timer has already been deregistered, do nothing.
131  */
132 void
133 cputimer_deregister(struct cputimer *timer)
134 {
135     struct cputimer *scan;
136     struct cputimer *best;
137
138     /*
139      * Locate and remove the timer.  If the timer is our currently active
140      * timer, revert to the dummy timer.
141      */
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;
148             }
149     }
150
151     /*
152      * If sys_cputimer reverted to the dummy, select the best one
153      */
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;
159         }
160         if (best)
161             cputimer_select(best, 0x7FFFFFFF);
162     }
163 }
164
165 /*
166  * Calculate usec / tick and nsec / tick, scaled by (1 << 32).
167  *
168  * so e.g. a 3 mhz timer would be 3 usec / tick x (1 << 32),
169  * or 3000 nsec / tick x (1 << 32)
170  */
171 void
172 cputimer_set_frequency(struct cputimer *timer, sysclock_t freq)
173 {
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);
179 }
180
181 sysclock_t
182 cputimer_default_fromhz(int freq)
183 {
184     return(sys_cputimer->freq / freq + 1);
185 }
186
187 sysclock_t
188 cputimer_default_fromus(int us)
189 {
190     return((int64_t)sys_cputimer->freq * us / 1000000);
191 }
192
193 /*
194  * Dummy counter implementation
195  */
196 static
197 sysclock_t
198 dummy_cputimer_count(void)
199 {
200     return(++dummy_cputimer.base);
201 }
202
203 void
204 cputimer_default_construct(struct cputimer *cputimer, sysclock_t oldclock)
205 {
206     cputimer->base = oldclock;
207 }
208
209 void
210 cputimer_default_destruct(struct cputimer *cputimer)
211 {
212 }
213
214 /************************************************************************
215  *                              SYSCTL SUPPORT                          *
216  ************************************************************************
217  *
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.
221  */
222 static int
223 sysctl_cputimer_reglist(SYSCTL_HANDLER_ARGS)
224 {
225     struct cputimer *scan;
226     int error = 0;
227     int loop = 0;
228
229     /*
230      * Build a list of available timers
231      */
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;
238     }
239     return (error);
240 }
241
242 static int
243 sysctl_cputimer_name(SYSCTL_HANDLER_ARGS)
244 {
245     int error;
246
247     error = SYSCTL_OUT(req, sys_cputimer->name, strlen(sys_cputimer->name));
248     return (error);
249 }
250
251 static int
252 sysctl_cputimer_clock(SYSCTL_HANDLER_ARGS)
253 {
254     sysclock_t clock;
255     int error;
256
257     clock = sys_cputimer->count();
258     error = SYSCTL_OUT(req, &clock, sizeof(clock));
259     return (error);
260 }
261
262 static int
263 sysctl_cputimer_freq(SYSCTL_HANDLER_ARGS)
264 {
265     int error;
266
267     error = SYSCTL_OUT(req, &sys_cputimer->freq, sizeof(sys_cputimer->freq));
268     return (error);
269 }
270
271 SYSCTL_DECL(_kern_cputimer);
272 SYSCTL_NODE(_kern, OID_AUTO, cputimer, CTLFLAG_RW, NULL, "cputimer");
273
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", "");
282
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);
287
288 void
289 cputimer_intr_register(struct cputimer_intr *cti)
290 {
291     struct cputimer_intr *scan;
292
293     SLIST_FOREACH(scan, &cputimer_intr_head, next) {
294         if (scan == cti)
295             return;
296     }
297     cti->config(cti, sys_cputimer);
298     SLIST_INSERT_HEAD(&cputimer_intr_head, cti, next);
299 }
300
301 void
302 cputimer_intr_deregister(struct cputimer_intr *cti)
303 {
304     KKASSERT(cti != sys_cputimer_intr);
305     SLIST_REMOVE(&cputimer_intr_head, cti, cputimer_intr, next);
306 }
307
308 int
309 cputimer_intr_select(struct cputimer_intr *cti, int prio)
310 {
311     KKASSERT(cti != NULL);
312
313     if (prio == 0)
314         prio = cti->prio;
315
316     if (sys_cputimer_intr == NULL) {
317         KKASSERT(cputimer_intr_caps == 0);
318         sys_cputimer_intr = cti;
319         return 0;
320     }
321
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;
328         }
329     } else {
330         return EOPNOTSUPP;
331     }
332 }
333
334 void
335 cputimer_intr_default_enable(struct cputimer_intr *cti __unused)
336 {
337 }
338
339 void
340 cputimer_intr_default_restart(struct cputimer_intr *cti)
341 {
342     cti->reload(cti, 0);
343 }
344
345 void
346 cputimer_intr_default_config(struct cputimer_intr *cti __unused,
347                              const struct cputimer *timer __unused)
348 {
349 }
350
351 void
352 cputimer_intr_default_pmfixup(struct cputimer_intr *cti __unused)
353 {
354 }
355
356 void
357 cputimer_intr_default_initclock(struct cputimer_intr *cti __unused,
358                                 boolean_t selected __unused)
359 {
360 }
361
362 void
363 cputimer_intr_enable(void)
364 {
365     struct cputimer_intr *cti;
366
367     SLIST_FOREACH(cti, &cputimer_intr_head, next)
368         cti->enable(cti);
369 }
370
371 void
372 cputimer_intr_config(const struct cputimer *timer)
373 {
374     struct cputimer_intr *cti;
375
376     SLIST_FOREACH(cti, &cputimer_intr_head, next)
377         cti->config(cti, timer);
378 }
379
380 void
381 cputimer_intr_pmfixup(void)
382 {
383     struct cputimer_intr *cti;
384
385     SLIST_FOREACH(cti, &cputimer_intr_head, next)
386         cti->pmfixup(cti);
387 }
388
389 void
390 cputimer_intr_reload(sysclock_t reload)
391 {
392     struct cputimer_intr *cti = sys_cputimer_intr;
393
394     cti->reload(cti, reload);
395 }
396
397 void
398 cputimer_intr_restart(void)
399 {
400     struct cputimer_intr *cti = sys_cputimer_intr;
401
402     cti->restart(cti);
403 }
404
405 int
406 cputimer_intr_select_caps(uint32_t caps)
407 {
408     struct cputimer_intr *cti, *maybe;
409     int error;
410
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;
418         }
419     }
420     if (maybe == NULL)
421         return ENOENT;
422
423     if (sys_cputimer_intr == maybe)
424         return 0;
425
426     cputimer_intr_caps = caps;
427     error = cputimer_intr_select(maybe, CPUTIMER_INTR_PRIO_MAX);
428     KKASSERT(!error);
429
430     return ERESTART;
431 }
432
433 static void
434 cputimer_intr_initclocks(void)
435 {
436     struct cputimer_intr *cti, *ncti;
437
438     /*
439      * An interrupt cputimer may deregister itself,
440      * so use SLIST_FOREACH_MUTABLE here.
441      */
442     SLIST_FOREACH_MUTABLE(cti, &cputimer_intr_head, next, ncti) {
443         boolean_t selected = FALSE;
444
445         if (cti == sys_cputimer_intr)
446             selected = TRUE;
447         cti->initclock(cti, selected);
448     }
449 }
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);
453
454 static int
455 sysctl_cputimer_intr_reglist(SYSCTL_HANDLER_ARGS)
456 {
457     struct cputimer_intr *scan;
458     int error = 0;
459     int loop = 0;
460
461     /*
462      * Build a list of available interrupt cputimers
463      */
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;
470     }
471     return (error);
472 }
473
474 static int
475 sysctl_cputimer_intr_freq(SYSCTL_HANDLER_ARGS)
476 {
477     int error;
478
479     error = SYSCTL_OUT(req, &sys_cputimer_intr->freq,
480                        sizeof(sys_cputimer_intr->freq));
481     return (error);
482 }
483
484 static int
485 sysctl_cputimer_intr_select(SYSCTL_HANDLER_ARGS)
486 {
487     struct cputimer_intr *cti;
488     char name[32];
489     int error;
490
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;
495
496     SLIST_FOREACH(cti, &cputimer_intr_head, next) {
497         if (strcmp(cti->name, name) == 0)
498             break;
499     }
500     if (cti == NULL)
501         return ENOENT;
502     if (cti == sys_cputimer_intr)
503         return 0;
504
505     error = cputimer_intr_select(cti, CPUTIMER_INTR_PRIO_MAX);
506     if (!error)
507         cputimer_intr_restart();
508     return error;
509 }
510
511 SYSCTL_NODE(_kern_cputimer, OID_AUTO, intr, CTLFLAG_RW, NULL,
512             "interrupt cputimer");
513
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", "");
520
521 int
522 cputimer_intr_powersave_addreq(void)
523 {
524     int error = 0;
525
526     lwkt_serialize_enter(&cputimer_intr_ps_slize);
527
528     ++cputimer_intr_ps_reqs;
529     if (cputimer_intr_ps_reqs == 1) {
530         /*
531          * Upon the first power saving request, switch to an one shot
532          * timer, which would not stop in the any power saving state.
533          */
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");
545         }
546     }
547
548     lwkt_serialize_exit(&cputimer_intr_ps_slize);
549
550     return error;
551 }
552
553 void
554 cputimer_intr_powersave_remreq(void)
555 {
556     lwkt_serialize_enter(&cputimer_intr_ps_slize);
557
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;
563
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");
573         }
574     }
575
576     lwkt_serialize_exit(&cputimer_intr_ps_slize);
577 }
578
579 static __inline void
580 cputimer_intr_pcpuhand(void)
581 {
582     struct cputimer_intr *cti = sys_cputimer_intr;
583
584     if (cti->pcpuhand != NULL)
585         cti->pcpuhand(cti);
586 }
587
588 static void
589 pcpu_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
590 {
591         sysclock_t count;
592
593         cputimer_intr_pcpuhand();
594
595         gd->gd_timer_running = 0;
596
597         count = sys_cputimer->count();
598         if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
599                 systimer_intr(&count, 0, frame);
600 }
601
602 void
603 pcpu_timer_process(void)
604 {
605         pcpu_timer_process_oncpu(mycpu, NULL);
606 }
607
608 void
609 pcpu_timer_process_frame(struct intrframe *frame)
610 {
611         pcpu_timer_process_oncpu(mycpu, frame);
612 }