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 #include <linux/device.h>
27 #include <linux/clocksource.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
31 #include <linux/tick.h>
32 #include <linux/kthread.h>
34 #include "tick-internal.h"
35 #include "timekeeping_internal.h"
38 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
39 * @mult: pointer to mult variable
40 * @shift: pointer to shift variable
41 * @from: frequency to convert from
42 * @to: frequency to convert to
43 * @maxsec: guaranteed runtime conversion range in seconds
45 * The function evaluates the shift/mult pair for the scaled math
46 * operations of clocksources and clockevents.
48 * @to and @from are frequency values in HZ. For clock sources @to is
49 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
50 * event @to is the counter frequency and @from is NSEC_PER_SEC.
52 * The @maxsec conversion range argument controls the time frame in
53 * seconds which must be covered by the runtime conversion with the
54 * calculated mult and shift factors. This guarantees that no 64bit
55 * overflow happens when the input value of the conversion is
56 * multiplied with the calculated mult factor. Larger ranges may
57 * reduce the conversion accuracy by chosing smaller mult and shift
61 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
67 * Calculate the shift factor which is limiting the conversion
70 tmp = ((u64)maxsec * from) >> 32;
77 * Find the conversion shift/mult pair which has the best
78 * accuracy and fits the maxsec conversion range:
80 for (sft = 32; sft > 0; sft--) {
81 tmp = (u64) to << sft;
84 if ((tmp >> sftacc) == 0)
91 /*[Clocksource internal variables]---------
93 * currently selected clocksource.
95 * linked list with the registered clocksources
97 * protects manipulations to curr_clocksource and the clocksource_list
99 * Name of the user-specified clocksource.
101 static struct clocksource *curr_clocksource;
102 static LIST_HEAD(clocksource_list);
103 static DEFINE_MUTEX(clocksource_mutex);
104 static char override_name[CS_NAME_LEN];
105 static int finished_booting;
107 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
108 static void clocksource_watchdog_work(struct work_struct *work);
109 static void clocksource_select(void);
111 static LIST_HEAD(watchdog_list);
112 static struct clocksource *watchdog;
113 static struct timer_list watchdog_timer;
114 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
115 static DEFINE_SPINLOCK(watchdog_lock);
116 static int watchdog_running;
117 static atomic_t watchdog_reset_pending;
119 static int clocksource_watchdog_kthread(void *data);
120 static void __clocksource_change_rating(struct clocksource *cs, int rating);
123 * Interval: 0.5sec Threshold: 0.0625s
125 #define WATCHDOG_INTERVAL (HZ >> 1)
126 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
128 static void clocksource_watchdog_work(struct work_struct *work)
131 * If kthread_run fails the next watchdog scan over the
132 * watchdog_list will find the unstable clock again.
134 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
137 static void __clocksource_unstable(struct clocksource *cs)
139 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
140 cs->flags |= CLOCK_SOURCE_UNSTABLE;
141 if (finished_booting)
142 schedule_work(&watchdog_work);
145 static void clocksource_unstable(struct clocksource *cs, int64_t delta)
147 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
149 __clocksource_unstable(cs);
153 * clocksource_mark_unstable - mark clocksource unstable via watchdog
154 * @cs: clocksource to be marked unstable
156 * This function is called instead of clocksource_change_rating from
157 * cpu hotplug code to avoid a deadlock between the clocksource mutex
158 * and the cpu hotplug mutex. It defers the update of the clocksource
159 * to the watchdog thread.
161 void clocksource_mark_unstable(struct clocksource *cs)
165 spin_lock_irqsave(&watchdog_lock, flags);
166 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
167 if (list_empty(&cs->wd_list))
168 list_add(&cs->wd_list, &watchdog_list);
169 __clocksource_unstable(cs);
171 spin_unlock_irqrestore(&watchdog_lock, flags);
174 static void clocksource_watchdog(unsigned long data)
176 struct clocksource *cs;
177 cycle_t csnow, wdnow, delta;
178 int64_t wd_nsec, cs_nsec;
179 int next_cpu, reset_pending;
181 spin_lock(&watchdog_lock);
182 if (!watchdog_running)
185 reset_pending = atomic_read(&watchdog_reset_pending);
187 list_for_each_entry(cs, &watchdog_list, wd_list) {
189 /* Clocksource already marked unstable? */
190 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
191 if (finished_booting)
192 schedule_work(&watchdog_work);
197 csnow = cs->read(cs);
198 wdnow = watchdog->read(watchdog);
201 /* Clocksource initialized ? */
202 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
203 atomic_read(&watchdog_reset_pending)) {
204 cs->flags |= CLOCK_SOURCE_WATCHDOG;
210 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
211 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
214 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
215 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
219 if (atomic_read(&watchdog_reset_pending))
222 /* Check the deviation from the watchdog clocksource. */
223 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
224 clocksource_unstable(cs, cs_nsec - wd_nsec);
228 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
229 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
230 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
231 /* Mark it valid for high-res. */
232 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
235 * clocksource_done_booting() will sort it if
236 * finished_booting is not set yet.
238 if (!finished_booting)
242 * If this is not the current clocksource let
243 * the watchdog thread reselect it. Due to the
244 * change to high res this clocksource might
245 * be preferred now. If it is the current
246 * clocksource let the tick code know about
249 if (cs != curr_clocksource) {
250 cs->flags |= CLOCK_SOURCE_RESELECT;
251 schedule_work(&watchdog_work);
259 * We only clear the watchdog_reset_pending, when we did a
260 * full cycle through all clocksources.
263 atomic_dec(&watchdog_reset_pending);
266 * Cycle through CPUs to check if the CPUs stay synchronized
269 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
270 if (next_cpu >= nr_cpu_ids)
271 next_cpu = cpumask_first(cpu_online_mask);
272 watchdog_timer.expires += WATCHDOG_INTERVAL;
273 add_timer_on(&watchdog_timer, next_cpu);
275 spin_unlock(&watchdog_lock);
278 static inline void clocksource_start_watchdog(void)
280 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
282 init_timer(&watchdog_timer);
283 watchdog_timer.function = clocksource_watchdog;
284 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
285 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
286 watchdog_running = 1;
289 static inline void clocksource_stop_watchdog(void)
291 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
293 del_timer(&watchdog_timer);
294 watchdog_running = 0;
297 static inline void clocksource_reset_watchdog(void)
299 struct clocksource *cs;
301 list_for_each_entry(cs, &watchdog_list, wd_list)
302 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
305 static void clocksource_resume_watchdog(void)
307 atomic_inc(&watchdog_reset_pending);
310 static void clocksource_enqueue_watchdog(struct clocksource *cs)
314 spin_lock_irqsave(&watchdog_lock, flags);
315 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
316 /* cs is a clocksource to be watched. */
317 list_add(&cs->wd_list, &watchdog_list);
318 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
320 /* cs is a watchdog. */
321 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
322 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
323 /* Pick the best watchdog. */
324 if (!watchdog || cs->rating > watchdog->rating) {
326 /* Reset watchdog cycles */
327 clocksource_reset_watchdog();
330 /* Check if the watchdog timer needs to be started. */
331 clocksource_start_watchdog();
332 spin_unlock_irqrestore(&watchdog_lock, flags);
335 static void clocksource_dequeue_watchdog(struct clocksource *cs)
339 spin_lock_irqsave(&watchdog_lock, flags);
340 if (cs != watchdog) {
341 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
342 /* cs is a watched clocksource. */
343 list_del_init(&cs->wd_list);
344 /* Check if the watchdog timer needs to be stopped. */
345 clocksource_stop_watchdog();
348 spin_unlock_irqrestore(&watchdog_lock, flags);
351 static int __clocksource_watchdog_kthread(void)
353 struct clocksource *cs, *tmp;
358 spin_lock_irqsave(&watchdog_lock, flags);
359 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
360 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
361 list_del_init(&cs->wd_list);
362 list_add(&cs->wd_list, &unstable);
365 if (cs->flags & CLOCK_SOURCE_RESELECT) {
366 cs->flags &= ~CLOCK_SOURCE_RESELECT;
370 /* Check if the watchdog timer needs to be stopped. */
371 clocksource_stop_watchdog();
372 spin_unlock_irqrestore(&watchdog_lock, flags);
374 /* Needs to be done outside of watchdog lock */
375 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
376 list_del_init(&cs->wd_list);
377 __clocksource_change_rating(cs, 0);
382 static int clocksource_watchdog_kthread(void *data)
384 mutex_lock(&clocksource_mutex);
385 if (__clocksource_watchdog_kthread())
386 clocksource_select();
387 mutex_unlock(&clocksource_mutex);
391 static bool clocksource_is_watchdog(struct clocksource *cs)
393 return cs == watchdog;
396 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
398 static void clocksource_enqueue_watchdog(struct clocksource *cs)
400 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
401 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
404 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
405 static inline void clocksource_resume_watchdog(void) { }
406 static inline int __clocksource_watchdog_kthread(void) { return 0; }
407 static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
408 void clocksource_mark_unstable(struct clocksource *cs) { }
410 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
413 * clocksource_suspend - suspend the clocksource(s)
415 void clocksource_suspend(void)
417 struct clocksource *cs;
419 list_for_each_entry_reverse(cs, &clocksource_list, list)
425 * clocksource_resume - resume the clocksource(s)
427 void clocksource_resume(void)
429 struct clocksource *cs;
431 list_for_each_entry(cs, &clocksource_list, list)
435 clocksource_resume_watchdog();
439 * clocksource_touch_watchdog - Update watchdog
441 * Update the watchdog after exception contexts such as kgdb so as not
442 * to incorrectly trip the watchdog. This might fail when the kernel
443 * was stopped in code which holds watchdog_lock.
445 void clocksource_touch_watchdog(void)
447 clocksource_resume_watchdog();
451 * clocksource_max_adjustment- Returns max adjustment amount
452 * @cs: Pointer to clocksource
455 static u32 clocksource_max_adjustment(struct clocksource *cs)
459 * We won't try to correct for more than 11% adjustments (110,000 ppm),
461 ret = (u64)cs->mult * 11;
467 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
468 * @mult: cycle to nanosecond multiplier
469 * @shift: cycle to nanosecond divisor (power of two)
470 * @maxadj: maximum adjustment value to mult (~11%)
471 * @mask: bitmask for two's complement subtraction of non 64 bit counters
473 * NOTE: This function includes a safety margin of 50%, so that bad clock values
476 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask)
478 u64 max_nsecs, max_cycles;
481 * Calculate the maximum number of cycles that we can pass to the
482 * cyc2ns() function without overflowing a 64-bit result.
484 max_cycles = ULLONG_MAX;
485 do_div(max_cycles, mult+maxadj);
488 * The actual maximum number of cycles we can defer the clocksource is
489 * determined by the minimum of max_cycles and mask.
490 * Note: Here we subtract the maxadj to make sure we don't sleep for
491 * too long if there's a large negative adjustment.
493 max_cycles = min(max_cycles, mask);
494 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
496 /* Return 50% of the actual maximum, so we can detect bad values */
503 * clocksource_max_deferment - Returns max time the clocksource should be deferred
504 * @cs: Pointer to clocksource
507 static u64 clocksource_max_deferment(struct clocksource *cs)
511 max_nsecs = clocks_calc_max_nsecs(cs->mult, cs->shift, cs->maxadj,
516 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
518 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
520 struct clocksource *cs;
522 if (!finished_booting || list_empty(&clocksource_list))
526 * We pick the clocksource with the highest rating. If oneshot
527 * mode is active, we pick the highres valid clocksource with
530 list_for_each_entry(cs, &clocksource_list, list) {
531 if (skipcur && cs == curr_clocksource)
533 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
540 static void __clocksource_select(bool skipcur)
542 bool oneshot = tick_oneshot_mode_active();
543 struct clocksource *best, *cs;
545 /* Find the best suitable clocksource */
546 best = clocksource_find_best(oneshot, skipcur);
550 /* Check for the override clocksource. */
551 list_for_each_entry(cs, &clocksource_list, list) {
552 if (skipcur && cs == curr_clocksource)
554 if (strcmp(cs->name, override_name) != 0)
557 * Check to make sure we don't switch to a non-highres
558 * capable clocksource if the tick code is in oneshot
559 * mode (highres or nohz)
561 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
562 /* Override clocksource cannot be used. */
563 printk(KERN_WARNING "Override clocksource %s is not "
564 "HRT compatible. Cannot switch while in "
565 "HRT/NOHZ mode\n", cs->name);
566 override_name[0] = 0;
568 /* Override clocksource can be used. */
573 if (curr_clocksource != best && !timekeeping_notify(best)) {
574 pr_info("Switched to clocksource %s\n", best->name);
575 curr_clocksource = best;
580 * clocksource_select - Select the best clocksource available
582 * Private function. Must hold clocksource_mutex when called.
584 * Select the clocksource with the best rating, or the clocksource,
585 * which is selected by userspace override.
587 static void clocksource_select(void)
589 return __clocksource_select(false);
592 static void clocksource_select_fallback(void)
594 return __clocksource_select(true);
597 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
599 static inline void clocksource_select(void) { }
600 static inline void clocksource_select_fallback(void) { }
605 * clocksource_done_booting - Called near the end of core bootup
607 * Hack to avoid lots of clocksource churn at boot time.
608 * We use fs_initcall because we want this to start before
609 * device_initcall but after subsys_initcall.
611 static int __init clocksource_done_booting(void)
613 mutex_lock(&clocksource_mutex);
614 curr_clocksource = clocksource_default_clock();
615 finished_booting = 1;
617 * Run the watchdog first to eliminate unstable clock sources
619 __clocksource_watchdog_kthread();
620 clocksource_select();
621 mutex_unlock(&clocksource_mutex);
624 fs_initcall(clocksource_done_booting);
627 * Enqueue the clocksource sorted by rating
629 static void clocksource_enqueue(struct clocksource *cs)
631 struct list_head *entry = &clocksource_list;
632 struct clocksource *tmp;
634 list_for_each_entry(tmp, &clocksource_list, list)
635 /* Keep track of the place, where to insert */
636 if (tmp->rating >= cs->rating)
638 list_add(&cs->list, entry);
642 * __clocksource_updatefreq_scale - Used update clocksource with new freq
643 * @cs: clocksource to be registered
644 * @scale: Scale factor multiplied against freq to get clocksource hz
645 * @freq: clocksource frequency (cycles per second) divided by scale
647 * This should only be called from the clocksource->enable() method.
649 * This *SHOULD NOT* be called directly! Please use the
650 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
652 void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
656 * Calc the maximum number of seconds which we can run before
657 * wrapping around. For clocksources which have a mask > 32bit
658 * we need to limit the max sleep time to have a good
659 * conversion precision. 10 minutes is still a reasonable
660 * amount. That results in a shift value of 24 for a
661 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
662 * ~ 0.06ppm granularity for NTP.
669 else if (sec > 600 && cs->mask > UINT_MAX)
672 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
673 NSEC_PER_SEC / scale, sec * scale);
676 * Ensure clocksources that have large 'mult' values don't overflow
679 cs->maxadj = clocksource_max_adjustment(cs);
680 while ((cs->mult + cs->maxadj < cs->mult)
681 || (cs->mult - cs->maxadj > cs->mult)) {
684 cs->maxadj = clocksource_max_adjustment(cs);
687 cs->max_idle_ns = clocksource_max_deferment(cs);
689 EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
692 * __clocksource_register_scale - Used to install new clocksources
693 * @cs: clocksource to be registered
694 * @scale: Scale factor multiplied against freq to get clocksource hz
695 * @freq: clocksource frequency (cycles per second) divided by scale
697 * Returns -EBUSY if registration fails, zero otherwise.
699 * This *SHOULD NOT* be called directly! Please use the
700 * clocksource_register_hz() or clocksource_register_khz helper functions.
702 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
705 /* Initialize mult/shift and max_idle_ns */
706 __clocksource_updatefreq_scale(cs, scale, freq);
708 /* Add clocksource to the clocksource list */
709 mutex_lock(&clocksource_mutex);
710 clocksource_enqueue(cs);
711 clocksource_enqueue_watchdog(cs);
712 clocksource_select();
713 mutex_unlock(&clocksource_mutex);
716 EXPORT_SYMBOL_GPL(__clocksource_register_scale);
720 * clocksource_register - Used to install new clocksources
721 * @cs: clocksource to be registered
723 * Returns -EBUSY if registration fails, zero otherwise.
725 int clocksource_register(struct clocksource *cs)
727 /* calculate max adjustment for given mult/shift */
728 cs->maxadj = clocksource_max_adjustment(cs);
729 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
730 "Clocksource %s might overflow on 11%% adjustment\n",
733 /* calculate max idle time permitted for this clocksource */
734 cs->max_idle_ns = clocksource_max_deferment(cs);
736 mutex_lock(&clocksource_mutex);
737 clocksource_enqueue(cs);
738 clocksource_enqueue_watchdog(cs);
739 clocksource_select();
740 mutex_unlock(&clocksource_mutex);
743 EXPORT_SYMBOL(clocksource_register);
745 static void __clocksource_change_rating(struct clocksource *cs, int rating)
749 clocksource_enqueue(cs);
753 * clocksource_change_rating - Change the rating of a registered clocksource
754 * @cs: clocksource to be changed
755 * @rating: new rating
757 void clocksource_change_rating(struct clocksource *cs, int rating)
759 mutex_lock(&clocksource_mutex);
760 __clocksource_change_rating(cs, rating);
761 clocksource_select();
762 mutex_unlock(&clocksource_mutex);
764 EXPORT_SYMBOL(clocksource_change_rating);
767 * Unbind clocksource @cs. Called with clocksource_mutex held
769 static int clocksource_unbind(struct clocksource *cs)
772 * I really can't convince myself to support this on hardware
773 * designed by lobotomized monkeys.
775 if (clocksource_is_watchdog(cs))
778 if (cs == curr_clocksource) {
779 /* Select and try to install a replacement clock source */
780 clocksource_select_fallback();
781 if (curr_clocksource == cs)
784 clocksource_dequeue_watchdog(cs);
785 list_del_init(&cs->list);
790 * clocksource_unregister - remove a registered clocksource
791 * @cs: clocksource to be unregistered
793 int clocksource_unregister(struct clocksource *cs)
797 mutex_lock(&clocksource_mutex);
798 if (!list_empty(&cs->list))
799 ret = clocksource_unbind(cs);
800 mutex_unlock(&clocksource_mutex);
803 EXPORT_SYMBOL(clocksource_unregister);
807 * sysfs_show_current_clocksources - sysfs interface for current clocksource
810 * @buf: char buffer to be filled with clocksource list
812 * Provides sysfs interface for listing current clocksource.
815 sysfs_show_current_clocksources(struct device *dev,
816 struct device_attribute *attr, char *buf)
820 mutex_lock(&clocksource_mutex);
821 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
822 mutex_unlock(&clocksource_mutex);
827 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
831 /* strings from sysfs write are not 0 terminated! */
832 if (!cnt || cnt >= CS_NAME_LEN)
836 if (buf[cnt-1] == '\n')
839 memcpy(dst, buf, cnt);
845 * sysfs_override_clocksource - interface for manually overriding clocksource
848 * @buf: name of override clocksource
849 * @count: length of buffer
851 * Takes input from sysfs interface for manually overriding the default
852 * clocksource selection.
854 static ssize_t sysfs_override_clocksource(struct device *dev,
855 struct device_attribute *attr,
856 const char *buf, size_t count)
860 mutex_lock(&clocksource_mutex);
862 ret = sysfs_get_uname(buf, override_name, count);
864 clocksource_select();
866 mutex_unlock(&clocksource_mutex);
872 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
876 * @count: length of buffer
878 * Takes input from sysfs interface for manually unbinding a clocksource.
880 static ssize_t sysfs_unbind_clocksource(struct device *dev,
881 struct device_attribute *attr,
882 const char *buf, size_t count)
884 struct clocksource *cs;
885 char name[CS_NAME_LEN];
888 ret = sysfs_get_uname(buf, name, count);
893 mutex_lock(&clocksource_mutex);
894 list_for_each_entry(cs, &clocksource_list, list) {
895 if (strcmp(cs->name, name))
897 ret = clocksource_unbind(cs);
900 mutex_unlock(&clocksource_mutex);
902 return ret ? ret : count;
906 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
909 * @buf: char buffer to be filled with clocksource list
911 * Provides sysfs interface for listing registered clocksources
914 sysfs_show_available_clocksources(struct device *dev,
915 struct device_attribute *attr,
918 struct clocksource *src;
921 mutex_lock(&clocksource_mutex);
922 list_for_each_entry(src, &clocksource_list, list) {
924 * Don't show non-HRES clocksource if the tick code is
925 * in one shot mode (highres=on or nohz=on)
927 if (!tick_oneshot_mode_active() ||
928 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
929 count += snprintf(buf + count,
930 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
933 mutex_unlock(&clocksource_mutex);
935 count += snprintf(buf + count,
936 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
944 static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
945 sysfs_override_clocksource);
947 static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
949 static DEVICE_ATTR(available_clocksource, 0444,
950 sysfs_show_available_clocksources, NULL);
952 static struct bus_type clocksource_subsys = {
953 .name = "clocksource",
954 .dev_name = "clocksource",
957 static struct device device_clocksource = {
959 .bus = &clocksource_subsys,
962 static int __init init_clocksource_sysfs(void)
964 int error = subsys_system_register(&clocksource_subsys, NULL);
967 error = device_register(&device_clocksource);
969 error = device_create_file(
971 &dev_attr_current_clocksource);
973 error = device_create_file(&device_clocksource,
974 &dev_attr_unbind_clocksource);
976 error = device_create_file(
978 &dev_attr_available_clocksource);
982 device_initcall(init_clocksource_sysfs);
983 #endif /* CONFIG_SYSFS */
986 * boot_override_clocksource - boot clock override
987 * @str: override name
989 * Takes a clocksource= boot argument and uses it
990 * as the clocksource override name.
992 static int __init boot_override_clocksource(char* str)
994 mutex_lock(&clocksource_mutex);
996 strlcpy(override_name, str, sizeof(override_name));
997 mutex_unlock(&clocksource_mutex);
1001 __setup("clocksource=", boot_override_clocksource);
1004 * boot_override_clock - Compatibility layer for deprecated boot option
1005 * @str: override name
1007 * DEPRECATED! Takes a clock= boot argument and uses it
1008 * as the clocksource override name
1010 static int __init boot_override_clock(char* str)
1012 if (!strcmp(str, "pmtmr")) {
1013 printk("Warning: clock=pmtmr is deprecated. "
1014 "Use clocksource=acpi_pm.\n");
1015 return boot_override_clocksource("acpi_pm");
1017 printk("Warning! clock= boot option is deprecated. "
1018 "Use clocksource=xyz\n");
1019 return boot_override_clocksource(str);
1022 __setup("clock=", boot_override_clock);