[PATCH] clocksource: fix resume logic
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / time / clocksource.c
blob5baee91ebc701804e5edafd6b2b1e76de7fb6ca1
1 /*
2 * linux/kernel/time/clocksource.c
4 * This file contains the functions which manage clocksource drivers.
6 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
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.
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.
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.
22 * TODO WishList:
23 * o Allow clocksource drivers to be unregistered
24 * o get rid of clocksource_jiffies extern
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>
34 /* XXX - Would like a better way for initializing curr_clocksource */
35 extern struct clocksource clocksource_jiffies;
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.
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;
58 /* clocksource_done_booting - Called near the end of core bootup
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.
64 static int __init clocksource_done_booting(void)
66 finished_booting = 1;
67 return 0;
69 fs_initcall(clocksource_done_booting);
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;
80 * Interval: 0.5sec Treshold: 0.0625s
82 #define WATCHDOG_INTERVAL (HZ >> 1)
83 #define WATCHDOG_TRESHOLD (NSEC_PER_SEC >> 4)
85 static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
87 if (delta > -WATCHDOG_TRESHOLD && delta < WATCHDOG_TRESHOLD)
88 return;
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);
98 static void clocksource_watchdog(unsigned long data)
100 struct clocksource *cs, *tmp;
101 cycle_t csnow, wdnow;
102 int64_t wd_nsec, cs_nsec;
103 int resumed;
105 spin_lock(&watchdog_lock);
107 resumed = watchdog_resumed;
108 if (unlikely(resumed))
109 watchdog_resumed = 0;
111 wdnow = watchdog->read();
112 wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
113 watchdog_last = wdnow;
115 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
116 csnow = cs->read();
118 if (unlikely(resumed)) {
119 cs->wd_last = csnow;
120 continue;
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;
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:
134 tick_clock_notify();
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);
146 if (!list_empty(&watchdog_list)) {
147 __mod_timer(&watchdog_timer,
148 watchdog_timer.expires + WATCHDOG_INTERVAL);
150 spin_unlock(&watchdog_lock);
152 static void clocksource_resume_watchdog(void)
154 spin_lock(&watchdog_lock);
155 watchdog_resumed = 1;
156 spin_unlock(&watchdog_lock);
159 static void clocksource_check_watchdog(struct clocksource *cs)
161 struct clocksource *cse;
162 unsigned long flags;
164 spin_lock_irqsave(&watchdog_lock, flags);
165 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
166 int started = !list_empty(&watchdog_list);
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);
174 } else {
175 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
176 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
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;
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);
197 spin_unlock_irqrestore(&watchdog_lock, flags);
199 #else
200 static void clocksource_check_watchdog(struct clocksource *cs)
202 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
203 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
206 static inline void clocksource_resume_watchdog(void) { }
207 #endif
210 * clocksource_resume - resume the clocksource(s)
212 void clocksource_resume(void)
214 struct list_head *tmp;
215 unsigned long flags;
217 spin_lock_irqsave(&clocksource_lock, flags);
219 list_for_each(tmp, &clocksource_list) {
220 struct clocksource *cs;
222 cs = list_entry(tmp, struct clocksource, list);
223 if (cs->resume)
224 cs->resume();
227 clocksource_resume_watchdog();
229 spin_unlock_irqrestore(&clocksource_lock, flags);
233 * clocksource_get_next - Returns the selected clocksource
236 struct clocksource *clocksource_get_next(void)
238 unsigned long flags;
240 spin_lock_irqsave(&clocksource_lock, flags);
241 if (next_clocksource && finished_booting) {
242 curr_clocksource = next_clocksource;
243 next_clocksource = NULL;
245 spin_unlock_irqrestore(&clocksource_lock, flags);
247 return curr_clocksource;
251 * select_clocksource - Selects the best registered clocksource.
253 * Private function. Must hold clocksource_lock when called.
255 * Select the clocksource with the best rating, or the clocksource,
256 * which is selected by userspace override.
258 static struct clocksource *select_clocksource(void)
260 struct clocksource *next;
262 if (list_empty(&clocksource_list))
263 return NULL;
265 if (clocksource_override)
266 next = clocksource_override;
267 else
268 next = list_entry(clocksource_list.next, struct clocksource,
269 list);
271 if (next == curr_clocksource)
272 return NULL;
274 return next;
278 * Enqueue the clocksource sorted by rating
280 static int clocksource_enqueue(struct clocksource *c)
282 struct list_head *tmp, *entry = &clocksource_list;
284 list_for_each(tmp, &clocksource_list) {
285 struct clocksource *cs;
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;
294 list_add(&c->list, entry);
296 if (strlen(c->name) == strlen(override_name) &&
297 !strcmp(c->name, override_name))
298 clocksource_override = c;
300 return 0;
304 * clocksource_register - Used to install new clocksources
305 * @t: clocksource to be registered
307 * Returns -EBUSY if registration fails, zero otherwise.
309 int clocksource_register(struct clocksource *c)
311 unsigned long flags;
312 int ret;
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;
323 EXPORT_SYMBOL(clocksource_register);
326 * clocksource_change_rating - Change the rating of a registered clocksource
329 void clocksource_change_rating(struct clocksource *cs, int rating)
331 unsigned long flags;
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);
341 #ifdef CONFIG_SYSFS
343 * sysfs_show_current_clocksources - sysfs interface for current clocksource
344 * @dev: unused
345 * @buf: char buffer to be filled with clocksource list
347 * Provides sysfs interface for listing current clocksource.
349 static ssize_t
350 sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
352 char *curr = buf;
354 spin_lock_irq(&clocksource_lock);
355 curr += sprintf(curr, "%s ", curr_clocksource->name);
356 spin_unlock_irq(&clocksource_lock);
358 curr += sprintf(curr, "\n");
360 return curr - buf;
364 * sysfs_override_clocksource - interface for manually overriding clocksource
365 * @dev: unused
366 * @buf: name of override clocksource
367 * @count: length of buffer
369 * Takes input from sysfs interface for manually overriding the default
370 * clocksource selction.
372 static ssize_t sysfs_override_clocksource(struct sys_device *dev,
373 const char *buf, size_t count)
375 struct clocksource *ovr = NULL;
376 struct list_head *tmp;
377 size_t ret = count;
378 int len;
380 /* strings from sysfs write are not 0 terminated! */
381 if (count >= sizeof(override_name))
382 return -EINVAL;
384 /* strip of \n: */
385 if (buf[count-1] == '\n')
386 count--;
388 spin_lock_irq(&clocksource_lock);
390 if (count > 0)
391 memcpy(override_name, buf, count);
392 override_name[count] = 0;
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;
401 cs = list_entry(tmp, struct clocksource, list);
402 if (strlen(cs->name) == len &&
403 !strcmp(cs->name, override_name))
404 ovr = cs;
408 /* Reselect, when the override name has changed */
409 if (ovr != clocksource_override) {
410 clocksource_override = ovr;
411 next_clocksource = select_clocksource();
414 spin_unlock_irq(&clocksource_lock);
416 return ret;
420 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
421 * @dev: unused
422 * @buf: char buffer to be filled with clocksource list
424 * Provides sysfs interface for listing registered clocksources
426 static ssize_t
427 sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
429 struct list_head *tmp;
430 char *curr = buf;
432 spin_lock_irq(&clocksource_lock);
433 list_for_each(tmp, &clocksource_list) {
434 struct clocksource *src;
436 src = list_entry(tmp, struct clocksource, list);
437 curr += sprintf(curr, "%s ", src->name);
439 spin_unlock_irq(&clocksource_lock);
441 curr += sprintf(curr, "\n");
443 return curr - buf;
447 * Sysfs setup bits:
449 static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
450 sysfs_override_clocksource);
452 static SYSDEV_ATTR(available_clocksource, 0600,
453 sysfs_show_available_clocksources, NULL);
455 static struct sysdev_class clocksource_sysclass = {
456 set_kset_name("clocksource"),
459 static struct sys_device device_clocksource = {
460 .id = 0,
461 .cls = &clocksource_sysclass,
464 static int __init init_clocksource_sysfs(void)
466 int error = sysdev_class_register(&clocksource_sysclass);
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;
481 device_initcall(init_clocksource_sysfs);
482 #endif /* CONFIG_SYSFS */
485 * boot_override_clocksource - boot clock override
486 * @str: override name
488 * Takes a clocksource= boot argument and uses it
489 * as the clocksource override name.
491 static int __init boot_override_clocksource(char* str)
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;
501 __setup("clocksource=", boot_override_clocksource);
504 * boot_override_clock - Compatibility layer for deprecated boot option
505 * @str: override name
507 * DEPRECATED! Takes a clock= boot argument and uses it
508 * as the clocksource override name
510 static int __init boot_override_clock(char* str)
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");
517 printk("Warning! clock= boot option is deprecated. "
518 "Use clocksource=xyz\n");
519 return boot_override_clocksource(str);
522 __setup("clock=", boot_override_clock);