blob: 18b9f5da4ee95a0887993b0ae1155ec6f44da01c [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
24 * o get rid of clocksource_jiffies extern
25 */
26
27#include <linux/clocksource.h>
28#include <linux/sysdev.h>
29#include <linux/init.h>
30#include <linux/module.h>
Mathieu Desnoyersdc29a362007-02-10 01:43:43 -080031#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080032#include <linux/tick.h>
john stultz3f685352009-01-21 22:53:22 -070033#include <linux/hrtimer.h>
john stultz734efb42006-06-26 00:25:05 -070034
Patrick Ohlya038a352009-02-12 05:03:34 +000035void timecounter_init(struct timecounter *tc,
36 const struct cyclecounter *cc,
37 u64 start_tstamp)
38{
39 tc->cc = cc;
40 tc->cycle_last = cc->read(cc);
41 tc->nsec = start_tstamp;
42}
43EXPORT_SYMBOL(timecounter_init);
44
45/**
46 * timecounter_read_delta - get nanoseconds since last call of this function
47 * @tc: Pointer to time counter
48 *
49 * When the underlying cycle counter runs over, this will be handled
50 * correctly as long as it does not run over more than once between
51 * calls.
52 *
53 * The first call to this function for a new time counter initializes
54 * the time tracking and returns an undefined result.
55 */
56static u64 timecounter_read_delta(struct timecounter *tc)
57{
58 cycle_t cycle_now, cycle_delta;
59 u64 ns_offset;
60
61 /* read cycle counter: */
62 cycle_now = tc->cc->read(tc->cc);
63
64 /* calculate the delta since the last timecounter_read_delta(): */
65 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
66
67 /* convert to nanoseconds: */
68 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
69
70 /* update time stamp of timecounter_read_delta() call: */
71 tc->cycle_last = cycle_now;
72
73 return ns_offset;
74}
75
76u64 timecounter_read(struct timecounter *tc)
77{
78 u64 nsec;
79
80 /* increment time by nanoseconds since last call */
81 nsec = timecounter_read_delta(tc);
82 nsec += tc->nsec;
83 tc->nsec = nsec;
84
85 return nsec;
86}
87EXPORT_SYMBOL(timecounter_read);
88
89u64 timecounter_cyc2time(struct timecounter *tc,
90 cycle_t cycle_tstamp)
91{
92 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
93 u64 nsec;
94
95 /*
96 * Instead of always treating cycle_tstamp as more recent
97 * than tc->cycle_last, detect when it is too far in the
98 * future and treat it as old time stamp instead.
99 */
100 if (cycle_delta > tc->cc->mask / 2) {
101 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
102 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
103 } else {
104 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
105 }
106
107 return nsec;
108}
109EXPORT_SYMBOL(timecounter_cyc2time);
110
john stultz734efb42006-06-26 00:25:05 -0700111/* XXX - Would like a better way for initializing curr_clocksource */
112extern struct clocksource clocksource_jiffies;
113
114/*[Clocksource internal variables]---------
115 * curr_clocksource:
116 * currently selected clocksource. Initialized to clocksource_jiffies.
117 * next_clocksource:
118 * pending next selected clocksource.
119 * clocksource_list:
120 * linked list with the registered clocksources
121 * clocksource_lock:
122 * protects manipulations to curr_clocksource and next_clocksource
123 * and the clocksource_list
124 * override_name:
125 * Name of the user-specified clocksource.
126 */
127static struct clocksource *curr_clocksource = &clocksource_jiffies;
128static struct clocksource *next_clocksource;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800129static struct clocksource *clocksource_override;
john stultz734efb42006-06-26 00:25:05 -0700130static LIST_HEAD(clocksource_list);
131static DEFINE_SPINLOCK(clocksource_lock);
132static char override_name[32];
133static int finished_booting;
134
john stultz6bb74df2007-03-05 00:30:50 -0800135/* clocksource_done_booting - Called near the end of core bootup
john stultz734efb42006-06-26 00:25:05 -0700136 *
john stultz6bb74df2007-03-05 00:30:50 -0800137 * Hack to avoid lots of clocksource churn at boot time.
138 * We use fs_initcall because we want this to start before
139 * device_initcall but after subsys_initcall.
john stultz734efb42006-06-26 00:25:05 -0700140 */
john stultzad596172006-06-26 00:25:06 -0700141static int __init clocksource_done_booting(void)
john stultz734efb42006-06-26 00:25:05 -0700142{
143 finished_booting = 1;
144 return 0;
145}
john stultz6bb74df2007-03-05 00:30:50 -0800146fs_initcall(clocksource_done_booting);
john stultz734efb42006-06-26 00:25:05 -0700147
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800148#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
149static LIST_HEAD(watchdog_list);
150static struct clocksource *watchdog;
151static struct timer_list watchdog_timer;
152static DEFINE_SPINLOCK(watchdog_lock);
153static cycle_t watchdog_last;
Thomas Gleixner8f894412007-05-15 01:41:32 -0700154static unsigned long watchdog_resumed;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700155
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800156/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700157 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800158 */
159#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700160#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800161
162static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
163{
Daniel Walker35c35d12007-05-09 02:33:40 -0700164 if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800165 return;
166
167 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
168 cs->name, delta);
169 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
170 clocksource_change_rating(cs, 0);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800171 list_del(&cs->wd_list);
172}
173
174static void clocksource_watchdog(unsigned long data)
175{
176 struct clocksource *cs, *tmp;
177 cycle_t csnow, wdnow;
178 int64_t wd_nsec, cs_nsec;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700179 int resumed;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800180
181 spin_lock(&watchdog_lock);
182
Thomas Gleixner8f894412007-05-15 01:41:32 -0700183 resumed = test_and_clear_bit(0, &watchdog_resumed);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700184
Magnus Damm8e196082009-04-21 12:24:00 -0700185 wdnow = watchdog->read(watchdog);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800186 wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
187 watchdog_last = wdnow;
188
189 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
Magnus Damm8e196082009-04-21 12:24:00 -0700190 csnow = cs->read(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700191
192 if (unlikely(resumed)) {
193 cs->wd_last = csnow;
194 continue;
195 }
196
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800197 /* Initialized ? */
198 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
199 if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
200 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
201 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800202 /*
203 * We just marked the clocksource as
204 * highres-capable, notify the rest of the
205 * system as well so that we transition
206 * into high-res mode:
207 */
208 tick_clock_notify();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800209 }
210 cs->flags |= CLOCK_SOURCE_WATCHDOG;
211 cs->wd_last = csnow;
212 } else {
213 cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
214 cs->wd_last = csnow;
215 /* Check the delta. Might remove from the list ! */
216 clocksource_ratewd(cs, cs_nsec - wd_nsec);
217 }
218 }
219
220 if (!list_empty(&watchdog_list)) {
Andi Kleen6993fc52008-01-30 13:30:02 +0100221 /*
222 * Cycle through CPUs to check if the CPUs stay
223 * synchronized to each other.
224 */
Rusty Russell5db0e1e2009-01-01 10:12:29 +1030225 int next_cpu = cpumask_next(raw_smp_processor_id(),
226 cpu_online_mask);
Andi Kleen6993fc52008-01-30 13:30:02 +0100227
Mike Traviscad0e452008-05-12 21:21:13 +0200228 if (next_cpu >= nr_cpu_ids)
Rusty Russell6b954822009-01-01 10:12:25 +1030229 next_cpu = cpumask_first(cpu_online_mask);
Andi Kleen6993fc52008-01-30 13:30:02 +0100230 watchdog_timer.expires += WATCHDOG_INTERVAL;
231 add_timer_on(&watchdog_timer, next_cpu);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800232 }
233 spin_unlock(&watchdog_lock);
234}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700235static void clocksource_resume_watchdog(void)
236{
Thomas Gleixner8f894412007-05-15 01:41:32 -0700237 set_bit(0, &watchdog_resumed);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700238}
239
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800240static void clocksource_check_watchdog(struct clocksource *cs)
241{
242 struct clocksource *cse;
243 unsigned long flags;
244
245 spin_lock_irqsave(&watchdog_lock, flags);
246 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
247 int started = !list_empty(&watchdog_list);
248
249 list_add(&cs->wd_list, &watchdog_list);
250 if (!started && watchdog) {
Magnus Damm8e196082009-04-21 12:24:00 -0700251 watchdog_last = watchdog->read(watchdog);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800252 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
Andi Kleen6993fc52008-01-30 13:30:02 +0100253 add_timer_on(&watchdog_timer,
Rusty Russell6b954822009-01-01 10:12:25 +1030254 cpumask_first(cpu_online_mask));
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800255 }
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200256 } else {
257 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800258 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
259
260 if (!watchdog || cs->rating > watchdog->rating) {
261 if (watchdog)
262 del_timer(&watchdog_timer);
263 watchdog = cs;
Thomas Gleixner898a19d2008-03-25 09:01:51 +0100264 init_timer(&watchdog_timer);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800265 watchdog_timer.function = clocksource_watchdog;
266
267 /* Reset watchdog cycles */
268 list_for_each_entry(cse, &watchdog_list, wd_list)
269 cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
270 /* Start if list is not empty */
271 if (!list_empty(&watchdog_list)) {
Magnus Damm8e196082009-04-21 12:24:00 -0700272 watchdog_last = watchdog->read(watchdog);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800273 watchdog_timer.expires =
274 jiffies + WATCHDOG_INTERVAL;
Andi Kleen6993fc52008-01-30 13:30:02 +0100275 add_timer_on(&watchdog_timer,
Rusty Russell6b954822009-01-01 10:12:25 +1030276 cpumask_first(cpu_online_mask));
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800277 }
278 }
279 }
280 spin_unlock_irqrestore(&watchdog_lock, flags);
281}
282#else
283static void clocksource_check_watchdog(struct clocksource *cs)
284{
285 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
286 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
287}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700288
289static inline void clocksource_resume_watchdog(void) { }
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800290#endif
291
john stultz734efb42006-06-26 00:25:05 -0700292/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700293 * clocksource_resume - resume the clocksource(s)
294 */
295void clocksource_resume(void)
296{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700297 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700298 unsigned long flags;
299
300 spin_lock_irqsave(&clocksource_lock, flags);
301
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700302 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700303 if (cs->resume)
304 cs->resume();
305 }
306
307 clocksource_resume_watchdog();
308
309 spin_unlock_irqrestore(&clocksource_lock, flags);
310}
311
312/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600313 * clocksource_touch_watchdog - Update watchdog
314 *
315 * Update the watchdog after exception contexts such as kgdb so as not
316 * to incorrectly trip the watchdog.
317 *
318 */
319void clocksource_touch_watchdog(void)
320{
321 clocksource_resume_watchdog();
322}
323
324/**
john stultza2752542006-06-26 00:25:14 -0700325 * clocksource_get_next - Returns the selected clocksource
john stultz734efb42006-06-26 00:25:05 -0700326 *
327 */
john stultza2752542006-06-26 00:25:14 -0700328struct clocksource *clocksource_get_next(void)
john stultz734efb42006-06-26 00:25:05 -0700329{
330 unsigned long flags;
331
332 spin_lock_irqsave(&clocksource_lock, flags);
333 if (next_clocksource && finished_booting) {
334 curr_clocksource = next_clocksource;
335 next_clocksource = NULL;
336 }
337 spin_unlock_irqrestore(&clocksource_lock, flags);
338
339 return curr_clocksource;
340}
341
342/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800343 * select_clocksource - Selects the best registered clocksource.
john stultz734efb42006-06-26 00:25:05 -0700344 *
345 * Private function. Must hold clocksource_lock when called.
346 *
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800347 * Select the clocksource with the best rating, or the clocksource,
348 * which is selected by userspace override.
john stultz734efb42006-06-26 00:25:05 -0700349 */
350static struct clocksource *select_clocksource(void)
351{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800352 struct clocksource *next;
353
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800354 if (list_empty(&clocksource_list))
355 return NULL;
john stultz734efb42006-06-26 00:25:05 -0700356
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800357 if (clocksource_override)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800358 next = clocksource_override;
359 else
360 next = list_entry(clocksource_list.next, struct clocksource,
361 list);
john stultz734efb42006-06-26 00:25:05 -0700362
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800363 if (next == curr_clocksource)
364 return NULL;
365
366 return next;
john stultz734efb42006-06-26 00:25:05 -0700367}
368
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800369/*
370 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700371 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800372static int clocksource_enqueue(struct clocksource *c)
john stultz734efb42006-06-26 00:25:05 -0700373{
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800374 struct list_head *tmp, *entry = &clocksource_list;
john stultz734efb42006-06-26 00:25:05 -0700375
376 list_for_each(tmp, &clocksource_list) {
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800377 struct clocksource *cs;
john stultz734efb42006-06-26 00:25:05 -0700378
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800379 cs = list_entry(tmp, struct clocksource, list);
380 if (cs == c)
381 return -EBUSY;
382 /* Keep track of the place, where to insert */
383 if (cs->rating >= c->rating)
384 entry = tmp;
john stultz734efb42006-06-26 00:25:05 -0700385 }
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800386 list_add(&c->list, entry);
387
388 if (strlen(c->name) == strlen(override_name) &&
389 !strcmp(c->name, override_name))
390 clocksource_override = c;
john stultz734efb42006-06-26 00:25:05 -0700391
392 return 0;
393}
394
395/**
john stultza2752542006-06-26 00:25:14 -0700396 * clocksource_register - Used to install new clocksources
john stultz734efb42006-06-26 00:25:05 -0700397 * @t: clocksource to be registered
398 *
399 * Returns -EBUSY if registration fails, zero otherwise.
400 */
john stultza2752542006-06-26 00:25:14 -0700401int clocksource_register(struct clocksource *c)
john stultz734efb42006-06-26 00:25:05 -0700402{
john stultz734efb42006-06-26 00:25:05 -0700403 unsigned long flags;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800404 int ret;
john stultz734efb42006-06-26 00:25:05 -0700405
406 spin_lock_irqsave(&clocksource_lock, flags);
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800407 ret = clocksource_enqueue(c);
408 if (!ret)
john stultz734efb42006-06-26 00:25:05 -0700409 next_clocksource = select_clocksource();
john stultz734efb42006-06-26 00:25:05 -0700410 spin_unlock_irqrestore(&clocksource_lock, flags);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800411 if (!ret)
412 clocksource_check_watchdog(c);
john stultz734efb42006-06-26 00:25:05 -0700413 return ret;
414}
john stultza2752542006-06-26 00:25:14 -0700415EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700416
417/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800418 * clocksource_change_rating - Change the rating of a registered clocksource
john stultz734efb42006-06-26 00:25:05 -0700419 *
john stultz734efb42006-06-26 00:25:05 -0700420 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800421void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700422{
423 unsigned long flags;
424
425 spin_lock_irqsave(&clocksource_lock, flags);
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800426 list_del(&cs->list);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800427 cs->rating = rating;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800428 clocksource_enqueue(cs);
john stultz734efb42006-06-26 00:25:05 -0700429 next_clocksource = select_clocksource();
430 spin_unlock_irqrestore(&clocksource_lock, flags);
431}
432
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100433/**
434 * clocksource_unregister - remove a registered clocksource
435 */
436void clocksource_unregister(struct clocksource *cs)
437{
438 unsigned long flags;
439
440 spin_lock_irqsave(&clocksource_lock, flags);
441 list_del(&cs->list);
442 if (clocksource_override == cs)
443 clocksource_override = NULL;
444 next_clocksource = select_clocksource();
445 spin_unlock_irqrestore(&clocksource_lock, flags);
446}
447
Daniel Walker2b013702006-12-10 02:21:30 -0800448#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700449/**
450 * sysfs_show_current_clocksources - sysfs interface for current clocksource
451 * @dev: unused
452 * @buf: char buffer to be filled with clocksource list
453 *
454 * Provides sysfs interface for listing current clocksource.
455 */
456static ssize_t
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200457sysfs_show_current_clocksources(struct sys_device *dev,
458 struct sysdev_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700459{
Miao Xie5e2cb102008-02-06 01:36:53 -0800460 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700461
462 spin_lock_irq(&clocksource_lock);
Miao Xie5e2cb102008-02-06 01:36:53 -0800463 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
john stultz734efb42006-06-26 00:25:05 -0700464 spin_unlock_irq(&clocksource_lock);
465
Miao Xie5e2cb102008-02-06 01:36:53 -0800466 return count;
john stultz734efb42006-06-26 00:25:05 -0700467}
468
469/**
470 * sysfs_override_clocksource - interface for manually overriding clocksource
471 * @dev: unused
472 * @buf: name of override clocksource
473 * @count: length of buffer
474 *
475 * Takes input from sysfs interface for manually overriding the default
476 * clocksource selction.
477 */
478static ssize_t sysfs_override_clocksource(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200479 struct sysdev_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700480 const char *buf, size_t count)
481{
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800482 struct clocksource *ovr = NULL;
john stultz734efb42006-06-26 00:25:05 -0700483 size_t ret = count;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800484 int len;
485
john stultz734efb42006-06-26 00:25:05 -0700486 /* strings from sysfs write are not 0 terminated! */
487 if (count >= sizeof(override_name))
488 return -EINVAL;
489
490 /* strip of \n: */
491 if (buf[count-1] == '\n')
492 count--;
john stultz734efb42006-06-26 00:25:05 -0700493
494 spin_lock_irq(&clocksource_lock);
495
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800496 if (count > 0)
497 memcpy(override_name, buf, count);
john stultz734efb42006-06-26 00:25:05 -0700498 override_name[count] = 0;
499
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800500 len = strlen(override_name);
501 if (len) {
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700502 struct clocksource *cs;
503
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800504 ovr = clocksource_override;
505 /* try to select it: */
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700506 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800507 if (strlen(cs->name) == len &&
508 !strcmp(cs->name, override_name))
509 ovr = cs;
510 }
511 }
512
john stultz3f685352009-01-21 22:53:22 -0700513 /*
514 * Check to make sure we don't switch to a non-HRT usable
515 * clocksource if HRT is enabled and running
516 */
517 if (hrtimer_hres_active() &&
518 !(ovr->flags & CLOCK_SOURCE_VALID_FOR_HRES)) {
519 printk(KERN_WARNING "%s clocksource is not HRT compatible. "
520 "Cannot switch while in HRT mode\n", ovr->name);
521 ovr = NULL;
522 override_name[0] = 0;
523 }
524
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800525 /* Reselect, when the override name has changed */
526 if (ovr != clocksource_override) {
527 clocksource_override = ovr;
528 next_clocksource = select_clocksource();
529 }
john stultz734efb42006-06-26 00:25:05 -0700530
531 spin_unlock_irq(&clocksource_lock);
532
533 return ret;
534}
535
536/**
537 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
538 * @dev: unused
539 * @buf: char buffer to be filled with clocksource list
540 *
541 * Provides sysfs interface for listing registered clocksources
542 */
543static ssize_t
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200544sysfs_show_available_clocksources(struct sys_device *dev,
545 struct sysdev_attribute *attr,
546 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700547{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700548 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -0800549 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700550
551 spin_lock_irq(&clocksource_lock);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700552 list_for_each_entry(src, &clocksource_list, list) {
john stultz3f685352009-01-21 22:53:22 -0700553 /* Don't show non-HRES clocksource if HRES is enabled */
554 if (!hrtimer_hres_active() ||
555 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
556 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -0800557 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
558 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -0700559 }
560 spin_unlock_irq(&clocksource_lock);
561
Miao Xie5e2cb102008-02-06 01:36:53 -0800562 count += snprintf(buf + count,
563 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -0700564
Miao Xie5e2cb102008-02-06 01:36:53 -0800565 return count;
john stultz734efb42006-06-26 00:25:05 -0700566}
567
568/*
569 * Sysfs setup bits:
570 */
Heiko Carstens4f95f812008-05-03 14:23:14 +0200571static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800572 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700573
Heiko Carstens4f95f812008-05-03 14:23:14 +0200574static SYSDEV_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800575 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -0700576
577static struct sysdev_class clocksource_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100578 .name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -0700579};
580
581static struct sys_device device_clocksource = {
582 .id = 0,
583 .cls = &clocksource_sysclass,
584};
585
john stultzad596172006-06-26 00:25:06 -0700586static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -0700587{
588 int error = sysdev_class_register(&clocksource_sysclass);
589
590 if (!error)
591 error = sysdev_register(&device_clocksource);
592 if (!error)
593 error = sysdev_create_file(
594 &device_clocksource,
595 &attr_current_clocksource);
596 if (!error)
597 error = sysdev_create_file(
598 &device_clocksource,
599 &attr_available_clocksource);
600 return error;
601}
602
603device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -0800604#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -0700605
606/**
607 * boot_override_clocksource - boot clock override
608 * @str: override name
609 *
610 * Takes a clocksource= boot argument and uses it
611 * as the clocksource override name.
612 */
613static int __init boot_override_clocksource(char* str)
614{
615 unsigned long flags;
616 spin_lock_irqsave(&clocksource_lock, flags);
617 if (str)
618 strlcpy(override_name, str, sizeof(override_name));
619 spin_unlock_irqrestore(&clocksource_lock, flags);
620 return 1;
621}
622
623__setup("clocksource=", boot_override_clocksource);
624
625/**
626 * boot_override_clock - Compatibility layer for deprecated boot option
627 * @str: override name
628 *
629 * DEPRECATED! Takes a clock= boot argument and uses it
630 * as the clocksource override name
631 */
632static int __init boot_override_clock(char* str)
633{
john stultz5d0cf412006-06-26 00:25:12 -0700634 if (!strcmp(str, "pmtmr")) {
635 printk("Warning: clock=pmtmr is deprecated. "
636 "Use clocksource=acpi_pm.\n");
637 return boot_override_clocksource("acpi_pm");
638 }
639 printk("Warning! clock= boot option is deprecated. "
640 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -0700641 return boot_override_clocksource(str);
642}
643
644__setup("clock=", boot_override_clock);