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/clocksource.h>
27 #include <linux/sysdev.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 void timecounter_init(struct timecounter
*tc
,
35 const struct cyclecounter
*cc
,
39 tc
->cycle_last
= cc
->read(cc
);
40 tc
->nsec
= start_tstamp
;
42 EXPORT_SYMBOL(timecounter_init
);
45 * timecounter_read_delta - get nanoseconds since last call of this function
46 * @tc: Pointer to time counter
48 * When the underlying cycle counter runs over, this will be handled
49 * correctly as long as it does not run over more than once between
52 * The first call to this function for a new time counter initializes
53 * the time tracking and returns an undefined result.
55 static u64
timecounter_read_delta(struct timecounter
*tc
)
57 cycle_t cycle_now
, cycle_delta
;
60 /* read cycle counter: */
61 cycle_now
= tc
->cc
->read(tc
->cc
);
63 /* calculate the delta since the last timecounter_read_delta(): */
64 cycle_delta
= (cycle_now
- tc
->cycle_last
) & tc
->cc
->mask
;
66 /* convert to nanoseconds: */
67 ns_offset
= cyclecounter_cyc2ns(tc
->cc
, cycle_delta
);
69 /* update time stamp of timecounter_read_delta() call: */
70 tc
->cycle_last
= cycle_now
;
75 u64
timecounter_read(struct timecounter
*tc
)
79 /* increment time by nanoseconds since last call */
80 nsec
= timecounter_read_delta(tc
);
86 EXPORT_SYMBOL(timecounter_read
);
88 u64
timecounter_cyc2time(struct timecounter
*tc
,
91 u64 cycle_delta
= (cycle_tstamp
- tc
->cycle_last
) & tc
->cc
->mask
;
95 * Instead of always treating cycle_tstamp as more recent
96 * than tc->cycle_last, detect when it is too far in the
97 * future and treat it as old time stamp instead.
99 if (cycle_delta
> tc
->cc
->mask
/ 2) {
100 cycle_delta
= (tc
->cycle_last
- cycle_tstamp
) & tc
->cc
->mask
;
101 nsec
= tc
->nsec
- cyclecounter_cyc2ns(tc
->cc
, cycle_delta
);
103 nsec
= cyclecounter_cyc2ns(tc
->cc
, cycle_delta
) + tc
->nsec
;
108 EXPORT_SYMBOL(timecounter_cyc2time
);
110 /*[Clocksource internal variables]---------
112 * currently selected clocksource.
114 * linked list with the registered clocksources
116 * protects manipulations to curr_clocksource and the clocksource_list
118 * Name of the user-specified clocksource.
120 static struct clocksource
*curr_clocksource
;
121 static LIST_HEAD(clocksource_list
);
122 static DEFINE_MUTEX(clocksource_mutex
);
123 static char override_name
[32];
124 static int finished_booting
;
126 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
127 static void clocksource_watchdog_work(struct work_struct
*work
);
129 static LIST_HEAD(watchdog_list
);
130 static struct clocksource
*watchdog
;
131 static struct timer_list watchdog_timer
;
132 static DECLARE_WORK(watchdog_work
, clocksource_watchdog_work
);
133 static DEFINE_SPINLOCK(watchdog_lock
);
134 static int watchdog_running
;
136 static int clocksource_watchdog_kthread(void *data
);
137 static void __clocksource_change_rating(struct clocksource
*cs
, int rating
);
140 * Interval: 0.5sec Threshold: 0.0625s
142 #define WATCHDOG_INTERVAL (HZ >> 1)
143 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
145 static void clocksource_watchdog_work(struct work_struct
*work
)
148 * If kthread_run fails the next watchdog scan over the
149 * watchdog_list will find the unstable clock again.
151 kthread_run(clocksource_watchdog_kthread
, NULL
, "kwatchdog");
154 static void __clocksource_unstable(struct clocksource
*cs
)
156 cs
->flags
&= ~(CLOCK_SOURCE_VALID_FOR_HRES
| CLOCK_SOURCE_WATCHDOG
);
157 cs
->flags
|= CLOCK_SOURCE_UNSTABLE
;
158 if (finished_booting
)
159 schedule_work(&watchdog_work
);
162 static void clocksource_unstable(struct clocksource
*cs
, int64_t delta
)
164 printk(KERN_WARNING
"Clocksource %s unstable (delta = %Ld ns)\n",
166 __clocksource_unstable(cs
);
170 * clocksource_mark_unstable - mark clocksource unstable via watchdog
171 * @cs: clocksource to be marked unstable
173 * This function is called instead of clocksource_change_rating from
174 * cpu hotplug code to avoid a deadlock between the clocksource mutex
175 * and the cpu hotplug mutex. It defers the update of the clocksource
176 * to the watchdog thread.
178 void clocksource_mark_unstable(struct clocksource
*cs
)
182 spin_lock_irqsave(&watchdog_lock
, flags
);
183 if (!(cs
->flags
& CLOCK_SOURCE_UNSTABLE
)) {
184 if (list_empty(&cs
->wd_list
))
185 list_add(&cs
->wd_list
, &watchdog_list
);
186 __clocksource_unstable(cs
);
188 spin_unlock_irqrestore(&watchdog_lock
, flags
);
191 static void clocksource_watchdog(unsigned long data
)
193 struct clocksource
*cs
;
194 cycle_t csnow
, wdnow
;
195 int64_t wd_nsec
, cs_nsec
;
198 spin_lock(&watchdog_lock
);
199 if (!watchdog_running
)
202 list_for_each_entry(cs
, &watchdog_list
, wd_list
) {
204 /* Clocksource already marked unstable? */
205 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
) {
206 if (finished_booting
)
207 schedule_work(&watchdog_work
);
212 csnow
= cs
->read(cs
);
213 wdnow
= watchdog
->read(watchdog
);
216 /* Clocksource initialized ? */
217 if (!(cs
->flags
& CLOCK_SOURCE_WATCHDOG
)) {
218 cs
->flags
|= CLOCK_SOURCE_WATCHDOG
;
224 wd_nsec
= clocksource_cyc2ns((wdnow
- cs
->wd_last
) & watchdog
->mask
,
225 watchdog
->mult
, watchdog
->shift
);
227 cs_nsec
= clocksource_cyc2ns((csnow
- cs
->cs_last
) &
228 cs
->mask
, cs
->mult
, cs
->shift
);
232 /* Check the deviation from the watchdog clocksource. */
233 if (abs(cs_nsec
- wd_nsec
) > WATCHDOG_THRESHOLD
) {
234 clocksource_unstable(cs
, cs_nsec
- wd_nsec
);
238 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) &&
239 (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
) &&
240 (watchdog
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)) {
241 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
243 * We just marked the clocksource as highres-capable,
244 * notify the rest of the system as well so that we
245 * transition into high-res mode:
252 * Cycle through CPUs to check if the CPUs stay synchronized
255 next_cpu
= cpumask_next(raw_smp_processor_id(), cpu_online_mask
);
256 if (next_cpu
>= nr_cpu_ids
)
257 next_cpu
= cpumask_first(cpu_online_mask
);
258 watchdog_timer
.expires
+= WATCHDOG_INTERVAL
;
259 add_timer_on(&watchdog_timer
, next_cpu
);
261 spin_unlock(&watchdog_lock
);
264 static inline void clocksource_start_watchdog(void)
266 if (watchdog_running
|| !watchdog
|| list_empty(&watchdog_list
))
268 init_timer(&watchdog_timer
);
269 watchdog_timer
.function
= clocksource_watchdog
;
270 watchdog_timer
.expires
= jiffies
+ WATCHDOG_INTERVAL
;
271 add_timer_on(&watchdog_timer
, cpumask_first(cpu_online_mask
));
272 watchdog_running
= 1;
275 static inline void clocksource_stop_watchdog(void)
277 if (!watchdog_running
|| (watchdog
&& !list_empty(&watchdog_list
)))
279 del_timer(&watchdog_timer
);
280 watchdog_running
= 0;
283 static inline void clocksource_reset_watchdog(void)
285 struct clocksource
*cs
;
287 list_for_each_entry(cs
, &watchdog_list
, wd_list
)
288 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
291 static void clocksource_resume_watchdog(void)
295 spin_lock_irqsave(&watchdog_lock
, flags
);
296 clocksource_reset_watchdog();
297 spin_unlock_irqrestore(&watchdog_lock
, flags
);
300 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
304 spin_lock_irqsave(&watchdog_lock
, flags
);
305 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
306 /* cs is a clocksource to be watched. */
307 list_add(&cs
->wd_list
, &watchdog_list
);
308 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
310 /* cs is a watchdog. */
311 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
312 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
313 /* Pick the best watchdog. */
314 if (!watchdog
|| cs
->rating
> watchdog
->rating
) {
316 /* Reset watchdog cycles */
317 clocksource_reset_watchdog();
320 /* Check if the watchdog timer needs to be started. */
321 clocksource_start_watchdog();
322 spin_unlock_irqrestore(&watchdog_lock
, flags
);
325 static void clocksource_dequeue_watchdog(struct clocksource
*cs
)
327 struct clocksource
*tmp
;
330 spin_lock_irqsave(&watchdog_lock
, flags
);
331 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
332 /* cs is a watched clocksource. */
333 list_del_init(&cs
->wd_list
);
334 } else if (cs
== watchdog
) {
335 /* Reset watchdog cycles */
336 clocksource_reset_watchdog();
337 /* Current watchdog is removed. Find an alternative. */
339 list_for_each_entry(tmp
, &clocksource_list
, list
) {
340 if (tmp
== cs
|| tmp
->flags
& CLOCK_SOURCE_MUST_VERIFY
)
342 if (!watchdog
|| tmp
->rating
> watchdog
->rating
)
346 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
347 /* Check if the watchdog timer needs to be stopped. */
348 clocksource_stop_watchdog();
349 spin_unlock_irqrestore(&watchdog_lock
, flags
);
352 static int clocksource_watchdog_kthread(void *data
)
354 struct clocksource
*cs
, *tmp
;
358 mutex_lock(&clocksource_mutex
);
359 spin_lock_irqsave(&watchdog_lock
, flags
);
360 list_for_each_entry_safe(cs
, tmp
, &watchdog_list
, wd_list
)
361 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
) {
362 list_del_init(&cs
->wd_list
);
363 list_add(&cs
->wd_list
, &unstable
);
365 /* Check if the watchdog timer needs to be stopped. */
366 clocksource_stop_watchdog();
367 spin_unlock_irqrestore(&watchdog_lock
, flags
);
369 /* Needs to be done outside of watchdog lock */
370 list_for_each_entry_safe(cs
, tmp
, &unstable
, wd_list
) {
371 list_del_init(&cs
->wd_list
);
372 __clocksource_change_rating(cs
, 0);
374 mutex_unlock(&clocksource_mutex
);
378 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
380 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
382 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
383 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
386 static inline void clocksource_dequeue_watchdog(struct clocksource
*cs
) { }
387 static inline void clocksource_resume_watchdog(void) { }
388 static inline int clocksource_watchdog_kthread(void *data
) { return 0; }
390 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
393 * clocksource_resume - resume the clocksource(s)
395 void clocksource_resume(void)
397 struct clocksource
*cs
;
399 list_for_each_entry(cs
, &clocksource_list
, list
)
403 clocksource_resume_watchdog();
407 * clocksource_touch_watchdog - Update watchdog
409 * Update the watchdog after exception contexts such as kgdb so as not
410 * to incorrectly trip the watchdog.
413 void clocksource_touch_watchdog(void)
415 clocksource_resume_watchdog();
419 * clocksource_max_deferment - Returns max time the clocksource can be deferred
420 * @cs: Pointer to clocksource
423 static u64
clocksource_max_deferment(struct clocksource
*cs
)
425 u64 max_nsecs
, max_cycles
;
428 * Calculate the maximum number of cycles that we can pass to the
429 * cyc2ns function without overflowing a 64-bit signed result. The
430 * maximum number of cycles is equal to ULLONG_MAX/cs->mult which
431 * is equivalent to the below.
432 * max_cycles < (2^63)/cs->mult
433 * max_cycles < 2^(log2((2^63)/cs->mult))
434 * max_cycles < 2^(log2(2^63) - log2(cs->mult))
435 * max_cycles < 2^(63 - log2(cs->mult))
436 * max_cycles < 1 << (63 - log2(cs->mult))
437 * Please note that we add 1 to the result of the log2 to account for
438 * any rounding errors, ensure the above inequality is satisfied and
439 * no overflow will occur.
441 max_cycles
= 1ULL << (63 - (ilog2(cs
->mult
) + 1));
444 * The actual maximum number of cycles we can defer the clocksource is
445 * determined by the minimum of max_cycles and cs->mask.
447 max_cycles
= min_t(u64
, max_cycles
, (u64
) cs
->mask
);
448 max_nsecs
= clocksource_cyc2ns(max_cycles
, cs
->mult
, cs
->shift
);
451 * To ensure that the clocksource does not wrap whilst we are idle,
452 * limit the time the clocksource can be deferred by 12.5%. Please
453 * note a margin of 12.5% is used because this can be computed with
454 * a shift, versus say 10% which would require division.
456 return max_nsecs
- (max_nsecs
>> 5);
459 #ifdef CONFIG_GENERIC_TIME
462 * clocksource_select - Select the best clocksource available
464 * Private function. Must hold clocksource_mutex when called.
466 * Select the clocksource with the best rating, or the clocksource,
467 * which is selected by userspace override.
469 static void clocksource_select(void)
471 struct clocksource
*best
, *cs
;
473 if (!finished_booting
|| list_empty(&clocksource_list
))
475 /* First clocksource on the list has the best rating. */
476 best
= list_first_entry(&clocksource_list
, struct clocksource
, list
);
477 /* Check for the override clocksource. */
478 list_for_each_entry(cs
, &clocksource_list
, list
) {
479 if (strcmp(cs
->name
, override_name
) != 0)
482 * Check to make sure we don't switch to a non-highres
483 * capable clocksource if the tick code is in oneshot
484 * mode (highres or nohz)
486 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) &&
487 tick_oneshot_mode_active()) {
488 /* Override clocksource cannot be used. */
489 printk(KERN_WARNING
"Override clocksource %s is not "
490 "HRT compatible. Cannot switch while in "
491 "HRT/NOHZ mode\n", cs
->name
);
492 override_name
[0] = 0;
494 /* Override clocksource can be used. */
498 if (curr_clocksource
!= best
) {
499 printk(KERN_INFO
"Switching to clocksource %s\n", best
->name
);
500 curr_clocksource
= best
;
501 timekeeping_notify(curr_clocksource
);
505 #else /* CONFIG_GENERIC_TIME */
507 static inline void clocksource_select(void) { }
512 * clocksource_done_booting - Called near the end of core bootup
514 * Hack to avoid lots of clocksource churn at boot time.
515 * We use fs_initcall because we want this to start before
516 * device_initcall but after subsys_initcall.
518 static int __init
clocksource_done_booting(void)
520 mutex_lock(&clocksource_mutex
);
521 curr_clocksource
= clocksource_default_clock();
522 mutex_unlock(&clocksource_mutex
);
524 finished_booting
= 1;
527 * Run the watchdog first to eliminate unstable clock sources
529 clocksource_watchdog_kthread(NULL
);
531 mutex_lock(&clocksource_mutex
);
532 clocksource_select();
533 mutex_unlock(&clocksource_mutex
);
536 fs_initcall(clocksource_done_booting
);
539 * Enqueue the clocksource sorted by rating
541 static void clocksource_enqueue(struct clocksource
*cs
)
543 struct list_head
*entry
= &clocksource_list
;
544 struct clocksource
*tmp
;
546 list_for_each_entry(tmp
, &clocksource_list
, list
)
547 /* Keep track of the place, where to insert */
548 if (tmp
->rating
>= cs
->rating
)
550 list_add(&cs
->list
, entry
);
554 * clocksource_register - Used to install new clocksources
555 * @t: clocksource to be registered
557 * Returns -EBUSY if registration fails, zero otherwise.
559 int clocksource_register(struct clocksource
*cs
)
561 /* calculate max idle time permitted for this clocksource */
562 cs
->max_idle_ns
= clocksource_max_deferment(cs
);
564 mutex_lock(&clocksource_mutex
);
565 clocksource_enqueue(cs
);
566 clocksource_enqueue_watchdog(cs
);
567 clocksource_select();
568 mutex_unlock(&clocksource_mutex
);
571 EXPORT_SYMBOL(clocksource_register
);
573 static void __clocksource_change_rating(struct clocksource
*cs
, int rating
)
577 clocksource_enqueue(cs
);
578 clocksource_select();
582 * clocksource_change_rating - Change the rating of a registered clocksource
584 void clocksource_change_rating(struct clocksource
*cs
, int rating
)
586 mutex_lock(&clocksource_mutex
);
587 __clocksource_change_rating(cs
, rating
);
588 mutex_unlock(&clocksource_mutex
);
590 EXPORT_SYMBOL(clocksource_change_rating
);
593 * clocksource_unregister - remove a registered clocksource
595 void clocksource_unregister(struct clocksource
*cs
)
597 mutex_lock(&clocksource_mutex
);
598 clocksource_dequeue_watchdog(cs
);
600 clocksource_select();
601 mutex_unlock(&clocksource_mutex
);
603 EXPORT_SYMBOL(clocksource_unregister
);
607 * sysfs_show_current_clocksources - sysfs interface for current clocksource
609 * @buf: char buffer to be filled with clocksource list
611 * Provides sysfs interface for listing current clocksource.
614 sysfs_show_current_clocksources(struct sys_device
*dev
,
615 struct sysdev_attribute
*attr
, char *buf
)
619 mutex_lock(&clocksource_mutex
);
620 count
= snprintf(buf
, PAGE_SIZE
, "%s\n", curr_clocksource
->name
);
621 mutex_unlock(&clocksource_mutex
);
627 * sysfs_override_clocksource - interface for manually overriding clocksource
629 * @buf: name of override clocksource
630 * @count: length of buffer
632 * Takes input from sysfs interface for manually overriding the default
633 * clocksource selction.
635 static ssize_t
sysfs_override_clocksource(struct sys_device
*dev
,
636 struct sysdev_attribute
*attr
,
637 const char *buf
, size_t count
)
641 /* strings from sysfs write are not 0 terminated! */
642 if (count
>= sizeof(override_name
))
646 if (buf
[count
-1] == '\n')
649 mutex_lock(&clocksource_mutex
);
652 memcpy(override_name
, buf
, count
);
653 override_name
[count
] = 0;
654 clocksource_select();
656 mutex_unlock(&clocksource_mutex
);
662 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
664 * @buf: char buffer to be filled with clocksource list
666 * Provides sysfs interface for listing registered clocksources
669 sysfs_show_available_clocksources(struct sys_device
*dev
,
670 struct sysdev_attribute
*attr
,
673 struct clocksource
*src
;
676 mutex_lock(&clocksource_mutex
);
677 list_for_each_entry(src
, &clocksource_list
, list
) {
679 * Don't show non-HRES clocksource if the tick code is
680 * in one shot mode (highres=on or nohz=on)
682 if (!tick_oneshot_mode_active() ||
683 (src
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
))
684 count
+= snprintf(buf
+ count
,
685 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0),
688 mutex_unlock(&clocksource_mutex
);
690 count
+= snprintf(buf
+ count
,
691 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0), "\n");
699 static SYSDEV_ATTR(current_clocksource
, 0644, sysfs_show_current_clocksources
,
700 sysfs_override_clocksource
);
702 static SYSDEV_ATTR(available_clocksource
, 0444,
703 sysfs_show_available_clocksources
, NULL
);
705 static struct sysdev_class clocksource_sysclass
= {
706 .name
= "clocksource",
709 static struct sys_device device_clocksource
= {
711 .cls
= &clocksource_sysclass
,
714 static int __init
init_clocksource_sysfs(void)
716 int error
= sysdev_class_register(&clocksource_sysclass
);
719 error
= sysdev_register(&device_clocksource
);
721 error
= sysdev_create_file(
723 &attr_current_clocksource
);
725 error
= sysdev_create_file(
727 &attr_available_clocksource
);
731 device_initcall(init_clocksource_sysfs
);
732 #endif /* CONFIG_SYSFS */
735 * boot_override_clocksource - boot clock override
736 * @str: override name
738 * Takes a clocksource= boot argument and uses it
739 * as the clocksource override name.
741 static int __init
boot_override_clocksource(char* str
)
743 mutex_lock(&clocksource_mutex
);
745 strlcpy(override_name
, str
, sizeof(override_name
));
746 mutex_unlock(&clocksource_mutex
);
750 __setup("clocksource=", boot_override_clocksource
);
753 * boot_override_clock - Compatibility layer for deprecated boot option
754 * @str: override name
756 * DEPRECATED! Takes a clock= boot argument and uses it
757 * as the clocksource override name
759 static int __init
boot_override_clock(char* str
)
761 if (!strcmp(str
, "pmtmr")) {
762 printk("Warning: clock=pmtmr is deprecated. "
763 "Use clocksource=acpi_pm.\n");
764 return boot_override_clocksource("acpi_pm");
766 printk("Warning! clock= boot option is deprecated. "
767 "Use clocksource=xyz\n");
768 return boot_override_clocksource(str
);
771 __setup("clock=", boot_override_clock
);