clocksource: fix resume logic
[safe/jmp/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 int 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         cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
95         list_del(&cs->wd_list);
96 }
97
98 static void clocksource_watchdog(unsigned long data)
99 {
100         struct clocksource *cs, *tmp;
101         cycle_t csnow, wdnow;
102         int64_t wd_nsec, cs_nsec;
103         int resumed;
104
105         spin_lock(&watchdog_lock);
106
107         resumed = watchdog_resumed;
108         if (unlikely(resumed))
109                 watchdog_resumed = 0;
110
111         wdnow = watchdog->read();
112         wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
113         watchdog_last = wdnow;
114
115         list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
116                 csnow = cs->read();
117
118                 if (unlikely(resumed)) {
119                         cs->wd_last = csnow;
120                         continue;
121                 }
122
123                 /* Initialized ? */
124                 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
125                         if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
126                             (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
127                                 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
128                                 /*
129                                  * We just marked the clocksource as
130                                  * highres-capable, notify the rest of the
131                                  * system as well so that we transition
132                                  * into high-res mode:
133                                  */
134                                 tick_clock_notify();
135                         }
136                         cs->flags |= CLOCK_SOURCE_WATCHDOG;
137                         cs->wd_last = csnow;
138                 } else {
139                         cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
140                         cs->wd_last = csnow;
141                         /* Check the delta. Might remove from the list ! */
142                         clocksource_ratewd(cs, cs_nsec - wd_nsec);
143                 }
144         }
145
146         if (!list_empty(&watchdog_list)) {
147                 __mod_timer(&watchdog_timer,
148                             watchdog_timer.expires + WATCHDOG_INTERVAL);
149         }
150         spin_unlock(&watchdog_lock);
151 }
152 static void clocksource_resume_watchdog(void)
153 {
154         spin_lock(&watchdog_lock);
155         watchdog_resumed = 1;
156         spin_unlock(&watchdog_lock);
157 }
158
159 static void clocksource_check_watchdog(struct clocksource *cs)
160 {
161         struct clocksource *cse;
162         unsigned long flags;
163
164         spin_lock_irqsave(&watchdog_lock, flags);
165         if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
166                 int started = !list_empty(&watchdog_list);
167
168                 list_add(&cs->wd_list, &watchdog_list);
169                 if (!started && watchdog) {
170                         watchdog_last = watchdog->read();
171                         watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
172                         add_timer(&watchdog_timer);
173                 }
174         } else {
175                 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
176                         cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
177
178                 if (!watchdog || cs->rating > watchdog->rating) {
179                         if (watchdog)
180                                 del_timer(&watchdog_timer);
181                         watchdog = cs;
182                         init_timer(&watchdog_timer);
183                         watchdog_timer.function = clocksource_watchdog;
184
185                         /* Reset watchdog cycles */
186                         list_for_each_entry(cse, &watchdog_list, wd_list)
187                                 cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
188                         /* Start if list is not empty */
189                         if (!list_empty(&watchdog_list)) {
190                                 watchdog_last = watchdog->read();
191                                 watchdog_timer.expires =
192                                         jiffies + WATCHDOG_INTERVAL;
193                                 add_timer(&watchdog_timer);
194                         }
195                 }
196         }
197         spin_unlock_irqrestore(&watchdog_lock, flags);
198 }
199 #else
200 static void clocksource_check_watchdog(struct clocksource *cs)
201 {
202         if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
203                 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
204 }
205
206 static inline void clocksource_resume_watchdog(void) { }
207 #endif
208
209 /**
210  * clocksource_resume - resume the clocksource(s)
211  */
212 void clocksource_resume(void)
213 {
214         struct list_head *tmp;
215         unsigned long flags;
216
217         spin_lock_irqsave(&clocksource_lock, flags);
218
219         list_for_each(tmp, &clocksource_list) {
220                 struct clocksource *cs;
221
222                 cs = list_entry(tmp, struct clocksource, list);
223                 if (cs->resume)
224                         cs->resume();
225         }
226
227         clocksource_resume_watchdog();
228
229         spin_unlock_irqrestore(&clocksource_lock, flags);
230 }
231
232 /**
233  * clocksource_get_next - Returns the selected clocksource
234  *
235  */
236 struct clocksource *clocksource_get_next(void)
237 {
238         unsigned long flags;
239
240         spin_lock_irqsave(&clocksource_lock, flags);
241         if (next_clocksource && finished_booting) {
242                 curr_clocksource = next_clocksource;
243                 next_clocksource = NULL;
244         }
245         spin_unlock_irqrestore(&clocksource_lock, flags);
246
247         return curr_clocksource;
248 }
249
250 /**
251  * select_clocksource - Selects the best registered clocksource.
252  *
253  * Private function. Must hold clocksource_lock when called.
254  *
255  * Select the clocksource with the best rating, or the clocksource,
256  * which is selected by userspace override.
257  */
258 static struct clocksource *select_clocksource(void)
259 {
260         struct clocksource *next;
261
262         if (list_empty(&clocksource_list))
263                 return NULL;
264
265         if (clocksource_override)
266                 next = clocksource_override;
267         else
268                 next = list_entry(clocksource_list.next, struct clocksource,
269                                   list);
270
271         if (next == curr_clocksource)
272                 return NULL;
273
274         return next;
275 }
276
277 /*
278  * Enqueue the clocksource sorted by rating
279  */
280 static int clocksource_enqueue(struct clocksource *c)
281 {
282         struct list_head *tmp, *entry = &clocksource_list;
283
284         list_for_each(tmp, &clocksource_list) {
285                 struct clocksource *cs;
286
287                 cs = list_entry(tmp, struct clocksource, list);
288                 if (cs == c)
289                         return -EBUSY;
290                 /* Keep track of the place, where to insert */
291                 if (cs->rating >= c->rating)
292                         entry = tmp;
293         }
294         list_add(&c->list, entry);
295
296         if (strlen(c->name) == strlen(override_name) &&
297             !strcmp(c->name, override_name))
298                 clocksource_override = c;
299
300         return 0;
301 }
302
303 /**
304  * clocksource_register - Used to install new clocksources
305  * @t:          clocksource to be registered
306  *
307  * Returns -EBUSY if registration fails, zero otherwise.
308  */
309 int clocksource_register(struct clocksource *c)
310 {
311         unsigned long flags;
312         int ret;
313
314         spin_lock_irqsave(&clocksource_lock, flags);
315         ret = clocksource_enqueue(c);
316         if (!ret)
317                 next_clocksource = select_clocksource();
318         spin_unlock_irqrestore(&clocksource_lock, flags);
319         if (!ret)
320                 clocksource_check_watchdog(c);
321         return ret;
322 }
323 EXPORT_SYMBOL(clocksource_register);
324
325 /**
326  * clocksource_change_rating - Change the rating of a registered clocksource
327  *
328  */
329 void clocksource_change_rating(struct clocksource *cs, int rating)
330 {
331         unsigned long flags;
332
333         spin_lock_irqsave(&clocksource_lock, flags);
334         list_del(&cs->list);
335         cs->rating = rating;
336         clocksource_enqueue(cs);
337         next_clocksource = select_clocksource();
338         spin_unlock_irqrestore(&clocksource_lock, flags);
339 }
340
341 #ifdef CONFIG_SYSFS
342 /**
343  * sysfs_show_current_clocksources - sysfs interface for current clocksource
344  * @dev:        unused
345  * @buf:        char buffer to be filled with clocksource list
346  *
347  * Provides sysfs interface for listing current clocksource.
348  */
349 static ssize_t
350 sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
351 {
352         char *curr = buf;
353
354         spin_lock_irq(&clocksource_lock);
355         curr += sprintf(curr, "%s ", curr_clocksource->name);
356         spin_unlock_irq(&clocksource_lock);
357
358         curr += sprintf(curr, "\n");
359
360         return curr - buf;
361 }
362
363 /**
364  * sysfs_override_clocksource - interface for manually overriding clocksource
365  * @dev:        unused
366  * @buf:        name of override clocksource
367  * @count:      length of buffer
368  *
369  * Takes input from sysfs interface for manually overriding the default
370  * clocksource selction.
371  */
372 static ssize_t sysfs_override_clocksource(struct sys_device *dev,
373                                           const char *buf, size_t count)
374 {
375         struct clocksource *ovr = NULL;
376         struct list_head *tmp;
377         size_t ret = count;
378         int len;
379
380         /* strings from sysfs write are not 0 terminated! */
381         if (count >= sizeof(override_name))
382                 return -EINVAL;
383
384         /* strip of \n: */
385         if (buf[count-1] == '\n')
386                 count--;
387
388         spin_lock_irq(&clocksource_lock);
389
390         if (count > 0)
391                 memcpy(override_name, buf, count);
392         override_name[count] = 0;
393
394         len = strlen(override_name);
395         if (len) {
396                 ovr = clocksource_override;
397                 /* try to select it: */
398                 list_for_each(tmp, &clocksource_list) {
399                         struct clocksource *cs;
400
401                         cs = list_entry(tmp, struct clocksource, list);
402                         if (strlen(cs->name) == len &&
403                             !strcmp(cs->name, override_name))
404                                 ovr = cs;
405                 }
406         }
407
408         /* Reselect, when the override name has changed */
409         if (ovr != clocksource_override) {
410                 clocksource_override = ovr;
411                 next_clocksource = select_clocksource();
412         }
413
414         spin_unlock_irq(&clocksource_lock);
415
416         return ret;
417 }
418
419 /**
420  * sysfs_show_available_clocksources - sysfs interface for listing clocksource
421  * @dev:        unused
422  * @buf:        char buffer to be filled with clocksource list
423  *
424  * Provides sysfs interface for listing registered clocksources
425  */
426 static ssize_t
427 sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
428 {
429         struct list_head *tmp;
430         char *curr = buf;
431
432         spin_lock_irq(&clocksource_lock);
433         list_for_each(tmp, &clocksource_list) {
434                 struct clocksource *src;
435
436                 src = list_entry(tmp, struct clocksource, list);
437                 curr += sprintf(curr, "%s ", src->name);
438         }
439         spin_unlock_irq(&clocksource_lock);
440
441         curr += sprintf(curr, "\n");
442
443         return curr - buf;
444 }
445
446 /*
447  * Sysfs setup bits:
448  */
449 static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
450                    sysfs_override_clocksource);
451
452 static SYSDEV_ATTR(available_clocksource, 0600,
453                    sysfs_show_available_clocksources, NULL);
454
455 static struct sysdev_class clocksource_sysclass = {
456         set_kset_name("clocksource"),
457 };
458
459 static struct sys_device device_clocksource = {
460         .id     = 0,
461         .cls    = &clocksource_sysclass,
462 };
463
464 static int __init init_clocksource_sysfs(void)
465 {
466         int error = sysdev_class_register(&clocksource_sysclass);
467
468         if (!error)
469                 error = sysdev_register(&device_clocksource);
470         if (!error)
471                 error = sysdev_create_file(
472                                 &device_clocksource,
473                                 &attr_current_clocksource);
474         if (!error)
475                 error = sysdev_create_file(
476                                 &device_clocksource,
477                                 &attr_available_clocksource);
478         return error;
479 }
480
481 device_initcall(init_clocksource_sysfs);
482 #endif /* CONFIG_SYSFS */
483
484 /**
485  * boot_override_clocksource - boot clock override
486  * @str:        override name
487  *
488  * Takes a clocksource= boot argument and uses it
489  * as the clocksource override name.
490  */
491 static int __init boot_override_clocksource(char* str)
492 {
493         unsigned long flags;
494         spin_lock_irqsave(&clocksource_lock, flags);
495         if (str)
496                 strlcpy(override_name, str, sizeof(override_name));
497         spin_unlock_irqrestore(&clocksource_lock, flags);
498         return 1;
499 }
500
501 __setup("clocksource=", boot_override_clocksource);
502
503 /**
504  * boot_override_clock - Compatibility layer for deprecated boot option
505  * @str:        override name
506  *
507  * DEPRECATED! Takes a clock= boot argument and uses it
508  * as the clocksource override name
509  */
510 static int __init boot_override_clock(char* str)
511 {
512         if (!strcmp(str, "pmtmr")) {
513                 printk("Warning: clock=pmtmr is deprecated. "
514                         "Use clocksource=acpi_pm.\n");
515                 return boot_override_clocksource("acpi_pm");
516         }
517         printk("Warning! clock= boot option is deprecated. "
518                 "Use clocksource=xyz\n");
519         return boot_override_clocksource(str);
520 }
521
522 __setup("clock=", boot_override_clock);