blob: d7f1a45c2fa59a8723cf3346c92a3d0fcdfd873b [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
Patrick Ohlya038a352009-02-12 05:03:34 +000034void timecounter_init(struct timecounter *tc,
35 const struct cyclecounter *cc,
36 u64 start_tstamp)
37{
38 tc->cc = cc;
39 tc->cycle_last = cc->read(cc);
40 tc->nsec = start_tstamp;
41}
David S. Miller3586e0a2009-11-11 19:06:30 -080042EXPORT_SYMBOL_GPL(timecounter_init);
Patrick Ohlya038a352009-02-12 05:03:34 +000043
44/**
45 * timecounter_read_delta - get nanoseconds since last call of this function
46 * @tc: Pointer to time counter
47 *
48 * When the underlying cycle counter runs over, this will be handled
49 * correctly as long as it does not run over more than once between
50 * calls.
51 *
52 * The first call to this function for a new time counter initializes
53 * the time tracking and returns an undefined result.
54 */
55static u64 timecounter_read_delta(struct timecounter *tc)
56{
57 cycle_t cycle_now, cycle_delta;
58 u64 ns_offset;
59
60 /* read cycle counter: */
61 cycle_now = tc->cc->read(tc->cc);
62
63 /* calculate the delta since the last timecounter_read_delta(): */
64 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
65
66 /* convert to nanoseconds: */
67 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
68
69 /* update time stamp of timecounter_read_delta() call: */
70 tc->cycle_last = cycle_now;
71
72 return ns_offset;
73}
74
75u64 timecounter_read(struct timecounter *tc)
76{
77 u64 nsec;
78
79 /* increment time by nanoseconds since last call */
80 nsec = timecounter_read_delta(tc);
81 nsec += tc->nsec;
82 tc->nsec = nsec;
83
84 return nsec;
85}
David S. Miller3586e0a2009-11-11 19:06:30 -080086EXPORT_SYMBOL_GPL(timecounter_read);
Patrick Ohlya038a352009-02-12 05:03:34 +000087
88u64 timecounter_cyc2time(struct timecounter *tc,
89 cycle_t cycle_tstamp)
90{
91 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
92 u64 nsec;
93
94 /*
95 * Instead of always treating cycle_tstamp as more recent
96 * than tc->cycle_last, detect when it is too far in the
97 * future and treat it as old time stamp instead.
98 */
99 if (cycle_delta > tc->cc->mask / 2) {
100 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
101 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
102 } else {
103 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
104 }
105
106 return nsec;
107}
David S. Miller3586e0a2009-11-11 19:06:30 -0800108EXPORT_SYMBOL_GPL(timecounter_cyc2time);
Patrick Ohlya038a352009-02-12 05:03:34 +0000109
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000110/**
111 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
112 * @mult: pointer to mult variable
113 * @shift: pointer to shift variable
114 * @from: frequency to convert from
115 * @to: frequency to convert to
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500116 * @maxsec: guaranteed runtime conversion range in seconds
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000117 *
118 * The function evaluates the shift/mult pair for the scaled math
119 * operations of clocksources and clockevents.
120 *
121 * @to and @from are frequency values in HZ. For clock sources @to is
122 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
123 * event @to is the counter frequency and @from is NSEC_PER_SEC.
124 *
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500125 * The @maxsec conversion range argument controls the time frame in
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000126 * seconds which must be covered by the runtime conversion with the
127 * calculated mult and shift factors. This guarantees that no 64bit
128 * overflow happens when the input value of the conversion is
129 * multiplied with the calculated mult factor. Larger ranges may
130 * reduce the conversion accuracy by chosing smaller mult and shift
131 * factors.
132 */
133void
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500134clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000135{
136 u64 tmp;
137 u32 sft, sftacc= 32;
138
139 /*
140 * Calculate the shift factor which is limiting the conversion
141 * range:
142 */
Nicolas Pitre5fdade92011-01-11 12:18:12 -0500143 tmp = ((u64)maxsec * from) >> 32;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000144 while (tmp) {
145 tmp >>=1;
146 sftacc--;
147 }
148
149 /*
150 * Find the conversion shift/mult pair which has the best
151 * accuracy and fits the maxsec conversion range:
152 */
153 for (sft = 32; sft > 0; sft--) {
154 tmp = (u64) to << sft;
john stultzb5776c42010-12-16 19:03:27 +0000155 tmp += from / 2;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +0000156 do_div(tmp, from);
157 if ((tmp >> sftacc) == 0)
158 break;
159 }
160 *mult = tmp;
161 *shift = sft;
162}
163
john stultz734efb42006-06-26 00:25:05 -0700164/*[Clocksource internal variables]---------
165 * curr_clocksource:
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200166 * currently selected clocksource.
john stultz734efb42006-06-26 00:25:05 -0700167 * clocksource_list:
168 * linked list with the registered clocksources
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200169 * clocksource_mutex:
170 * protects manipulations to curr_clocksource and the clocksource_list
john stultz734efb42006-06-26 00:25:05 -0700171 * override_name:
172 * Name of the user-specified clocksource.
173 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200174static struct clocksource *curr_clocksource;
john stultz734efb42006-06-26 00:25:05 -0700175static LIST_HEAD(clocksource_list);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200176static DEFINE_MUTEX(clocksource_mutex);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000177#define CS_NAME_LEN 32
178static char override_name[CS_NAME_LEN];
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200179static int finished_booting;
john stultz734efb42006-06-26 00:25:05 -0700180
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800181#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200182static void clocksource_watchdog_work(struct work_struct *work);
183
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800184static LIST_HEAD(watchdog_list);
185static struct clocksource *watchdog;
186static struct timer_list watchdog_timer;
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200187static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800188static DEFINE_SPINLOCK(watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200189static int watchdog_running;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200190static atomic_t watchdog_reset_pending;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700191
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200192static int clocksource_watchdog_kthread(void *data);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200193static void __clocksource_change_rating(struct clocksource *cs, int rating);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200194
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800195/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700196 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800197 */
198#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700199#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800200
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200201static void clocksource_watchdog_work(struct work_struct *work)
202{
203 /*
204 * If kthread_run fails the next watchdog scan over the
205 * watchdog_list will find the unstable clock again.
206 */
207 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
208}
209
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200210static void __clocksource_unstable(struct clocksource *cs)
211{
212 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
213 cs->flags |= CLOCK_SOURCE_UNSTABLE;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200214 if (finished_booting)
215 schedule_work(&watchdog_work);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200216}
217
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200218static void clocksource_unstable(struct clocksource *cs, int64_t delta)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800219{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800220 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
221 cs->name, delta);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200222 __clocksource_unstable(cs);
223}
224
225/**
226 * clocksource_mark_unstable - mark clocksource unstable via watchdog
227 * @cs: clocksource to be marked unstable
228 *
229 * This function is called instead of clocksource_change_rating from
230 * cpu hotplug code to avoid a deadlock between the clocksource mutex
231 * and the cpu hotplug mutex. It defers the update of the clocksource
232 * to the watchdog thread.
233 */
234void clocksource_mark_unstable(struct clocksource *cs)
235{
236 unsigned long flags;
237
238 spin_lock_irqsave(&watchdog_lock, flags);
239 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
240 if (list_empty(&cs->wd_list))
241 list_add(&cs->wd_list, &watchdog_list);
242 __clocksource_unstable(cs);
243 }
244 spin_unlock_irqrestore(&watchdog_lock, flags);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800245}
246
247static void clocksource_watchdog(unsigned long data)
248{
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200249 struct clocksource *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800250 cycle_t csnow, wdnow;
251 int64_t wd_nsec, cs_nsec;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200252 int next_cpu, reset_pending;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800253
254 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200255 if (!watchdog_running)
256 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800257
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200258 reset_pending = atomic_read(&watchdog_reset_pending);
259
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200260 list_for_each_entry(cs, &watchdog_list, wd_list) {
261
262 /* Clocksource already marked unstable? */
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200263 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200264 if (finished_booting)
265 schedule_work(&watchdog_work);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200266 continue;
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200267 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200268
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200269 local_irq_disable();
Magnus Damm8e196082009-04-21 12:24:00 -0700270 csnow = cs->read(cs);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200271 wdnow = watchdog->read(watchdog);
272 local_irq_enable();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700273
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200274 /* Clocksource initialized ? */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200275 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
276 atomic_read(&watchdog_reset_pending)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200277 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200278 cs->wd_last = wdnow;
279 cs->cs_last = csnow;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700280 continue;
281 }
282
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200283 wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask,
284 watchdog->mult, watchdog->shift);
285
286 cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200287 cs->mask, cs->mult, cs->shift);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200288 cs->cs_last = csnow;
289 cs->wd_last = wdnow;
290
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200291 if (atomic_read(&watchdog_reset_pending))
292 continue;
293
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200294 /* Check the deviation from the watchdog clocksource. */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200295 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200296 clocksource_unstable(cs, cs_nsec - wd_nsec);
297 continue;
298 }
299
300 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
301 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
302 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
303 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
304 /*
305 * We just marked the clocksource as highres-capable,
306 * notify the rest of the system as well so that we
307 * transition into high-res mode:
308 */
309 tick_clock_notify();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800310 }
311 }
312
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200313 /*
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200314 * We only clear the watchdog_reset_pending, when we did a
315 * full cycle through all clocksources.
316 */
317 if (reset_pending)
318 atomic_dec(&watchdog_reset_pending);
319
320 /*
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200321 * Cycle through CPUs to check if the CPUs stay synchronized
322 * to each other.
323 */
324 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
325 if (next_cpu >= nr_cpu_ids)
326 next_cpu = cpumask_first(cpu_online_mask);
327 watchdog_timer.expires += WATCHDOG_INTERVAL;
328 add_timer_on(&watchdog_timer, next_cpu);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200329out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800330 spin_unlock(&watchdog_lock);
331}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200332
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200333static inline void clocksource_start_watchdog(void)
334{
335 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
336 return;
337 init_timer(&watchdog_timer);
338 watchdog_timer.function = clocksource_watchdog;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200339 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
340 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
341 watchdog_running = 1;
342}
343
344static inline void clocksource_stop_watchdog(void)
345{
346 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
347 return;
348 del_timer(&watchdog_timer);
349 watchdog_running = 0;
350}
351
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200352static inline void clocksource_reset_watchdog(void)
353{
354 struct clocksource *cs;
355
356 list_for_each_entry(cs, &watchdog_list, wd_list)
357 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
358}
359
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700360static void clocksource_resume_watchdog(void)
361{
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200362 atomic_inc(&watchdog_reset_pending);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700363}
364
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200365static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800366{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800367 unsigned long flags;
368
369 spin_lock_irqsave(&watchdog_lock, flags);
370 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200371 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800372 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200373 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200374 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200375 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200376 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800377 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200378 /* Pick the best watchdog. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800379 if (!watchdog || cs->rating > watchdog->rating) {
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800380 watchdog = cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800381 /* Reset watchdog cycles */
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200382 clocksource_reset_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800383 }
384 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200385 /* Check if the watchdog timer needs to be started. */
386 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800387 spin_unlock_irqrestore(&watchdog_lock, flags);
388}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200389
390static void clocksource_dequeue_watchdog(struct clocksource *cs)
391{
392 struct clocksource *tmp;
393 unsigned long flags;
394
395 spin_lock_irqsave(&watchdog_lock, flags);
396 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
397 /* cs is a watched clocksource. */
398 list_del_init(&cs->wd_list);
399 } else if (cs == watchdog) {
400 /* Reset watchdog cycles */
401 clocksource_reset_watchdog();
402 /* Current watchdog is removed. Find an alternative. */
403 watchdog = NULL;
404 list_for_each_entry(tmp, &clocksource_list, list) {
405 if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
406 continue;
407 if (!watchdog || tmp->rating > watchdog->rating)
408 watchdog = tmp;
409 }
410 }
411 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
412 /* Check if the watchdog timer needs to be stopped. */
413 clocksource_stop_watchdog();
414 spin_unlock_irqrestore(&watchdog_lock, flags);
415}
416
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200417static int clocksource_watchdog_kthread(void *data)
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200418{
419 struct clocksource *cs, *tmp;
420 unsigned long flags;
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200421 LIST_HEAD(unstable);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200422
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200423 mutex_lock(&clocksource_mutex);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200424 spin_lock_irqsave(&watchdog_lock, flags);
425 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
426 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
427 list_del_init(&cs->wd_list);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200428 list_add(&cs->wd_list, &unstable);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200429 }
430 /* Check if the watchdog timer needs to be stopped. */
431 clocksource_stop_watchdog();
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200432 spin_unlock_irqrestore(&watchdog_lock, flags);
433
434 /* Needs to be done outside of watchdog lock */
435 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
436 list_del_init(&cs->wd_list);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200437 __clocksource_change_rating(cs, 0);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200438 }
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200439 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200440 return 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200441}
442
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200443#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
444
445static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800446{
447 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
448 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
449}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700450
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200451static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700452static inline void clocksource_resume_watchdog(void) { }
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200453static inline int clocksource_watchdog_kthread(void *data) { return 0; }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200454
455#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800456
john stultz734efb42006-06-26 00:25:05 -0700457/**
Magnus Dammc54a42b2010-02-02 14:41:41 -0800458 * clocksource_suspend - suspend the clocksource(s)
459 */
460void clocksource_suspend(void)
461{
462 struct clocksource *cs;
463
464 list_for_each_entry_reverse(cs, &clocksource_list, list)
465 if (cs->suspend)
466 cs->suspend(cs);
467}
468
469/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700470 * clocksource_resume - resume the clocksource(s)
471 */
472void clocksource_resume(void)
473{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700474 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700475
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200476 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700477 if (cs->resume)
Magnus Damm17622332010-02-02 14:41:39 -0800478 cs->resume(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700479
480 clocksource_resume_watchdog();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700481}
482
483/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600484 * clocksource_touch_watchdog - Update watchdog
485 *
486 * Update the watchdog after exception contexts such as kgdb so as not
Thomas Gleixner7b7422a2010-01-26 12:51:10 +0100487 * to incorrectly trip the watchdog. This might fail when the kernel
488 * was stopped in code which holds watchdog_lock.
Jason Wessel7c3078b2008-02-15 14:55:54 -0600489 */
490void clocksource_touch_watchdog(void)
491{
492 clocksource_resume_watchdog();
493}
494
john stultz734efb42006-06-26 00:25:05 -0700495/**
John Stultzd65670a2011-10-31 17:06:35 -0400496 * clocksource_max_adjustment- Returns max adjustment amount
497 * @cs: Pointer to clocksource
498 *
499 */
500static u32 clocksource_max_adjustment(struct clocksource *cs)
501{
502 u64 ret;
503 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600504 * We won't try to correct for more than 11% adjustments (110,000 ppm),
John Stultzd65670a2011-10-31 17:06:35 -0400505 */
506 ret = (u64)cs->mult * 11;
507 do_div(ret,100);
508 return (u32)ret;
509}
510
511/**
Jon Hunter98962462009-08-18 12:45:10 -0500512 * clocksource_max_deferment - Returns max time the clocksource can be deferred
513 * @cs: Pointer to clocksource
514 *
515 */
516static u64 clocksource_max_deferment(struct clocksource *cs)
517{
518 u64 max_nsecs, max_cycles;
519
520 /*
521 * Calculate the maximum number of cycles that we can pass to the
522 * cyc2ns function without overflowing a 64-bit signed result. The
John Stultzd65670a2011-10-31 17:06:35 -0400523 * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
524 * which is equivalent to the below.
525 * max_cycles < (2^63)/(cs->mult + cs->maxadj)
526 * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
527 * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
528 * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
529 * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
Jon Hunter98962462009-08-18 12:45:10 -0500530 * Please note that we add 1 to the result of the log2 to account for
531 * any rounding errors, ensure the above inequality is satisfied and
532 * no overflow will occur.
533 */
John Stultzd65670a2011-10-31 17:06:35 -0400534 max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
Jon Hunter98962462009-08-18 12:45:10 -0500535
536 /*
537 * The actual maximum number of cycles we can defer the clocksource is
538 * determined by the minimum of max_cycles and cs->mask.
John Stultzd65670a2011-10-31 17:06:35 -0400539 * Note: Here we subtract the maxadj to make sure we don't sleep for
540 * too long if there's a large negative adjustment.
Jon Hunter98962462009-08-18 12:45:10 -0500541 */
542 max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
John Stultzd65670a2011-10-31 17:06:35 -0400543 max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
544 cs->shift);
Jon Hunter98962462009-08-18 12:45:10 -0500545
546 /*
547 * To ensure that the clocksource does not wrap whilst we are idle,
548 * limit the time the clocksource can be deferred by 12.5%. Please
549 * note a margin of 12.5% is used because this can be computed with
550 * a shift, versus say 10% which would require division.
551 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500552 return max_nsecs - (max_nsecs >> 3);
Jon Hunter98962462009-08-18 12:45:10 -0500553}
554
John Stultz592913e2010-07-13 17:56:20 -0700555#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
john stultz734efb42006-06-26 00:25:05 -0700556
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000557static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000558{
559 struct clocksource *cs;
560
561 if (!finished_booting || list_empty(&clocksource_list))
562 return NULL;
563
564 /*
565 * We pick the clocksource with the highest rating. If oneshot
566 * mode is active, we pick the highres valid clocksource with
567 * the best rating.
568 */
569 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000570 if (skipcur && cs == curr_clocksource)
571 continue;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000572 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
573 continue;
574 return cs;
575 }
576 return NULL;
577}
578
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000579static void __clocksource_select(bool skipcur)
john stultz734efb42006-06-26 00:25:05 -0700580{
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000581 bool oneshot = tick_oneshot_mode_active();
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200582 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800583
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000584 /* Find the best suitable clocksource */
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000585 best = clocksource_find_best(oneshot, skipcur);
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000586 if (!best)
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200587 return;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000588
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200589 /* Check for the override clocksource. */
590 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000591 if (skipcur && cs == curr_clocksource)
592 continue;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200593 if (strcmp(cs->name, override_name) != 0)
594 continue;
595 /*
596 * Check to make sure we don't switch to a non-highres
597 * capable clocksource if the tick code is in oneshot
598 * mode (highres or nohz)
599 */
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000600 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200601 /* Override clocksource cannot be used. */
602 printk(KERN_WARNING "Override clocksource %s is not "
603 "HRT compatible. Cannot switch while in "
604 "HRT/NOHZ mode\n", cs->name);
605 override_name[0] = 0;
606 } else
607 /* Override clocksource can be used. */
608 best = cs;
609 break;
610 }
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000611
612 if (curr_clocksource != best && !timekeeping_notify(best)) {
613 pr_info("Switched to clocksource %s\n", best->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200614 curr_clocksource = best;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200615 }
john stultz734efb42006-06-26 00:25:05 -0700616}
617
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000618/**
619 * clocksource_select - Select the best clocksource available
620 *
621 * Private function. Must hold clocksource_mutex when called.
622 *
623 * Select the clocksource with the best rating, or the clocksource,
624 * which is selected by userspace override.
625 */
626static void clocksource_select(void)
627{
628 return __clocksource_select(false);
629}
630
John Stultz592913e2010-07-13 17:56:20 -0700631#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200632
633static inline void clocksource_select(void) { }
634
635#endif
636
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200637/*
638 * clocksource_done_booting - Called near the end of core bootup
639 *
640 * Hack to avoid lots of clocksource churn at boot time.
641 * We use fs_initcall because we want this to start before
642 * device_initcall but after subsys_initcall.
643 */
644static int __init clocksource_done_booting(void)
645{
john stultzad6759f2010-03-01 12:34:43 -0800646 mutex_lock(&clocksource_mutex);
647 curr_clocksource = clocksource_default_clock();
648 mutex_unlock(&clocksource_mutex);
649
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200650 finished_booting = 1;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200651
652 /*
653 * Run the watchdog first to eliminate unstable clock sources
654 */
655 clocksource_watchdog_kthread(NULL);
656
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200657 mutex_lock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200658 clocksource_select();
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200659 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200660 return 0;
661}
662fs_initcall(clocksource_done_booting);
663
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800664/*
665 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700666 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200667static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700668{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200669 struct list_head *entry = &clocksource_list;
670 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700671
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200672 list_for_each_entry(tmp, &clocksource_list, list)
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800673 /* Keep track of the place, where to insert */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200674 if (tmp->rating >= cs->rating)
675 entry = &tmp->list;
676 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700677}
678
John Stultzd7e81c22010-05-07 18:07:38 -0700679/**
John Stultz852db462010-07-13 17:56:28 -0700680 * __clocksource_updatefreq_scale - Used update clocksource with new freq
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900681 * @cs: clocksource to be registered
John Stultz852db462010-07-13 17:56:28 -0700682 * @scale: Scale factor multiplied against freq to get clocksource hz
683 * @freq: clocksource frequency (cycles per second) divided by scale
684 *
685 * This should only be called from the clocksource->enable() method.
686 *
687 * This *SHOULD NOT* be called directly! Please use the
688 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
689 */
690void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
691{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200692 u64 sec;
John Stultz852db462010-07-13 17:56:28 -0700693 /*
Thomas Gleixner724ed532011-05-18 21:33:40 +0000694 * Calc the maximum number of seconds which we can run before
695 * wrapping around. For clocksources which have a mask > 32bit
696 * we need to limit the max sleep time to have a good
697 * conversion precision. 10 minutes is still a reasonable
698 * amount. That results in a shift value of 24 for a
699 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
700 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
701 * margin as we do in clocksource_max_deferment()
John Stultz852db462010-07-13 17:56:28 -0700702 */
Yang Honggang (Joseph)b1f91962011-12-01 22:22:41 -0500703 sec = (cs->mask - (cs->mask >> 3));
Thomas Gleixner724ed532011-05-18 21:33:40 +0000704 do_div(sec, freq);
705 do_div(sec, scale);
706 if (!sec)
707 sec = 1;
708 else if (sec > 600 && cs->mask > UINT_MAX)
709 sec = 600;
710
John Stultz852db462010-07-13 17:56:28 -0700711 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
Thomas Gleixner724ed532011-05-18 21:33:40 +0000712 NSEC_PER_SEC / scale, sec * scale);
John Stultzd65670a2011-10-31 17:06:35 -0400713
714 /*
715 * for clocksources that have large mults, to avoid overflow.
716 * Since mult may be adjusted by ntp, add an safety extra margin
717 *
718 */
719 cs->maxadj = clocksource_max_adjustment(cs);
720 while ((cs->mult + cs->maxadj < cs->mult)
721 || (cs->mult - cs->maxadj > cs->mult)) {
722 cs->mult >>= 1;
723 cs->shift--;
724 cs->maxadj = clocksource_max_adjustment(cs);
725 }
726
John Stultz852db462010-07-13 17:56:28 -0700727 cs->max_idle_ns = clocksource_max_deferment(cs);
728}
729EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
730
731/**
John Stultzd7e81c22010-05-07 18:07:38 -0700732 * __clocksource_register_scale - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900733 * @cs: clocksource to be registered
John Stultzd7e81c22010-05-07 18:07:38 -0700734 * @scale: Scale factor multiplied against freq to get clocksource hz
735 * @freq: clocksource frequency (cycles per second) divided by scale
736 *
737 * Returns -EBUSY if registration fails, zero otherwise.
738 *
739 * This *SHOULD NOT* be called directly! Please use the
740 * clocksource_register_hz() or clocksource_register_khz helper functions.
741 */
742int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
743{
744
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400745 /* Initialize mult/shift and max_idle_ns */
John Stultz852db462010-07-13 17:56:28 -0700746 __clocksource_updatefreq_scale(cs, scale, freq);
John Stultzd7e81c22010-05-07 18:07:38 -0700747
John Stultz852db462010-07-13 17:56:28 -0700748 /* Add clocksource to the clcoksource list */
John Stultzd7e81c22010-05-07 18:07:38 -0700749 mutex_lock(&clocksource_mutex);
750 clocksource_enqueue(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700751 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700752 clocksource_select();
John Stultzd7e81c22010-05-07 18:07:38 -0700753 mutex_unlock(&clocksource_mutex);
754 return 0;
755}
756EXPORT_SYMBOL_GPL(__clocksource_register_scale);
757
758
john stultz734efb42006-06-26 00:25:05 -0700759/**
john stultza2752542006-06-26 00:25:14 -0700760 * clocksource_register - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900761 * @cs: clocksource to be registered
john stultz734efb42006-06-26 00:25:05 -0700762 *
763 * Returns -EBUSY if registration fails, zero otherwise.
764 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200765int clocksource_register(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700766{
John Stultzd65670a2011-10-31 17:06:35 -0400767 /* calculate max adjustment for given mult/shift */
768 cs->maxadj = clocksource_max_adjustment(cs);
769 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
770 "Clocksource %s might overflow on 11%% adjustment\n",
771 cs->name);
772
Jon Hunter98962462009-08-18 12:45:10 -0500773 /* calculate max idle time permitted for this clocksource */
774 cs->max_idle_ns = clocksource_max_deferment(cs);
775
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200776 mutex_lock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200777 clocksource_enqueue(cs);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200778 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700779 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200780 mutex_unlock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200781 return 0;
john stultz734efb42006-06-26 00:25:05 -0700782}
john stultza2752542006-06-26 00:25:14 -0700783EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700784
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200785static void __clocksource_change_rating(struct clocksource *cs, int rating)
786{
787 list_del(&cs->list);
788 cs->rating = rating;
789 clocksource_enqueue(cs);
790 clocksource_select();
791}
792
john stultz734efb42006-06-26 00:25:05 -0700793/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800794 * clocksource_change_rating - Change the rating of a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900795 * @cs: clocksource to be changed
796 * @rating: new rating
john stultz734efb42006-06-26 00:25:05 -0700797 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800798void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700799{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200800 mutex_lock(&clocksource_mutex);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200801 __clocksource_change_rating(cs, rating);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200802 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700803}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200804EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700805
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100806/**
807 * clocksource_unregister - remove a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900808 * @cs: clocksource to be unregistered
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100809 */
810void clocksource_unregister(struct clocksource *cs)
811{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200812 mutex_lock(&clocksource_mutex);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200813 clocksource_dequeue_watchdog(cs);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100814 list_del(&cs->list);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200815 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200816 mutex_unlock(&clocksource_mutex);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100817}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200818EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100819
Daniel Walker2b013702006-12-10 02:21:30 -0800820#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700821/**
822 * sysfs_show_current_clocksources - sysfs interface for current clocksource
823 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900824 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700825 * @buf: char buffer to be filled with clocksource list
826 *
827 * Provides sysfs interface for listing current clocksource.
828 */
829static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800830sysfs_show_current_clocksources(struct device *dev,
831 struct device_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700832{
Miao Xie5e2cb102008-02-06 01:36:53 -0800833 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700834
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200835 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -0800836 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200837 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700838
Miao Xie5e2cb102008-02-06 01:36:53 -0800839 return count;
john stultz734efb42006-06-26 00:25:05 -0700840}
841
Thomas Gleixner29b54072013-04-25 20:31:45 +0000842static size_t clocksource_get_uname(const char *buf, char *dst, size_t cnt)
843{
844 size_t ret = cnt;
845
846 /* strings from sysfs write are not 0 terminated! */
847 if (!cnt || cnt >= CS_NAME_LEN)
848 return -EINVAL;
849
850 /* strip of \n: */
851 if (buf[cnt-1] == '\n')
852 cnt--;
853 if (cnt > 0)
854 memcpy(dst, buf, cnt);
855 dst[cnt] = 0;
856 return ret;
857}
858
john stultz734efb42006-06-26 00:25:05 -0700859/**
860 * sysfs_override_clocksource - interface for manually overriding clocksource
861 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900862 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700863 * @buf: name of override clocksource
864 * @count: length of buffer
865 *
866 * Takes input from sysfs interface for manually overriding the default
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200867 * clocksource selection.
john stultz734efb42006-06-26 00:25:05 -0700868 */
Kay Sieversd369a5d2011-12-14 15:28:51 -0800869static ssize_t sysfs_override_clocksource(struct device *dev,
870 struct device_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700871 const char *buf, size_t count)
872{
Thomas Gleixner29b54072013-04-25 20:31:45 +0000873 size_t ret;
john stultz734efb42006-06-26 00:25:05 -0700874
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200875 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700876
Thomas Gleixner29b54072013-04-25 20:31:45 +0000877 ret = clocksource_get_uname(buf, override_name, count);
878 if (ret >= 0)
879 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700880
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200881 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700882
883 return ret;
884}
885
886/**
887 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
888 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900889 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700890 * @buf: char buffer to be filled with clocksource list
891 *
892 * Provides sysfs interface for listing registered clocksources
893 */
894static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800895sysfs_show_available_clocksources(struct device *dev,
896 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200897 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700898{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700899 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -0800900 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700901
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200902 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700903 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200904 /*
905 * Don't show non-HRES clocksource if the tick code is
906 * in one shot mode (highres=on or nohz=on)
907 */
908 if (!tick_oneshot_mode_active() ||
909 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -0700910 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -0800911 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
912 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -0700913 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200914 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700915
Miao Xie5e2cb102008-02-06 01:36:53 -0800916 count += snprintf(buf + count,
917 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -0700918
Miao Xie5e2cb102008-02-06 01:36:53 -0800919 return count;
john stultz734efb42006-06-26 00:25:05 -0700920}
921
922/*
923 * Sysfs setup bits:
924 */
Kay Sieversd369a5d2011-12-14 15:28:51 -0800925static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800926 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700927
Kay Sieversd369a5d2011-12-14 15:28:51 -0800928static DEVICE_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800929 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -0700930
Kay Sieversd369a5d2011-12-14 15:28:51 -0800931static struct bus_type clocksource_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100932 .name = "clocksource",
Kay Sieversd369a5d2011-12-14 15:28:51 -0800933 .dev_name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -0700934};
935
Kay Sieversd369a5d2011-12-14 15:28:51 -0800936static struct device device_clocksource = {
john stultz734efb42006-06-26 00:25:05 -0700937 .id = 0,
Kay Sieversd369a5d2011-12-14 15:28:51 -0800938 .bus = &clocksource_subsys,
john stultz734efb42006-06-26 00:25:05 -0700939};
940
john stultzad596172006-06-26 00:25:06 -0700941static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -0700942{
Kay Sieversd369a5d2011-12-14 15:28:51 -0800943 int error = subsys_system_register(&clocksource_subsys, NULL);
john stultz734efb42006-06-26 00:25:05 -0700944
945 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -0800946 error = device_register(&device_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700947 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -0800948 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -0700949 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -0800950 &dev_attr_current_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700951 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -0800952 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -0700953 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -0800954 &dev_attr_available_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700955 return error;
956}
957
958device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -0800959#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -0700960
961/**
962 * boot_override_clocksource - boot clock override
963 * @str: override name
964 *
965 * Takes a clocksource= boot argument and uses it
966 * as the clocksource override name.
967 */
968static int __init boot_override_clocksource(char* str)
969{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200970 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700971 if (str)
972 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200973 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700974 return 1;
975}
976
977__setup("clocksource=", boot_override_clocksource);
978
979/**
980 * boot_override_clock - Compatibility layer for deprecated boot option
981 * @str: override name
982 *
983 * DEPRECATED! Takes a clock= boot argument and uses it
984 * as the clocksource override name
985 */
986static int __init boot_override_clock(char* str)
987{
john stultz5d0cf412006-06-26 00:25:12 -0700988 if (!strcmp(str, "pmtmr")) {
989 printk("Warning: clock=pmtmr is deprecated. "
990 "Use clocksource=acpi_pm.\n");
991 return boot_override_clocksource("acpi_pm");
992 }
993 printk("Warning! clock= boot option is deprecated. "
994 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -0700995 return boot_override_clocksource(str);
996}
997
998__setup("clock=", boot_override_clock);