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