blob: c6bff11f7957e3252f64c89257875be5bbb871a2 [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
26#include <linux/clocksource.h>
27#include <linux/sysdev.h>
28#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>
john stultz734efb42006-06-26 00:25:05 -070032
Patrick Ohlya038a352009-02-12 05:03:34 +000033void timecounter_init(struct timecounter *tc,
34 const struct cyclecounter *cc,
35 u64 start_tstamp)
36{
37 tc->cc = cc;
38 tc->cycle_last = cc->read(cc);
39 tc->nsec = start_tstamp;
40}
41EXPORT_SYMBOL(timecounter_init);
42
43/**
44 * timecounter_read_delta - get nanoseconds since last call of this function
45 * @tc: Pointer to time counter
46 *
47 * When the underlying cycle counter runs over, this will be handled
48 * correctly as long as it does not run over more than once between
49 * calls.
50 *
51 * The first call to this function for a new time counter initializes
52 * the time tracking and returns an undefined result.
53 */
54static u64 timecounter_read_delta(struct timecounter *tc)
55{
56 cycle_t cycle_now, cycle_delta;
57 u64 ns_offset;
58
59 /* read cycle counter: */
60 cycle_now = tc->cc->read(tc->cc);
61
62 /* calculate the delta since the last timecounter_read_delta(): */
63 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
64
65 /* convert to nanoseconds: */
66 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
67
68 /* update time stamp of timecounter_read_delta() call: */
69 tc->cycle_last = cycle_now;
70
71 return ns_offset;
72}
73
74u64 timecounter_read(struct timecounter *tc)
75{
76 u64 nsec;
77
78 /* increment time by nanoseconds since last call */
79 nsec = timecounter_read_delta(tc);
80 nsec += tc->nsec;
81 tc->nsec = nsec;
82
83 return nsec;
84}
85EXPORT_SYMBOL(timecounter_read);
86
87u64 timecounter_cyc2time(struct timecounter *tc,
88 cycle_t cycle_tstamp)
89{
90 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
91 u64 nsec;
92
93 /*
94 * Instead of always treating cycle_tstamp as more recent
95 * than tc->cycle_last, detect when it is too far in the
96 * future and treat it as old time stamp instead.
97 */
98 if (cycle_delta > tc->cc->mask / 2) {
99 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
100 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
101 } else {
102 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
103 }
104
105 return nsec;
106}
107EXPORT_SYMBOL(timecounter_cyc2time);
108
john stultz734efb42006-06-26 00:25:05 -0700109/*[Clocksource internal variables]---------
110 * curr_clocksource:
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200111 * currently selected clocksource.
john stultz734efb42006-06-26 00:25:05 -0700112 * clocksource_list:
113 * linked list with the registered clocksources
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200114 * clocksource_mutex:
115 * protects manipulations to curr_clocksource and the clocksource_list
john stultz734efb42006-06-26 00:25:05 -0700116 * override_name:
117 * Name of the user-specified clocksource.
118 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200119static struct clocksource *curr_clocksource;
john stultz734efb42006-06-26 00:25:05 -0700120static LIST_HEAD(clocksource_list);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200121static DEFINE_MUTEX(clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700122static char override_name[32];
john stultz734efb42006-06-26 00:25:05 -0700123
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800124#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
125static LIST_HEAD(watchdog_list);
126static struct clocksource *watchdog;
127static struct timer_list watchdog_timer;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200128static struct work_struct watchdog_work;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800129static DEFINE_SPINLOCK(watchdog_lock);
130static cycle_t watchdog_last;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200131static int watchdog_running;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700132
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200133static void clocksource_watchdog_work(struct work_struct *work);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200134static void __clocksource_change_rating(struct clocksource *cs, int rating);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200135
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800136/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700137 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800138 */
139#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700140#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800141
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200142static void clocksource_unstable(struct clocksource *cs, int64_t delta)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800143{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800144 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
145 cs->name, delta);
146 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200147 cs->flags |= CLOCK_SOURCE_UNSTABLE;
148 schedule_work(&watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800149}
150
151static void clocksource_watchdog(unsigned long data)
152{
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200153 struct clocksource *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800154 cycle_t csnow, wdnow;
155 int64_t wd_nsec, cs_nsec;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200156 int next_cpu;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800157
158 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200159 if (!watchdog_running)
160 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800161
Magnus Damm8e196082009-04-21 12:24:00 -0700162 wdnow = watchdog->read(watchdog);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200163 wd_nsec = clocksource_cyc2ns((wdnow - watchdog_last) & watchdog->mask,
164 watchdog->mult, watchdog->shift);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800165 watchdog_last = wdnow;
166
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200167 list_for_each_entry(cs, &watchdog_list, wd_list) {
168
169 /* Clocksource already marked unstable? */
170 if (cs->flags & CLOCK_SOURCE_UNSTABLE)
171 continue;
172
Magnus Damm8e196082009-04-21 12:24:00 -0700173 csnow = cs->read(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700174
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200175 /* Clocksource initialized ? */
176 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
177 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700178 cs->wd_last = csnow;
179 continue;
180 }
181
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200182 /* Check the deviation from the watchdog clocksource. */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200183 cs_nsec = clocksource_cyc2ns((csnow - cs->wd_last) &
184 cs->mask, cs->mult, cs->shift);
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200185 cs->wd_last = csnow;
186 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
187 clocksource_unstable(cs, cs_nsec - wd_nsec);
188 continue;
189 }
190
191 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
192 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
193 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
194 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
195 /*
196 * We just marked the clocksource as highres-capable,
197 * notify the rest of the system as well so that we
198 * transition into high-res mode:
199 */
200 tick_clock_notify();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800201 }
202 }
203
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200204 /*
205 * Cycle through CPUs to check if the CPUs stay synchronized
206 * to each other.
207 */
208 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
209 if (next_cpu >= nr_cpu_ids)
210 next_cpu = cpumask_first(cpu_online_mask);
211 watchdog_timer.expires += WATCHDOG_INTERVAL;
212 add_timer_on(&watchdog_timer, next_cpu);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200213out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800214 spin_unlock(&watchdog_lock);
215}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200216
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200217static inline void clocksource_start_watchdog(void)
218{
219 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
220 return;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200221 INIT_WORK(&watchdog_work, clocksource_watchdog_work);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200222 init_timer(&watchdog_timer);
223 watchdog_timer.function = clocksource_watchdog;
224 watchdog_last = watchdog->read(watchdog);
225 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
226 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
227 watchdog_running = 1;
228}
229
230static inline void clocksource_stop_watchdog(void)
231{
232 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
233 return;
234 del_timer(&watchdog_timer);
235 watchdog_running = 0;
236}
237
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200238static inline void clocksource_reset_watchdog(void)
239{
240 struct clocksource *cs;
241
242 list_for_each_entry(cs, &watchdog_list, wd_list)
243 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
244}
245
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700246static void clocksource_resume_watchdog(void)
247{
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200248 unsigned long flags;
249
250 spin_lock_irqsave(&watchdog_lock, flags);
251 clocksource_reset_watchdog();
252 spin_unlock_irqrestore(&watchdog_lock, flags);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700253}
254
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200255static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800256{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800257 unsigned long flags;
258
259 spin_lock_irqsave(&watchdog_lock, flags);
260 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200261 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800262 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200263 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200264 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200265 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200266 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800267 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200268 /* Pick the best watchdog. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800269 if (!watchdog || cs->rating > watchdog->rating) {
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800270 watchdog = cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800271 /* Reset watchdog cycles */
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200272 clocksource_reset_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800273 }
274 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200275 /* Check if the watchdog timer needs to be started. */
276 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800277 spin_unlock_irqrestore(&watchdog_lock, flags);
278}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200279
280static void clocksource_dequeue_watchdog(struct clocksource *cs)
281{
282 struct clocksource *tmp;
283 unsigned long flags;
284
285 spin_lock_irqsave(&watchdog_lock, flags);
286 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
287 /* cs is a watched clocksource. */
288 list_del_init(&cs->wd_list);
289 } else if (cs == watchdog) {
290 /* Reset watchdog cycles */
291 clocksource_reset_watchdog();
292 /* Current watchdog is removed. Find an alternative. */
293 watchdog = NULL;
294 list_for_each_entry(tmp, &clocksource_list, list) {
295 if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
296 continue;
297 if (!watchdog || tmp->rating > watchdog->rating)
298 watchdog = tmp;
299 }
300 }
301 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
302 /* Check if the watchdog timer needs to be stopped. */
303 clocksource_stop_watchdog();
304 spin_unlock_irqrestore(&watchdog_lock, flags);
305}
306
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200307static void clocksource_watchdog_work(struct work_struct *work)
308{
309 struct clocksource *cs, *tmp;
310 unsigned long flags;
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200311 LIST_HEAD(unstable);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200312
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200313 mutex_lock(&clocksource_mutex);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200314 spin_lock_irqsave(&watchdog_lock, flags);
315 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
316 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
317 list_del_init(&cs->wd_list);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200318 list_add(&cs->wd_list, &unstable);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200319 }
320 /* Check if the watchdog timer needs to be stopped. */
321 clocksource_stop_watchdog();
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200322 spin_unlock_irqrestore(&watchdog_lock, flags);
323
324 /* Needs to be done outside of watchdog lock */
325 list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
326 list_del_init(&cs->wd_list);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200327 __clocksource_change_rating(cs, 0);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200328 }
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200329 mutex_unlock(&clocksource_mutex);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200330}
331
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200332#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
333
334static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800335{
336 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
337 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
338}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700339
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200340static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700341static inline void clocksource_resume_watchdog(void) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200342
343#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800344
john stultz734efb42006-06-26 00:25:05 -0700345/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700346 * clocksource_resume - resume the clocksource(s)
347 */
348void clocksource_resume(void)
349{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700350 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700351
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200352 mutex_lock(&clocksource_mutex);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700353
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200354 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700355 if (cs->resume)
356 cs->resume();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700357
358 clocksource_resume_watchdog();
359
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200360 mutex_unlock(&clocksource_mutex);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700361}
362
363/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600364 * clocksource_touch_watchdog - Update watchdog
365 *
366 * Update the watchdog after exception contexts such as kgdb so as not
367 * to incorrectly trip the watchdog.
368 *
369 */
370void clocksource_touch_watchdog(void)
371{
372 clocksource_resume_watchdog();
373}
374
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200375#ifdef CONFIG_GENERIC_TIME
john stultz734efb42006-06-26 00:25:05 -0700376
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200377static int finished_booting;
john stultz734efb42006-06-26 00:25:05 -0700378
379/**
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200380 * clocksource_select - Select the best clocksource available
john stultz734efb42006-06-26 00:25:05 -0700381 *
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200382 * Private function. Must hold clocksource_mutex when called.
john stultz734efb42006-06-26 00:25:05 -0700383 *
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800384 * Select the clocksource with the best rating, or the clocksource,
385 * which is selected by userspace override.
john stultz734efb42006-06-26 00:25:05 -0700386 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200387static void clocksource_select(void)
john stultz734efb42006-06-26 00:25:05 -0700388{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200389 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800390
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200391 if (!finished_booting || list_empty(&clocksource_list))
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200392 return;
393 /* First clocksource on the list has the best rating. */
394 best = list_first_entry(&clocksource_list, struct clocksource, list);
395 /* Check for the override clocksource. */
396 list_for_each_entry(cs, &clocksource_list, list) {
397 if (strcmp(cs->name, override_name) != 0)
398 continue;
399 /*
400 * Check to make sure we don't switch to a non-highres
401 * capable clocksource if the tick code is in oneshot
402 * mode (highres or nohz)
403 */
404 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
405 tick_oneshot_mode_active()) {
406 /* Override clocksource cannot be used. */
407 printk(KERN_WARNING "Override clocksource %s is not "
408 "HRT compatible. Cannot switch while in "
409 "HRT/NOHZ mode\n", cs->name);
410 override_name[0] = 0;
411 } else
412 /* Override clocksource can be used. */
413 best = cs;
414 break;
415 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200416 if (curr_clocksource != best) {
417 printk(KERN_INFO "Switching to clocksource %s\n", best->name);
418 curr_clocksource = best;
419 timekeeping_notify(curr_clocksource);
420 }
john stultz734efb42006-06-26 00:25:05 -0700421}
422
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200423/*
424 * clocksource_done_booting - Called near the end of core bootup
425 *
426 * Hack to avoid lots of clocksource churn at boot time.
427 * We use fs_initcall because we want this to start before
428 * device_initcall but after subsys_initcall.
429 */
430static int __init clocksource_done_booting(void)
431{
432 finished_booting = 1;
433 clocksource_select();
434 return 0;
435}
436fs_initcall(clocksource_done_booting);
437
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200438#else /* CONFIG_GENERIC_TIME */
439
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200440static inline void clocksource_select(void) { }
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200441
442#endif
443
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800444/*
445 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700446 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200447static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700448{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200449 struct list_head *entry = &clocksource_list;
450 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700451
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200452 list_for_each_entry(tmp, &clocksource_list, list)
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800453 /* Keep track of the place, where to insert */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200454 if (tmp->rating >= cs->rating)
455 entry = &tmp->list;
456 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700457}
458
459/**
john stultza2752542006-06-26 00:25:14 -0700460 * clocksource_register - Used to install new clocksources
john stultz734efb42006-06-26 00:25:05 -0700461 * @t: clocksource to be registered
462 *
463 * Returns -EBUSY if registration fails, zero otherwise.
464 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200465int clocksource_register(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700466{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200467 mutex_lock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200468 clocksource_enqueue(cs);
469 clocksource_select();
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200470 clocksource_enqueue_watchdog(cs);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200471 mutex_unlock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200472 return 0;
john stultz734efb42006-06-26 00:25:05 -0700473}
john stultza2752542006-06-26 00:25:14 -0700474EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700475
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200476static void __clocksource_change_rating(struct clocksource *cs, int rating)
477{
478 list_del(&cs->list);
479 cs->rating = rating;
480 clocksource_enqueue(cs);
481 clocksource_select();
482}
483
john stultz734efb42006-06-26 00:25:05 -0700484/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800485 * clocksource_change_rating - Change the rating of a registered clocksource
john stultz734efb42006-06-26 00:25:05 -0700486 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800487void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700488{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200489 mutex_lock(&clocksource_mutex);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200490 __clocksource_change_rating(cs, rating);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200491 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700492}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200493EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700494
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100495/**
496 * clocksource_unregister - remove a registered clocksource
497 */
498void clocksource_unregister(struct clocksource *cs)
499{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200500 mutex_lock(&clocksource_mutex);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200501 clocksource_dequeue_watchdog(cs);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100502 list_del(&cs->list);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200503 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200504 mutex_unlock(&clocksource_mutex);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100505}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200506EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100507
Daniel Walker2b013702006-12-10 02:21:30 -0800508#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700509/**
510 * sysfs_show_current_clocksources - sysfs interface for current clocksource
511 * @dev: unused
512 * @buf: char buffer to be filled with clocksource list
513 *
514 * Provides sysfs interface for listing current clocksource.
515 */
516static ssize_t
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200517sysfs_show_current_clocksources(struct sys_device *dev,
518 struct sysdev_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700519{
Miao Xie5e2cb102008-02-06 01:36:53 -0800520 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700521
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200522 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -0800523 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200524 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700525
Miao Xie5e2cb102008-02-06 01:36:53 -0800526 return count;
john stultz734efb42006-06-26 00:25:05 -0700527}
528
529/**
530 * sysfs_override_clocksource - interface for manually overriding clocksource
531 * @dev: unused
532 * @buf: name of override clocksource
533 * @count: length of buffer
534 *
535 * Takes input from sysfs interface for manually overriding the default
536 * clocksource selction.
537 */
538static ssize_t sysfs_override_clocksource(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200539 struct sysdev_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700540 const char *buf, size_t count)
541{
542 size_t ret = count;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800543
john stultz734efb42006-06-26 00:25:05 -0700544 /* strings from sysfs write are not 0 terminated! */
545 if (count >= sizeof(override_name))
546 return -EINVAL;
547
548 /* strip of \n: */
549 if (buf[count-1] == '\n')
550 count--;
john stultz734efb42006-06-26 00:25:05 -0700551
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200552 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700553
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800554 if (count > 0)
555 memcpy(override_name, buf, count);
john stultz734efb42006-06-26 00:25:05 -0700556 override_name[count] = 0;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200557 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700558
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200559 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700560
561 return ret;
562}
563
564/**
565 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
566 * @dev: unused
567 * @buf: char buffer to be filled with clocksource list
568 *
569 * Provides sysfs interface for listing registered clocksources
570 */
571static ssize_t
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200572sysfs_show_available_clocksources(struct sys_device *dev,
573 struct sysdev_attribute *attr,
574 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700575{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700576 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -0800577 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700578
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200579 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700580 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200581 /*
582 * Don't show non-HRES clocksource if the tick code is
583 * in one shot mode (highres=on or nohz=on)
584 */
585 if (!tick_oneshot_mode_active() ||
586 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -0700587 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -0800588 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
589 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -0700590 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200591 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700592
Miao Xie5e2cb102008-02-06 01:36:53 -0800593 count += snprintf(buf + count,
594 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -0700595
Miao Xie5e2cb102008-02-06 01:36:53 -0800596 return count;
john stultz734efb42006-06-26 00:25:05 -0700597}
598
599/*
600 * Sysfs setup bits:
601 */
Heiko Carstens4f95f812008-05-03 14:23:14 +0200602static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800603 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700604
Heiko Carstens4f95f812008-05-03 14:23:14 +0200605static SYSDEV_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800606 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -0700607
608static struct sysdev_class clocksource_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100609 .name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -0700610};
611
612static struct sys_device device_clocksource = {
613 .id = 0,
614 .cls = &clocksource_sysclass,
615};
616
john stultzad596172006-06-26 00:25:06 -0700617static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -0700618{
619 int error = sysdev_class_register(&clocksource_sysclass);
620
621 if (!error)
622 error = sysdev_register(&device_clocksource);
623 if (!error)
624 error = sysdev_create_file(
625 &device_clocksource,
626 &attr_current_clocksource);
627 if (!error)
628 error = sysdev_create_file(
629 &device_clocksource,
630 &attr_available_clocksource);
631 return error;
632}
633
634device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -0800635#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -0700636
637/**
638 * boot_override_clocksource - boot clock override
639 * @str: override name
640 *
641 * Takes a clocksource= boot argument and uses it
642 * as the clocksource override name.
643 */
644static int __init boot_override_clocksource(char* str)
645{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200646 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700647 if (str)
648 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200649 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700650 return 1;
651}
652
653__setup("clocksource=", boot_override_clocksource);
654
655/**
656 * boot_override_clock - Compatibility layer for deprecated boot option
657 * @str: override name
658 *
659 * DEPRECATED! Takes a clock= boot argument and uses it
660 * as the clocksource override name
661 */
662static int __init boot_override_clock(char* str)
663{
john stultz5d0cf412006-06-26 00:25:12 -0700664 if (!strcmp(str, "pmtmr")) {
665 printk("Warning: clock=pmtmr is deprecated. "
666 "Use clocksource=acpi_pm.\n");
667 return boot_override_clocksource("acpi_pm");
668 }
669 printk("Warning! clock= boot option is deprecated. "
670 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -0700671 return boot_override_clocksource(str);
672}
673
674__setup("clock=", boot_override_clock);