powerpc: Don't try to fix up misaligned load-with-reservation instructions
[linux/fpc-iii.git] / kernel / time / clocksource.c
blob93621ae718d391ac6a95cd561dfc22bd6dc15a18
1 /*
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.
22 * TODO WishList:
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"
39 /**
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
60 * factors.
62 void
63 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
65 u64 tmp;
66 u32 sft, sftacc= 32;
69 * Calculate the shift factor which is limiting the conversion
70 * range:
72 tmp = ((u64)maxsec * from) >> 32;
73 while (tmp) {
74 tmp >>=1;
75 sftacc--;
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;
84 tmp += from / 2;
85 do_div(tmp, from);
86 if ((tmp >> sftacc) == 0)
87 break;
89 *mult = tmp;
90 *shift = sft;
92 EXPORT_SYMBOL_GPL(clocks_calc_mult_shift);
94 /*[Clocksource internal variables]---------
95 * curr_clocksource:
96 * currently selected clocksource.
97 * clocksource_list:
98 * linked list with the registered clocksources
99 * clocksource_mutex:
100 * protects manipulations to curr_clocksource and the clocksource_list
101 * override_name:
102 * Name of the user-specified clocksource.
104 static struct clocksource *curr_clocksource;
105 static LIST_HEAD(clocksource_list);
106 static DEFINE_MUTEX(clocksource_mutex);
107 static char override_name[CS_NAME_LEN];
108 static int finished_booting;
110 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
111 static void clocksource_watchdog_work(struct work_struct *work);
112 static void clocksource_select(void);
114 static LIST_HEAD(watchdog_list);
115 static struct clocksource *watchdog;
116 static struct timer_list watchdog_timer;
117 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
118 static DEFINE_SPINLOCK(watchdog_lock);
119 static int watchdog_running;
120 static atomic_t watchdog_reset_pending;
122 static int clocksource_watchdog_kthread(void *data);
123 static void __clocksource_change_rating(struct clocksource *cs, int rating);
126 * Interval: 0.5sec Threshold: 0.0625s
128 #define WATCHDOG_INTERVAL (HZ >> 1)
129 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
131 static void clocksource_watchdog_work(struct work_struct *work)
134 * If kthread_run fails the next watchdog scan over the
135 * watchdog_list will find the unstable clock again.
137 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
140 static void __clocksource_unstable(struct clocksource *cs)
142 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
143 cs->flags |= CLOCK_SOURCE_UNSTABLE;
145 if (cs->mark_unstable)
146 cs->mark_unstable(cs);
148 if (finished_booting)
149 schedule_work(&watchdog_work);
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)
163 unsigned long flags;
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 u64 csnow, wdnow, cslast, wdlast, delta;
178 int64_t wd_nsec, cs_nsec;
179 int next_cpu, reset_pending;
181 spin_lock(&watchdog_lock);
182 if (!watchdog_running)
183 goto out;
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);
193 continue;
196 local_irq_disable();
197 csnow = cs->read(cs);
198 wdnow = watchdog->read(watchdog);
199 local_irq_enable();
201 /* Clocksource initialized ? */
202 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
203 atomic_read(&watchdog_reset_pending)) {
204 cs->flags |= CLOCK_SOURCE_WATCHDOG;
205 cs->wd_last = wdnow;
206 cs->cs_last = csnow;
207 continue;
210 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
211 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
212 watchdog->shift);
214 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
215 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
216 wdlast = cs->wd_last; /* save these in case we print them */
217 cslast = cs->cs_last;
218 cs->cs_last = csnow;
219 cs->wd_last = wdnow;
221 if (atomic_read(&watchdog_reset_pending))
222 continue;
224 /* Check the deviation from the watchdog clocksource. */
225 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
226 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
227 smp_processor_id(), cs->name);
228 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
229 watchdog->name, wdnow, wdlast, watchdog->mask);
230 pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
231 cs->name, csnow, cslast, cs->mask);
232 __clocksource_unstable(cs);
233 continue;
236 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
237 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
238 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
239 /* Mark it valid for high-res. */
240 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
243 * clocksource_done_booting() will sort it if
244 * finished_booting is not set yet.
246 if (!finished_booting)
247 continue;
250 * If this is not the current clocksource let
251 * the watchdog thread reselect it. Due to the
252 * change to high res this clocksource might
253 * be preferred now. If it is the current
254 * clocksource let the tick code know about
255 * that change.
257 if (cs != curr_clocksource) {
258 cs->flags |= CLOCK_SOURCE_RESELECT;
259 schedule_work(&watchdog_work);
260 } else {
261 tick_clock_notify();
267 * We only clear the watchdog_reset_pending, when we did a
268 * full cycle through all clocksources.
270 if (reset_pending)
271 atomic_dec(&watchdog_reset_pending);
274 * Cycle through CPUs to check if the CPUs stay synchronized
275 * to each other.
277 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
278 if (next_cpu >= nr_cpu_ids)
279 next_cpu = cpumask_first(cpu_online_mask);
280 watchdog_timer.expires += WATCHDOG_INTERVAL;
281 add_timer_on(&watchdog_timer, next_cpu);
282 out:
283 spin_unlock(&watchdog_lock);
286 static inline void clocksource_start_watchdog(void)
288 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
289 return;
290 init_timer(&watchdog_timer);
291 watchdog_timer.function = clocksource_watchdog;
292 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
293 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
294 watchdog_running = 1;
297 static inline void clocksource_stop_watchdog(void)
299 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
300 return;
301 del_timer(&watchdog_timer);
302 watchdog_running = 0;
305 static inline void clocksource_reset_watchdog(void)
307 struct clocksource *cs;
309 list_for_each_entry(cs, &watchdog_list, wd_list)
310 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
313 static void clocksource_resume_watchdog(void)
315 atomic_inc(&watchdog_reset_pending);
318 static void clocksource_enqueue_watchdog(struct clocksource *cs)
320 unsigned long flags;
322 spin_lock_irqsave(&watchdog_lock, flags);
323 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
324 /* cs is a clocksource to be watched. */
325 list_add(&cs->wd_list, &watchdog_list);
326 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
327 } else {
328 /* cs is a watchdog. */
329 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
330 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
332 spin_unlock_irqrestore(&watchdog_lock, flags);
335 static void clocksource_select_watchdog(bool fallback)
337 struct clocksource *cs, *old_wd;
338 unsigned long flags;
340 spin_lock_irqsave(&watchdog_lock, flags);
341 /* save current watchdog */
342 old_wd = watchdog;
343 if (fallback)
344 watchdog = NULL;
346 list_for_each_entry(cs, &clocksource_list, list) {
347 /* cs is a clocksource to be watched. */
348 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY)
349 continue;
351 /* Skip current if we were requested for a fallback. */
352 if (fallback && cs == old_wd)
353 continue;
355 /* Pick the best watchdog. */
356 if (!watchdog || cs->rating > watchdog->rating)
357 watchdog = cs;
359 /* If we failed to find a fallback restore the old one. */
360 if (!watchdog)
361 watchdog = old_wd;
363 /* If we changed the watchdog we need to reset cycles. */
364 if (watchdog != old_wd)
365 clocksource_reset_watchdog();
367 /* Check if the watchdog timer needs to be started. */
368 clocksource_start_watchdog();
369 spin_unlock_irqrestore(&watchdog_lock, flags);
372 static void clocksource_dequeue_watchdog(struct clocksource *cs)
374 unsigned long flags;
376 spin_lock_irqsave(&watchdog_lock, flags);
377 if (cs != watchdog) {
378 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
379 /* cs is a watched clocksource. */
380 list_del_init(&cs->wd_list);
381 /* Check if the watchdog timer needs to be stopped. */
382 clocksource_stop_watchdog();
385 spin_unlock_irqrestore(&watchdog_lock, flags);
388 static int __clocksource_watchdog_kthread(void)
390 struct clocksource *cs, *tmp;
391 unsigned long flags;
392 LIST_HEAD(unstable);
393 int select = 0;
395 spin_lock_irqsave(&watchdog_lock, flags);
396 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
397 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
398 list_del_init(&cs->wd_list);
399 list_add(&cs->wd_list, &unstable);
400 select = 1;
402 if (cs->flags & CLOCK_SOURCE_RESELECT) {
403 cs->flags &= ~CLOCK_SOURCE_RESELECT;
404 select = 1;
407 /* Check if the watchdog timer needs to be stopped. */
408 clocksource_stop_watchdog();
409 spin_unlock_irqrestore(&watchdog_lock, flags);
411 /* Needs to be done outside of watchdog lock */
412 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
413 list_del_init(&cs->wd_list);
414 __clocksource_change_rating(cs, 0);
416 return select;
419 static int clocksource_watchdog_kthread(void *data)
421 mutex_lock(&clocksource_mutex);
422 if (__clocksource_watchdog_kthread())
423 clocksource_select();
424 mutex_unlock(&clocksource_mutex);
425 return 0;
428 static bool clocksource_is_watchdog(struct clocksource *cs)
430 return cs == watchdog;
433 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
435 static void clocksource_enqueue_watchdog(struct clocksource *cs)
437 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
438 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
441 static void clocksource_select_watchdog(bool fallback) { }
442 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
443 static inline void clocksource_resume_watchdog(void) { }
444 static inline int __clocksource_watchdog_kthread(void) { return 0; }
445 static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
446 void clocksource_mark_unstable(struct clocksource *cs) { }
448 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
451 * clocksource_suspend - suspend the clocksource(s)
453 void clocksource_suspend(void)
455 struct clocksource *cs;
457 list_for_each_entry_reverse(cs, &clocksource_list, list)
458 if (cs->suspend)
459 cs->suspend(cs);
463 * clocksource_resume - resume the clocksource(s)
465 void clocksource_resume(void)
467 struct clocksource *cs;
469 list_for_each_entry(cs, &clocksource_list, list)
470 if (cs->resume)
471 cs->resume(cs);
473 clocksource_resume_watchdog();
477 * clocksource_touch_watchdog - Update watchdog
479 * Update the watchdog after exception contexts such as kgdb so as not
480 * to incorrectly trip the watchdog. This might fail when the kernel
481 * was stopped in code which holds watchdog_lock.
483 void clocksource_touch_watchdog(void)
485 clocksource_resume_watchdog();
489 * clocksource_max_adjustment- Returns max adjustment amount
490 * @cs: Pointer to clocksource
493 static u32 clocksource_max_adjustment(struct clocksource *cs)
495 u64 ret;
497 * We won't try to correct for more than 11% adjustments (110,000 ppm),
499 ret = (u64)cs->mult * 11;
500 do_div(ret,100);
501 return (u32)ret;
505 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
506 * @mult: cycle to nanosecond multiplier
507 * @shift: cycle to nanosecond divisor (power of two)
508 * @maxadj: maximum adjustment value to mult (~11%)
509 * @mask: bitmask for two's complement subtraction of non 64 bit counters
510 * @max_cyc: maximum cycle value before potential overflow (does not include
511 * any safety margin)
513 * NOTE: This function includes a safety margin of 50%, in other words, we
514 * return half the number of nanoseconds the hardware counter can technically
515 * cover. This is done so that we can potentially detect problems caused by
516 * delayed timers or bad hardware, which might result in time intervals that
517 * are larger than what the math used can handle without overflows.
519 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
521 u64 max_nsecs, max_cycles;
524 * Calculate the maximum number of cycles that we can pass to the
525 * cyc2ns() function without overflowing a 64-bit result.
527 max_cycles = ULLONG_MAX;
528 do_div(max_cycles, mult+maxadj);
531 * The actual maximum number of cycles we can defer the clocksource is
532 * determined by the minimum of max_cycles and mask.
533 * Note: Here we subtract the maxadj to make sure we don't sleep for
534 * too long if there's a large negative adjustment.
536 max_cycles = min(max_cycles, mask);
537 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
539 /* return the max_cycles value as well if requested */
540 if (max_cyc)
541 *max_cyc = max_cycles;
543 /* Return 50% of the actual maximum, so we can detect bad values */
544 max_nsecs >>= 1;
546 return max_nsecs;
550 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
551 * @cs: Pointer to clocksource to be updated
554 static inline void clocksource_update_max_deferment(struct clocksource *cs)
556 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
557 cs->maxadj, cs->mask,
558 &cs->max_cycles);
561 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
563 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
565 struct clocksource *cs;
567 if (!finished_booting || list_empty(&clocksource_list))
568 return NULL;
571 * We pick the clocksource with the highest rating. If oneshot
572 * mode is active, we pick the highres valid clocksource with
573 * the best rating.
575 list_for_each_entry(cs, &clocksource_list, list) {
576 if (skipcur && cs == curr_clocksource)
577 continue;
578 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
579 continue;
580 return cs;
582 return NULL;
585 static void __clocksource_select(bool skipcur)
587 bool oneshot = tick_oneshot_mode_active();
588 struct clocksource *best, *cs;
590 /* Find the best suitable clocksource */
591 best = clocksource_find_best(oneshot, skipcur);
592 if (!best)
593 return;
595 /* Check for the override clocksource. */
596 list_for_each_entry(cs, &clocksource_list, list) {
597 if (skipcur && cs == curr_clocksource)
598 continue;
599 if (strcmp(cs->name, override_name) != 0)
600 continue;
602 * Check to make sure we don't switch to a non-highres
603 * capable clocksource if the tick code is in oneshot
604 * mode (highres or nohz)
606 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
607 /* Override clocksource cannot be used. */
608 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
609 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
610 cs->name);
611 override_name[0] = 0;
612 } else {
614 * The override cannot be currently verified.
615 * Deferring to let the watchdog check.
617 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
618 cs->name);
620 } else
621 /* Override clocksource can be used. */
622 best = cs;
623 break;
626 if (curr_clocksource != best && !timekeeping_notify(best)) {
627 pr_info("Switched to clocksource %s\n", best->name);
628 curr_clocksource = best;
633 * clocksource_select - Select the best clocksource available
635 * Private function. Must hold clocksource_mutex when called.
637 * Select the clocksource with the best rating, or the clocksource,
638 * which is selected by userspace override.
640 static void clocksource_select(void)
642 __clocksource_select(false);
645 static void clocksource_select_fallback(void)
647 __clocksource_select(true);
650 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
651 static inline void clocksource_select(void) { }
652 static inline void clocksource_select_fallback(void) { }
654 #endif
657 * clocksource_done_booting - Called near the end of core bootup
659 * Hack to avoid lots of clocksource churn at boot time.
660 * We use fs_initcall because we want this to start before
661 * device_initcall but after subsys_initcall.
663 static int __init clocksource_done_booting(void)
665 mutex_lock(&clocksource_mutex);
666 curr_clocksource = clocksource_default_clock();
667 finished_booting = 1;
669 * Run the watchdog first to eliminate unstable clock sources
671 __clocksource_watchdog_kthread();
672 clocksource_select();
673 mutex_unlock(&clocksource_mutex);
674 return 0;
676 fs_initcall(clocksource_done_booting);
679 * Enqueue the clocksource sorted by rating
681 static void clocksource_enqueue(struct clocksource *cs)
683 struct list_head *entry = &clocksource_list;
684 struct clocksource *tmp;
686 list_for_each_entry(tmp, &clocksource_list, list) {
687 /* Keep track of the place, where to insert */
688 if (tmp->rating < cs->rating)
689 break;
690 entry = &tmp->list;
692 list_add(&cs->list, entry);
696 * __clocksource_update_freq_scale - Used update clocksource with new freq
697 * @cs: clocksource to be registered
698 * @scale: Scale factor multiplied against freq to get clocksource hz
699 * @freq: clocksource frequency (cycles per second) divided by scale
701 * This should only be called from the clocksource->enable() method.
703 * This *SHOULD NOT* be called directly! Please use the
704 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
705 * functions.
707 void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
709 u64 sec;
712 * Default clocksources are *special* and self-define their mult/shift.
713 * But, you're not special, so you should specify a freq value.
715 if (freq) {
717 * Calc the maximum number of seconds which we can run before
718 * wrapping around. For clocksources which have a mask > 32-bit
719 * we need to limit the max sleep time to have a good
720 * conversion precision. 10 minutes is still a reasonable
721 * amount. That results in a shift value of 24 for a
722 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
723 * ~ 0.06ppm granularity for NTP.
725 sec = cs->mask;
726 do_div(sec, freq);
727 do_div(sec, scale);
728 if (!sec)
729 sec = 1;
730 else if (sec > 600 && cs->mask > UINT_MAX)
731 sec = 600;
733 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
734 NSEC_PER_SEC / scale, sec * scale);
737 * Ensure clocksources that have large 'mult' values don't overflow
738 * when adjusted.
740 cs->maxadj = clocksource_max_adjustment(cs);
741 while (freq && ((cs->mult + cs->maxadj < cs->mult)
742 || (cs->mult - cs->maxadj > cs->mult))) {
743 cs->mult >>= 1;
744 cs->shift--;
745 cs->maxadj = clocksource_max_adjustment(cs);
749 * Only warn for *special* clocksources that self-define
750 * their mult/shift values and don't specify a freq.
752 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
753 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
754 cs->name);
756 clocksource_update_max_deferment(cs);
758 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
759 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
761 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
764 * __clocksource_register_scale - Used to install new clocksources
765 * @cs: clocksource to be registered
766 * @scale: Scale factor multiplied against freq to get clocksource hz
767 * @freq: clocksource frequency (cycles per second) divided by scale
769 * Returns -EBUSY if registration fails, zero otherwise.
771 * This *SHOULD NOT* be called directly! Please use the
772 * clocksource_register_hz() or clocksource_register_khz helper functions.
774 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
777 /* Initialize mult/shift and max_idle_ns */
778 __clocksource_update_freq_scale(cs, scale, freq);
780 /* Add clocksource to the clocksource list */
781 mutex_lock(&clocksource_mutex);
782 clocksource_enqueue(cs);
783 clocksource_enqueue_watchdog(cs);
784 clocksource_select();
785 clocksource_select_watchdog(false);
786 mutex_unlock(&clocksource_mutex);
787 return 0;
789 EXPORT_SYMBOL_GPL(__clocksource_register_scale);
791 static void __clocksource_change_rating(struct clocksource *cs, int rating)
793 list_del(&cs->list);
794 cs->rating = rating;
795 clocksource_enqueue(cs);
799 * clocksource_change_rating - Change the rating of a registered clocksource
800 * @cs: clocksource to be changed
801 * @rating: new rating
803 void clocksource_change_rating(struct clocksource *cs, int rating)
805 mutex_lock(&clocksource_mutex);
806 __clocksource_change_rating(cs, rating);
807 clocksource_select();
808 clocksource_select_watchdog(false);
809 mutex_unlock(&clocksource_mutex);
811 EXPORT_SYMBOL(clocksource_change_rating);
814 * Unbind clocksource @cs. Called with clocksource_mutex held
816 static int clocksource_unbind(struct clocksource *cs)
818 if (clocksource_is_watchdog(cs)) {
819 /* Select and try to install a replacement watchdog. */
820 clocksource_select_watchdog(true);
821 if (clocksource_is_watchdog(cs))
822 return -EBUSY;
825 if (cs == curr_clocksource) {
826 /* Select and try to install a replacement clock source */
827 clocksource_select_fallback();
828 if (curr_clocksource == cs)
829 return -EBUSY;
831 clocksource_dequeue_watchdog(cs);
832 list_del_init(&cs->list);
833 return 0;
837 * clocksource_unregister - remove a registered clocksource
838 * @cs: clocksource to be unregistered
840 int clocksource_unregister(struct clocksource *cs)
842 int ret = 0;
844 mutex_lock(&clocksource_mutex);
845 if (!list_empty(&cs->list))
846 ret = clocksource_unbind(cs);
847 mutex_unlock(&clocksource_mutex);
848 return ret;
850 EXPORT_SYMBOL(clocksource_unregister);
852 #ifdef CONFIG_SYSFS
854 * sysfs_show_current_clocksources - sysfs interface for current clocksource
855 * @dev: unused
856 * @attr: unused
857 * @buf: char buffer to be filled with clocksource list
859 * Provides sysfs interface for listing current clocksource.
861 static ssize_t
862 sysfs_show_current_clocksources(struct device *dev,
863 struct device_attribute *attr, char *buf)
865 ssize_t count = 0;
867 mutex_lock(&clocksource_mutex);
868 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
869 mutex_unlock(&clocksource_mutex);
871 return count;
874 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
876 size_t ret = cnt;
878 /* strings from sysfs write are not 0 terminated! */
879 if (!cnt || cnt >= CS_NAME_LEN)
880 return -EINVAL;
882 /* strip of \n: */
883 if (buf[cnt-1] == '\n')
884 cnt--;
885 if (cnt > 0)
886 memcpy(dst, buf, cnt);
887 dst[cnt] = 0;
888 return ret;
892 * sysfs_override_clocksource - interface for manually overriding clocksource
893 * @dev: unused
894 * @attr: unused
895 * @buf: name of override clocksource
896 * @count: length of buffer
898 * Takes input from sysfs interface for manually overriding the default
899 * clocksource selection.
901 static ssize_t sysfs_override_clocksource(struct device *dev,
902 struct device_attribute *attr,
903 const char *buf, size_t count)
905 ssize_t ret;
907 mutex_lock(&clocksource_mutex);
909 ret = sysfs_get_uname(buf, override_name, count);
910 if (ret >= 0)
911 clocksource_select();
913 mutex_unlock(&clocksource_mutex);
915 return ret;
919 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
920 * @dev: unused
921 * @attr: unused
922 * @buf: unused
923 * @count: length of buffer
925 * Takes input from sysfs interface for manually unbinding a clocksource.
927 static ssize_t sysfs_unbind_clocksource(struct device *dev,
928 struct device_attribute *attr,
929 const char *buf, size_t count)
931 struct clocksource *cs;
932 char name[CS_NAME_LEN];
933 ssize_t ret;
935 ret = sysfs_get_uname(buf, name, count);
936 if (ret < 0)
937 return ret;
939 ret = -ENODEV;
940 mutex_lock(&clocksource_mutex);
941 list_for_each_entry(cs, &clocksource_list, list) {
942 if (strcmp(cs->name, name))
943 continue;
944 ret = clocksource_unbind(cs);
945 break;
947 mutex_unlock(&clocksource_mutex);
949 return ret ? ret : count;
953 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
954 * @dev: unused
955 * @attr: unused
956 * @buf: char buffer to be filled with clocksource list
958 * Provides sysfs interface for listing registered clocksources
960 static ssize_t
961 sysfs_show_available_clocksources(struct device *dev,
962 struct device_attribute *attr,
963 char *buf)
965 struct clocksource *src;
966 ssize_t count = 0;
968 mutex_lock(&clocksource_mutex);
969 list_for_each_entry(src, &clocksource_list, list) {
971 * Don't show non-HRES clocksource if the tick code is
972 * in one shot mode (highres=on or nohz=on)
974 if (!tick_oneshot_mode_active() ||
975 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
976 count += snprintf(buf + count,
977 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
978 "%s ", src->name);
980 mutex_unlock(&clocksource_mutex);
982 count += snprintf(buf + count,
983 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
985 return count;
989 * Sysfs setup bits:
991 static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
992 sysfs_override_clocksource);
994 static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
996 static DEVICE_ATTR(available_clocksource, 0444,
997 sysfs_show_available_clocksources, NULL);
999 static struct bus_type clocksource_subsys = {
1000 .name = "clocksource",
1001 .dev_name = "clocksource",
1004 static struct device device_clocksource = {
1005 .id = 0,
1006 .bus = &clocksource_subsys,
1009 static int __init init_clocksource_sysfs(void)
1011 int error = subsys_system_register(&clocksource_subsys, NULL);
1013 if (!error)
1014 error = device_register(&device_clocksource);
1015 if (!error)
1016 error = device_create_file(
1017 &device_clocksource,
1018 &dev_attr_current_clocksource);
1019 if (!error)
1020 error = device_create_file(&device_clocksource,
1021 &dev_attr_unbind_clocksource);
1022 if (!error)
1023 error = device_create_file(
1024 &device_clocksource,
1025 &dev_attr_available_clocksource);
1026 return error;
1029 device_initcall(init_clocksource_sysfs);
1030 #endif /* CONFIG_SYSFS */
1033 * boot_override_clocksource - boot clock override
1034 * @str: override name
1036 * Takes a clocksource= boot argument and uses it
1037 * as the clocksource override name.
1039 static int __init boot_override_clocksource(char* str)
1041 mutex_lock(&clocksource_mutex);
1042 if (str)
1043 strlcpy(override_name, str, sizeof(override_name));
1044 mutex_unlock(&clocksource_mutex);
1045 return 1;
1048 __setup("clocksource=", boot_override_clocksource);
1051 * boot_override_clock - Compatibility layer for deprecated boot option
1052 * @str: override name
1054 * DEPRECATED! Takes a clock= boot argument and uses it
1055 * as the clocksource override name
1057 static int __init boot_override_clock(char* str)
1059 if (!strcmp(str, "pmtmr")) {
1060 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
1061 return boot_override_clocksource("acpi_pm");
1063 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
1064 return boot_override_clocksource(str);
1067 __setup("clock=", boot_override_clock);