clocksource: Provide unbind interface in sysfs
[deliverable/linux.git] / kernel / time / clocksource.c
CommitLineData
734efb46 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 24 */
25
d369a5d8 26#include <linux/device.h>
734efb46 27#include <linux/clocksource.h>
734efb46 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 164/*[Clocksource internal variables]---------
165 * curr_clocksource:
f1b82746 166 * currently selected clocksource.
734efb46 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 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{
392 struct clocksource *tmp;
393 unsigned long flags;
394
395 spin_lock_irqsave(&watchdog_lock, flags);
396 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
397 /* cs is a watched clocksource. */
398 list_del_init(&cs->wd_list);
399 } else if (cs == watchdog) {
400 /* Reset watchdog cycles */
401 clocksource_reset_watchdog();
402 /* Current watchdog is removed. Find an alternative. */
403 watchdog = NULL;
404 list_for_each_entry(tmp, &clocksource_list, list) {
405 if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
406 continue;
407 if (!watchdog || tmp->rating > watchdog->rating)
408 watchdog = tmp;
409 }
410 }
411 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
412 /* Check if the watchdog timer needs to be stopped. */
413 clocksource_stop_watchdog();
414 spin_unlock_irqrestore(&watchdog_lock, flags);
415}
416
01548f4d 417static int clocksource_watchdog_kthread(void *data)
c55c87c8
MS
418{
419 struct clocksource *cs, *tmp;
420 unsigned long flags;
6ea41d25 421 LIST_HEAD(unstable);
c55c87c8 422
d0981a1b 423 mutex_lock(&clocksource_mutex);
c55c87c8
MS
424 spin_lock_irqsave(&watchdog_lock, flags);
425 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
426 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
427 list_del_init(&cs->wd_list);
6ea41d25 428 list_add(&cs->wd_list, &unstable);
c55c87c8
MS
429 }
430 /* Check if the watchdog timer needs to be stopped. */
431 clocksource_stop_watchdog();
6ea41d25
TG
432 spin_unlock_irqrestore(&watchdog_lock, flags);
433
434 /* Needs to be done outside of watchdog lock */
435 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
436 list_del_init(&cs->wd_list);
d0981a1b 437 __clocksource_change_rating(cs, 0);
6ea41d25 438 }
d0981a1b 439 mutex_unlock(&clocksource_mutex);
01548f4d 440 return 0;
c55c87c8
MS
441}
442
7eaeb343
TG
443static bool clocksource_is_watchdog(struct clocksource *cs)
444{
445 return cs == watchdog;
446}
447
fb63a0eb
MS
448#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
449
450static void clocksource_enqueue_watchdog(struct clocksource *cs)
5d8b34fd
TG
451{
452 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
453 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
454}
b52f52a0 455
fb63a0eb 456static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
b52f52a0 457static inline void clocksource_resume_watchdog(void) { }
54a6bc0b 458static inline int clocksource_watchdog_kthread(void *data) { return 0; }
7eaeb343 459static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
fb63a0eb
MS
460
461#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
5d8b34fd 462
c54a42b1
MD
463/**
464 * clocksource_suspend - suspend the clocksource(s)
465 */
466void clocksource_suspend(void)
467{
468 struct clocksource *cs;
469
470 list_for_each_entry_reverse(cs, &clocksource_list, list)
471 if (cs->suspend)
472 cs->suspend(cs);
473}
474
b52f52a0
TG
475/**
476 * clocksource_resume - resume the clocksource(s)
477 */
478void clocksource_resume(void)
479{
2e197586 480 struct clocksource *cs;
b52f52a0 481
75c5158f 482 list_for_each_entry(cs, &clocksource_list, list)
b52f52a0 483 if (cs->resume)
17622339 484 cs->resume(cs);
b52f52a0
TG
485
486 clocksource_resume_watchdog();
b52f52a0
TG
487}
488
7c3078b6
JW
489/**
490 * clocksource_touch_watchdog - Update watchdog
491 *
492 * Update the watchdog after exception contexts such as kgdb so as not
7b7422a5
TG
493 * to incorrectly trip the watchdog. This might fail when the kernel
494 * was stopped in code which holds watchdog_lock.
7c3078b6
JW
495 */
496void clocksource_touch_watchdog(void)
497{
498 clocksource_resume_watchdog();
499}
500
d65670a7
JS
501/**
502 * clocksource_max_adjustment- Returns max adjustment amount
503 * @cs: Pointer to clocksource
504 *
505 */
506static u32 clocksource_max_adjustment(struct clocksource *cs)
507{
508 u64 ret;
509 /*
88b28adf 510 * We won't try to correct for more than 11% adjustments (110,000 ppm),
d65670a7
JS
511 */
512 ret = (u64)cs->mult * 11;
513 do_div(ret,100);
514 return (u32)ret;
515}
516
98962465
JH
517/**
518 * clocksource_max_deferment - Returns max time the clocksource can be deferred
519 * @cs: Pointer to clocksource
520 *
521 */
522static u64 clocksource_max_deferment(struct clocksource *cs)
523{
524 u64 max_nsecs, max_cycles;
525
526 /*
527 * Calculate the maximum number of cycles that we can pass to the
528 * cyc2ns function without overflowing a 64-bit signed result. The
d65670a7
JS
529 * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
530 * which is equivalent to the below.
531 * max_cycles < (2^63)/(cs->mult + cs->maxadj)
532 * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
533 * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
534 * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
535 * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
98962465
JH
536 * Please note that we add 1 to the result of the log2 to account for
537 * any rounding errors, ensure the above inequality is satisfied and
538 * no overflow will occur.
539 */
d65670a7 540 max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
98962465
JH
541
542 /*
543 * The actual maximum number of cycles we can defer the clocksource is
544 * determined by the minimum of max_cycles and cs->mask.
d65670a7
JS
545 * Note: Here we subtract the maxadj to make sure we don't sleep for
546 * too long if there's a large negative adjustment.
98962465
JH
547 */
548 max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
d65670a7
JS
549 max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
550 cs->shift);
98962465
JH
551
552 /*
553 * To ensure that the clocksource does not wrap whilst we are idle,
554 * limit the time the clocksource can be deferred by 12.5%. Please
555 * note a margin of 12.5% is used because this can be computed with
556 * a shift, versus say 10% which would require division.
557 */
b1f91966 558 return max_nsecs - (max_nsecs >> 3);
98962465
JH
559}
560
592913ec 561#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
734efb46 562
f5a2e343 563static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
5d33b883
TG
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) {
f5a2e343
TG
576 if (skipcur && cs == curr_clocksource)
577 continue;
5d33b883
TG
578 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
579 continue;
580 return cs;
581 }
582 return NULL;
583}
584
f5a2e343 585static void __clocksource_select(bool skipcur)
734efb46 586{
5d33b883 587 bool oneshot = tick_oneshot_mode_active();
f1b82746 588 struct clocksource *best, *cs;
5d8b34fd 589
5d33b883 590 /* Find the best suitable clocksource */
f5a2e343 591 best = clocksource_find_best(oneshot, skipcur);
5d33b883 592 if (!best)
f1b82746 593 return;
5d33b883 594
f1b82746
MS
595 /* Check for the override clocksource. */
596 list_for_each_entry(cs, &clocksource_list, list) {
f5a2e343
TG
597 if (skipcur && cs == curr_clocksource)
598 continue;
f1b82746
MS
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 */
5d33b883 606 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
f1b82746
MS
607 /* Override clocksource cannot be used. */
608 printk(KERN_WARNING "Override clocksource %s is not "
609 "HRT compatible. Cannot switch while in "
610 "HRT/NOHZ mode\n", cs->name);
611 override_name[0] = 0;
612 } else
613 /* Override clocksource can be used. */
614 best = cs;
615 break;
616 }
ba919d1c
TG
617
618 if (curr_clocksource != best && !timekeeping_notify(best)) {
619 pr_info("Switched to clocksource %s\n", best->name);
75c5158f 620 curr_clocksource = best;
75c5158f 621 }
f1b82746 622}
734efb46 623
f5a2e343
TG
624/**
625 * clocksource_select - Select the best clocksource available
626 *
627 * Private function. Must hold clocksource_mutex when called.
628 *
629 * Select the clocksource with the best rating, or the clocksource,
630 * which is selected by userspace override.
631 */
632static void clocksource_select(void)
633{
634 return __clocksource_select(false);
635}
636
7eaeb343
TG
637static void clocksource_select_fallback(void)
638{
639 return __clocksource_select(true);
640}
641
592913ec 642#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
54a6bc0b
TG
643
644static inline void clocksource_select(void) { }
645
646#endif
647
75c5158f
MS
648/*
649 * clocksource_done_booting - Called near the end of core bootup
650 *
651 * Hack to avoid lots of clocksource churn at boot time.
652 * We use fs_initcall because we want this to start before
653 * device_initcall but after subsys_initcall.
654 */
655static int __init clocksource_done_booting(void)
656{
ad6759fb 657 mutex_lock(&clocksource_mutex);
658 curr_clocksource = clocksource_default_clock();
659 mutex_unlock(&clocksource_mutex);
660
75c5158f 661 finished_booting = 1;
54a6bc0b
TG
662
663 /*
664 * Run the watchdog first to eliminate unstable clock sources
665 */
666 clocksource_watchdog_kthread(NULL);
667
e6c73305 668 mutex_lock(&clocksource_mutex);
75c5158f 669 clocksource_select();
e6c73305 670 mutex_unlock(&clocksource_mutex);
75c5158f
MS
671 return 0;
672}
673fs_initcall(clocksource_done_booting);
674
92c7e002
TG
675/*
676 * Enqueue the clocksource sorted by rating
734efb46 677 */
f1b82746 678static void clocksource_enqueue(struct clocksource *cs)
734efb46 679{
f1b82746
MS
680 struct list_head *entry = &clocksource_list;
681 struct clocksource *tmp;
92c7e002 682
f1b82746 683 list_for_each_entry(tmp, &clocksource_list, list)
92c7e002 684 /* Keep track of the place, where to insert */
f1b82746
MS
685 if (tmp->rating >= cs->rating)
686 entry = &tmp->list;
687 list_add(&cs->list, entry);
734efb46 688}
689
d7e81c26 690/**
852db46d 691 * __clocksource_updatefreq_scale - Used update clocksource with new freq
b1b73d09 692 * @cs: clocksource to be registered
d7e81c26
JS
693 * @scale: Scale factor multiplied against freq to get clocksource hz
694 * @freq: clocksource frequency (cycles per second) divided by scale
695 *
852db46d 696 * This should only be called from the clocksource->enable() method.
d7e81c26
JS
697 *
698 * This *SHOULD NOT* be called directly! Please use the
852db46d 699 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
d7e81c26 700 */
852db46d 701void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
d7e81c26 702{
c0e299b1 703 u64 sec;
d7e81c26 704 /*
724ed53e
TG
705 * Calc the maximum number of seconds which we can run before
706 * wrapping around. For clocksources which have a mask > 32bit
707 * we need to limit the max sleep time to have a good
708 * conversion precision. 10 minutes is still a reasonable
709 * amount. That results in a shift value of 24 for a
710 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
711 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
712 * margin as we do in clocksource_max_deferment()
d7e81c26 713 */
b1f91966 714 sec = (cs->mask - (cs->mask >> 3));
724ed53e
TG
715 do_div(sec, freq);
716 do_div(sec, scale);
717 if (!sec)
718 sec = 1;
719 else if (sec > 600 && cs->mask > UINT_MAX)
720 sec = 600;
721
d7e81c26 722 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
724ed53e 723 NSEC_PER_SEC / scale, sec * scale);
d65670a7
JS
724
725 /*
726 * for clocksources that have large mults, to avoid overflow.
727 * Since mult may be adjusted by ntp, add an safety extra margin
728 *
729 */
730 cs->maxadj = clocksource_max_adjustment(cs);
731 while ((cs->mult + cs->maxadj < cs->mult)
732 || (cs->mult - cs->maxadj > cs->mult)) {
733 cs->mult >>= 1;
734 cs->shift--;
735 cs->maxadj = clocksource_max_adjustment(cs);
736 }
737
d7e81c26 738 cs->max_idle_ns = clocksource_max_deferment(cs);
852db46d
JS
739}
740EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
741
742/**
743 * __clocksource_register_scale - Used to install new clocksources
b1b73d09 744 * @cs: clocksource to be registered
852db46d
JS
745 * @scale: Scale factor multiplied against freq to get clocksource hz
746 * @freq: clocksource frequency (cycles per second) divided by scale
747 *
748 * Returns -EBUSY if registration fails, zero otherwise.
749 *
750 * This *SHOULD NOT* be called directly! Please use the
751 * clocksource_register_hz() or clocksource_register_khz helper functions.
752 */
753int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
754{
755
b595076a 756 /* Initialize mult/shift and max_idle_ns */
852db46d 757 __clocksource_updatefreq_scale(cs, scale, freq);
d7e81c26 758
852db46d 759 /* Add clocksource to the clcoksource list */
d7e81c26
JS
760 mutex_lock(&clocksource_mutex);
761 clocksource_enqueue(cs);
d7e81c26 762 clocksource_enqueue_watchdog(cs);
e05b2efb 763 clocksource_select();
d7e81c26
JS
764 mutex_unlock(&clocksource_mutex);
765 return 0;
766}
767EXPORT_SYMBOL_GPL(__clocksource_register_scale);
768
769
734efb46 770/**
a2752549 771 * clocksource_register - Used to install new clocksources
b1b73d09 772 * @cs: clocksource to be registered
734efb46 773 *
774 * Returns -EBUSY if registration fails, zero otherwise.
775 */
f1b82746 776int clocksource_register(struct clocksource *cs)
734efb46 777{
d65670a7
JS
778 /* calculate max adjustment for given mult/shift */
779 cs->maxadj = clocksource_max_adjustment(cs);
780 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
781 "Clocksource %s might overflow on 11%% adjustment\n",
782 cs->name);
783
98962465
JH
784 /* calculate max idle time permitted for this clocksource */
785 cs->max_idle_ns = clocksource_max_deferment(cs);
786
75c5158f 787 mutex_lock(&clocksource_mutex);
f1b82746 788 clocksource_enqueue(cs);
fb63a0eb 789 clocksource_enqueue_watchdog(cs);
e05b2efb 790 clocksource_select();
75c5158f 791 mutex_unlock(&clocksource_mutex);
f1b82746 792 return 0;
734efb46 793}
a2752549 794EXPORT_SYMBOL(clocksource_register);
734efb46 795
d0981a1b
TG
796static void __clocksource_change_rating(struct clocksource *cs, int rating)
797{
798 list_del(&cs->list);
799 cs->rating = rating;
800 clocksource_enqueue(cs);
801 clocksource_select();
802}
803
734efb46 804/**
92c7e002 805 * clocksource_change_rating - Change the rating of a registered clocksource
b1b73d09
KK
806 * @cs: clocksource to be changed
807 * @rating: new rating
734efb46 808 */
92c7e002 809void clocksource_change_rating(struct clocksource *cs, int rating)
734efb46 810{
75c5158f 811 mutex_lock(&clocksource_mutex);
d0981a1b 812 __clocksource_change_rating(cs, rating);
75c5158f 813 mutex_unlock(&clocksource_mutex);
734efb46 814}
fb63a0eb 815EXPORT_SYMBOL(clocksource_change_rating);
734efb46 816
7eaeb343
TG
817/*
818 * Unbind clocksource @cs. Called with clocksource_mutex held
819 */
820static int clocksource_unbind(struct clocksource *cs)
821{
822 /*
823 * I really can't convince myself to support this on hardware
824 * designed by lobotomized monkeys.
825 */
826 if (clocksource_is_watchdog(cs))
827 return -EBUSY;
828
829 if (cs == curr_clocksource) {
830 /* Select and try to install a replacement clock source */
831 clocksource_select_fallback();
832 if (curr_clocksource == cs)
833 return -EBUSY;
834 }
835 clocksource_dequeue_watchdog(cs);
836 list_del_init(&cs->list);
837 return 0;
838}
839
4713e22c
TG
840/**
841 * clocksource_unregister - remove a registered clocksource
b1b73d09 842 * @cs: clocksource to be unregistered
4713e22c
TG
843 */
844void clocksource_unregister(struct clocksource *cs)
845{
75c5158f 846 mutex_lock(&clocksource_mutex);
fb63a0eb 847 clocksource_dequeue_watchdog(cs);
4713e22c 848 list_del(&cs->list);
f1b82746 849 clocksource_select();
75c5158f 850 mutex_unlock(&clocksource_mutex);
4713e22c 851}
fb63a0eb 852EXPORT_SYMBOL(clocksource_unregister);
4713e22c 853
2b013700 854#ifdef CONFIG_SYSFS
734efb46 855/**
856 * sysfs_show_current_clocksources - sysfs interface for current clocksource
857 * @dev: unused
b1b73d09 858 * @attr: unused
734efb46 859 * @buf: char buffer to be filled with clocksource list
860 *
861 * Provides sysfs interface for listing current clocksource.
862 */
863static ssize_t
d369a5d8
KS
864sysfs_show_current_clocksources(struct device *dev,
865 struct device_attribute *attr, char *buf)
734efb46 866{
5e2cb101 867 ssize_t count = 0;
734efb46 868
75c5158f 869 mutex_lock(&clocksource_mutex);
5e2cb101 870 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
75c5158f 871 mutex_unlock(&clocksource_mutex);
734efb46 872
5e2cb101 873 return count;
734efb46 874}
875
29b54078
TG
876static size_t clocksource_get_uname(const char *buf, char *dst, size_t cnt)
877{
878 size_t ret = cnt;
879
880 /* strings from sysfs write are not 0 terminated! */
881 if (!cnt || cnt >= CS_NAME_LEN)
882 return -EINVAL;
883
884 /* strip of \n: */
885 if (buf[cnt-1] == '\n')
886 cnt--;
887 if (cnt > 0)
888 memcpy(dst, buf, cnt);
889 dst[cnt] = 0;
890 return ret;
891}
892
734efb46 893/**
894 * sysfs_override_clocksource - interface for manually overriding clocksource
895 * @dev: unused
b1b73d09 896 * @attr: unused
734efb46 897 * @buf: name of override clocksource
898 * @count: length of buffer
899 *
900 * Takes input from sysfs interface for manually overriding the default
b71a8eb0 901 * clocksource selection.
734efb46 902 */
d369a5d8
KS
903static ssize_t sysfs_override_clocksource(struct device *dev,
904 struct device_attribute *attr,
734efb46 905 const char *buf, size_t count)
906{
29b54078 907 size_t ret;
734efb46 908
75c5158f 909 mutex_lock(&clocksource_mutex);
734efb46 910
29b54078
TG
911 ret = clocksource_get_uname(buf, override_name, count);
912 if (ret >= 0)
913 clocksource_select();
734efb46 914
75c5158f 915 mutex_unlock(&clocksource_mutex);
734efb46 916
917 return ret;
918}
919
7eaeb343
TG
920/**
921 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
922 * @dev: unused
923 * @attr: unused
924 * @buf: unused
925 * @count: length of buffer
926 *
927 * Takes input from sysfs interface for manually unbinding a clocksource.
928 */
929static ssize_t sysfs_unbind_clocksource(struct device *dev,
930 struct device_attribute *attr,
931 const char *buf, size_t count)
932{
933 struct clocksource *cs;
934 char name[CS_NAME_LEN];
935 size_t ret;
936
937 ret = clocksource_get_uname(buf, name, count);
938 if (ret < 0)
939 return ret;
940
941 ret = -ENODEV;
942 mutex_lock(&clocksource_mutex);
943 list_for_each_entry(cs, &clocksource_list, list) {
944 if (strcmp(cs->name, name))
945 continue;
946 ret = clocksource_unbind(cs);
947 break;
948 }
949 mutex_unlock(&clocksource_mutex);
950
951 return ret ? ret : count;
952}
953
734efb46 954/**
955 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
956 * @dev: unused
b1b73d09 957 * @attr: unused
734efb46 958 * @buf: char buffer to be filled with clocksource list
959 *
960 * Provides sysfs interface for listing registered clocksources
961 */
962static ssize_t
d369a5d8
KS
963sysfs_show_available_clocksources(struct device *dev,
964 struct device_attribute *attr,
4a0b2b4d 965 char *buf)
734efb46 966{
2e197586 967 struct clocksource *src;
5e2cb101 968 ssize_t count = 0;
734efb46 969
75c5158f 970 mutex_lock(&clocksource_mutex);
2e197586 971 list_for_each_entry(src, &clocksource_list, list) {
cd6d95d8
TG
972 /*
973 * Don't show non-HRES clocksource if the tick code is
974 * in one shot mode (highres=on or nohz=on)
975 */
976 if (!tick_oneshot_mode_active() ||
977 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
3f68535a 978 count += snprintf(buf + count,
5e2cb101
MX
979 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
980 "%s ", src->name);
734efb46 981 }
75c5158f 982 mutex_unlock(&clocksource_mutex);
734efb46 983
5e2cb101
MX
984 count += snprintf(buf + count,
985 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
734efb46 986
5e2cb101 987 return count;
734efb46 988}
989
990/*
991 * Sysfs setup bits:
992 */
d369a5d8 993static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
f5f1a24a 994 sysfs_override_clocksource);
734efb46 995
7eaeb343
TG
996static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
997
d369a5d8 998static DEVICE_ATTR(available_clocksource, 0444,
f5f1a24a 999 sysfs_show_available_clocksources, NULL);
734efb46 1000
d369a5d8 1001static struct bus_type clocksource_subsys = {
af5ca3f4 1002 .name = "clocksource",
d369a5d8 1003 .dev_name = "clocksource",
734efb46 1004};
1005
d369a5d8 1006static struct device device_clocksource = {
734efb46 1007 .id = 0,
d369a5d8 1008 .bus = &clocksource_subsys,
734efb46 1009};
1010
ad596171 1011static int __init init_clocksource_sysfs(void)
734efb46 1012{
d369a5d8 1013 int error = subsys_system_register(&clocksource_subsys, NULL);
734efb46 1014
1015 if (!error)
d369a5d8 1016 error = device_register(&device_clocksource);
734efb46 1017 if (!error)
d369a5d8 1018 error = device_create_file(
734efb46 1019 &device_clocksource,
d369a5d8 1020 &dev_attr_current_clocksource);
7eaeb343
TG
1021 if (!error)
1022 error = device_create_file(&device_clocksource,
1023 &dev_attr_unbind_clocksource);
734efb46 1024 if (!error)
d369a5d8 1025 error = device_create_file(
734efb46 1026 &device_clocksource,
d369a5d8 1027 &dev_attr_available_clocksource);
734efb46 1028 return error;
1029}
1030
1031device_initcall(init_clocksource_sysfs);
2b013700 1032#endif /* CONFIG_SYSFS */
734efb46 1033
1034/**
1035 * boot_override_clocksource - boot clock override
1036 * @str: override name
1037 *
1038 * Takes a clocksource= boot argument and uses it
1039 * as the clocksource override name.
1040 */
1041static int __init boot_override_clocksource(char* str)
1042{
75c5158f 1043 mutex_lock(&clocksource_mutex);
734efb46 1044 if (str)
1045 strlcpy(override_name, str, sizeof(override_name));
75c5158f 1046 mutex_unlock(&clocksource_mutex);
734efb46 1047 return 1;
1048}
1049
1050__setup("clocksource=", boot_override_clocksource);
1051
1052/**
1053 * boot_override_clock - Compatibility layer for deprecated boot option
1054 * @str: override name
1055 *
1056 * DEPRECATED! Takes a clock= boot argument and uses it
1057 * as the clocksource override name
1058 */
1059static int __init boot_override_clock(char* str)
1060{
5d0cf410 1061 if (!strcmp(str, "pmtmr")) {
1062 printk("Warning: clock=pmtmr is deprecated. "
1063 "Use clocksource=acpi_pm.\n");
1064 return boot_override_clocksource("acpi_pm");
1065 }
1066 printk("Warning! clock= boot option is deprecated. "
1067 "Use clocksource=xyz\n");
734efb46 1068 return boot_override_clocksource(str);
1069}
1070
1071__setup("clock=", boot_override_clock);
This page took 0.601285 seconds and 5 git commands to generate.