blob: 50a8736757f3b470c5d0d393be03cfbf05f5df03 [file] [log] [blame]
john stultz734efb42006-06-26 00:25:05 -07001/*
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
john stultz734efb42006-06-26 00:25:05 -070024 */
25
Kay Sieversd369a5d2011-12-14 15:28:51 -080026#include <linux/device.h>
john stultz734efb42006-06-26 00:25:05 -070027#include <linux/clocksource.h>
john stultz734efb42006-06-26 00:25:05 -070028#include <linux/init.h>
29#include <linux/module.h>
Mathieu Desnoyersdc29a362007-02-10 01:43:43 -080030#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080031#include <linux/tick.h>
Martin Schwidefsky01548f42009-08-18 17:09:42 +020032#include <linux/kthread.h>
john stultz734efb42006-06-26 00:25:05 -070033
Thomas Gleixner03e13cf2013-04-25 20:31:50 +000034#include "tick-internal.h"
35
Patrick Ohlya038a352009-02-12 05:03:34 +000036void timecounter_init(struct timecounter *tc,
37 const struct cyclecounter *cc,
38 u64 start_tstamp)
39{
40 tc->cc = cc;
41 tc->cycle_last = cc->read(cc);
42 tc->nsec = start_tstamp;
43}
David S. Miller3586e0a2009-11-11 19:06:30 -080044EXPORT_SYMBOL_GPL(timecounter_init);
Patrick Ohlya038a352009-02-12 05:03:34 +000045
46/**
47 * timecounter_read_delta - get nanoseconds since last call of this function
48 * @tc: Pointer to time counter
49 *
50 * When the underlying cycle counter runs over, this will be handled
51 * correctly as long as it does not run over more than once between
52 * calls.
53 *
54 * The first call to this function for a new time counter initializes
55 * the time tracking and returns an undefined result.
56 */
57static u64 timecounter_read_delta(struct timecounter *tc)
58{
59 cycle_t cycle_now, cycle_delta;
60 u64 ns_offset;
61
62 /* read cycle counter: */
63 cycle_now = tc->cc->read(tc->cc);
64
65 /* calculate the delta since the last timecounter_read_delta(): */
66 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
67
68 /* convert to nanoseconds: */
69 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
70
71 /* update time stamp of timecounter_read_delta() call: */
72 tc->cycle_last = cycle_now;
73
74 return ns_offset;
75}
76
77u64 timecounter_read(struct timecounter *tc)
78{
79 u64 nsec;
80
81 /* increment time by nanoseconds since last call */
82 nsec = timecounter_read_delta(tc);
83 nsec += tc->nsec;
84 tc->nsec = nsec;
85
86 return nsec;
87}
David S. Miller3586e0a2009-11-11 19:06:30 -080088EXPORT_SYMBOL_GPL(timecounter_read);
Patrick Ohlya038a352009-02-12 05:03:34 +000089
90u64 timecounter_cyc2time(struct timecounter *tc,
91 cycle_t cycle_tstamp)
92{
93 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
94 u64 nsec;
95
96 /*
97 * Instead of always treating cycle_tstamp as more recent
98 * than tc->cycle_last, detect when it is too far in the
99 * future and treat it as old time stamp instead.
100 */
101 if (cycle_delta > tc->cc->mask / 2) {
102 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
103 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
104 } else {
105 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
106 }
107
108 return nsec;
109}
David S. Miller3586e0a2009-11-11 19:06:30 -0800110EXPORT_SYMBOL_GPL(timecounter_cyc2time);
Patrick Ohlya038a352009-02-12 05:03:34 +0000111
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000112/**
113 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
114 * @mult: pointer to mult variable
115 * @shift: pointer to shift variable
116 * @from: frequency to convert from
117 * @to: frequency to convert to
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500118 * @maxsec: guaranteed runtime conversion range in seconds
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000119 *
120 * The function evaluates the shift/mult pair for the scaled math
121 * operations of clocksources and clockevents.
122 *
123 * @to and @from are frequency values in HZ. For clock sources @to is
124 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
125 * event @to is the counter frequency and @from is NSEC_PER_SEC.
126 *
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500127 * The @maxsec conversion range argument controls the time frame in
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000128 * seconds which must be covered by the runtime conversion with the
129 * calculated mult and shift factors. This guarantees that no 64bit
130 * overflow happens when the input value of the conversion is
131 * multiplied with the calculated mult factor. Larger ranges may
132 * reduce the conversion accuracy by chosing smaller mult and shift
133 * factors.
134 */
135void
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500136clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000137{
138 u64 tmp;
139 u32 sft, sftacc= 32;
140
141 /*
142 * Calculate the shift factor which is limiting the conversion
143 * range:
144 */
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500145 tmp = ((u64)maxsec * from) >> 32;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000146 while (tmp) {
147 tmp >>=1;
148 sftacc--;
149 }
150
151 /*
152 * Find the conversion shift/mult pair which has the best
153 * accuracy and fits the maxsec conversion range:
154 */
155 for (sft = 32; sft > 0; sft--) {
156 tmp = (u64) to << sft;
john stultzb5776c42010-12-16 19:03:27 +0000157 tmp += from / 2;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000158 do_div(tmp, from);
159 if ((tmp >> sftacc) == 0)
160 break;
161 }
162 *mult = tmp;
163 *shift = sft;
164}
165
john stultz734efb42006-06-26 00:25:05 -0700166/*[Clocksource internal variables]---------
167 * curr_clocksource:
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200168 * currently selected clocksource.
john stultz734efb42006-06-26 00:25:05 -0700169 * clocksource_list:
170 * linked list with the registered clocksources
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200171 * clocksource_mutex:
172 * protects manipulations to curr_clocksource and the clocksource_list
john stultz734efb42006-06-26 00:25:05 -0700173 * override_name:
174 * Name of the user-specified clocksource.
175 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200176static struct clocksource *curr_clocksource;
john stultz734efb42006-06-26 00:25:05 -0700177static LIST_HEAD(clocksource_list);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200178static DEFINE_MUTEX(clocksource_mutex);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000179static char override_name[CS_NAME_LEN];
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200180static int finished_booting;
john stultz734efb42006-06-26 00:25:05 -0700181
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800182#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200183static void clocksource_watchdog_work(struct work_struct *work);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200184static void clocksource_select(void);
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200185
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800186static LIST_HEAD(watchdog_list);
187static struct clocksource *watchdog;
188static struct timer_list watchdog_timer;
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200189static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800190static DEFINE_SPINLOCK(watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200191static int watchdog_running;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200192static atomic_t watchdog_reset_pending;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700193
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200194static int clocksource_watchdog_kthread(void *data);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200195static void __clocksource_change_rating(struct clocksource *cs, int rating);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200196
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800197/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700198 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800199 */
200#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700201#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800202
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200203static void clocksource_watchdog_work(struct work_struct *work)
204{
205 /*
206 * If kthread_run fails the next watchdog scan over the
207 * watchdog_list will find the unstable clock again.
208 */
209 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
210}
211
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200212static void __clocksource_unstable(struct clocksource *cs)
213{
214 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
215 cs->flags |= CLOCK_SOURCE_UNSTABLE;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200216 if (finished_booting)
217 schedule_work(&watchdog_work);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200218}
219
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200220static void clocksource_unstable(struct clocksource *cs, int64_t delta)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800221{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800222 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
223 cs->name, delta);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200224 __clocksource_unstable(cs);
225}
226
227/**
228 * clocksource_mark_unstable - mark clocksource unstable via watchdog
229 * @cs: clocksource to be marked unstable
230 *
231 * This function is called instead of clocksource_change_rating from
232 * cpu hotplug code to avoid a deadlock between the clocksource mutex
233 * and the cpu hotplug mutex. It defers the update of the clocksource
234 * to the watchdog thread.
235 */
236void clocksource_mark_unstable(struct clocksource *cs)
237{
238 unsigned long flags;
239
240 spin_lock_irqsave(&watchdog_lock, flags);
241 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
242 if (list_empty(&cs->wd_list))
243 list_add(&cs->wd_list, &watchdog_list);
244 __clocksource_unstable(cs);
245 }
246 spin_unlock_irqrestore(&watchdog_lock, flags);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800247}
248
249static void clocksource_watchdog(unsigned long data)
250{
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200251 struct clocksource *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800252 cycle_t csnow, wdnow;
253 int64_t wd_nsec, cs_nsec;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200254 int next_cpu, reset_pending;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800255
256 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200257 if (!watchdog_running)
258 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800259
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200260 reset_pending = atomic_read(&watchdog_reset_pending);
261
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200262 list_for_each_entry(cs, &watchdog_list, wd_list) {
263
264 /* Clocksource already marked unstable? */
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200265 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200266 if (finished_booting)
267 schedule_work(&watchdog_work);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200268 continue;
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200269 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200270
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200271 local_irq_disable();
Magnus Damm8e196082009-04-21 12:24:00 -0700272 csnow = cs->read(cs);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200273 wdnow = watchdog->read(watchdog);
274 local_irq_enable();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700275
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200276 /* Clocksource initialized ? */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200277 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
278 atomic_read(&watchdog_reset_pending)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200279 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200280 cs->wd_last = wdnow;
281 cs->cs_last = csnow;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700282 continue;
283 }
284
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200285 wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask,
286 watchdog->mult, watchdog->shift);
287
288 cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200289 cs->mask, cs->mult, cs->shift);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200290 cs->cs_last = csnow;
291 cs->wd_last = wdnow;
292
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200293 if (atomic_read(&watchdog_reset_pending))
294 continue;
295
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200296 /* Check the deviation from the watchdog clocksource. */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200297 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200298 clocksource_unstable(cs, cs_nsec - wd_nsec);
299 continue;
300 }
301
302 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
303 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
304 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
Thomas Gleixner332962f2013-07-04 22:46:45 +0200305 /* Mark it valid for high-res. */
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200306 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner332962f2013-07-04 22:46:45 +0200307
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200308 /*
Thomas Gleixner332962f2013-07-04 22:46:45 +0200309 * clocksource_done_booting() will sort it if
310 * finished_booting is not set yet.
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200311 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200312 if (!finished_booting)
313 continue;
314
315 /*
316 * If this is not the current clocksource let
317 * the watchdog thread reselect it. Due to the
318 * change to high res this clocksource might
319 * be preferred now. If it is the current
320 * clocksource let the tick code know about
321 * that change.
322 */
323 if (cs != curr_clocksource) {
324 cs->flags |= CLOCK_SOURCE_RESELECT;
325 schedule_work(&watchdog_work);
326 } else {
327 tick_clock_notify();
328 }
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800329 }
330 }
331
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200332 /*
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200333 * We only clear the watchdog_reset_pending, when we did a
334 * full cycle through all clocksources.
335 */
336 if (reset_pending)
337 atomic_dec(&watchdog_reset_pending);
338
339 /*
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200340 * Cycle through CPUs to check if the CPUs stay synchronized
341 * to each other.
342 */
343 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
344 if (next_cpu >= nr_cpu_ids)
345 next_cpu = cpumask_first(cpu_online_mask);
346 watchdog_timer.expires += WATCHDOG_INTERVAL;
347 add_timer_on(&watchdog_timer, next_cpu);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200348out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800349 spin_unlock(&watchdog_lock);
350}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200351
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200352static inline void clocksource_start_watchdog(void)
353{
354 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
355 return;
356 init_timer(&watchdog_timer);
357 watchdog_timer.function = clocksource_watchdog;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200358 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
359 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
360 watchdog_running = 1;
361}
362
363static inline void clocksource_stop_watchdog(void)
364{
365 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
366 return;
367 del_timer(&watchdog_timer);
368 watchdog_running = 0;
369}
370
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200371static inline void clocksource_reset_watchdog(void)
372{
373 struct clocksource *cs;
374
375 list_for_each_entry(cs, &watchdog_list, wd_list)
376 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
377}
378
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700379static void clocksource_resume_watchdog(void)
380{
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200381 atomic_inc(&watchdog_reset_pending);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700382}
383
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200384static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800385{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800386 unsigned long flags;
387
388 spin_lock_irqsave(&watchdog_lock, flags);
389 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200390 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800391 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200392 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200393 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200394 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200395 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800396 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200397 /* Pick the best watchdog. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800398 if (!watchdog || cs->rating > watchdog->rating) {
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800399 watchdog = cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800400 /* Reset watchdog cycles */
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200401 clocksource_reset_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800402 }
403 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200404 /* Check if the watchdog timer needs to be started. */
405 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800406 spin_unlock_irqrestore(&watchdog_lock, flags);
407}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200408
409static void clocksource_dequeue_watchdog(struct clocksource *cs)
410{
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200411 unsigned long flags;
412
413 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000414 if (cs != watchdog) {
415 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
416 /* cs is a watched clocksource. */
417 list_del_init(&cs->wd_list);
418 /* Check if the watchdog timer needs to be stopped. */
419 clocksource_stop_watchdog();
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200420 }
421 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200422 spin_unlock_irqrestore(&watchdog_lock, flags);
423}
424
Thomas Gleixner332962f2013-07-04 22:46:45 +0200425static int __clocksource_watchdog_kthread(void)
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200426{
427 struct clocksource *cs, *tmp;
428 unsigned long flags;
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200429 LIST_HEAD(unstable);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200430 int select = 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200431
432 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200433 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200434 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
435 list_del_init(&cs->wd_list);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200436 list_add(&cs->wd_list, &unstable);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200437 select = 1;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200438 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200439 if (cs->flags & CLOCK_SOURCE_RESELECT) {
440 cs->flags &= ~CLOCK_SOURCE_RESELECT;
441 select = 1;
442 }
443 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200444 /* Check if the watchdog timer needs to be stopped. */
445 clocksource_stop_watchdog();
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200446 spin_unlock_irqrestore(&watchdog_lock, flags);
447
448 /* Needs to be done outside of watchdog lock */
449 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
450 list_del_init(&cs->wd_list);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200451 __clocksource_change_rating(cs, 0);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200452 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200453 return select;
454}
455
456static int clocksource_watchdog_kthread(void *data)
457{
458 mutex_lock(&clocksource_mutex);
459 if (__clocksource_watchdog_kthread())
460 clocksource_select();
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200461 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200462 return 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200463}
464
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000465static bool clocksource_is_watchdog(struct clocksource *cs)
466{
467 return cs == watchdog;
468}
469
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200470#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
471
472static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800473{
474 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
475 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
476}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700477
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200478static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700479static inline void clocksource_resume_watchdog(void) { }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200480static inline int __clocksource_watchdog_kthread(void) { return 0; }
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000481static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200482
483#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800484
john stultz734efb42006-06-26 00:25:05 -0700485/**
Magnus Dammc54a42b2010-02-02 14:41:41 -0800486 * clocksource_suspend - suspend the clocksource(s)
487 */
488void clocksource_suspend(void)
489{
490 struct clocksource *cs;
491
492 list_for_each_entry_reverse(cs, &clocksource_list, list)
493 if (cs->suspend)
494 cs->suspend(cs);
495}
496
497/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700498 * clocksource_resume - resume the clocksource(s)
499 */
500void clocksource_resume(void)
501{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700502 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700503
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200504 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700505 if (cs->resume)
Magnus Damm17622332010-02-02 14:41:39 -0800506 cs->resume(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700507
508 clocksource_resume_watchdog();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700509}
510
511/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600512 * clocksource_touch_watchdog - Update watchdog
513 *
514 * Update the watchdog after exception contexts such as kgdb so as not
Thomas Gleixner7b7422a2010-01-26 12:51:10 +0100515 * to incorrectly trip the watchdog. This might fail when the kernel
516 * was stopped in code which holds watchdog_lock.
Jason Wessel7c3078b2008-02-15 14:55:54 -0600517 */
518void clocksource_touch_watchdog(void)
519{
520 clocksource_resume_watchdog();
521}
522
john stultz734efb42006-06-26 00:25:05 -0700523/**
John Stultzd65670a2011-10-31 17:06:35 -0400524 * clocksource_max_adjustment- Returns max adjustment amount
525 * @cs: Pointer to clocksource
526 *
527 */
528static u32 clocksource_max_adjustment(struct clocksource *cs)
529{
530 u64 ret;
531 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600532 * We won't try to correct for more than 11% adjustments (110,000 ppm),
John Stultzd65670a2011-10-31 17:06:35 -0400533 */
534 ret = (u64)cs->mult * 11;
535 do_div(ret,100);
536 return (u32)ret;
537}
538
539/**
Jon Hunter98962462009-08-18 12:45:10 -0500540 * clocksource_max_deferment - Returns max time the clocksource can be deferred
541 * @cs: Pointer to clocksource
542 *
543 */
544static u64 clocksource_max_deferment(struct clocksource *cs)
545{
546 u64 max_nsecs, max_cycles;
547
548 /*
549 * Calculate the maximum number of cycles that we can pass to the
550 * cyc2ns function without overflowing a 64-bit signed result. The
John Stultzd65670a2011-10-31 17:06:35 -0400551 * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
552 * which is equivalent to the below.
553 * max_cycles < (2^63)/(cs->mult + cs->maxadj)
554 * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
555 * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
556 * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
557 * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
Jon Hunter98962462009-08-18 12:45:10 -0500558 * Please note that we add 1 to the result of the log2 to account for
559 * any rounding errors, ensure the above inequality is satisfied and
560 * no overflow will occur.
561 */
John Stultzd65670a2011-10-31 17:06:35 -0400562 max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
Jon Hunter98962462009-08-18 12:45:10 -0500563
564 /*
565 * The actual maximum number of cycles we can defer the clocksource is
566 * determined by the minimum of max_cycles and cs->mask.
John Stultzd65670a2011-10-31 17:06:35 -0400567 * Note: Here we subtract the maxadj to make sure we don't sleep for
568 * too long if there's a large negative adjustment.
Jon Hunter98962462009-08-18 12:45:10 -0500569 */
570 max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
John Stultzd65670a2011-10-31 17:06:35 -0400571 max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
572 cs->shift);
Jon Hunter98962462009-08-18 12:45:10 -0500573
574 /*
575 * To ensure that the clocksource does not wrap whilst we are idle,
576 * limit the time the clocksource can be deferred by 12.5%. Please
577 * note a margin of 12.5% is used because this can be computed with
578 * a shift, versus say 10% which would require division.
579 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500580 return max_nsecs - (max_nsecs >> 3);
Jon Hunter98962462009-08-18 12:45:10 -0500581}
582
John Stultz592913e2010-07-13 17:56:20 -0700583#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
john stultz734efb42006-06-26 00:25:05 -0700584
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000585static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000586{
587 struct clocksource *cs;
588
589 if (!finished_booting || list_empty(&clocksource_list))
590 return NULL;
591
592 /*
593 * We pick the clocksource with the highest rating. If oneshot
594 * mode is active, we pick the highres valid clocksource with
595 * the best rating.
596 */
597 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000598 if (skipcur && cs == curr_clocksource)
599 continue;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000600 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
601 continue;
602 return cs;
603 }
604 return NULL;
605}
606
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000607static void __clocksource_select(bool skipcur)
john stultz734efb42006-06-26 00:25:05 -0700608{
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000609 bool oneshot = tick_oneshot_mode_active();
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200610 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800611
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000612 /* Find the best suitable clocksource */
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000613 best = clocksource_find_best(oneshot, skipcur);
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000614 if (!best)
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200615 return;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000616
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200617 /* Check for the override clocksource. */
618 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000619 if (skipcur && cs == curr_clocksource)
620 continue;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200621 if (strcmp(cs->name, override_name) != 0)
622 continue;
623 /*
624 * Check to make sure we don't switch to a non-highres
625 * capable clocksource if the tick code is in oneshot
626 * mode (highres or nohz)
627 */
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000628 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200629 /* Override clocksource cannot be used. */
630 printk(KERN_WARNING "Override clocksource %s is not "
631 "HRT compatible. Cannot switch while in "
632 "HRT/NOHZ mode\n", cs->name);
633 override_name[0] = 0;
634 } else
635 /* Override clocksource can be used. */
636 best = cs;
637 break;
638 }
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000639
640 if (curr_clocksource != best && !timekeeping_notify(best)) {
641 pr_info("Switched to clocksource %s\n", best->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200642 curr_clocksource = best;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200643 }
john stultz734efb42006-06-26 00:25:05 -0700644}
645
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000646/**
647 * clocksource_select - Select the best clocksource available
648 *
649 * Private function. Must hold clocksource_mutex when called.
650 *
651 * Select the clocksource with the best rating, or the clocksource,
652 * which is selected by userspace override.
653 */
654static void clocksource_select(void)
655{
656 return __clocksource_select(false);
657}
658
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000659static void clocksource_select_fallback(void)
660{
661 return __clocksource_select(true);
662}
663
John Stultz592913e2010-07-13 17:56:20 -0700664#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200665
666static inline void clocksource_select(void) { }
Thomas Gleixner1eaff672013-05-28 09:48:46 +0200667static inline void clocksource_select_fallback(void) { }
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200668
669#endif
670
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200671/*
672 * clocksource_done_booting - Called near the end of core bootup
673 *
674 * Hack to avoid lots of clocksource churn at boot time.
675 * We use fs_initcall because we want this to start before
676 * device_initcall but after subsys_initcall.
677 */
678static int __init clocksource_done_booting(void)
679{
john stultzad6759f2010-03-01 12:34:43 -0800680 mutex_lock(&clocksource_mutex);
681 curr_clocksource = clocksource_default_clock();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200682 finished_booting = 1;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200683 /*
684 * Run the watchdog first to eliminate unstable clock sources
685 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200686 __clocksource_watchdog_kthread();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200687 clocksource_select();
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200688 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200689 return 0;
690}
691fs_initcall(clocksource_done_booting);
692
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800693/*
694 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700695 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200696static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700697{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200698 struct list_head *entry = &clocksource_list;
699 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700700
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200701 list_for_each_entry(tmp, &clocksource_list, list)
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800702 /* Keep track of the place, where to insert */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200703 if (tmp->rating >= cs->rating)
704 entry = &tmp->list;
705 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700706}
707
John Stultzd7e81c22010-05-07 18:07:38 -0700708/**
John Stultz852db462010-07-13 17:56:28 -0700709 * __clocksource_updatefreq_scale - Used update clocksource with new freq
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900710 * @cs: clocksource to be registered
John Stultz852db462010-07-13 17:56:28 -0700711 * @scale: Scale factor multiplied against freq to get clocksource hz
712 * @freq: clocksource frequency (cycles per second) divided by scale
713 *
714 * This should only be called from the clocksource->enable() method.
715 *
716 * This *SHOULD NOT* be called directly! Please use the
717 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
718 */
719void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
720{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200721 u64 sec;
John Stultz852db462010-07-13 17:56:28 -0700722 /*
Thomas Gleixner724ed532011-05-18 21:33:40 +0000723 * Calc the maximum number of seconds which we can run before
724 * wrapping around. For clocksources which have a mask > 32bit
725 * we need to limit the max sleep time to have a good
726 * conversion precision. 10 minutes is still a reasonable
727 * amount. That results in a shift value of 24 for a
728 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
729 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
730 * margin as we do in clocksource_max_deferment()
John Stultz852db462010-07-13 17:56:28 -0700731 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500732 sec = (cs->mask - (cs->mask >> 3));
Thomas Gleixner724ed532011-05-18 21:33:40 +0000733 do_div(sec, freq);
734 do_div(sec, scale);
735 if (!sec)
736 sec = 1;
737 else if (sec > 600 && cs->mask > UINT_MAX)
738 sec = 600;
739
John Stultz852db462010-07-13 17:56:28 -0700740 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
Thomas Gleixner724ed532011-05-18 21:33:40 +0000741 NSEC_PER_SEC / scale, sec * scale);
John Stultzd65670a2011-10-31 17:06:35 -0400742
743 /*
744 * for clocksources that have large mults, to avoid overflow.
745 * Since mult may be adjusted by ntp, add an safety extra margin
746 *
747 */
748 cs->maxadj = clocksource_max_adjustment(cs);
749 while ((cs->mult + cs->maxadj < cs->mult)
750 || (cs->mult - cs->maxadj > cs->mult)) {
751 cs->mult >>= 1;
752 cs->shift--;
753 cs->maxadj = clocksource_max_adjustment(cs);
754 }
755
John Stultz852db462010-07-13 17:56:28 -0700756 cs->max_idle_ns = clocksource_max_deferment(cs);
757}
758EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
759
760/**
John Stultzd7e81c22010-05-07 18:07:38 -0700761 * __clocksource_register_scale - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900762 * @cs: clocksource to be registered
John Stultzd7e81c22010-05-07 18:07:38 -0700763 * @scale: Scale factor multiplied against freq to get clocksource hz
764 * @freq: clocksource frequency (cycles per second) divided by scale
765 *
766 * Returns -EBUSY if registration fails, zero otherwise.
767 *
768 * This *SHOULD NOT* be called directly! Please use the
769 * clocksource_register_hz() or clocksource_register_khz helper functions.
770 */
771int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
772{
773
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400774 /* Initialize mult/shift and max_idle_ns */
John Stultz852db462010-07-13 17:56:28 -0700775 __clocksource_updatefreq_scale(cs, scale, freq);
John Stultzd7e81c22010-05-07 18:07:38 -0700776
John Stultz852db462010-07-13 17:56:28 -0700777 /* Add clocksource to the clcoksource list */
John Stultzd7e81c22010-05-07 18:07:38 -0700778 mutex_lock(&clocksource_mutex);
779 clocksource_enqueue(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700780 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700781 clocksource_select();
John Stultzd7e81c22010-05-07 18:07:38 -0700782 mutex_unlock(&clocksource_mutex);
783 return 0;
784}
785EXPORT_SYMBOL_GPL(__clocksource_register_scale);
786
787
john stultz734efb42006-06-26 00:25:05 -0700788/**
john stultza2752542006-06-26 00:25:14 -0700789 * clocksource_register - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900790 * @cs: clocksource to be registered
john stultz734efb42006-06-26 00:25:05 -0700791 *
792 * Returns -EBUSY if registration fails, zero otherwise.
793 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200794int clocksource_register(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700795{
John Stultzd65670a2011-10-31 17:06:35 -0400796 /* calculate max adjustment for given mult/shift */
797 cs->maxadj = clocksource_max_adjustment(cs);
798 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
799 "Clocksource %s might overflow on 11%% adjustment\n",
800 cs->name);
801
Jon Hunter98962462009-08-18 12:45:10 -0500802 /* calculate max idle time permitted for this clocksource */
803 cs->max_idle_ns = clocksource_max_deferment(cs);
804
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200805 mutex_lock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200806 clocksource_enqueue(cs);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200807 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700808 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200809 mutex_unlock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200810 return 0;
john stultz734efb42006-06-26 00:25:05 -0700811}
john stultza2752542006-06-26 00:25:14 -0700812EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700813
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200814static void __clocksource_change_rating(struct clocksource *cs, int rating)
815{
816 list_del(&cs->list);
817 cs->rating = rating;
818 clocksource_enqueue(cs);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200819}
820
john stultz734efb42006-06-26 00:25:05 -0700821/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800822 * clocksource_change_rating - Change the rating of a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900823 * @cs: clocksource to be changed
824 * @rating: new rating
john stultz734efb42006-06-26 00:25:05 -0700825 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800826void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700827{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200828 mutex_lock(&clocksource_mutex);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200829 __clocksource_change_rating(cs, rating);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200830 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200831 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700832}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200833EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700834
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000835/*
836 * Unbind clocksource @cs. Called with clocksource_mutex held
837 */
838static int clocksource_unbind(struct clocksource *cs)
839{
840 /*
841 * I really can't convince myself to support this on hardware
842 * designed by lobotomized monkeys.
843 */
844 if (clocksource_is_watchdog(cs))
845 return -EBUSY;
846
847 if (cs == curr_clocksource) {
848 /* Select and try to install a replacement clock source */
849 clocksource_select_fallback();
850 if (curr_clocksource == cs)
851 return -EBUSY;
852 }
853 clocksource_dequeue_watchdog(cs);
854 list_del_init(&cs->list);
855 return 0;
856}
857
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100858/**
859 * clocksource_unregister - remove a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900860 * @cs: clocksource to be unregistered
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100861 */
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000862int clocksource_unregister(struct clocksource *cs)
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100863{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000864 int ret = 0;
865
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200866 mutex_lock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000867 if (!list_empty(&cs->list))
868 ret = clocksource_unbind(cs);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200869 mutex_unlock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000870 return ret;
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100871}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200872EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100873
Daniel Walker2b013702006-12-10 02:21:30 -0800874#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700875/**
876 * sysfs_show_current_clocksources - sysfs interface for current clocksource
877 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900878 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700879 * @buf: char buffer to be filled with clocksource list
880 *
881 * Provides sysfs interface for listing current clocksource.
882 */
883static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800884sysfs_show_current_clocksources(struct device *dev,
885 struct device_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700886{
Miao Xie5e2cb102008-02-06 01:36:53 -0800887 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700888
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200889 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -0800890 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200891 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700892
Miao Xie5e2cb102008-02-06 01:36:53 -0800893 return count;
john stultz734efb42006-06-26 00:25:05 -0700894}
895
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000896size_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
Thomas Gleixner29b54072013-04-25 20:31:45 +0000897{
898 size_t ret = cnt;
899
900 /* strings from sysfs write are not 0 terminated! */
901 if (!cnt || cnt >= CS_NAME_LEN)
902 return -EINVAL;
903
904 /* strip of \n: */
905 if (buf[cnt-1] == '\n')
906 cnt--;
907 if (cnt > 0)
908 memcpy(dst, buf, cnt);
909 dst[cnt] = 0;
910 return ret;
911}
912
john stultz734efb42006-06-26 00:25:05 -0700913/**
914 * sysfs_override_clocksource - interface for manually overriding clocksource
915 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900916 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700917 * @buf: name of override clocksource
918 * @count: length of buffer
919 *
920 * Takes input from sysfs interface for manually overriding the default
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200921 * clocksource selection.
john stultz734efb42006-06-26 00:25:05 -0700922 */
Kay Sieversd369a5d2011-12-14 15:28:51 -0800923static ssize_t sysfs_override_clocksource(struct device *dev,
924 struct device_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700925 const char *buf, size_t count)
926{
Thomas Gleixner29b54072013-04-25 20:31:45 +0000927 size_t ret;
john stultz734efb42006-06-26 00:25:05 -0700928
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200929 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700930
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000931 ret = sysfs_get_uname(buf, override_name, count);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000932 if (ret >= 0)
933 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700934
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200935 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700936
937 return ret;
938}
939
940/**
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000941 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
942 * @dev: unused
943 * @attr: unused
944 * @buf: unused
945 * @count: length of buffer
946 *
947 * Takes input from sysfs interface for manually unbinding a clocksource.
948 */
949static ssize_t sysfs_unbind_clocksource(struct device *dev,
950 struct device_attribute *attr,
951 const char *buf, size_t count)
952{
953 struct clocksource *cs;
954 char name[CS_NAME_LEN];
955 size_t ret;
956
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000957 ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000958 if (ret < 0)
959 return ret;
960
961 ret = -ENODEV;
962 mutex_lock(&clocksource_mutex);
963 list_for_each_entry(cs, &clocksource_list, list) {
964 if (strcmp(cs->name, name))
965 continue;
966 ret = clocksource_unbind(cs);
967 break;
968 }
969 mutex_unlock(&clocksource_mutex);
970
971 return ret ? ret : count;
972}
973
974/**
john stultz734efb42006-06-26 00:25:05 -0700975 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
976 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900977 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700978 * @buf: char buffer to be filled with clocksource list
979 *
980 * Provides sysfs interface for listing registered clocksources
981 */
982static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800983sysfs_show_available_clocksources(struct device *dev,
984 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200985 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700986{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700987 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -0800988 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700989
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200990 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700991 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200992 /*
993 * Don't show non-HRES clocksource if the tick code is
994 * in one shot mode (highres=on or nohz=on)
995 */
996 if (!tick_oneshot_mode_active() ||
997 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -0700998 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -0800999 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1000 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -07001001 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001002 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001003
Miao Xie5e2cb102008-02-06 01:36:53 -08001004 count += snprintf(buf + count,
1005 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -07001006
Miao Xie5e2cb102008-02-06 01:36:53 -08001007 return count;
john stultz734efb42006-06-26 00:25:05 -07001008}
1009
1010/*
1011 * Sysfs setup bits:
1012 */
Kay Sieversd369a5d2011-12-14 15:28:51 -08001013static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001014 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001015
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001016static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
1017
Kay Sieversd369a5d2011-12-14 15:28:51 -08001018static DEVICE_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001019 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -07001020
Kay Sieversd369a5d2011-12-14 15:28:51 -08001021static struct bus_type clocksource_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001022 .name = "clocksource",
Kay Sieversd369a5d2011-12-14 15:28:51 -08001023 .dev_name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -07001024};
1025
Kay Sieversd369a5d2011-12-14 15:28:51 -08001026static struct device device_clocksource = {
john stultz734efb42006-06-26 00:25:05 -07001027 .id = 0,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001028 .bus = &clocksource_subsys,
john stultz734efb42006-06-26 00:25:05 -07001029};
1030
john stultzad596172006-06-26 00:25:06 -07001031static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -07001032{
Kay Sieversd369a5d2011-12-14 15:28:51 -08001033 int error = subsys_system_register(&clocksource_subsys, NULL);
john stultz734efb42006-06-26 00:25:05 -07001034
1035 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001036 error = device_register(&device_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001037 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001038 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -07001039 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001040 &dev_attr_current_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001041 if (!error)
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001042 error = device_create_file(&device_clocksource,
1043 &dev_attr_unbind_clocksource);
1044 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001045 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -07001046 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001047 &dev_attr_available_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001048 return error;
1049}
1050
1051device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -08001052#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -07001053
1054/**
1055 * boot_override_clocksource - boot clock override
1056 * @str: override name
1057 *
1058 * Takes a clocksource= boot argument and uses it
1059 * as the clocksource override name.
1060 */
1061static int __init boot_override_clocksource(char* str)
1062{
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001063 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001064 if (str)
1065 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001066 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001067 return 1;
1068}
1069
1070__setup("clocksource=", boot_override_clocksource);
1071
1072/**
1073 * boot_override_clock - Compatibility layer for deprecated boot option
1074 * @str: override name
1075 *
1076 * DEPRECATED! Takes a clock= boot argument and uses it
1077 * as the clocksource override name
1078 */
1079static int __init boot_override_clock(char* str)
1080{
john stultz5d0cf412006-06-26 00:25:12 -07001081 if (!strcmp(str, "pmtmr")) {
1082 printk("Warning: clock=pmtmr is deprecated. "
1083 "Use clocksource=acpi_pm.\n");
1084 return boot_override_clocksource("acpi_pm");
1085 }
1086 printk("Warning! clock= boot option is deprecated. "
1087 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -07001088 return boot_override_clocksource(str);
1089}
1090
1091__setup("clock=", boot_override_clock);