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>
33 void timecounter_init(struct timecounter
*tc
,
34 const struct cyclecounter
*cc
,
38 tc
->cycle_last
= cc
->read(cc
);
39 tc
->nsec
= start_tstamp
;
41 EXPORT_SYMBOL(timecounter_init
);
44 * timecounter_read_delta - get nanoseconds since last call of this function
45 * @tc: Pointer to time counter
47 * When the underlying cycle counter runs over, this will be handled
48 * correctly as long as it does not run over more than once between
51 * The first call to this function for a new time counter initializes
52 * the time tracking and returns an undefined result.
54 static u64
timecounter_read_delta(struct timecounter
*tc
)
56 cycle_t cycle_now
, cycle_delta
;
59 /* read cycle counter: */
60 cycle_now
= tc
->cc
->read(tc
->cc
);
62 /* calculate the delta since the last timecounter_read_delta(): */
63 cycle_delta
= (cycle_now
- tc
->cycle_last
) & tc
->cc
->mask
;
65 /* convert to nanoseconds: */
66 ns_offset
= cyclecounter_cyc2ns(tc
->cc
, cycle_delta
);
68 /* update time stamp of timecounter_read_delta() call: */
69 tc
->cycle_last
= cycle_now
;
74 u64
timecounter_read(struct timecounter
*tc
)
78 /* increment time by nanoseconds since last call */
79 nsec
= timecounter_read_delta(tc
);
85 EXPORT_SYMBOL(timecounter_read
);
87 u64
timecounter_cyc2time(struct timecounter
*tc
,
90 u64 cycle_delta
= (cycle_tstamp
- tc
->cycle_last
) & tc
->cc
->mask
;
94 * Instead of always treating cycle_tstamp as more recent
95 * than tc->cycle_last, detect when it is too far in the
96 * future and treat it as old time stamp instead.
98 if (cycle_delta
> tc
->cc
->mask
/ 2) {
99 cycle_delta
= (tc
->cycle_last
- cycle_tstamp
) & tc
->cc
->mask
;
100 nsec
= tc
->nsec
- cyclecounter_cyc2ns(tc
->cc
, cycle_delta
);
102 nsec
= cyclecounter_cyc2ns(tc
->cc
, cycle_delta
) + tc
->nsec
;
107 EXPORT_SYMBOL(timecounter_cyc2time
);
109 /*[Clocksource internal variables]---------
111 * currently selected clocksource.
113 * linked list with the registered clocksources
115 * protects manipulations to curr_clocksource and the clocksource_list
117 * Name of the user-specified clocksource.
119 static struct clocksource
*curr_clocksource
;
120 static LIST_HEAD(clocksource_list
);
121 static DEFINE_MUTEX(clocksource_mutex
);
122 static char override_name
[32];
124 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
125 static LIST_HEAD(watchdog_list
);
126 static struct clocksource
*watchdog
;
127 static struct timer_list watchdog_timer
;
128 static struct work_struct watchdog_work
;
129 static DEFINE_SPINLOCK(watchdog_lock
);
130 static cycle_t watchdog_last
;
131 static int watchdog_running
;
133 static void clocksource_watchdog_work(struct work_struct
*work
);
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_unstable(struct clocksource
*cs
, int64_t delta
)
143 printk(KERN_WARNING
"Clocksource %s unstable (delta = %Ld ns)\n",
145 cs
->flags
&= ~(CLOCK_SOURCE_VALID_FOR_HRES
| CLOCK_SOURCE_WATCHDOG
);
146 cs
->flags
|= CLOCK_SOURCE_UNSTABLE
;
147 schedule_work(&watchdog_work
);
150 static void clocksource_watchdog(unsigned long data
)
152 struct clocksource
*cs
;
153 cycle_t csnow
, wdnow
;
154 int64_t wd_nsec
, cs_nsec
;
157 spin_lock(&watchdog_lock
);
158 if (!watchdog_running
)
161 wdnow
= watchdog
->read(watchdog
);
162 wd_nsec
= clocksource_cyc2ns((wdnow
- watchdog_last
) & watchdog
->mask
,
163 watchdog
->mult
, watchdog
->shift
);
164 watchdog_last
= wdnow
;
166 list_for_each_entry(cs
, &watchdog_list
, wd_list
) {
168 /* Clocksource already marked unstable? */
169 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
)
172 csnow
= cs
->read(cs
);
174 /* Clocksource initialized ? */
175 if (!(cs
->flags
& CLOCK_SOURCE_WATCHDOG
)) {
176 cs
->flags
|= CLOCK_SOURCE_WATCHDOG
;
181 /* Check the deviation from the watchdog clocksource. */
182 cs_nsec
= clocksource_cyc2ns((csnow
- cs
->wd_last
) &
183 cs
->mask
, cs
->mult
, cs
->shift
);
185 if (abs(cs_nsec
- wd_nsec
) > WATCHDOG_THRESHOLD
) {
186 clocksource_unstable(cs
, cs_nsec
- wd_nsec
);
190 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) &&
191 (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
) &&
192 (watchdog
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)) {
193 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
195 * We just marked the clocksource as highres-capable,
196 * notify the rest of the system as well so that we
197 * transition into high-res mode:
204 * Cycle through CPUs to check if the CPUs stay synchronized
207 next_cpu
= cpumask_next(raw_smp_processor_id(), cpu_online_mask
);
208 if (next_cpu
>= nr_cpu_ids
)
209 next_cpu
= cpumask_first(cpu_online_mask
);
210 watchdog_timer
.expires
+= WATCHDOG_INTERVAL
;
211 add_timer_on(&watchdog_timer
, next_cpu
);
213 spin_unlock(&watchdog_lock
);
216 static inline void clocksource_start_watchdog(void)
218 if (watchdog_running
|| !watchdog
|| list_empty(&watchdog_list
))
220 INIT_WORK(&watchdog_work
, clocksource_watchdog_work
);
221 init_timer(&watchdog_timer
);
222 watchdog_timer
.function
= clocksource_watchdog
;
223 watchdog_last
= watchdog
->read(watchdog
);
224 watchdog_timer
.expires
= jiffies
+ WATCHDOG_INTERVAL
;
225 add_timer_on(&watchdog_timer
, cpumask_first(cpu_online_mask
));
226 watchdog_running
= 1;
229 static inline void clocksource_stop_watchdog(void)
231 if (!watchdog_running
|| (watchdog
&& !list_empty(&watchdog_list
)))
233 del_timer(&watchdog_timer
);
234 watchdog_running
= 0;
237 static inline void clocksource_reset_watchdog(void)
239 struct clocksource
*cs
;
241 list_for_each_entry(cs
, &watchdog_list
, wd_list
)
242 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
245 static void clocksource_resume_watchdog(void)
249 spin_lock_irqsave(&watchdog_lock
, flags
);
250 clocksource_reset_watchdog();
251 spin_unlock_irqrestore(&watchdog_lock
, flags
);
254 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
258 spin_lock_irqsave(&watchdog_lock
, flags
);
259 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
260 /* cs is a clocksource to be watched. */
261 list_add(&cs
->wd_list
, &watchdog_list
);
262 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
264 /* cs is a watchdog. */
265 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
266 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
267 /* Pick the best watchdog. */
268 if (!watchdog
|| cs
->rating
> watchdog
->rating
) {
270 /* Reset watchdog cycles */
271 clocksource_reset_watchdog();
274 /* Check if the watchdog timer needs to be started. */
275 clocksource_start_watchdog();
276 spin_unlock_irqrestore(&watchdog_lock
, flags
);
279 static void clocksource_dequeue_watchdog(struct clocksource
*cs
)
281 struct clocksource
*tmp
;
284 spin_lock_irqsave(&watchdog_lock
, flags
);
285 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
286 /* cs is a watched clocksource. */
287 list_del_init(&cs
->wd_list
);
288 } else if (cs
== watchdog
) {
289 /* Reset watchdog cycles */
290 clocksource_reset_watchdog();
291 /* Current watchdog is removed. Find an alternative. */
293 list_for_each_entry(tmp
, &clocksource_list
, list
) {
294 if (tmp
== cs
|| tmp
->flags
& CLOCK_SOURCE_MUST_VERIFY
)
296 if (!watchdog
|| tmp
->rating
> watchdog
->rating
)
300 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
301 /* Check if the watchdog timer needs to be stopped. */
302 clocksource_stop_watchdog();
303 spin_unlock_irqrestore(&watchdog_lock
, flags
);
306 static void clocksource_watchdog_work(struct work_struct
*work
)
308 struct clocksource
*cs
, *tmp
;
312 spin_lock_irqsave(&watchdog_lock
, flags
);
313 list_for_each_entry_safe(cs
, tmp
, &watchdog_list
, wd_list
)
314 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
) {
315 list_del_init(&cs
->wd_list
);
316 list_add(&cs
->wd_list
, &unstable
);
318 /* Check if the watchdog timer needs to be stopped. */
319 clocksource_stop_watchdog();
320 spin_unlock_irqrestore(&watchdog_lock
, flags
);
322 /* Needs to be done outside of watchdog lock */
323 list_for_each_entry_safe(cs
, tmp
, &unstable
, wd_list
) {
324 list_del_init(&cs
->wd_list
);
325 clocksource_change_rating(cs
, 0);
329 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
331 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
333 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
334 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
337 static inline void clocksource_dequeue_watchdog(struct clocksource
*cs
) { }
338 static inline void clocksource_resume_watchdog(void) { }
340 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
343 * clocksource_resume - resume the clocksource(s)
345 void clocksource_resume(void)
347 struct clocksource
*cs
;
349 mutex_lock(&clocksource_mutex
);
351 list_for_each_entry(cs
, &clocksource_list
, list
)
355 clocksource_resume_watchdog();
357 mutex_unlock(&clocksource_mutex
);
361 * clocksource_touch_watchdog - Update watchdog
363 * Update the watchdog after exception contexts such as kgdb so as not
364 * to incorrectly trip the watchdog.
367 void clocksource_touch_watchdog(void)
369 clocksource_resume_watchdog();
372 #ifdef CONFIG_GENERIC_TIME
374 static int finished_booting
;
377 * clocksource_select - Select the best clocksource available
379 * Private function. Must hold clocksource_mutex when called.
381 * Select the clocksource with the best rating, or the clocksource,
382 * which is selected by userspace override.
384 static void clocksource_select(void)
386 struct clocksource
*best
, *cs
;
388 if (!finished_booting
|| list_empty(&clocksource_list
))
390 /* First clocksource on the list has the best rating. */
391 best
= list_first_entry(&clocksource_list
, struct clocksource
, list
);
392 /* Check for the override clocksource. */
393 list_for_each_entry(cs
, &clocksource_list
, list
) {
394 if (strcmp(cs
->name
, override_name
) != 0)
397 * Check to make sure we don't switch to a non-highres
398 * capable clocksource if the tick code is in oneshot
399 * mode (highres or nohz)
401 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) &&
402 tick_oneshot_mode_active()) {
403 /* Override clocksource cannot be used. */
404 printk(KERN_WARNING
"Override clocksource %s is not "
405 "HRT compatible. Cannot switch while in "
406 "HRT/NOHZ mode\n", cs
->name
);
407 override_name
[0] = 0;
409 /* Override clocksource can be used. */
413 if (curr_clocksource
!= best
) {
414 printk(KERN_INFO
"Switching to clocksource %s\n", best
->name
);
415 curr_clocksource
= best
;
416 timekeeping_notify(curr_clocksource
);
421 * clocksource_done_booting - Called near the end of core bootup
423 * Hack to avoid lots of clocksource churn at boot time.
424 * We use fs_initcall because we want this to start before
425 * device_initcall but after subsys_initcall.
427 static int __init
clocksource_done_booting(void)
429 finished_booting
= 1;
430 clocksource_select();
433 fs_initcall(clocksource_done_booting
);
435 #else /* CONFIG_GENERIC_TIME */
437 static inline void clocksource_select(void) { }
442 * Enqueue the clocksource sorted by rating
444 static void clocksource_enqueue(struct clocksource
*cs
)
446 struct list_head
*entry
= &clocksource_list
;
447 struct clocksource
*tmp
;
449 list_for_each_entry(tmp
, &clocksource_list
, list
)
450 /* Keep track of the place, where to insert */
451 if (tmp
->rating
>= cs
->rating
)
453 list_add(&cs
->list
, entry
);
457 * clocksource_register - Used to install new clocksources
458 * @t: clocksource to be registered
460 * Returns -EBUSY if registration fails, zero otherwise.
462 int clocksource_register(struct clocksource
*cs
)
464 mutex_lock(&clocksource_mutex
);
465 clocksource_enqueue(cs
);
466 clocksource_select();
467 clocksource_enqueue_watchdog(cs
);
468 mutex_unlock(&clocksource_mutex
);
471 EXPORT_SYMBOL(clocksource_register
);
474 * clocksource_change_rating - Change the rating of a registered clocksource
476 void clocksource_change_rating(struct clocksource
*cs
, int rating
)
478 mutex_lock(&clocksource_mutex
);
481 clocksource_enqueue(cs
);
482 clocksource_select();
483 mutex_unlock(&clocksource_mutex
);
485 EXPORT_SYMBOL(clocksource_change_rating
);
488 * clocksource_unregister - remove a registered clocksource
490 void clocksource_unregister(struct clocksource
*cs
)
492 mutex_lock(&clocksource_mutex
);
493 clocksource_dequeue_watchdog(cs
);
495 clocksource_select();
496 mutex_unlock(&clocksource_mutex
);
498 EXPORT_SYMBOL(clocksource_unregister
);
502 * sysfs_show_current_clocksources - sysfs interface for current clocksource
504 * @buf: char buffer to be filled with clocksource list
506 * Provides sysfs interface for listing current clocksource.
509 sysfs_show_current_clocksources(struct sys_device
*dev
,
510 struct sysdev_attribute
*attr
, char *buf
)
514 mutex_lock(&clocksource_mutex
);
515 count
= snprintf(buf
, PAGE_SIZE
, "%s\n", curr_clocksource
->name
);
516 mutex_unlock(&clocksource_mutex
);
522 * sysfs_override_clocksource - interface for manually overriding clocksource
524 * @buf: name of override clocksource
525 * @count: length of buffer
527 * Takes input from sysfs interface for manually overriding the default
528 * clocksource selction.
530 static ssize_t
sysfs_override_clocksource(struct sys_device
*dev
,
531 struct sysdev_attribute
*attr
,
532 const char *buf
, size_t count
)
536 /* strings from sysfs write are not 0 terminated! */
537 if (count
>= sizeof(override_name
))
541 if (buf
[count
-1] == '\n')
544 mutex_lock(&clocksource_mutex
);
547 memcpy(override_name
, buf
, count
);
548 override_name
[count
] = 0;
549 clocksource_select();
551 mutex_unlock(&clocksource_mutex
);
557 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
559 * @buf: char buffer to be filled with clocksource list
561 * Provides sysfs interface for listing registered clocksources
564 sysfs_show_available_clocksources(struct sys_device
*dev
,
565 struct sysdev_attribute
*attr
,
568 struct clocksource
*src
;
571 mutex_lock(&clocksource_mutex
);
572 list_for_each_entry(src
, &clocksource_list
, list
) {
574 * Don't show non-HRES clocksource if the tick code is
575 * in one shot mode (highres=on or nohz=on)
577 if (!tick_oneshot_mode_active() ||
578 (src
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
))
579 count
+= snprintf(buf
+ count
,
580 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0),
583 mutex_unlock(&clocksource_mutex
);
585 count
+= snprintf(buf
+ count
,
586 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0), "\n");
594 static SYSDEV_ATTR(current_clocksource
, 0644, sysfs_show_current_clocksources
,
595 sysfs_override_clocksource
);
597 static SYSDEV_ATTR(available_clocksource
, 0444,
598 sysfs_show_available_clocksources
, NULL
);
600 static struct sysdev_class clocksource_sysclass
= {
601 .name
= "clocksource",
604 static struct sys_device device_clocksource
= {
606 .cls
= &clocksource_sysclass
,
609 static int __init
init_clocksource_sysfs(void)
611 int error
= sysdev_class_register(&clocksource_sysclass
);
614 error
= sysdev_register(&device_clocksource
);
616 error
= sysdev_create_file(
618 &attr_current_clocksource
);
620 error
= sysdev_create_file(
622 &attr_available_clocksource
);
626 device_initcall(init_clocksource_sysfs
);
627 #endif /* CONFIG_SYSFS */
630 * boot_override_clocksource - boot clock override
631 * @str: override name
633 * Takes a clocksource= boot argument and uses it
634 * as the clocksource override name.
636 static int __init
boot_override_clocksource(char* str
)
638 mutex_lock(&clocksource_mutex
);
640 strlcpy(override_name
, str
, sizeof(override_name
));
641 mutex_unlock(&clocksource_mutex
);
645 __setup("clocksource=", boot_override_clocksource
);
648 * boot_override_clock - Compatibility layer for deprecated boot option
649 * @str: override name
651 * DEPRECATED! Takes a clock= boot argument and uses it
652 * as the clocksource override name
654 static int __init
boot_override_clock(char* str
)
656 if (!strcmp(str
, "pmtmr")) {
657 printk("Warning: clock=pmtmr is deprecated. "
658 "Use clocksource=acpi_pm.\n");
659 return boot_override_clocksource("acpi_pm");
661 printk("Warning! clock= boot option is deprecated. "
662 "Use clocksource=xyz\n");
663 return boot_override_clocksource(str
);
666 __setup("clock=", boot_override_clock
);