Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-2.6
[linux-2.6] / kernel / time / clocksource.c
1 /*
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>
31 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
32 #include <linux/tick.h>
33
34 /* XXX - Would like a better way for initializing curr_clocksource */
35 extern struct clocksource clocksource_jiffies;
36
37 /*[Clocksource internal variables]---------
38  * curr_clocksource:
39  *      currently selected clocksource. Initialized to clocksource_jiffies.
40  * next_clocksource:
41  *      pending next selected clocksource.
42  * clocksource_list:
43  *      linked list with the registered clocksources
44  * clocksource_lock:
45  *      protects manipulations to curr_clocksource and next_clocksource
46  *      and the clocksource_list
47  * override_name:
48  *      Name of the user-specified clocksource.
49  */
50 static struct clocksource *curr_clocksource = &clocksource_jiffies;
51 static struct clocksource *next_clocksource;
52 static struct clocksource *clocksource_override;
53 static LIST_HEAD(clocksource_list);
54 static DEFINE_SPINLOCK(clocksource_lock);
55 static char override_name[32];
56 static int finished_booting;
57
58 /* clocksource_done_booting - Called near the end of core bootup
59  *
60  * Hack to avoid lots of clocksource churn at boot time.
61  * We use fs_initcall because we want this to start before
62  * device_initcall but after subsys_initcall.
63  */
64 static int __init clocksource_done_booting(void)
65 {
66         finished_booting = 1;
67         return 0;
68 }
69 fs_initcall(clocksource_done_booting);
70
71 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
72 static LIST_HEAD(watchdog_list);
73 static struct clocksource *watchdog;
74 static struct timer_list watchdog_timer;
75 static DEFINE_SPINLOCK(watchdog_lock);
76 static cycle_t watchdog_last;
77 static unsigned long watchdog_resumed;
78
79 /*
80  * Interval: 0.5sec Threshold: 0.0625s
81  */
82 #define WATCHDOG_INTERVAL (HZ >> 1)
83 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
84
85 static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
86 {
87         if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
88                 return;
89
90         printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
91                cs->name, delta);
92         cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
93         clocksource_change_rating(cs, 0);
94         list_del(&cs->wd_list);
95 }
96
97 static void clocksource_watchdog(unsigned long data)
98 {
99         struct clocksource *cs, *tmp;
100         cycle_t csnow, wdnow;
101         int64_t wd_nsec, cs_nsec;
102         int resumed;
103
104         spin_lock(&watchdog_lock);
105
106         resumed = test_and_clear_bit(0, &watchdog_resumed);
107
108         wdnow = watchdog->read();
109         wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
110         watchdog_last = wdnow;
111
112         list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
113                 csnow = cs->read();
114
115                 if (unlikely(resumed)) {
116                         cs->wd_last = csnow;
117                         continue;
118                 }
119
120                 /* Initialized ? */
121                 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
122                         if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
123                             (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
124                                 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
125                                 /*
126                                  * We just marked the clocksource as
127                                  * highres-capable, notify the rest of the
128                                  * system as well so that we transition
129                                  * into high-res mode:
130                                  */
131                                 tick_clock_notify();
132                         }
133                         cs->flags |= CLOCK_SOURCE_WATCHDOG;
134                         cs->wd_last = csnow;
135                 } else {
136                         cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
137                         cs->wd_last = csnow;
138                         /* Check the delta. Might remove from the list ! */
139                         clocksource_ratewd(cs, cs_nsec - wd_nsec);
140                 }
141         }
142
143         if (!list_empty(&watchdog_list)) {
144                 /*
145                  * Cycle through CPUs to check if the CPUs stay
146                  * synchronized to each other.
147                  */
148                 int next_cpu = next_cpu(raw_smp_processor_id(), cpu_online_map);
149
150                 if (next_cpu >= NR_CPUS)
151                         next_cpu = first_cpu(cpu_online_map);
152                 watchdog_timer.expires += WATCHDOG_INTERVAL;
153                 add_timer_on(&watchdog_timer, next_cpu);
154         }
155         spin_unlock(&watchdog_lock);
156 }
157 static void clocksource_resume_watchdog(void)
158 {
159         set_bit(0, &watchdog_resumed);
160 }
161
162 static void clocksource_check_watchdog(struct clocksource *cs)
163 {
164         struct clocksource *cse;
165         unsigned long flags;
166
167         spin_lock_irqsave(&watchdog_lock, flags);
168         if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
169                 int started = !list_empty(&watchdog_list);
170
171                 list_add(&cs->wd_list, &watchdog_list);
172                 if (!started && watchdog) {
173                         watchdog_last = watchdog->read();
174                         watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
175                         add_timer_on(&watchdog_timer,
176                                      first_cpu(cpu_online_map));
177                 }
178         } else {
179                 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
180                         cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
181
182                 if (!watchdog || cs->rating > watchdog->rating) {
183                         if (watchdog)
184                                 del_timer(&watchdog_timer);
185                         watchdog = cs;
186                         init_timer(&watchdog_timer);
187                         watchdog_timer.function = clocksource_watchdog;
188
189                         /* Reset watchdog cycles */
190                         list_for_each_entry(cse, &watchdog_list, wd_list)
191                                 cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
192                         /* Start if list is not empty */
193                         if (!list_empty(&watchdog_list)) {
194                                 watchdog_last = watchdog->read();
195                                 watchdog_timer.expires =
196                                         jiffies + WATCHDOG_INTERVAL;
197                                 add_timer_on(&watchdog_timer,
198                                              first_cpu(cpu_online_map));
199                         }
200                 }
201         }
202         spin_unlock_irqrestore(&watchdog_lock, flags);
203 }
204 #else
205 static void clocksource_check_watchdog(struct clocksource *cs)
206 {
207         if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
208                 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
209 }
210
211 static inline void clocksource_resume_watchdog(void) { }
212 #endif
213
214 /**
215  * clocksource_resume - resume the clocksource(s)
216  */
217 void clocksource_resume(void)
218 {
219         struct clocksource *cs;
220         unsigned long flags;
221
222         spin_lock_irqsave(&clocksource_lock, flags);
223
224         list_for_each_entry(cs, &clocksource_list, list) {
225                 if (cs->resume)
226                         cs->resume();
227         }
228
229         clocksource_resume_watchdog();
230
231         spin_unlock_irqrestore(&clocksource_lock, flags);
232 }
233
234 /**
235  * clocksource_touch_watchdog - Update watchdog
236  *
237  * Update the watchdog after exception contexts such as kgdb so as not
238  * to incorrectly trip the watchdog.
239  *
240  */
241 void clocksource_touch_watchdog(void)
242 {
243         clocksource_resume_watchdog();
244 }
245
246 /**
247  * clocksource_get_next - Returns the selected clocksource
248  *
249  */
250 struct clocksource *clocksource_get_next(void)
251 {
252         unsigned long flags;
253
254         spin_lock_irqsave(&clocksource_lock, flags);
255         if (next_clocksource && finished_booting) {
256                 curr_clocksource = next_clocksource;
257                 next_clocksource = NULL;
258         }
259         spin_unlock_irqrestore(&clocksource_lock, flags);
260
261         return curr_clocksource;
262 }
263
264 /**
265  * select_clocksource - Selects the best registered clocksource.
266  *
267  * Private function. Must hold clocksource_lock when called.
268  *
269  * Select the clocksource with the best rating, or the clocksource,
270  * which is selected by userspace override.
271  */
272 static struct clocksource *select_clocksource(void)
273 {
274         struct clocksource *next;
275
276         if (list_empty(&clocksource_list))
277                 return NULL;
278
279         if (clocksource_override)
280                 next = clocksource_override;
281         else
282                 next = list_entry(clocksource_list.next, struct clocksource,
283                                   list);
284
285         if (next == curr_clocksource)
286                 return NULL;
287
288         return next;
289 }
290
291 /*
292  * Enqueue the clocksource sorted by rating
293  */
294 static int clocksource_enqueue(struct clocksource *c)
295 {
296         struct list_head *tmp, *entry = &clocksource_list;
297
298         list_for_each(tmp, &clocksource_list) {
299                 struct clocksource *cs;
300
301                 cs = list_entry(tmp, struct clocksource, list);
302                 if (cs == c)
303                         return -EBUSY;
304                 /* Keep track of the place, where to insert */
305                 if (cs->rating >= c->rating)
306                         entry = tmp;
307         }
308         list_add(&c->list, entry);
309
310         if (strlen(c->name) == strlen(override_name) &&
311             !strcmp(c->name, override_name))
312                 clocksource_override = c;
313
314         return 0;
315 }
316
317 /**
318  * clocksource_register - Used to install new clocksources
319  * @t:          clocksource to be registered
320  *
321  * Returns -EBUSY if registration fails, zero otherwise.
322  */
323 int clocksource_register(struct clocksource *c)
324 {
325         unsigned long flags;
326         int ret;
327
328         spin_lock_irqsave(&clocksource_lock, flags);
329         ret = clocksource_enqueue(c);
330         if (!ret)
331                 next_clocksource = select_clocksource();
332         spin_unlock_irqrestore(&clocksource_lock, flags);
333         if (!ret)
334                 clocksource_check_watchdog(c);
335         return ret;
336 }
337 EXPORT_SYMBOL(clocksource_register);
338
339 /**
340  * clocksource_change_rating - Change the rating of a registered clocksource
341  *
342  */
343 void clocksource_change_rating(struct clocksource *cs, int rating)
344 {
345         unsigned long flags;
346
347         spin_lock_irqsave(&clocksource_lock, flags);
348         list_del(&cs->list);
349         cs->rating = rating;
350         clocksource_enqueue(cs);
351         next_clocksource = select_clocksource();
352         spin_unlock_irqrestore(&clocksource_lock, flags);
353 }
354
355 /**
356  * clocksource_unregister - remove a registered clocksource
357  */
358 void clocksource_unregister(struct clocksource *cs)
359 {
360         unsigned long flags;
361
362         spin_lock_irqsave(&clocksource_lock, flags);
363         list_del(&cs->list);
364         if (clocksource_override == cs)
365                 clocksource_override = NULL;
366         next_clocksource = select_clocksource();
367         spin_unlock_irqrestore(&clocksource_lock, flags);
368 }
369
370 #ifdef CONFIG_SYSFS
371 /**
372  * sysfs_show_current_clocksources - sysfs interface for current clocksource
373  * @dev:        unused
374  * @buf:        char buffer to be filled with clocksource list
375  *
376  * Provides sysfs interface for listing current clocksource.
377  */
378 static ssize_t
379 sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
380 {
381         ssize_t count = 0;
382
383         spin_lock_irq(&clocksource_lock);
384         count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
385         spin_unlock_irq(&clocksource_lock);
386
387         return count;
388 }
389
390 /**
391  * sysfs_override_clocksource - interface for manually overriding clocksource
392  * @dev:        unused
393  * @buf:        name of override clocksource
394  * @count:      length of buffer
395  *
396  * Takes input from sysfs interface for manually overriding the default
397  * clocksource selction.
398  */
399 static ssize_t sysfs_override_clocksource(struct sys_device *dev,
400                                           const char *buf, size_t count)
401 {
402         struct clocksource *ovr = NULL;
403         size_t ret = count;
404         int len;
405
406         /* strings from sysfs write are not 0 terminated! */
407         if (count >= sizeof(override_name))
408                 return -EINVAL;
409
410         /* strip of \n: */
411         if (buf[count-1] == '\n')
412                 count--;
413
414         spin_lock_irq(&clocksource_lock);
415
416         if (count > 0)
417                 memcpy(override_name, buf, count);
418         override_name[count] = 0;
419
420         len = strlen(override_name);
421         if (len) {
422                 struct clocksource *cs;
423
424                 ovr = clocksource_override;
425                 /* try to select it: */
426                 list_for_each_entry(cs, &clocksource_list, list) {
427                         if (strlen(cs->name) == len &&
428                             !strcmp(cs->name, override_name))
429                                 ovr = cs;
430                 }
431         }
432
433         /* Reselect, when the override name has changed */
434         if (ovr != clocksource_override) {
435                 clocksource_override = ovr;
436                 next_clocksource = select_clocksource();
437         }
438
439         spin_unlock_irq(&clocksource_lock);
440
441         return ret;
442 }
443
444 /**
445  * sysfs_show_available_clocksources - sysfs interface for listing clocksource
446  * @dev:        unused
447  * @buf:        char buffer to be filled with clocksource list
448  *
449  * Provides sysfs interface for listing registered clocksources
450  */
451 static ssize_t
452 sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
453 {
454         struct clocksource *src;
455         ssize_t count = 0;
456
457         spin_lock_irq(&clocksource_lock);
458         list_for_each_entry(src, &clocksource_list, list) {
459                 count += snprintf(buf + count,
460                                   max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
461                                   "%s ", src->name);
462         }
463         spin_unlock_irq(&clocksource_lock);
464
465         count += snprintf(buf + count,
466                           max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
467
468         return count;
469 }
470
471 /*
472  * Sysfs setup bits:
473  */
474 static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
475                    sysfs_override_clocksource);
476
477 static SYSDEV_ATTR(available_clocksource, 0600,
478                    sysfs_show_available_clocksources, NULL);
479
480 static struct sysdev_class clocksource_sysclass = {
481         .name = "clocksource",
482 };
483
484 static struct sys_device device_clocksource = {
485         .id     = 0,
486         .cls    = &clocksource_sysclass,
487 };
488
489 static int __init init_clocksource_sysfs(void)
490 {
491         int error = sysdev_class_register(&clocksource_sysclass);
492
493         if (!error)
494                 error = sysdev_register(&device_clocksource);
495         if (!error)
496                 error = sysdev_create_file(
497                                 &device_clocksource,
498                                 &attr_current_clocksource);
499         if (!error)
500                 error = sysdev_create_file(
501                                 &device_clocksource,
502                                 &attr_available_clocksource);
503         return error;
504 }
505
506 device_initcall(init_clocksource_sysfs);
507 #endif /* CONFIG_SYSFS */
508
509 /**
510  * boot_override_clocksource - boot clock override
511  * @str:        override name
512  *
513  * Takes a clocksource= boot argument and uses it
514  * as the clocksource override name.
515  */
516 static int __init boot_override_clocksource(char* str)
517 {
518         unsigned long flags;
519         spin_lock_irqsave(&clocksource_lock, flags);
520         if (str)
521                 strlcpy(override_name, str, sizeof(override_name));
522         spin_unlock_irqrestore(&clocksource_lock, flags);
523         return 1;
524 }
525
526 __setup("clocksource=", boot_override_clocksource);
527
528 /**
529  * boot_override_clock - Compatibility layer for deprecated boot option
530  * @str:        override name
531  *
532  * DEPRECATED! Takes a clock= boot argument and uses it
533  * as the clocksource override name
534  */
535 static int __init boot_override_clock(char* str)
536 {
537         if (!strcmp(str, "pmtmr")) {
538                 printk("Warning: clock=pmtmr is deprecated. "
539                         "Use clocksource=acpi_pm.\n");
540                 return boot_override_clocksource("acpi_pm");
541         }
542         printk("Warning! clock= boot option is deprecated. "
543                 "Use clocksource=xyz\n");
544         return boot_override_clocksource(str);
545 }
546
547 __setup("clock=", boot_override_clock);