blob: 3db5c3c460d74a845aa6f3ce0074cbd72a29f837 [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 stultz734efb42006-06-26 00:25:05 -070033
34/* XXX - Would like a better way for initializing curr_clocksource */
35extern 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 */
50static struct clocksource *curr_clocksource = &clocksource_jiffies;
51static struct clocksource *next_clocksource;
Thomas Gleixner92c7e002007-02-16 01:27:33 -080052static struct clocksource *clocksource_override;
john stultz734efb42006-06-26 00:25:05 -070053static LIST_HEAD(clocksource_list);
54static DEFINE_SPINLOCK(clocksource_lock);
55static char override_name[32];
56static int finished_booting;
57
john stultz6bb74df2007-03-05 00:30:50 -080058/* clocksource_done_booting - Called near the end of core bootup
john stultz734efb42006-06-26 00:25:05 -070059 *
john stultz6bb74df2007-03-05 00:30:50 -080060 * 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.
john stultz734efb42006-06-26 00:25:05 -070063 */
john stultzad596172006-06-26 00:25:06 -070064static int __init clocksource_done_booting(void)
john stultz734efb42006-06-26 00:25:05 -070065{
66 finished_booting = 1;
67 return 0;
68}
john stultz6bb74df2007-03-05 00:30:50 -080069fs_initcall(clocksource_done_booting);
john stultz734efb42006-06-26 00:25:05 -070070
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080071#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
72static LIST_HEAD(watchdog_list);
73static struct clocksource *watchdog;
74static struct timer_list watchdog_timer;
75static DEFINE_SPINLOCK(watchdog_lock);
76static cycle_t watchdog_last;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -070077static int watchdog_resumed;
78
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080079/*
Daniel Walker35c35d12007-05-09 02:33:40 -070080 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080081 */
82#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -070083#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080084
85static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
86{
Daniel Walker35c35d12007-05-09 02:33:40 -070087 if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080088 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
98static void clocksource_watchdog(unsigned long data)
99{
100 struct clocksource *cs, *tmp;
101 cycle_t csnow, wdnow;
102 int64_t wd_nsec, cs_nsec;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700103 int resumed;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800104
105 spin_lock(&watchdog_lock);
106
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700107 resumed = watchdog_resumed;
108 if (unlikely(resumed))
109 watchdog_resumed = 0;
110
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800111 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();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700117
118 if (unlikely(resumed)) {
119 cs->wd_last = csnow;
120 continue;
121 }
122
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800123 /* 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;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800128 /*
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();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800135 }
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}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700152static void clocksource_resume_watchdog(void)
153{
154 spin_lock(&watchdog_lock);
155 watchdog_resumed = 1;
156 spin_unlock(&watchdog_lock);
157}
158
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800159static 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 }
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200174 } else {
175 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800176 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
200static 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}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700205
206static inline void clocksource_resume_watchdog(void) { }
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800207#endif
208
john stultz734efb42006-06-26 00:25:05 -0700209/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700210 * clocksource_resume - resume the clocksource(s)
211 */
212void 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/**
john stultza2752542006-06-26 00:25:14 -0700233 * clocksource_get_next - Returns the selected clocksource
john stultz734efb42006-06-26 00:25:05 -0700234 *
235 */
john stultza2752542006-06-26 00:25:14 -0700236struct clocksource *clocksource_get_next(void)
john stultz734efb42006-06-26 00:25:05 -0700237{
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/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800251 * select_clocksource - Selects the best registered clocksource.
john stultz734efb42006-06-26 00:25:05 -0700252 *
253 * Private function. Must hold clocksource_lock when called.
254 *
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800255 * Select the clocksource with the best rating, or the clocksource,
256 * which is selected by userspace override.
john stultz734efb42006-06-26 00:25:05 -0700257 */
258static struct clocksource *select_clocksource(void)
259{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800260 struct clocksource *next;
261
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800262 if (list_empty(&clocksource_list))
263 return NULL;
john stultz734efb42006-06-26 00:25:05 -0700264
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800265 if (clocksource_override)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800266 next = clocksource_override;
267 else
268 next = list_entry(clocksource_list.next, struct clocksource,
269 list);
john stultz734efb42006-06-26 00:25:05 -0700270
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800271 if (next == curr_clocksource)
272 return NULL;
273
274 return next;
john stultz734efb42006-06-26 00:25:05 -0700275}
276
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800277/*
278 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700279 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800280static int clocksource_enqueue(struct clocksource *c)
john stultz734efb42006-06-26 00:25:05 -0700281{
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800282 struct list_head *tmp, *entry = &clocksource_list;
john stultz734efb42006-06-26 00:25:05 -0700283
284 list_for_each(tmp, &clocksource_list) {
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800285 struct clocksource *cs;
john stultz734efb42006-06-26 00:25:05 -0700286
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800287 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;
john stultz734efb42006-06-26 00:25:05 -0700293 }
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800294 list_add(&c->list, entry);
295
296 if (strlen(c->name) == strlen(override_name) &&
297 !strcmp(c->name, override_name))
298 clocksource_override = c;
john stultz734efb42006-06-26 00:25:05 -0700299
300 return 0;
301}
302
303/**
john stultza2752542006-06-26 00:25:14 -0700304 * clocksource_register - Used to install new clocksources
john stultz734efb42006-06-26 00:25:05 -0700305 * @t: clocksource to be registered
306 *
307 * Returns -EBUSY if registration fails, zero otherwise.
308 */
john stultza2752542006-06-26 00:25:14 -0700309int clocksource_register(struct clocksource *c)
john stultz734efb42006-06-26 00:25:05 -0700310{
john stultz734efb42006-06-26 00:25:05 -0700311 unsigned long flags;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800312 int ret;
john stultz734efb42006-06-26 00:25:05 -0700313
314 spin_lock_irqsave(&clocksource_lock, flags);
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800315 ret = clocksource_enqueue(c);
316 if (!ret)
john stultz734efb42006-06-26 00:25:05 -0700317 next_clocksource = select_clocksource();
john stultz734efb42006-06-26 00:25:05 -0700318 spin_unlock_irqrestore(&clocksource_lock, flags);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800319 if (!ret)
320 clocksource_check_watchdog(c);
john stultz734efb42006-06-26 00:25:05 -0700321 return ret;
322}
john stultza2752542006-06-26 00:25:14 -0700323EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700324
325/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800326 * clocksource_change_rating - Change the rating of a registered clocksource
john stultz734efb42006-06-26 00:25:05 -0700327 *
john stultz734efb42006-06-26 00:25:05 -0700328 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800329void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700330{
331 unsigned long flags;
332
333 spin_lock_irqsave(&clocksource_lock, flags);
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800334 list_del(&cs->list);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800335 cs->rating = rating;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800336 clocksource_enqueue(cs);
john stultz734efb42006-06-26 00:25:05 -0700337 next_clocksource = select_clocksource();
338 spin_unlock_irqrestore(&clocksource_lock, flags);
339}
340
Daniel Walker2b013702006-12-10 02:21:30 -0800341#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700342/**
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 */
349static ssize_t
350sysfs_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 */
372static ssize_t sysfs_override_clocksource(struct sys_device *dev,
373 const char *buf, size_t count)
374{
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800375 struct clocksource *ovr = NULL;
376 struct list_head *tmp;
john stultz734efb42006-06-26 00:25:05 -0700377 size_t ret = count;
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800378 int len;
379
john stultz734efb42006-06-26 00:25:05 -0700380 /* 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--;
john stultz734efb42006-06-26 00:25:05 -0700387
388 spin_lock_irq(&clocksource_lock);
389
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800390 if (count > 0)
391 memcpy(override_name, buf, count);
john stultz734efb42006-06-26 00:25:05 -0700392 override_name[count] = 0;
393
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800394 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 }
john stultz734efb42006-06-26 00:25:05 -0700413
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 */
426static ssize_t
427sysfs_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 */
449static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800450 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700451
452static SYSDEV_ATTR(available_clocksource, 0600,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800453 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -0700454
455static struct sysdev_class clocksource_sysclass = {
456 set_kset_name("clocksource"),
457};
458
459static struct sys_device device_clocksource = {
460 .id = 0,
461 .cls = &clocksource_sysclass,
462};
463
john stultzad596172006-06-26 00:25:06 -0700464static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -0700465{
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
481device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -0800482#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -0700483
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 */
491static 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 */
510static int __init boot_override_clock(char* str)
511{
john stultz5d0cf412006-06-26 00:25:12 -0700512 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");
john stultz734efb42006-06-26 00:25:05 -0700519 return boot_override_clocksource(str);
520}
521
522__setup("clock=", boot_override_clock);