blob: 84f37420fcf544c09e12f730f05a8a85411b36f1 [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
Joe Perches45bbfe62015-05-25 11:49:55 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Kay Sieversd369a5d2011-12-14 15:28:51 -080028#include <linux/device.h>
john stultz734efb42006-06-26 00:25:05 -070029#include <linux/clocksource.h>
john stultz734efb42006-06-26 00:25:05 -070030#include <linux/init.h>
31#include <linux/module.h>
Mathieu Desnoyersdc29a362007-02-10 01:43:43 -080032#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080033#include <linux/tick.h>
Martin Schwidefsky01548f42009-08-18 17:09:42 +020034#include <linux/kthread.h>
john stultz734efb42006-06-26 00:25:05 -070035
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010036#include "tick-internal.h"
Thomas Gleixner3a978372014-07-16 21:05:10 +000037#include "timekeeping_internal.h"
Thomas Gleixner03e13cf2013-04-25 20:31:50 +000038
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000039/**
40 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
41 * @mult: pointer to mult variable
42 * @shift: pointer to shift variable
43 * @from: frequency to convert from
44 * @to: frequency to convert to
Nicolas Pitre5fdade92011-01-11 12:18:12 -050045 * @maxsec: guaranteed runtime conversion range in seconds
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000046 *
47 * The function evaluates the shift/mult pair for the scaled math
48 * operations of clocksources and clockevents.
49 *
50 * @to and @from are frequency values in HZ. For clock sources @to is
51 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
52 * event @to is the counter frequency and @from is NSEC_PER_SEC.
53 *
Nicolas Pitre5fdade92011-01-11 12:18:12 -050054 * The @maxsec conversion range argument controls the time frame in
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000055 * seconds which must be covered by the runtime conversion with the
56 * calculated mult and shift factors. This guarantees that no 64bit
57 * overflow happens when the input value of the conversion is
58 * multiplied with the calculated mult factor. Larger ranges may
59 * reduce the conversion accuracy by chosing smaller mult and shift
60 * factors.
61 */
62void
Nicolas Pitre5fdade92011-01-11 12:18:12 -050063clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000064{
65 u64 tmp;
66 u32 sft, sftacc= 32;
67
68 /*
69 * Calculate the shift factor which is limiting the conversion
70 * range:
71 */
Nicolas Pitre5fdade92011-01-11 12:18:12 -050072 tmp = ((u64)maxsec * from) >> 32;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000073 while (tmp) {
74 tmp >>=1;
75 sftacc--;
76 }
77
78 /*
79 * Find the conversion shift/mult pair which has the best
80 * accuracy and fits the maxsec conversion range:
81 */
82 for (sft = 32; sft > 0; sft--) {
83 tmp = (u64) to << sft;
john stultzb5776c42010-12-16 19:03:27 +000084 tmp += from / 2;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000085 do_div(tmp, from);
86 if ((tmp >> sftacc) == 0)
87 break;
88 }
89 *mult = tmp;
90 *shift = sft;
91}
Murali Karicheri53041212016-12-06 18:00:43 -060092EXPORT_SYMBOL_GPL(clocks_calc_mult_shift);
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000093
john stultz734efb42006-06-26 00:25:05 -070094/*[Clocksource internal variables]---------
95 * curr_clocksource:
Martin Schwidefskyf1b82742009-08-14 15:47:21 +020096 * currently selected clocksource.
john stultz734efb42006-06-26 00:25:05 -070097 * clocksource_list:
98 * linked list with the registered clocksources
Martin Schwidefsky75c51582009-08-14 15:47:30 +020099 * clocksource_mutex:
100 * protects manipulations to curr_clocksource and the clocksource_list
john stultz734efb42006-06-26 00:25:05 -0700101 * override_name:
102 * Name of the user-specified clocksource.
103 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200104static struct clocksource *curr_clocksource;
john stultz734efb42006-06-26 00:25:05 -0700105static LIST_HEAD(clocksource_list);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200106static DEFINE_MUTEX(clocksource_mutex);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000107static char override_name[CS_NAME_LEN];
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200108static int finished_booting;
john stultz734efb42006-06-26 00:25:05 -0700109
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800110#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200111static void clocksource_watchdog_work(struct work_struct *work);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200112static void clocksource_select(void);
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200113
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800114static LIST_HEAD(watchdog_list);
115static struct clocksource *watchdog;
116static struct timer_list watchdog_timer;
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200117static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800118static DEFINE_SPINLOCK(watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200119static int watchdog_running;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200120static atomic_t watchdog_reset_pending;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700121
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200122static void inline clocksource_watchdog_lock(unsigned long *flags)
123{
124 spin_lock_irqsave(&watchdog_lock, *flags);
125}
126
127static void inline clocksource_watchdog_unlock(unsigned long *flags)
128{
129 spin_unlock_irqrestore(&watchdog_lock, *flags);
130}
131
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200132static int clocksource_watchdog_kthread(void *data);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200133static void __clocksource_change_rating(struct clocksource *cs, int rating);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200134
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800135/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700136 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800137 */
138#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700139#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800140
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200141static void clocksource_watchdog_work(struct work_struct *work)
142{
143 /*
144 * If kthread_run fails the next watchdog scan over the
145 * watchdog_list will find the unstable clock again.
146 */
147 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
148}
149
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200150static void __clocksource_unstable(struct clocksource *cs)
151{
152 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
153 cs->flags |= CLOCK_SOURCE_UNSTABLE;
Thomas Gleixner12907fb2016-12-15 11:44:28 +0100154
Peter Zijlstracd2af07d2018-04-30 12:00:13 +0200155 /*
156 * If the clocksource is registered clocksource_watchdog_kthread() will
157 * re-rate and re-select.
158 */
159 if (list_empty(&cs->list)) {
160 cs->rating = 0;
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200161 return;
Peter Zijlstracd2af07d2018-04-30 12:00:13 +0200162 }
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200163
Thomas Gleixner12907fb2016-12-15 11:44:28 +0100164 if (cs->mark_unstable)
165 cs->mark_unstable(cs);
166
Peter Zijlstracd2af07d2018-04-30 12:00:13 +0200167 /* kick clocksource_watchdog_kthread() */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200168 if (finished_booting)
169 schedule_work(&watchdog_work);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200170}
171
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200172/**
173 * clocksource_mark_unstable - mark clocksource unstable via watchdog
174 * @cs: clocksource to be marked unstable
175 *
Peter Zijlstra7dba33c2018-04-30 12:00:14 +0200176 * This function is called by the x86 TSC code to mark clocksources as unstable;
177 * it defers demotion and re-selection to a kthread.
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200178 */
179void clocksource_mark_unstable(struct clocksource *cs)
180{
181 unsigned long flags;
182
183 spin_lock_irqsave(&watchdog_lock, flags);
184 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200185 if (!list_empty(&cs->list) && list_empty(&cs->wd_list))
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200186 list_add(&cs->wd_list, &watchdog_list);
187 __clocksource_unstable(cs);
188 }
189 spin_unlock_irqrestore(&watchdog_lock, flags);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800190}
191
Kees Cooke99e88a2017-10-16 14:43:17 -0700192static void clocksource_watchdog(struct timer_list *unused)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800193{
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200194 struct clocksource *cs;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100195 u64 csnow, wdnow, cslast, wdlast, delta;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800196 int64_t wd_nsec, cs_nsec;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200197 int next_cpu, reset_pending;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800198
199 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200200 if (!watchdog_running)
201 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800202
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200203 reset_pending = atomic_read(&watchdog_reset_pending);
204
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200205 list_for_each_entry(cs, &watchdog_list, wd_list) {
206
207 /* Clocksource already marked unstable? */
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200208 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200209 if (finished_booting)
210 schedule_work(&watchdog_work);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200211 continue;
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200212 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200213
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200214 local_irq_disable();
Magnus Damm8e196082009-04-21 12:24:00 -0700215 csnow = cs->read(cs);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200216 wdnow = watchdog->read(watchdog);
217 local_irq_enable();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700218
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200219 /* Clocksource initialized ? */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200220 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
221 atomic_read(&watchdog_reset_pending)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200222 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200223 cs->wd_last = wdnow;
224 cs->cs_last = csnow;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700225 continue;
226 }
227
Thomas Gleixner3a978372014-07-16 21:05:10 +0000228 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
229 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
230 watchdog->shift);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200231
Thomas Gleixner3a978372014-07-16 21:05:10 +0000232 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
233 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
John Stultz0b046b22015-03-11 21:16:36 -0700234 wdlast = cs->wd_last; /* save these in case we print them */
235 cslast = cs->cs_last;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200236 cs->cs_last = csnow;
237 cs->wd_last = wdnow;
238
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200239 if (atomic_read(&watchdog_reset_pending))
240 continue;
241
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200242 /* Check the deviation from the watchdog clocksource. */
Andrew Morton79211c82015-11-09 14:58:13 -0800243 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
Seiichi Ikarashi390dd672015-09-10 18:01:56 +0900244 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
245 smp_processor_id(), cs->name);
Joe Perches45bbfe62015-05-25 11:49:55 -0700246 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
John Stultz0b046b22015-03-11 21:16:36 -0700247 watchdog->name, wdnow, wdlast, watchdog->mask);
Joe Perches45bbfe62015-05-25 11:49:55 -0700248 pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
John Stultz0b046b22015-03-11 21:16:36 -0700249 cs->name, csnow, cslast, cs->mask);
250 __clocksource_unstable(cs);
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200251 continue;
252 }
253
Peter Zijlstrab421b222017-04-21 12:14:13 +0200254 if (cs == curr_clocksource && cs->tick_stable)
255 cs->tick_stable(cs);
256
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200257 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
258 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
259 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
Thomas Gleixner332962f2013-07-04 22:46:45 +0200260 /* Mark it valid for high-res. */
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200261 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner332962f2013-07-04 22:46:45 +0200262
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200263 /*
Thomas Gleixner332962f2013-07-04 22:46:45 +0200264 * clocksource_done_booting() will sort it if
265 * finished_booting is not set yet.
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200266 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200267 if (!finished_booting)
268 continue;
269
270 /*
271 * If this is not the current clocksource let
272 * the watchdog thread reselect it. Due to the
273 * change to high res this clocksource might
274 * be preferred now. If it is the current
275 * clocksource let the tick code know about
276 * that change.
277 */
278 if (cs != curr_clocksource) {
279 cs->flags |= CLOCK_SOURCE_RESELECT;
280 schedule_work(&watchdog_work);
281 } else {
282 tick_clock_notify();
283 }
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800284 }
285 }
286
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200287 /*
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200288 * We only clear the watchdog_reset_pending, when we did a
289 * full cycle through all clocksources.
290 */
291 if (reset_pending)
292 atomic_dec(&watchdog_reset_pending);
293
294 /*
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200295 * Cycle through CPUs to check if the CPUs stay synchronized
296 * to each other.
297 */
298 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
299 if (next_cpu >= nr_cpu_ids)
300 next_cpu = cpumask_first(cpu_online_mask);
301 watchdog_timer.expires += WATCHDOG_INTERVAL;
302 add_timer_on(&watchdog_timer, next_cpu);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200303out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800304 spin_unlock(&watchdog_lock);
305}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200306
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200307static inline void clocksource_start_watchdog(void)
308{
309 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
310 return;
Kees Cooke99e88a2017-10-16 14:43:17 -0700311 timer_setup(&watchdog_timer, clocksource_watchdog, 0);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200312 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
313 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
314 watchdog_running = 1;
315}
316
317static inline void clocksource_stop_watchdog(void)
318{
319 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
320 return;
321 del_timer(&watchdog_timer);
322 watchdog_running = 0;
323}
324
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200325static inline void clocksource_reset_watchdog(void)
326{
327 struct clocksource *cs;
328
329 list_for_each_entry(cs, &watchdog_list, wd_list)
330 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
331}
332
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700333static void clocksource_resume_watchdog(void)
334{
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200335 atomic_inc(&watchdog_reset_pending);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700336}
337
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200338static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800339{
Peter Zijlstra5b9e8862018-04-30 12:00:11 +0200340 INIT_LIST_HEAD(&cs->wd_list);
341
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800342 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200343 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800344 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200345 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200346 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200347 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200348 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800349 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800350 }
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100351}
352
353static void clocksource_select_watchdog(bool fallback)
354{
355 struct clocksource *cs, *old_wd;
356 unsigned long flags;
357
358 spin_lock_irqsave(&watchdog_lock, flags);
359 /* save current watchdog */
360 old_wd = watchdog;
361 if (fallback)
362 watchdog = NULL;
363
364 list_for_each_entry(cs, &clocksource_list, list) {
365 /* cs is a clocksource to be watched. */
366 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY)
367 continue;
368
369 /* Skip current if we were requested for a fallback. */
370 if (fallback && cs == old_wd)
371 continue;
372
373 /* Pick the best watchdog. */
374 if (!watchdog || cs->rating > watchdog->rating)
375 watchdog = cs;
376 }
377 /* If we failed to find a fallback restore the old one. */
378 if (!watchdog)
379 watchdog = old_wd;
380
381 /* If we changed the watchdog we need to reset cycles. */
382 if (watchdog != old_wd)
383 clocksource_reset_watchdog();
384
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200385 /* Check if the watchdog timer needs to be started. */
386 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800387 spin_unlock_irqrestore(&watchdog_lock, flags);
388}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200389
390static void clocksource_dequeue_watchdog(struct clocksource *cs)
391{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000392 if (cs != watchdog) {
393 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
394 /* cs is a watched clocksource. */
395 list_del_init(&cs->wd_list);
396 /* Check if the watchdog timer needs to be stopped. */
397 clocksource_stop_watchdog();
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200398 }
399 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200400}
401
Thomas Gleixner332962f2013-07-04 22:46:45 +0200402static int __clocksource_watchdog_kthread(void)
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200403{
404 struct clocksource *cs, *tmp;
405 unsigned long flags;
Thomas Gleixner332962f2013-07-04 22:46:45 +0200406 int select = 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200407
408 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200409 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200410 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
411 list_del_init(&cs->wd_list);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200412 __clocksource_change_rating(cs, 0);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200413 select = 1;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200414 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200415 if (cs->flags & CLOCK_SOURCE_RESELECT) {
416 cs->flags &= ~CLOCK_SOURCE_RESELECT;
417 select = 1;
418 }
419 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200420 /* Check if the watchdog timer needs to be stopped. */
421 clocksource_stop_watchdog();
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200422 spin_unlock_irqrestore(&watchdog_lock, flags);
423
Thomas Gleixner332962f2013-07-04 22:46:45 +0200424 return select;
425}
426
427static int clocksource_watchdog_kthread(void *data)
428{
429 mutex_lock(&clocksource_mutex);
430 if (__clocksource_watchdog_kthread())
431 clocksource_select();
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200432 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200433 return 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200434}
435
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000436static bool clocksource_is_watchdog(struct clocksource *cs)
437{
438 return cs == watchdog;
439}
440
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200441#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
442
443static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800444{
445 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
446 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
447}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700448
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100449static void clocksource_select_watchdog(bool fallback) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200450static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700451static inline void clocksource_resume_watchdog(void) { }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200452static inline int __clocksource_watchdog_kthread(void) { return 0; }
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000453static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
Prarit Bhargava397bbf62013-02-22 15:08:56 -0500454void clocksource_mark_unstable(struct clocksource *cs) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200455
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200456static void inline clocksource_watchdog_lock(unsigned long *flags) { }
457static void inline clocksource_watchdog_unlock(unsigned long *flags) { }
458
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200459#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800460
john stultz734efb42006-06-26 00:25:05 -0700461/**
Magnus Dammc54a42b2010-02-02 14:41:41 -0800462 * clocksource_suspend - suspend the clocksource(s)
463 */
464void clocksource_suspend(void)
465{
466 struct clocksource *cs;
467
468 list_for_each_entry_reverse(cs, &clocksource_list, list)
469 if (cs->suspend)
470 cs->suspend(cs);
471}
472
473/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700474 * clocksource_resume - resume the clocksource(s)
475 */
476void clocksource_resume(void)
477{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700478 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700479
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200480 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700481 if (cs->resume)
Magnus Damm17622332010-02-02 14:41:39 -0800482 cs->resume(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700483
484 clocksource_resume_watchdog();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700485}
486
487/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600488 * clocksource_touch_watchdog - Update watchdog
489 *
490 * Update the watchdog after exception contexts such as kgdb so as not
Thomas Gleixner7b7422a2010-01-26 12:51:10 +0100491 * to incorrectly trip the watchdog. This might fail when the kernel
492 * was stopped in code which holds watchdog_lock.
Jason Wessel7c3078b2008-02-15 14:55:54 -0600493 */
494void clocksource_touch_watchdog(void)
495{
496 clocksource_resume_watchdog();
497}
498
john stultz734efb42006-06-26 00:25:05 -0700499/**
John Stultzd65670a2011-10-31 17:06:35 -0400500 * clocksource_max_adjustment- Returns max adjustment amount
501 * @cs: Pointer to clocksource
502 *
503 */
504static u32 clocksource_max_adjustment(struct clocksource *cs)
505{
506 u64 ret;
507 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600508 * We won't try to correct for more than 11% adjustments (110,000 ppm),
John Stultzd65670a2011-10-31 17:06:35 -0400509 */
510 ret = (u64)cs->mult * 11;
511 do_div(ret,100);
512 return (u32)ret;
513}
514
515/**
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700516 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
517 * @mult: cycle to nanosecond multiplier
518 * @shift: cycle to nanosecond divisor (power of two)
519 * @maxadj: maximum adjustment value to mult (~11%)
520 * @mask: bitmask for two's complement subtraction of non 64 bit counters
John Stultzfb82fe22015-03-11 21:16:31 -0700521 * @max_cyc: maximum cycle value before potential overflow (does not include
522 * any safety margin)
John Stultz362fde02015-03-11 21:16:30 -0700523 *
John Stultz8e56f332015-04-01 20:34:39 -0700524 * NOTE: This function includes a safety margin of 50%, in other words, we
525 * return half the number of nanoseconds the hardware counter can technically
526 * cover. This is done so that we can potentially detect problems caused by
527 * delayed timers or bad hardware, which might result in time intervals that
Zhen Lei571af552015-08-25 14:42:53 +0800528 * are larger than what the math used can handle without overflows.
Jon Hunter98962462009-08-18 12:45:10 -0500529 */
John Stultzfb82fe22015-03-11 21:16:31 -0700530u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
Jon Hunter98962462009-08-18 12:45:10 -0500531{
532 u64 max_nsecs, max_cycles;
533
534 /*
535 * Calculate the maximum number of cycles that we can pass to the
John Stultz6086e342015-03-11 21:16:29 -0700536 * cyc2ns() function without overflowing a 64-bit result.
Jon Hunter98962462009-08-18 12:45:10 -0500537 */
John Stultz6086e342015-03-11 21:16:29 -0700538 max_cycles = ULLONG_MAX;
539 do_div(max_cycles, mult+maxadj);
Jon Hunter98962462009-08-18 12:45:10 -0500540
541 /*
542 * The actual maximum number of cycles we can defer the clocksource is
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700543 * determined by the minimum of max_cycles and mask.
John Stultzd65670a2011-10-31 17:06:35 -0400544 * Note: Here we subtract the maxadj to make sure we don't sleep for
545 * too long if there's a large negative adjustment.
Jon Hunter98962462009-08-18 12:45:10 -0500546 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700547 max_cycles = min(max_cycles, mask);
548 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
Jon Hunter98962462009-08-18 12:45:10 -0500549
John Stultzfb82fe22015-03-11 21:16:31 -0700550 /* return the max_cycles value as well if requested */
551 if (max_cyc)
552 *max_cyc = max_cycles;
553
John Stultz362fde02015-03-11 21:16:30 -0700554 /* Return 50% of the actual maximum, so we can detect bad values */
555 max_nsecs >>= 1;
556
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700557 return max_nsecs;
558}
559
560/**
John Stultzfb82fe22015-03-11 21:16:31 -0700561 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
562 * @cs: Pointer to clocksource to be updated
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700563 *
564 */
John Stultzfb82fe22015-03-11 21:16:31 -0700565static inline void clocksource_update_max_deferment(struct clocksource *cs)
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700566{
John Stultzfb82fe22015-03-11 21:16:31 -0700567 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
568 cs->maxadj, cs->mask,
569 &cs->max_cycles);
Jon Hunter98962462009-08-18 12:45:10 -0500570}
571
John Stultz592913e2010-07-13 17:56:20 -0700572#ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
john stultz734efb42006-06-26 00:25:05 -0700573
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000574static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000575{
576 struct clocksource *cs;
577
578 if (!finished_booting || list_empty(&clocksource_list))
579 return NULL;
580
581 /*
582 * We pick the clocksource with the highest rating. If oneshot
583 * mode is active, we pick the highres valid clocksource with
584 * the best rating.
585 */
586 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000587 if (skipcur && cs == curr_clocksource)
588 continue;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000589 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
590 continue;
591 return cs;
592 }
593 return NULL;
594}
595
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000596static void __clocksource_select(bool skipcur)
john stultz734efb42006-06-26 00:25:05 -0700597{
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000598 bool oneshot = tick_oneshot_mode_active();
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200599 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800600
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000601 /* Find the best suitable clocksource */
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000602 best = clocksource_find_best(oneshot, skipcur);
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000603 if (!best)
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200604 return;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000605
Baolin Wang7f852afe2018-01-17 14:01:28 +0800606 if (!strlen(override_name))
607 goto found;
608
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200609 /* Check for the override clocksource. */
610 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000611 if (skipcur && cs == curr_clocksource)
612 continue;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200613 if (strcmp(cs->name, override_name) != 0)
614 continue;
615 /*
616 * Check to make sure we don't switch to a non-highres
617 * capable clocksource if the tick code is in oneshot
618 * mode (highres or nohz)
619 */
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000620 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200621 /* Override clocksource cannot be used. */
Kyle Walker36374582016-08-06 12:07:30 -0400622 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
623 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
624 cs->name);
625 override_name[0] = 0;
626 } else {
627 /*
628 * The override cannot be currently verified.
629 * Deferring to let the watchdog check.
630 */
631 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
632 cs->name);
633 }
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200634 } else
635 /* Override clocksource can be used. */
636 best = cs;
637 break;
638 }
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000639
Baolin Wang7f852afe2018-01-17 14:01:28 +0800640found:
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000641 if (curr_clocksource != best && !timekeeping_notify(best)) {
642 pr_info("Switched to clocksource %s\n", best->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200643 curr_clocksource = best;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200644 }
john stultz734efb42006-06-26 00:25:05 -0700645}
646
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000647/**
648 * clocksource_select - Select the best clocksource available
649 *
650 * Private function. Must hold clocksource_mutex when called.
651 *
652 * Select the clocksource with the best rating, or the clocksource,
653 * which is selected by userspace override.
654 */
655static void clocksource_select(void)
656{
Guillaume Gomezcfed4322015-09-23 13:19:19 +0200657 __clocksource_select(false);
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000658}
659
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000660static void clocksource_select_fallback(void)
661{
Guillaume Gomezcfed4322015-09-23 13:19:19 +0200662 __clocksource_select(true);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000663}
664
John Stultz592913e2010-07-13 17:56:20 -0700665#else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200666static inline void clocksource_select(void) { }
Thomas Gleixner1eaff672013-05-28 09:48:46 +0200667static inline void clocksource_select_fallback(void) { }
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200668
669#endif
670
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200671/*
672 * clocksource_done_booting - Called near the end of core bootup
673 *
674 * Hack to avoid lots of clocksource churn at boot time.
675 * We use fs_initcall because we want this to start before
676 * device_initcall but after subsys_initcall.
677 */
678static int __init clocksource_done_booting(void)
679{
john stultzad6759f2010-03-01 12:34:43 -0800680 mutex_lock(&clocksource_mutex);
681 curr_clocksource = clocksource_default_clock();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200682 finished_booting = 1;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200683 /*
684 * Run the watchdog first to eliminate unstable clock sources
685 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200686 __clocksource_watchdog_kthread();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200687 clocksource_select();
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200688 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200689 return 0;
690}
691fs_initcall(clocksource_done_booting);
692
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800693/*
694 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700695 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200696static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700697{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200698 struct list_head *entry = &clocksource_list;
699 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700700
Minfei Huang0fb71d32016-04-25 17:20:28 +0800701 list_for_each_entry(tmp, &clocksource_list, list) {
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800702 /* Keep track of the place, where to insert */
Minfei Huang0fb71d32016-04-25 17:20:28 +0800703 if (tmp->rating < cs->rating)
704 break;
705 entry = &tmp->list;
706 }
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200707 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700708}
709
John Stultzd7e81c22010-05-07 18:07:38 -0700710/**
John Stultzfba9e072015-03-11 21:16:40 -0700711 * __clocksource_update_freq_scale - Used update clocksource with new freq
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900712 * @cs: clocksource to be registered
John Stultz852db462010-07-13 17:56:28 -0700713 * @scale: Scale factor multiplied against freq to get clocksource hz
714 * @freq: clocksource frequency (cycles per second) divided by scale
715 *
716 * This should only be called from the clocksource->enable() method.
717 *
718 * This *SHOULD NOT* be called directly! Please use the
John Stultzfba9e072015-03-11 21:16:40 -0700719 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
720 * functions.
John Stultz852db462010-07-13 17:56:28 -0700721 */
John Stultzfba9e072015-03-11 21:16:40 -0700722void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
John Stultz852db462010-07-13 17:56:28 -0700723{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200724 u64 sec;
John Stultzf8935982015-03-11 21:16:37 -0700725
John Stultz852db462010-07-13 17:56:28 -0700726 /*
John Stultzf8935982015-03-11 21:16:37 -0700727 * Default clocksources are *special* and self-define their mult/shift.
728 * But, you're not special, so you should specify a freq value.
John Stultz852db462010-07-13 17:56:28 -0700729 */
John Stultzf8935982015-03-11 21:16:37 -0700730 if (freq) {
731 /*
732 * Calc the maximum number of seconds which we can run before
733 * wrapping around. For clocksources which have a mask > 32-bit
734 * we need to limit the max sleep time to have a good
735 * conversion precision. 10 minutes is still a reasonable
736 * amount. That results in a shift value of 24 for a
737 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
738 * ~ 0.06ppm granularity for NTP.
739 */
740 sec = cs->mask;
741 do_div(sec, freq);
742 do_div(sec, scale);
743 if (!sec)
744 sec = 1;
745 else if (sec > 600 && cs->mask > UINT_MAX)
746 sec = 600;
Thomas Gleixner724ed532011-05-18 21:33:40 +0000747
John Stultzf8935982015-03-11 21:16:37 -0700748 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
749 NSEC_PER_SEC / scale, sec * scale);
750 }
John Stultzd65670a2011-10-31 17:06:35 -0400751 /*
John Stultz362fde02015-03-11 21:16:30 -0700752 * Ensure clocksources that have large 'mult' values don't overflow
753 * when adjusted.
John Stultzd65670a2011-10-31 17:06:35 -0400754 */
755 cs->maxadj = clocksource_max_adjustment(cs);
John Stultzf8935982015-03-11 21:16:37 -0700756 while (freq && ((cs->mult + cs->maxadj < cs->mult)
757 || (cs->mult - cs->maxadj > cs->mult))) {
John Stultzd65670a2011-10-31 17:06:35 -0400758 cs->mult >>= 1;
759 cs->shift--;
760 cs->maxadj = clocksource_max_adjustment(cs);
761 }
762
John Stultzf8935982015-03-11 21:16:37 -0700763 /*
764 * Only warn for *special* clocksources that self-define
765 * their mult/shift values and don't specify a freq.
766 */
767 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
768 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
769 cs->name);
770
John Stultzfb82fe22015-03-11 21:16:31 -0700771 clocksource_update_max_deferment(cs);
John Stultz8cc8c522015-03-11 21:16:39 -0700772
Joe Perches45bbfe62015-05-25 11:49:55 -0700773 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
774 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
John Stultz852db462010-07-13 17:56:28 -0700775}
John Stultzfba9e072015-03-11 21:16:40 -0700776EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
John Stultz852db462010-07-13 17:56:28 -0700777
778/**
John Stultzd7e81c22010-05-07 18:07:38 -0700779 * __clocksource_register_scale - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900780 * @cs: clocksource to be registered
John Stultzd7e81c22010-05-07 18:07:38 -0700781 * @scale: Scale factor multiplied against freq to get clocksource hz
782 * @freq: clocksource frequency (cycles per second) divided by scale
783 *
784 * Returns -EBUSY if registration fails, zero otherwise.
785 *
786 * This *SHOULD NOT* be called directly! Please use the
787 * clocksource_register_hz() or clocksource_register_khz helper functions.
788 */
789int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
790{
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200791 unsigned long flags;
John Stultzd7e81c22010-05-07 18:07:38 -0700792
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400793 /* Initialize mult/shift and max_idle_ns */
John Stultzfba9e072015-03-11 21:16:40 -0700794 __clocksource_update_freq_scale(cs, scale, freq);
John Stultzd7e81c22010-05-07 18:07:38 -0700795
James Hartleybe278e92014-09-18 15:59:07 +0100796 /* Add clocksource to the clocksource list */
John Stultzd7e81c22010-05-07 18:07:38 -0700797 mutex_lock(&clocksource_mutex);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200798
799 clocksource_watchdog_lock(&flags);
John Stultzd7e81c22010-05-07 18:07:38 -0700800 clocksource_enqueue(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700801 clocksource_enqueue_watchdog(cs);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200802 clocksource_watchdog_unlock(&flags);
803
john stultze05b2ef2011-05-04 18:16:50 -0700804 clocksource_select();
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100805 clocksource_select_watchdog(false);
John Stultzd7e81c22010-05-07 18:07:38 -0700806 mutex_unlock(&clocksource_mutex);
807 return 0;
808}
809EXPORT_SYMBOL_GPL(__clocksource_register_scale);
810
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200811static void __clocksource_change_rating(struct clocksource *cs, int rating)
812{
813 list_del(&cs->list);
814 cs->rating = rating;
815 clocksource_enqueue(cs);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200816}
817
john stultz734efb42006-06-26 00:25:05 -0700818/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800819 * clocksource_change_rating - Change the rating of a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900820 * @cs: clocksource to be changed
821 * @rating: new rating
john stultz734efb42006-06-26 00:25:05 -0700822 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800823void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -0700824{
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200825 unsigned long flags;
826
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200827 mutex_lock(&clocksource_mutex);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200828 clocksource_watchdog_lock(&flags);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200829 __clocksource_change_rating(cs, rating);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200830 clocksource_watchdog_unlock(&flags);
831
Thomas Gleixner332962f2013-07-04 22:46:45 +0200832 clocksource_select();
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100833 clocksource_select_watchdog(false);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200834 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700835}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200836EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -0700837
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000838/*
839 * Unbind clocksource @cs. Called with clocksource_mutex held
840 */
841static int clocksource_unbind(struct clocksource *cs)
842{
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200843 unsigned long flags;
844
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100845 if (clocksource_is_watchdog(cs)) {
846 /* Select and try to install a replacement watchdog. */
847 clocksource_select_watchdog(true);
848 if (clocksource_is_watchdog(cs))
849 return -EBUSY;
850 }
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000851
852 if (cs == curr_clocksource) {
853 /* Select and try to install a replacement clock source */
854 clocksource_select_fallback();
855 if (curr_clocksource == cs)
856 return -EBUSY;
857 }
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200858
859 clocksource_watchdog_lock(&flags);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000860 clocksource_dequeue_watchdog(cs);
861 list_del_init(&cs->list);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200862 clocksource_watchdog_unlock(&flags);
863
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000864 return 0;
865}
866
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100867/**
868 * clocksource_unregister - remove a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900869 * @cs: clocksource to be unregistered
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100870 */
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000871int clocksource_unregister(struct clocksource *cs)
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100872{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000873 int ret = 0;
874
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200875 mutex_lock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000876 if (!list_empty(&cs->list))
877 ret = clocksource_unbind(cs);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200878 mutex_unlock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000879 return ret;
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100880}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200881EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +0100882
Daniel Walker2b013702006-12-10 02:21:30 -0800883#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -0700884/**
Baolin Wange87821d2018-01-17 14:01:29 +0800885 * current_clocksource_show - sysfs interface for current clocksource
john stultz734efb42006-06-26 00:25:05 -0700886 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900887 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700888 * @buf: char buffer to be filled with clocksource list
889 *
890 * Provides sysfs interface for listing current clocksource.
891 */
Baolin Wange87821d2018-01-17 14:01:29 +0800892static ssize_t current_clocksource_show(struct device *dev,
893 struct device_attribute *attr,
894 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700895{
Miao Xie5e2cb102008-02-06 01:36:53 -0800896 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700897
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200898 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -0800899 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200900 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700901
Miao Xie5e2cb102008-02-06 01:36:53 -0800902 return count;
john stultz734efb42006-06-26 00:25:05 -0700903}
904
Patrick Palka891292a2013-10-11 13:11:55 -0400905ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
Thomas Gleixner29b54072013-04-25 20:31:45 +0000906{
907 size_t ret = cnt;
908
909 /* strings from sysfs write are not 0 terminated! */
910 if (!cnt || cnt >= CS_NAME_LEN)
911 return -EINVAL;
912
913 /* strip of \n: */
914 if (buf[cnt-1] == '\n')
915 cnt--;
916 if (cnt > 0)
917 memcpy(dst, buf, cnt);
918 dst[cnt] = 0;
919 return ret;
920}
921
john stultz734efb42006-06-26 00:25:05 -0700922/**
Baolin Wange87821d2018-01-17 14:01:29 +0800923 * current_clocksource_store - interface for manually overriding clocksource
john stultz734efb42006-06-26 00:25:05 -0700924 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900925 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700926 * @buf: name of override clocksource
927 * @count: length of buffer
928 *
929 * Takes input from sysfs interface for manually overriding the default
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +0200930 * clocksource selection.
john stultz734efb42006-06-26 00:25:05 -0700931 */
Baolin Wange87821d2018-01-17 14:01:29 +0800932static ssize_t current_clocksource_store(struct device *dev,
933 struct device_attribute *attr,
934 const char *buf, size_t count)
john stultz734efb42006-06-26 00:25:05 -0700935{
Elad Wexler233bcb42013-09-12 13:28:54 +0300936 ssize_t ret;
john stultz734efb42006-06-26 00:25:05 -0700937
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200938 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700939
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000940 ret = sysfs_get_uname(buf, override_name, count);
Thomas Gleixner29b54072013-04-25 20:31:45 +0000941 if (ret >= 0)
942 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -0700943
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200944 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -0700945
946 return ret;
947}
Baolin Wange87821d2018-01-17 14:01:29 +0800948static DEVICE_ATTR_RW(current_clocksource);
john stultz734efb42006-06-26 00:25:05 -0700949
950/**
Baolin Wange87821d2018-01-17 14:01:29 +0800951 * unbind_clocksource_store - interface for manually unbinding clocksource
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000952 * @dev: unused
953 * @attr: unused
954 * @buf: unused
955 * @count: length of buffer
956 *
957 * Takes input from sysfs interface for manually unbinding a clocksource.
958 */
Baolin Wange87821d2018-01-17 14:01:29 +0800959static ssize_t unbind_clocksource_store(struct device *dev,
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000960 struct device_attribute *attr,
961 const char *buf, size_t count)
962{
963 struct clocksource *cs;
964 char name[CS_NAME_LEN];
Elad Wexler233bcb42013-09-12 13:28:54 +0300965 ssize_t ret;
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000966
Thomas Gleixner03e13cf2013-04-25 20:31:50 +0000967 ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000968 if (ret < 0)
969 return ret;
970
971 ret = -ENODEV;
972 mutex_lock(&clocksource_mutex);
973 list_for_each_entry(cs, &clocksource_list, list) {
974 if (strcmp(cs->name, name))
975 continue;
976 ret = clocksource_unbind(cs);
977 break;
978 }
979 mutex_unlock(&clocksource_mutex);
980
981 return ret ? ret : count;
982}
Baolin Wange87821d2018-01-17 14:01:29 +0800983static DEVICE_ATTR_WO(unbind_clocksource);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000984
985/**
Baolin Wange87821d2018-01-17 14:01:29 +0800986 * available_clocksource_show - sysfs interface for listing clocksource
john stultz734efb42006-06-26 00:25:05 -0700987 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900988 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -0700989 * @buf: char buffer to be filled with clocksource list
990 *
991 * Provides sysfs interface for listing registered clocksources
992 */
Baolin Wange87821d2018-01-17 14:01:29 +0800993static ssize_t available_clocksource_show(struct device *dev,
994 struct device_attribute *attr,
995 char *buf)
john stultz734efb42006-06-26 00:25:05 -0700996{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700997 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -0800998 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -0700999
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001000 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -07001001 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +02001002 /*
1003 * Don't show non-HRES clocksource if the tick code is
1004 * in one shot mode (highres=on or nohz=on)
1005 */
1006 if (!tick_oneshot_mode_active() ||
1007 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -07001008 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -08001009 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1010 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -07001011 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001012 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001013
Miao Xie5e2cb102008-02-06 01:36:53 -08001014 count += snprintf(buf + count,
1015 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -07001016
Miao Xie5e2cb102008-02-06 01:36:53 -08001017 return count;
john stultz734efb42006-06-26 00:25:05 -07001018}
Baolin Wange87821d2018-01-17 14:01:29 +08001019static DEVICE_ATTR_RO(available_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001020
Baolin Wang27263e82018-01-17 14:01:30 +08001021static struct attribute *clocksource_attrs[] = {
1022 &dev_attr_current_clocksource.attr,
1023 &dev_attr_unbind_clocksource.attr,
1024 &dev_attr_available_clocksource.attr,
1025 NULL
1026};
1027ATTRIBUTE_GROUPS(clocksource);
1028
Kay Sieversd369a5d2011-12-14 15:28:51 -08001029static struct bus_type clocksource_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001030 .name = "clocksource",
Kay Sieversd369a5d2011-12-14 15:28:51 -08001031 .dev_name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -07001032};
1033
Kay Sieversd369a5d2011-12-14 15:28:51 -08001034static struct device device_clocksource = {
john stultz734efb42006-06-26 00:25:05 -07001035 .id = 0,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001036 .bus = &clocksource_subsys,
Baolin Wang27263e82018-01-17 14:01:30 +08001037 .groups = clocksource_groups,
john stultz734efb42006-06-26 00:25:05 -07001038};
1039
john stultzad596172006-06-26 00:25:06 -07001040static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -07001041{
Kay Sieversd369a5d2011-12-14 15:28:51 -08001042 int error = subsys_system_register(&clocksource_subsys, NULL);
john stultz734efb42006-06-26 00:25:05 -07001043
1044 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001045 error = device_register(&device_clocksource);
Baolin Wang27263e82018-01-17 14:01:30 +08001046
john stultz734efb42006-06-26 00:25:05 -07001047 return error;
1048}
1049
1050device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -08001051#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -07001052
1053/**
1054 * boot_override_clocksource - boot clock override
1055 * @str: override name
1056 *
1057 * Takes a clocksource= boot argument and uses it
1058 * as the clocksource override name.
1059 */
1060static int __init boot_override_clocksource(char* str)
1061{
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001062 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001063 if (str)
1064 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001065 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001066 return 1;
1067}
1068
1069__setup("clocksource=", boot_override_clocksource);
1070
1071/**
1072 * boot_override_clock - Compatibility layer for deprecated boot option
1073 * @str: override name
1074 *
1075 * DEPRECATED! Takes a clock= boot argument and uses it
1076 * as the clocksource override name
1077 */
1078static int __init boot_override_clock(char* str)
1079{
john stultz5d0cf412006-06-26 00:25:12 -07001080 if (!strcmp(str, "pmtmr")) {
Joe Perches45bbfe62015-05-25 11:49:55 -07001081 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
john stultz5d0cf412006-06-26 00:25:12 -07001082 return boot_override_clocksource("acpi_pm");
1083 }
Joe Perches45bbfe62015-05-25 11:49:55 -07001084 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -07001085 return boot_override_clocksource(str);
1086}
1087
1088__setup("clock=", boot_override_clock);