blob: c4cc04bec69831e8a09aa118df61390a078bff3c [file] [log] [blame]
john stultz734efb42006-06-26 00:25:05 -07001/*
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 stultz734efb42006-06-26 00:25:05 -070024 */
25
Kay Sieversd369a5d2011-12-14 15:28:51 -080026#include <linux/device.h>
john stultz734efb42006-06-26 00:25:05 -070027#include <linux/clocksource.h>
john stultz734efb42006-06-26 00:25:05 -070028#include <linux/init.h>
29#include <linux/module.h>
Mathieu Desnoyersdc29a362007-02-10 01:43:43 -080030#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080031#include <linux/tick.h>
Martin Schwidefsky01548f42009-08-18 17:09:42 +020032#include <linux/kthread.h>
john stultz734efb42006-06-26 00:25:05 -070033
Thomas Gleixner03e13cf2013-04-25 20:31:50 +000034#include "tick-internal.h"
Thomas Gleixner3a978372014-07-16 21:05:10 +000035#include "timekeeping_internal.h"
Thomas Gleixner03e13cf2013-04-25 20:31:50 +000036
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000037/**
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 Pitre5fdade92011-01-11 12:18:12 -050043 * @maxsec: guaranteed runtime conversion range in seconds
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000044 *
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 Pitre5fdade92011-01-11 12:18:12 -050052 * The @maxsec conversion range argument controls the time frame in
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000053 * 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 */
60void
Nicolas Pitre5fdade92011-01-11 12:18:12 -050061clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000062{
63 u64 tmp;
64 u32 sft, sftacc= 32;
65
66 /*
67 * Calculate the shift factor which is limiting the conversion
68 * range:
69 */
Nicolas Pitre5fdade92011-01-11 12:18:12 -050070 tmp = ((u64)maxsec * from) >> 32;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000071 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 stultzb5776c42010-12-16 19:03:27 +000082 tmp += from / 2;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000083 do_div(tmp, from);
84 if ((tmp >> sftacc) == 0)
85 break;
86 }
87 *mult = tmp;
88 *shift = sft;
89}
90
john stultz734efb42006-06-26 00:25:05 -070091/*[Clocksource internal variables]---------
92 * curr_clocksource:
Martin Schwidefskyf1b82742009-08-14 15:47:21 +020093 * currently selected clocksource.
john stultz734efb42006-06-26 00:25:05 -070094 * clocksource_list:
95 * linked list with the registered clocksources
Martin Schwidefsky75c51582009-08-14 15:47:30 +020096 * clocksource_mutex:
97 * protects manipulations to curr_clocksource and the clocksource_list
john stultz734efb42006-06-26 00:25:05 -070098 * override_name:
99 * Name of the user-specified clocksource.
100 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200101static struct clocksource *curr_clocksource;
john stultz734efb42006-06-26 00:25:05 -0700102static LIST_HEAD(clocksource_list);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200103static DEFINE_MUTEX(clocksource_mutex);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000104static char override_name[CS_NAME_LEN];
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200105static int finished_booting;
john stultz734efb42006-06-26 00:25:05 -0700106
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800107#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200108static void clocksource_watchdog_work(struct work_struct *work);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200109static void clocksource_select(void);
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200110
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800111static LIST_HEAD(watchdog_list);
112static struct clocksource *watchdog;
113static struct timer_list watchdog_timer;
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200114static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800115static DEFINE_SPINLOCK(watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200116static int watchdog_running;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200117static atomic_t watchdog_reset_pending;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700118
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200119static int clocksource_watchdog_kthread(void *data);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200120static void __clocksource_change_rating(struct clocksource *cs, int rating);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200121
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800122/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700123 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800124 */
125#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700126#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800127
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200128static 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 Gleixner7285dd72009-08-28 20:25:24 +0200137static 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 Gleixner54a6bc02009-09-14 19:49:02 +0200141 if (finished_booting)
142 schedule_work(&watchdog_work);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200143}
144
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200145/**
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 */
154void 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 Gleixner5d8b34f2007-02-16 01:27:43 -0800165}
166
167static void clocksource_watchdog(unsigned long data)
168{
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200169 struct clocksource *cs;
John Stultz0b046b22015-03-11 21:16:36 -0700170 cycle_t csnow, wdnow, cslast, wdlast, delta;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800171 int64_t wd_nsec, cs_nsec;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200172 int next_cpu, reset_pending;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800173
174 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200175 if (!watchdog_running)
176 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800177
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200178 reset_pending = atomic_read(&watchdog_reset_pending);
179
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200180 list_for_each_entry(cs, &watchdog_list, wd_list) {
181
182 /* Clocksource already marked unstable? */
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200183 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200184 if (finished_booting)
185 schedule_work(&watchdog_work);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200186 continue;
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200187 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200188
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200189 local_irq_disable();
Magnus Damm8e196082009-04-21 12:24:00 -0700190 csnow = cs->read(cs);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200191 wdnow = watchdog->read(watchdog);
192 local_irq_enable();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700193
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200194 /* Clocksource initialized ? */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200195 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
196 atomic_read(&watchdog_reset_pending)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200197 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200198 cs->wd_last = wdnow;
199 cs->cs_last = csnow;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700200 continue;
201 }
202
Thomas Gleixner3a978372014-07-16 21:05:10 +0000203 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
204 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
205 watchdog->shift);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200206
Thomas Gleixner3a978372014-07-16 21:05:10 +0000207 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
208 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
John Stultz0b046b22015-03-11 21:16:36 -0700209 wdlast = cs->wd_last; /* save these in case we print them */
210 cslast = cs->cs_last;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200211 cs->cs_last = csnow;
212 cs->wd_last = wdnow;
213
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200214 if (atomic_read(&watchdog_reset_pending))
215 continue;
216
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200217 /* Check the deviation from the watchdog clocksource. */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200218 if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
John Stultz0b046b22015-03-11 21:16:36 -0700219 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 Schwidefsky8cf4e752009-08-14 15:47:22 +0200225 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 Gleixner332962f2013-07-04 22:46:45 +0200231 /* Mark it valid for high-res. */
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200232 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner332962f2013-07-04 22:46:45 +0200233
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200234 /*
Thomas Gleixner332962f2013-07-04 22:46:45 +0200235 * clocksource_done_booting() will sort it if
236 * finished_booting is not set yet.
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200237 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200238 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 Gleixner5d8b34f2007-02-16 01:27:43 -0800255 }
256 }
257
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200258 /*
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200259 * 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 Schwidefskyc55c87c2009-08-14 15:47:25 +0200266 * 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 Schwidefskyfb63a0e2009-08-14 15:47:24 +0200274out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800275 spin_unlock(&watchdog_lock);
276}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200277
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200278static 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 Schwidefskyfb63a0e2009-08-14 15:47:24 +0200284 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
285 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
286 watchdog_running = 1;
287}
288
289static 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 Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200297static 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 Gleixnerb52f52a2007-05-09 02:35:15 -0700305static void clocksource_resume_watchdog(void)
306{
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200307 atomic_inc(&watchdog_reset_pending);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700308}
309
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200310static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800311{
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800312 unsigned long flags;
313
314 spin_lock_irqsave(&watchdog_lock, flags);
315 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200316 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800317 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200318 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200319 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200320 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200321 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800322 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200323 /* Pick the best watchdog. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800324 if (!watchdog || cs->rating > watchdog->rating) {
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800325 watchdog = cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800326 /* Reset watchdog cycles */
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200327 clocksource_reset_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800328 }
329 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200330 /* Check if the watchdog timer needs to be started. */
331 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800332 spin_unlock_irqrestore(&watchdog_lock, flags);
333}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200334
335static void clocksource_dequeue_watchdog(struct clocksource *cs)
336{
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200337 unsigned long flags;
338
339 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000340 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 Schwidefskyfb63a0e2009-08-14 15:47:24 +0200346 }
347 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200348 spin_unlock_irqrestore(&watchdog_lock, flags);
349}
350
Thomas Gleixner332962f2013-07-04 22:46:45 +0200351static int __clocksource_watchdog_kthread(void)
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200352{
353 struct clocksource *cs, *tmp;
354 unsigned long flags;
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200355 LIST_HEAD(unstable);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200356 int select = 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200357
358 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200359 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200360 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
361 list_del_init(&cs->wd_list);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200362 list_add(&cs->wd_list, &unstable);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200363 select = 1;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200364 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200365 if (cs->flags & CLOCK_SOURCE_RESELECT) {
366 cs->flags &= ~CLOCK_SOURCE_RESELECT;
367 select = 1;
368 }
369 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200370 /* Check if the watchdog timer needs to be stopped. */
371 clocksource_stop_watchdog();
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200372 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 Gleixnerd0981a12009-08-19 11:26:09 +0200377 __clocksource_change_rating(cs, 0);
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200378 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200379 return select;
380}
381
382static int clocksource_watchdog_kthread(void *data)
383{
384 mutex_lock(&clocksource_mutex);
385 if (__clocksource_watchdog_kthread())
386 clocksource_select();
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200387 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200388 return 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200389}
390
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000391static bool clocksource_is_watchdog(struct clocksource *cs)
392{
393 return cs == watchdog;
394}
395
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200396#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
397
398static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800399{
400 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
401 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
402}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700403
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200404static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700405static inline void clocksource_resume_watchdog(void) { }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200406static inline int __clocksource_watchdog_kthread(void) { return 0; }
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000407static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
Prarit Bhargava397bbf62013-02-22 15:08:56 -0500408void clocksource_mark_unstable(struct clocksource *cs) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200409
410#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800411
john stultz734efb42006-06-26 00:25:05 -0700412/**
Magnus Dammc54a42b2010-02-02 14:41:41 -0800413 * clocksource_suspend - suspend the clocksource(s)
414 */
415void 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 Gleixnerb52f52a2007-05-09 02:35:15 -0700425 * clocksource_resume - resume the clocksource(s)
426 */
427void clocksource_resume(void)
428{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700429 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700430
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200431 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700432 if (cs->resume)
Magnus Damm17622332010-02-02 14:41:39 -0800433 cs->resume(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700434
435 clocksource_resume_watchdog();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700436}
437
438/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600439 * clocksource_touch_watchdog - Update watchdog
440 *
441 * Update the watchdog after exception contexts such as kgdb so as not
Thomas Gleixner7b7422a2010-01-26 12:51:10 +0100442 * to incorrectly trip the watchdog. This might fail when the kernel
443 * was stopped in code which holds watchdog_lock.
Jason Wessel7c3078b2008-02-15 14:55:54 -0600444 */
445void clocksource_touch_watchdog(void)
446{
447 clocksource_resume_watchdog();
448}
449
john stultz734efb42006-06-26 00:25:05 -0700450/**
John Stultzd65670a2011-10-31 17:06:35 -0400451 * clocksource_max_adjustment- Returns max adjustment amount
452 * @cs: Pointer to clocksource
453 *
454 */
455static u32 clocksource_max_adjustment(struct clocksource *cs)
456{
457 u64 ret;
458 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600459 * We won't try to correct for more than 11% adjustments (110,000 ppm),
John Stultzd65670a2011-10-31 17:06:35 -0400460 */
461 ret = (u64)cs->mult * 11;
462 do_div(ret,100);
463 return (u32)ret;
464}
465
466/**
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700467 * 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 Stultzfb82fe22015-03-11 21:16:31 -0700472 * @max_cyc: maximum cycle value before potential overflow (does not include
473 * any safety margin)
John Stultz362fde02015-03-11 21:16:30 -0700474 *
475 * NOTE: This function includes a safety margin of 50%, so that bad clock values
476 * can be detected.
Jon Hunter98962462009-08-18 12:45:10 -0500477 */
John Stultzfb82fe22015-03-11 21:16:31 -0700478u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
Jon Hunter98962462009-08-18 12:45:10 -0500479{
480 u64 max_nsecs, max_cycles;
481
482 /*
483 * Calculate the maximum number of cycles that we can pass to the
John Stultz6086e342015-03-11 21:16:29 -0700484 * cyc2ns() function without overflowing a 64-bit result.
Jon Hunter98962462009-08-18 12:45:10 -0500485 */
John Stultz6086e342015-03-11 21:16:29 -0700486 max_cycles = ULLONG_MAX;
487 do_div(max_cycles, mult+maxadj);
Jon Hunter98962462009-08-18 12:45:10 -0500488
489 /*
490 * The actual maximum number of cycles we can defer the clocksource is
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700491 * determined by the minimum of max_cycles and mask.
John Stultzd65670a2011-10-31 17:06:35 -0400492 * 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 Hunter98962462009-08-18 12:45:10 -0500494 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700495 max_cycles = min(max_cycles, mask);
496 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
Jon Hunter98962462009-08-18 12:45:10 -0500497
John Stultzfb82fe22015-03-11 21:16:31 -0700498 /* return the max_cycles value as well if requested */
499 if (max_cyc)
500 *max_cyc = max_cycles;
501
John Stultz362fde02015-03-11 21:16:30 -0700502 /* Return 50% of the actual maximum, so we can detect bad values */
503 max_nsecs >>= 1;
504
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700505 return max_nsecs;
506}
507
508/**
John Stultzfb82fe22015-03-11 21:16:31 -0700509 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
510 * @cs: Pointer to clocksource to be updated
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700511 *
512 */
John Stultzfb82fe22015-03-11 21:16:31 -0700513static inline void clocksource_update_max_deferment(struct clocksource *cs)
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700514{
John Stultzfb82fe22015-03-11 21:16:31 -0700515 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
516 cs->maxadj, cs->mask,
517 &cs->max_cycles);
Jon Hunter98962462009-08-18 12:45:10 -0500518}
519
John Stultz592913e2010-07-13 17:56:20 -0700520#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
john stultz734efb42006-06-26 00:25:05 -0700521
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000522static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000523{
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 Gleixnerf5a2e342013-04-25 20:31:45 +0000535 if (skipcur && cs == curr_clocksource)
536 continue;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000537 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
538 continue;
539 return cs;
540 }
541 return NULL;
542}
543
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000544static void __clocksource_select(bool skipcur)
john stultz734efb42006-06-26 00:25:05 -0700545{
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000546 bool oneshot = tick_oneshot_mode_active();
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200547 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800548
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000549 /* Find the best suitable clocksource */
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000550 best = clocksource_find_best(oneshot, skipcur);
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000551 if (!best)
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200552 return;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000553
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200554 /* Check for the override clocksource. */
555 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000556 if (skipcur && cs == curr_clocksource)
557 continue;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200558 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 Gleixner5d33b882013-04-25 20:31:43 +0000565 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200566 /* 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 Gleixnerba919d12013-04-25 20:31:44 +0000576
577 if (curr_clocksource != best && !timekeeping_notify(best)) {
578 pr_info("Switched to clocksource %s\n", best->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200579 curr_clocksource = best;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200580 }
john stultz734efb42006-06-26 00:25:05 -0700581}
582
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000583/**
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 */
591static void clocksource_select(void)
592{
593 return __clocksource_select(false);
594}
595
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000596static void clocksource_select_fallback(void)
597{
598 return __clocksource_select(true);
599}
600
John Stultz592913e2010-07-13 17:56:20 -0700601#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200602
603static inline void clocksource_select(void) { }
Thomas Gleixner1eaff672013-05-28 09:48:46 +0200604static inline void clocksource_select_fallback(void) { }
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200605
606#endif
607
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200608/*
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 */
615static int __init clocksource_done_booting(void)
616{
john stultzad6759f2010-03-01 12:34:43 -0800617 mutex_lock(&clocksource_mutex);
618 curr_clocksource = clocksource_default_clock();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200619 finished_booting = 1;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200620 /*
621 * Run the watchdog first to eliminate unstable clock sources
622 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200623 __clocksource_watchdog_kthread();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200624 clocksource_select();
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200625 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200626 return 0;
627}
628fs_initcall(clocksource_done_booting);
629
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800630/*
631 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700632 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200633static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700634{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200635 struct list_head *entry = &clocksource_list;
636 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700637
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200638 list_for_each_entry(tmp, &clocksource_list, list)
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800639 /* Keep track of the place, where to insert */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200640 if (tmp->rating >= cs->rating)
641 entry = &tmp->list;
642 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700643}
644
John Stultzd7e81c22010-05-07 18:07:38 -0700645/**
John Stultz852db462010-07-13 17:56:28 -0700646 * __clocksource_updatefreq_scale - Used update clocksource with new freq
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900647 * @cs: clocksource to be registered
John Stultz852db462010-07-13 17:56:28 -0700648 * @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
654 * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
655 */
656void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
657{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200658 u64 sec;
John Stultz852db462010-07-13 17:56:28 -0700659 /*
Thomas Gleixner724ed532011-05-18 21:33:40 +0000660 * Calc the maximum number of seconds which we can run before
661 * wrapping around. For clocksources which have a mask > 32bit
662 * we need to limit the max sleep time to have a good
663 * conversion precision. 10 minutes is still a reasonable
664 * amount. That results in a shift value of 24 for a
665 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
John Stultz362fde02015-03-11 21:16:30 -0700666 * ~ 0.06ppm granularity for NTP.
John Stultz852db462010-07-13 17:56:28 -0700667 */
John Stultz362fde02015-03-11 21:16:30 -0700668 sec = cs->mask;
Thomas Gleixner724ed532011-05-18 21:33:40 +0000669 do_div(sec, freq);
670 do_div(sec, scale);
671 if (!sec)
672 sec = 1;
673 else if (sec > 600 && cs->mask > UINT_MAX)
674 sec = 600;
675
John Stultz852db462010-07-13 17:56:28 -0700676 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
Thomas Gleixner724ed532011-05-18 21:33:40 +0000677 NSEC_PER_SEC / scale, sec * scale);
John Stultzd65670a2011-10-31 17:06:35 -0400678
679 /*
John Stultz362fde02015-03-11 21:16:30 -0700680 * Ensure clocksources that have large 'mult' values don't overflow
681 * when adjusted.
John Stultzd65670a2011-10-31 17:06:35 -0400682 */
683 cs->maxadj = clocksource_max_adjustment(cs);
684 while ((cs->mult + cs->maxadj < cs->mult)
685 || (cs->mult - cs->maxadj > cs->mult)) {
686 cs->mult >>= 1;
687 cs->shift--;
688 cs->maxadj = clocksource_max_adjustment(cs);
689 }
690
John Stultzfb82fe22015-03-11 21:16:31 -0700691 clocksource_update_max_deferment(cs);
John Stultz852db462010-07-13 17:56:28 -0700692}
693EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
694
695/**
John Stultzd7e81c22010-05-07 18:07:38 -0700696 * __clocksource_register_scale - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900697 * @cs: clocksource to be registered
John Stultzd7e81c22010-05-07 18:07:38 -0700698 * @scale: Scale factor multiplied against freq to get clocksource hz
699 * @freq: clocksource frequency (cycles per second) divided by scale
700 *
701 * Returns -EBUSY if registration fails, zero otherwise.
702 *
703 * This *SHOULD NOT* be called directly! Please use the
704 * clocksource_register_hz() or clocksource_register_khz helper functions.
705 */
706int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
707{
708
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400709 /* Initialize mult/shift and max_idle_ns */
John Stultz852db462010-07-13 17:56:28 -0700710 __clocksource_updatefreq_scale(cs, scale, freq);
John Stultzd7e81c22010-05-07 18:07:38 -0700711
James Hartleybe278e92014-09-18 15:59:07 +0100712 /* Add clocksource to the clocksource list */
John Stultzd7e81c22010-05-07 18:07:38 -0700713 mutex_lock(&clocksource_mutex);
714 clocksource_enqueue(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700715 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700716 clocksource_select();
John Stultzd7e81c22010-05-07 18:07:38 -0700717 mutex_unlock(&clocksource_mutex);
718 return 0;
719}
720EXPORT_SYMBOL_GPL(__clocksource_register_scale);
721
722
john stultz734efb42006-06-26 00:25:05 -0700723/**
john stultza2752542006-06-26 00:25:14 -0700724 * clocksource_register - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900725 * @cs: clocksource to be registered
john stultz734efb42006-06-26 00:25:05 -0700726 *
727 * Returns -EBUSY if registration fails, zero otherwise.
728 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200729int clocksource_register(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700730{
John Stultzd65670a2011-10-31 17:06:35 -0400731 /* calculate max adjustment for given mult/shift */
732 cs->maxadj = clocksource_max_adjustment(cs);
733 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
734 "Clocksource %s might overflow on 11%% adjustment\n",
735 cs->name);
736
John Stultzfb82fe22015-03-11 21:16:31 -0700737 /* Update max idle time permitted for this clocksource */
738 clocksource_update_max_deferment(cs);
Jon Hunter98962462009-08-18 12:45:10 -0500739
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200740 mutex_lock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200741 clocksource_enqueue(cs);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200742 clocksource_enqueue_watchdog(cs);
john stultze05b2ef2011-05-04 18:16:50 -0700743 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200744 mutex_unlock(&clocksource_mutex);
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200745 return 0;
john stultz734efb42006-06-26 00:25:05 -0700746}
john stultza2752542006-06-26 00:25:14 -0700747EXPORT_SYMBOL(clocksource_register);
john stultz734efb42006-06-26 00:25:05 -0700748
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200749static void __clocksource_change_rating(struct clocksource *cs, int rating)
750{
751 list_del(&cs->list);
752 cs->rating = rating;
753 clocksource_enqueue(cs);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200754}
755
john stultz734efb42006-06-26 00:25:05 -0700756/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800757 * clocksource_change_rating - Change the rating of a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900758 * @cs: clocksource to be changed
759 * @rating: new rating
john stultz734efb42006-06-26 00:25:05 -0700760 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800761void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700762{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200763 mutex_lock(&clocksource_mutex);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200764 __clocksource_change_rating(cs, rating);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200765 clocksource_select();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200766 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700767}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200768EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700769
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000770/*
771 * Unbind clocksource @cs. Called with clocksource_mutex held
772 */
773static int clocksource_unbind(struct clocksource *cs)
774{
775 /*
776 * I really can't convince myself to support this on hardware
777 * designed by lobotomized monkeys.
778 */
779 if (clocksource_is_watchdog(cs))
780 return -EBUSY;
781
782 if (cs == curr_clocksource) {
783 /* Select and try to install a replacement clock source */
784 clocksource_select_fallback();
785 if (curr_clocksource == cs)
786 return -EBUSY;
787 }
788 clocksource_dequeue_watchdog(cs);
789 list_del_init(&cs->list);
790 return 0;
791}
792
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100793/**
794 * clocksource_unregister - remove a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900795 * @cs: clocksource to be unregistered
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100796 */
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000797int clocksource_unregister(struct clocksource *cs)
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100798{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000799 int ret = 0;
800
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200801 mutex_lock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000802 if (!list_empty(&cs->list))
803 ret = clocksource_unbind(cs);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200804 mutex_unlock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000805 return ret;
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100806}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200807EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100808
Daniel Walker2b013702006-12-10 02:21:30 -0800809#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700810/**
811 * sysfs_show_current_clocksources - sysfs interface for current clocksource
812 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900813 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700814 * @buf: char buffer to be filled with clocksource list
815 *
816 * Provides sysfs interface for listing current clocksource.
817 */
818static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800819sysfs_show_current_clocksources(struct device *dev,
820 struct device_attribute *attr, char *buf)
john stultz734efb42006-06-26 00:25:05 -0700821{
Miao Xie5e2cb102008-02-06 01:36:53 -0800822 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700823
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200824 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -0800825 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200826 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700827
Miao Xie5e2cb102008-02-06 01:36:53 -0800828 return count;
john stultz734efb42006-06-26 00:25:05 -0700829}
830
Patrick Palka891292a2013-10-11 13:11:55 -0400831ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
Thomas Gleixner29b54072013-04-25 20:31:45 +0000832{
833 size_t ret = cnt;
834
835 /* strings from sysfs write are not 0 terminated! */
836 if (!cnt || cnt >= CS_NAME_LEN)
837 return -EINVAL;
838
839 /* strip of \n: */
840 if (buf[cnt-1] == '\n')
841 cnt--;
842 if (cnt > 0)
843 memcpy(dst, buf, cnt);
844 dst[cnt] = 0;
845 return ret;
846}
847
john stultz734efb42006-06-26 00:25:05 -0700848/**
849 * sysfs_override_clocksource - interface for manually overriding clocksource
850 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900851 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700852 * @buf: name of override clocksource
853 * @count: length of buffer
854 *
855 * Takes input from sysfs interface for manually overriding the default
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200856 * clocksource selection.
john stultz734efb42006-06-26 00:25:05 -0700857 */
Kay Sieversd369a5d2011-12-14 15:28:51 -0800858static ssize_t sysfs_override_clocksource(struct device *dev,
859 struct device_attribute *attr,
john stultz734efb42006-06-26 00:25:05 -0700860 const char *buf, size_t count)
861{
Elad Wexler233bcb42013-09-12 13:28:54 +0300862 ssize_t ret;
john stultz734efb42006-06-26 00:25:05 -0700863
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200864 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700865
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000866 ret = sysfs_get_uname(buf, override_name, count);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000867 if (ret >= 0)
868 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700869
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200870 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700871
872 return ret;
873}
874
875/**
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000876 * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
877 * @dev: unused
878 * @attr: unused
879 * @buf: unused
880 * @count: length of buffer
881 *
882 * Takes input from sysfs interface for manually unbinding a clocksource.
883 */
884static ssize_t sysfs_unbind_clocksource(struct device *dev,
885 struct device_attribute *attr,
886 const char *buf, size_t count)
887{
888 struct clocksource *cs;
889 char name[CS_NAME_LEN];
Elad Wexler233bcb42013-09-12 13:28:54 +0300890 ssize_t ret;
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000891
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000892 ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000893 if (ret < 0)
894 return ret;
895
896 ret = -ENODEV;
897 mutex_lock(&clocksource_mutex);
898 list_for_each_entry(cs, &clocksource_list, list) {
899 if (strcmp(cs->name, name))
900 continue;
901 ret = clocksource_unbind(cs);
902 break;
903 }
904 mutex_unlock(&clocksource_mutex);
905
906 return ret ? ret : count;
907}
908
909/**
john stultz734efb42006-06-26 00:25:05 -0700910 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
911 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900912 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700913 * @buf: char buffer to be filled with clocksource list
914 *
915 * Provides sysfs interface for listing registered clocksources
916 */
917static ssize_t
Kay Sieversd369a5d2011-12-14 15:28:51 -0800918sysfs_show_available_clocksources(struct device *dev,
919 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200920 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700921{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700922 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -0800923 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700924
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200925 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700926 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +0200927 /*
928 * Don't show non-HRES clocksource if the tick code is
929 * in one shot mode (highres=on or nohz=on)
930 */
931 if (!tick_oneshot_mode_active() ||
932 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -0700933 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -0800934 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
935 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -0700936 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200937 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700938
Miao Xie5e2cb102008-02-06 01:36:53 -0800939 count += snprintf(buf + count,
940 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -0700941
Miao Xie5e2cb102008-02-06 01:36:53 -0800942 return count;
john stultz734efb42006-06-26 00:25:05 -0700943}
944
945/*
946 * Sysfs setup bits:
947 */
Kay Sieversd369a5d2011-12-14 15:28:51 -0800948static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800949 sysfs_override_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700950
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000951static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
952
Kay Sieversd369a5d2011-12-14 15:28:51 -0800953static DEVICE_ATTR(available_clocksource, 0444,
Daniel Walkerf5f1a242006-12-10 02:21:33 -0800954 sysfs_show_available_clocksources, NULL);
john stultz734efb42006-06-26 00:25:05 -0700955
Kay Sieversd369a5d2011-12-14 15:28:51 -0800956static struct bus_type clocksource_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +0100957 .name = "clocksource",
Kay Sieversd369a5d2011-12-14 15:28:51 -0800958 .dev_name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -0700959};
960
Kay Sieversd369a5d2011-12-14 15:28:51 -0800961static struct device device_clocksource = {
john stultz734efb42006-06-26 00:25:05 -0700962 .id = 0,
Kay Sieversd369a5d2011-12-14 15:28:51 -0800963 .bus = &clocksource_subsys,
john stultz734efb42006-06-26 00:25:05 -0700964};
965
john stultzad596172006-06-26 00:25:06 -0700966static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -0700967{
Kay Sieversd369a5d2011-12-14 15:28:51 -0800968 int error = subsys_system_register(&clocksource_subsys, NULL);
john stultz734efb42006-06-26 00:25:05 -0700969
970 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -0800971 error = device_register(&device_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700972 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -0800973 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -0700974 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -0800975 &dev_attr_current_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700976 if (!error)
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000977 error = device_create_file(&device_clocksource,
978 &dev_attr_unbind_clocksource);
979 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -0800980 error = device_create_file(
john stultz734efb42006-06-26 00:25:05 -0700981 &device_clocksource,
Kay Sieversd369a5d2011-12-14 15:28:51 -0800982 &dev_attr_available_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700983 return error;
984}
985
986device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -0800987#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -0700988
989/**
990 * boot_override_clocksource - boot clock override
991 * @str: override name
992 *
993 * Takes a clocksource= boot argument and uses it
994 * as the clocksource override name.
995 */
996static int __init boot_override_clocksource(char* str)
997{
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200998 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700999 if (str)
1000 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001001 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001002 return 1;
1003}
1004
1005__setup("clocksource=", boot_override_clocksource);
1006
1007/**
1008 * boot_override_clock - Compatibility layer for deprecated boot option
1009 * @str: override name
1010 *
1011 * DEPRECATED! Takes a clock= boot argument and uses it
1012 * as the clocksource override name
1013 */
1014static int __init boot_override_clock(char* str)
1015{
john stultz5d0cf412006-06-26 00:25:12 -07001016 if (!strcmp(str, "pmtmr")) {
1017 printk("Warning: clock=pmtmr is deprecated. "
1018 "Use clocksource=acpi_pm.\n");
1019 return boot_override_clocksource("acpi_pm");
1020 }
1021 printk("Warning! clock= boot option is deprecated. "
1022 "Use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -07001023 return boot_override_clocksource(str);
1024}
1025
1026__setup("clock=", boot_override_clock);