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.
97 * suspend_clocksource:
98 * used to calculate the suspend time.
100 * linked list with the registered clocksources
102 * protects manipulations to curr_clocksource and the clocksource_list
104 * Name of the user-specified clocksource.
106 static struct clocksource *curr_clocksource;
107 static struct clocksource *suspend_clocksource;
108 static LIST_HEAD(clocksource_list);
109 static DEFINE_MUTEX(clocksource_mutex);
110 static char override_name[CS_NAME_LEN];
111 static int finished_booting;
112 static u64 suspend_start;
114 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
115 static void clocksource_watchdog_work(struct work_struct *work);
116 static void clocksource_select(void);
118 static LIST_HEAD(watchdog_list);
119 static struct clocksource *watchdog;
120 static struct timer_list watchdog_timer;
121 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
122 static DEFINE_SPINLOCK(watchdog_lock);
123 static int watchdog_running;
124 static atomic_t watchdog_reset_pending;
126 static void inline clocksource_watchdog_lock(unsigned long *flags)
128 spin_lock_irqsave(&watchdog_lock, *flags);
131 static void inline clocksource_watchdog_unlock(unsigned long *flags)
133 spin_unlock_irqrestore(&watchdog_lock, *flags);
137 * Interval: 0.5sec Threshold: 0.0625s
139 #define WATCHDOG_INTERVAL (HZ >> 1)
140 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
142 static void __clocksource_unstable(struct clocksource *cs)
144 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
145 cs->flags |= CLOCK_SOURCE_UNSTABLE;
148 * If the clocksource is registered clocksource_watchdog_work() will
149 * re-rate and re-select.
151 if (list_empty(&cs->list)) {
156 if (cs->mark_unstable)
157 cs->mark_unstable(cs);
159 /* kick clocksource_watchdog_work() */
160 if (finished_booting)
161 schedule_work(&watchdog_work);
165 * clocksource_mark_unstable - mark clocksource unstable via watchdog
166 * @cs: clocksource to be marked unstable
168 * This function is called by the x86 TSC code to mark clocksources as unstable;
169 * it defers demotion and re-selection to a work.
171 void clocksource_mark_unstable(struct clocksource *cs)
175 spin_lock_irqsave(&watchdog_lock, flags);
176 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
177 if (!list_empty(&cs->list) && list_empty(&cs->wd_list))
178 list_add(&cs->wd_list, &watchdog_list);
179 __clocksource_unstable(cs);
181 spin_unlock_irqrestore(&watchdog_lock, flags);
184 static void clocksource_watchdog(struct timer_list *unused)
186 struct clocksource *cs;
187 u64 csnow, wdnow, cslast, wdlast, delta;
188 int64_t wd_nsec, cs_nsec;
189 int next_cpu, reset_pending;
191 spin_lock(&watchdog_lock);
192 if (!watchdog_running)
195 reset_pending = atomic_read(&watchdog_reset_pending);
197 list_for_each_entry(cs, &watchdog_list, wd_list) {
199 /* Clocksource already marked unstable? */
200 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
201 if (finished_booting)
202 schedule_work(&watchdog_work);
207 csnow = cs->read(cs);
208 wdnow = watchdog->read(watchdog);
211 /* Clocksource initialized ? */
212 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
213 atomic_read(&watchdog_reset_pending)) {
214 cs->flags |= CLOCK_SOURCE_WATCHDOG;
220 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
221 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
224 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
225 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
226 wdlast = cs->wd_last; /* save these in case we print them */
227 cslast = cs->cs_last;
231 if (atomic_read(&watchdog_reset_pending))
234 /* Check the deviation from the watchdog clocksource. */
235 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
236 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
237 smp_processor_id(), cs->name);
238 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
239 watchdog->name, wdnow, wdlast, watchdog->mask);
240 pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
241 cs->name, csnow, cslast, cs->mask);
242 __clocksource_unstable(cs);
246 if (cs == curr_clocksource && cs->tick_stable)
249 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
250 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
251 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
252 /* Mark it valid for high-res. */
253 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
256 * clocksource_done_booting() will sort it if
257 * finished_booting is not set yet.
259 if (!finished_booting)
263 * If this is not the current clocksource let
264 * the watchdog thread reselect it. Due to the
265 * change to high res this clocksource might
266 * be preferred now. If it is the current
267 * clocksource let the tick code know about
270 if (cs != curr_clocksource) {
271 cs->flags |= CLOCK_SOURCE_RESELECT;
272 schedule_work(&watchdog_work);
280 * We only clear the watchdog_reset_pending, when we did a
281 * full cycle through all clocksources.
284 atomic_dec(&watchdog_reset_pending);
287 * Cycle through CPUs to check if the CPUs stay synchronized
290 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
291 if (next_cpu >= nr_cpu_ids)
292 next_cpu = cpumask_first(cpu_online_mask);
293 watchdog_timer.expires += WATCHDOG_INTERVAL;
294 add_timer_on(&watchdog_timer, next_cpu);
296 spin_unlock(&watchdog_lock);
299 static inline void clocksource_start_watchdog(void)
301 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
303 timer_setup(&watchdog_timer, clocksource_watchdog, 0);
304 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
305 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
306 watchdog_running = 1;
309 static inline void clocksource_stop_watchdog(void)
311 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
313 del_timer(&watchdog_timer);
314 watchdog_running = 0;
317 static inline void clocksource_reset_watchdog(void)
319 struct clocksource *cs;
321 list_for_each_entry(cs, &watchdog_list, wd_list)
322 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
325 static void clocksource_resume_watchdog(void)
327 atomic_inc(&watchdog_reset_pending);
330 static void clocksource_enqueue_watchdog(struct clocksource *cs)
332 INIT_LIST_HEAD(&cs->wd_list);
334 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
335 /* cs is a clocksource to be watched. */
336 list_add(&cs->wd_list, &watchdog_list);
337 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
339 /* cs is a watchdog. */
340 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
341 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
345 static void clocksource_select_watchdog(bool fallback)
347 struct clocksource *cs, *old_wd;
350 spin_lock_irqsave(&watchdog_lock, flags);
351 /* save current watchdog */
356 list_for_each_entry(cs, &clocksource_list, list) {
357 /* cs is a clocksource to be watched. */
358 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY)
361 /* Skip current if we were requested for a fallback. */
362 if (fallback && cs == old_wd)
365 /* Pick the best watchdog. */
366 if (!watchdog || cs->rating > watchdog->rating)
369 /* If we failed to find a fallback restore the old one. */
373 /* If we changed the watchdog we need to reset cycles. */
374 if (watchdog != old_wd)
375 clocksource_reset_watchdog();
377 /* Check if the watchdog timer needs to be started. */
378 clocksource_start_watchdog();
379 spin_unlock_irqrestore(&watchdog_lock, flags);
382 static void clocksource_dequeue_watchdog(struct clocksource *cs)
384 if (cs != watchdog) {
385 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
386 /* cs is a watched clocksource. */
387 list_del_init(&cs->wd_list);
388 /* Check if the watchdog timer needs to be stopped. */
389 clocksource_stop_watchdog();
394 static void __clocksource_change_rating(struct clocksource *cs, int rating);
396 static int __clocksource_watchdog_work(void)
398 struct clocksource *cs, *tmp;
402 spin_lock_irqsave(&watchdog_lock, flags);
403 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
404 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
405 list_del_init(&cs->wd_list);
406 __clocksource_change_rating(cs, 0);
409 if (cs->flags & CLOCK_SOURCE_RESELECT) {
410 cs->flags &= ~CLOCK_SOURCE_RESELECT;
414 /* Check if the watchdog timer needs to be stopped. */
415 clocksource_stop_watchdog();
416 spin_unlock_irqrestore(&watchdog_lock, flags);
421 static void clocksource_watchdog_work(struct work_struct *work)
423 mutex_lock(&clocksource_mutex);
424 if (__clocksource_watchdog_work())
425 clocksource_select();
426 mutex_unlock(&clocksource_mutex);
429 static bool clocksource_is_watchdog(struct clocksource *cs)
431 return cs == watchdog;
434 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
436 static void clocksource_enqueue_watchdog(struct clocksource *cs)
438 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
439 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
442 static void clocksource_select_watchdog(bool fallback) { }
443 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
444 static inline void clocksource_resume_watchdog(void) { }
445 static inline int __clocksource_watchdog_work(void) { return 0; }
446 static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
447 void clocksource_mark_unstable(struct clocksource *cs) { }
449 static inline void clocksource_watchdog_lock(unsigned long *flags) { }
450 static inline void clocksource_watchdog_unlock(unsigned long *flags) { }
452 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
454 static bool clocksource_is_suspend(struct clocksource *cs)
456 return cs == suspend_clocksource;
459 static void __clocksource_suspend_select(struct clocksource *cs)
462 * Skip the clocksource which will be stopped in suspend state.
464 if (!(cs->flags & CLOCK_SOURCE_SUSPEND_NONSTOP))
468 * The nonstop clocksource can be selected as the suspend clocksource to
469 * calculate the suspend time, so it should not supply suspend/resume
470 * interfaces to suspend the nonstop clocksource when system suspends.
472 if (cs->suspend || cs->resume) {
473 pr_warn("Nonstop clocksource %s should not supply suspend/resume interfaces\n",
477 /* Pick the best rating. */
478 if (!suspend_clocksource || cs->rating > suspend_clocksource->rating)
479 suspend_clocksource = cs;
483 * clocksource_suspend_select - Select the best clocksource for suspend timing
484 * @fallback: if select a fallback clocksource
486 static void clocksource_suspend_select(bool fallback)
488 struct clocksource *cs, *old_suspend;
490 old_suspend = suspend_clocksource;
492 suspend_clocksource = NULL;
494 list_for_each_entry(cs, &clocksource_list, list) {
495 /* Skip current if we were requested for a fallback. */
496 if (fallback && cs == old_suspend)
499 __clocksource_suspend_select(cs);
504 * clocksource_start_suspend_timing - Start measuring the suspend timing
505 * @cs: current clocksource from timekeeping
506 * @start_cycles: current cycles from timekeeping
508 * This function will save the start cycle values of suspend timer to calculate
509 * the suspend time when resuming system.
511 * This function is called late in the suspend process from timekeeping_suspend(),
512 * that means processes are freezed, non-boot cpus and interrupts are disabled
513 * now. It is therefore possible to start the suspend timer without taking the
516 void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles)
518 if (!suspend_clocksource)
522 * If current clocksource is the suspend timer, we should use the
523 * tkr_mono.cycle_last value as suspend_start to avoid same reading
524 * from suspend timer.
526 if (clocksource_is_suspend(cs)) {
527 suspend_start = start_cycles;
531 if (suspend_clocksource->enable &&
532 suspend_clocksource->enable(suspend_clocksource)) {
533 pr_warn_once("Failed to enable the non-suspend-able clocksource.\n");
537 suspend_start = suspend_clocksource->read(suspend_clocksource);
541 * clocksource_stop_suspend_timing - Stop measuring the suspend timing
542 * @cs: current clocksource from timekeeping
543 * @cycle_now: current cycles from timekeeping
545 * This function will calculate the suspend time from suspend timer.
547 * Returns nanoseconds since suspend started, 0 if no usable suspend clocksource.
549 * This function is called early in the resume process from timekeeping_resume(),
550 * that means there is only one cpu, no processes are running and the interrupts
551 * are disabled. It is therefore possible to stop the suspend timer without
552 * taking the clocksource mutex.
554 u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now)
556 u64 now, delta, nsec = 0;
558 if (!suspend_clocksource)
562 * If current clocksource is the suspend timer, we should use the
563 * tkr_mono.cycle_last value from timekeeping as current cycle to
564 * avoid same reading from suspend timer.
566 if (clocksource_is_suspend(cs))
569 now = suspend_clocksource->read(suspend_clocksource);
571 if (now > suspend_start) {
572 delta = clocksource_delta(now, suspend_start,
573 suspend_clocksource->mask);
574 nsec = mul_u64_u32_shr(delta, suspend_clocksource->mult,
575 suspend_clocksource->shift);
579 * Disable the suspend timer to save power if current clocksource is
580 * not the suspend timer.
582 if (!clocksource_is_suspend(cs) && suspend_clocksource->disable)
583 suspend_clocksource->disable(suspend_clocksource);
589 * clocksource_suspend - suspend the clocksource(s)
591 void clocksource_suspend(void)
593 struct clocksource *cs;
595 list_for_each_entry_reverse(cs, &clocksource_list, list)
601 * clocksource_resume - resume the clocksource(s)
603 void clocksource_resume(void)
605 struct clocksource *cs;
607 list_for_each_entry(cs, &clocksource_list, list)
611 clocksource_resume_watchdog();
615 * clocksource_touch_watchdog - Update watchdog
617 * Update the watchdog after exception contexts such as kgdb so as not
618 * to incorrectly trip the watchdog. This might fail when the kernel
619 * was stopped in code which holds watchdog_lock.
621 void clocksource_touch_watchdog(void)
623 clocksource_resume_watchdog();
627 * clocksource_max_adjustment- Returns max adjustment amount
628 * @cs: Pointer to clocksource
631 static u32 clocksource_max_adjustment(struct clocksource *cs)
635 * We won't try to correct for more than 11% adjustments (110,000 ppm),
637 ret = (u64)cs->mult * 11;
643 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
644 * @mult: cycle to nanosecond multiplier
645 * @shift: cycle to nanosecond divisor (power of two)
646 * @maxadj: maximum adjustment value to mult (~11%)
647 * @mask: bitmask for two's complement subtraction of non 64 bit counters
648 * @max_cyc: maximum cycle value before potential overflow (does not include
651 * NOTE: This function includes a safety margin of 50%, in other words, we
652 * return half the number of nanoseconds the hardware counter can technically
653 * cover. This is done so that we can potentially detect problems caused by
654 * delayed timers or bad hardware, which might result in time intervals that
655 * are larger than what the math used can handle without overflows.
657 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
659 u64 max_nsecs, max_cycles;
662 * Calculate the maximum number of cycles that we can pass to the
663 * cyc2ns() function without overflowing a 64-bit result.
665 max_cycles = ULLONG_MAX;
666 do_div(max_cycles, mult+maxadj);
669 * The actual maximum number of cycles we can defer the clocksource is
670 * determined by the minimum of max_cycles and mask.
671 * Note: Here we subtract the maxadj to make sure we don't sleep for
672 * too long if there's a large negative adjustment.
674 max_cycles = min(max_cycles, mask);
675 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
677 /* return the max_cycles value as well if requested */
679 *max_cyc = max_cycles;
681 /* Return 50% of the actual maximum, so we can detect bad values */
688 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
689 * @cs: Pointer to clocksource to be updated
692 static inline void clocksource_update_max_deferment(struct clocksource *cs)
694 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
695 cs->maxadj, cs->mask,
699 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
701 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
703 struct clocksource *cs;
705 if (!finished_booting || list_empty(&clocksource_list))
709 * We pick the clocksource with the highest rating. If oneshot
710 * mode is active, we pick the highres valid clocksource with
713 list_for_each_entry(cs, &clocksource_list, list) {
714 if (skipcur && cs == curr_clocksource)
716 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
723 static void __clocksource_select(bool skipcur)
725 bool oneshot = tick_oneshot_mode_active();
726 struct clocksource *best, *cs;
728 /* Find the best suitable clocksource */
729 best = clocksource_find_best(oneshot, skipcur);
733 if (!strlen(override_name))
736 /* Check for the override clocksource. */
737 list_for_each_entry(cs, &clocksource_list, list) {
738 if (skipcur && cs == curr_clocksource)
740 if (strcmp(cs->name, override_name) != 0)
743 * Check to make sure we don't switch to a non-highres
744 * capable clocksource if the tick code is in oneshot
745 * mode (highres or nohz)
747 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
748 /* Override clocksource cannot be used. */
749 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
750 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
752 override_name[0] = 0;
755 * The override cannot be currently verified.
756 * Deferring to let the watchdog check.
758 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
762 /* Override clocksource can be used. */
768 if (curr_clocksource != best && !timekeeping_notify(best)) {
769 pr_info("Switched to clocksource %s\n", best->name);
770 curr_clocksource = best;
775 * clocksource_select - Select the best clocksource available
777 * Private function. Must hold clocksource_mutex when called.
779 * Select the clocksource with the best rating, or the clocksource,
780 * which is selected by userspace override.
782 static void clocksource_select(void)
784 __clocksource_select(false);
787 static void clocksource_select_fallback(void)
789 __clocksource_select(true);
792 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
793 static inline void clocksource_select(void) { }
794 static inline void clocksource_select_fallback(void) { }
799 * clocksource_done_booting - Called near the end of core bootup
801 * Hack to avoid lots of clocksource churn at boot time.
802 * We use fs_initcall because we want this to start before
803 * device_initcall but after subsys_initcall.
805 static int __init clocksource_done_booting(void)
807 mutex_lock(&clocksource_mutex);
808 curr_clocksource = clocksource_default_clock();
809 finished_booting = 1;
811 * Run the watchdog first to eliminate unstable clock sources
813 __clocksource_watchdog_work();
814 clocksource_select();
815 mutex_unlock(&clocksource_mutex);
818 fs_initcall(clocksource_done_booting);
821 * Enqueue the clocksource sorted by rating
823 static void clocksource_enqueue(struct clocksource *cs)
825 struct list_head *entry = &clocksource_list;
826 struct clocksource *tmp;
828 list_for_each_entry(tmp, &clocksource_list, list) {
829 /* Keep track of the place, where to insert */
830 if (tmp->rating < cs->rating)
834 list_add(&cs->list, entry);
838 * __clocksource_update_freq_scale - Used update clocksource with new freq
839 * @cs: clocksource to be registered
840 * @scale: Scale factor multiplied against freq to get clocksource hz
841 * @freq: clocksource frequency (cycles per second) divided by scale
843 * This should only be called from the clocksource->enable() method.
845 * This *SHOULD NOT* be called directly! Please use the
846 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
849 void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
854 * Default clocksources are *special* and self-define their mult/shift.
855 * But, you're not special, so you should specify a freq value.
859 * Calc the maximum number of seconds which we can run before
860 * wrapping around. For clocksources which have a mask > 32-bit
861 * we need to limit the max sleep time to have a good
862 * conversion precision. 10 minutes is still a reasonable
863 * amount. That results in a shift value of 24 for a
864 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
865 * ~ 0.06ppm granularity for NTP.
872 else if (sec > 600 && cs->mask > UINT_MAX)
875 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
876 NSEC_PER_SEC / scale, sec * scale);
879 * Ensure clocksources that have large 'mult' values don't overflow
882 cs->maxadj = clocksource_max_adjustment(cs);
883 while (freq && ((cs->mult + cs->maxadj < cs->mult)
884 || (cs->mult - cs->maxadj > cs->mult))) {
887 cs->maxadj = clocksource_max_adjustment(cs);
891 * Only warn for *special* clocksources that self-define
892 * their mult/shift values and don't specify a freq.
894 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
895 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
898 clocksource_update_max_deferment(cs);
900 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
901 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
903 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
906 * __clocksource_register_scale - Used to install new clocksources
907 * @cs: clocksource to be registered
908 * @scale: Scale factor multiplied against freq to get clocksource hz
909 * @freq: clocksource frequency (cycles per second) divided by scale
911 * Returns -EBUSY if registration fails, zero otherwise.
913 * This *SHOULD NOT* be called directly! Please use the
914 * clocksource_register_hz() or clocksource_register_khz helper functions.
916 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
920 /* Initialize mult/shift and max_idle_ns */
921 __clocksource_update_freq_scale(cs, scale, freq);
923 /* Add clocksource to the clocksource list */
924 mutex_lock(&clocksource_mutex);
926 clocksource_watchdog_lock(&flags);
927 clocksource_enqueue(cs);
928 clocksource_enqueue_watchdog(cs);
929 clocksource_watchdog_unlock(&flags);
931 clocksource_select();
932 clocksource_select_watchdog(false);
933 __clocksource_suspend_select(cs);
934 mutex_unlock(&clocksource_mutex);
937 EXPORT_SYMBOL_GPL(__clocksource_register_scale);
939 static void __clocksource_change_rating(struct clocksource *cs, int rating)
943 clocksource_enqueue(cs);
947 * clocksource_change_rating - Change the rating of a registered clocksource
948 * @cs: clocksource to be changed
949 * @rating: new rating
951 void clocksource_change_rating(struct clocksource *cs, int rating)
955 mutex_lock(&clocksource_mutex);
956 clocksource_watchdog_lock(&flags);
957 __clocksource_change_rating(cs, rating);
958 clocksource_watchdog_unlock(&flags);
960 clocksource_select();
961 clocksource_select_watchdog(false);
962 clocksource_suspend_select(false);
963 mutex_unlock(&clocksource_mutex);
965 EXPORT_SYMBOL(clocksource_change_rating);
968 * Unbind clocksource @cs. Called with clocksource_mutex held
970 static int clocksource_unbind(struct clocksource *cs)
974 if (clocksource_is_watchdog(cs)) {
975 /* Select and try to install a replacement watchdog. */
976 clocksource_select_watchdog(true);
977 if (clocksource_is_watchdog(cs))
981 if (cs == curr_clocksource) {
982 /* Select and try to install a replacement clock source */
983 clocksource_select_fallback();
984 if (curr_clocksource == cs)
988 if (clocksource_is_suspend(cs)) {
990 * Select and try to install a replacement suspend clocksource.
991 * If no replacement suspend clocksource, we will just let the
992 * clocksource go and have no suspend clocksource.
994 clocksource_suspend_select(true);
997 clocksource_watchdog_lock(&flags);
998 clocksource_dequeue_watchdog(cs);
999 list_del_init(&cs->list);
1000 clocksource_watchdog_unlock(&flags);
1006 * clocksource_unregister - remove a registered clocksource
1007 * @cs: clocksource to be unregistered
1009 int clocksource_unregister(struct clocksource *cs)
1013 mutex_lock(&clocksource_mutex);
1014 if (!list_empty(&cs->list))
1015 ret = clocksource_unbind(cs);
1016 mutex_unlock(&clocksource_mutex);
1019 EXPORT_SYMBOL(clocksource_unregister);
1023 * current_clocksource_show - sysfs interface for current clocksource
1026 * @buf: char buffer to be filled with clocksource list
1028 * Provides sysfs interface for listing current clocksource.
1030 static ssize_t current_clocksource_show(struct device *dev,
1031 struct device_attribute *attr,
1036 mutex_lock(&clocksource_mutex);
1037 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
1038 mutex_unlock(&clocksource_mutex);
1043 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
1047 /* strings from sysfs write are not 0 terminated! */
1048 if (!cnt || cnt >= CS_NAME_LEN)
1052 if (buf[cnt-1] == '\n')
1055 memcpy(dst, buf, cnt);
1061 * current_clocksource_store - interface for manually overriding clocksource
1064 * @buf: name of override clocksource
1065 * @count: length of buffer
1067 * Takes input from sysfs interface for manually overriding the default
1068 * clocksource selection.
1070 static ssize_t current_clocksource_store(struct device *dev,
1071 struct device_attribute *attr,
1072 const char *buf, size_t count)
1076 mutex_lock(&clocksource_mutex);
1078 ret = sysfs_get_uname(buf, override_name, count);
1080 clocksource_select();
1082 mutex_unlock(&clocksource_mutex);
1086 static DEVICE_ATTR_RW(current_clocksource);
1089 * unbind_clocksource_store - interface for manually unbinding clocksource
1093 * @count: length of buffer
1095 * Takes input from sysfs interface for manually unbinding a clocksource.
1097 static ssize_t unbind_clocksource_store(struct device *dev,
1098 struct device_attribute *attr,
1099 const char *buf, size_t count)
1101 struct clocksource *cs;
1102 char name[CS_NAME_LEN];
1105 ret = sysfs_get_uname(buf, name, count);
1110 mutex_lock(&clocksource_mutex);
1111 list_for_each_entry(cs, &clocksource_list, list) {
1112 if (strcmp(cs->name, name))
1114 ret = clocksource_unbind(cs);
1117 mutex_unlock(&clocksource_mutex);
1119 return ret ? ret : count;
1121 static DEVICE_ATTR_WO(unbind_clocksource);
1124 * available_clocksource_show - sysfs interface for listing clocksource
1127 * @buf: char buffer to be filled with clocksource list
1129 * Provides sysfs interface for listing registered clocksources
1131 static ssize_t available_clocksource_show(struct device *dev,
1132 struct device_attribute *attr,
1135 struct clocksource *src;
1138 mutex_lock(&clocksource_mutex);
1139 list_for_each_entry(src, &clocksource_list, list) {
1141 * Don't show non-HRES clocksource if the tick code is
1142 * in one shot mode (highres=on or nohz=on)
1144 if (!tick_oneshot_mode_active() ||
1145 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
1146 count += snprintf(buf + count,
1147 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1150 mutex_unlock(&clocksource_mutex);
1152 count += snprintf(buf + count,
1153 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
1157 static DEVICE_ATTR_RO(available_clocksource);
1159 static struct attribute *clocksource_attrs[] = {
1160 &dev_attr_current_clocksource.attr,
1161 &dev_attr_unbind_clocksource.attr,
1162 &dev_attr_available_clocksource.attr,
1165 ATTRIBUTE_GROUPS(clocksource);
1167 static struct bus_type clocksource_subsys = {
1168 .name = "clocksource",
1169 .dev_name = "clocksource",
1172 static struct device device_clocksource = {
1174 .bus = &clocksource_subsys,
1175 .groups = clocksource_groups,
1178 static int __init init_clocksource_sysfs(void)
1180 int error = subsys_system_register(&clocksource_subsys, NULL);
1183 error = device_register(&device_clocksource);
1188 device_initcall(init_clocksource_sysfs);
1189 #endif /* CONFIG_SYSFS */
1192 * boot_override_clocksource - boot clock override
1193 * @str: override name
1195 * Takes a clocksource= boot argument and uses it
1196 * as the clocksource override name.
1198 static int __init boot_override_clocksource(char* str)
1200 mutex_lock(&clocksource_mutex);
1202 strlcpy(override_name, str, sizeof(override_name));
1203 mutex_unlock(&clocksource_mutex);
1207 __setup("clocksource=", boot_override_clocksource);
1210 * boot_override_clock - Compatibility layer for deprecated boot option
1211 * @str: override name
1213 * DEPRECATED! Takes a clock= boot argument and uses it
1214 * as the clocksource override name
1216 static int __init boot_override_clock(char* str)
1218 if (!strcmp(str, "pmtmr")) {
1219 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
1220 return boot_override_clocksource("acpi_pm");
1222 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
1223 return boot_override_clocksource(str);
1226 __setup("clock=", boot_override_clock);