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