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 |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <linux/clocksource.h> |
| 27 | #include <linux/sysdev.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/module.h> |
Mathieu Desnoyers | dc29a36 | 2007-02-10 01:43:43 -0800 | [diff] [blame] | 30 | #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 31 | #include <linux/tick.h> |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 32 | |
Patrick Ohly | a038a35 | 2009-02-12 05:03:34 +0000 | [diff] [blame] | 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 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 109 | /*[Clocksource internal variables]--------- |
| 110 | * curr_clocksource: |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 111 | * currently selected clocksource. |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 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 | */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 122 | static struct clocksource *curr_clocksource; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 123 | static struct clocksource *next_clocksource; |
| 124 | static LIST_HEAD(clocksource_list); |
| 125 | static DEFINE_SPINLOCK(clocksource_lock); |
| 126 | static char override_name[32]; |
| 127 | static int finished_booting; |
| 128 | |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 129 | /* clocksource_done_booting - Called near the end of core bootup |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 130 | * |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 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. |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 134 | */ |
john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 135 | static int __init clocksource_done_booting(void) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 136 | { |
| 137 | finished_booting = 1; |
| 138 | return 0; |
| 139 | } |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 140 | fs_initcall(clocksource_done_booting); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 141 | |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 142 | #ifdef CONFIG_CLOCKSOURCE_WATCHDOG |
| 143 | static LIST_HEAD(watchdog_list); |
| 144 | static struct clocksource *watchdog; |
| 145 | static struct timer_list watchdog_timer; |
| 146 | static DEFINE_SPINLOCK(watchdog_lock); |
| 147 | static cycle_t watchdog_last; |
Thomas Gleixner | 8f89441 | 2007-05-15 01:41:32 -0700 | [diff] [blame] | 148 | static unsigned long watchdog_resumed; |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 149 | |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 150 | /* |
Daniel Walker | 35c35d1 | 2007-05-09 02:33:40 -0700 | [diff] [blame] | 151 | * Interval: 0.5sec Threshold: 0.0625s |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 152 | */ |
| 153 | #define WATCHDOG_INTERVAL (HZ >> 1) |
Daniel Walker | 35c35d1 | 2007-05-09 02:33:40 -0700 | [diff] [blame] | 154 | #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 155 | |
Martin Schwidefsky | 8cf4e75 | 2009-08-14 15:47:22 +0200 | [diff] [blame^] | 156 | static void clocksource_unstable(struct clocksource *cs, int64_t delta) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 157 | { |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 158 | printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n", |
| 159 | cs->name, delta); |
| 160 | cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); |
| 161 | clocksource_change_rating(cs, 0); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 162 | list_del(&cs->wd_list); |
| 163 | } |
| 164 | |
| 165 | static void clocksource_watchdog(unsigned long data) |
| 166 | { |
| 167 | struct clocksource *cs, *tmp; |
| 168 | cycle_t csnow, wdnow; |
| 169 | int64_t wd_nsec, cs_nsec; |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 170 | int resumed; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 171 | |
| 172 | spin_lock(&watchdog_lock); |
| 173 | |
Thomas Gleixner | 8f89441 | 2007-05-15 01:41:32 -0700 | [diff] [blame] | 174 | resumed = test_and_clear_bit(0, &watchdog_resumed); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 175 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 176 | wdnow = watchdog->read(watchdog); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 177 | wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask); |
| 178 | watchdog_last = wdnow; |
| 179 | |
| 180 | list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) { |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 181 | csnow = cs->read(cs); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 182 | |
Martin Schwidefsky | 8cf4e75 | 2009-08-14 15:47:22 +0200 | [diff] [blame^] | 183 | /* Clocksource initialized ? */ |
| 184 | if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) { |
| 185 | cs->flags |= CLOCK_SOURCE_WATCHDOG; |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 186 | cs->wd_last = csnow; |
| 187 | continue; |
| 188 | } |
| 189 | |
Martin Schwidefsky | 8cf4e75 | 2009-08-14 15:47:22 +0200 | [diff] [blame^] | 190 | /* Check the deviation from the watchdog clocksource. */ |
| 191 | cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask); |
| 192 | cs->wd_last = csnow; |
| 193 | if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) { |
| 194 | clocksource_unstable(cs, cs_nsec - wd_nsec); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && |
| 199 | (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && |
| 200 | (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { |
| 201 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
| 202 | /* |
| 203 | * We just marked the clocksource as highres-capable, |
| 204 | * notify the rest of the system as well so that we |
| 205 | * transition into high-res mode: |
| 206 | */ |
| 207 | tick_clock_notify(); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | if (!list_empty(&watchdog_list)) { |
Andi Kleen | 6993fc5 | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 212 | /* |
| 213 | * Cycle through CPUs to check if the CPUs stay |
| 214 | * synchronized to each other. |
| 215 | */ |
Rusty Russell | 5db0e1e | 2009-01-01 10:12:29 +1030 | [diff] [blame] | 216 | int next_cpu = cpumask_next(raw_smp_processor_id(), |
| 217 | cpu_online_mask); |
Andi Kleen | 6993fc5 | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 218 | |
Mike Travis | cad0e45 | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 219 | if (next_cpu >= nr_cpu_ids) |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 220 | next_cpu = cpumask_first(cpu_online_mask); |
Andi Kleen | 6993fc5 | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 221 | watchdog_timer.expires += WATCHDOG_INTERVAL; |
| 222 | add_timer_on(&watchdog_timer, next_cpu); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 223 | } |
| 224 | spin_unlock(&watchdog_lock); |
| 225 | } |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 226 | static void clocksource_resume_watchdog(void) |
| 227 | { |
Thomas Gleixner | 8f89441 | 2007-05-15 01:41:32 -0700 | [diff] [blame] | 228 | set_bit(0, &watchdog_resumed); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 231 | static void clocksource_check_watchdog(struct clocksource *cs) |
| 232 | { |
| 233 | struct clocksource *cse; |
| 234 | unsigned long flags; |
| 235 | |
| 236 | spin_lock_irqsave(&watchdog_lock, flags); |
| 237 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { |
| 238 | int started = !list_empty(&watchdog_list); |
| 239 | |
| 240 | list_add(&cs->wd_list, &watchdog_list); |
| 241 | if (!started && watchdog) { |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 242 | watchdog_last = watchdog->read(watchdog); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 243 | watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; |
Andi Kleen | 6993fc5 | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 244 | add_timer_on(&watchdog_timer, |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 245 | cpumask_first(cpu_online_mask)); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 246 | } |
Thomas Gleixner | 948ac6d | 2007-03-25 14:42:51 +0200 | [diff] [blame] | 247 | } else { |
| 248 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 249 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
| 250 | |
| 251 | if (!watchdog || cs->rating > watchdog->rating) { |
| 252 | if (watchdog) |
| 253 | del_timer(&watchdog_timer); |
| 254 | watchdog = cs; |
Thomas Gleixner | 898a19d | 2008-03-25 09:01:51 +0100 | [diff] [blame] | 255 | init_timer(&watchdog_timer); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 256 | watchdog_timer.function = clocksource_watchdog; |
| 257 | |
| 258 | /* Reset watchdog cycles */ |
| 259 | list_for_each_entry(cse, &watchdog_list, wd_list) |
| 260 | cse->flags &= ~CLOCK_SOURCE_WATCHDOG; |
| 261 | /* Start if list is not empty */ |
| 262 | if (!list_empty(&watchdog_list)) { |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 263 | watchdog_last = watchdog->read(watchdog); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 264 | watchdog_timer.expires = |
| 265 | jiffies + WATCHDOG_INTERVAL; |
Andi Kleen | 6993fc5 | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 266 | add_timer_on(&watchdog_timer, |
Rusty Russell | 6b95482 | 2009-01-01 10:12:25 +1030 | [diff] [blame] | 267 | cpumask_first(cpu_online_mask)); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | spin_unlock_irqrestore(&watchdog_lock, flags); |
| 272 | } |
| 273 | #else |
| 274 | static void clocksource_check_watchdog(struct clocksource *cs) |
| 275 | { |
| 276 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) |
| 277 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
| 278 | } |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 279 | |
| 280 | static inline void clocksource_resume_watchdog(void) { } |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 281 | #endif |
| 282 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 283 | /** |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 284 | * clocksource_resume - resume the clocksource(s) |
| 285 | */ |
| 286 | void clocksource_resume(void) |
| 287 | { |
Matthias Kaehlcke | 2e19758 | 2007-10-18 23:39:58 -0700 | [diff] [blame] | 288 | struct clocksource *cs; |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 289 | unsigned long flags; |
| 290 | |
| 291 | spin_lock_irqsave(&clocksource_lock, flags); |
| 292 | |
Matthias Kaehlcke | 2e19758 | 2007-10-18 23:39:58 -0700 | [diff] [blame] | 293 | list_for_each_entry(cs, &clocksource_list, list) { |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 294 | if (cs->resume) |
| 295 | cs->resume(); |
| 296 | } |
| 297 | |
| 298 | clocksource_resume_watchdog(); |
| 299 | |
| 300 | spin_unlock_irqrestore(&clocksource_lock, flags); |
| 301 | } |
| 302 | |
| 303 | /** |
Jason Wessel | 7c3078b | 2008-02-15 14:55:54 -0600 | [diff] [blame] | 304 | * clocksource_touch_watchdog - Update watchdog |
| 305 | * |
| 306 | * Update the watchdog after exception contexts such as kgdb so as not |
| 307 | * to incorrectly trip the watchdog. |
| 308 | * |
| 309 | */ |
| 310 | void clocksource_touch_watchdog(void) |
| 311 | { |
| 312 | clocksource_resume_watchdog(); |
| 313 | } |
| 314 | |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 315 | #ifdef CONFIG_GENERIC_TIME |
Jason Wessel | 7c3078b | 2008-02-15 14:55:54 -0600 | [diff] [blame] | 316 | /** |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 317 | * clocksource_get_next - Returns the selected clocksource |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 318 | * |
| 319 | */ |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 320 | struct clocksource *clocksource_get_next(void) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 321 | { |
| 322 | unsigned long flags; |
| 323 | |
| 324 | spin_lock_irqsave(&clocksource_lock, flags); |
| 325 | if (next_clocksource && finished_booting) { |
| 326 | curr_clocksource = next_clocksource; |
| 327 | next_clocksource = NULL; |
| 328 | } |
| 329 | spin_unlock_irqrestore(&clocksource_lock, flags); |
| 330 | |
| 331 | return curr_clocksource; |
| 332 | } |
| 333 | |
| 334 | /** |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 335 | * clocksource_select - Select the best clocksource available |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 336 | * |
| 337 | * Private function. Must hold clocksource_lock when called. |
| 338 | * |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 339 | * Select the clocksource with the best rating, or the clocksource, |
| 340 | * which is selected by userspace override. |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 341 | */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 342 | static void clocksource_select(void) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 343 | { |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 344 | struct clocksource *best, *cs; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 345 | |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 346 | if (list_empty(&clocksource_list)) |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 347 | return; |
| 348 | /* First clocksource on the list has the best rating. */ |
| 349 | best = list_first_entry(&clocksource_list, struct clocksource, list); |
| 350 | /* Check for the override clocksource. */ |
| 351 | list_for_each_entry(cs, &clocksource_list, list) { |
| 352 | if (strcmp(cs->name, override_name) != 0) |
| 353 | continue; |
| 354 | /* |
| 355 | * Check to make sure we don't switch to a non-highres |
| 356 | * capable clocksource if the tick code is in oneshot |
| 357 | * mode (highres or nohz) |
| 358 | */ |
| 359 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && |
| 360 | tick_oneshot_mode_active()) { |
| 361 | /* Override clocksource cannot be used. */ |
| 362 | printk(KERN_WARNING "Override clocksource %s is not " |
| 363 | "HRT compatible. Cannot switch while in " |
| 364 | "HRT/NOHZ mode\n", cs->name); |
| 365 | override_name[0] = 0; |
| 366 | } else |
| 367 | /* Override clocksource can be used. */ |
| 368 | best = cs; |
| 369 | break; |
| 370 | } |
| 371 | if (curr_clocksource != best) |
| 372 | next_clocksource = best; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 375 | #else /* CONFIG_GENERIC_TIME */ |
| 376 | |
| 377 | static void clocksource_select(void) { } |
| 378 | |
| 379 | #endif |
| 380 | |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 381 | /* |
| 382 | * Enqueue the clocksource sorted by rating |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 383 | */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 384 | static void clocksource_enqueue(struct clocksource *cs) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 385 | { |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 386 | struct list_head *entry = &clocksource_list; |
| 387 | struct clocksource *tmp; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 388 | |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 389 | list_for_each_entry(tmp, &clocksource_list, list) |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 390 | /* Keep track of the place, where to insert */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 391 | if (tmp->rating >= cs->rating) |
| 392 | entry = &tmp->list; |
| 393 | list_add(&cs->list, entry); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 397 | * clocksource_register - Used to install new clocksources |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 398 | * @t: clocksource to be registered |
| 399 | * |
| 400 | * Returns -EBUSY if registration fails, zero otherwise. |
| 401 | */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 402 | int clocksource_register(struct clocksource *cs) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 403 | { |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 404 | unsigned long flags; |
| 405 | |
| 406 | spin_lock_irqsave(&clocksource_lock, flags); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 407 | clocksource_enqueue(cs); |
| 408 | clocksource_select(); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 409 | spin_unlock_irqrestore(&clocksource_lock, flags); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 410 | clocksource_check_watchdog(cs); |
| 411 | return 0; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 412 | } |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 413 | EXPORT_SYMBOL(clocksource_register); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 414 | |
| 415 | /** |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 416 | * clocksource_change_rating - Change the rating of a registered clocksource |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 417 | * |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 418 | */ |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 419 | void clocksource_change_rating(struct clocksource *cs, int rating) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 420 | { |
| 421 | unsigned long flags; |
| 422 | |
| 423 | spin_lock_irqsave(&clocksource_lock, flags); |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 424 | list_del(&cs->list); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 425 | cs->rating = rating; |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 426 | clocksource_enqueue(cs); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 427 | clocksource_select(); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 428 | spin_unlock_irqrestore(&clocksource_lock, flags); |
| 429 | } |
| 430 | |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 431 | /** |
| 432 | * clocksource_unregister - remove a registered clocksource |
| 433 | */ |
| 434 | void clocksource_unregister(struct clocksource *cs) |
| 435 | { |
| 436 | unsigned long flags; |
| 437 | |
| 438 | spin_lock_irqsave(&clocksource_lock, flags); |
| 439 | list_del(&cs->list); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 440 | clocksource_select(); |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 441 | spin_unlock_irqrestore(&clocksource_lock, flags); |
| 442 | } |
| 443 | |
Daniel Walker | 2b01370 | 2006-12-10 02:21:30 -0800 | [diff] [blame] | 444 | #ifdef CONFIG_SYSFS |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 445 | /** |
| 446 | * sysfs_show_current_clocksources - sysfs interface for current clocksource |
| 447 | * @dev: unused |
| 448 | * @buf: char buffer to be filled with clocksource list |
| 449 | * |
| 450 | * Provides sysfs interface for listing current clocksource. |
| 451 | */ |
| 452 | static ssize_t |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 453 | sysfs_show_current_clocksources(struct sys_device *dev, |
| 454 | struct sysdev_attribute *attr, char *buf) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 455 | { |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 456 | ssize_t count = 0; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 457 | |
| 458 | spin_lock_irq(&clocksource_lock); |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 459 | count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 460 | spin_unlock_irq(&clocksource_lock); |
| 461 | |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 462 | return count; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /** |
| 466 | * sysfs_override_clocksource - interface for manually overriding clocksource |
| 467 | * @dev: unused |
| 468 | * @buf: name of override clocksource |
| 469 | * @count: length of buffer |
| 470 | * |
| 471 | * Takes input from sysfs interface for manually overriding the default |
| 472 | * clocksource selction. |
| 473 | */ |
| 474 | static ssize_t sysfs_override_clocksource(struct sys_device *dev, |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 475 | struct sysdev_attribute *attr, |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 476 | const char *buf, size_t count) |
| 477 | { |
| 478 | size_t ret = count; |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 479 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 480 | /* strings from sysfs write are not 0 terminated! */ |
| 481 | if (count >= sizeof(override_name)) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | /* strip of \n: */ |
| 485 | if (buf[count-1] == '\n') |
| 486 | count--; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 487 | |
| 488 | spin_lock_irq(&clocksource_lock); |
| 489 | |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 490 | if (count > 0) |
| 491 | memcpy(override_name, buf, count); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 492 | override_name[count] = 0; |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 493 | clocksource_select(); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 494 | |
| 495 | spin_unlock_irq(&clocksource_lock); |
| 496 | |
| 497 | return ret; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * sysfs_show_available_clocksources - sysfs interface for listing clocksource |
| 502 | * @dev: unused |
| 503 | * @buf: char buffer to be filled with clocksource list |
| 504 | * |
| 505 | * Provides sysfs interface for listing registered clocksources |
| 506 | */ |
| 507 | static ssize_t |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 508 | sysfs_show_available_clocksources(struct sys_device *dev, |
| 509 | struct sysdev_attribute *attr, |
| 510 | char *buf) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 511 | { |
Matthias Kaehlcke | 2e19758 | 2007-10-18 23:39:58 -0700 | [diff] [blame] | 512 | struct clocksource *src; |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 513 | ssize_t count = 0; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 514 | |
| 515 | spin_lock_irq(&clocksource_lock); |
Matthias Kaehlcke | 2e19758 | 2007-10-18 23:39:58 -0700 | [diff] [blame] | 516 | list_for_each_entry(src, &clocksource_list, list) { |
Thomas Gleixner | cd6d95d | 2009-06-12 11:29:27 +0200 | [diff] [blame] | 517 | /* |
| 518 | * Don't show non-HRES clocksource if the tick code is |
| 519 | * in one shot mode (highres=on or nohz=on) |
| 520 | */ |
| 521 | if (!tick_oneshot_mode_active() || |
| 522 | (src->flags & CLOCK_SOURCE_VALID_FOR_HRES)) |
john stultz | 3f68535 | 2009-01-21 22:53:22 -0700 | [diff] [blame] | 523 | count += snprintf(buf + count, |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 524 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), |
| 525 | "%s ", src->name); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 526 | } |
| 527 | spin_unlock_irq(&clocksource_lock); |
| 528 | |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 529 | count += snprintf(buf + count, |
| 530 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n"); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 531 | |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 532 | return count; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* |
| 536 | * Sysfs setup bits: |
| 537 | */ |
Heiko Carstens | 4f95f81 | 2008-05-03 14:23:14 +0200 | [diff] [blame] | 538 | static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources, |
Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 539 | sysfs_override_clocksource); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 540 | |
Heiko Carstens | 4f95f81 | 2008-05-03 14:23:14 +0200 | [diff] [blame] | 541 | static SYSDEV_ATTR(available_clocksource, 0444, |
Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 542 | sysfs_show_available_clocksources, NULL); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 543 | |
| 544 | static struct sysdev_class clocksource_sysclass = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 545 | .name = "clocksource", |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | static struct sys_device device_clocksource = { |
| 549 | .id = 0, |
| 550 | .cls = &clocksource_sysclass, |
| 551 | }; |
| 552 | |
john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 553 | static int __init init_clocksource_sysfs(void) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 554 | { |
| 555 | int error = sysdev_class_register(&clocksource_sysclass); |
| 556 | |
| 557 | if (!error) |
| 558 | error = sysdev_register(&device_clocksource); |
| 559 | if (!error) |
| 560 | error = sysdev_create_file( |
| 561 | &device_clocksource, |
| 562 | &attr_current_clocksource); |
| 563 | if (!error) |
| 564 | error = sysdev_create_file( |
| 565 | &device_clocksource, |
| 566 | &attr_available_clocksource); |
| 567 | return error; |
| 568 | } |
| 569 | |
| 570 | device_initcall(init_clocksource_sysfs); |
Daniel Walker | 2b01370 | 2006-12-10 02:21:30 -0800 | [diff] [blame] | 571 | #endif /* CONFIG_SYSFS */ |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * boot_override_clocksource - boot clock override |
| 575 | * @str: override name |
| 576 | * |
| 577 | * Takes a clocksource= boot argument and uses it |
| 578 | * as the clocksource override name. |
| 579 | */ |
| 580 | static int __init boot_override_clocksource(char* str) |
| 581 | { |
| 582 | unsigned long flags; |
| 583 | spin_lock_irqsave(&clocksource_lock, flags); |
| 584 | if (str) |
| 585 | strlcpy(override_name, str, sizeof(override_name)); |
| 586 | spin_unlock_irqrestore(&clocksource_lock, flags); |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | __setup("clocksource=", boot_override_clocksource); |
| 591 | |
| 592 | /** |
| 593 | * boot_override_clock - Compatibility layer for deprecated boot option |
| 594 | * @str: override name |
| 595 | * |
| 596 | * DEPRECATED! Takes a clock= boot argument and uses it |
| 597 | * as the clocksource override name |
| 598 | */ |
| 599 | static int __init boot_override_clock(char* str) |
| 600 | { |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 601 | if (!strcmp(str, "pmtmr")) { |
| 602 | printk("Warning: clock=pmtmr is deprecated. " |
| 603 | "Use clocksource=acpi_pm.\n"); |
| 604 | return boot_override_clocksource("acpi_pm"); |
| 605 | } |
| 606 | printk("Warning! clock= boot option is deprecated. " |
| 607 | "Use clocksource=xyz\n"); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 608 | return boot_override_clocksource(str); |
| 609 | } |
| 610 | |
| 611 | __setup("clock=", boot_override_clock); |