2 * linux/kernel/time/clocksource.c
4 * This file contains the functions which manage clocksource drivers.
6 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * o Allow clocksource drivers to be unregistered
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/device.h>
29 #include <linux/clocksource.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
33 #include <linux/tick.h>
34 #include <linux/kthread.h>
36 #include "tick-internal.h"
37 #include "timekeeping_internal.h"
40 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
41 * @mult: pointer to mult variable
42 * @shift: pointer to shift variable
43 * @from: frequency to convert from
44 * @to: frequency to convert to
45 * @maxsec: guaranteed runtime conversion range in seconds
47 * The function evaluates the shift/mult pair for the scaled math
48 * operations of clocksources and clockevents.
50 * @to and @from are frequency values in HZ. For clock sources @to is
51 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
52 * event @to is the counter frequency and @from is NSEC_PER_SEC.
54 * The @maxsec conversion range argument controls the time frame in
55 * seconds which must be covered by the runtime conversion with the
56 * calculated mult and shift factors. This guarantees that no 64bit
57 * overflow happens when the input value of the conversion is
58 * multiplied with the calculated mult factor. Larger ranges may
59 * reduce the conversion accuracy by chosing smaller mult and shift
63 clocks_calc_mult_shift(u32
*mult
, u32
*shift
, u32 from
, u32 to
, u32 maxsec
)
69 * Calculate the shift factor which is limiting the conversion
72 tmp
= ((u64
)maxsec
* from
) >> 32;
79 * Find the conversion shift/mult pair which has the best
80 * accuracy and fits the maxsec conversion range:
82 for (sft
= 32; sft
> 0; sft
--) {
83 tmp
= (u64
) to
<< sft
;
86 if ((tmp
>> sftacc
) == 0)
92 EXPORT_SYMBOL_GPL(clocks_calc_mult_shift
);
94 /*[Clocksource internal variables]---------
96 * currently selected clocksource.
98 * linked list with the registered clocksources
100 * protects manipulations to curr_clocksource and the clocksource_list
102 * Name of the user-specified clocksource.
104 static struct clocksource
*curr_clocksource
;
105 static LIST_HEAD(clocksource_list
);
106 static DEFINE_MUTEX(clocksource_mutex
);
107 static char override_name
[CS_NAME_LEN
];
108 static int finished_booting
;
110 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
111 static void clocksource_watchdog_work(struct work_struct
*work
);
112 static void clocksource_select(void);
114 static LIST_HEAD(watchdog_list
);
115 static struct clocksource
*watchdog
;
116 static struct timer_list watchdog_timer
;
117 static DECLARE_WORK(watchdog_work
, clocksource_watchdog_work
);
118 static DEFINE_SPINLOCK(watchdog_lock
);
119 static int watchdog_running
;
120 static atomic_t watchdog_reset_pending
;
122 static void inline clocksource_watchdog_lock(unsigned long *flags
)
124 spin_lock_irqsave(&watchdog_lock
, *flags
);
127 static void inline clocksource_watchdog_unlock(unsigned long *flags
)
129 spin_unlock_irqrestore(&watchdog_lock
, *flags
);
132 static int clocksource_watchdog_kthread(void *data
);
133 static void __clocksource_change_rating(struct clocksource
*cs
, int rating
);
136 * Interval: 0.5sec Threshold: 0.0625s
138 #define WATCHDOG_INTERVAL (HZ >> 1)
139 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
141 static void clocksource_watchdog_work(struct work_struct
*work
)
144 * If kthread_run fails the next watchdog scan over the
145 * watchdog_list will find the unstable clock again.
147 kthread_run(clocksource_watchdog_kthread
, NULL
, "kwatchdog");
150 static void __clocksource_unstable(struct clocksource
*cs
)
152 cs
->flags
&= ~(CLOCK_SOURCE_VALID_FOR_HRES
| CLOCK_SOURCE_WATCHDOG
);
153 cs
->flags
|= CLOCK_SOURCE_UNSTABLE
;
156 * If the clocksource is registered clocksource_watchdog_kthread() will
157 * re-rate and re-select.
159 if (list_empty(&cs
->list
)) {
164 if (cs
->mark_unstable
)
165 cs
->mark_unstable(cs
);
167 /* kick clocksource_watchdog_kthread() */
168 if (finished_booting
)
169 schedule_work(&watchdog_work
);
173 * clocksource_mark_unstable - mark clocksource unstable via watchdog
174 * @cs: clocksource to be marked unstable
176 * This function is called instead of clocksource_change_rating from
177 * cpu hotplug code to avoid a deadlock between the clocksource mutex
178 * and the cpu hotplug mutex. It defers the update of the clocksource
179 * to the watchdog thread.
181 void clocksource_mark_unstable(struct clocksource
*cs
)
185 spin_lock_irqsave(&watchdog_lock
, flags
);
186 if (!(cs
->flags
& CLOCK_SOURCE_UNSTABLE
)) {
187 if (!list_empty(&cs
->list
) && list_empty(&cs
->wd_list
))
188 list_add(&cs
->wd_list
, &watchdog_list
);
189 __clocksource_unstable(cs
);
191 spin_unlock_irqrestore(&watchdog_lock
, flags
);
194 static void clocksource_watchdog(struct timer_list
*unused
)
196 struct clocksource
*cs
;
197 u64 csnow
, wdnow
, cslast
, wdlast
, delta
;
198 int64_t wd_nsec
, cs_nsec
;
199 int next_cpu
, reset_pending
;
201 spin_lock(&watchdog_lock
);
202 if (!watchdog_running
)
205 reset_pending
= atomic_read(&watchdog_reset_pending
);
207 list_for_each_entry(cs
, &watchdog_list
, wd_list
) {
209 /* Clocksource already marked unstable? */
210 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
) {
211 if (finished_booting
)
212 schedule_work(&watchdog_work
);
217 csnow
= cs
->read(cs
);
218 wdnow
= watchdog
->read(watchdog
);
221 /* Clocksource initialized ? */
222 if (!(cs
->flags
& CLOCK_SOURCE_WATCHDOG
) ||
223 atomic_read(&watchdog_reset_pending
)) {
224 cs
->flags
|= CLOCK_SOURCE_WATCHDOG
;
230 delta
= clocksource_delta(wdnow
, cs
->wd_last
, watchdog
->mask
);
231 wd_nsec
= clocksource_cyc2ns(delta
, watchdog
->mult
,
234 delta
= clocksource_delta(csnow
, cs
->cs_last
, cs
->mask
);
235 cs_nsec
= clocksource_cyc2ns(delta
, cs
->mult
, cs
->shift
);
236 wdlast
= cs
->wd_last
; /* save these in case we print them */
237 cslast
= cs
->cs_last
;
241 if (atomic_read(&watchdog_reset_pending
))
244 /* Check the deviation from the watchdog clocksource. */
245 if (abs(cs_nsec
- wd_nsec
) > WATCHDOG_THRESHOLD
) {
246 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
247 smp_processor_id(), cs
->name
);
248 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
249 watchdog
->name
, wdnow
, wdlast
, watchdog
->mask
);
250 pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
251 cs
->name
, csnow
, cslast
, cs
->mask
);
252 __clocksource_unstable(cs
);
256 if (cs
== curr_clocksource
&& cs
->tick_stable
)
259 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) &&
260 (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
) &&
261 (watchdog
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)) {
262 /* Mark it valid for high-res. */
263 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
266 * clocksource_done_booting() will sort it if
267 * finished_booting is not set yet.
269 if (!finished_booting
)
273 * If this is not the current clocksource let
274 * the watchdog thread reselect it. Due to the
275 * change to high res this clocksource might
276 * be preferred now. If it is the current
277 * clocksource let the tick code know about
280 if (cs
!= curr_clocksource
) {
281 cs
->flags
|= CLOCK_SOURCE_RESELECT
;
282 schedule_work(&watchdog_work
);
290 * We only clear the watchdog_reset_pending, when we did a
291 * full cycle through all clocksources.
294 atomic_dec(&watchdog_reset_pending
);
297 * Cycle through CPUs to check if the CPUs stay synchronized
300 next_cpu
= cpumask_next(raw_smp_processor_id(), cpu_online_mask
);
301 if (next_cpu
>= nr_cpu_ids
)
302 next_cpu
= cpumask_first(cpu_online_mask
);
303 watchdog_timer
.expires
+= WATCHDOG_INTERVAL
;
304 add_timer_on(&watchdog_timer
, next_cpu
);
306 spin_unlock(&watchdog_lock
);
309 static inline void clocksource_start_watchdog(void)
311 if (watchdog_running
|| !watchdog
|| list_empty(&watchdog_list
))
313 timer_setup(&watchdog_timer
, clocksource_watchdog
, 0);
314 watchdog_timer
.expires
= jiffies
+ WATCHDOG_INTERVAL
;
315 add_timer_on(&watchdog_timer
, cpumask_first(cpu_online_mask
));
316 watchdog_running
= 1;
319 static inline void clocksource_stop_watchdog(void)
321 if (!watchdog_running
|| (watchdog
&& !list_empty(&watchdog_list
)))
323 del_timer(&watchdog_timer
);
324 watchdog_running
= 0;
327 static inline void clocksource_reset_watchdog(void)
329 struct clocksource
*cs
;
331 list_for_each_entry(cs
, &watchdog_list
, wd_list
)
332 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
335 static void clocksource_resume_watchdog(void)
337 atomic_inc(&watchdog_reset_pending
);
340 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
342 INIT_LIST_HEAD(&cs
->wd_list
);
344 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
345 /* cs is a clocksource to be watched. */
346 list_add(&cs
->wd_list
, &watchdog_list
);
347 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
349 /* cs is a watchdog. */
350 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
351 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
355 static void clocksource_select_watchdog(bool fallback
)
357 struct clocksource
*cs
, *old_wd
;
360 spin_lock_irqsave(&watchdog_lock
, flags
);
361 /* save current watchdog */
366 list_for_each_entry(cs
, &clocksource_list
, list
) {
367 /* cs is a clocksource to be watched. */
368 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
)
371 /* Skip current if we were requested for a fallback. */
372 if (fallback
&& cs
== old_wd
)
375 /* Pick the best watchdog. */
376 if (!watchdog
|| cs
->rating
> watchdog
->rating
)
379 /* If we failed to find a fallback restore the old one. */
383 /* If we changed the watchdog we need to reset cycles. */
384 if (watchdog
!= old_wd
)
385 clocksource_reset_watchdog();
387 /* Check if the watchdog timer needs to be started. */
388 clocksource_start_watchdog();
389 spin_unlock_irqrestore(&watchdog_lock
, flags
);
392 static void clocksource_dequeue_watchdog(struct clocksource
*cs
)
394 if (cs
!= watchdog
) {
395 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
396 /* cs is a watched clocksource. */
397 list_del_init(&cs
->wd_list
);
398 /* Check if the watchdog timer needs to be stopped. */
399 clocksource_stop_watchdog();
404 static int __clocksource_watchdog_kthread(void)
406 struct clocksource
*cs
, *tmp
;
410 spin_lock_irqsave(&watchdog_lock
, flags
);
411 list_for_each_entry_safe(cs
, tmp
, &watchdog_list
, wd_list
) {
412 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
) {
413 list_del_init(&cs
->wd_list
);
414 __clocksource_change_rating(cs
, 0);
417 if (cs
->flags
& CLOCK_SOURCE_RESELECT
) {
418 cs
->flags
&= ~CLOCK_SOURCE_RESELECT
;
422 /* Check if the watchdog timer needs to be stopped. */
423 clocksource_stop_watchdog();
424 spin_unlock_irqrestore(&watchdog_lock
, flags
);
429 static int clocksource_watchdog_kthread(void *data
)
431 mutex_lock(&clocksource_mutex
);
432 if (__clocksource_watchdog_kthread())
433 clocksource_select();
434 mutex_unlock(&clocksource_mutex
);
438 static bool clocksource_is_watchdog(struct clocksource
*cs
)
440 return cs
== watchdog
;
443 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
445 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
447 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
448 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
451 static void clocksource_select_watchdog(bool fallback
) { }
452 static inline void clocksource_dequeue_watchdog(struct clocksource
*cs
) { }
453 static inline void clocksource_resume_watchdog(void) { }
454 static inline int __clocksource_watchdog_kthread(void) { return 0; }
455 static bool clocksource_is_watchdog(struct clocksource
*cs
) { return false; }
456 void clocksource_mark_unstable(struct clocksource
*cs
) { }
458 static inline void clocksource_watchdog_lock(unsigned long *flags
) { }
459 static inline void clocksource_watchdog_unlock(unsigned long *flags
) { }
461 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
464 * clocksource_suspend - suspend the clocksource(s)
466 void clocksource_suspend(void)
468 struct clocksource
*cs
;
470 list_for_each_entry_reverse(cs
, &clocksource_list
, list
)
476 * clocksource_resume - resume the clocksource(s)
478 void clocksource_resume(void)
480 struct clocksource
*cs
;
482 list_for_each_entry(cs
, &clocksource_list
, list
)
486 clocksource_resume_watchdog();
490 * clocksource_touch_watchdog - Update watchdog
492 * Update the watchdog after exception contexts such as kgdb so as not
493 * to incorrectly trip the watchdog. This might fail when the kernel
494 * was stopped in code which holds watchdog_lock.
496 void clocksource_touch_watchdog(void)
498 clocksource_resume_watchdog();
502 * clocksource_max_adjustment- Returns max adjustment amount
503 * @cs: Pointer to clocksource
506 static u32
clocksource_max_adjustment(struct clocksource
*cs
)
510 * We won't try to correct for more than 11% adjustments (110,000 ppm),
512 ret
= (u64
)cs
->mult
* 11;
518 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
519 * @mult: cycle to nanosecond multiplier
520 * @shift: cycle to nanosecond divisor (power of two)
521 * @maxadj: maximum adjustment value to mult (~11%)
522 * @mask: bitmask for two's complement subtraction of non 64 bit counters
523 * @max_cyc: maximum cycle value before potential overflow (does not include
526 * NOTE: This function includes a safety margin of 50%, in other words, we
527 * return half the number of nanoseconds the hardware counter can technically
528 * cover. This is done so that we can potentially detect problems caused by
529 * delayed timers or bad hardware, which might result in time intervals that
530 * are larger than what the math used can handle without overflows.
532 u64
clocks_calc_max_nsecs(u32 mult
, u32 shift
, u32 maxadj
, u64 mask
, u64
*max_cyc
)
534 u64 max_nsecs
, max_cycles
;
537 * Calculate the maximum number of cycles that we can pass to the
538 * cyc2ns() function without overflowing a 64-bit result.
540 max_cycles
= ULLONG_MAX
;
541 do_div(max_cycles
, mult
+maxadj
);
544 * The actual maximum number of cycles we can defer the clocksource is
545 * determined by the minimum of max_cycles and mask.
546 * Note: Here we subtract the maxadj to make sure we don't sleep for
547 * too long if there's a large negative adjustment.
549 max_cycles
= min(max_cycles
, mask
);
550 max_nsecs
= clocksource_cyc2ns(max_cycles
, mult
- maxadj
, shift
);
552 /* return the max_cycles value as well if requested */
554 *max_cyc
= max_cycles
;
556 /* Return 50% of the actual maximum, so we can detect bad values */
563 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
564 * @cs: Pointer to clocksource to be updated
567 static inline void clocksource_update_max_deferment(struct clocksource
*cs
)
569 cs
->max_idle_ns
= clocks_calc_max_nsecs(cs
->mult
, cs
->shift
,
570 cs
->maxadj
, cs
->mask
,
574 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
576 static struct clocksource
*clocksource_find_best(bool oneshot
, bool skipcur
)
578 struct clocksource
*cs
;
580 if (!finished_booting
|| list_empty(&clocksource_list
))
584 * We pick the clocksource with the highest rating. If oneshot
585 * mode is active, we pick the highres valid clocksource with
588 list_for_each_entry(cs
, &clocksource_list
, list
) {
589 if (skipcur
&& cs
== curr_clocksource
)
591 if (oneshot
&& !(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
))
598 static void __clocksource_select(bool skipcur
)
600 bool oneshot
= tick_oneshot_mode_active();
601 struct clocksource
*best
, *cs
;
603 /* Find the best suitable clocksource */
604 best
= clocksource_find_best(oneshot
, skipcur
);
608 /* Check for the override clocksource. */
609 list_for_each_entry(cs
, &clocksource_list
, list
) {
610 if (skipcur
&& cs
== curr_clocksource
)
612 if (strcmp(cs
->name
, override_name
) != 0)
615 * Check to make sure we don't switch to a non-highres
616 * capable clocksource if the tick code is in oneshot
617 * mode (highres or nohz)
619 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) && oneshot
) {
620 /* Override clocksource cannot be used. */
621 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
) {
622 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
624 override_name
[0] = 0;
627 * The override cannot be currently verified.
628 * Deferring to let the watchdog check.
630 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
634 /* Override clocksource can be used. */
639 if (curr_clocksource
!= best
&& !timekeeping_notify(best
)) {
640 pr_info("Switched to clocksource %s\n", best
->name
);
641 curr_clocksource
= best
;
646 * clocksource_select - Select the best clocksource available
648 * Private function. Must hold clocksource_mutex when called.
650 * Select the clocksource with the best rating, or the clocksource,
651 * which is selected by userspace override.
653 static void clocksource_select(void)
655 __clocksource_select(false);
658 static void clocksource_select_fallback(void)
660 __clocksource_select(true);
663 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
664 static inline void clocksource_select(void) { }
665 static inline void clocksource_select_fallback(void) { }
670 * clocksource_done_booting - Called near the end of core bootup
672 * Hack to avoid lots of clocksource churn at boot time.
673 * We use fs_initcall because we want this to start before
674 * device_initcall but after subsys_initcall.
676 static int __init
clocksource_done_booting(void)
678 mutex_lock(&clocksource_mutex
);
679 curr_clocksource
= clocksource_default_clock();
680 finished_booting
= 1;
682 * Run the watchdog first to eliminate unstable clock sources
684 __clocksource_watchdog_kthread();
685 clocksource_select();
686 mutex_unlock(&clocksource_mutex
);
689 fs_initcall(clocksource_done_booting
);
692 * Enqueue the clocksource sorted by rating
694 static void clocksource_enqueue(struct clocksource
*cs
)
696 struct list_head
*entry
= &clocksource_list
;
697 struct clocksource
*tmp
;
699 list_for_each_entry(tmp
, &clocksource_list
, list
) {
700 /* Keep track of the place, where to insert */
701 if (tmp
->rating
< cs
->rating
)
705 list_add(&cs
->list
, entry
);
709 * __clocksource_update_freq_scale - Used update clocksource with new freq
710 * @cs: clocksource to be registered
711 * @scale: Scale factor multiplied against freq to get clocksource hz
712 * @freq: clocksource frequency (cycles per second) divided by scale
714 * This should only be called from the clocksource->enable() method.
716 * This *SHOULD NOT* be called directly! Please use the
717 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
720 void __clocksource_update_freq_scale(struct clocksource
*cs
, u32 scale
, u32 freq
)
725 * Default clocksources are *special* and self-define their mult/shift.
726 * But, you're not special, so you should specify a freq value.
730 * Calc the maximum number of seconds which we can run before
731 * wrapping around. For clocksources which have a mask > 32-bit
732 * we need to limit the max sleep time to have a good
733 * conversion precision. 10 minutes is still a reasonable
734 * amount. That results in a shift value of 24 for a
735 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
736 * ~ 0.06ppm granularity for NTP.
743 else if (sec
> 600 && cs
->mask
> UINT_MAX
)
746 clocks_calc_mult_shift(&cs
->mult
, &cs
->shift
, freq
,
747 NSEC_PER_SEC
/ scale
, sec
* scale
);
750 * Ensure clocksources that have large 'mult' values don't overflow
753 cs
->maxadj
= clocksource_max_adjustment(cs
);
754 while (freq
&& ((cs
->mult
+ cs
->maxadj
< cs
->mult
)
755 || (cs
->mult
- cs
->maxadj
> cs
->mult
))) {
758 cs
->maxadj
= clocksource_max_adjustment(cs
);
762 * Only warn for *special* clocksources that self-define
763 * their mult/shift values and don't specify a freq.
765 WARN_ONCE(cs
->mult
+ cs
->maxadj
< cs
->mult
,
766 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
769 clocksource_update_max_deferment(cs
);
771 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
772 cs
->name
, cs
->mask
, cs
->max_cycles
, cs
->max_idle_ns
);
774 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale
);
777 * __clocksource_register_scale - Used to install new clocksources
778 * @cs: clocksource to be registered
779 * @scale: Scale factor multiplied against freq to get clocksource hz
780 * @freq: clocksource frequency (cycles per second) divided by scale
782 * Returns -EBUSY if registration fails, zero otherwise.
784 * This *SHOULD NOT* be called directly! Please use the
785 * clocksource_register_hz() or clocksource_register_khz helper functions.
787 int __clocksource_register_scale(struct clocksource
*cs
, u32 scale
, u32 freq
)
791 /* Initialize mult/shift and max_idle_ns */
792 __clocksource_update_freq_scale(cs
, scale
, freq
);
794 /* Add clocksource to the clocksource list */
795 mutex_lock(&clocksource_mutex
);
797 clocksource_watchdog_lock(&flags
);
798 clocksource_enqueue(cs
);
799 clocksource_enqueue_watchdog(cs
);
800 clocksource_watchdog_unlock(&flags
);
802 clocksource_select();
803 clocksource_select_watchdog(false);
804 mutex_unlock(&clocksource_mutex
);
807 EXPORT_SYMBOL_GPL(__clocksource_register_scale
);
809 static void __clocksource_change_rating(struct clocksource
*cs
, int rating
)
813 clocksource_enqueue(cs
);
817 * clocksource_change_rating - Change the rating of a registered clocksource
818 * @cs: clocksource to be changed
819 * @rating: new rating
821 void clocksource_change_rating(struct clocksource
*cs
, int rating
)
825 mutex_lock(&clocksource_mutex
);
826 clocksource_watchdog_lock(&flags
);
827 __clocksource_change_rating(cs
, rating
);
828 clocksource_watchdog_unlock(&flags
);
830 clocksource_select();
831 clocksource_select_watchdog(false);
832 mutex_unlock(&clocksource_mutex
);
834 EXPORT_SYMBOL(clocksource_change_rating
);
837 * Unbind clocksource @cs. Called with clocksource_mutex held
839 static int clocksource_unbind(struct clocksource
*cs
)
843 if (clocksource_is_watchdog(cs
)) {
844 /* Select and try to install a replacement watchdog. */
845 clocksource_select_watchdog(true);
846 if (clocksource_is_watchdog(cs
))
850 if (cs
== curr_clocksource
) {
851 /* Select and try to install a replacement clock source */
852 clocksource_select_fallback();
853 if (curr_clocksource
== cs
)
857 clocksource_watchdog_lock(&flags
);
858 clocksource_dequeue_watchdog(cs
);
859 list_del_init(&cs
->list
);
860 clocksource_watchdog_unlock(&flags
);
866 * clocksource_unregister - remove a registered clocksource
867 * @cs: clocksource to be unregistered
869 int clocksource_unregister(struct clocksource
*cs
)
873 mutex_lock(&clocksource_mutex
);
874 if (!list_empty(&cs
->list
))
875 ret
= clocksource_unbind(cs
);
876 mutex_unlock(&clocksource_mutex
);
879 EXPORT_SYMBOL(clocksource_unregister
);
883 * sysfs_show_current_clocksources - sysfs interface for current clocksource
886 * @buf: char buffer to be filled with clocksource list
888 * Provides sysfs interface for listing current clocksource.
891 sysfs_show_current_clocksources(struct device
*dev
,
892 struct device_attribute
*attr
, char *buf
)
896 mutex_lock(&clocksource_mutex
);
897 count
= snprintf(buf
, PAGE_SIZE
, "%s\n", curr_clocksource
->name
);
898 mutex_unlock(&clocksource_mutex
);
903 ssize_t
sysfs_get_uname(const char *buf
, char *dst
, size_t cnt
)
907 /* strings from sysfs write are not 0 terminated! */
908 if (!cnt
|| cnt
>= CS_NAME_LEN
)
912 if (buf
[cnt
-1] == '\n')
915 memcpy(dst
, buf
, cnt
);
921 * sysfs_override_clocksource - interface for manually overriding clocksource
924 * @buf: name of override clocksource
925 * @count: length of buffer
927 * Takes input from sysfs interface for manually overriding the default
928 * clocksource selection.
930 static ssize_t
sysfs_override_clocksource(struct device
*dev
,
931 struct device_attribute
*attr
,
932 const char *buf
, size_t count
)
936 mutex_lock(&clocksource_mutex
);
938 ret
= sysfs_get_uname(buf
, override_name
, count
);
940 clocksource_select();
942 mutex_unlock(&clocksource_mutex
);
948 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
952 * @count: length of buffer
954 * Takes input from sysfs interface for manually unbinding a clocksource.
956 static ssize_t
sysfs_unbind_clocksource(struct device
*dev
,
957 struct device_attribute
*attr
,
958 const char *buf
, size_t count
)
960 struct clocksource
*cs
;
961 char name
[CS_NAME_LEN
];
964 ret
= sysfs_get_uname(buf
, name
, count
);
969 mutex_lock(&clocksource_mutex
);
970 list_for_each_entry(cs
, &clocksource_list
, list
) {
971 if (strcmp(cs
->name
, name
))
973 ret
= clocksource_unbind(cs
);
976 mutex_unlock(&clocksource_mutex
);
978 return ret
? ret
: count
;
982 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
985 * @buf: char buffer to be filled with clocksource list
987 * Provides sysfs interface for listing registered clocksources
990 sysfs_show_available_clocksources(struct device
*dev
,
991 struct device_attribute
*attr
,
994 struct clocksource
*src
;
997 mutex_lock(&clocksource_mutex
);
998 list_for_each_entry(src
, &clocksource_list
, list
) {
1000 * Don't show non-HRES clocksource if the tick code is
1001 * in one shot mode (highres=on or nohz=on)
1003 if (!tick_oneshot_mode_active() ||
1004 (src
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
))
1005 count
+= snprintf(buf
+ count
,
1006 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0),
1009 mutex_unlock(&clocksource_mutex
);
1011 count
+= snprintf(buf
+ count
,
1012 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0), "\n");
1020 static DEVICE_ATTR(current_clocksource
, 0644, sysfs_show_current_clocksources
,
1021 sysfs_override_clocksource
);
1023 static DEVICE_ATTR(unbind_clocksource
, 0200, NULL
, sysfs_unbind_clocksource
);
1025 static DEVICE_ATTR(available_clocksource
, 0444,
1026 sysfs_show_available_clocksources
, NULL
);
1028 static struct bus_type clocksource_subsys
= {
1029 .name
= "clocksource",
1030 .dev_name
= "clocksource",
1033 static struct device device_clocksource
= {
1035 .bus
= &clocksource_subsys
,
1038 static int __init
init_clocksource_sysfs(void)
1040 int error
= subsys_system_register(&clocksource_subsys
, NULL
);
1043 error
= device_register(&device_clocksource
);
1045 error
= device_create_file(
1046 &device_clocksource
,
1047 &dev_attr_current_clocksource
);
1049 error
= device_create_file(&device_clocksource
,
1050 &dev_attr_unbind_clocksource
);
1052 error
= device_create_file(
1053 &device_clocksource
,
1054 &dev_attr_available_clocksource
);
1058 device_initcall(init_clocksource_sysfs
);
1059 #endif /* CONFIG_SYSFS */
1062 * boot_override_clocksource - boot clock override
1063 * @str: override name
1065 * Takes a clocksource= boot argument and uses it
1066 * as the clocksource override name.
1068 static int __init
boot_override_clocksource(char* str
)
1070 mutex_lock(&clocksource_mutex
);
1072 strlcpy(override_name
, str
, sizeof(override_name
));
1073 mutex_unlock(&clocksource_mutex
);
1077 __setup("clocksource=", boot_override_clocksource
);
1080 * boot_override_clock - Compatibility layer for deprecated boot option
1081 * @str: override name
1083 * DEPRECATED! Takes a clock= boot argument and uses it
1084 * as the clocksource override name
1086 static int __init
boot_override_clock(char* str
)
1088 if (!strcmp(str
, "pmtmr")) {
1089 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
1090 return boot_override_clocksource("acpi_pm");
1092 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
1093 return boot_override_clocksource(str
);
1096 __setup("clock=", boot_override_clock
);