blob: c9317e14aae6cfab03d1c9566a9de3b5cc78a0fa [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; }
Prarit Bhargava397bbf62013-02-22 15:08:56 -0500482void clocksource_mark_unstable(struct clocksource *cs) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200483
484#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800485
john stultz734efb42006-06-26 00:25:05 -0700486/**
Magnus Dammc54a42b2010-02-02 14:41:41 -0800487 * clocksource_suspend - suspend the clocksource(s)
488 */
489void clocksource_suspend(void)
490{
491 struct clocksource *cs;
492
493 list_for_each_entry_reverse(cs, &clocksource_list, list)
494 if (cs->suspend)
495 cs->suspend(cs);
496}
497
498/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700499 * clocksource_resume - resume the clocksource(s)
500 */
501void clocksource_resume(void)
502{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700503 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700504
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200505 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700506 if (cs->resume)
Magnus Damm17622332010-02-02 14:41:39 -0800507 cs->resume(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700508
509 clocksource_resume_watchdog();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700510}
511
512/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600513 * clocksource_touch_watchdog - Update watchdog
514 *
515 * Update the watchdog after exception contexts such as kgdb so as not
Thomas Gleixner7b7422a2010-01-26 12:51:10 +0100516 * to incorrectly trip the watchdog. This might fail when the kernel
517 * was stopped in code which holds watchdog_lock.
Jason Wessel7c3078b2008-02-15 14:55:54 -0600518 */
519void clocksource_touch_watchdog(void)
520{
521 clocksource_resume_watchdog();
522}
523
john stultz734efb42006-06-26 00:25:05 -0700524/**
John Stultzd65670a2011-10-31 17:06:35 -0400525 * clocksource_max_adjustment- Returns max adjustment amount
526 * @cs: Pointer to clocksource
527 *
528 */
529static u32 clocksource_max_adjustment(struct clocksource *cs)
530{
531 u64 ret;
532 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600533 * We won't try to correct for more than 11% adjustments (110,000 ppm),
John Stultzd65670a2011-10-31 17:06:35 -0400534 */
535 ret = (u64)cs->mult * 11;
536 do_div(ret,100);
537 return (u32)ret;
538}
539
540/**
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700541 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
542 * @mult: cycle to nanosecond multiplier
543 * @shift: cycle to nanosecond divisor (power of two)
544 * @maxadj: maximum adjustment value to mult (~11%)
545 * @mask: bitmask for two's complement subtraction of non 64 bit counters
Jon Hunter98962462009-08-18 12:45:10 -0500546 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700547u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask)
Jon Hunter98962462009-08-18 12:45:10 -0500548{
549 u64 max_nsecs, max_cycles;
550
551 /*
552 * Calculate the maximum number of cycles that we can pass to the
553 * cyc2ns function without overflowing a 64-bit signed result. The
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700554 * maximum number of cycles is equal to ULLONG_MAX/(mult+maxadj)
John Stultzd65670a2011-10-31 17:06:35 -0400555 * which is equivalent to the below.
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700556 * max_cycles < (2^63)/(mult + maxadj)
557 * max_cycles < 2^(log2((2^63)/(mult + maxadj)))
558 * max_cycles < 2^(log2(2^63) - log2(mult + maxadj))
559 * max_cycles < 2^(63 - log2(mult + maxadj))
560 * max_cycles < 1 << (63 - log2(mult + maxadj))
Jon Hunter98962462009-08-18 12:45:10 -0500561 * Please note that we add 1 to the result of the log2 to account for
562 * any rounding errors, ensure the above inequality is satisfied and
563 * no overflow will occur.
564 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700565 max_cycles = 1ULL << (63 - (ilog2(mult + maxadj) + 1));
Jon Hunter98962462009-08-18 12:45:10 -0500566
567 /*
568 * The actual maximum number of cycles we can defer the clocksource is
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700569 * determined by the minimum of max_cycles and mask.
John Stultzd65670a2011-10-31 17:06:35 -0400570 * Note: Here we subtract the maxadj to make sure we don't sleep for
571 * too long if there's a large negative adjustment.
Jon Hunter98962462009-08-18 12:45:10 -0500572 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700573 max_cycles = min(max_cycles, mask);
574 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
Jon Hunter98962462009-08-18 12:45:10 -0500575
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700576 return max_nsecs;
577}
578
579/**
580 * clocksource_max_deferment - Returns max time the clocksource can be deferred
581 * @cs: Pointer to clocksource
582 *
583 */
584static u64 clocksource_max_deferment(struct clocksource *cs)
585{
586 u64 max_nsecs;
587
588 max_nsecs = clocks_calc_max_nsecs(cs->mult, cs->shift, cs->maxadj,
589 cs->mask);
Jon Hunter98962462009-08-18 12:45:10 -0500590 /*
591 * To ensure that the clocksource does not wrap whilst we are idle,
592 * limit the time the clocksource can be deferred by 12.5%. Please
593 * note a margin of 12.5% is used because this can be computed with
594 * a shift, versus say 10% which would require division.
595 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500596 return max_nsecs - (max_nsecs >> 3);
Jon Hunter98962462009-08-18 12:45:10 -0500597}
598
John Stultz592913e2010-07-13 17:56:20 -0700599#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
john stultz734efb42006-06-26 00:25:05 -0700600
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000601static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000602{
603 struct clocksource *cs;
604
605 if (!finished_booting || list_empty(&clocksource_list))
606 return NULL;
607
608 /*
609 * We pick the clocksource with the highest rating. If oneshot
610 * mode is active, we pick the highres valid clocksource with
611 * the best rating.
612 */
613 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000614 if (skipcur && cs == curr_clocksource)
615 continue;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000616 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
617 continue;
618 return cs;
619 }
620 return NULL;
621}
622
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000623static void __clocksource_select(bool skipcur)
john stultz734efb42006-06-26 00:25:05 -0700624{
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000625 bool oneshot = tick_oneshot_mode_active();
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200626 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800627
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000628 /* Find the best suitable clocksource */
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000629 best = clocksource_find_best(oneshot, skipcur);
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000630 if (!best)
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200631 return;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000632
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200633 /* Check for the override clocksource. */
634 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000635 if (skipcur && cs == curr_clocksource)
636 continue;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200637 if (strcmp(cs->name, override_name) != 0)
638 continue;
639 /*
640 * Check to make sure we don't switch to a non-highres
641 * capable clocksource if the tick code is in oneshot
642 * mode (highres or nohz)
643 */
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000644 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200645 /* Override clocksource cannot be used. */
646 printk(KERN_WARNING "Override clocksource %s is not "
647 "HRT compatible. Cannot switch while in "
648 "HRT/NOHZ mode\n", cs->name);
649 override_name[0] = 0;
650 } else
651 /* Override clocksource can be used. */
652 best = cs;
653 break;
654 }
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000655
656 if (curr_clocksource != best && !timekeeping_notify(best)) {
657 pr_info("Switched to clocksource %s\n", best->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200658 curr_clocksource = best;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200659 }
john stultz734efb42006-06-26 00:25:05 -0700660}
661
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000662/**
663 * clocksource_select - Select the best clocksource available
664 *
665 * Private function. Must hold clocksource_mutex when called.
666 *
667 * Select the clocksource with the best rating, or the clocksource,
668 * which is selected by userspace override.
669 */
670static void clocksource_select(void)
671{
672 return __clocksource_select(false);
673}
674
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000675static void clocksource_select_fallback(void)
676{
677 return __clocksource_select(true);
678}
679
John Stultz592913e2010-07-13 17:56:20 -0700680#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200681
682static inline void clocksource_select(void) { }
Thomas Gleixner1eaff672013-05-28 09:48:46 +0200683static inline void clocksource_select_fallback(void) { }
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200684
685#endif
686
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200687/*
688 * clocksource_done_booting - Called near the end of core bootup
689 *
690 * Hack to avoid lots of clocksource churn at boot time.
691 * We use fs_initcall because we want this to start before
692 * device_initcall but after subsys_initcall.
693 */
694static int __init clocksource_done_booting(void)
695{
john stultzad6759f2010-03-01 12:34:43 -0800696 mutex_lock(&clocksource_mutex);
697 curr_clocksource = clocksource_default_clock();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200698 finished_booting = 1;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200699 /*
700 * Run the watchdog first to eliminate unstable clock sources
701 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200702 __clocksource_watchdog_kthread();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200703 clocksource_select();
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200704 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200705 return 0;
706}
707fs_initcall(clocksource_done_booting);
708
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800709/*
710 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700711 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200712static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700713{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200714 struct list_head *entry = &clocksource_list;
715 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700716
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200717 list_for_each_entry(tmp, &clocksource_list, list)
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800718 /* Keep track of the place, where to insert */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200719 if (tmp->rating >= cs->rating)
720 entry = &tmp->list;
721 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700722}
723
John Stultzd7e81c22010-05-07 18:07:38 -0700724/**
John Stultz852db462010-07-13 17:56:28 -0700725 * __clocksource_updatefreq_scale - Used update clocksource with new freq
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900726 * @cs: clocksource to be registered
John Stultz852db462010-07-13 17:56:28 -0700727 * @scale: Scale factor multiplied against freq to get clocksource hz
728 * @freq: clocksource frequency (cycles per second) divided by scale
729 *
730 * This should only be called from the clocksource->enable() method.
731 *
732 * This *SHOULD NOT* be called directly! Please use the
733 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
734 */
735void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
736{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200737 u64 sec;
John Stultz852db462010-07-13 17:56:28 -0700738 /*
Thomas Gleixner724ed532011-05-18 21:33:40 +0000739 * Calc the maximum number of seconds which we can run before
740 * wrapping around. For clocksources which have a mask > 32bit
741 * we need to limit the max sleep time to have a good
742 * conversion precision. 10 minutes is still a reasonable
743 * amount. That results in a shift value of 24 for a
744 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
745 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
746 * margin as we do in clocksource_max_deferment()
John Stultz852db462010-07-13 17:56:28 -0700747 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500748 sec = (cs->mask - (cs->mask >> 3));
Thomas Gleixner724ed532011-05-18 21:33:40 +0000749 do_div(sec, freq);
750 do_div(sec, scale);
751 if (!sec)
752 sec = 1;
753 else if (sec > 600 && cs->mask > UINT_MAX)
754 sec = 600;
755
John Stultz852db462010-07-13 17:56:28 -0700756 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
Thomas Gleixner724ed532011-05-18 21:33:40 +0000757 NSEC_PER_SEC / scale, sec * scale);
John Stultzd65670a2011-10-31 17:06:35 -0400758
759 /*
760 * for clocksources that have large mults, to avoid overflow.
761 * Since mult may be adjusted by ntp, add an safety extra margin
762 *
763 */
764 cs->maxadj = clocksource_max_adjustment(cs);
765 while ((cs->mult + cs->maxadj < cs->mult)
766 || (cs->mult - cs->maxadj > cs->mult)) {
767 cs->mult >>= 1;
768 cs->shift--;
769 cs->maxadj = clocksource_max_adjustment(cs);
770 }
771
John Stultz852db462010-07-13 17:56:28 -0700772 cs->max_idle_ns = clocksource_max_deferment(cs);
773}
774EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
775
776/**
John Stultzd7e81c22010-05-07 18:07:38 -0700777 * __clocksource_register_scale - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900778 * @cs: clocksource to be registered
John Stultzd7e81c22010-05-07 18:07:38 -0700779 * @scale: Scale factor multiplied against freq to get clocksource hz
780 * @freq: clocksource frequency (cycles per second) divided by scale
781 *
782 * Returns -EBUSY if registration fails, zero otherwise.
783 *
784 * This *SHOULD NOT* be called directly! Please use the
785 * clocksource_register_hz() or clocksource_register_khz helper functions.
786 */
787int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
788{
789
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400790 /* Initialize mult/shift and max_idle_ns */
John Stultz852db462010-07-13 17:56:28 -0700791 __clocksource_updatefreq_scale(cs, scale, freq);
John Stultzd7e81c22010-05-07 18:07:38 -0700792
John Stultz852db462010-07-13 17:56:28 -0700793 /* Add clocksource to the clcoksource list */
John Stultzd7e81c22010-05-07 18:07:38 -0700794 mutex_lock(&clocksource_mutex);
795 clocksource_enqueue(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700796 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700797 clocksource_select();
John Stultzd7e81c22010-05-07 18:07:38 -0700798 mutex_unlock(&clocksource_mutex);
799 return 0;
800}
801EXPORT_SYMBOL_GPL(__clocksource_register_scale);
802
803
john stultz734efb42006-06-26 00:25:05 -0700804/**
john stultza2752542006-06-26 00:25:14 -0700805 * clocksource_register - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900806 * @cs: clocksource to be registered
john stultz734efb42006-06-26 00:25:05 -0700807 *
808 * Returns -EBUSY if registration fails, zero otherwise.
809 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200810int clocksource_register(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700811{
John Stultzd65670a2011-10-31 17:06:35 -0400812 /* calculate max adjustment for given mult/shift */
813 cs->maxadj = clocksource_max_adjustment(cs);
814 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
815 "Clocksource %s might overflow on 11%% adjustment\n",
816 cs->name);
817
Jon Hunter98962462009-08-18 12:45:10 -0500818 /* calculate max idle time permitted for this clocksource */
819 cs->max_idle_ns = clocksource_max_deferment(cs);
820
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200821 mutex_lock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200822 clocksource_enqueue(cs);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200823 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700824 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200825 mutex_unlock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200826 return 0;
john stultz734efb42006-06-26 00:25:05 -0700827}
john stultza2752542006-06-26 00:25:14 -0700828EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700829
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200830static void __clocksource_change_rating(struct clocksource *cs, int rating)
831{
832 list_del(&cs->list);
833 cs->rating = rating;
834 clocksource_enqueue(cs);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200835}
836
john stultz734efb42006-06-26 00:25:05 -0700837/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800838 * clocksource_change_rating - Change the rating of a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900839 * @cs: clocksource to be changed
840 * @rating: new rating
john stultz734efb42006-06-26 00:25:05 -0700841 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800842void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700843{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200844 mutex_lock(&clocksource_mutex);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200845 __clocksource_change_rating(cs, rating);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200846 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200847 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700848}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200849EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700850
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000851/*
852 * Unbind clocksource @cs. Called with clocksource_mutex held
853 */
854static int clocksource_unbind(struct clocksource *cs)
855{
856 /*
857 * I really can't convince myself to support this on hardware
858 * designed by lobotomized monkeys.
859 */
860 if (clocksource_is_watchdog(cs))
861 return -EBUSY;
862
863 if (cs == curr_clocksource) {
864 /* Select and try to install a replacement clock source */
865 clocksource_select_fallback();
866 if (curr_clocksource == cs)
867 return -EBUSY;
868 }
869 clocksource_dequeue_watchdog(cs);
870 list_del_init(&cs->list);
871 return 0;
872}
873
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100874/**
875 * clocksource_unregister - remove a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900876 * @cs: clocksource to be unregistered
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100877 */
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000878int clocksource_unregister(struct clocksource *cs)
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100879{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000880 int ret = 0;
881
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200882 mutex_lock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000883 if (!list_empty(&cs->list))
884 ret = clocksource_unbind(cs);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200885 mutex_unlock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000886 return ret;
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100887}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200888EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100889
Daniel Walker2b013702006-12-10 02:21:30 -0800890#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700891/**
892 * sysfs_show_current_clocksources - sysfs interface for current clocksource
893 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900894 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700895 * @buf: char buffer to be filled with clocksource list
896 *
897 * Provides sysfs interface for listing current clocksource.
898 */
899static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800900sysfs_show_current_clocksources(struct device *dev,
901 struct device_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700902{
Miao Xie5e2cb102008-02-06 01:36:53 -0800903 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700904
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200905 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -0800906 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200907 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700908
Miao Xie5e2cb102008-02-06 01:36:53 -0800909 return count;
john stultz734efb42006-06-26 00:25:05 -0700910}
911
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000912size_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
Thomas Gleixner29b54072013-04-25 20:31:45 +0000913{
914 size_t ret = cnt;
915
916 /* strings from sysfs write are not 0 terminated! */
917 if (!cnt || cnt >= CS_NAME_LEN)
918 return -EINVAL;
919
920 /* strip of \n: */
921 if (buf[cnt-1] == '\n')
922 cnt--;
923 if (cnt > 0)
924 memcpy(dst, buf, cnt);
925 dst[cnt] = 0;
926 return ret;
927}
928
john stultz734efb42006-06-26 00:25:05 -0700929/**
930 * sysfs_override_clocksource - interface for manually overriding clocksource
931 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900932 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700933 * @buf: name of override clocksource
934 * @count: length of buffer
935 *
936 * Takes input from sysfs interface for manually overriding the default
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200937 * clocksource selection.
john stultz734efb42006-06-26 00:25:05 -0700938 */
Kay Sieversd369a5d2011-12-14 15:28:51 -0800939static ssize_t sysfs_override_clocksource(struct device *dev,
940 struct device_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700941 const char *buf, size_t count)
942{
Elad Wexler233bcb42013-09-12 13:28:54 +0300943 ssize_t ret;
john stultz734efb42006-06-26 00:25:05 -0700944
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200945 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700946
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000947 ret = sysfs_get_uname(buf, override_name, count);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000948 if (ret >= 0)
949 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700950
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200951 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700952
953 return ret;
954}
955
956/**
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000957 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
958 * @dev: unused
959 * @attr: unused
960 * @buf: unused
961 * @count: length of buffer
962 *
963 * Takes input from sysfs interface for manually unbinding a clocksource.
964 */
965static ssize_t sysfs_unbind_clocksource(struct device *dev,
966 struct device_attribute *attr,
967 const char *buf, size_t count)
968{
969 struct clocksource *cs;
970 char name[CS_NAME_LEN];
Elad Wexler233bcb42013-09-12 13:28:54 +0300971 ssize_t ret;
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000972
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000973 ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000974 if (ret < 0)
975 return ret;
976
977 ret = -ENODEV;
978 mutex_lock(&clocksource_mutex);
979 list_for_each_entry(cs, &clocksource_list, list) {
980 if (strcmp(cs->name, name))
981 continue;
982 ret = clocksource_unbind(cs);
983 break;
984 }
985 mutex_unlock(&clocksource_mutex);
986
987 return ret ? ret : count;
988}
989
990/**
john stultz734efb42006-06-26 00:25:05 -0700991 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
992 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900993 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700994 * @buf: char buffer to be filled with clocksource list
995 *
996 * Provides sysfs interface for listing registered clocksources
997 */
998static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800999sysfs_show_available_clocksources(struct device *dev,
1000 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001001 char *buf)
john stultz734efb42006-06-26 00:25:05 -07001002{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -07001003 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -08001004 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -07001005
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001006 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -07001007 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +02001008 /*
1009 * Don't show non-HRES clocksource if the tick code is
1010 * in one shot mode (highres=on or nohz=on)
1011 */
1012 if (!tick_oneshot_mode_active() ||
1013 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -07001014 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -08001015 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1016 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -07001017 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001018 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001019
Miao Xie5e2cb102008-02-06 01:36:53 -08001020 count += snprintf(buf + count,
1021 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -07001022
Miao Xie5e2cb102008-02-06 01:36:53 -08001023 return count;
john stultz734efb42006-06-26 00:25:05 -07001024}
1025
1026/*
1027 * Sysfs setup bits:
1028 */
Kay Sieversd369a5d2011-12-14 15:28:51 -08001029static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001030 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001031
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001032static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
1033
Kay Sieversd369a5d2011-12-14 15:28:51 -08001034static DEVICE_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001035 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -07001036
Kay Sieversd369a5d2011-12-14 15:28:51 -08001037static struct bus_type clocksource_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001038 .name = "clocksource",
Kay Sieversd369a5d2011-12-14 15:28:51 -08001039 .dev_name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -07001040};
1041
Kay Sieversd369a5d2011-12-14 15:28:51 -08001042static struct device device_clocksource = {
john stultz734efb42006-06-26 00:25:05 -07001043 .id = 0,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001044 .bus = &clocksource_subsys,
john stultz734efb42006-06-26 00:25:05 -07001045};
1046
john stultzad596172006-06-26 00:25:06 -07001047static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -07001048{
Kay Sieversd369a5d2011-12-14 15:28:51 -08001049 int error = subsys_system_register(&clocksource_subsys, NULL);
john stultz734efb42006-06-26 00:25:05 -07001050
1051 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001052 error = device_register(&device_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001053 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001054 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -07001055 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001056 &dev_attr_current_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001057 if (!error)
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001058 error = device_create_file(&device_clocksource,
1059 &dev_attr_unbind_clocksource);
1060 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001061 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -07001062 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001063 &dev_attr_available_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001064 return error;
1065}
1066
1067device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -08001068#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -07001069
1070/**
1071 * boot_override_clocksource - boot clock override
1072 * @str: override name
1073 *
1074 * Takes a clocksource= boot argument and uses it
1075 * as the clocksource override name.
1076 */
1077static int __init boot_override_clocksource(char* str)
1078{
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001079 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001080 if (str)
1081 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001082 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001083 return 1;
1084}
1085
1086__setup("clocksource=", boot_override_clocksource);
1087
1088/**
1089 * boot_override_clock - Compatibility layer for deprecated boot option
1090 * @str: override name
1091 *
1092 * DEPRECATED! Takes a clock= boot argument and uses it
1093 * as the clocksource override name
1094 */
1095static int __init boot_override_clock(char* str)
1096{
john stultz5d0cf412006-06-26 00:25:12 -07001097 if (!strcmp(str, "pmtmr")) {
1098 printk("Warning: clock=pmtmr is deprecated. "
1099 "Use clocksource=acpi_pm.\n");
1100 return boot_override_clocksource("acpi_pm");
1101 }
1102 printk("Warning! clock= boot option is deprecated. "
1103 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -07001104 return boot_override_clocksource(str);
1105}
1106
1107__setup("clock=", boot_override_clock);