]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/time/clocksource.c
clockevents: Split out selection logic
[mirror_ubuntu-zesty-kernel.git] / kernel / time / clocksource.c
CommitLineData
734efb46
JS
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
734efb46
JS
24 */
25
d369a5d8 26#include <linux/device.h>
734efb46 27#include <linux/clocksource.h>
734efb46
JS
28#include <linux/init.h>
29#include <linux/module.h>
dc29a365 30#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
79bf2bb3 31#include <linux/tick.h>
01548f4d 32#include <linux/kthread.h>
734efb46 33
a038a353
PO
34void timecounter_init(struct timecounter *tc,
35 const struct cyclecounter *cc,
36 u64 start_tstamp)
37{
38 tc->cc = cc;
39 tc->cycle_last = cc->read(cc);
40 tc->nsec = start_tstamp;
41}
3586e0a9 42EXPORT_SYMBOL_GPL(timecounter_init);
a038a353
PO
43
44/**
45 * timecounter_read_delta - get nanoseconds since last call of this function
46 * @tc: Pointer to time counter
47 *
48 * When the underlying cycle counter runs over, this will be handled
49 * correctly as long as it does not run over more than once between
50 * calls.
51 *
52 * The first call to this function for a new time counter initializes
53 * the time tracking and returns an undefined result.
54 */
55static u64 timecounter_read_delta(struct timecounter *tc)
56{
57 cycle_t cycle_now, cycle_delta;
58 u64 ns_offset;
59
60 /* read cycle counter: */
61 cycle_now = tc->cc->read(tc->cc);
62
63 /* calculate the delta since the last timecounter_read_delta(): */
64 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
65
66 /* convert to nanoseconds: */
67 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
68
69 /* update time stamp of timecounter_read_delta() call: */
70 tc->cycle_last = cycle_now;
71
72 return ns_offset;
73}
74
75u64 timecounter_read(struct timecounter *tc)
76{
77 u64 nsec;
78
79 /* increment time by nanoseconds since last call */
80 nsec = timecounter_read_delta(tc);
81 nsec += tc->nsec;
82 tc->nsec = nsec;
83
84 return nsec;
85}
3586e0a9 86EXPORT_SYMBOL_GPL(timecounter_read);
a038a353
PO
87
88u64 timecounter_cyc2time(struct timecounter *tc,
89 cycle_t cycle_tstamp)
90{
91 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
92 u64 nsec;
93
94 /*
95 * Instead of always treating cycle_tstamp as more recent
96 * than tc->cycle_last, detect when it is too far in the
97 * future and treat it as old time stamp instead.
98 */
99 if (cycle_delta > tc->cc->mask / 2) {
100 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
101 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
102 } else {
103 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
104 }
105
106 return nsec;
107}
3586e0a9 108EXPORT_SYMBOL_GPL(timecounter_cyc2time);
a038a353 109
7d2f944a
TG
110/**
111 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
112 * @mult: pointer to mult variable
113 * @shift: pointer to shift variable
114 * @from: frequency to convert from
115 * @to: frequency to convert to
5fdade95 116 * @maxsec: guaranteed runtime conversion range in seconds
7d2f944a
TG
117 *
118 * The function evaluates the shift/mult pair for the scaled math
119 * operations of clocksources and clockevents.
120 *
121 * @to and @from are frequency values in HZ. For clock sources @to is
122 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
123 * event @to is the counter frequency and @from is NSEC_PER_SEC.
124 *
5fdade95 125 * The @maxsec conversion range argument controls the time frame in
7d2f944a
TG
126 * seconds which must be covered by the runtime conversion with the
127 * calculated mult and shift factors. This guarantees that no 64bit
128 * overflow happens when the input value of the conversion is
129 * multiplied with the calculated mult factor. Larger ranges may
130 * reduce the conversion accuracy by chosing smaller mult and shift
131 * factors.
132 */
133void
5fdade95 134clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
7d2f944a
TG
135{
136 u64 tmp;
137 u32 sft, sftacc= 32;
138
139 /*
140 * Calculate the shift factor which is limiting the conversion
141 * range:
142 */
5fdade95 143 tmp = ((u64)maxsec * from) >> 32;
7d2f944a
TG
144 while (tmp) {
145 tmp >>=1;
146 sftacc--;
147 }
148
149 /*
150 * Find the conversion shift/mult pair which has the best
151 * accuracy and fits the maxsec conversion range:
152 */
153 for (sft = 32; sft > 0; sft--) {
154 tmp = (u64) to << sft;
b5776c4a 155 tmp += from / 2;
7d2f944a
TG
156 do_div(tmp, from);
157 if ((tmp >> sftacc) == 0)
158 break;
159 }
160 *mult = tmp;
161 *shift = sft;
162}
163
734efb46
JS
164/*[Clocksource internal variables]---------
165 * curr_clocksource:
f1b82746 166 * currently selected clocksource.
734efb46
JS
167 * clocksource_list:
168 * linked list with the registered clocksources
75c5158f
MS
169 * clocksource_mutex:
170 * protects manipulations to curr_clocksource and the clocksource_list
734efb46
JS
171 * override_name:
172 * Name of the user-specified clocksource.
173 */
f1b82746 174static struct clocksource *curr_clocksource;
734efb46 175static LIST_HEAD(clocksource_list);
75c5158f 176static DEFINE_MUTEX(clocksource_mutex);
29b54078
TG
177#define CS_NAME_LEN 32
178static char override_name[CS_NAME_LEN];
54a6bc0b 179static int finished_booting;
734efb46 180
5d8b34fd 181#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
f79e0258
MS
182static void clocksource_watchdog_work(struct work_struct *work);
183
5d8b34fd
TG
184static LIST_HEAD(watchdog_list);
185static struct clocksource *watchdog;
186static struct timer_list watchdog_timer;
f79e0258 187static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
5d8b34fd 188static DEFINE_SPINLOCK(watchdog_lock);
fb63a0eb 189static int watchdog_running;
9fb60336 190static atomic_t watchdog_reset_pending;
b52f52a0 191
01548f4d 192static int clocksource_watchdog_kthread(void *data);
d0981a1b 193static void __clocksource_change_rating(struct clocksource *cs, int rating);
c55c87c8 194
5d8b34fd 195/*
35c35d1a 196 * Interval: 0.5sec Threshold: 0.0625s
5d8b34fd
TG
197 */
198#define WATCHDOG_INTERVAL (HZ >> 1)
35c35d1a 199#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
5d8b34fd 200
01548f4d
MS
201static void clocksource_watchdog_work(struct work_struct *work)
202{
203 /*
204 * If kthread_run fails the next watchdog scan over the
205 * watchdog_list will find the unstable clock again.
206 */
207 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
208}
209
7285dd7f 210static void __clocksource_unstable(struct clocksource *cs)
5d8b34fd 211{
5d8b34fd 212 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
c55c87c8 213 cs->flags |= CLOCK_SOURCE_UNSTABLE;
54a6bc0b
TG
214 if (finished_booting)
215 schedule_work(&watchdog_work);
5d8b34fd
TG
216}
217
7285dd7f
TG
218static void clocksource_unstable(struct clocksource *cs, int64_t delta)
219{
220 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
221 cs->name, delta);
222 __clocksource_unstable(cs);
223}
224
225/**
226 * clocksource_mark_unstable - mark clocksource unstable via watchdog
227 * @cs: clocksource to be marked unstable
228 *
229 * This function is called instead of clocksource_change_rating from
230 * cpu hotplug code to avoid a deadlock between the clocksource mutex
231 * and the cpu hotplug mutex. It defers the update of the clocksource
232 * to the watchdog thread.
233 */
234void clocksource_mark_unstable(struct clocksource *cs)
235{
236 unsigned long flags;
237
238 spin_lock_irqsave(&watchdog_lock, flags);
239 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
240 if (list_empty(&cs->wd_list))
241 list_add(&cs->wd_list, &watchdog_list);
242 __clocksource_unstable(cs);
243 }
244 spin_unlock_irqrestore(&watchdog_lock, flags);
245}
246
5d8b34fd
TG
247static void clocksource_watchdog(unsigned long data)
248{
c55c87c8 249 struct clocksource *cs;
5d8b34fd
TG
250 cycle_t csnow, wdnow;
251 int64_t wd_nsec, cs_nsec;
9fb60336 252 int next_cpu, reset_pending;
5d8b34fd
TG
253
254 spin_lock(&watchdog_lock);
fb63a0eb
MS
255 if (!watchdog_running)
256 goto out;
5d8b34fd 257
9fb60336
TG
258 reset_pending = atomic_read(&watchdog_reset_pending);
259
c55c87c8
MS
260 list_for_each_entry(cs, &watchdog_list, wd_list) {
261
262 /* Clocksource already marked unstable? */
01548f4d 263 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
54a6bc0b
TG
264 if (finished_booting)
265 schedule_work(&watchdog_work);
c55c87c8 266 continue;
01548f4d 267 }
c55c87c8 268
b5199515 269 local_irq_disable();
8e19608e 270 csnow = cs->read(cs);
b5199515
TG
271 wdnow = watchdog->read(watchdog);
272 local_irq_enable();
b52f52a0 273
8cf4e750 274 /* Clocksource initialized ? */
9fb60336
TG
275 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
276 atomic_read(&watchdog_reset_pending)) {
8cf4e750 277 cs->flags |= CLOCK_SOURCE_WATCHDOG;
b5199515
TG
278 cs->wd_last = wdnow;
279 cs->cs_last = csnow;
b52f52a0
TG
280 continue;
281 }
282
b5199515
TG
283 wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask,
284 watchdog->mult, watchdog->shift);
285
286 cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
155ec602 287 cs->mask, cs->mult, cs->shift);
b5199515
TG
288 cs->cs_last = csnow;
289 cs->wd_last = wdnow;
290
9fb60336
TG
291 if (atomic_read(&watchdog_reset_pending))
292 continue;
293
b5199515 294 /* Check the deviation from the watchdog clocksource. */
9fb60336 295 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
8cf4e750
MS
296 clocksource_unstable(cs, cs_nsec - wd_nsec);
297 continue;
298 }
299
300 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
301 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
302 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
303 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
304 /*
305 * We just marked the clocksource as highres-capable,
306 * notify the rest of the system as well so that we
307 * transition into high-res mode:
308 */
309 tick_clock_notify();
5d8b34fd
TG
310 }
311 }
312
9fb60336
TG
313 /*
314 * We only clear the watchdog_reset_pending, when we did a
315 * full cycle through all clocksources.
316 */
317 if (reset_pending)
318 atomic_dec(&watchdog_reset_pending);
319
c55c87c8
MS
320 /*
321 * Cycle through CPUs to check if the CPUs stay synchronized
322 * to each other.
323 */
324 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
325 if (next_cpu >= nr_cpu_ids)
326 next_cpu = cpumask_first(cpu_online_mask);
327 watchdog_timer.expires += WATCHDOG_INTERVAL;
328 add_timer_on(&watchdog_timer, next_cpu);
fb63a0eb 329out:
5d8b34fd
TG
330 spin_unlock(&watchdog_lock);
331}
0f8e8ef7 332
fb63a0eb
MS
333static inline void clocksource_start_watchdog(void)
334{
335 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
336 return;
337 init_timer(&watchdog_timer);
338 watchdog_timer.function = clocksource_watchdog;
fb63a0eb
MS
339 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
340 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
341 watchdog_running = 1;
342}
343
344static inline void clocksource_stop_watchdog(void)
345{
346 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
347 return;
348 del_timer(&watchdog_timer);
349 watchdog_running = 0;
350}
351
0f8e8ef7
MS
352static inline void clocksource_reset_watchdog(void)
353{
354 struct clocksource *cs;
355
356 list_for_each_entry(cs, &watchdog_list, wd_list)
357 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
358}
359
b52f52a0
TG
360static void clocksource_resume_watchdog(void)
361{
9fb60336 362 atomic_inc(&watchdog_reset_pending);
b52f52a0
TG
363}
364
fb63a0eb 365static void clocksource_enqueue_watchdog(struct clocksource *cs)
5d8b34fd 366{
5d8b34fd
TG
367 unsigned long flags;
368
369 spin_lock_irqsave(&watchdog_lock, flags);
370 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
fb63a0eb 371 /* cs is a clocksource to be watched. */
5d8b34fd 372 list_add(&cs->wd_list, &watchdog_list);
fb63a0eb 373 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
948ac6d7 374 } else {
fb63a0eb 375 /* cs is a watchdog. */
948ac6d7 376 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
5d8b34fd 377 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
fb63a0eb 378 /* Pick the best watchdog. */
5d8b34fd 379 if (!watchdog || cs->rating > watchdog->rating) {
5d8b34fd 380 watchdog = cs;
5d8b34fd 381 /* Reset watchdog cycles */
0f8e8ef7 382 clocksource_reset_watchdog();
5d8b34fd
TG
383 }
384 }
fb63a0eb
MS
385 /* Check if the watchdog timer needs to be started. */
386 clocksource_start_watchdog();
5d8b34fd
TG
387 spin_unlock_irqrestore(&watchdog_lock, flags);
388}
fb63a0eb
MS
389
390static void clocksource_dequeue_watchdog(struct clocksource *cs)
391{
fb63a0eb
MS
392 unsigned long flags;
393
394 spin_lock_irqsave(&watchdog_lock, flags);
a89c7edb
TG
395 if (cs != watchdog) {
396 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
397 /* cs is a watched clocksource. */
398 list_del_init(&cs->wd_list);
399 /* Check if the watchdog timer needs to be stopped. */
400 clocksource_stop_watchdog();
fb63a0eb
MS
401 }
402 }
fb63a0eb
MS
403 spin_unlock_irqrestore(&watchdog_lock, flags);
404}
405
01548f4d 406static int clocksource_watchdog_kthread(void *data)
c55c87c8
MS
407{
408 struct clocksource *cs, *tmp;
409 unsigned long flags;
6ea41d25 410 LIST_HEAD(unstable);
c55c87c8 411
d0981a1b 412 mutex_lock(&clocksource_mutex);
c55c87c8
MS
413 spin_lock_irqsave(&watchdog_lock, flags);
414 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
415 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
416 list_del_init(&cs->wd_list);
6ea41d25 417 list_add(&cs->wd_list, &unstable);
c55c87c8
MS
418 }
419 /* Check if the watchdog timer needs to be stopped. */
420 clocksource_stop_watchdog();
6ea41d25
TG
421 spin_unlock_irqrestore(&watchdog_lock, flags);
422
423 /* Needs to be done outside of watchdog lock */
424 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
425 list_del_init(&cs->wd_list);
d0981a1b 426 __clocksource_change_rating(cs, 0);
6ea41d25 427 }
d0981a1b 428 mutex_unlock(&clocksource_mutex);
01548f4d 429 return 0;
c55c87c8
MS
430}
431
7eaeb343
TG
432static bool clocksource_is_watchdog(struct clocksource *cs)
433{
434 return cs == watchdog;
435}
436
fb63a0eb
MS
437#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
438
439static void clocksource_enqueue_watchdog(struct clocksource *cs)
5d8b34fd
TG
440{
441 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
442 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
443}
b52f52a0 444
fb63a0eb 445static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
b52f52a0 446static inline void clocksource_resume_watchdog(void) { }
54a6bc0b 447static inline int clocksource_watchdog_kthread(void *data) { return 0; }
7eaeb343 448static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
fb63a0eb
MS
449
450#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
5d8b34fd 451
c54a42b1
MD
452/**
453 * clocksource_suspend - suspend the clocksource(s)
454 */
455void clocksource_suspend(void)
456{
457 struct clocksource *cs;
458
459 list_for_each_entry_reverse(cs, &clocksource_list, list)
460 if (cs->suspend)
461 cs->suspend(cs);
462}
463
b52f52a0
TG
464/**
465 * clocksource_resume - resume the clocksource(s)
466 */
467void clocksource_resume(void)
468{
2e197586 469 struct clocksource *cs;
b52f52a0 470
75c5158f 471 list_for_each_entry(cs, &clocksource_list, list)
b52f52a0 472 if (cs->resume)
17622339 473 cs->resume(cs);
b52f52a0
TG
474
475 clocksource_resume_watchdog();
b52f52a0
TG
476}
477
7c3078b6
JW
478/**
479 * clocksource_touch_watchdog - Update watchdog
480 *
481 * Update the watchdog after exception contexts such as kgdb so as not
7b7422a5
TG
482 * to incorrectly trip the watchdog. This might fail when the kernel
483 * was stopped in code which holds watchdog_lock.
7c3078b6
JW
484 */
485void clocksource_touch_watchdog(void)
486{
487 clocksource_resume_watchdog();
488}
489
d65670a7
JS
490/**
491 * clocksource_max_adjustment- Returns max adjustment amount
492 * @cs: Pointer to clocksource
493 *
494 */
495static u32 clocksource_max_adjustment(struct clocksource *cs)
496{
497 u64 ret;
498 /*
88b28adf 499 * We won't try to correct for more than 11% adjustments (110,000 ppm),
d65670a7
JS
500 */
501 ret = (u64)cs->mult * 11;
502 do_div(ret,100);
503 return (u32)ret;
504}
505
98962465
JH
506/**
507 * clocksource_max_deferment - Returns max time the clocksource can be deferred
508 * @cs: Pointer to clocksource
509 *
510 */
511static u64 clocksource_max_deferment(struct clocksource *cs)
512{
513 u64 max_nsecs, max_cycles;
514
515 /*
516 * Calculate the maximum number of cycles that we can pass to the
517 * cyc2ns function without overflowing a 64-bit signed result. The
d65670a7
JS
518 * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
519 * which is equivalent to the below.
520 * max_cycles < (2^63)/(cs->mult + cs->maxadj)
521 * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
522 * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
523 * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
524 * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
98962465
JH
525 * Please note that we add 1 to the result of the log2 to account for
526 * any rounding errors, ensure the above inequality is satisfied and
527 * no overflow will occur.
528 */
d65670a7 529 max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
98962465
JH
530
531 /*
532 * The actual maximum number of cycles we can defer the clocksource is
533 * determined by the minimum of max_cycles and cs->mask.
d65670a7
JS
534 * Note: Here we subtract the maxadj to make sure we don't sleep for
535 * too long if there's a large negative adjustment.
98962465
JH
536 */
537 max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
d65670a7
JS
538 max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
539 cs->shift);
98962465
JH
540
541 /*
542 * To ensure that the clocksource does not wrap whilst we are idle,
543 * limit the time the clocksource can be deferred by 12.5%. Please
544 * note a margin of 12.5% is used because this can be computed with
545 * a shift, versus say 10% which would require division.
546 */
b1f91966 547 return max_nsecs - (max_nsecs >> 3);
98962465
JH
548}
549
592913ec 550#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
734efb46 551
f5a2e343 552static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
5d33b883
TG
553{
554 struct clocksource *cs;
555
556 if (!finished_booting || list_empty(&clocksource_list))
557 return NULL;
558
559 /*
560 * We pick the clocksource with the highest rating. If oneshot
561 * mode is active, we pick the highres valid clocksource with
562 * the best rating.
563 */
564 list_for_each_entry(cs, &clocksource_list, list) {
f5a2e343
TG
565 if (skipcur && cs == curr_clocksource)
566 continue;
5d33b883
TG
567 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
568 continue;
569 return cs;
570 }
571 return NULL;
572}
573
f5a2e343 574static void __clocksource_select(bool skipcur)
734efb46 575{
5d33b883 576 bool oneshot = tick_oneshot_mode_active();
f1b82746 577 struct clocksource *best, *cs;
5d8b34fd 578
5d33b883 579 /* Find the best suitable clocksource */
f5a2e343 580 best = clocksource_find_best(oneshot, skipcur);
5d33b883 581 if (!best)
f1b82746 582 return;
5d33b883 583
f1b82746
MS
584 /* Check for the override clocksource. */
585 list_for_each_entry(cs, &clocksource_list, list) {
f5a2e343
TG
586 if (skipcur && cs == curr_clocksource)
587 continue;
f1b82746
MS
588 if (strcmp(cs->name, override_name) != 0)
589 continue;
590 /*
591 * Check to make sure we don't switch to a non-highres
592 * capable clocksource if the tick code is in oneshot
593 * mode (highres or nohz)
594 */
5d33b883 595 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
f1b82746
MS
596 /* Override clocksource cannot be used. */
597 printk(KERN_WARNING "Override clocksource %s is not "
598 "HRT compatible. Cannot switch while in "
599 "HRT/NOHZ mode\n", cs->name);
600 override_name[0] = 0;
601 } else
602 /* Override clocksource can be used. */
603 best = cs;
604 break;
605 }
ba919d1c
TG
606
607 if (curr_clocksource != best && !timekeeping_notify(best)) {
608 pr_info("Switched to clocksource %s\n", best->name);
75c5158f 609 curr_clocksource = best;
75c5158f 610 }
f1b82746 611}
734efb46 612
f5a2e343
TG
613/**
614 * clocksource_select - Select the best clocksource available
615 *
616 * Private function. Must hold clocksource_mutex when called.
617 *
618 * Select the clocksource with the best rating, or the clocksource,
619 * which is selected by userspace override.
620 */
621static void clocksource_select(void)
622{
623 return __clocksource_select(false);
624}
625
7eaeb343
TG
626static void clocksource_select_fallback(void)
627{
628 return __clocksource_select(true);
629}
630
592913ec 631#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
54a6bc0b
TG
632
633static inline void clocksource_select(void) { }
634
635#endif
636
75c5158f
MS
637/*
638 * clocksource_done_booting - Called near the end of core bootup
639 *
640 * Hack to avoid lots of clocksource churn at boot time.
641 * We use fs_initcall because we want this to start before
642 * device_initcall but after subsys_initcall.
643 */
644static int __init clocksource_done_booting(void)
645{
ad6759fb
JS
646 mutex_lock(&clocksource_mutex);
647 curr_clocksource = clocksource_default_clock();
648 mutex_unlock(&clocksource_mutex);
649
75c5158f 650 finished_booting = 1;
54a6bc0b
TG
651
652 /*
653 * Run the watchdog first to eliminate unstable clock sources
654 */
655 clocksource_watchdog_kthread(NULL);
656
e6c73305 657 mutex_lock(&clocksource_mutex);
75c5158f 658 clocksource_select();
e6c73305 659 mutex_unlock(&clocksource_mutex);
75c5158f
MS
660 return 0;
661}
662fs_initcall(clocksource_done_booting);
663
92c7e002
TG
664/*
665 * Enqueue the clocksource sorted by rating
734efb46 666 */
f1b82746 667static void clocksource_enqueue(struct clocksource *cs)
734efb46 668{
f1b82746
MS
669 struct list_head *entry = &clocksource_list;
670 struct clocksource *tmp;
92c7e002 671
f1b82746 672 list_for_each_entry(tmp, &clocksource_list, list)
92c7e002 673 /* Keep track of the place, where to insert */
f1b82746
MS
674 if (tmp->rating >= cs->rating)
675 entry = &tmp->list;
676 list_add(&cs->list, entry);
734efb46
JS
677}
678
d7e81c26 679/**
852db46d 680 * __clocksource_updatefreq_scale - Used update clocksource with new freq
b1b73d09 681 * @cs: clocksource to be registered
d7e81c26
JS
682 * @scale: Scale factor multiplied against freq to get clocksource hz
683 * @freq: clocksource frequency (cycles per second) divided by scale
684 *
852db46d 685 * This should only be called from the clocksource->enable() method.
d7e81c26
JS
686 *
687 * This *SHOULD NOT* be called directly! Please use the
852db46d 688 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
d7e81c26 689 */
852db46d 690void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
d7e81c26 691{
c0e299b1 692 u64 sec;
d7e81c26 693 /*
724ed53e
TG
694 * Calc the maximum number of seconds which we can run before
695 * wrapping around. For clocksources which have a mask > 32bit
696 * we need to limit the max sleep time to have a good
697 * conversion precision. 10 minutes is still a reasonable
698 * amount. That results in a shift value of 24 for a
699 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
700 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
701 * margin as we do in clocksource_max_deferment()
d7e81c26 702 */
b1f91966 703 sec = (cs->mask - (cs->mask >> 3));
724ed53e
TG
704 do_div(sec, freq);
705 do_div(sec, scale);
706 if (!sec)
707 sec = 1;
708 else if (sec > 600 && cs->mask > UINT_MAX)
709 sec = 600;
710
d7e81c26 711 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
724ed53e 712 NSEC_PER_SEC / scale, sec * scale);
d65670a7
JS
713
714 /*
715 * for clocksources that have large mults, to avoid overflow.
716 * Since mult may be adjusted by ntp, add an safety extra margin
717 *
718 */
719 cs->maxadj = clocksource_max_adjustment(cs);
720 while ((cs->mult + cs->maxadj < cs->mult)
721 || (cs->mult - cs->maxadj > cs->mult)) {
722 cs->mult >>= 1;
723 cs->shift--;
724 cs->maxadj = clocksource_max_adjustment(cs);
725 }
726
d7e81c26 727 cs->max_idle_ns = clocksource_max_deferment(cs);
852db46d
JS
728}
729EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
730
731/**
732 * __clocksource_register_scale - Used to install new clocksources
b1b73d09 733 * @cs: clocksource to be registered
852db46d
JS
734 * @scale: Scale factor multiplied against freq to get clocksource hz
735 * @freq: clocksource frequency (cycles per second) divided by scale
736 *
737 * Returns -EBUSY if registration fails, zero otherwise.
738 *
739 * This *SHOULD NOT* be called directly! Please use the
740 * clocksource_register_hz() or clocksource_register_khz helper functions.
741 */
742int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
743{
744
b595076a 745 /* Initialize mult/shift and max_idle_ns */
852db46d 746 __clocksource_updatefreq_scale(cs, scale, freq);
d7e81c26 747
852db46d 748 /* Add clocksource to the clcoksource list */
d7e81c26
JS
749 mutex_lock(&clocksource_mutex);
750 clocksource_enqueue(cs);
d7e81c26 751 clocksource_enqueue_watchdog(cs);
e05b2efb 752 clocksource_select();
d7e81c26
JS
753 mutex_unlock(&clocksource_mutex);
754 return 0;
755}
756EXPORT_SYMBOL_GPL(__clocksource_register_scale);
757
758
734efb46 759/**
a2752549 760 * clocksource_register - Used to install new clocksources
b1b73d09 761 * @cs: clocksource to be registered
734efb46
JS
762 *
763 * Returns -EBUSY if registration fails, zero otherwise.
764 */
f1b82746 765int clocksource_register(struct clocksource *cs)
734efb46 766{
d65670a7
JS
767 /* calculate max adjustment for given mult/shift */
768 cs->maxadj = clocksource_max_adjustment(cs);
769 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
770 "Clocksource %s might overflow on 11%% adjustment\n",
771 cs->name);
772
98962465
JH
773 /* calculate max idle time permitted for this clocksource */
774 cs->max_idle_ns = clocksource_max_deferment(cs);
775
75c5158f 776 mutex_lock(&clocksource_mutex);
f1b82746 777 clocksource_enqueue(cs);
fb63a0eb 778 clocksource_enqueue_watchdog(cs);
e05b2efb 779 clocksource_select();
75c5158f 780 mutex_unlock(&clocksource_mutex);
f1b82746 781 return 0;
734efb46 782}
a2752549 783EXPORT_SYMBOL(clocksource_register);
734efb46 784
d0981a1b
TG
785static void __clocksource_change_rating(struct clocksource *cs, int rating)
786{
787 list_del(&cs->list);
788 cs->rating = rating;
789 clocksource_enqueue(cs);
790 clocksource_select();
791}
792
734efb46 793/**
92c7e002 794 * clocksource_change_rating - Change the rating of a registered clocksource
b1b73d09
KK
795 * @cs: clocksource to be changed
796 * @rating: new rating
734efb46 797 */
92c7e002 798void clocksource_change_rating(struct clocksource *cs, int rating)
734efb46 799{
75c5158f 800 mutex_lock(&clocksource_mutex);
d0981a1b 801 __clocksource_change_rating(cs, rating);
75c5158f 802 mutex_unlock(&clocksource_mutex);
734efb46 803}
fb63a0eb 804EXPORT_SYMBOL(clocksource_change_rating);
734efb46 805
7eaeb343
TG
806/*
807 * Unbind clocksource @cs. Called with clocksource_mutex held
808 */
809static int clocksource_unbind(struct clocksource *cs)
810{
811 /*
812 * I really can't convince myself to support this on hardware
813 * designed by lobotomized monkeys.
814 */
815 if (clocksource_is_watchdog(cs))
816 return -EBUSY;
817
818 if (cs == curr_clocksource) {
819 /* Select and try to install a replacement clock source */
820 clocksource_select_fallback();
821 if (curr_clocksource == cs)
822 return -EBUSY;
823 }
824 clocksource_dequeue_watchdog(cs);
825 list_del_init(&cs->list);
826 return 0;
827}
828
4713e22c
TG
829/**
830 * clocksource_unregister - remove a registered clocksource
b1b73d09 831 * @cs: clocksource to be unregistered
4713e22c 832 */
a89c7edb 833int clocksource_unregister(struct clocksource *cs)
4713e22c 834{
a89c7edb
TG
835 int ret = 0;
836
75c5158f 837 mutex_lock(&clocksource_mutex);
a89c7edb
TG
838 if (!list_empty(&cs->list))
839 ret = clocksource_unbind(cs);
75c5158f 840 mutex_unlock(&clocksource_mutex);
a89c7edb 841 return ret;
4713e22c 842}
fb63a0eb 843EXPORT_SYMBOL(clocksource_unregister);
4713e22c 844
2b013700 845#ifdef CONFIG_SYSFS
734efb46
JS
846/**
847 * sysfs_show_current_clocksources - sysfs interface for current clocksource
848 * @dev: unused
b1b73d09 849 * @attr: unused
734efb46
JS
850 * @buf: char buffer to be filled with clocksource list
851 *
852 * Provides sysfs interface for listing current clocksource.
853 */
854static ssize_t
d369a5d8
KS
855sysfs_show_current_clocksources(struct device *dev,
856 struct device_attribute *attr, char *buf)
734efb46 857{
5e2cb101 858 ssize_t count = 0;
734efb46 859
75c5158f 860 mutex_lock(&clocksource_mutex);
5e2cb101 861 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
75c5158f 862 mutex_unlock(&clocksource_mutex);
734efb46 863
5e2cb101 864 return count;
734efb46
JS
865}
866
29b54078
TG
867static size_t clocksource_get_uname(const char *buf, char *dst, size_t cnt)
868{
869 size_t ret = cnt;
870
871 /* strings from sysfs write are not 0 terminated! */
872 if (!cnt || cnt >= CS_NAME_LEN)
873 return -EINVAL;
874
875 /* strip of \n: */
876 if (buf[cnt-1] == '\n')
877 cnt--;
878 if (cnt > 0)
879 memcpy(dst, buf, cnt);
880 dst[cnt] = 0;
881 return ret;
882}
883
734efb46
JS
884/**
885 * sysfs_override_clocksource - interface for manually overriding clocksource
886 * @dev: unused
b1b73d09 887 * @attr: unused
734efb46
JS
888 * @buf: name of override clocksource
889 * @count: length of buffer
890 *
891 * Takes input from sysfs interface for manually overriding the default
b71a8eb0 892 * clocksource selection.
734efb46 893 */
d369a5d8
KS
894static ssize_t sysfs_override_clocksource(struct device *dev,
895 struct device_attribute *attr,
734efb46
JS
896 const char *buf, size_t count)
897{
29b54078 898 size_t ret;
734efb46 899
75c5158f 900 mutex_lock(&clocksource_mutex);
734efb46 901
29b54078
TG
902 ret = clocksource_get_uname(buf, override_name, count);
903 if (ret >= 0)
904 clocksource_select();
734efb46 905
75c5158f 906 mutex_unlock(&clocksource_mutex);
734efb46
JS
907
908 return ret;
909}
910
7eaeb343
TG
911/**
912 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
913 * @dev: unused
914 * @attr: unused
915 * @buf: unused
916 * @count: length of buffer
917 *
918 * Takes input from sysfs interface for manually unbinding a clocksource.
919 */
920static ssize_t sysfs_unbind_clocksource(struct device *dev,
921 struct device_attribute *attr,
922 const char *buf, size_t count)
923{
924 struct clocksource *cs;
925 char name[CS_NAME_LEN];
926 size_t ret;
927
928 ret = clocksource_get_uname(buf, name, count);
929 if (ret < 0)
930 return ret;
931
932 ret = -ENODEV;
933 mutex_lock(&clocksource_mutex);
934 list_for_each_entry(cs, &clocksource_list, list) {
935 if (strcmp(cs->name, name))
936 continue;
937 ret = clocksource_unbind(cs);
938 break;
939 }
940 mutex_unlock(&clocksource_mutex);
941
942 return ret ? ret : count;
943}
944
734efb46
JS
945/**
946 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
947 * @dev: unused
b1b73d09 948 * @attr: unused
734efb46
JS
949 * @buf: char buffer to be filled with clocksource list
950 *
951 * Provides sysfs interface for listing registered clocksources
952 */
953static ssize_t
d369a5d8
KS
954sysfs_show_available_clocksources(struct device *dev,
955 struct device_attribute *attr,
4a0b2b4d 956 char *buf)
734efb46 957{
2e197586 958 struct clocksource *src;
5e2cb101 959 ssize_t count = 0;
734efb46 960
75c5158f 961 mutex_lock(&clocksource_mutex);
2e197586 962 list_for_each_entry(src, &clocksource_list, list) {
cd6d95d8
TG
963 /*
964 * Don't show non-HRES clocksource if the tick code is
965 * in one shot mode (highres=on or nohz=on)
966 */
967 if (!tick_oneshot_mode_active() ||
968 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
3f68535a 969 count += snprintf(buf + count,
5e2cb101
MX
970 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
971 "%s ", src->name);
734efb46 972 }
75c5158f 973 mutex_unlock(&clocksource_mutex);
734efb46 974
5e2cb101
MX
975 count += snprintf(buf + count,
976 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
734efb46 977
5e2cb101 978 return count;
734efb46
JS
979}
980
981/*
982 * Sysfs setup bits:
983 */
d369a5d8 984static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
f5f1a24a 985 sysfs_override_clocksource);
734efb46 986
7eaeb343
TG
987static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
988
d369a5d8 989static DEVICE_ATTR(available_clocksource, 0444,
f5f1a24a 990 sysfs_show_available_clocksources, NULL);
734efb46 991
d369a5d8 992static struct bus_type clocksource_subsys = {
af5ca3f4 993 .name = "clocksource",
d369a5d8 994 .dev_name = "clocksource",
734efb46
JS
995};
996
d369a5d8 997static struct device device_clocksource = {
734efb46 998 .id = 0,
d369a5d8 999 .bus = &clocksource_subsys,
734efb46
JS
1000};
1001
ad596171 1002static int __init init_clocksource_sysfs(void)
734efb46 1003{
d369a5d8 1004 int error = subsys_system_register(&clocksource_subsys, NULL);
734efb46
JS
1005
1006 if (!error)
d369a5d8 1007 error = device_register(&device_clocksource);
734efb46 1008 if (!error)
d369a5d8 1009 error = device_create_file(
734efb46 1010 &device_clocksource,
d369a5d8 1011 &dev_attr_current_clocksource);
7eaeb343
TG
1012 if (!error)
1013 error = device_create_file(&device_clocksource,
1014 &dev_attr_unbind_clocksource);
734efb46 1015 if (!error)
d369a5d8 1016 error = device_create_file(
734efb46 1017 &device_clocksource,
d369a5d8 1018 &dev_attr_available_clocksource);
734efb46
JS
1019 return error;
1020}
1021
1022device_initcall(init_clocksource_sysfs);
2b013700 1023#endif /* CONFIG_SYSFS */
734efb46
JS
1024
1025/**
1026 * boot_override_clocksource - boot clock override
1027 * @str: override name
1028 *
1029 * Takes a clocksource= boot argument and uses it
1030 * as the clocksource override name.
1031 */
1032static int __init boot_override_clocksource(char* str)
1033{
75c5158f 1034 mutex_lock(&clocksource_mutex);
734efb46
JS
1035 if (str)
1036 strlcpy(override_name, str, sizeof(override_name));
75c5158f 1037 mutex_unlock(&clocksource_mutex);
734efb46
JS
1038 return 1;
1039}
1040
1041__setup("clocksource=", boot_override_clocksource);
1042
1043/**
1044 * boot_override_clock - Compatibility layer for deprecated boot option
1045 * @str: override name
1046 *
1047 * DEPRECATED! Takes a clock= boot argument and uses it
1048 * as the clocksource override name
1049 */
1050static int __init boot_override_clock(char* str)
1051{
5d0cf410
JS
1052 if (!strcmp(str, "pmtmr")) {
1053 printk("Warning: clock=pmtmr is deprecated. "
1054 "Use clocksource=acpi_pm.\n");
1055 return boot_override_clocksource("acpi_pm");
1056 }
1057 printk("Warning! clock= boot option is deprecated. "
1058 "Use clocksource=xyz\n");
734efb46
JS
1059 return boot_override_clocksource(str);
1060}
1061
1062__setup("clock=", boot_override_clock);