Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / kernel / time / clocksource.c
1 /*
2  * linux/kernel/time/clocksource.c
3  *
4  * This file contains the functions which manage clocksource drivers.
5  *
6  * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
7  *
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.
12  *
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.
17  *
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.
21  *
22  * TODO WishList:
23  *   o Allow clocksource drivers to be unregistered
24  */
25
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>
33
34 #include "tick-internal.h"
35 #include "timekeeping_internal.h"
36
37 /**
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
44  *
45  * The function evaluates the shift/mult pair for the scaled math
46  * operations of clocksources and clockevents.
47  *
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.
51  *
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
58  * factors.
59  */
60 void
61 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
62 {
63         u64 tmp;
64         u32 sft, sftacc= 32;
65
66         /*
67          * Calculate the shift factor which is limiting the conversion
68          * range:
69          */
70         tmp = ((u64)maxsec * from) >> 32;
71         while (tmp) {
72                 tmp >>=1;
73                 sftacc--;
74         }
75
76         /*
77          * Find the conversion shift/mult pair which has the best
78          * accuracy and fits the maxsec conversion range:
79          */
80         for (sft = 32; sft > 0; sft--) {
81                 tmp = (u64) to << sft;
82                 tmp += from / 2;
83                 do_div(tmp, from);
84                 if ((tmp >> sftacc) == 0)
85                         break;
86         }
87         *mult = tmp;
88         *shift = sft;
89 }
90
91 /*[Clocksource internal variables]---------
92  * curr_clocksource:
93  *      currently selected clocksource.
94  * clocksource_list:
95  *      linked list with the registered clocksources
96  * clocksource_mutex:
97  *      protects manipulations to curr_clocksource and the clocksource_list
98  * override_name:
99  *      Name of the user-specified clocksource.
100  */
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;
106
107 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
108 static void clocksource_watchdog_work(struct work_struct *work);
109 static void clocksource_select(void);
110
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;
118
119 static int clocksource_watchdog_kthread(void *data);
120 static void __clocksource_change_rating(struct clocksource *cs, int rating);
121
122 /*
123  * Interval: 0.5sec Threshold: 0.0625s
124  */
125 #define WATCHDOG_INTERVAL (HZ >> 1)
126 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
127
128 static void clocksource_watchdog_work(struct work_struct *work)
129 {
130         /*
131          * If kthread_run fails the next watchdog scan over the
132          * watchdog_list will find the unstable clock again.
133          */
134         kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
135 }
136
137 static void __clocksource_unstable(struct clocksource *cs)
138 {
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);
143 }
144
145 /**
146  * clocksource_mark_unstable - mark clocksource unstable via watchdog
147  * @cs:         clocksource to be marked unstable
148  *
149  * This function is called instead of clocksource_change_rating from
150  * cpu hotplug code to avoid a deadlock between the clocksource mutex
151  * and the cpu hotplug mutex. It defers the update of the clocksource
152  * to the watchdog thread.
153  */
154 void clocksource_mark_unstable(struct clocksource *cs)
155 {
156         unsigned long flags;
157
158         spin_lock_irqsave(&watchdog_lock, flags);
159         if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
160                 if (list_empty(&cs->wd_list))
161                         list_add(&cs->wd_list, &watchdog_list);
162                 __clocksource_unstable(cs);
163         }
164         spin_unlock_irqrestore(&watchdog_lock, flags);
165 }
166
167 static void clocksource_watchdog(unsigned long data)
168 {
169         struct clocksource *cs;
170         cycle_t csnow, wdnow, cslast, wdlast, delta;
171         int64_t wd_nsec, cs_nsec;
172         int next_cpu, reset_pending;
173
174         spin_lock(&watchdog_lock);
175         if (!watchdog_running)
176                 goto out;
177
178         reset_pending = atomic_read(&watchdog_reset_pending);
179
180         list_for_each_entry(cs, &watchdog_list, wd_list) {
181
182                 /* Clocksource already marked unstable? */
183                 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
184                         if (finished_booting)
185                                 schedule_work(&watchdog_work);
186                         continue;
187                 }
188
189                 local_irq_disable();
190                 csnow = cs->read(cs);
191                 wdnow = watchdog->read(watchdog);
192                 local_irq_enable();
193
194                 /* Clocksource initialized ? */
195                 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
196                     atomic_read(&watchdog_reset_pending)) {
197                         cs->flags |= CLOCK_SOURCE_WATCHDOG;
198                         cs->wd_last = wdnow;
199                         cs->cs_last = csnow;
200                         continue;
201                 }
202
203                 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
204                 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
205                                              watchdog->shift);
206
207                 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
208                 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
209                 wdlast = cs->wd_last; /* save these in case we print them */
210                 cslast = cs->cs_last;
211                 cs->cs_last = csnow;
212                 cs->wd_last = wdnow;
213
214                 if (atomic_read(&watchdog_reset_pending))
215                         continue;
216
217                 /* Check the deviation from the watchdog clocksource. */
218                 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
219                         pr_warn("timekeeping watchdog: Marking clocksource '%s' as unstable, because the skew is too large:\n", cs->name);
220                         pr_warn("       '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
221                                 watchdog->name, wdnow, wdlast, watchdog->mask);
222                         pr_warn("       '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
223                                 cs->name, csnow, cslast, cs->mask);
224                         __clocksource_unstable(cs);
225                         continue;
226                 }
227
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;
233
234                         /*
235                          * clocksource_done_booting() will sort it if
236                          * finished_booting is not set yet.
237                          */
238                         if (!finished_booting)
239                                 continue;
240
241                         /*
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
247                          * that change.
248                          */
249                         if (cs != curr_clocksource) {
250                                 cs->flags |= CLOCK_SOURCE_RESELECT;
251                                 schedule_work(&watchdog_work);
252                         } else {
253                                 tick_clock_notify();
254                         }
255                 }
256         }
257
258         /*
259          * We only clear the watchdog_reset_pending, when we did a
260          * full cycle through all clocksources.
261          */
262         if (reset_pending)
263                 atomic_dec(&watchdog_reset_pending);
264
265         /*
266          * Cycle through CPUs to check if the CPUs stay synchronized
267          * to each other.
268          */
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);
274 out:
275         spin_unlock(&watchdog_lock);
276 }
277
278 static inline void clocksource_start_watchdog(void)
279 {
280         if (watchdog_running || !watchdog || list_empty(&watchdog_list))
281                 return;
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;
287 }
288
289 static inline void clocksource_stop_watchdog(void)
290 {
291         if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
292                 return;
293         del_timer(&watchdog_timer);
294         watchdog_running = 0;
295 }
296
297 static inline void clocksource_reset_watchdog(void)
298 {
299         struct clocksource *cs;
300
301         list_for_each_entry(cs, &watchdog_list, wd_list)
302                 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
303 }
304
305 static void clocksource_resume_watchdog(void)
306 {
307         atomic_inc(&watchdog_reset_pending);
308 }
309
310 static void clocksource_enqueue_watchdog(struct clocksource *cs)
311 {
312         unsigned long flags;
313
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;
319         } else {
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) {
325                         watchdog = cs;
326                         /* Reset watchdog cycles */
327                         clocksource_reset_watchdog();
328                 }
329         }
330         /* Check if the watchdog timer needs to be started. */
331         clocksource_start_watchdog();
332         spin_unlock_irqrestore(&watchdog_lock, flags);
333 }
334
335 static void clocksource_dequeue_watchdog(struct clocksource *cs)
336 {
337         unsigned long flags;
338
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();
346                 }
347         }
348         spin_unlock_irqrestore(&watchdog_lock, flags);
349 }
350
351 static int __clocksource_watchdog_kthread(void)
352 {
353         struct clocksource *cs, *tmp;
354         unsigned long flags;
355         LIST_HEAD(unstable);
356         int select = 0;
357
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);
363                         select = 1;
364                 }
365                 if (cs->flags & CLOCK_SOURCE_RESELECT) {
366                         cs->flags &= ~CLOCK_SOURCE_RESELECT;
367                         select = 1;
368                 }
369         }
370         /* Check if the watchdog timer needs to be stopped. */
371         clocksource_stop_watchdog();
372         spin_unlock_irqrestore(&watchdog_lock, flags);
373
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);
378         }
379         return select;
380 }
381
382 static int clocksource_watchdog_kthread(void *data)
383 {
384         mutex_lock(&clocksource_mutex);
385         if (__clocksource_watchdog_kthread())
386                 clocksource_select();
387         mutex_unlock(&clocksource_mutex);
388         return 0;
389 }
390
391 static bool clocksource_is_watchdog(struct clocksource *cs)
392 {
393         return cs == watchdog;
394 }
395
396 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
397
398 static void clocksource_enqueue_watchdog(struct clocksource *cs)
399 {
400         if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
401                 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
402 }
403
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) { }
409
410 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
411
412 /**
413  * clocksource_suspend - suspend the clocksource(s)
414  */
415 void clocksource_suspend(void)
416 {
417         struct clocksource *cs;
418
419         list_for_each_entry_reverse(cs, &clocksource_list, list)
420                 if (cs->suspend)
421                         cs->suspend(cs);
422 }
423
424 /**
425  * clocksource_resume - resume the clocksource(s)
426  */
427 void clocksource_resume(void)
428 {
429         struct clocksource *cs;
430
431         list_for_each_entry(cs, &clocksource_list, list)
432                 if (cs->resume)
433                         cs->resume(cs);
434
435         clocksource_resume_watchdog();
436 }
437
438 /**
439  * clocksource_touch_watchdog - Update watchdog
440  *
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.
444  */
445 void clocksource_touch_watchdog(void)
446 {
447         clocksource_resume_watchdog();
448 }
449
450 /**
451  * clocksource_max_adjustment- Returns max adjustment amount
452  * @cs:         Pointer to clocksource
453  *
454  */
455 static u32 clocksource_max_adjustment(struct clocksource *cs)
456 {
457         u64 ret;
458         /*
459          * We won't try to correct for more than 11% adjustments (110,000 ppm),
460          */
461         ret = (u64)cs->mult * 11;
462         do_div(ret,100);
463         return (u32)ret;
464 }
465
466 /**
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
472  * @max_cyc:    maximum cycle value before potential overflow (does not include
473  *              any safety margin)
474  *
475  * NOTE: This function includes a safety margin of 50%, in other words, we
476  * return half the number of nanoseconds the hardware counter can technically
477  * cover. This is done so that we can potentially detect problems caused by
478  * delayed timers or bad hardware, which might result in time intervals that
479  * are larger then what the math used can handle without overflows.
480  */
481 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
482 {
483         u64 max_nsecs, max_cycles;
484
485         /*
486          * Calculate the maximum number of cycles that we can pass to the
487          * cyc2ns() function without overflowing a 64-bit result.
488          */
489         max_cycles = ULLONG_MAX;
490         do_div(max_cycles, mult+maxadj);
491
492         /*
493          * The actual maximum number of cycles we can defer the clocksource is
494          * determined by the minimum of max_cycles and mask.
495          * Note: Here we subtract the maxadj to make sure we don't sleep for
496          * too long if there's a large negative adjustment.
497          */
498         max_cycles = min(max_cycles, mask);
499         max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
500
501         /* return the max_cycles value as well if requested */
502         if (max_cyc)
503                 *max_cyc = max_cycles;
504
505         /* Return 50% of the actual maximum, so we can detect bad values */
506         max_nsecs >>= 1;
507
508         return max_nsecs;
509 }
510
511 /**
512  * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
513  * @cs:         Pointer to clocksource to be updated
514  *
515  */
516 static inline void clocksource_update_max_deferment(struct clocksource *cs)
517 {
518         cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
519                                                 cs->maxadj, cs->mask,
520                                                 &cs->max_cycles);
521 }
522
523 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
524
525 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
526 {
527         struct clocksource *cs;
528
529         if (!finished_booting || list_empty(&clocksource_list))
530                 return NULL;
531
532         /*
533          * We pick the clocksource with the highest rating. If oneshot
534          * mode is active, we pick the highres valid clocksource with
535          * the best rating.
536          */
537         list_for_each_entry(cs, &clocksource_list, list) {
538                 if (skipcur && cs == curr_clocksource)
539                         continue;
540                 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
541                         continue;
542                 return cs;
543         }
544         return NULL;
545 }
546
547 static void __clocksource_select(bool skipcur)
548 {
549         bool oneshot = tick_oneshot_mode_active();
550         struct clocksource *best, *cs;
551
552         /* Find the best suitable clocksource */
553         best = clocksource_find_best(oneshot, skipcur);
554         if (!best)
555                 return;
556
557         /* Check for the override clocksource. */
558         list_for_each_entry(cs, &clocksource_list, list) {
559                 if (skipcur && cs == curr_clocksource)
560                         continue;
561                 if (strcmp(cs->name, override_name) != 0)
562                         continue;
563                 /*
564                  * Check to make sure we don't switch to a non-highres
565                  * capable clocksource if the tick code is in oneshot
566                  * mode (highres or nohz)
567                  */
568                 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
569                         /* Override clocksource cannot be used. */
570                         printk(KERN_WARNING "Override clocksource %s is not "
571                                "HRT compatible. Cannot switch while in "
572                                "HRT/NOHZ mode\n", cs->name);
573                         override_name[0] = 0;
574                 } else
575                         /* Override clocksource can be used. */
576                         best = cs;
577                 break;
578         }
579
580         if (curr_clocksource != best && !timekeeping_notify(best)) {
581                 pr_info("Switched to clocksource %s\n", best->name);
582                 curr_clocksource = best;
583         }
584 }
585
586 /**
587  * clocksource_select - Select the best clocksource available
588  *
589  * Private function. Must hold clocksource_mutex when called.
590  *
591  * Select the clocksource with the best rating, or the clocksource,
592  * which is selected by userspace override.
593  */
594 static void clocksource_select(void)
595 {
596         return __clocksource_select(false);
597 }
598
599 static void clocksource_select_fallback(void)
600 {
601         return __clocksource_select(true);
602 }
603
604 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
605
606 static inline void clocksource_select(void) { }
607 static inline void clocksource_select_fallback(void) { }
608
609 #endif
610
611 /*
612  * clocksource_done_booting - Called near the end of core bootup
613  *
614  * Hack to avoid lots of clocksource churn at boot time.
615  * We use fs_initcall because we want this to start before
616  * device_initcall but after subsys_initcall.
617  */
618 static int __init clocksource_done_booting(void)
619 {
620         mutex_lock(&clocksource_mutex);
621         curr_clocksource = clocksource_default_clock();
622         finished_booting = 1;
623         /*
624          * Run the watchdog first to eliminate unstable clock sources
625          */
626         __clocksource_watchdog_kthread();
627         clocksource_select();
628         mutex_unlock(&clocksource_mutex);
629         return 0;
630 }
631 fs_initcall(clocksource_done_booting);
632
633 /*
634  * Enqueue the clocksource sorted by rating
635  */
636 static void clocksource_enqueue(struct clocksource *cs)
637 {
638         struct list_head *entry = &clocksource_list;
639         struct clocksource *tmp;
640
641         list_for_each_entry(tmp, &clocksource_list, list)
642                 /* Keep track of the place, where to insert */
643                 if (tmp->rating >= cs->rating)
644                         entry = &tmp->list;
645         list_add(&cs->list, entry);
646 }
647
648 /**
649  * __clocksource_update_freq_scale - Used update clocksource with new freq
650  * @cs:         clocksource to be registered
651  * @scale:      Scale factor multiplied against freq to get clocksource hz
652  * @freq:       clocksource frequency (cycles per second) divided by scale
653  *
654  * This should only be called from the clocksource->enable() method.
655  *
656  * This *SHOULD NOT* be called directly! Please use the
657  * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
658  * functions.
659  */
660 void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
661 {
662         u64 sec;
663
664         /*
665          * Default clocksources are *special* and self-define their mult/shift.
666          * But, you're not special, so you should specify a freq value.
667          */
668         if (freq) {
669                 /*
670                  * Calc the maximum number of seconds which we can run before
671                  * wrapping around. For clocksources which have a mask > 32-bit
672                  * we need to limit the max sleep time to have a good
673                  * conversion precision. 10 minutes is still a reasonable
674                  * amount. That results in a shift value of 24 for a
675                  * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
676                  * ~ 0.06ppm granularity for NTP.
677                  */
678                 sec = cs->mask;
679                 do_div(sec, freq);
680                 do_div(sec, scale);
681                 if (!sec)
682                         sec = 1;
683                 else if (sec > 600 && cs->mask > UINT_MAX)
684                         sec = 600;
685
686                 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
687                                        NSEC_PER_SEC / scale, sec * scale);
688         }
689         /*
690          * Ensure clocksources that have large 'mult' values don't overflow
691          * when adjusted.
692          */
693         cs->maxadj = clocksource_max_adjustment(cs);
694         while (freq && ((cs->mult + cs->maxadj < cs->mult)
695                 || (cs->mult - cs->maxadj > cs->mult))) {
696                 cs->mult >>= 1;
697                 cs->shift--;
698                 cs->maxadj = clocksource_max_adjustment(cs);
699         }
700
701         /*
702          * Only warn for *special* clocksources that self-define
703          * their mult/shift values and don't specify a freq.
704          */
705         WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
706                 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
707                 cs->name);
708
709         clocksource_update_max_deferment(cs);
710
711         pr_info("clocksource %s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
712                         cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
713 }
714 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
715
716 /**
717  * __clocksource_register_scale - Used to install new clocksources
718  * @cs:         clocksource to be registered
719  * @scale:      Scale factor multiplied against freq to get clocksource hz
720  * @freq:       clocksource frequency (cycles per second) divided by scale
721  *
722  * Returns -EBUSY if registration fails, zero otherwise.
723  *
724  * This *SHOULD NOT* be called directly! Please use the
725  * clocksource_register_hz() or clocksource_register_khz helper functions.
726  */
727 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
728 {
729
730         /* Initialize mult/shift and max_idle_ns */
731         __clocksource_update_freq_scale(cs, scale, freq);
732
733         /* Add clocksource to the clocksource list */
734         mutex_lock(&clocksource_mutex);
735         clocksource_enqueue(cs);
736         clocksource_enqueue_watchdog(cs);
737         clocksource_select();
738         mutex_unlock(&clocksource_mutex);
739         return 0;
740 }
741 EXPORT_SYMBOL_GPL(__clocksource_register_scale);
742
743 static void __clocksource_change_rating(struct clocksource *cs, int rating)
744 {
745         list_del(&cs->list);
746         cs->rating = rating;
747         clocksource_enqueue(cs);
748 }
749
750 /**
751  * clocksource_change_rating - Change the rating of a registered clocksource
752  * @cs:         clocksource to be changed
753  * @rating:     new rating
754  */
755 void clocksource_change_rating(struct clocksource *cs, int rating)
756 {
757         mutex_lock(&clocksource_mutex);
758         __clocksource_change_rating(cs, rating);
759         clocksource_select();
760         mutex_unlock(&clocksource_mutex);
761 }
762 EXPORT_SYMBOL(clocksource_change_rating);
763
764 /*
765  * Unbind clocksource @cs. Called with clocksource_mutex held
766  */
767 static int clocksource_unbind(struct clocksource *cs)
768 {
769         /*
770          * I really can't convince myself to support this on hardware
771          * designed by lobotomized monkeys.
772          */
773         if (clocksource_is_watchdog(cs))
774                 return -EBUSY;
775
776         if (cs == curr_clocksource) {
777                 /* Select and try to install a replacement clock source */
778                 clocksource_select_fallback();
779                 if (curr_clocksource == cs)
780                         return -EBUSY;
781         }
782         clocksource_dequeue_watchdog(cs);
783         list_del_init(&cs->list);
784         return 0;
785 }
786
787 /**
788  * clocksource_unregister - remove a registered clocksource
789  * @cs: clocksource to be unregistered
790  */
791 int clocksource_unregister(struct clocksource *cs)
792 {
793         int ret = 0;
794
795         mutex_lock(&clocksource_mutex);
796         if (!list_empty(&cs->list))
797                 ret = clocksource_unbind(cs);
798         mutex_unlock(&clocksource_mutex);
799         return ret;
800 }
801 EXPORT_SYMBOL(clocksource_unregister);
802
803 #ifdef CONFIG_SYSFS
804 /**
805  * sysfs_show_current_clocksources - sysfs interface for current clocksource
806  * @dev:        unused
807  * @attr:       unused
808  * @buf:        char buffer to be filled with clocksource list
809  *
810  * Provides sysfs interface for listing current clocksource.
811  */
812 static ssize_t
813 sysfs_show_current_clocksources(struct device *dev,
814                                 struct device_attribute *attr, char *buf)
815 {
816         ssize_t count = 0;
817
818         mutex_lock(&clocksource_mutex);
819         count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
820         mutex_unlock(&clocksource_mutex);
821
822         return count;
823 }
824
825 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
826 {
827         size_t ret = cnt;
828
829         /* strings from sysfs write are not 0 terminated! */
830         if (!cnt || cnt >= CS_NAME_LEN)
831                 return -EINVAL;
832
833         /* strip of \n: */
834         if (buf[cnt-1] == '\n')
835                 cnt--;
836         if (cnt > 0)
837                 memcpy(dst, buf, cnt);
838         dst[cnt] = 0;
839         return ret;
840 }
841
842 /**
843  * sysfs_override_clocksource - interface for manually overriding clocksource
844  * @dev:        unused
845  * @attr:       unused
846  * @buf:        name of override clocksource
847  * @count:      length of buffer
848  *
849  * Takes input from sysfs interface for manually overriding the default
850  * clocksource selection.
851  */
852 static ssize_t sysfs_override_clocksource(struct device *dev,
853                                           struct device_attribute *attr,
854                                           const char *buf, size_t count)
855 {
856         ssize_t ret;
857
858         mutex_lock(&clocksource_mutex);
859
860         ret = sysfs_get_uname(buf, override_name, count);
861         if (ret >= 0)
862                 clocksource_select();
863
864         mutex_unlock(&clocksource_mutex);
865
866         return ret;
867 }
868
869 /**
870  * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
871  * @dev:        unused
872  * @attr:       unused
873  * @buf:        unused
874  * @count:      length of buffer
875  *
876  * Takes input from sysfs interface for manually unbinding a clocksource.
877  */
878 static ssize_t sysfs_unbind_clocksource(struct device *dev,
879                                         struct device_attribute *attr,
880                                         const char *buf, size_t count)
881 {
882         struct clocksource *cs;
883         char name[CS_NAME_LEN];
884         ssize_t ret;
885
886         ret = sysfs_get_uname(buf, name, count);
887         if (ret < 0)
888                 return ret;
889
890         ret = -ENODEV;
891         mutex_lock(&clocksource_mutex);
892         list_for_each_entry(cs, &clocksource_list, list) {
893                 if (strcmp(cs->name, name))
894                         continue;
895                 ret = clocksource_unbind(cs);
896                 break;
897         }
898         mutex_unlock(&clocksource_mutex);
899
900         return ret ? ret : count;
901 }
902
903 /**
904  * sysfs_show_available_clocksources - sysfs interface for listing clocksource
905  * @dev:        unused
906  * @attr:       unused
907  * @buf:        char buffer to be filled with clocksource list
908  *
909  * Provides sysfs interface for listing registered clocksources
910  */
911 static ssize_t
912 sysfs_show_available_clocksources(struct device *dev,
913                                   struct device_attribute *attr,
914                                   char *buf)
915 {
916         struct clocksource *src;
917         ssize_t count = 0;
918
919         mutex_lock(&clocksource_mutex);
920         list_for_each_entry(src, &clocksource_list, list) {
921                 /*
922                  * Don't show non-HRES clocksource if the tick code is
923                  * in one shot mode (highres=on or nohz=on)
924                  */
925                 if (!tick_oneshot_mode_active() ||
926                     (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
927                         count += snprintf(buf + count,
928                                   max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
929                                   "%s ", src->name);
930         }
931         mutex_unlock(&clocksource_mutex);
932
933         count += snprintf(buf + count,
934                           max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
935
936         return count;
937 }
938
939 /*
940  * Sysfs setup bits:
941  */
942 static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
943                    sysfs_override_clocksource);
944
945 static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
946
947 static DEVICE_ATTR(available_clocksource, 0444,
948                    sysfs_show_available_clocksources, NULL);
949
950 static struct bus_type clocksource_subsys = {
951         .name = "clocksource",
952         .dev_name = "clocksource",
953 };
954
955 static struct device device_clocksource = {
956         .id     = 0,
957         .bus    = &clocksource_subsys,
958 };
959
960 static int __init init_clocksource_sysfs(void)
961 {
962         int error = subsys_system_register(&clocksource_subsys, NULL);
963
964         if (!error)
965                 error = device_register(&device_clocksource);
966         if (!error)
967                 error = device_create_file(
968                                 &device_clocksource,
969                                 &dev_attr_current_clocksource);
970         if (!error)
971                 error = device_create_file(&device_clocksource,
972                                            &dev_attr_unbind_clocksource);
973         if (!error)
974                 error = device_create_file(
975                                 &device_clocksource,
976                                 &dev_attr_available_clocksource);
977         return error;
978 }
979
980 device_initcall(init_clocksource_sysfs);
981 #endif /* CONFIG_SYSFS */
982
983 /**
984  * boot_override_clocksource - boot clock override
985  * @str:        override name
986  *
987  * Takes a clocksource= boot argument and uses it
988  * as the clocksource override name.
989  */
990 static int __init boot_override_clocksource(char* str)
991 {
992         mutex_lock(&clocksource_mutex);
993         if (str)
994                 strlcpy(override_name, str, sizeof(override_name));
995         mutex_unlock(&clocksource_mutex);
996         return 1;
997 }
998
999 __setup("clocksource=", boot_override_clocksource);
1000
1001 /**
1002  * boot_override_clock - Compatibility layer for deprecated boot option
1003  * @str:        override name
1004  *
1005  * DEPRECATED! Takes a clock= boot argument and uses it
1006  * as the clocksource override name
1007  */
1008 static int __init boot_override_clock(char* str)
1009 {
1010         if (!strcmp(str, "pmtmr")) {
1011                 printk("Warning: clock=pmtmr is deprecated. "
1012                         "Use clocksource=acpi_pm.\n");
1013                 return boot_override_clocksource("acpi_pm");
1014         }
1015         printk("Warning! clock= boot option is deprecated. "
1016                 "Use clocksource=xyz\n");
1017         return boot_override_clocksource(str);
1018 }
1019
1020 __setup("clock=", boot_override_clock);