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