blob: b79f39bda7e111436be3b8245f26e77407fdab97 [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"
Thomas Gleixner3a978372014-07-16 21:05:10 +000035#include "timekeeping_internal.h"
Thomas Gleixner03e13cf2013-04-25 20:31:50 +000036
Patrick Ohlya038a352009-02-12 05:03:34 +000037void timecounter_init(struct timecounter *tc,
38 const struct cyclecounter *cc,
39 u64 start_tstamp)
40{
41 tc->cc = cc;
42 tc->cycle_last = cc->read(cc);
43 tc->nsec = start_tstamp;
44}
David S. Miller3586e0a2009-11-11 19:06:30 -080045EXPORT_SYMBOL_GPL(timecounter_init);
Patrick Ohlya038a352009-02-12 05:03:34 +000046
47/**
48 * timecounter_read_delta - get nanoseconds since last call of this function
49 * @tc: Pointer to time counter
50 *
51 * When the underlying cycle counter runs over, this will be handled
52 * correctly as long as it does not run over more than once between
53 * calls.
54 *
55 * The first call to this function for a new time counter initializes
56 * the time tracking and returns an undefined result.
57 */
58static u64 timecounter_read_delta(struct timecounter *tc)
59{
60 cycle_t cycle_now, cycle_delta;
61 u64 ns_offset;
62
63 /* read cycle counter: */
64 cycle_now = tc->cc->read(tc->cc);
65
66 /* calculate the delta since the last timecounter_read_delta(): */
67 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
68
69 /* convert to nanoseconds: */
70 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
71
72 /* update time stamp of timecounter_read_delta() call: */
73 tc->cycle_last = cycle_now;
74
75 return ns_offset;
76}
77
78u64 timecounter_read(struct timecounter *tc)
79{
80 u64 nsec;
81
82 /* increment time by nanoseconds since last call */
83 nsec = timecounter_read_delta(tc);
84 nsec += tc->nsec;
85 tc->nsec = nsec;
86
87 return nsec;
88}
David S. Miller3586e0a2009-11-11 19:06:30 -080089EXPORT_SYMBOL_GPL(timecounter_read);
Patrick Ohlya038a352009-02-12 05:03:34 +000090
91u64 timecounter_cyc2time(struct timecounter *tc,
92 cycle_t cycle_tstamp)
93{
94 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
95 u64 nsec;
96
97 /*
98 * Instead of always treating cycle_tstamp as more recent
99 * than tc->cycle_last, detect when it is too far in the
100 * future and treat it as old time stamp instead.
101 */
102 if (cycle_delta > tc->cc->mask / 2) {
103 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
104 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
105 } else {
106 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
107 }
108
109 return nsec;
110}
David S. Miller3586e0a2009-11-11 19:06:30 -0800111EXPORT_SYMBOL_GPL(timecounter_cyc2time);
Patrick Ohlya038a352009-02-12 05:03:34 +0000112
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000113/**
114 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
115 * @mult: pointer to mult variable
116 * @shift: pointer to shift variable
117 * @from: frequency to convert from
118 * @to: frequency to convert to
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500119 * @maxsec: guaranteed runtime conversion range in seconds
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000120 *
121 * The function evaluates the shift/mult pair for the scaled math
122 * operations of clocksources and clockevents.
123 *
124 * @to and @from are frequency values in HZ. For clock sources @to is
125 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
126 * event @to is the counter frequency and @from is NSEC_PER_SEC.
127 *
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500128 * The @maxsec conversion range argument controls the time frame in
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000129 * seconds which must be covered by the runtime conversion with the
130 * calculated mult and shift factors. This guarantees that no 64bit
131 * overflow happens when the input value of the conversion is
132 * multiplied with the calculated mult factor. Larger ranges may
133 * reduce the conversion accuracy by chosing smaller mult and shift
134 * factors.
135 */
136void
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500137clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000138{
139 u64 tmp;
140 u32 sft, sftacc= 32;
141
142 /*
143 * Calculate the shift factor which is limiting the conversion
144 * range:
145 */
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500146 tmp = ((u64)maxsec * from) >> 32;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000147 while (tmp) {
148 tmp >>=1;
149 sftacc--;
150 }
151
152 /*
153 * Find the conversion shift/mult pair which has the best
154 * accuracy and fits the maxsec conversion range:
155 */
156 for (sft = 32; sft > 0; sft--) {
157 tmp = (u64) to << sft;
john stultzb5776c42010-12-16 19:03:27 +0000158 tmp += from / 2;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000159 do_div(tmp, from);
160 if ((tmp >> sftacc) == 0)
161 break;
162 }
163 *mult = tmp;
164 *shift = sft;
165}
166
john stultz734efb42006-06-26 00:25:05 -0700167/*[Clocksource internal variables]---------
168 * curr_clocksource:
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200169 * currently selected clocksource.
john stultz734efb42006-06-26 00:25:05 -0700170 * clocksource_list:
171 * linked list with the registered clocksources
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200172 * clocksource_mutex:
173 * protects manipulations to curr_clocksource and the clocksource_list
john stultz734efb42006-06-26 00:25:05 -0700174 * override_name:
175 * Name of the user-specified clocksource.
176 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200177static struct clocksource *curr_clocksource;
john stultz734efb42006-06-26 00:25:05 -0700178static LIST_HEAD(clocksource_list);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200179static DEFINE_MUTEX(clocksource_mutex);
Thomas Gleixner29b540782013-04-25 20:31:45 +0000180static char override_name[CS_NAME_LEN];
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200181static int finished_booting;
john stultz734efb42006-06-26 00:25:05 -0700182
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800183#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200184static void clocksource_watchdog_work(struct work_struct *work);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200185static void clocksource_select(void);
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200186
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800187static LIST_HEAD(watchdog_list);
188static struct clocksource *watchdog;
189static struct timer_list watchdog_timer;
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200190static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800191static DEFINE_SPINLOCK(watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200192static int watchdog_running;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200193static atomic_t watchdog_reset_pending;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700194
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200195static int clocksource_watchdog_kthread(void *data);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200196static void __clocksource_change_rating(struct clocksource *cs, int rating);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200197
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800198/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700199 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800200 */
201#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700202#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800203
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200204static void clocksource_watchdog_work(struct work_struct *work)
205{
206 /*
207 * If kthread_run fails the next watchdog scan over the
208 * watchdog_list will find the unstable clock again.
209 */
210 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
211}
212
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200213static void __clocksource_unstable(struct clocksource *cs)
214{
215 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
216 cs->flags |= CLOCK_SOURCE_UNSTABLE;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200217 if (finished_booting)
218 schedule_work(&watchdog_work);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200219}
220
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200221static void clocksource_unstable(struct clocksource *cs, int64_t delta)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800222{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800223 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
224 cs->name, delta);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200225 __clocksource_unstable(cs);
226}
227
228/**
229 * clocksource_mark_unstable - mark clocksource unstable via watchdog
230 * @cs: clocksource to be marked unstable
231 *
232 * This function is called instead of clocksource_change_rating from
233 * cpu hotplug code to avoid a deadlock between the clocksource mutex
234 * and the cpu hotplug mutex. It defers the update of the clocksource
235 * to the watchdog thread.
236 */
237void clocksource_mark_unstable(struct clocksource *cs)
238{
239 unsigned long flags;
240
241 spin_lock_irqsave(&watchdog_lock, flags);
242 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
243 if (list_empty(&cs->wd_list))
244 list_add(&cs->wd_list, &watchdog_list);
245 __clocksource_unstable(cs);
246 }
247 spin_unlock_irqrestore(&watchdog_lock, flags);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800248}
249
250static void clocksource_watchdog(unsigned long data)
251{
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200252 struct clocksource *cs;
Thomas Gleixner3a978372014-07-16 21:05:10 +0000253 cycle_t csnow, wdnow, delta;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800254 int64_t wd_nsec, cs_nsec;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200255 int next_cpu, reset_pending;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800256
257 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200258 if (!watchdog_running)
259 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800260
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200261 reset_pending = atomic_read(&watchdog_reset_pending);
262
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200263 list_for_each_entry(cs, &watchdog_list, wd_list) {
264
265 /* Clocksource already marked unstable? */
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200266 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200267 if (finished_booting)
268 schedule_work(&watchdog_work);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200269 continue;
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200270 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200271
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200272 local_irq_disable();
Magnus Damm8e196082009-04-21 12:24:00 -0700273 csnow = cs->read(cs);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200274 wdnow = watchdog->read(watchdog);
275 local_irq_enable();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700276
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200277 /* Clocksource initialized ? */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200278 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
279 atomic_read(&watchdog_reset_pending)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200280 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200281 cs->wd_last = wdnow;
282 cs->cs_last = csnow;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700283 continue;
284 }
285
Thomas Gleixner3a978372014-07-16 21:05:10 +0000286 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
287 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
288 watchdog->shift);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200289
Thomas Gleixner3a978372014-07-16 21:05:10 +0000290 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
291 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200292 cs->cs_last = csnow;
293 cs->wd_last = wdnow;
294
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200295 if (atomic_read(&watchdog_reset_pending))
296 continue;
297
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200298 /* Check the deviation from the watchdog clocksource. */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200299 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200300 clocksource_unstable(cs, cs_nsec - wd_nsec);
301 continue;
302 }
303
304 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
305 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
306 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
Thomas Gleixner332962f2013-07-04 22:46:45 +0200307 /* Mark it valid for high-res. */
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200308 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner332962f2013-07-04 22:46:45 +0200309
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200310 /*
Thomas Gleixner332962f2013-07-04 22:46:45 +0200311 * clocksource_done_booting() will sort it if
312 * finished_booting is not set yet.
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200313 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200314 if (!finished_booting)
315 continue;
316
317 /*
318 * If this is not the current clocksource let
319 * the watchdog thread reselect it. Due to the
320 * change to high res this clocksource might
321 * be preferred now. If it is the current
322 * clocksource let the tick code know about
323 * that change.
324 */
325 if (cs != curr_clocksource) {
326 cs->flags |= CLOCK_SOURCE_RESELECT;
327 schedule_work(&watchdog_work);
328 } else {
329 tick_clock_notify();
330 }
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800331 }
332 }
333
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200334 /*
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200335 * We only clear the watchdog_reset_pending, when we did a
336 * full cycle through all clocksources.
337 */
338 if (reset_pending)
339 atomic_dec(&watchdog_reset_pending);
340
341 /*
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200342 * Cycle through CPUs to check if the CPUs stay synchronized
343 * to each other.
344 */
345 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
346 if (next_cpu >= nr_cpu_ids)
347 next_cpu = cpumask_first(cpu_online_mask);
348 watchdog_timer.expires += WATCHDOG_INTERVAL;
349 add_timer_on(&watchdog_timer, next_cpu);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200350out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800351 spin_unlock(&watchdog_lock);
352}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200353
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200354static inline void clocksource_start_watchdog(void)
355{
356 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
357 return;
358 init_timer(&watchdog_timer);
359 watchdog_timer.function = clocksource_watchdog;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200360 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
361 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
362 watchdog_running = 1;
363}
364
365static inline void clocksource_stop_watchdog(void)
366{
367 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
368 return;
369 del_timer(&watchdog_timer);
370 watchdog_running = 0;
371}
372
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200373static inline void clocksource_reset_watchdog(void)
374{
375 struct clocksource *cs;
376
377 list_for_each_entry(cs, &watchdog_list, wd_list)
378 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
379}
380
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700381static void clocksource_resume_watchdog(void)
382{
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200383 atomic_inc(&watchdog_reset_pending);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700384}
385
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200386static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800387{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800388 unsigned long flags;
389
390 spin_lock_irqsave(&watchdog_lock, flags);
391 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200392 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800393 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200394 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200395 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200396 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200397 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800398 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200399 /* Pick the best watchdog. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800400 if (!watchdog || cs->rating > watchdog->rating) {
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800401 watchdog = cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800402 /* Reset watchdog cycles */
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200403 clocksource_reset_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800404 }
405 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200406 /* Check if the watchdog timer needs to be started. */
407 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800408 spin_unlock_irqrestore(&watchdog_lock, flags);
409}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200410
411static void clocksource_dequeue_watchdog(struct clocksource *cs)
412{
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200413 unsigned long flags;
414
415 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000416 if (cs != watchdog) {
417 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
418 /* cs is a watched clocksource. */
419 list_del_init(&cs->wd_list);
420 /* Check if the watchdog timer needs to be stopped. */
421 clocksource_stop_watchdog();
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200422 }
423 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200424 spin_unlock_irqrestore(&watchdog_lock, flags);
425}
426
Thomas Gleixner332962f2013-07-04 22:46:45 +0200427static int __clocksource_watchdog_kthread(void)
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200428{
429 struct clocksource *cs, *tmp;
430 unsigned long flags;
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200431 LIST_HEAD(unstable);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200432 int select = 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200433
434 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200435 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200436 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
437 list_del_init(&cs->wd_list);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200438 list_add(&cs->wd_list, &unstable);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200439 select = 1;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200440 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200441 if (cs->flags & CLOCK_SOURCE_RESELECT) {
442 cs->flags &= ~CLOCK_SOURCE_RESELECT;
443 select = 1;
444 }
445 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200446 /* Check if the watchdog timer needs to be stopped. */
447 clocksource_stop_watchdog();
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200448 spin_unlock_irqrestore(&watchdog_lock, flags);
449
450 /* Needs to be done outside of watchdog lock */
451 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
452 list_del_init(&cs->wd_list);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200453 __clocksource_change_rating(cs, 0);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200454 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200455 return select;
456}
457
458static int clocksource_watchdog_kthread(void *data)
459{
460 mutex_lock(&clocksource_mutex);
461 if (__clocksource_watchdog_kthread())
462 clocksource_select();
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200463 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200464 return 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200465}
466
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000467static bool clocksource_is_watchdog(struct clocksource *cs)
468{
469 return cs == watchdog;
470}
471
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200472#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
473
474static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800475{
476 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
477 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
478}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700479
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200480static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700481static inline void clocksource_resume_watchdog(void) { }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200482static inline int __clocksource_watchdog_kthread(void) { return 0; }
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000483static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
Prarit Bhargava397bbf62013-02-22 15:08:56 -0500484void clocksource_mark_unstable(struct clocksource *cs) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200485
486#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800487
john stultz734efb42006-06-26 00:25:05 -0700488/**
Magnus Dammc54a42b2010-02-02 14:41:41 -0800489 * clocksource_suspend - suspend the clocksource(s)
490 */
491void clocksource_suspend(void)
492{
493 struct clocksource *cs;
494
495 list_for_each_entry_reverse(cs, &clocksource_list, list)
496 if (cs->suspend)
497 cs->suspend(cs);
498}
499
500/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700501 * clocksource_resume - resume the clocksource(s)
502 */
503void clocksource_resume(void)
504{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700505 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700506
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200507 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700508 if (cs->resume)
Magnus Damm17622332010-02-02 14:41:39 -0800509 cs->resume(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700510
511 clocksource_resume_watchdog();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700512}
513
514/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600515 * clocksource_touch_watchdog - Update watchdog
516 *
517 * Update the watchdog after exception contexts such as kgdb so as not
Thomas Gleixner7b7422a2010-01-26 12:51:10 +0100518 * to incorrectly trip the watchdog. This might fail when the kernel
519 * was stopped in code which holds watchdog_lock.
Jason Wessel7c3078b2008-02-15 14:55:54 -0600520 */
521void clocksource_touch_watchdog(void)
522{
523 clocksource_resume_watchdog();
524}
525
john stultz734efb42006-06-26 00:25:05 -0700526/**
John Stultzd65670a2011-10-31 17:06:35 -0400527 * clocksource_max_adjustment- Returns max adjustment amount
528 * @cs: Pointer to clocksource
529 *
530 */
531static u32 clocksource_max_adjustment(struct clocksource *cs)
532{
533 u64 ret;
534 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600535 * We won't try to correct for more than 11% adjustments (110,000 ppm),
John Stultzd65670a2011-10-31 17:06:35 -0400536 */
537 ret = (u64)cs->mult * 11;
538 do_div(ret,100);
539 return (u32)ret;
540}
541
542/**
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700543 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
544 * @mult: cycle to nanosecond multiplier
545 * @shift: cycle to nanosecond divisor (power of two)
546 * @maxadj: maximum adjustment value to mult (~11%)
547 * @mask: bitmask for two's complement subtraction of non 64 bit counters
Jon Hunter98962462009-08-18 12:45:10 -0500548 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700549u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask)
Jon Hunter98962462009-08-18 12:45:10 -0500550{
551 u64 max_nsecs, max_cycles;
552
553 /*
554 * Calculate the maximum number of cycles that we can pass to the
555 * cyc2ns function without overflowing a 64-bit signed result. The
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700556 * maximum number of cycles is equal to ULLONG_MAX/(mult+maxadj)
John Stultzd65670a2011-10-31 17:06:35 -0400557 * which is equivalent to the below.
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700558 * max_cycles < (2^63)/(mult + maxadj)
559 * max_cycles < 2^(log2((2^63)/(mult + maxadj)))
560 * max_cycles < 2^(log2(2^63) - log2(mult + maxadj))
561 * max_cycles < 2^(63 - log2(mult + maxadj))
562 * max_cycles < 1 << (63 - log2(mult + maxadj))
Jon Hunter98962462009-08-18 12:45:10 -0500563 * Please note that we add 1 to the result of the log2 to account for
564 * any rounding errors, ensure the above inequality is satisfied and
565 * no overflow will occur.
566 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700567 max_cycles = 1ULL << (63 - (ilog2(mult + maxadj) + 1));
Jon Hunter98962462009-08-18 12:45:10 -0500568
569 /*
570 * The actual maximum number of cycles we can defer the clocksource is
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700571 * determined by the minimum of max_cycles and mask.
John Stultzd65670a2011-10-31 17:06:35 -0400572 * Note: Here we subtract the maxadj to make sure we don't sleep for
573 * too long if there's a large negative adjustment.
Jon Hunter98962462009-08-18 12:45:10 -0500574 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700575 max_cycles = min(max_cycles, mask);
576 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
Jon Hunter98962462009-08-18 12:45:10 -0500577
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700578 return max_nsecs;
579}
580
581/**
582 * clocksource_max_deferment - Returns max time the clocksource can be deferred
583 * @cs: Pointer to clocksource
584 *
585 */
586static u64 clocksource_max_deferment(struct clocksource *cs)
587{
588 u64 max_nsecs;
589
590 max_nsecs = clocks_calc_max_nsecs(cs->mult, cs->shift, cs->maxadj,
591 cs->mask);
Jon Hunter98962462009-08-18 12:45:10 -0500592 /*
593 * To ensure that the clocksource does not wrap whilst we are idle,
594 * limit the time the clocksource can be deferred by 12.5%. Please
595 * note a margin of 12.5% is used because this can be computed with
596 * a shift, versus say 10% which would require division.
597 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500598 return max_nsecs - (max_nsecs >> 3);
Jon Hunter98962462009-08-18 12:45:10 -0500599}
600
John Stultz592913e2010-07-13 17:56:20 -0700601#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
john stultz734efb42006-06-26 00:25:05 -0700602
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000603static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000604{
605 struct clocksource *cs;
606
607 if (!finished_booting || list_empty(&clocksource_list))
608 return NULL;
609
610 /*
611 * We pick the clocksource with the highest rating. If oneshot
612 * mode is active, we pick the highres valid clocksource with
613 * the best rating.
614 */
615 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000616 if (skipcur && cs == curr_clocksource)
617 continue;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000618 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
619 continue;
620 return cs;
621 }
622 return NULL;
623}
624
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000625static void __clocksource_select(bool skipcur)
john stultz734efb42006-06-26 00:25:05 -0700626{
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000627 bool oneshot = tick_oneshot_mode_active();
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200628 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800629
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000630 /* Find the best suitable clocksource */
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000631 best = clocksource_find_best(oneshot, skipcur);
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000632 if (!best)
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200633 return;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000634
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200635 /* Check for the override clocksource. */
636 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000637 if (skipcur && cs == curr_clocksource)
638 continue;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200639 if (strcmp(cs->name, override_name) != 0)
640 continue;
641 /*
642 * Check to make sure we don't switch to a non-highres
643 * capable clocksource if the tick code is in oneshot
644 * mode (highres or nohz)
645 */
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000646 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200647 /* Override clocksource cannot be used. */
648 printk(KERN_WARNING "Override clocksource %s is not "
649 "HRT compatible. Cannot switch while in "
650 "HRT/NOHZ mode\n", cs->name);
651 override_name[0] = 0;
652 } else
653 /* Override clocksource can be used. */
654 best = cs;
655 break;
656 }
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000657
658 if (curr_clocksource != best && !timekeeping_notify(best)) {
659 pr_info("Switched to clocksource %s\n", best->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200660 curr_clocksource = best;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200661 }
john stultz734efb42006-06-26 00:25:05 -0700662}
663
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000664/**
665 * clocksource_select - Select the best clocksource available
666 *
667 * Private function. Must hold clocksource_mutex when called.
668 *
669 * Select the clocksource with the best rating, or the clocksource,
670 * which is selected by userspace override.
671 */
672static void clocksource_select(void)
673{
674 return __clocksource_select(false);
675}
676
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000677static void clocksource_select_fallback(void)
678{
679 return __clocksource_select(true);
680}
681
John Stultz592913e2010-07-13 17:56:20 -0700682#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200683
684static inline void clocksource_select(void) { }
Thomas Gleixner1eaff672013-05-28 09:48:46 +0200685static inline void clocksource_select_fallback(void) { }
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200686
687#endif
688
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200689/*
690 * clocksource_done_booting - Called near the end of core bootup
691 *
692 * Hack to avoid lots of clocksource churn at boot time.
693 * We use fs_initcall because we want this to start before
694 * device_initcall but after subsys_initcall.
695 */
696static int __init clocksource_done_booting(void)
697{
john stultzad6759f2010-03-01 12:34:43 -0800698 mutex_lock(&clocksource_mutex);
699 curr_clocksource = clocksource_default_clock();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200700 finished_booting = 1;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200701 /*
702 * Run the watchdog first to eliminate unstable clock sources
703 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200704 __clocksource_watchdog_kthread();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200705 clocksource_select();
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200706 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200707 return 0;
708}
709fs_initcall(clocksource_done_booting);
710
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800711/*
712 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700713 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200714static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700715{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200716 struct list_head *entry = &clocksource_list;
717 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700718
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200719 list_for_each_entry(tmp, &clocksource_list, list)
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800720 /* Keep track of the place, where to insert */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200721 if (tmp->rating >= cs->rating)
722 entry = &tmp->list;
723 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700724}
725
John Stultzd7e81c22010-05-07 18:07:38 -0700726/**
John Stultz852db462010-07-13 17:56:28 -0700727 * __clocksource_updatefreq_scale - Used update clocksource with new freq
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900728 * @cs: clocksource to be registered
John Stultz852db462010-07-13 17:56:28 -0700729 * @scale: Scale factor multiplied against freq to get clocksource hz
730 * @freq: clocksource frequency (cycles per second) divided by scale
731 *
732 * This should only be called from the clocksource->enable() method.
733 *
734 * This *SHOULD NOT* be called directly! Please use the
735 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
736 */
737void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
738{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200739 u64 sec;
John Stultz852db462010-07-13 17:56:28 -0700740 /*
Thomas Gleixner724ed532011-05-18 21:33:40 +0000741 * Calc the maximum number of seconds which we can run before
742 * wrapping around. For clocksources which have a mask > 32bit
743 * we need to limit the max sleep time to have a good
744 * conversion precision. 10 minutes is still a reasonable
745 * amount. That results in a shift value of 24 for a
746 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
747 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
748 * margin as we do in clocksource_max_deferment()
John Stultz852db462010-07-13 17:56:28 -0700749 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500750 sec = (cs->mask - (cs->mask >> 3));
Thomas Gleixner724ed532011-05-18 21:33:40 +0000751 do_div(sec, freq);
752 do_div(sec, scale);
753 if (!sec)
754 sec = 1;
755 else if (sec > 600 && cs->mask > UINT_MAX)
756 sec = 600;
757
John Stultz852db462010-07-13 17:56:28 -0700758 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
Thomas Gleixner724ed532011-05-18 21:33:40 +0000759 NSEC_PER_SEC / scale, sec * scale);
John Stultzd65670a2011-10-31 17:06:35 -0400760
761 /*
762 * for clocksources that have large mults, to avoid overflow.
763 * Since mult may be adjusted by ntp, add an safety extra margin
764 *
765 */
766 cs->maxadj = clocksource_max_adjustment(cs);
767 while ((cs->mult + cs->maxadj < cs->mult)
768 || (cs->mult - cs->maxadj > cs->mult)) {
769 cs->mult >>= 1;
770 cs->shift--;
771 cs->maxadj = clocksource_max_adjustment(cs);
772 }
773
John Stultz852db462010-07-13 17:56:28 -0700774 cs->max_idle_ns = clocksource_max_deferment(cs);
775}
776EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
777
778/**
John Stultzd7e81c22010-05-07 18:07:38 -0700779 * __clocksource_register_scale - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900780 * @cs: clocksource to be registered
John Stultzd7e81c22010-05-07 18:07:38 -0700781 * @scale: Scale factor multiplied against freq to get clocksource hz
782 * @freq: clocksource frequency (cycles per second) divided by scale
783 *
784 * Returns -EBUSY if registration fails, zero otherwise.
785 *
786 * This *SHOULD NOT* be called directly! Please use the
787 * clocksource_register_hz() or clocksource_register_khz helper functions.
788 */
789int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
790{
791
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400792 /* Initialize mult/shift and max_idle_ns */
John Stultz852db462010-07-13 17:56:28 -0700793 __clocksource_updatefreq_scale(cs, scale, freq);
John Stultzd7e81c22010-05-07 18:07:38 -0700794
James Hartleybe278e92014-09-18 15:59:07 +0100795 /* Add clocksource to the clocksource list */
John Stultzd7e81c22010-05-07 18:07:38 -0700796 mutex_lock(&clocksource_mutex);
797 clocksource_enqueue(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700798 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700799 clocksource_select();
John Stultzd7e81c22010-05-07 18:07:38 -0700800 mutex_unlock(&clocksource_mutex);
801 return 0;
802}
803EXPORT_SYMBOL_GPL(__clocksource_register_scale);
804
805
john stultz734efb42006-06-26 00:25:05 -0700806/**
john stultza2752542006-06-26 00:25:14 -0700807 * clocksource_register - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900808 * @cs: clocksource to be registered
john stultz734efb42006-06-26 00:25:05 -0700809 *
810 * Returns -EBUSY if registration fails, zero otherwise.
811 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200812int clocksource_register(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700813{
John Stultzd65670a2011-10-31 17:06:35 -0400814 /* calculate max adjustment for given mult/shift */
815 cs->maxadj = clocksource_max_adjustment(cs);
816 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
817 "Clocksource %s might overflow on 11%% adjustment\n",
818 cs->name);
819
Jon Hunter98962462009-08-18 12:45:10 -0500820 /* calculate max idle time permitted for this clocksource */
821 cs->max_idle_ns = clocksource_max_deferment(cs);
822
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200823 mutex_lock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200824 clocksource_enqueue(cs);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200825 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700826 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200827 mutex_unlock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200828 return 0;
john stultz734efb42006-06-26 00:25:05 -0700829}
john stultza2752542006-06-26 00:25:14 -0700830EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700831
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200832static void __clocksource_change_rating(struct clocksource *cs, int rating)
833{
834 list_del(&cs->list);
835 cs->rating = rating;
836 clocksource_enqueue(cs);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200837}
838
john stultz734efb42006-06-26 00:25:05 -0700839/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800840 * clocksource_change_rating - Change the rating of a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900841 * @cs: clocksource to be changed
842 * @rating: new rating
john stultz734efb42006-06-26 00:25:05 -0700843 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800844void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700845{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200846 mutex_lock(&clocksource_mutex);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200847 __clocksource_change_rating(cs, rating);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200848 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200849 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700850}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200851EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700852
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000853/*
854 * Unbind clocksource @cs. Called with clocksource_mutex held
855 */
856static int clocksource_unbind(struct clocksource *cs)
857{
858 /*
859 * I really can't convince myself to support this on hardware
860 * designed by lobotomized monkeys.
861 */
862 if (clocksource_is_watchdog(cs))
863 return -EBUSY;
864
865 if (cs == curr_clocksource) {
866 /* Select and try to install a replacement clock source */
867 clocksource_select_fallback();
868 if (curr_clocksource == cs)
869 return -EBUSY;
870 }
871 clocksource_dequeue_watchdog(cs);
872 list_del_init(&cs->list);
873 return 0;
874}
875
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100876/**
877 * clocksource_unregister - remove a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900878 * @cs: clocksource to be unregistered
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100879 */
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000880int clocksource_unregister(struct clocksource *cs)
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100881{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000882 int ret = 0;
883
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200884 mutex_lock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000885 if (!list_empty(&cs->list))
886 ret = clocksource_unbind(cs);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200887 mutex_unlock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000888 return ret;
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100889}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200890EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100891
Daniel Walker2b013702006-12-10 02:21:30 -0800892#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700893/**
894 * sysfs_show_current_clocksources - sysfs interface for current clocksource
895 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900896 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700897 * @buf: char buffer to be filled with clocksource list
898 *
899 * Provides sysfs interface for listing current clocksource.
900 */
901static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800902sysfs_show_current_clocksources(struct device *dev,
903 struct device_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700904{
Miao Xie5e2cb102008-02-06 01:36:53 -0800905 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700906
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200907 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -0800908 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200909 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700910
Miao Xie5e2cb102008-02-06 01:36:53 -0800911 return count;
john stultz734efb42006-06-26 00:25:05 -0700912}
913
Patrick Palka891292a2013-10-11 13:11:55 -0400914ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
Thomas Gleixner29b540782013-04-25 20:31:45 +0000915{
916 size_t ret = cnt;
917
918 /* strings from sysfs write are not 0 terminated! */
919 if (!cnt || cnt >= CS_NAME_LEN)
920 return -EINVAL;
921
922 /* strip of \n: */
923 if (buf[cnt-1] == '\n')
924 cnt--;
925 if (cnt > 0)
926 memcpy(dst, buf, cnt);
927 dst[cnt] = 0;
928 return ret;
929}
930
john stultz734efb42006-06-26 00:25:05 -0700931/**
932 * sysfs_override_clocksource - interface for manually overriding clocksource
933 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900934 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700935 * @buf: name of override clocksource
936 * @count: length of buffer
937 *
938 * Takes input from sysfs interface for manually overriding the default
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200939 * clocksource selection.
john stultz734efb42006-06-26 00:25:05 -0700940 */
Kay Sieversd369a5d2011-12-14 15:28:51 -0800941static ssize_t sysfs_override_clocksource(struct device *dev,
942 struct device_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700943 const char *buf, size_t count)
944{
Elad Wexler233bcb42013-09-12 13:28:54 +0300945 ssize_t ret;
john stultz734efb42006-06-26 00:25:05 -0700946
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200947 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700948
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000949 ret = sysfs_get_uname(buf, override_name, count);
Thomas Gleixner29b540782013-04-25 20:31:45 +0000950 if (ret >= 0)
951 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700952
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200953 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700954
955 return ret;
956}
957
958/**
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000959 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
960 * @dev: unused
961 * @attr: unused
962 * @buf: unused
963 * @count: length of buffer
964 *
965 * Takes input from sysfs interface for manually unbinding a clocksource.
966 */
967static ssize_t sysfs_unbind_clocksource(struct device *dev,
968 struct device_attribute *attr,
969 const char *buf, size_t count)
970{
971 struct clocksource *cs;
972 char name[CS_NAME_LEN];
Elad Wexler233bcb42013-09-12 13:28:54 +0300973 ssize_t ret;
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000974
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000975 ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000976 if (ret < 0)
977 return ret;
978
979 ret = -ENODEV;
980 mutex_lock(&clocksource_mutex);
981 list_for_each_entry(cs, &clocksource_list, list) {
982 if (strcmp(cs->name, name))
983 continue;
984 ret = clocksource_unbind(cs);
985 break;
986 }
987 mutex_unlock(&clocksource_mutex);
988
989 return ret ? ret : count;
990}
991
992/**
john stultz734efb42006-06-26 00:25:05 -0700993 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
994 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900995 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700996 * @buf: char buffer to be filled with clocksource list
997 *
998 * Provides sysfs interface for listing registered clocksources
999 */
1000static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -08001001sysfs_show_available_clocksources(struct device *dev,
1002 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001003 char *buf)
john stultz734efb42006-06-26 00:25:05 -07001004{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -07001005 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -08001006 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -07001007
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001008 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -07001009 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +02001010 /*
1011 * Don't show non-HRES clocksource if the tick code is
1012 * in one shot mode (highres=on or nohz=on)
1013 */
1014 if (!tick_oneshot_mode_active() ||
1015 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -07001016 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -08001017 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1018 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -07001019 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001020 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001021
Miao Xie5e2cb102008-02-06 01:36:53 -08001022 count += snprintf(buf + count,
1023 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -07001024
Miao Xie5e2cb102008-02-06 01:36:53 -08001025 return count;
john stultz734efb42006-06-26 00:25:05 -07001026}
1027
1028/*
1029 * Sysfs setup bits:
1030 */
Kay Sieversd369a5d2011-12-14 15:28:51 -08001031static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001032 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001033
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001034static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
1035
Kay Sieversd369a5d2011-12-14 15:28:51 -08001036static DEVICE_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -08001037 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -07001038
Kay Sieversd369a5d2011-12-14 15:28:51 -08001039static struct bus_type clocksource_subsys = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01001040 .name = "clocksource",
Kay Sieversd369a5d2011-12-14 15:28:51 -08001041 .dev_name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -07001042};
1043
Kay Sieversd369a5d2011-12-14 15:28:51 -08001044static struct device device_clocksource = {
john stultz734efb42006-06-26 00:25:05 -07001045 .id = 0,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001046 .bus = &clocksource_subsys,
john stultz734efb42006-06-26 00:25:05 -07001047};
1048
john stultzad596172006-06-26 00:25:06 -07001049static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -07001050{
Kay Sieversd369a5d2011-12-14 15:28:51 -08001051 int error = subsys_system_register(&clocksource_subsys, NULL);
john stultz734efb42006-06-26 00:25:05 -07001052
1053 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001054 error = device_register(&device_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001055 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001056 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -07001057 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001058 &dev_attr_current_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001059 if (!error)
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001060 error = device_create_file(&device_clocksource,
1061 &dev_attr_unbind_clocksource);
1062 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001063 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -07001064 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001065 &dev_attr_available_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001066 return error;
1067}
1068
1069device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -08001070#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -07001071
1072/**
1073 * boot_override_clocksource - boot clock override
1074 * @str: override name
1075 *
1076 * Takes a clocksource= boot argument and uses it
1077 * as the clocksource override name.
1078 */
1079static int __init boot_override_clocksource(char* str)
1080{
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001081 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001082 if (str)
1083 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001084 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001085 return 1;
1086}
1087
1088__setup("clocksource=", boot_override_clocksource);
1089
1090/**
1091 * boot_override_clock - Compatibility layer for deprecated boot option
1092 * @str: override name
1093 *
1094 * DEPRECATED! Takes a clock= boot argument and uses it
1095 * as the clocksource override name
1096 */
1097static int __init boot_override_clock(char* str)
1098{
john stultz5d0cf412006-06-26 00:25:12 -07001099 if (!strcmp(str, "pmtmr")) {
1100 printk("Warning: clock=pmtmr is deprecated. "
1101 "Use clocksource=acpi_pm.\n");
1102 return boot_override_clocksource("acpi_pm");
1103 }
1104 printk("Warning! clock= boot option is deprecated. "
1105 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -07001106 return boot_override_clocksource(str);
1107}
1108
1109__setup("clock=", boot_override_clock);