blob: f18c9a6bdcf457b0da77b0f9ad83527504a62a32 [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 * next_clocksource:
113 * pending next selected clocksource.
114 * clocksource_list:
115 * linked list with the registered clocksources
116 * clocksource_lock:
117 * protects manipulations to curr_clocksource and next_clocksource
118 * and the clocksource_list
119 * override_name:
120 * Name of the user-specified clocksource.
121 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200122static struct clocksource *curr_clocksource;
john stultz734efb42006-06-26 00:25:05 -0700123static struct clocksource *next_clocksource;
124static LIST_HEAD(clocksource_list);
125static DEFINE_SPINLOCK(clocksource_lock);
126static char override_name[32];
127static int finished_booting;
128
john stultz6bb74df2007-03-05 00:30:50 -0800129/* clocksource_done_booting - Called near the end of core bootup
john stultz734efb42006-06-26 00:25:05 -0700130 *
john stultz6bb74df2007-03-05 00:30:50 -0800131 * Hack to avoid lots of clocksource churn at boot time.
132 * We use fs_initcall because we want this to start before
133 * device_initcall but after subsys_initcall.
john stultz734efb42006-06-26 00:25:05 -0700134 */
john stultzad596172006-06-26 00:25:06 -0700135static int __init clocksource_done_booting(void)
john stultz734efb42006-06-26 00:25:05 -0700136{
137 finished_booting = 1;
138 return 0;
139}
john stultz6bb74df2007-03-05 00:30:50 -0800140fs_initcall(clocksource_done_booting);
john stultz734efb42006-06-26 00:25:05 -0700141
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800142#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
143static LIST_HEAD(watchdog_list);
144static struct clocksource *watchdog;
145static struct timer_list watchdog_timer;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200146static struct work_struct watchdog_work;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800147static DEFINE_SPINLOCK(watchdog_lock);
148static cycle_t watchdog_last;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200149static int watchdog_running;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700150
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200151static void clocksource_watchdog_work(struct work_struct *work);
152
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800153/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700154 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800155 */
156#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700157#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800158
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200159static void clocksource_unstable(struct clocksource *cs, int64_t delta)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800160{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800161 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
162 cs->name, delta);
163 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200164 cs->flags |= CLOCK_SOURCE_UNSTABLE;
165 schedule_work(&watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800166}
167
168static void clocksource_watchdog(unsigned long data)
169{
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200170 struct clocksource *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800171 cycle_t csnow, wdnow;
172 int64_t wd_nsec, cs_nsec;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200173 int next_cpu;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800174
175 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200176 if (!watchdog_running)
177 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800178
Magnus Damm8e196082009-04-21 12:24:00 -0700179 wdnow = watchdog->read(watchdog);
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200180 wd_nsec = clocksource_cyc2ns((wdnow - watchdog_last) & watchdog->mask,
181 watchdog->mult, watchdog->shift);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800182 watchdog_last = wdnow;
183
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200184 list_for_each_entry(cs, &watchdog_list, wd_list) {
185
186 /* Clocksource already marked unstable? */
187 if (cs->flags & CLOCK_SOURCE_UNSTABLE)
188 continue;
189
Magnus Damm8e196082009-04-21 12:24:00 -0700190 csnow = cs->read(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700191
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200192 /* Clocksource initialized ? */
193 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
194 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700195 cs->wd_last = csnow;
196 continue;
197 }
198
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200199 /* Check the deviation from the watchdog clocksource. */
Martin Schwidefsky155ec602009-08-14 15:47:26 +0200200 cs_nsec = clocksource_cyc2ns((csnow - cs->wd_last) &
201 cs->mask, cs->mult, cs->shift);
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200202 cs->wd_last = csnow;
203 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
204 clocksource_unstable(cs, cs_nsec - wd_nsec);
205 continue;
206 }
207
208 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
209 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
210 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
211 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
212 /*
213 * We just marked the clocksource as highres-capable,
214 * notify the rest of the system as well so that we
215 * transition into high-res mode:
216 */
217 tick_clock_notify();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800218 }
219 }
220
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200221 /*
222 * Cycle through CPUs to check if the CPUs stay synchronized
223 * to each other.
224 */
225 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
226 if (next_cpu >= nr_cpu_ids)
227 next_cpu = cpumask_first(cpu_online_mask);
228 watchdog_timer.expires += WATCHDOG_INTERVAL;
229 add_timer_on(&watchdog_timer, next_cpu);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200230out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800231 spin_unlock(&watchdog_lock);
232}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200233
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200234static inline void clocksource_start_watchdog(void)
235{
236 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
237 return;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200238 INIT_WORK(&watchdog_work, clocksource_watchdog_work);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200239 init_timer(&watchdog_timer);
240 watchdog_timer.function = clocksource_watchdog;
241 watchdog_last = watchdog->read(watchdog);
242 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
243 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
244 watchdog_running = 1;
245}
246
247static inline void clocksource_stop_watchdog(void)
248{
249 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
250 return;
251 del_timer(&watchdog_timer);
252 watchdog_running = 0;
253}
254
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200255static inline void clocksource_reset_watchdog(void)
256{
257 struct clocksource *cs;
258
259 list_for_each_entry(cs, &watchdog_list, wd_list)
260 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
261}
262
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700263static void clocksource_resume_watchdog(void)
264{
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200265 unsigned long flags;
266
267 spin_lock_irqsave(&watchdog_lock, flags);
268 clocksource_reset_watchdog();
269 spin_unlock_irqrestore(&watchdog_lock, flags);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700270}
271
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200272static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800273{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800274 unsigned long flags;
275
276 spin_lock_irqsave(&watchdog_lock, flags);
277 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200278 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800279 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200280 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200281 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200282 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200283 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800284 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200285 /* Pick the best watchdog. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800286 if (!watchdog || cs->rating > watchdog->rating) {
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800287 watchdog = cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800288 /* Reset watchdog cycles */
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200289 clocksource_reset_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800290 }
291 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200292 /* Check if the watchdog timer needs to be started. */
293 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800294 spin_unlock_irqrestore(&watchdog_lock, flags);
295}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200296
297static void clocksource_dequeue_watchdog(struct clocksource *cs)
298{
299 struct clocksource *tmp;
300 unsigned long flags;
301
302 spin_lock_irqsave(&watchdog_lock, flags);
303 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
304 /* cs is a watched clocksource. */
305 list_del_init(&cs->wd_list);
306 } else if (cs == watchdog) {
307 /* Reset watchdog cycles */
308 clocksource_reset_watchdog();
309 /* Current watchdog is removed. Find an alternative. */
310 watchdog = NULL;
311 list_for_each_entry(tmp, &clocksource_list, list) {
312 if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
313 continue;
314 if (!watchdog || tmp->rating > watchdog->rating)
315 watchdog = tmp;
316 }
317 }
318 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
319 /* Check if the watchdog timer needs to be stopped. */
320 clocksource_stop_watchdog();
321 spin_unlock_irqrestore(&watchdog_lock, flags);
322}
323
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200324static void clocksource_watchdog_work(struct work_struct *work)
325{
326 struct clocksource *cs, *tmp;
327 unsigned long flags;
328
329 spin_lock_irqsave(&watchdog_lock, flags);
330 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
331 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
332 list_del_init(&cs->wd_list);
333 clocksource_change_rating(cs, 0);
334 }
335 /* Check if the watchdog timer needs to be stopped. */
336 clocksource_stop_watchdog();
337 spin_unlock(&watchdog_lock);
338}
339
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200340#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
341
342static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800343{
344 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
345 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
346}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700347
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200348static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700349static inline void clocksource_resume_watchdog(void) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200350
351#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800352
john stultz734efb42006-06-26 00:25:05 -0700353/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700354 * clocksource_resume - resume the clocksource(s)
355 */
356void clocksource_resume(void)
357{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700358 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700359 unsigned long flags;
360
361 spin_lock_irqsave(&clocksource_lock, flags);
362
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700363 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700364 if (cs->resume)
365 cs->resume();
366 }
367
368 clocksource_resume_watchdog();
369
370 spin_unlock_irqrestore(&clocksource_lock, flags);
371}
372
373/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600374 * clocksource_touch_watchdog - Update watchdog
375 *
376 * Update the watchdog after exception contexts such as kgdb so as not
377 * to incorrectly trip the watchdog.
378 *
379 */
380void clocksource_touch_watchdog(void)
381{
382 clocksource_resume_watchdog();
383}
384
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200385#ifdef CONFIG_GENERIC_TIME
Jason Wessel7c3078b2008-02-15 14:55:54 -0600386/**
john stultza2752542006-06-26 00:25:14 -0700387 * clocksource_get_next - Returns the selected clocksource
john stultz734efb42006-06-26 00:25:05 -0700388 *
389 */
john stultza2752542006-06-26 00:25:14 -0700390struct clocksource *clocksource_get_next(void)
john stultz734efb42006-06-26 00:25:05 -0700391{
392 unsigned long flags;
393
394 spin_lock_irqsave(&clocksource_lock, flags);
395 if (next_clocksource && finished_booting) {
396 curr_clocksource = next_clocksource;
397 next_clocksource = NULL;
398 }
399 spin_unlock_irqrestore(&clocksource_lock, flags);
400
401 return curr_clocksource;
402}
403
404/**
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200405 * clocksource_select - Select the best clocksource available
john stultz734efb42006-06-26 00:25:05 -0700406 *
407 * Private function. Must hold clocksource_lock when called.
408 *
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800409 * Select the clocksource with the best rating, or the clocksource,
410 * which is selected by userspace override.
john stultz734efb42006-06-26 00:25:05 -0700411 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200412static void clocksource_select(void)
john stultz734efb42006-06-26 00:25:05 -0700413{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200414 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800415
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800416 if (list_empty(&clocksource_list))
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200417 return;
418 /* First clocksource on the list has the best rating. */
419 best = list_first_entry(&clocksource_list, struct clocksource, list);
420 /* Check for the override clocksource. */
421 list_for_each_entry(cs, &clocksource_list, list) {
422 if (strcmp(cs->name, override_name) != 0)
423 continue;
424 /*
425 * Check to make sure we don't switch to a non-highres
426 * capable clocksource if the tick code is in oneshot
427 * mode (highres or nohz)
428 */
429 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
430 tick_oneshot_mode_active()) {
431 /* Override clocksource cannot be used. */
432 printk(KERN_WARNING "Override clocksource %s is not "
433 "HRT compatible. Cannot switch while in "
434 "HRT/NOHZ mode\n", cs->name);
435 override_name[0] = 0;
436 } else
437 /* Override clocksource can be used. */
438 best = cs;
439 break;
440 }
441 if (curr_clocksource != best)
442 next_clocksource = best;
john stultz734efb42006-06-26 00:25:05 -0700443}
444
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200445#else /* CONFIG_GENERIC_TIME */
446
447static void clocksource_select(void) { }
448
449#endif
450
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800451/*
452 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700453 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200454static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700455{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200456 struct list_head *entry = &clocksource_list;
457 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700458
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200459 list_for_each_entry(tmp, &clocksource_list, list)
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800460 /* Keep track of the place, where to insert */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200461 if (tmp->rating >= cs->rating)
462 entry = &tmp->list;
463 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700464}
465
466/**
john stultza2752542006-06-26 00:25:14 -0700467 * clocksource_register - Used to install new clocksources
john stultz734efb42006-06-26 00:25:05 -0700468 * @t: clocksource to be registered
469 *
470 * Returns -EBUSY if registration fails, zero otherwise.
471 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200472int clocksource_register(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700473{
john stultz734efb42006-06-26 00:25:05 -0700474 unsigned long flags;
475
476 spin_lock_irqsave(&clocksource_lock, flags);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200477 clocksource_enqueue(cs);
478 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700479 spin_unlock_irqrestore(&clocksource_lock, flags);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200480 clocksource_enqueue_watchdog(cs);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200481 return 0;
john stultz734efb42006-06-26 00:25:05 -0700482}
john stultza2752542006-06-26 00:25:14 -0700483EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700484
485/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800486 * clocksource_change_rating - Change the rating of a registered clocksource
john stultz734efb42006-06-26 00:25:05 -0700487 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800488void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700489{
490 unsigned long flags;
491
492 spin_lock_irqsave(&clocksource_lock, flags);
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800493 list_del(&cs->list);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800494 cs->rating = rating;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800495 clocksource_enqueue(cs);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200496 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700497 spin_unlock_irqrestore(&clocksource_lock, flags);
498}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200499EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700500
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100501/**
502 * clocksource_unregister - remove a registered clocksource
503 */
504void clocksource_unregister(struct clocksource *cs)
505{
506 unsigned long flags;
507
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200508 clocksource_dequeue_watchdog(cs);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100509 spin_lock_irqsave(&clocksource_lock, flags);
510 list_del(&cs->list);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200511 clocksource_select();
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100512 spin_unlock_irqrestore(&clocksource_lock, flags);
513}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200514EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100515
Daniel Walker2b013702006-12-10 02:21:30 -0800516#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700517/**
518 * sysfs_show_current_clocksources - sysfs interface for current clocksource
519 * @dev: unused
520 * @buf: char buffer to be filled with clocksource list
521 *
522 * Provides sysfs interface for listing current clocksource.
523 */
524static ssize_t
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200525sysfs_show_current_clocksources(struct sys_device *dev,
526 struct sysdev_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700527{
Miao Xie5e2cb102008-02-06 01:36:53 -0800528 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700529
530 spin_lock_irq(&clocksource_lock);
Miao Xie5e2cb102008-02-06 01:36:53 -0800531 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
john stultz734efb42006-06-26 00:25:05 -0700532 spin_unlock_irq(&clocksource_lock);
533
Miao Xie5e2cb102008-02-06 01:36:53 -0800534 return count;
john stultz734efb42006-06-26 00:25:05 -0700535}
536
537/**
538 * sysfs_override_clocksource - interface for manually overriding clocksource
539 * @dev: unused
540 * @buf: name of override clocksource
541 * @count: length of buffer
542 *
543 * Takes input from sysfs interface for manually overriding the default
544 * clocksource selction.
545 */
546static ssize_t sysfs_override_clocksource(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200547 struct sysdev_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700548 const char *buf, size_t count)
549{
550 size_t ret = count;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800551
john stultz734efb42006-06-26 00:25:05 -0700552 /* strings from sysfs write are not 0 terminated! */
553 if (count >= sizeof(override_name))
554 return -EINVAL;
555
556 /* strip of \n: */
557 if (buf[count-1] == '\n')
558 count--;
john stultz734efb42006-06-26 00:25:05 -0700559
560 spin_lock_irq(&clocksource_lock);
561
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800562 if (count > 0)
563 memcpy(override_name, buf, count);
john stultz734efb42006-06-26 00:25:05 -0700564 override_name[count] = 0;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200565 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700566
567 spin_unlock_irq(&clocksource_lock);
568
569 return ret;
570}
571
572/**
573 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
574 * @dev: unused
575 * @buf: char buffer to be filled with clocksource list
576 *
577 * Provides sysfs interface for listing registered clocksources
578 */
579static ssize_t
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200580sysfs_show_available_clocksources(struct sys_device *dev,
581 struct sysdev_attribute *attr,
582 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700583{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700584 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -0800585 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700586
587 spin_lock_irq(&clocksource_lock);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700588 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200589 /*
590 * Don't show non-HRES clocksource if the tick code is
591 * in one shot mode (highres=on or nohz=on)
592 */
593 if (!tick_oneshot_mode_active() ||
594 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -0700595 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -0800596 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
597 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -0700598 }
599 spin_unlock_irq(&clocksource_lock);
600
Miao Xie5e2cb102008-02-06 01:36:53 -0800601 count += snprintf(buf + count,
602 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -0700603
Miao Xie5e2cb102008-02-06 01:36:53 -0800604 return count;
john stultz734efb42006-06-26 00:25:05 -0700605}
606
607/*
608 * Sysfs setup bits:
609 */
Heiko Carstens4f95f812008-05-03 14:23:14 +0200610static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800611 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700612
Heiko Carstens4f95f812008-05-03 14:23:14 +0200613static SYSDEV_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800614 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -0700615
616static struct sysdev_class clocksource_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100617 .name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -0700618};
619
620static struct sys_device device_clocksource = {
621 .id = 0,
622 .cls = &clocksource_sysclass,
623};
624
john stultzad596172006-06-26 00:25:06 -0700625static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -0700626{
627 int error = sysdev_class_register(&clocksource_sysclass);
628
629 if (!error)
630 error = sysdev_register(&device_clocksource);
631 if (!error)
632 error = sysdev_create_file(
633 &device_clocksource,
634 &attr_current_clocksource);
635 if (!error)
636 error = sysdev_create_file(
637 &device_clocksource,
638 &attr_available_clocksource);
639 return error;
640}
641
642device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -0800643#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -0700644
645/**
646 * boot_override_clocksource - boot clock override
647 * @str: override name
648 *
649 * Takes a clocksource= boot argument and uses it
650 * as the clocksource override name.
651 */
652static int __init boot_override_clocksource(char* str)
653{
654 unsigned long flags;
655 spin_lock_irqsave(&clocksource_lock, flags);
656 if (str)
657 strlcpy(override_name, str, sizeof(override_name));
658 spin_unlock_irqrestore(&clocksource_lock, flags);
659 return 1;
660}
661
662__setup("clocksource=", boot_override_clocksource);
663
664/**
665 * boot_override_clock - Compatibility layer for deprecated boot option
666 * @str: override name
667 *
668 * DEPRECATED! Takes a clock= boot argument and uses it
669 * as the clocksource override name
670 */
671static int __init boot_override_clock(char* str)
672{
john stultz5d0cf412006-06-26 00:25:12 -0700673 if (!strcmp(str, "pmtmr")) {
674 printk("Warning: clock=pmtmr is deprecated. "
675 "Use clocksource=acpi_pm.\n");
676 return boot_override_clocksource("acpi_pm");
677 }
678 printk("Warning! clock= boot option is deprecated. "
679 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -0700680 return boot_override_clocksource(str);
681}
682
683__setup("clock=", boot_override_clock);