Merge tag 'md/4.12-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/shli/md
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
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>
35
36 #include "tick-internal.h"
37 #include "timekeeping_internal.h"
38
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
46 *
47 * The function evaluates the shift/mult pair for the scaled math
48 * operations of clocksources and clockevents.
49 *
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.
53 *
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.
61 */
62 void
63 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
64 {
65 u64 tmp;
66 u32 sft, sftacc= 32;
67
68 /*
69 * Calculate the shift factor which is limiting the conversion
70 * range:
71 */
72 tmp = ((u64)maxsec * from) >> 32;
73 while (tmp) {
74 tmp >>=1;
75 sftacc--;
76 }
77
78 /*
79 * Find the conversion shift/mult pair which has the best
80 * accuracy and fits the maxsec conversion range:
81 */
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;
88 }
89 *mult = tmp;
90 *shift = sft;
91 }
92 EXPORT_SYMBOL_GPL(clocks_calc_mult_shift);
93
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.
103 */
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;
109
110 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
111 static void clocksource_watchdog_work(struct work_struct *work);
112 static void clocksource_select(void);
113
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;
121
122 static int clocksource_watchdog_kthread(void *data);
123 static void __clocksource_change_rating(struct clocksource *cs, int rating);
124
125 /*
126 * Interval: 0.5sec Threshold: 0.0625s
127 */
128 #define WATCHDOG_INTERVAL (HZ >> 1)
129 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
130
131 static void clocksource_watchdog_work(struct work_struct *work)
132 {
133 /*
134 * If kthread_run fails the next watchdog scan over the
135 * watchdog_list will find the unstable clock again.
136 */
137 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
138 }
139
140 static void __clocksource_unstable(struct clocksource *cs)
141 {
142 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
143 cs->flags |= CLOCK_SOURCE_UNSTABLE;
144
145 if (cs->mark_unstable)
146 cs->mark_unstable(cs);
147
148 if (finished_booting)
149 schedule_work(&watchdog_work);
150 }
151
152 /**
153 * clocksource_mark_unstable - mark clocksource unstable via watchdog
154 * @cs: clocksource to be marked unstable
155 *
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.
160 */
161 void clocksource_mark_unstable(struct clocksource *cs)
162 {
163 unsigned long flags;
164
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);
170 }
171 spin_unlock_irqrestore(&watchdog_lock, flags);
172 }
173
174 static void clocksource_watchdog(unsigned long data)
175 {
176 struct clocksource *cs;
177 u64 csnow, wdnow, cslast, wdlast, delta;
178 int64_t wd_nsec, cs_nsec;
179 int next_cpu, reset_pending;
180
181 spin_lock(&watchdog_lock);
182 if (!watchdog_running)
183 goto out;
184
185 reset_pending = atomic_read(&watchdog_reset_pending);
186
187 list_for_each_entry(cs, &watchdog_list, wd_list) {
188
189 /* Clocksource already marked unstable? */
190 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
191 if (finished_booting)
192 schedule_work(&watchdog_work);
193 continue;
194 }
195
196 local_irq_disable();
197 csnow = cs->read(cs);
198 wdnow = watchdog->read(watchdog);
199 local_irq_enable();
200
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;
208 }
209
210 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
211 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
212 watchdog->shift);
213
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;
220
221 if (atomic_read(&watchdog_reset_pending))
222 continue;
223
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;
234 }
235
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;
241
242 /*
243 * clocksource_done_booting() will sort it if
244 * finished_booting is not set yet.
245 */
246 if (!finished_booting)
247 continue;
248
249 /*
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.
256 */
257 if (cs != curr_clocksource) {
258 cs->flags |= CLOCK_SOURCE_RESELECT;
259 schedule_work(&watchdog_work);
260 } else {
261 tick_clock_notify();
262 }
263 }
264 }
265
266 /*
267 * We only clear the watchdog_reset_pending, when we did a
268 * full cycle through all clocksources.
269 */
270 if (reset_pending)
271 atomic_dec(&watchdog_reset_pending);
272
273 /*
274 * Cycle through CPUs to check if the CPUs stay synchronized
275 * to each other.
276 */
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);
284 }
285
286 static inline void clocksource_start_watchdog(void)
287 {
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;
295 }
296
297 static inline void clocksource_stop_watchdog(void)
298 {
299 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
300 return;
301 del_timer(&watchdog_timer);
302 watchdog_running = 0;
303 }
304
305 static inline void clocksource_reset_watchdog(void)
306 {
307 struct clocksource *cs;
308
309 list_for_each_entry(cs, &watchdog_list, wd_list)
310 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
311 }
312
313 static void clocksource_resume_watchdog(void)
314 {
315 atomic_inc(&watchdog_reset_pending);
316 }
317
318 static void clocksource_enqueue_watchdog(struct clocksource *cs)
319 {
320 unsigned long flags;
321
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;
331 }
332 spin_unlock_irqrestore(&watchdog_lock, flags);
333 }
334
335 static void clocksource_select_watchdog(bool fallback)
336 {
337 struct clocksource *cs, *old_wd;
338 unsigned long flags;
339
340 spin_lock_irqsave(&watchdog_lock, flags);
341 /* save current watchdog */
342 old_wd = watchdog;
343 if (fallback)
344 watchdog = NULL;
345
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;
350
351 /* Skip current if we were requested for a fallback. */
352 if (fallback && cs == old_wd)
353 continue;
354
355 /* Pick the best watchdog. */
356 if (!watchdog || cs->rating > watchdog->rating)
357 watchdog = cs;
358 }
359 /* If we failed to find a fallback restore the old one. */
360 if (!watchdog)
361 watchdog = old_wd;
362
363 /* If we changed the watchdog we need to reset cycles. */
364 if (watchdog != old_wd)
365 clocksource_reset_watchdog();
366
367 /* Check if the watchdog timer needs to be started. */
368 clocksource_start_watchdog();
369 spin_unlock_irqrestore(&watchdog_lock, flags);
370 }
371
372 static void clocksource_dequeue_watchdog(struct clocksource *cs)
373 {
374 unsigned long flags;
375
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();
383 }
384 }
385 spin_unlock_irqrestore(&watchdog_lock, flags);
386 }
387
388 static int __clocksource_watchdog_kthread(void)
389 {
390 struct clocksource *cs, *tmp;
391 unsigned long flags;
392 LIST_HEAD(unstable);
393 int select = 0;
394
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;
401 }
402 if (cs->flags & CLOCK_SOURCE_RESELECT) {
403 cs->flags &= ~CLOCK_SOURCE_RESELECT;
404 select = 1;
405 }
406 }
407 /* Check if the watchdog timer needs to be stopped. */
408 clocksource_stop_watchdog();
409 spin_unlock_irqrestore(&watchdog_lock, flags);
410
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);
415 }
416 return select;
417 }
418
419 static int clocksource_watchdog_kthread(void *data)
420 {
421 mutex_lock(&clocksource_mutex);
422 if (__clocksource_watchdog_kthread())
423 clocksource_select();
424 mutex_unlock(&clocksource_mutex);
425 return 0;
426 }
427
428 static bool clocksource_is_watchdog(struct clocksource *cs)
429 {
430 return cs == watchdog;
431 }
432
433 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
434
435 static void clocksource_enqueue_watchdog(struct clocksource *cs)
436 {
437 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
438 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
439 }
440
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) { }
447
448 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
449
450 /**
451 * clocksource_suspend - suspend the clocksource(s)
452 */
453 void clocksource_suspend(void)
454 {
455 struct clocksource *cs;
456
457 list_for_each_entry_reverse(cs, &clocksource_list, list)
458 if (cs->suspend)
459 cs->suspend(cs);
460 }
461
462 /**
463 * clocksource_resume - resume the clocksource(s)
464 */
465 void clocksource_resume(void)
466 {
467 struct clocksource *cs;
468
469 list_for_each_entry(cs, &clocksource_list, list)
470 if (cs->resume)
471 cs->resume(cs);
472
473 clocksource_resume_watchdog();
474 }
475
476 /**
477 * clocksource_touch_watchdog - Update watchdog
478 *
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.
482 */
483 void clocksource_touch_watchdog(void)
484 {
485 clocksource_resume_watchdog();
486 }
487
488 /**
489 * clocksource_max_adjustment- Returns max adjustment amount
490 * @cs: Pointer to clocksource
491 *
492 */
493 static u32 clocksource_max_adjustment(struct clocksource *cs)
494 {
495 u64 ret;
496 /*
497 * We won't try to correct for more than 11% adjustments (110,000 ppm),
498 */
499 ret = (u64)cs->mult * 11;
500 do_div(ret,100);
501 return (u32)ret;
502 }
503
504 /**
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)
512 *
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.
518 */
519 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
520 {
521 u64 max_nsecs, max_cycles;
522
523 /*
524 * Calculate the maximum number of cycles that we can pass to the
525 * cyc2ns() function without overflowing a 64-bit result.
526 */
527 max_cycles = ULLONG_MAX;
528 do_div(max_cycles, mult+maxadj);
529
530 /*
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.
535 */
536 max_cycles = min(max_cycles, mask);
537 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
538
539 /* return the max_cycles value as well if requested */
540 if (max_cyc)
541 *max_cyc = max_cycles;
542
543 /* Return 50% of the actual maximum, so we can detect bad values */
544 max_nsecs >>= 1;
545
546 return max_nsecs;
547 }
548
549 /**
550 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
551 * @cs: Pointer to clocksource to be updated
552 *
553 */
554 static inline void clocksource_update_max_deferment(struct clocksource *cs)
555 {
556 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
557 cs->maxadj, cs->mask,
558 &cs->max_cycles);
559 }
560
561 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
562
563 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
564 {
565 struct clocksource *cs;
566
567 if (!finished_booting || list_empty(&clocksource_list))
568 return NULL;
569
570 /*
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.
574 */
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;
581 }
582 return NULL;
583 }
584
585 static void __clocksource_select(bool skipcur)
586 {
587 bool oneshot = tick_oneshot_mode_active();
588 struct clocksource *best, *cs;
589
590 /* Find the best suitable clocksource */
591 best = clocksource_find_best(oneshot, skipcur);
592 if (!best)
593 return;
594
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;
601 /*
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)
605 */
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 {
613 /*
614 * The override cannot be currently verified.
615 * Deferring to let the watchdog check.
616 */
617 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
618 cs->name);
619 }
620 } else
621 /* Override clocksource can be used. */
622 best = cs;
623 break;
624 }
625
626 if (curr_clocksource != best && !timekeeping_notify(best)) {
627 pr_info("Switched to clocksource %s\n", best->name);
628 curr_clocksource = best;
629 }
630 }
631
632 /**
633 * clocksource_select - Select the best clocksource available
634 *
635 * Private function. Must hold clocksource_mutex when called.
636 *
637 * Select the clocksource with the best rating, or the clocksource,
638 * which is selected by userspace override.
639 */
640 static void clocksource_select(void)
641 {
642 __clocksource_select(false);
643 }
644
645 static void clocksource_select_fallback(void)
646 {
647 __clocksource_select(true);
648 }
649
650 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
651 static inline void clocksource_select(void) { }
652 static inline void clocksource_select_fallback(void) { }
653
654 #endif
655
656 /*
657 * clocksource_done_booting - Called near the end of core bootup
658 *
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.
662 */
663 static int __init clocksource_done_booting(void)
664 {
665 mutex_lock(&clocksource_mutex);
666 curr_clocksource = clocksource_default_clock();
667 finished_booting = 1;
668 /*
669 * Run the watchdog first to eliminate unstable clock sources
670 */
671 __clocksource_watchdog_kthread();
672 clocksource_select();
673 mutex_unlock(&clocksource_mutex);
674 return 0;
675 }
676 fs_initcall(clocksource_done_booting);
677
678 /*
679 * Enqueue the clocksource sorted by rating
680 */
681 static void clocksource_enqueue(struct clocksource *cs)
682 {
683 struct list_head *entry = &clocksource_list;
684 struct clocksource *tmp;
685
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;
691 }
692 list_add(&cs->list, entry);
693 }
694
695 /**
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
700 *
701 * This should only be called from the clocksource->enable() method.
702 *
703 * This *SHOULD NOT* be called directly! Please use the
704 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
705 * functions.
706 */
707 void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
708 {
709 u64 sec;
710
711 /*
712 * Default clocksources are *special* and self-define their mult/shift.
713 * But, you're not special, so you should specify a freq value.
714 */
715 if (freq) {
716 /*
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.
724 */
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;
732
733 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
734 NSEC_PER_SEC / scale, sec * scale);
735 }
736 /*
737 * Ensure clocksources that have large 'mult' values don't overflow
738 * when adjusted.
739 */
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);
746 }
747
748 /*
749 * Only warn for *special* clocksources that self-define
750 * their mult/shift values and don't specify a freq.
751 */
752 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
753 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
754 cs->name);
755
756 clocksource_update_max_deferment(cs);
757
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);
760 }
761 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
762
763 /**
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
768 *
769 * Returns -EBUSY if registration fails, zero otherwise.
770 *
771 * This *SHOULD NOT* be called directly! Please use the
772 * clocksource_register_hz() or clocksource_register_khz helper functions.
773 */
774 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
775 {
776
777 /* Initialize mult/shift and max_idle_ns */
778 __clocksource_update_freq_scale(cs, scale, freq);
779
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;
788 }
789 EXPORT_SYMBOL_GPL(__clocksource_register_scale);
790
791 static void __clocksource_change_rating(struct clocksource *cs, int rating)
792 {
793 list_del(&cs->list);
794 cs->rating = rating;
795 clocksource_enqueue(cs);
796 }
797
798 /**
799 * clocksource_change_rating - Change the rating of a registered clocksource
800 * @cs: clocksource to be changed
801 * @rating: new rating
802 */
803 void clocksource_change_rating(struct clocksource *cs, int rating)
804 {
805 mutex_lock(&clocksource_mutex);
806 __clocksource_change_rating(cs, rating);
807 clocksource_select();
808 clocksource_select_watchdog(false);
809 mutex_unlock(&clocksource_mutex);
810 }
811 EXPORT_SYMBOL(clocksource_change_rating);
812
813 /*
814 * Unbind clocksource @cs. Called with clocksource_mutex held
815 */
816 static int clocksource_unbind(struct clocksource *cs)
817 {
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;
823 }
824
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;
830 }
831 clocksource_dequeue_watchdog(cs);
832 list_del_init(&cs->list);
833 return 0;
834 }
835
836 /**
837 * clocksource_unregister - remove a registered clocksource
838 * @cs: clocksource to be unregistered
839 */
840 int clocksource_unregister(struct clocksource *cs)
841 {
842 int ret = 0;
843
844 mutex_lock(&clocksource_mutex);
845 if (!list_empty(&cs->list))
846 ret = clocksource_unbind(cs);
847 mutex_unlock(&clocksource_mutex);
848 return ret;
849 }
850 EXPORT_SYMBOL(clocksource_unregister);
851
852 #ifdef CONFIG_SYSFS
853 /**
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
858 *
859 * Provides sysfs interface for listing current clocksource.
860 */
861 static ssize_t
862 sysfs_show_current_clocksources(struct device *dev,
863 struct device_attribute *attr, char *buf)
864 {
865 ssize_t count = 0;
866
867 mutex_lock(&clocksource_mutex);
868 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
869 mutex_unlock(&clocksource_mutex);
870
871 return count;
872 }
873
874 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
875 {
876 size_t ret = cnt;
877
878 /* strings from sysfs write are not 0 terminated! */
879 if (!cnt || cnt >= CS_NAME_LEN)
880 return -EINVAL;
881
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;
889 }
890
891 /**
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
897 *
898 * Takes input from sysfs interface for manually overriding the default
899 * clocksource selection.
900 */
901 static ssize_t sysfs_override_clocksource(struct device *dev,
902 struct device_attribute *attr,
903 const char *buf, size_t count)
904 {
905 ssize_t ret;
906
907 mutex_lock(&clocksource_mutex);
908
909 ret = sysfs_get_uname(buf, override_name, count);
910 if (ret >= 0)
911 clocksource_select();
912
913 mutex_unlock(&clocksource_mutex);
914
915 return ret;
916 }
917
918 /**
919 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
920 * @dev: unused
921 * @attr: unused
922 * @buf: unused
923 * @count: length of buffer
924 *
925 * Takes input from sysfs interface for manually unbinding a clocksource.
926 */
927 static ssize_t sysfs_unbind_clocksource(struct device *dev,
928 struct device_attribute *attr,
929 const char *buf, size_t count)
930 {
931 struct clocksource *cs;
932 char name[CS_NAME_LEN];
933 ssize_t ret;
934
935 ret = sysfs_get_uname(buf, name, count);
936 if (ret < 0)
937 return ret;
938
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;
946 }
947 mutex_unlock(&clocksource_mutex);
948
949 return ret ? ret : count;
950 }
951
952 /**
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
957 *
958 * Provides sysfs interface for listing registered clocksources
959 */
960 static ssize_t
961 sysfs_show_available_clocksources(struct device *dev,
962 struct device_attribute *attr,
963 char *buf)
964 {
965 struct clocksource *src;
966 ssize_t count = 0;
967
968 mutex_lock(&clocksource_mutex);
969 list_for_each_entry(src, &clocksource_list, list) {
970 /*
971 * Don't show non-HRES clocksource if the tick code is
972 * in one shot mode (highres=on or nohz=on)
973 */
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);
979 }
980 mutex_unlock(&clocksource_mutex);
981
982 count += snprintf(buf + count,
983 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
984
985 return count;
986 }
987
988 /*
989 * Sysfs setup bits:
990 */
991 static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
992 sysfs_override_clocksource);
993
994 static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
995
996 static DEVICE_ATTR(available_clocksource, 0444,
997 sysfs_show_available_clocksources, NULL);
998
999 static struct bus_type clocksource_subsys = {
1000 .name = "clocksource",
1001 .dev_name = "clocksource",
1002 };
1003
1004 static struct device device_clocksource = {
1005 .id = 0,
1006 .bus = &clocksource_subsys,
1007 };
1008
1009 static int __init init_clocksource_sysfs(void)
1010 {
1011 int error = subsys_system_register(&clocksource_subsys, NULL);
1012
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;
1027 }
1028
1029 device_initcall(init_clocksource_sysfs);
1030 #endif /* CONFIG_SYSFS */
1031
1032 /**
1033 * boot_override_clocksource - boot clock override
1034 * @str: override name
1035 *
1036 * Takes a clocksource= boot argument and uses it
1037 * as the clocksource override name.
1038 */
1039 static int __init boot_override_clocksource(char* str)
1040 {
1041 mutex_lock(&clocksource_mutex);
1042 if (str)
1043 strlcpy(override_name, str, sizeof(override_name));
1044 mutex_unlock(&clocksource_mutex);
1045 return 1;
1046 }
1047
1048 __setup("clocksource=", boot_override_clocksource);
1049
1050 /**
1051 * boot_override_clock - Compatibility layer for deprecated boot option
1052 * @str: override name
1053 *
1054 * DEPRECATED! Takes a clock= boot argument and uses it
1055 * as the clocksource override name
1056 */
1057 static int __init boot_override_clock(char* str)
1058 {
1059 if (!strcmp(str, "pmtmr")) {
1060 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
1061 return boot_override_clocksource("acpi_pm");
1062 }
1063 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
1064 return boot_override_clocksource(str);
1065 }
1066
1067 __setup("clock=", boot_override_clock);