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 | |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 26 | #include <linux/device.h> |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 27 | #include <linux/clocksource.h> |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 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> |
Martin Schwidefsky | 01548f4 | 2009-08-18 17:09:42 +0200 | [diff] [blame] | 32 | #include <linux/kthread.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 | } |
David S. Miller | 3586e0a | 2009-11-11 19:06:30 -0800 | [diff] [blame] | 42 | EXPORT_SYMBOL_GPL(timecounter_init); |
Patrick Ohly | a038a35 | 2009-02-12 05:03:34 +0000 | [diff] [blame] | 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 | } |
David S. Miller | 3586e0a | 2009-11-11 19:06:30 -0800 | [diff] [blame] | 86 | EXPORT_SYMBOL_GPL(timecounter_read); |
Patrick Ohly | a038a35 | 2009-02-12 05:03:34 +0000 | [diff] [blame] | 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 | } |
David S. Miller | 3586e0a | 2009-11-11 19:06:30 -0800 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(timecounter_cyc2time); |
Patrick Ohly | a038a35 | 2009-02-12 05:03:34 +0000 | [diff] [blame] | 109 | |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 110 | /** |
| 111 | * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks |
| 112 | * @mult: pointer to mult variable |
| 113 | * @shift: pointer to shift variable |
| 114 | * @from: frequency to convert from |
| 115 | * @to: frequency to convert to |
Nicolas Pitre | 5fdade9 | 2011-01-11 12:18:12 -0500 | [diff] [blame] | 116 | * @maxsec: guaranteed runtime conversion range in seconds |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 117 | * |
| 118 | * The function evaluates the shift/mult pair for the scaled math |
| 119 | * operations of clocksources and clockevents. |
| 120 | * |
| 121 | * @to and @from are frequency values in HZ. For clock sources @to is |
| 122 | * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock |
| 123 | * event @to is the counter frequency and @from is NSEC_PER_SEC. |
| 124 | * |
Nicolas Pitre | 5fdade9 | 2011-01-11 12:18:12 -0500 | [diff] [blame] | 125 | * The @maxsec conversion range argument controls the time frame in |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 126 | * seconds which must be covered by the runtime conversion with the |
| 127 | * calculated mult and shift factors. This guarantees that no 64bit |
| 128 | * overflow happens when the input value of the conversion is |
| 129 | * multiplied with the calculated mult factor. Larger ranges may |
| 130 | * reduce the conversion accuracy by chosing smaller mult and shift |
| 131 | * factors. |
| 132 | */ |
| 133 | void |
Nicolas Pitre | 5fdade9 | 2011-01-11 12:18:12 -0500 | [diff] [blame] | 134 | clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec) |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 135 | { |
| 136 | u64 tmp; |
| 137 | u32 sft, sftacc= 32; |
| 138 | |
| 139 | /* |
| 140 | * Calculate the shift factor which is limiting the conversion |
| 141 | * range: |
| 142 | */ |
Nicolas Pitre | 5fdade9 | 2011-01-11 12:18:12 -0500 | [diff] [blame] | 143 | tmp = ((u64)maxsec * from) >> 32; |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 144 | while (tmp) { |
| 145 | tmp >>=1; |
| 146 | sftacc--; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Find the conversion shift/mult pair which has the best |
| 151 | * accuracy and fits the maxsec conversion range: |
| 152 | */ |
| 153 | for (sft = 32; sft > 0; sft--) { |
| 154 | tmp = (u64) to << sft; |
john stultz | b5776c4 | 2010-12-16 19:03:27 +0000 | [diff] [blame] | 155 | tmp += from / 2; |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 156 | do_div(tmp, from); |
| 157 | if ((tmp >> sftacc) == 0) |
| 158 | break; |
| 159 | } |
| 160 | *mult = tmp; |
| 161 | *shift = sft; |
| 162 | } |
| 163 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 164 | /*[Clocksource internal variables]--------- |
| 165 | * curr_clocksource: |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 166 | * currently selected clocksource. |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 167 | * clocksource_list: |
| 168 | * linked list with the registered clocksources |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 169 | * clocksource_mutex: |
| 170 | * protects manipulations to curr_clocksource and the clocksource_list |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 171 | * override_name: |
| 172 | * Name of the user-specified clocksource. |
| 173 | */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 174 | static struct clocksource *curr_clocksource; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 175 | static LIST_HEAD(clocksource_list); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 176 | static DEFINE_MUTEX(clocksource_mutex); |
Thomas Gleixner | 29b5407 | 2013-04-25 20:31:45 +0000 | [diff] [blame^] | 177 | #define CS_NAME_LEN 32 |
| 178 | static char override_name[CS_NAME_LEN]; |
Thomas Gleixner | 54a6bc0 | 2009-09-14 19:49:02 +0200 | [diff] [blame] | 179 | static int finished_booting; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 180 | |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 181 | #ifdef CONFIG_CLOCKSOURCE_WATCHDOG |
Martin Schwidefsky | f79e025 | 2009-09-11 15:33:05 +0200 | [diff] [blame] | 182 | static void clocksource_watchdog_work(struct work_struct *work); |
| 183 | |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 184 | static LIST_HEAD(watchdog_list); |
| 185 | static struct clocksource *watchdog; |
| 186 | static struct timer_list watchdog_timer; |
Martin Schwidefsky | f79e025 | 2009-09-11 15:33:05 +0200 | [diff] [blame] | 187 | static DECLARE_WORK(watchdog_work, clocksource_watchdog_work); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 188 | static DEFINE_SPINLOCK(watchdog_lock); |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 189 | static int watchdog_running; |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 190 | static atomic_t watchdog_reset_pending; |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 191 | |
Martin Schwidefsky | 01548f4 | 2009-08-18 17:09:42 +0200 | [diff] [blame] | 192 | static int clocksource_watchdog_kthread(void *data); |
Thomas Gleixner | d0981a1 | 2009-08-19 11:26:09 +0200 | [diff] [blame] | 193 | static void __clocksource_change_rating(struct clocksource *cs, int rating); |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 194 | |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 195 | /* |
Daniel Walker | 35c35d1 | 2007-05-09 02:33:40 -0700 | [diff] [blame] | 196 | * Interval: 0.5sec Threshold: 0.0625s |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 197 | */ |
| 198 | #define WATCHDOG_INTERVAL (HZ >> 1) |
Daniel Walker | 35c35d1 | 2007-05-09 02:33:40 -0700 | [diff] [blame] | 199 | #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 200 | |
Martin Schwidefsky | 01548f4 | 2009-08-18 17:09:42 +0200 | [diff] [blame] | 201 | static void clocksource_watchdog_work(struct work_struct *work) |
| 202 | { |
| 203 | /* |
| 204 | * If kthread_run fails the next watchdog scan over the |
| 205 | * watchdog_list will find the unstable clock again. |
| 206 | */ |
| 207 | kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog"); |
| 208 | } |
| 209 | |
Thomas Gleixner | 7285dd7 | 2009-08-28 20:25:24 +0200 | [diff] [blame] | 210 | static void __clocksource_unstable(struct clocksource *cs) |
| 211 | { |
| 212 | cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); |
| 213 | cs->flags |= CLOCK_SOURCE_UNSTABLE; |
Thomas Gleixner | 54a6bc0 | 2009-09-14 19:49:02 +0200 | [diff] [blame] | 214 | if (finished_booting) |
| 215 | schedule_work(&watchdog_work); |
Thomas Gleixner | 7285dd7 | 2009-08-28 20:25:24 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Martin Schwidefsky | 8cf4e75 | 2009-08-14 15:47:22 +0200 | [diff] [blame] | 218 | static void clocksource_unstable(struct clocksource *cs, int64_t delta) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 219 | { |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 220 | printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n", |
| 221 | cs->name, delta); |
Thomas Gleixner | 7285dd7 | 2009-08-28 20:25:24 +0200 | [diff] [blame] | 222 | __clocksource_unstable(cs); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * clocksource_mark_unstable - mark clocksource unstable via watchdog |
| 227 | * @cs: clocksource to be marked unstable |
| 228 | * |
| 229 | * This function is called instead of clocksource_change_rating from |
| 230 | * cpu hotplug code to avoid a deadlock between the clocksource mutex |
| 231 | * and the cpu hotplug mutex. It defers the update of the clocksource |
| 232 | * to the watchdog thread. |
| 233 | */ |
| 234 | void clocksource_mark_unstable(struct clocksource *cs) |
| 235 | { |
| 236 | unsigned long flags; |
| 237 | |
| 238 | spin_lock_irqsave(&watchdog_lock, flags); |
| 239 | if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) { |
| 240 | if (list_empty(&cs->wd_list)) |
| 241 | list_add(&cs->wd_list, &watchdog_list); |
| 242 | __clocksource_unstable(cs); |
| 243 | } |
| 244 | spin_unlock_irqrestore(&watchdog_lock, flags); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static void clocksource_watchdog(unsigned long data) |
| 248 | { |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 249 | struct clocksource *cs; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 250 | cycle_t csnow, wdnow; |
| 251 | int64_t wd_nsec, cs_nsec; |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 252 | int next_cpu, reset_pending; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 253 | |
| 254 | spin_lock(&watchdog_lock); |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 255 | if (!watchdog_running) |
| 256 | goto out; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 257 | |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 258 | reset_pending = atomic_read(&watchdog_reset_pending); |
| 259 | |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 260 | list_for_each_entry(cs, &watchdog_list, wd_list) { |
| 261 | |
| 262 | /* Clocksource already marked unstable? */ |
Martin Schwidefsky | 01548f4 | 2009-08-18 17:09:42 +0200 | [diff] [blame] | 263 | if (cs->flags & CLOCK_SOURCE_UNSTABLE) { |
Thomas Gleixner | 54a6bc0 | 2009-09-14 19:49:02 +0200 | [diff] [blame] | 264 | if (finished_booting) |
| 265 | schedule_work(&watchdog_work); |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 266 | continue; |
Martin Schwidefsky | 01548f4 | 2009-08-18 17:09:42 +0200 | [diff] [blame] | 267 | } |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 268 | |
Thomas Gleixner | b519951 | 2011-06-16 16:22:08 +0200 | [diff] [blame] | 269 | local_irq_disable(); |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 270 | csnow = cs->read(cs); |
Thomas Gleixner | b519951 | 2011-06-16 16:22:08 +0200 | [diff] [blame] | 271 | wdnow = watchdog->read(watchdog); |
| 272 | local_irq_enable(); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 273 | |
Martin Schwidefsky | 8cf4e75 | 2009-08-14 15:47:22 +0200 | [diff] [blame] | 274 | /* Clocksource initialized ? */ |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 275 | if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) || |
| 276 | atomic_read(&watchdog_reset_pending)) { |
Martin Schwidefsky | 8cf4e75 | 2009-08-14 15:47:22 +0200 | [diff] [blame] | 277 | cs->flags |= CLOCK_SOURCE_WATCHDOG; |
Thomas Gleixner | b519951 | 2011-06-16 16:22:08 +0200 | [diff] [blame] | 278 | cs->wd_last = wdnow; |
| 279 | cs->cs_last = csnow; |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 280 | continue; |
| 281 | } |
| 282 | |
Thomas Gleixner | b519951 | 2011-06-16 16:22:08 +0200 | [diff] [blame] | 283 | wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask, |
| 284 | watchdog->mult, watchdog->shift); |
| 285 | |
| 286 | cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) & |
Martin Schwidefsky | 155ec60 | 2009-08-14 15:47:26 +0200 | [diff] [blame] | 287 | cs->mask, cs->mult, cs->shift); |
Thomas Gleixner | b519951 | 2011-06-16 16:22:08 +0200 | [diff] [blame] | 288 | cs->cs_last = csnow; |
| 289 | cs->wd_last = wdnow; |
| 290 | |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 291 | if (atomic_read(&watchdog_reset_pending)) |
| 292 | continue; |
| 293 | |
Thomas Gleixner | b519951 | 2011-06-16 16:22:08 +0200 | [diff] [blame] | 294 | /* Check the deviation from the watchdog clocksource. */ |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 295 | if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) { |
Martin Schwidefsky | 8cf4e75 | 2009-08-14 15:47:22 +0200 | [diff] [blame] | 296 | clocksource_unstable(cs, cs_nsec - wd_nsec); |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && |
| 301 | (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && |
| 302 | (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { |
| 303 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
| 304 | /* |
| 305 | * We just marked the clocksource as highres-capable, |
| 306 | * notify the rest of the system as well so that we |
| 307 | * transition into high-res mode: |
| 308 | */ |
| 309 | tick_clock_notify(); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 313 | /* |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 314 | * We only clear the watchdog_reset_pending, when we did a |
| 315 | * full cycle through all clocksources. |
| 316 | */ |
| 317 | if (reset_pending) |
| 318 | atomic_dec(&watchdog_reset_pending); |
| 319 | |
| 320 | /* |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 321 | * Cycle through CPUs to check if the CPUs stay synchronized |
| 322 | * to each other. |
| 323 | */ |
| 324 | next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask); |
| 325 | if (next_cpu >= nr_cpu_ids) |
| 326 | next_cpu = cpumask_first(cpu_online_mask); |
| 327 | watchdog_timer.expires += WATCHDOG_INTERVAL; |
| 328 | add_timer_on(&watchdog_timer, next_cpu); |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 329 | out: |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 330 | spin_unlock(&watchdog_lock); |
| 331 | } |
Martin Schwidefsky | 0f8e8ef | 2009-08-14 15:47:23 +0200 | [diff] [blame] | 332 | |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 333 | static inline void clocksource_start_watchdog(void) |
| 334 | { |
| 335 | if (watchdog_running || !watchdog || list_empty(&watchdog_list)) |
| 336 | return; |
| 337 | init_timer(&watchdog_timer); |
| 338 | watchdog_timer.function = clocksource_watchdog; |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 339 | watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; |
| 340 | add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask)); |
| 341 | watchdog_running = 1; |
| 342 | } |
| 343 | |
| 344 | static inline void clocksource_stop_watchdog(void) |
| 345 | { |
| 346 | if (!watchdog_running || (watchdog && !list_empty(&watchdog_list))) |
| 347 | return; |
| 348 | del_timer(&watchdog_timer); |
| 349 | watchdog_running = 0; |
| 350 | } |
| 351 | |
Martin Schwidefsky | 0f8e8ef | 2009-08-14 15:47:23 +0200 | [diff] [blame] | 352 | static inline void clocksource_reset_watchdog(void) |
| 353 | { |
| 354 | struct clocksource *cs; |
| 355 | |
| 356 | list_for_each_entry(cs, &watchdog_list, wd_list) |
| 357 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; |
| 358 | } |
| 359 | |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 360 | static void clocksource_resume_watchdog(void) |
| 361 | { |
Thomas Gleixner | 9fb6033 | 2011-09-12 13:32:23 +0200 | [diff] [blame] | 362 | atomic_inc(&watchdog_reset_pending); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 365 | static void clocksource_enqueue_watchdog(struct clocksource *cs) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 366 | { |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 367 | unsigned long flags; |
| 368 | |
| 369 | spin_lock_irqsave(&watchdog_lock, flags); |
| 370 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 371 | /* cs is a clocksource to be watched. */ |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 372 | list_add(&cs->wd_list, &watchdog_list); |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 373 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; |
Thomas Gleixner | 948ac6d | 2007-03-25 14:42:51 +0200 | [diff] [blame] | 374 | } else { |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 375 | /* cs is a watchdog. */ |
Thomas Gleixner | 948ac6d | 2007-03-25 14:42:51 +0200 | [diff] [blame] | 376 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 377 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 378 | /* Pick the best watchdog. */ |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 379 | if (!watchdog || cs->rating > watchdog->rating) { |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 380 | watchdog = cs; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 381 | /* Reset watchdog cycles */ |
Martin Schwidefsky | 0f8e8ef | 2009-08-14 15:47:23 +0200 | [diff] [blame] | 382 | clocksource_reset_watchdog(); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 383 | } |
| 384 | } |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 385 | /* Check if the watchdog timer needs to be started. */ |
| 386 | clocksource_start_watchdog(); |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 387 | spin_unlock_irqrestore(&watchdog_lock, flags); |
| 388 | } |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 389 | |
| 390 | static void clocksource_dequeue_watchdog(struct clocksource *cs) |
| 391 | { |
| 392 | struct clocksource *tmp; |
| 393 | unsigned long flags; |
| 394 | |
| 395 | spin_lock_irqsave(&watchdog_lock, flags); |
| 396 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { |
| 397 | /* cs is a watched clocksource. */ |
| 398 | list_del_init(&cs->wd_list); |
| 399 | } else if (cs == watchdog) { |
| 400 | /* Reset watchdog cycles */ |
| 401 | clocksource_reset_watchdog(); |
| 402 | /* Current watchdog is removed. Find an alternative. */ |
| 403 | watchdog = NULL; |
| 404 | list_for_each_entry(tmp, &clocksource_list, list) { |
| 405 | if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY) |
| 406 | continue; |
| 407 | if (!watchdog || tmp->rating > watchdog->rating) |
| 408 | watchdog = tmp; |
| 409 | } |
| 410 | } |
| 411 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; |
| 412 | /* Check if the watchdog timer needs to be stopped. */ |
| 413 | clocksource_stop_watchdog(); |
| 414 | spin_unlock_irqrestore(&watchdog_lock, flags); |
| 415 | } |
| 416 | |
Martin Schwidefsky | 01548f4 | 2009-08-18 17:09:42 +0200 | [diff] [blame] | 417 | static int clocksource_watchdog_kthread(void *data) |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 418 | { |
| 419 | struct clocksource *cs, *tmp; |
| 420 | unsigned long flags; |
Thomas Gleixner | 6ea41d2 | 2009-08-15 13:20:42 +0200 | [diff] [blame] | 421 | LIST_HEAD(unstable); |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 422 | |
Thomas Gleixner | d0981a1 | 2009-08-19 11:26:09 +0200 | [diff] [blame] | 423 | mutex_lock(&clocksource_mutex); |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 424 | spin_lock_irqsave(&watchdog_lock, flags); |
| 425 | list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) |
| 426 | if (cs->flags & CLOCK_SOURCE_UNSTABLE) { |
| 427 | list_del_init(&cs->wd_list); |
Thomas Gleixner | 6ea41d2 | 2009-08-15 13:20:42 +0200 | [diff] [blame] | 428 | list_add(&cs->wd_list, &unstable); |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 429 | } |
| 430 | /* Check if the watchdog timer needs to be stopped. */ |
| 431 | clocksource_stop_watchdog(); |
Thomas Gleixner | 6ea41d2 | 2009-08-15 13:20:42 +0200 | [diff] [blame] | 432 | spin_unlock_irqrestore(&watchdog_lock, flags); |
| 433 | |
| 434 | /* Needs to be done outside of watchdog lock */ |
| 435 | list_for_each_entry_safe(cs, tmp, &unstable, wd_list) { |
| 436 | list_del_init(&cs->wd_list); |
Thomas Gleixner | d0981a1 | 2009-08-19 11:26:09 +0200 | [diff] [blame] | 437 | __clocksource_change_rating(cs, 0); |
Thomas Gleixner | 6ea41d2 | 2009-08-15 13:20:42 +0200 | [diff] [blame] | 438 | } |
Thomas Gleixner | d0981a1 | 2009-08-19 11:26:09 +0200 | [diff] [blame] | 439 | mutex_unlock(&clocksource_mutex); |
Martin Schwidefsky | 01548f4 | 2009-08-18 17:09:42 +0200 | [diff] [blame] | 440 | return 0; |
Martin Schwidefsky | c55c87c | 2009-08-14 15:47:25 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 443 | #else /* CONFIG_CLOCKSOURCE_WATCHDOG */ |
| 444 | |
| 445 | static void clocksource_enqueue_watchdog(struct clocksource *cs) |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 446 | { |
| 447 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) |
| 448 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
| 449 | } |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 450 | |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 451 | static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { } |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 452 | static inline void clocksource_resume_watchdog(void) { } |
Thomas Gleixner | 54a6bc0 | 2009-09-14 19:49:02 +0200 | [diff] [blame] | 453 | static inline int clocksource_watchdog_kthread(void *data) { return 0; } |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 454 | |
| 455 | #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */ |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 456 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 457 | /** |
Magnus Damm | c54a42b | 2010-02-02 14:41:41 -0800 | [diff] [blame] | 458 | * clocksource_suspend - suspend the clocksource(s) |
| 459 | */ |
| 460 | void clocksource_suspend(void) |
| 461 | { |
| 462 | struct clocksource *cs; |
| 463 | |
| 464 | list_for_each_entry_reverse(cs, &clocksource_list, list) |
| 465 | if (cs->suspend) |
| 466 | cs->suspend(cs); |
| 467 | } |
| 468 | |
| 469 | /** |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 470 | * clocksource_resume - resume the clocksource(s) |
| 471 | */ |
| 472 | void clocksource_resume(void) |
| 473 | { |
Matthias Kaehlcke | 2e19758 | 2007-10-18 23:39:58 -0700 | [diff] [blame] | 474 | struct clocksource *cs; |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 475 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 476 | list_for_each_entry(cs, &clocksource_list, list) |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 477 | if (cs->resume) |
Magnus Damm | 1762233 | 2010-02-02 14:41:39 -0800 | [diff] [blame] | 478 | cs->resume(cs); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 479 | |
| 480 | clocksource_resume_watchdog(); |
Thomas Gleixner | b52f52a | 2007-05-09 02:35:15 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** |
Jason Wessel | 7c3078b | 2008-02-15 14:55:54 -0600 | [diff] [blame] | 484 | * clocksource_touch_watchdog - Update watchdog |
| 485 | * |
| 486 | * Update the watchdog after exception contexts such as kgdb so as not |
Thomas Gleixner | 7b7422a | 2010-01-26 12:51:10 +0100 | [diff] [blame] | 487 | * to incorrectly trip the watchdog. This might fail when the kernel |
| 488 | * was stopped in code which holds watchdog_lock. |
Jason Wessel | 7c3078b | 2008-02-15 14:55:54 -0600 | [diff] [blame] | 489 | */ |
| 490 | void clocksource_touch_watchdog(void) |
| 491 | { |
| 492 | clocksource_resume_watchdog(); |
| 493 | } |
| 494 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 495 | /** |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 496 | * clocksource_max_adjustment- Returns max adjustment amount |
| 497 | * @cs: Pointer to clocksource |
| 498 | * |
| 499 | */ |
| 500 | static u32 clocksource_max_adjustment(struct clocksource *cs) |
| 501 | { |
| 502 | u64 ret; |
| 503 | /* |
Jim Cromie | 88b28ad | 2012-03-14 21:28:56 -0600 | [diff] [blame] | 504 | * We won't try to correct for more than 11% adjustments (110,000 ppm), |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 505 | */ |
| 506 | ret = (u64)cs->mult * 11; |
| 507 | do_div(ret,100); |
| 508 | return (u32)ret; |
| 509 | } |
| 510 | |
| 511 | /** |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 512 | * clocksource_max_deferment - Returns max time the clocksource can be deferred |
| 513 | * @cs: Pointer to clocksource |
| 514 | * |
| 515 | */ |
| 516 | static u64 clocksource_max_deferment(struct clocksource *cs) |
| 517 | { |
| 518 | u64 max_nsecs, max_cycles; |
| 519 | |
| 520 | /* |
| 521 | * Calculate the maximum number of cycles that we can pass to the |
| 522 | * cyc2ns function without overflowing a 64-bit signed result. The |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 523 | * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj) |
| 524 | * which is equivalent to the below. |
| 525 | * max_cycles < (2^63)/(cs->mult + cs->maxadj) |
| 526 | * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj))) |
| 527 | * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj)) |
| 528 | * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj)) |
| 529 | * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj)) |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 530 | * Please note that we add 1 to the result of the log2 to account for |
| 531 | * any rounding errors, ensure the above inequality is satisfied and |
| 532 | * no overflow will occur. |
| 533 | */ |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 534 | max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1)); |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * The actual maximum number of cycles we can defer the clocksource is |
| 538 | * determined by the minimum of max_cycles and cs->mask. |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 539 | * Note: Here we subtract the maxadj to make sure we don't sleep for |
| 540 | * too long if there's a large negative adjustment. |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 541 | */ |
| 542 | max_cycles = min_t(u64, max_cycles, (u64) cs->mask); |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 543 | max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj, |
| 544 | cs->shift); |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * To ensure that the clocksource does not wrap whilst we are idle, |
| 548 | * limit the time the clocksource can be deferred by 12.5%. Please |
| 549 | * note a margin of 12.5% is used because this can be computed with |
| 550 | * a shift, versus say 10% which would require division. |
| 551 | */ |
Yang Honggang (Joseph) | b1f9196 | 2011-12-01 22:22:41 -0500 | [diff] [blame] | 552 | return max_nsecs - (max_nsecs >> 3); |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 553 | } |
| 554 | |
John Stultz | 592913e | 2010-07-13 17:56:20 -0700 | [diff] [blame] | 555 | #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 556 | |
Thomas Gleixner | f5a2e34 | 2013-04-25 20:31:45 +0000 | [diff] [blame] | 557 | static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur) |
Thomas Gleixner | 5d33b88 | 2013-04-25 20:31:43 +0000 | [diff] [blame] | 558 | { |
| 559 | struct clocksource *cs; |
| 560 | |
| 561 | if (!finished_booting || list_empty(&clocksource_list)) |
| 562 | return NULL; |
| 563 | |
| 564 | /* |
| 565 | * We pick the clocksource with the highest rating. If oneshot |
| 566 | * mode is active, we pick the highres valid clocksource with |
| 567 | * the best rating. |
| 568 | */ |
| 569 | list_for_each_entry(cs, &clocksource_list, list) { |
Thomas Gleixner | f5a2e34 | 2013-04-25 20:31:45 +0000 | [diff] [blame] | 570 | if (skipcur && cs == curr_clocksource) |
| 571 | continue; |
Thomas Gleixner | 5d33b88 | 2013-04-25 20:31:43 +0000 | [diff] [blame] | 572 | if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES)) |
| 573 | continue; |
| 574 | return cs; |
| 575 | } |
| 576 | return NULL; |
| 577 | } |
| 578 | |
Thomas Gleixner | f5a2e34 | 2013-04-25 20:31:45 +0000 | [diff] [blame] | 579 | static void __clocksource_select(bool skipcur) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 580 | { |
Thomas Gleixner | 5d33b88 | 2013-04-25 20:31:43 +0000 | [diff] [blame] | 581 | bool oneshot = tick_oneshot_mode_active(); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 582 | struct clocksource *best, *cs; |
Thomas Gleixner | 5d8b34f | 2007-02-16 01:27:43 -0800 | [diff] [blame] | 583 | |
Thomas Gleixner | 5d33b88 | 2013-04-25 20:31:43 +0000 | [diff] [blame] | 584 | /* Find the best suitable clocksource */ |
Thomas Gleixner | f5a2e34 | 2013-04-25 20:31:45 +0000 | [diff] [blame] | 585 | best = clocksource_find_best(oneshot, skipcur); |
Thomas Gleixner | 5d33b88 | 2013-04-25 20:31:43 +0000 | [diff] [blame] | 586 | if (!best) |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 587 | return; |
Thomas Gleixner | 5d33b88 | 2013-04-25 20:31:43 +0000 | [diff] [blame] | 588 | |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 589 | /* Check for the override clocksource. */ |
| 590 | list_for_each_entry(cs, &clocksource_list, list) { |
Thomas Gleixner | f5a2e34 | 2013-04-25 20:31:45 +0000 | [diff] [blame] | 591 | if (skipcur && cs == curr_clocksource) |
| 592 | continue; |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 593 | if (strcmp(cs->name, override_name) != 0) |
| 594 | continue; |
| 595 | /* |
| 596 | * Check to make sure we don't switch to a non-highres |
| 597 | * capable clocksource if the tick code is in oneshot |
| 598 | * mode (highres or nohz) |
| 599 | */ |
Thomas Gleixner | 5d33b88 | 2013-04-25 20:31:43 +0000 | [diff] [blame] | 600 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) { |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 601 | /* Override clocksource cannot be used. */ |
| 602 | printk(KERN_WARNING "Override clocksource %s is not " |
| 603 | "HRT compatible. Cannot switch while in " |
| 604 | "HRT/NOHZ mode\n", cs->name); |
| 605 | override_name[0] = 0; |
| 606 | } else |
| 607 | /* Override clocksource can be used. */ |
| 608 | best = cs; |
| 609 | break; |
| 610 | } |
Thomas Gleixner | ba919d1 | 2013-04-25 20:31:44 +0000 | [diff] [blame] | 611 | |
| 612 | if (curr_clocksource != best && !timekeeping_notify(best)) { |
| 613 | pr_info("Switched to clocksource %s\n", best->name); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 614 | curr_clocksource = best; |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 615 | } |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Thomas Gleixner | f5a2e34 | 2013-04-25 20:31:45 +0000 | [diff] [blame] | 618 | /** |
| 619 | * clocksource_select - Select the best clocksource available |
| 620 | * |
| 621 | * Private function. Must hold clocksource_mutex when called. |
| 622 | * |
| 623 | * Select the clocksource with the best rating, or the clocksource, |
| 624 | * which is selected by userspace override. |
| 625 | */ |
| 626 | static void clocksource_select(void) |
| 627 | { |
| 628 | return __clocksource_select(false); |
| 629 | } |
| 630 | |
John Stultz | 592913e | 2010-07-13 17:56:20 -0700 | [diff] [blame] | 631 | #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */ |
Thomas Gleixner | 54a6bc0 | 2009-09-14 19:49:02 +0200 | [diff] [blame] | 632 | |
| 633 | static inline void clocksource_select(void) { } |
| 634 | |
| 635 | #endif |
| 636 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 637 | /* |
| 638 | * clocksource_done_booting - Called near the end of core bootup |
| 639 | * |
| 640 | * Hack to avoid lots of clocksource churn at boot time. |
| 641 | * We use fs_initcall because we want this to start before |
| 642 | * device_initcall but after subsys_initcall. |
| 643 | */ |
| 644 | static int __init clocksource_done_booting(void) |
| 645 | { |
john stultz | ad6759f | 2010-03-01 12:34:43 -0800 | [diff] [blame] | 646 | mutex_lock(&clocksource_mutex); |
| 647 | curr_clocksource = clocksource_default_clock(); |
| 648 | mutex_unlock(&clocksource_mutex); |
| 649 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 650 | finished_booting = 1; |
Thomas Gleixner | 54a6bc0 | 2009-09-14 19:49:02 +0200 | [diff] [blame] | 651 | |
| 652 | /* |
| 653 | * Run the watchdog first to eliminate unstable clock sources |
| 654 | */ |
| 655 | clocksource_watchdog_kthread(NULL); |
| 656 | |
Thomas Gleixner | e6c7330 | 2009-09-14 19:51:11 +0200 | [diff] [blame] | 657 | mutex_lock(&clocksource_mutex); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 658 | clocksource_select(); |
Thomas Gleixner | e6c7330 | 2009-09-14 19:51:11 +0200 | [diff] [blame] | 659 | mutex_unlock(&clocksource_mutex); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 660 | return 0; |
| 661 | } |
| 662 | fs_initcall(clocksource_done_booting); |
| 663 | |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 664 | /* |
| 665 | * Enqueue the clocksource sorted by rating |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 666 | */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 667 | static void clocksource_enqueue(struct clocksource *cs) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 668 | { |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 669 | struct list_head *entry = &clocksource_list; |
| 670 | struct clocksource *tmp; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 671 | |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 672 | list_for_each_entry(tmp, &clocksource_list, list) |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 673 | /* Keep track of the place, where to insert */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 674 | if (tmp->rating >= cs->rating) |
| 675 | entry = &tmp->list; |
| 676 | list_add(&cs->list, entry); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 677 | } |
| 678 | |
John Stultz | d7e81c2 | 2010-05-07 18:07:38 -0700 | [diff] [blame] | 679 | /** |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 680 | * __clocksource_updatefreq_scale - Used update clocksource with new freq |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 681 | * @cs: clocksource to be registered |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 682 | * @scale: Scale factor multiplied against freq to get clocksource hz |
| 683 | * @freq: clocksource frequency (cycles per second) divided by scale |
| 684 | * |
| 685 | * This should only be called from the clocksource->enable() method. |
| 686 | * |
| 687 | * This *SHOULD NOT* be called directly! Please use the |
| 688 | * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions. |
| 689 | */ |
| 690 | void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) |
| 691 | { |
Thomas Gleixner | c0e299b | 2011-05-20 10:50:52 +0200 | [diff] [blame] | 692 | u64 sec; |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 693 | /* |
Thomas Gleixner | 724ed53 | 2011-05-18 21:33:40 +0000 | [diff] [blame] | 694 | * Calc the maximum number of seconds which we can run before |
| 695 | * wrapping around. For clocksources which have a mask > 32bit |
| 696 | * we need to limit the max sleep time to have a good |
| 697 | * conversion precision. 10 minutes is still a reasonable |
| 698 | * amount. That results in a shift value of 24 for a |
| 699 | * clocksource with mask >= 40bit and f >= 4GHz. That maps to |
| 700 | * ~ 0.06ppm granularity for NTP. We apply the same 12.5% |
| 701 | * margin as we do in clocksource_max_deferment() |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 702 | */ |
Yang Honggang (Joseph) | b1f9196 | 2011-12-01 22:22:41 -0500 | [diff] [blame] | 703 | sec = (cs->mask - (cs->mask >> 3)); |
Thomas Gleixner | 724ed53 | 2011-05-18 21:33:40 +0000 | [diff] [blame] | 704 | do_div(sec, freq); |
| 705 | do_div(sec, scale); |
| 706 | if (!sec) |
| 707 | sec = 1; |
| 708 | else if (sec > 600 && cs->mask > UINT_MAX) |
| 709 | sec = 600; |
| 710 | |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 711 | clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, |
Thomas Gleixner | 724ed53 | 2011-05-18 21:33:40 +0000 | [diff] [blame] | 712 | NSEC_PER_SEC / scale, sec * scale); |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * for clocksources that have large mults, to avoid overflow. |
| 716 | * Since mult may be adjusted by ntp, add an safety extra margin |
| 717 | * |
| 718 | */ |
| 719 | cs->maxadj = clocksource_max_adjustment(cs); |
| 720 | while ((cs->mult + cs->maxadj < cs->mult) |
| 721 | || (cs->mult - cs->maxadj > cs->mult)) { |
| 722 | cs->mult >>= 1; |
| 723 | cs->shift--; |
| 724 | cs->maxadj = clocksource_max_adjustment(cs); |
| 725 | } |
| 726 | |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 727 | cs->max_idle_ns = clocksource_max_deferment(cs); |
| 728 | } |
| 729 | EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale); |
| 730 | |
| 731 | /** |
John Stultz | d7e81c2 | 2010-05-07 18:07:38 -0700 | [diff] [blame] | 732 | * __clocksource_register_scale - Used to install new clocksources |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 733 | * @cs: clocksource to be registered |
John Stultz | d7e81c2 | 2010-05-07 18:07:38 -0700 | [diff] [blame] | 734 | * @scale: Scale factor multiplied against freq to get clocksource hz |
| 735 | * @freq: clocksource frequency (cycles per second) divided by scale |
| 736 | * |
| 737 | * Returns -EBUSY if registration fails, zero otherwise. |
| 738 | * |
| 739 | * This *SHOULD NOT* be called directly! Please use the |
| 740 | * clocksource_register_hz() or clocksource_register_khz helper functions. |
| 741 | */ |
| 742 | int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) |
| 743 | { |
| 744 | |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 745 | /* Initialize mult/shift and max_idle_ns */ |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 746 | __clocksource_updatefreq_scale(cs, scale, freq); |
John Stultz | d7e81c2 | 2010-05-07 18:07:38 -0700 | [diff] [blame] | 747 | |
John Stultz | 852db46 | 2010-07-13 17:56:28 -0700 | [diff] [blame] | 748 | /* Add clocksource to the clcoksource list */ |
John Stultz | d7e81c2 | 2010-05-07 18:07:38 -0700 | [diff] [blame] | 749 | mutex_lock(&clocksource_mutex); |
| 750 | clocksource_enqueue(cs); |
John Stultz | d7e81c2 | 2010-05-07 18:07:38 -0700 | [diff] [blame] | 751 | clocksource_enqueue_watchdog(cs); |
john stultz | e05b2ef | 2011-05-04 18:16:50 -0700 | [diff] [blame] | 752 | clocksource_select(); |
John Stultz | d7e81c2 | 2010-05-07 18:07:38 -0700 | [diff] [blame] | 753 | mutex_unlock(&clocksource_mutex); |
| 754 | return 0; |
| 755 | } |
| 756 | EXPORT_SYMBOL_GPL(__clocksource_register_scale); |
| 757 | |
| 758 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 759 | /** |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 760 | * clocksource_register - Used to install new clocksources |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 761 | * @cs: clocksource to be registered |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 762 | * |
| 763 | * Returns -EBUSY if registration fails, zero otherwise. |
| 764 | */ |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 765 | int clocksource_register(struct clocksource *cs) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 766 | { |
John Stultz | d65670a | 2011-10-31 17:06:35 -0400 | [diff] [blame] | 767 | /* calculate max adjustment for given mult/shift */ |
| 768 | cs->maxadj = clocksource_max_adjustment(cs); |
| 769 | WARN_ONCE(cs->mult + cs->maxadj < cs->mult, |
| 770 | "Clocksource %s might overflow on 11%% adjustment\n", |
| 771 | cs->name); |
| 772 | |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 773 | /* calculate max idle time permitted for this clocksource */ |
| 774 | cs->max_idle_ns = clocksource_max_deferment(cs); |
| 775 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 776 | mutex_lock(&clocksource_mutex); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 777 | clocksource_enqueue(cs); |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 778 | clocksource_enqueue_watchdog(cs); |
john stultz | e05b2ef | 2011-05-04 18:16:50 -0700 | [diff] [blame] | 779 | clocksource_select(); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 780 | mutex_unlock(&clocksource_mutex); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 781 | return 0; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 782 | } |
john stultz | a275254 | 2006-06-26 00:25:14 -0700 | [diff] [blame] | 783 | EXPORT_SYMBOL(clocksource_register); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 784 | |
Thomas Gleixner | d0981a1 | 2009-08-19 11:26:09 +0200 | [diff] [blame] | 785 | static void __clocksource_change_rating(struct clocksource *cs, int rating) |
| 786 | { |
| 787 | list_del(&cs->list); |
| 788 | cs->rating = rating; |
| 789 | clocksource_enqueue(cs); |
| 790 | clocksource_select(); |
| 791 | } |
| 792 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 793 | /** |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 794 | * clocksource_change_rating - Change the rating of a registered clocksource |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 795 | * @cs: clocksource to be changed |
| 796 | * @rating: new rating |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 797 | */ |
Thomas Gleixner | 92c7e00 | 2007-02-16 01:27:33 -0800 | [diff] [blame] | 798 | void clocksource_change_rating(struct clocksource *cs, int rating) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 799 | { |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 800 | mutex_lock(&clocksource_mutex); |
Thomas Gleixner | d0981a1 | 2009-08-19 11:26:09 +0200 | [diff] [blame] | 801 | __clocksource_change_rating(cs, rating); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 802 | mutex_unlock(&clocksource_mutex); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 803 | } |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 804 | EXPORT_SYMBOL(clocksource_change_rating); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 805 | |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 806 | /** |
| 807 | * clocksource_unregister - remove a registered clocksource |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 808 | * @cs: clocksource to be unregistered |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 809 | */ |
| 810 | void clocksource_unregister(struct clocksource *cs) |
| 811 | { |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 812 | mutex_lock(&clocksource_mutex); |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 813 | clocksource_dequeue_watchdog(cs); |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 814 | list_del(&cs->list); |
Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 815 | clocksource_select(); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 816 | mutex_unlock(&clocksource_mutex); |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 817 | } |
Martin Schwidefsky | fb63a0e | 2009-08-14 15:47:24 +0200 | [diff] [blame] | 818 | EXPORT_SYMBOL(clocksource_unregister); |
Thomas Gleixner | 4713e22c | 2008-01-30 13:30:02 +0100 | [diff] [blame] | 819 | |
Daniel Walker | 2b01370 | 2006-12-10 02:21:30 -0800 | [diff] [blame] | 820 | #ifdef CONFIG_SYSFS |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 821 | /** |
| 822 | * sysfs_show_current_clocksources - sysfs interface for current clocksource |
| 823 | * @dev: unused |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 824 | * @attr: unused |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 825 | * @buf: char buffer to be filled with clocksource list |
| 826 | * |
| 827 | * Provides sysfs interface for listing current clocksource. |
| 828 | */ |
| 829 | static ssize_t |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 830 | sysfs_show_current_clocksources(struct device *dev, |
| 831 | struct device_attribute *attr, char *buf) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 832 | { |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 833 | ssize_t count = 0; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 834 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 835 | mutex_lock(&clocksource_mutex); |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 836 | count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 837 | mutex_unlock(&clocksource_mutex); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 838 | |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 839 | return count; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Thomas Gleixner | 29b5407 | 2013-04-25 20:31:45 +0000 | [diff] [blame^] | 842 | static size_t clocksource_get_uname(const char *buf, char *dst, size_t cnt) |
| 843 | { |
| 844 | size_t ret = cnt; |
| 845 | |
| 846 | /* strings from sysfs write are not 0 terminated! */ |
| 847 | if (!cnt || cnt >= CS_NAME_LEN) |
| 848 | return -EINVAL; |
| 849 | |
| 850 | /* strip of \n: */ |
| 851 | if (buf[cnt-1] == '\n') |
| 852 | cnt--; |
| 853 | if (cnt > 0) |
| 854 | memcpy(dst, buf, cnt); |
| 855 | dst[cnt] = 0; |
| 856 | return ret; |
| 857 | } |
| 858 | |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 859 | /** |
| 860 | * sysfs_override_clocksource - interface for manually overriding clocksource |
| 861 | * @dev: unused |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 862 | * @attr: unused |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 863 | * @buf: name of override clocksource |
| 864 | * @count: length of buffer |
| 865 | * |
| 866 | * Takes input from sysfs interface for manually overriding the default |
Uwe Kleine-König | b71a8eb | 2009-10-06 12:42:51 +0200 | [diff] [blame] | 867 | * clocksource selection. |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 868 | */ |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 869 | static ssize_t sysfs_override_clocksource(struct device *dev, |
| 870 | struct device_attribute *attr, |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 871 | const char *buf, size_t count) |
| 872 | { |
Thomas Gleixner | 29b5407 | 2013-04-25 20:31:45 +0000 | [diff] [blame^] | 873 | size_t ret; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 874 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 875 | mutex_lock(&clocksource_mutex); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 876 | |
Thomas Gleixner | 29b5407 | 2013-04-25 20:31:45 +0000 | [diff] [blame^] | 877 | ret = clocksource_get_uname(buf, override_name, count); |
| 878 | if (ret >= 0) |
| 879 | clocksource_select(); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 880 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 881 | mutex_unlock(&clocksource_mutex); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 882 | |
| 883 | return ret; |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * sysfs_show_available_clocksources - sysfs interface for listing clocksource |
| 888 | * @dev: unused |
Kusanagi Kouichi | b1b73d0 | 2011-12-19 18:13:19 +0900 | [diff] [blame] | 889 | * @attr: unused |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 890 | * @buf: char buffer to be filled with clocksource list |
| 891 | * |
| 892 | * Provides sysfs interface for listing registered clocksources |
| 893 | */ |
| 894 | static ssize_t |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 895 | sysfs_show_available_clocksources(struct device *dev, |
| 896 | struct device_attribute *attr, |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 897 | char *buf) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 898 | { |
Matthias Kaehlcke | 2e19758 | 2007-10-18 23:39:58 -0700 | [diff] [blame] | 899 | struct clocksource *src; |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 900 | ssize_t count = 0; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 901 | |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 902 | mutex_lock(&clocksource_mutex); |
Matthias Kaehlcke | 2e19758 | 2007-10-18 23:39:58 -0700 | [diff] [blame] | 903 | list_for_each_entry(src, &clocksource_list, list) { |
Thomas Gleixner | cd6d95d | 2009-06-12 11:29:27 +0200 | [diff] [blame] | 904 | /* |
| 905 | * Don't show non-HRES clocksource if the tick code is |
| 906 | * in one shot mode (highres=on or nohz=on) |
| 907 | */ |
| 908 | if (!tick_oneshot_mode_active() || |
| 909 | (src->flags & CLOCK_SOURCE_VALID_FOR_HRES)) |
john stultz | 3f68535 | 2009-01-21 22:53:22 -0700 | [diff] [blame] | 910 | count += snprintf(buf + count, |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 911 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), |
| 912 | "%s ", src->name); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 913 | } |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 914 | mutex_unlock(&clocksource_mutex); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 915 | |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 916 | count += snprintf(buf + count, |
| 917 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n"); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 918 | |
Miao Xie | 5e2cb10 | 2008-02-06 01:36:53 -0800 | [diff] [blame] | 919 | return count; |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Sysfs setup bits: |
| 924 | */ |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 925 | static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources, |
Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 926 | sysfs_override_clocksource); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 927 | |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 928 | static DEVICE_ATTR(available_clocksource, 0444, |
Daniel Walker | f5f1a24 | 2006-12-10 02:21:33 -0800 | [diff] [blame] | 929 | sysfs_show_available_clocksources, NULL); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 930 | |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 931 | static struct bus_type clocksource_subsys = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 932 | .name = "clocksource", |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 933 | .dev_name = "clocksource", |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 934 | }; |
| 935 | |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 936 | static struct device device_clocksource = { |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 937 | .id = 0, |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 938 | .bus = &clocksource_subsys, |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 939 | }; |
| 940 | |
john stultz | ad59617 | 2006-06-26 00:25:06 -0700 | [diff] [blame] | 941 | static int __init init_clocksource_sysfs(void) |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 942 | { |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 943 | int error = subsys_system_register(&clocksource_subsys, NULL); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 944 | |
| 945 | if (!error) |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 946 | error = device_register(&device_clocksource); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 947 | if (!error) |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 948 | error = device_create_file( |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 949 | &device_clocksource, |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 950 | &dev_attr_current_clocksource); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 951 | if (!error) |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 952 | error = device_create_file( |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 953 | &device_clocksource, |
Kay Sievers | d369a5d | 2011-12-14 15:28:51 -0800 | [diff] [blame] | 954 | &dev_attr_available_clocksource); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 955 | return error; |
| 956 | } |
| 957 | |
| 958 | device_initcall(init_clocksource_sysfs); |
Daniel Walker | 2b01370 | 2006-12-10 02:21:30 -0800 | [diff] [blame] | 959 | #endif /* CONFIG_SYSFS */ |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 960 | |
| 961 | /** |
| 962 | * boot_override_clocksource - boot clock override |
| 963 | * @str: override name |
| 964 | * |
| 965 | * Takes a clocksource= boot argument and uses it |
| 966 | * as the clocksource override name. |
| 967 | */ |
| 968 | static int __init boot_override_clocksource(char* str) |
| 969 | { |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 970 | mutex_lock(&clocksource_mutex); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 971 | if (str) |
| 972 | strlcpy(override_name, str, sizeof(override_name)); |
Martin Schwidefsky | 75c5158 | 2009-08-14 15:47:30 +0200 | [diff] [blame] | 973 | mutex_unlock(&clocksource_mutex); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | __setup("clocksource=", boot_override_clocksource); |
| 978 | |
| 979 | /** |
| 980 | * boot_override_clock - Compatibility layer for deprecated boot option |
| 981 | * @str: override name |
| 982 | * |
| 983 | * DEPRECATED! Takes a clock= boot argument and uses it |
| 984 | * as the clocksource override name |
| 985 | */ |
| 986 | static int __init boot_override_clock(char* str) |
| 987 | { |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 988 | if (!strcmp(str, "pmtmr")) { |
| 989 | printk("Warning: clock=pmtmr is deprecated. " |
| 990 | "Use clocksource=acpi_pm.\n"); |
| 991 | return boot_override_clocksource("acpi_pm"); |
| 992 | } |
| 993 | printk("Warning! clock= boot option is deprecated. " |
| 994 | "Use clocksource=xyz\n"); |
john stultz | 734efb4 | 2006-06-26 00:25:05 -0700 | [diff] [blame] | 995 | return boot_override_clocksource(str); |
| 996 | } |
| 997 | |
| 998 | __setup("clock=", boot_override_clock); |