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