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