clocksource: Cleanup clocksource selection
[deliverable/linux.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
734efb46 24 */
25
26#include <linux/clocksource.h>
27#include <linux/sysdev.h>
28#include <linux/init.h>
29#include <linux/module.h>
dc29a365 30#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
79bf2bb3 31#include <linux/tick.h>
734efb46 32
a038a353
PO
33void timecounter_init(struct timecounter *tc,
34 const struct cyclecounter *cc,
35 u64 start_tstamp)
36{
37 tc->cc = cc;
38 tc->cycle_last = cc->read(cc);
39 tc->nsec = start_tstamp;
40}
41EXPORT_SYMBOL(timecounter_init);
42
43/**
44 * timecounter_read_delta - get nanoseconds since last call of this function
45 * @tc: Pointer to time counter
46 *
47 * When the underlying cycle counter runs over, this will be handled
48 * correctly as long as it does not run over more than once between
49 * calls.
50 *
51 * The first call to this function for a new time counter initializes
52 * the time tracking and returns an undefined result.
53 */
54static u64 timecounter_read_delta(struct timecounter *tc)
55{
56 cycle_t cycle_now, cycle_delta;
57 u64 ns_offset;
58
59 /* read cycle counter: */
60 cycle_now = tc->cc->read(tc->cc);
61
62 /* calculate the delta since the last timecounter_read_delta(): */
63 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
64
65 /* convert to nanoseconds: */
66 ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
67
68 /* update time stamp of timecounter_read_delta() call: */
69 tc->cycle_last = cycle_now;
70
71 return ns_offset;
72}
73
74u64 timecounter_read(struct timecounter *tc)
75{
76 u64 nsec;
77
78 /* increment time by nanoseconds since last call */
79 nsec = timecounter_read_delta(tc);
80 nsec += tc->nsec;
81 tc->nsec = nsec;
82
83 return nsec;
84}
85EXPORT_SYMBOL(timecounter_read);
86
87u64 timecounter_cyc2time(struct timecounter *tc,
88 cycle_t cycle_tstamp)
89{
90 u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
91 u64 nsec;
92
93 /*
94 * Instead of always treating cycle_tstamp as more recent
95 * than tc->cycle_last, detect when it is too far in the
96 * future and treat it as old time stamp instead.
97 */
98 if (cycle_delta > tc->cc->mask / 2) {
99 cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
100 nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
101 } else {
102 nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
103 }
104
105 return nsec;
106}
107EXPORT_SYMBOL(timecounter_cyc2time);
108
734efb46 109/*[Clocksource internal variables]---------
110 * curr_clocksource:
f1b82746 111 * currently selected clocksource.
734efb46 112 * next_clocksource:
113 * pending next selected clocksource.
114 * clocksource_list:
115 * linked list with the registered clocksources
116 * clocksource_lock:
117 * protects manipulations to curr_clocksource and next_clocksource
118 * and the clocksource_list
119 * override_name:
120 * Name of the user-specified clocksource.
121 */
f1b82746 122static struct clocksource *curr_clocksource;
734efb46 123static struct clocksource *next_clocksource;
124static LIST_HEAD(clocksource_list);
125static DEFINE_SPINLOCK(clocksource_lock);
126static char override_name[32];
127static int finished_booting;
128
6bb74df4 129/* clocksource_done_booting - Called near the end of core bootup
734efb46 130 *
6bb74df4 131 * Hack to avoid lots of clocksource churn at boot time.
132 * We use fs_initcall because we want this to start before
133 * device_initcall but after subsys_initcall.
734efb46 134 */
ad596171 135static int __init clocksource_done_booting(void)
734efb46 136{
137 finished_booting = 1;
138 return 0;
139}
6bb74df4 140fs_initcall(clocksource_done_booting);
734efb46 141
5d8b34fd
TG
142#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
143static LIST_HEAD(watchdog_list);
144static struct clocksource *watchdog;
145static struct timer_list watchdog_timer;
146static DEFINE_SPINLOCK(watchdog_lock);
147static cycle_t watchdog_last;
8f89441b 148static unsigned long watchdog_resumed;
b52f52a0 149
5d8b34fd 150/*
35c35d1a 151 * Interval: 0.5sec Threshold: 0.0625s
5d8b34fd
TG
152 */
153#define WATCHDOG_INTERVAL (HZ >> 1)
35c35d1a 154#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
5d8b34fd
TG
155
156static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
157{
35c35d1a 158 if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
5d8b34fd
TG
159 return;
160
161 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
162 cs->name, delta);
163 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
164 clocksource_change_rating(cs, 0);
5d8b34fd
TG
165 list_del(&cs->wd_list);
166}
167
168static void clocksource_watchdog(unsigned long data)
169{
170 struct clocksource *cs, *tmp;
171 cycle_t csnow, wdnow;
172 int64_t wd_nsec, cs_nsec;
b52f52a0 173 int resumed;
5d8b34fd
TG
174
175 spin_lock(&watchdog_lock);
176
8f89441b 177 resumed = test_and_clear_bit(0, &watchdog_resumed);
b52f52a0 178
8e19608e 179 wdnow = watchdog->read(watchdog);
5d8b34fd
TG
180 wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
181 watchdog_last = wdnow;
182
183 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
8e19608e 184 csnow = cs->read(cs);
b52f52a0
TG
185
186 if (unlikely(resumed)) {
187 cs->wd_last = csnow;
188 continue;
189 }
190
5d8b34fd
TG
191 /* Initialized ? */
192 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
193 if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
194 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
195 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
79bf2bb3
TG
196 /*
197 * We just marked the clocksource as
198 * highres-capable, notify the rest of the
199 * system as well so that we transition
200 * into high-res mode:
201 */
202 tick_clock_notify();
5d8b34fd
TG
203 }
204 cs->flags |= CLOCK_SOURCE_WATCHDOG;
205 cs->wd_last = csnow;
206 } else {
207 cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
208 cs->wd_last = csnow;
209 /* Check the delta. Might remove from the list ! */
210 clocksource_ratewd(cs, cs_nsec - wd_nsec);
211 }
212 }
213
214 if (!list_empty(&watchdog_list)) {
6993fc5b
AK
215 /*
216 * Cycle through CPUs to check if the CPUs stay
217 * synchronized to each other.
218 */
5db0e1e9
RR
219 int next_cpu = cpumask_next(raw_smp_processor_id(),
220 cpu_online_mask);
6993fc5b 221
cad0e458 222 if (next_cpu >= nr_cpu_ids)
6b954823 223 next_cpu = cpumask_first(cpu_online_mask);
6993fc5b
AK
224 watchdog_timer.expires += WATCHDOG_INTERVAL;
225 add_timer_on(&watchdog_timer, next_cpu);
5d8b34fd
TG
226 }
227 spin_unlock(&watchdog_lock);
228}
b52f52a0
TG
229static void clocksource_resume_watchdog(void)
230{
8f89441b 231 set_bit(0, &watchdog_resumed);
b52f52a0
TG
232}
233
5d8b34fd
TG
234static void clocksource_check_watchdog(struct clocksource *cs)
235{
236 struct clocksource *cse;
237 unsigned long flags;
238
239 spin_lock_irqsave(&watchdog_lock, flags);
240 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
241 int started = !list_empty(&watchdog_list);
242
243 list_add(&cs->wd_list, &watchdog_list);
244 if (!started && watchdog) {
8e19608e 245 watchdog_last = watchdog->read(watchdog);
5d8b34fd 246 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
6993fc5b 247 add_timer_on(&watchdog_timer,
6b954823 248 cpumask_first(cpu_online_mask));
5d8b34fd 249 }
948ac6d7
TG
250 } else {
251 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
5d8b34fd
TG
252 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
253
254 if (!watchdog || cs->rating > watchdog->rating) {
255 if (watchdog)
256 del_timer(&watchdog_timer);
257 watchdog = cs;
898a19de 258 init_timer(&watchdog_timer);
5d8b34fd
TG
259 watchdog_timer.function = clocksource_watchdog;
260
261 /* Reset watchdog cycles */
262 list_for_each_entry(cse, &watchdog_list, wd_list)
263 cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
264 /* Start if list is not empty */
265 if (!list_empty(&watchdog_list)) {
8e19608e 266 watchdog_last = watchdog->read(watchdog);
5d8b34fd
TG
267 watchdog_timer.expires =
268 jiffies + WATCHDOG_INTERVAL;
6993fc5b 269 add_timer_on(&watchdog_timer,
6b954823 270 cpumask_first(cpu_online_mask));
5d8b34fd
TG
271 }
272 }
273 }
274 spin_unlock_irqrestore(&watchdog_lock, flags);
275}
276#else
277static void clocksource_check_watchdog(struct clocksource *cs)
278{
279 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
280 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
281}
b52f52a0
TG
282
283static inline void clocksource_resume_watchdog(void) { }
5d8b34fd
TG
284#endif
285
b52f52a0
TG
286/**
287 * clocksource_resume - resume the clocksource(s)
288 */
289void clocksource_resume(void)
290{
2e197586 291 struct clocksource *cs;
b52f52a0
TG
292 unsigned long flags;
293
294 spin_lock_irqsave(&clocksource_lock, flags);
295
2e197586 296 list_for_each_entry(cs, &clocksource_list, list) {
b52f52a0
TG
297 if (cs->resume)
298 cs->resume();
299 }
300
301 clocksource_resume_watchdog();
302
303 spin_unlock_irqrestore(&clocksource_lock, flags);
304}
305
7c3078b6
JW
306/**
307 * clocksource_touch_watchdog - Update watchdog
308 *
309 * Update the watchdog after exception contexts such as kgdb so as not
310 * to incorrectly trip the watchdog.
311 *
312 */
313void clocksource_touch_watchdog(void)
314{
315 clocksource_resume_watchdog();
316}
317
f1b82746 318#ifdef CONFIG_GENERIC_TIME
734efb46 319/**
a2752549 320 * clocksource_get_next - Returns the selected clocksource
734efb46 321 *
322 */
a2752549 323struct clocksource *clocksource_get_next(void)
734efb46 324{
325 unsigned long flags;
326
327 spin_lock_irqsave(&clocksource_lock, flags);
328 if (next_clocksource && finished_booting) {
329 curr_clocksource = next_clocksource;
330 next_clocksource = NULL;
331 }
332 spin_unlock_irqrestore(&clocksource_lock, flags);
333
334 return curr_clocksource;
335}
336
337/**
f1b82746 338 * clocksource_select - Select the best clocksource available
734efb46 339 *
340 * Private function. Must hold clocksource_lock when called.
341 *
92c7e002
TG
342 * Select the clocksource with the best rating, or the clocksource,
343 * which is selected by userspace override.
734efb46 344 */
f1b82746 345static void clocksource_select(void)
734efb46 346{
f1b82746 347 struct clocksource *best, *cs;
5d8b34fd 348
92c7e002 349 if (list_empty(&clocksource_list))
f1b82746
MS
350 return;
351 /* First clocksource on the list has the best rating. */
352 best = list_first_entry(&clocksource_list, struct clocksource, list);
353 /* Check for the override clocksource. */
354 list_for_each_entry(cs, &clocksource_list, list) {
355 if (strcmp(cs->name, override_name) != 0)
356 continue;
357 /*
358 * Check to make sure we don't switch to a non-highres
359 * capable clocksource if the tick code is in oneshot
360 * mode (highres or nohz)
361 */
362 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
363 tick_oneshot_mode_active()) {
364 /* Override clocksource cannot be used. */
365 printk(KERN_WARNING "Override clocksource %s is not "
366 "HRT compatible. Cannot switch while in "
367 "HRT/NOHZ mode\n", cs->name);
368 override_name[0] = 0;
369 } else
370 /* Override clocksource can be used. */
371 best = cs;
372 break;
373 }
374 if (curr_clocksource != best)
375 next_clocksource = best;
376}
734efb46 377
f1b82746 378#else /* CONFIG_GENERIC_TIME */
734efb46 379
f1b82746 380static void clocksource_select(void) { }
5d8b34fd 381
f1b82746 382#endif
734efb46 383
92c7e002
TG
384/*
385 * Enqueue the clocksource sorted by rating
734efb46 386 */
f1b82746 387static void clocksource_enqueue(struct clocksource *cs)
734efb46 388{
f1b82746
MS
389 struct list_head *entry = &clocksource_list;
390 struct clocksource *tmp;
92c7e002 391
f1b82746 392 list_for_each_entry(tmp, &clocksource_list, list)
92c7e002 393 /* Keep track of the place, where to insert */
f1b82746
MS
394 if (tmp->rating >= cs->rating)
395 entry = &tmp->list;
396 list_add(&cs->list, entry);
734efb46 397}
398
399/**
a2752549 400 * clocksource_register - Used to install new clocksources
734efb46 401 * @t: clocksource to be registered
402 *
403 * Returns -EBUSY if registration fails, zero otherwise.
404 */
f1b82746 405int clocksource_register(struct clocksource *cs)
734efb46 406{
734efb46 407 unsigned long flags;
408
409 spin_lock_irqsave(&clocksource_lock, flags);
f1b82746
MS
410 clocksource_enqueue(cs);
411 clocksource_select();
734efb46 412 spin_unlock_irqrestore(&clocksource_lock, flags);
f1b82746
MS
413 clocksource_check_watchdog(cs);
414 return 0;
734efb46 415}
a2752549 416EXPORT_SYMBOL(clocksource_register);
734efb46 417
418/**
92c7e002 419 * clocksource_change_rating - Change the rating of a registered clocksource
734efb46 420 *
734efb46 421 */
92c7e002 422void clocksource_change_rating(struct clocksource *cs, int rating)
734efb46 423{
424 unsigned long flags;
425
426 spin_lock_irqsave(&clocksource_lock, flags);
92c7e002 427 list_del(&cs->list);
5d8b34fd 428 cs->rating = rating;
92c7e002 429 clocksource_enqueue(cs);
f1b82746 430 clocksource_select();
734efb46 431 spin_unlock_irqrestore(&clocksource_lock, flags);
432}
433
4713e22c
TG
434/**
435 * clocksource_unregister - remove a registered clocksource
436 */
437void clocksource_unregister(struct clocksource *cs)
438{
439 unsigned long flags;
440
441 spin_lock_irqsave(&clocksource_lock, flags);
442 list_del(&cs->list);
f1b82746 443 clocksource_select();
4713e22c
TG
444 spin_unlock_irqrestore(&clocksource_lock, flags);
445}
446
2b013700 447#ifdef CONFIG_SYSFS
734efb46 448/**
449 * sysfs_show_current_clocksources - sysfs interface for current clocksource
450 * @dev: unused
451 * @buf: char buffer to be filled with clocksource list
452 *
453 * Provides sysfs interface for listing current clocksource.
454 */
455static ssize_t
4a0b2b4d
AK
456sysfs_show_current_clocksources(struct sys_device *dev,
457 struct sysdev_attribute *attr, char *buf)
734efb46 458{
5e2cb101 459 ssize_t count = 0;
734efb46 460
461 spin_lock_irq(&clocksource_lock);
5e2cb101 462 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
734efb46 463 spin_unlock_irq(&clocksource_lock);
464
5e2cb101 465 return count;
734efb46 466}
467
468/**
469 * sysfs_override_clocksource - interface for manually overriding clocksource
470 * @dev: unused
471 * @buf: name of override clocksource
472 * @count: length of buffer
473 *
474 * Takes input from sysfs interface for manually overriding the default
475 * clocksource selction.
476 */
477static ssize_t sysfs_override_clocksource(struct sys_device *dev,
4a0b2b4d 478 struct sysdev_attribute *attr,
734efb46 479 const char *buf, size_t count)
480{
481 size_t ret = count;
92c7e002 482
734efb46 483 /* strings from sysfs write are not 0 terminated! */
484 if (count >= sizeof(override_name))
485 return -EINVAL;
486
487 /* strip of \n: */
488 if (buf[count-1] == '\n')
489 count--;
734efb46 490
491 spin_lock_irq(&clocksource_lock);
492
92c7e002
TG
493 if (count > 0)
494 memcpy(override_name, buf, count);
734efb46 495 override_name[count] = 0;
f1b82746 496 clocksource_select();
734efb46 497
498 spin_unlock_irq(&clocksource_lock);
499
500 return ret;
501}
502
503/**
504 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
505 * @dev: unused
506 * @buf: char buffer to be filled with clocksource list
507 *
508 * Provides sysfs interface for listing registered clocksources
509 */
510static ssize_t
4a0b2b4d
AK
511sysfs_show_available_clocksources(struct sys_device *dev,
512 struct sysdev_attribute *attr,
513 char *buf)
734efb46 514{
2e197586 515 struct clocksource *src;
5e2cb101 516 ssize_t count = 0;
734efb46 517
518 spin_lock_irq(&clocksource_lock);
2e197586 519 list_for_each_entry(src, &clocksource_list, list) {
cd6d95d8
TG
520 /*
521 * Don't show non-HRES clocksource if the tick code is
522 * in one shot mode (highres=on or nohz=on)
523 */
524 if (!tick_oneshot_mode_active() ||
525 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
3f68535a 526 count += snprintf(buf + count,
5e2cb101
MX
527 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
528 "%s ", src->name);
734efb46 529 }
530 spin_unlock_irq(&clocksource_lock);
531
5e2cb101
MX
532 count += snprintf(buf + count,
533 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
734efb46 534
5e2cb101 535 return count;
734efb46 536}
537
538/*
539 * Sysfs setup bits:
540 */
4f95f81a 541static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
f5f1a24a 542 sysfs_override_clocksource);
734efb46 543
4f95f81a 544static SYSDEV_ATTR(available_clocksource, 0444,
f5f1a24a 545 sysfs_show_available_clocksources, NULL);
734efb46 546
547static struct sysdev_class clocksource_sysclass = {
af5ca3f4 548 .name = "clocksource",
734efb46 549};
550
551static struct sys_device device_clocksource = {
552 .id = 0,
553 .cls = &clocksource_sysclass,
554};
555
ad596171 556static int __init init_clocksource_sysfs(void)
734efb46 557{
558 int error = sysdev_class_register(&clocksource_sysclass);
559
560 if (!error)
561 error = sysdev_register(&device_clocksource);
562 if (!error)
563 error = sysdev_create_file(
564 &device_clocksource,
565 &attr_current_clocksource);
566 if (!error)
567 error = sysdev_create_file(
568 &device_clocksource,
569 &attr_available_clocksource);
570 return error;
571}
572
573device_initcall(init_clocksource_sysfs);
2b013700 574#endif /* CONFIG_SYSFS */
734efb46 575
576/**
577 * boot_override_clocksource - boot clock override
578 * @str: override name
579 *
580 * Takes a clocksource= boot argument and uses it
581 * as the clocksource override name.
582 */
583static int __init boot_override_clocksource(char* str)
584{
585 unsigned long flags;
586 spin_lock_irqsave(&clocksource_lock, flags);
587 if (str)
588 strlcpy(override_name, str, sizeof(override_name));
589 spin_unlock_irqrestore(&clocksource_lock, flags);
590 return 1;
591}
592
593__setup("clocksource=", boot_override_clocksource);
594
595/**
596 * boot_override_clock - Compatibility layer for deprecated boot option
597 * @str: override name
598 *
599 * DEPRECATED! Takes a clock= boot argument and uses it
600 * as the clocksource override name
601 */
602static int __init boot_override_clock(char* str)
603{
5d0cf410 604 if (!strcmp(str, "pmtmr")) {
605 printk("Warning: clock=pmtmr is deprecated. "
606 "Use clocksource=acpi_pm.\n");
607 return boot_override_clocksource("acpi_pm");
608 }
609 printk("Warning! clock= boot option is deprecated. "
610 "Use clocksource=xyz\n");
734efb46 611 return boot_override_clocksource(str);
612}
613
614__setup("clock=", boot_override_clock);
This page took 0.326921 seconds and 5 git commands to generate.