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 * pending next selected clocksource.
115 * linked list with the registered clocksources
117 * protects manipulations to curr_clocksource and next_clocksource
118 * and the clocksource_list
120 * Name of the user-specified clocksource.
122 static struct clocksource
*curr_clocksource
;
123 static struct clocksource
*next_clocksource
;
124 static LIST_HEAD(clocksource_list
);
125 static DEFINE_SPINLOCK(clocksource_lock
);
126 static char override_name
[32];
127 static int finished_booting
;
129 /* clocksource_done_booting - Called near the end of core bootup
131 * Hack to avoid lots of clocksource churn at boot time.
132 * We use fs_initcall because we want this to start before
133 * device_initcall but after subsys_initcall.
135 static int __init
clocksource_done_booting(void)
137 finished_booting
= 1;
140 fs_initcall(clocksource_done_booting
);
142 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
143 static LIST_HEAD(watchdog_list
);
144 static struct clocksource
*watchdog
;
145 static struct timer_list watchdog_timer
;
146 static struct work_struct watchdog_work
;
147 static DEFINE_SPINLOCK(watchdog_lock
);
148 static cycle_t watchdog_last
;
149 static int watchdog_running
;
151 static void clocksource_watchdog_work(struct work_struct
*work
);
154 * Interval: 0.5sec Threshold: 0.0625s
156 #define WATCHDOG_INTERVAL (HZ >> 1)
157 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
159 static void clocksource_unstable(struct clocksource
*cs
, int64_t delta
)
161 printk(KERN_WARNING
"Clocksource %s unstable (delta = %Ld ns)\n",
163 cs
->flags
&= ~(CLOCK_SOURCE_VALID_FOR_HRES
| CLOCK_SOURCE_WATCHDOG
);
164 cs
->flags
|= CLOCK_SOURCE_UNSTABLE
;
165 schedule_work(&watchdog_work
);
168 static void clocksource_watchdog(unsigned long data
)
170 struct clocksource
*cs
;
171 cycle_t csnow
, wdnow
;
172 int64_t wd_nsec
, cs_nsec
;
175 spin_lock(&watchdog_lock
);
176 if (!watchdog_running
)
179 wdnow
= watchdog
->read(watchdog
);
180 wd_nsec
= clocksource_cyc2ns((wdnow
- watchdog_last
) & watchdog
->mask
,
181 watchdog
->mult
, watchdog
->shift
);
182 watchdog_last
= wdnow
;
184 list_for_each_entry(cs
, &watchdog_list
, wd_list
) {
186 /* Clocksource already marked unstable? */
187 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
)
190 csnow
= cs
->read(cs
);
192 /* Clocksource initialized ? */
193 if (!(cs
->flags
& CLOCK_SOURCE_WATCHDOG
)) {
194 cs
->flags
|= CLOCK_SOURCE_WATCHDOG
;
199 /* Check the deviation from the watchdog clocksource. */
200 cs_nsec
= clocksource_cyc2ns((csnow
- cs
->wd_last
) &
201 cs
->mask
, cs
->mult
, cs
->shift
);
203 if (abs(cs_nsec
- wd_nsec
) > WATCHDOG_THRESHOLD
) {
204 clocksource_unstable(cs
, cs_nsec
- wd_nsec
);
208 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) &&
209 (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
) &&
210 (watchdog
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)) {
211 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
213 * We just marked the clocksource as highres-capable,
214 * notify the rest of the system as well so that we
215 * transition into high-res mode:
222 * Cycle through CPUs to check if the CPUs stay synchronized
225 next_cpu
= cpumask_next(raw_smp_processor_id(), cpu_online_mask
);
226 if (next_cpu
>= nr_cpu_ids
)
227 next_cpu
= cpumask_first(cpu_online_mask
);
228 watchdog_timer
.expires
+= WATCHDOG_INTERVAL
;
229 add_timer_on(&watchdog_timer
, next_cpu
);
231 spin_unlock(&watchdog_lock
);
234 static inline void clocksource_start_watchdog(void)
236 if (watchdog_running
|| !watchdog
|| list_empty(&watchdog_list
))
238 INIT_WORK(&watchdog_work
, clocksource_watchdog_work
);
239 init_timer(&watchdog_timer
);
240 watchdog_timer
.function
= clocksource_watchdog
;
241 watchdog_last
= watchdog
->read(watchdog
);
242 watchdog_timer
.expires
= jiffies
+ WATCHDOG_INTERVAL
;
243 add_timer_on(&watchdog_timer
, cpumask_first(cpu_online_mask
));
244 watchdog_running
= 1;
247 static inline void clocksource_stop_watchdog(void)
249 if (!watchdog_running
|| (watchdog
&& !list_empty(&watchdog_list
)))
251 del_timer(&watchdog_timer
);
252 watchdog_running
= 0;
255 static inline void clocksource_reset_watchdog(void)
257 struct clocksource
*cs
;
259 list_for_each_entry(cs
, &watchdog_list
, wd_list
)
260 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
263 static void clocksource_resume_watchdog(void)
267 spin_lock_irqsave(&watchdog_lock
, flags
);
268 clocksource_reset_watchdog();
269 spin_unlock_irqrestore(&watchdog_lock
, flags
);
272 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
276 spin_lock_irqsave(&watchdog_lock
, flags
);
277 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
278 /* cs is a clocksource to be watched. */
279 list_add(&cs
->wd_list
, &watchdog_list
);
280 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
282 /* cs is a watchdog. */
283 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
284 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
285 /* Pick the best watchdog. */
286 if (!watchdog
|| cs
->rating
> watchdog
->rating
) {
288 /* Reset watchdog cycles */
289 clocksource_reset_watchdog();
292 /* Check if the watchdog timer needs to be started. */
293 clocksource_start_watchdog();
294 spin_unlock_irqrestore(&watchdog_lock
, flags
);
297 static void clocksource_dequeue_watchdog(struct clocksource
*cs
)
299 struct clocksource
*tmp
;
302 spin_lock_irqsave(&watchdog_lock
, flags
);
303 if (cs
->flags
& CLOCK_SOURCE_MUST_VERIFY
) {
304 /* cs is a watched clocksource. */
305 list_del_init(&cs
->wd_list
);
306 } else if (cs
== watchdog
) {
307 /* Reset watchdog cycles */
308 clocksource_reset_watchdog();
309 /* Current watchdog is removed. Find an alternative. */
311 list_for_each_entry(tmp
, &clocksource_list
, list
) {
312 if (tmp
== cs
|| tmp
->flags
& CLOCK_SOURCE_MUST_VERIFY
)
314 if (!watchdog
|| tmp
->rating
> watchdog
->rating
)
318 cs
->flags
&= ~CLOCK_SOURCE_WATCHDOG
;
319 /* Check if the watchdog timer needs to be stopped. */
320 clocksource_stop_watchdog();
321 spin_unlock_irqrestore(&watchdog_lock
, flags
);
324 static void clocksource_watchdog_work(struct work_struct
*work
)
326 struct clocksource
*cs
, *tmp
;
329 spin_lock_irqsave(&watchdog_lock
, flags
);
330 list_for_each_entry_safe(cs
, tmp
, &watchdog_list
, wd_list
)
331 if (cs
->flags
& CLOCK_SOURCE_UNSTABLE
) {
332 list_del_init(&cs
->wd_list
);
333 clocksource_change_rating(cs
, 0);
335 /* Check if the watchdog timer needs to be stopped. */
336 clocksource_stop_watchdog();
337 spin_unlock(&watchdog_lock
);
340 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
342 static void clocksource_enqueue_watchdog(struct clocksource
*cs
)
344 if (cs
->flags
& CLOCK_SOURCE_IS_CONTINUOUS
)
345 cs
->flags
|= CLOCK_SOURCE_VALID_FOR_HRES
;
348 static inline void clocksource_dequeue_watchdog(struct clocksource
*cs
) { }
349 static inline void clocksource_resume_watchdog(void) { }
351 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
354 * clocksource_resume - resume the clocksource(s)
356 void clocksource_resume(void)
358 struct clocksource
*cs
;
361 spin_lock_irqsave(&clocksource_lock
, flags
);
363 list_for_each_entry(cs
, &clocksource_list
, list
) {
368 clocksource_resume_watchdog();
370 spin_unlock_irqrestore(&clocksource_lock
, flags
);
374 * clocksource_touch_watchdog - Update watchdog
376 * Update the watchdog after exception contexts such as kgdb so as not
377 * to incorrectly trip the watchdog.
380 void clocksource_touch_watchdog(void)
382 clocksource_resume_watchdog();
385 #ifdef CONFIG_GENERIC_TIME
387 * clocksource_get_next - Returns the selected clocksource
390 struct clocksource
*clocksource_get_next(void)
394 spin_lock_irqsave(&clocksource_lock
, flags
);
395 if (next_clocksource
&& finished_booting
) {
396 curr_clocksource
= next_clocksource
;
397 next_clocksource
= NULL
;
399 spin_unlock_irqrestore(&clocksource_lock
, flags
);
401 return curr_clocksource
;
405 * clocksource_select - Select the best clocksource available
407 * Private function. Must hold clocksource_lock when called.
409 * Select the clocksource with the best rating, or the clocksource,
410 * which is selected by userspace override.
412 static void clocksource_select(void)
414 struct clocksource
*best
, *cs
;
416 if (list_empty(&clocksource_list
))
418 /* First clocksource on the list has the best rating. */
419 best
= list_first_entry(&clocksource_list
, struct clocksource
, list
);
420 /* Check for the override clocksource. */
421 list_for_each_entry(cs
, &clocksource_list
, list
) {
422 if (strcmp(cs
->name
, override_name
) != 0)
425 * Check to make sure we don't switch to a non-highres
426 * capable clocksource if the tick code is in oneshot
427 * mode (highres or nohz)
429 if (!(cs
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
) &&
430 tick_oneshot_mode_active()) {
431 /* Override clocksource cannot be used. */
432 printk(KERN_WARNING
"Override clocksource %s is not "
433 "HRT compatible. Cannot switch while in "
434 "HRT/NOHZ mode\n", cs
->name
);
435 override_name
[0] = 0;
437 /* Override clocksource can be used. */
441 if (curr_clocksource
!= best
)
442 next_clocksource
= best
;
445 #else /* CONFIG_GENERIC_TIME */
447 static void clocksource_select(void) { }
452 * Enqueue the clocksource sorted by rating
454 static void clocksource_enqueue(struct clocksource
*cs
)
456 struct list_head
*entry
= &clocksource_list
;
457 struct clocksource
*tmp
;
459 list_for_each_entry(tmp
, &clocksource_list
, list
)
460 /* Keep track of the place, where to insert */
461 if (tmp
->rating
>= cs
->rating
)
463 list_add(&cs
->list
, entry
);
467 * clocksource_register - Used to install new clocksources
468 * @t: clocksource to be registered
470 * Returns -EBUSY if registration fails, zero otherwise.
472 int clocksource_register(struct clocksource
*cs
)
476 spin_lock_irqsave(&clocksource_lock
, flags
);
477 clocksource_enqueue(cs
);
478 clocksource_select();
479 spin_unlock_irqrestore(&clocksource_lock
, flags
);
480 clocksource_enqueue_watchdog(cs
);
483 EXPORT_SYMBOL(clocksource_register
);
486 * clocksource_change_rating - Change the rating of a registered clocksource
488 void clocksource_change_rating(struct clocksource
*cs
, int rating
)
492 spin_lock_irqsave(&clocksource_lock
, flags
);
495 clocksource_enqueue(cs
);
496 clocksource_select();
497 spin_unlock_irqrestore(&clocksource_lock
, flags
);
499 EXPORT_SYMBOL(clocksource_change_rating
);
502 * clocksource_unregister - remove a registered clocksource
504 void clocksource_unregister(struct clocksource
*cs
)
508 clocksource_dequeue_watchdog(cs
);
509 spin_lock_irqsave(&clocksource_lock
, flags
);
511 clocksource_select();
512 spin_unlock_irqrestore(&clocksource_lock
, flags
);
514 EXPORT_SYMBOL(clocksource_unregister
);
518 * sysfs_show_current_clocksources - sysfs interface for current clocksource
520 * @buf: char buffer to be filled with clocksource list
522 * Provides sysfs interface for listing current clocksource.
525 sysfs_show_current_clocksources(struct sys_device
*dev
,
526 struct sysdev_attribute
*attr
, char *buf
)
530 spin_lock_irq(&clocksource_lock
);
531 count
= snprintf(buf
, PAGE_SIZE
, "%s\n", curr_clocksource
->name
);
532 spin_unlock_irq(&clocksource_lock
);
538 * sysfs_override_clocksource - interface for manually overriding clocksource
540 * @buf: name of override clocksource
541 * @count: length of buffer
543 * Takes input from sysfs interface for manually overriding the default
544 * clocksource selction.
546 static ssize_t
sysfs_override_clocksource(struct sys_device
*dev
,
547 struct sysdev_attribute
*attr
,
548 const char *buf
, size_t count
)
552 /* strings from sysfs write are not 0 terminated! */
553 if (count
>= sizeof(override_name
))
557 if (buf
[count
-1] == '\n')
560 spin_lock_irq(&clocksource_lock
);
563 memcpy(override_name
, buf
, count
);
564 override_name
[count
] = 0;
565 clocksource_select();
567 spin_unlock_irq(&clocksource_lock
);
573 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
575 * @buf: char buffer to be filled with clocksource list
577 * Provides sysfs interface for listing registered clocksources
580 sysfs_show_available_clocksources(struct sys_device
*dev
,
581 struct sysdev_attribute
*attr
,
584 struct clocksource
*src
;
587 spin_lock_irq(&clocksource_lock
);
588 list_for_each_entry(src
, &clocksource_list
, list
) {
590 * Don't show non-HRES clocksource if the tick code is
591 * in one shot mode (highres=on or nohz=on)
593 if (!tick_oneshot_mode_active() ||
594 (src
->flags
& CLOCK_SOURCE_VALID_FOR_HRES
))
595 count
+= snprintf(buf
+ count
,
596 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0),
599 spin_unlock_irq(&clocksource_lock
);
601 count
+= snprintf(buf
+ count
,
602 max((ssize_t
)PAGE_SIZE
- count
, (ssize_t
)0), "\n");
610 static SYSDEV_ATTR(current_clocksource
, 0644, sysfs_show_current_clocksources
,
611 sysfs_override_clocksource
);
613 static SYSDEV_ATTR(available_clocksource
, 0444,
614 sysfs_show_available_clocksources
, NULL
);
616 static struct sysdev_class clocksource_sysclass
= {
617 .name
= "clocksource",
620 static struct sys_device device_clocksource
= {
622 .cls
= &clocksource_sysclass
,
625 static int __init
init_clocksource_sysfs(void)
627 int error
= sysdev_class_register(&clocksource_sysclass
);
630 error
= sysdev_register(&device_clocksource
);
632 error
= sysdev_create_file(
634 &attr_current_clocksource
);
636 error
= sysdev_create_file(
638 &attr_available_clocksource
);
642 device_initcall(init_clocksource_sysfs
);
643 #endif /* CONFIG_SYSFS */
646 * boot_override_clocksource - boot clock override
647 * @str: override name
649 * Takes a clocksource= boot argument and uses it
650 * as the clocksource override name.
652 static int __init
boot_override_clocksource(char* str
)
655 spin_lock_irqsave(&clocksource_lock
, flags
);
657 strlcpy(override_name
, str
, sizeof(override_name
));
658 spin_unlock_irqrestore(&clocksource_lock
, flags
);
662 __setup("clocksource=", boot_override_clocksource
);
665 * boot_override_clock - Compatibility layer for deprecated boot option
666 * @str: override name
668 * DEPRECATED! Takes a clock= boot argument and uses it
669 * as the clocksource override name
671 static int __init
boot_override_clock(char* str
)
673 if (!strcmp(str
, "pmtmr")) {
674 printk("Warning: clock=pmtmr is deprecated. "
675 "Use clocksource=acpi_pm.\n");
676 return boot_override_clocksource("acpi_pm");
678 printk("Warning! clock= boot option is deprecated. "
679 "Use clocksource=xyz\n");
680 return boot_override_clocksource(str
);
683 __setup("clock=", boot_override_clock
);