timekeeping: Update clocksource with stop_machine
[deliverable/linux.git] / 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 */
25
26 #include <linux/clocksource.h>
27 #include <linux/sysdev.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
31 #include <linux/tick.h>
32
33 void 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 }
41 EXPORT_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 */
54 static 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
74 u64 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 }
85 EXPORT_SYMBOL(timecounter_read);
86
87 u64 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 }
107 EXPORT_SYMBOL(timecounter_cyc2time);
108
109 /*[Clocksource internal variables]---------
110 * curr_clocksource:
111 * currently selected clocksource.
112 * clocksource_list:
113 * linked list with the registered clocksources
114 * clocksource_mutex:
115 * protects manipulations to curr_clocksource and the clocksource_list
116 * override_name:
117 * Name of the user-specified clocksource.
118 */
119 static struct clocksource *curr_clocksource;
120 static LIST_HEAD(clocksource_list);
121 static DEFINE_MUTEX(clocksource_mutex);
122 static char override_name[32];
123
124 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
125 static LIST_HEAD(watchdog_list);
126 static struct clocksource *watchdog;
127 static struct timer_list watchdog_timer;
128 static struct work_struct watchdog_work;
129 static DEFINE_SPINLOCK(watchdog_lock);
130 static cycle_t watchdog_last;
131 static int watchdog_running;
132
133 static void clocksource_watchdog_work(struct work_struct *work);
134
135 /*
136 * Interval: 0.5sec Threshold: 0.0625s
137 */
138 #define WATCHDOG_INTERVAL (HZ >> 1)
139 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
140
141 static void clocksource_unstable(struct clocksource *cs, int64_t delta)
142 {
143 printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
144 cs->name, delta);
145 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
146 cs->flags |= CLOCK_SOURCE_UNSTABLE;
147 schedule_work(&watchdog_work);
148 }
149
150 static void clocksource_watchdog(unsigned long data)
151 {
152 struct clocksource *cs;
153 cycle_t csnow, wdnow;
154 int64_t wd_nsec, cs_nsec;
155 int next_cpu;
156
157 spin_lock(&watchdog_lock);
158 if (!watchdog_running)
159 goto out;
160
161 wdnow = watchdog->read(watchdog);
162 wd_nsec = clocksource_cyc2ns((wdnow - watchdog_last) & watchdog->mask,
163 watchdog->mult, watchdog->shift);
164 watchdog_last = wdnow;
165
166 list_for_each_entry(cs, &watchdog_list, wd_list) {
167
168 /* Clocksource already marked unstable? */
169 if (cs->flags & CLOCK_SOURCE_UNSTABLE)
170 continue;
171
172 csnow = cs->read(cs);
173
174 /* Clocksource initialized ? */
175 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
176 cs->flags |= CLOCK_SOURCE_WATCHDOG;
177 cs->wd_last = csnow;
178 continue;
179 }
180
181 /* Check the deviation from the watchdog clocksource. */
182 cs_nsec = clocksource_cyc2ns((csnow - cs->wd_last) &
183 cs->mask, cs->mult, cs->shift);
184 cs->wd_last = csnow;
185 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
186 clocksource_unstable(cs, cs_nsec - wd_nsec);
187 continue;
188 }
189
190 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
191 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
192 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
193 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
194 /*
195 * We just marked the clocksource as highres-capable,
196 * notify the rest of the system as well so that we
197 * transition into high-res mode:
198 */
199 tick_clock_notify();
200 }
201 }
202
203 /*
204 * Cycle through CPUs to check if the CPUs stay synchronized
205 * to each other.
206 */
207 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
208 if (next_cpu >= nr_cpu_ids)
209 next_cpu = cpumask_first(cpu_online_mask);
210 watchdog_timer.expires += WATCHDOG_INTERVAL;
211 add_timer_on(&watchdog_timer, next_cpu);
212 out:
213 spin_unlock(&watchdog_lock);
214 }
215
216 static inline void clocksource_start_watchdog(void)
217 {
218 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
219 return;
220 INIT_WORK(&watchdog_work, clocksource_watchdog_work);
221 init_timer(&watchdog_timer);
222 watchdog_timer.function = clocksource_watchdog;
223 watchdog_last = watchdog->read(watchdog);
224 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
225 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
226 watchdog_running = 1;
227 }
228
229 static inline void clocksource_stop_watchdog(void)
230 {
231 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
232 return;
233 del_timer(&watchdog_timer);
234 watchdog_running = 0;
235 }
236
237 static inline void clocksource_reset_watchdog(void)
238 {
239 struct clocksource *cs;
240
241 list_for_each_entry(cs, &watchdog_list, wd_list)
242 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
243 }
244
245 static void clocksource_resume_watchdog(void)
246 {
247 unsigned long flags;
248
249 spin_lock_irqsave(&watchdog_lock, flags);
250 clocksource_reset_watchdog();
251 spin_unlock_irqrestore(&watchdog_lock, flags);
252 }
253
254 static void clocksource_enqueue_watchdog(struct clocksource *cs)
255 {
256 unsigned long flags;
257
258 spin_lock_irqsave(&watchdog_lock, flags);
259 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
260 /* cs is a clocksource to be watched. */
261 list_add(&cs->wd_list, &watchdog_list);
262 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
263 } else {
264 /* cs is a watchdog. */
265 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
266 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
267 /* Pick the best watchdog. */
268 if (!watchdog || cs->rating > watchdog->rating) {
269 watchdog = cs;
270 /* Reset watchdog cycles */
271 clocksource_reset_watchdog();
272 }
273 }
274 /* Check if the watchdog timer needs to be started. */
275 clocksource_start_watchdog();
276 spin_unlock_irqrestore(&watchdog_lock, flags);
277 }
278
279 static void clocksource_dequeue_watchdog(struct clocksource *cs)
280 {
281 struct clocksource *tmp;
282 unsigned long flags;
283
284 spin_lock_irqsave(&watchdog_lock, flags);
285 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
286 /* cs is a watched clocksource. */
287 list_del_init(&cs->wd_list);
288 } else if (cs == watchdog) {
289 /* Reset watchdog cycles */
290 clocksource_reset_watchdog();
291 /* Current watchdog is removed. Find an alternative. */
292 watchdog = NULL;
293 list_for_each_entry(tmp, &clocksource_list, list) {
294 if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
295 continue;
296 if (!watchdog || tmp->rating > watchdog->rating)
297 watchdog = tmp;
298 }
299 }
300 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
301 /* Check if the watchdog timer needs to be stopped. */
302 clocksource_stop_watchdog();
303 spin_unlock_irqrestore(&watchdog_lock, flags);
304 }
305
306 static void clocksource_watchdog_work(struct work_struct *work)
307 {
308 struct clocksource *cs, *tmp;
309 unsigned long flags;
310
311 spin_lock_irqsave(&watchdog_lock, flags);
312 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
313 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
314 list_del_init(&cs->wd_list);
315 clocksource_change_rating(cs, 0);
316 }
317 /* Check if the watchdog timer needs to be stopped. */
318 clocksource_stop_watchdog();
319 spin_unlock(&watchdog_lock);
320 }
321
322 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
323
324 static void clocksource_enqueue_watchdog(struct clocksource *cs)
325 {
326 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
327 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
328 }
329
330 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
331 static inline void clocksource_resume_watchdog(void) { }
332
333 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
334
335 /**
336 * clocksource_resume - resume the clocksource(s)
337 */
338 void clocksource_resume(void)
339 {
340 struct clocksource *cs;
341
342 mutex_lock(&clocksource_mutex);
343
344 list_for_each_entry(cs, &clocksource_list, list)
345 if (cs->resume)
346 cs->resume();
347
348 clocksource_resume_watchdog();
349
350 mutex_unlock(&clocksource_mutex);
351 }
352
353 /**
354 * clocksource_touch_watchdog - Update watchdog
355 *
356 * Update the watchdog after exception contexts such as kgdb so as not
357 * to incorrectly trip the watchdog.
358 *
359 */
360 void clocksource_touch_watchdog(void)
361 {
362 clocksource_resume_watchdog();
363 }
364
365 #ifdef CONFIG_GENERIC_TIME
366
367 static int finished_booting;
368
369 /**
370 * clocksource_select - Select the best clocksource available
371 *
372 * Private function. Must hold clocksource_mutex when called.
373 *
374 * Select the clocksource with the best rating, or the clocksource,
375 * which is selected by userspace override.
376 */
377 static void clocksource_select(void)
378 {
379 struct clocksource *best, *cs;
380
381 if (!finished_booting || list_empty(&clocksource_list))
382 return;
383 /* First clocksource on the list has the best rating. */
384 best = list_first_entry(&clocksource_list, struct clocksource, list);
385 /* Check for the override clocksource. */
386 list_for_each_entry(cs, &clocksource_list, list) {
387 if (strcmp(cs->name, override_name) != 0)
388 continue;
389 /*
390 * Check to make sure we don't switch to a non-highres
391 * capable clocksource if the tick code is in oneshot
392 * mode (highres or nohz)
393 */
394 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
395 tick_oneshot_mode_active()) {
396 /* Override clocksource cannot be used. */
397 printk(KERN_WARNING "Override clocksource %s is not "
398 "HRT compatible. Cannot switch while in "
399 "HRT/NOHZ mode\n", cs->name);
400 override_name[0] = 0;
401 } else
402 /* Override clocksource can be used. */
403 best = cs;
404 break;
405 }
406 if (curr_clocksource != best) {
407 printk(KERN_INFO "Switching to clocksource %s\n", best->name);
408 curr_clocksource = best;
409 timekeeping_notify(curr_clocksource);
410 }
411 }
412
413 /*
414 * clocksource_done_booting - Called near the end of core bootup
415 *
416 * Hack to avoid lots of clocksource churn at boot time.
417 * We use fs_initcall because we want this to start before
418 * device_initcall but after subsys_initcall.
419 */
420 static int __init clocksource_done_booting(void)
421 {
422 finished_booting = 1;
423 clocksource_select();
424 return 0;
425 }
426 fs_initcall(clocksource_done_booting);
427
428 #else /* CONFIG_GENERIC_TIME */
429
430 static inline void clocksource_select(void) { }
431
432 #endif
433
434 /*
435 * Enqueue the clocksource sorted by rating
436 */
437 static void clocksource_enqueue(struct clocksource *cs)
438 {
439 struct list_head *entry = &clocksource_list;
440 struct clocksource *tmp;
441
442 list_for_each_entry(tmp, &clocksource_list, list)
443 /* Keep track of the place, where to insert */
444 if (tmp->rating >= cs->rating)
445 entry = &tmp->list;
446 list_add(&cs->list, entry);
447 }
448
449 /**
450 * clocksource_register - Used to install new clocksources
451 * @t: clocksource to be registered
452 *
453 * Returns -EBUSY if registration fails, zero otherwise.
454 */
455 int clocksource_register(struct clocksource *cs)
456 {
457 mutex_lock(&clocksource_mutex);
458 clocksource_enqueue(cs);
459 clocksource_select();
460 clocksource_enqueue_watchdog(cs);
461 mutex_unlock(&clocksource_mutex);
462 return 0;
463 }
464 EXPORT_SYMBOL(clocksource_register);
465
466 /**
467 * clocksource_change_rating - Change the rating of a registered clocksource
468 */
469 void clocksource_change_rating(struct clocksource *cs, int rating)
470 {
471 mutex_lock(&clocksource_mutex);
472 list_del(&cs->list);
473 cs->rating = rating;
474 clocksource_enqueue(cs);
475 clocksource_select();
476 mutex_unlock(&clocksource_mutex);
477 }
478 EXPORT_SYMBOL(clocksource_change_rating);
479
480 /**
481 * clocksource_unregister - remove a registered clocksource
482 */
483 void clocksource_unregister(struct clocksource *cs)
484 {
485 mutex_lock(&clocksource_mutex);
486 clocksource_dequeue_watchdog(cs);
487 list_del(&cs->list);
488 clocksource_select();
489 mutex_unlock(&clocksource_mutex);
490 }
491 EXPORT_SYMBOL(clocksource_unregister);
492
493 #ifdef CONFIG_SYSFS
494 /**
495 * sysfs_show_current_clocksources - sysfs interface for current clocksource
496 * @dev: unused
497 * @buf: char buffer to be filled with clocksource list
498 *
499 * Provides sysfs interface for listing current clocksource.
500 */
501 static ssize_t
502 sysfs_show_current_clocksources(struct sys_device *dev,
503 struct sysdev_attribute *attr, char *buf)
504 {
505 ssize_t count = 0;
506
507 mutex_lock(&clocksource_mutex);
508 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
509 mutex_unlock(&clocksource_mutex);
510
511 return count;
512 }
513
514 /**
515 * sysfs_override_clocksource - interface for manually overriding clocksource
516 * @dev: unused
517 * @buf: name of override clocksource
518 * @count: length of buffer
519 *
520 * Takes input from sysfs interface for manually overriding the default
521 * clocksource selction.
522 */
523 static ssize_t sysfs_override_clocksource(struct sys_device *dev,
524 struct sysdev_attribute *attr,
525 const char *buf, size_t count)
526 {
527 size_t ret = count;
528
529 /* strings from sysfs write are not 0 terminated! */
530 if (count >= sizeof(override_name))
531 return -EINVAL;
532
533 /* strip of \n: */
534 if (buf[count-1] == '\n')
535 count--;
536
537 mutex_lock(&clocksource_mutex);
538
539 if (count > 0)
540 memcpy(override_name, buf, count);
541 override_name[count] = 0;
542 clocksource_select();
543
544 mutex_unlock(&clocksource_mutex);
545
546 return ret;
547 }
548
549 /**
550 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
551 * @dev: unused
552 * @buf: char buffer to be filled with clocksource list
553 *
554 * Provides sysfs interface for listing registered clocksources
555 */
556 static ssize_t
557 sysfs_show_available_clocksources(struct sys_device *dev,
558 struct sysdev_attribute *attr,
559 char *buf)
560 {
561 struct clocksource *src;
562 ssize_t count = 0;
563
564 mutex_lock(&clocksource_mutex);
565 list_for_each_entry(src, &clocksource_list, list) {
566 /*
567 * Don't show non-HRES clocksource if the tick code is
568 * in one shot mode (highres=on or nohz=on)
569 */
570 if (!tick_oneshot_mode_active() ||
571 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
572 count += snprintf(buf + count,
573 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
574 "%s ", src->name);
575 }
576 mutex_unlock(&clocksource_mutex);
577
578 count += snprintf(buf + count,
579 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
580
581 return count;
582 }
583
584 /*
585 * Sysfs setup bits:
586 */
587 static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
588 sysfs_override_clocksource);
589
590 static SYSDEV_ATTR(available_clocksource, 0444,
591 sysfs_show_available_clocksources, NULL);
592
593 static struct sysdev_class clocksource_sysclass = {
594 .name = "clocksource",
595 };
596
597 static struct sys_device device_clocksource = {
598 .id = 0,
599 .cls = &clocksource_sysclass,
600 };
601
602 static int __init init_clocksource_sysfs(void)
603 {
604 int error = sysdev_class_register(&clocksource_sysclass);
605
606 if (!error)
607 error = sysdev_register(&device_clocksource);
608 if (!error)
609 error = sysdev_create_file(
610 &device_clocksource,
611 &attr_current_clocksource);
612 if (!error)
613 error = sysdev_create_file(
614 &device_clocksource,
615 &attr_available_clocksource);
616 return error;
617 }
618
619 device_initcall(init_clocksource_sysfs);
620 #endif /* CONFIG_SYSFS */
621
622 /**
623 * boot_override_clocksource - boot clock override
624 * @str: override name
625 *
626 * Takes a clocksource= boot argument and uses it
627 * as the clocksource override name.
628 */
629 static int __init boot_override_clocksource(char* str)
630 {
631 mutex_lock(&clocksource_mutex);
632 if (str)
633 strlcpy(override_name, str, sizeof(override_name));
634 mutex_unlock(&clocksource_mutex);
635 return 1;
636 }
637
638 __setup("clocksource=", boot_override_clocksource);
639
640 /**
641 * boot_override_clock - Compatibility layer for deprecated boot option
642 * @str: override name
643 *
644 * DEPRECATED! Takes a clock= boot argument and uses it
645 * as the clocksource override name
646 */
647 static int __init boot_override_clock(char* str)
648 {
649 if (!strcmp(str, "pmtmr")) {
650 printk("Warning: clock=pmtmr is deprecated. "
651 "Use clocksource=acpi_pm.\n");
652 return boot_override_clocksource("acpi_pm");
653 }
654 printk("Warning! clock= boot option is deprecated. "
655 "Use clocksource=xyz\n");
656 return boot_override_clocksource(str);
657 }
658
659 __setup("clock=", boot_override_clock);
This page took 0.047758 seconds and 6 git commands to generate.