blob: 43243f2be98e9a33200de8f8da212e4c2318ede3 [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0+
john stultz734efb42006-06-26 00:25:05 -07002/*
john stultz734efb42006-06-26 00:25:05 -07003 * This file contains the functions which manage clocksource drivers.
4 *
5 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
john stultz734efb42006-06-26 00:25:05 -07006 */
7
Joe Perches45bbfe62015-05-25 11:49:55 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Kay Sieversd369a5d2011-12-14 15:28:51 -080010#include <linux/device.h>
john stultz734efb42006-06-26 00:25:05 -070011#include <linux/clocksource.h>
john stultz734efb42006-06-26 00:25:05 -070012#include <linux/init.h>
13#include <linux/module.h>
Mathieu Desnoyersdc29a362007-02-10 01:43:43 -080014#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080015#include <linux/tick.h>
Martin Schwidefsky01548f42009-08-18 17:09:42 +020016#include <linux/kthread.h>
john stultz734efb42006-06-26 00:25:05 -070017
Thomas Gleixnerc1797ba2015-03-25 13:07:37 +010018#include "tick-internal.h"
Thomas Gleixner3a978372014-07-16 21:05:10 +000019#include "timekeeping_internal.h"
Thomas Gleixner03e13cf2013-04-25 20:31:50 +000020
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000021/**
22 * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
23 * @mult: pointer to mult variable
24 * @shift: pointer to shift variable
25 * @from: frequency to convert from
26 * @to: frequency to convert to
Nicolas Pitre5fdade92011-01-11 12:18:12 -050027 * @maxsec: guaranteed runtime conversion range in seconds
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000028 *
29 * The function evaluates the shift/mult pair for the scaled math
30 * operations of clocksources and clockevents.
31 *
32 * @to and @from are frequency values in HZ. For clock sources @to is
33 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
34 * event @to is the counter frequency and @from is NSEC_PER_SEC.
35 *
Nicolas Pitre5fdade92011-01-11 12:18:12 -050036 * The @maxsec conversion range argument controls the time frame in
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000037 * seconds which must be covered by the runtime conversion with the
38 * calculated mult and shift factors. This guarantees that no 64bit
39 * overflow happens when the input value of the conversion is
40 * multiplied with the calculated mult factor. Larger ranges may
Ingo Molnar4bf07f62021-03-22 22:39:03 +010041 * reduce the conversion accuracy by choosing smaller mult and shift
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000042 * factors.
43 */
44void
Nicolas Pitre5fdade92011-01-11 12:18:12 -050045clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000046{
47 u64 tmp;
48 u32 sft, sftacc= 32;
49
50 /*
51 * Calculate the shift factor which is limiting the conversion
52 * range:
53 */
Nicolas Pitre5fdade92011-01-11 12:18:12 -050054 tmp = ((u64)maxsec * from) >> 32;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000055 while (tmp) {
56 tmp >>=1;
57 sftacc--;
58 }
59
60 /*
61 * Find the conversion shift/mult pair which has the best
62 * accuracy and fits the maxsec conversion range:
63 */
64 for (sft = 32; sft > 0; sft--) {
65 tmp = (u64) to << sft;
john stultzb5776c42010-12-16 19:03:27 +000066 tmp += from / 2;
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000067 do_div(tmp, from);
68 if ((tmp >> sftacc) == 0)
69 break;
70 }
71 *mult = tmp;
72 *shift = sft;
73}
Murali Karicheri53041212016-12-06 18:00:43 -060074EXPORT_SYMBOL_GPL(clocks_calc_mult_shift);
Thomas Gleixner7d2f9442009-11-11 14:05:29 +000075
john stultz734efb42006-06-26 00:25:05 -070076/*[Clocksource internal variables]---------
77 * curr_clocksource:
Martin Schwidefskyf1b82742009-08-14 15:47:21 +020078 * currently selected clocksource.
Baolin Wang39232ed2018-07-17 15:55:16 +080079 * suspend_clocksource:
80 * used to calculate the suspend time.
john stultz734efb42006-06-26 00:25:05 -070081 * clocksource_list:
82 * linked list with the registered clocksources
Martin Schwidefsky75c51582009-08-14 15:47:30 +020083 * clocksource_mutex:
84 * protects manipulations to curr_clocksource and the clocksource_list
john stultz734efb42006-06-26 00:25:05 -070085 * override_name:
86 * Name of the user-specified clocksource.
87 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +020088static struct clocksource *curr_clocksource;
Baolin Wang39232ed2018-07-17 15:55:16 +080089static struct clocksource *suspend_clocksource;
john stultz734efb42006-06-26 00:25:05 -070090static LIST_HEAD(clocksource_list);
Martin Schwidefsky75c51582009-08-14 15:47:30 +020091static DEFINE_MUTEX(clocksource_mutex);
Thomas Gleixner29b54072013-04-25 20:31:45 +000092static char override_name[CS_NAME_LEN];
Thomas Gleixner54a6bc02009-09-14 19:49:02 +020093static int finished_booting;
Baolin Wang39232ed2018-07-17 15:55:16 +080094static u64 suspend_start;
john stultz734efb42006-06-26 00:25:05 -070095
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -080096#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
Martin Schwidefskyf79e0252009-09-11 15:33:05 +020097static void clocksource_watchdog_work(struct work_struct *work);
Thomas Gleixner332962f2013-07-04 22:46:45 +020098static void clocksource_select(void);
Martin Schwidefskyf79e0252009-09-11 15:33:05 +020099
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800100static LIST_HEAD(watchdog_list);
101static struct clocksource *watchdog;
102static struct timer_list watchdog_timer;
Martin Schwidefskyf79e0252009-09-11 15:33:05 +0200103static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800104static DEFINE_SPINLOCK(watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200105static int watchdog_running;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200106static atomic_t watchdog_reset_pending;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700107
Mathieu Malaterre0f48b412019-05-24 12:33:39 +0200108static inline void clocksource_watchdog_lock(unsigned long *flags)
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200109{
110 spin_lock_irqsave(&watchdog_lock, *flags);
111}
112
Mathieu Malaterre0f48b412019-05-24 12:33:39 +0200113static inline void clocksource_watchdog_unlock(unsigned long *flags)
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200114{
115 spin_unlock_irqrestore(&watchdog_lock, *flags);
116}
117
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200118static int clocksource_watchdog_kthread(void *data);
119static void __clocksource_change_rating(struct clocksource *cs, int rating);
120
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800121/*
Daniel Walker35c35d12007-05-09 02:33:40 -0700122 * Interval: 0.5sec Threshold: 0.0625s
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800123 */
124#define WATCHDOG_INTERVAL (HZ >> 1)
Daniel Walker35c35d12007-05-09 02:33:40 -0700125#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800126
Paul E. McKenneydb3a34e2021-05-27 12:01:19 -0700127/*
128 * Maximum permissible delay between two readouts of the watchdog
129 * clocksource surrounding a read of the clocksource being validated.
130 * This delay could be due to SMIs, NMIs, or to VCPU preemptions.
131 */
132#define WATCHDOG_MAX_SKEW (100 * NSEC_PER_USEC)
133
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200134static void clocksource_watchdog_work(struct work_struct *work)
135{
136 /*
137 * We cannot directly run clocksource_watchdog_kthread() here, because
138 * clocksource_select() calls timekeeping_notify() which uses
139 * stop_machine(). One cannot use stop_machine() from a workqueue() due
140 * lock inversions wrt CPU hotplug.
141 *
142 * Also, we only ever run this work once or twice during the lifetime
143 * of the kernel, so there is no point in creating a more permanent
144 * kthread for this.
145 *
146 * If kthread_run fails the next watchdog scan over the
147 * watchdog_list will find the unstable clock again.
148 */
149 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
150}
151
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200152static void __clocksource_unstable(struct clocksource *cs)
153{
154 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
155 cs->flags |= CLOCK_SOURCE_UNSTABLE;
Thomas Gleixner12907fb2016-12-15 11:44:28 +0100156
Peter Zijlstracd2af07d2018-04-30 12:00:13 +0200157 /*
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200158 * If the clocksource is registered clocksource_watchdog_kthread() will
Peter Zijlstracd2af07d2018-04-30 12:00:13 +0200159 * re-rate and re-select.
160 */
161 if (list_empty(&cs->list)) {
162 cs->rating = 0;
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200163 return;
Peter Zijlstracd2af07d2018-04-30 12:00:13 +0200164 }
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200165
Thomas Gleixner12907fb2016-12-15 11:44:28 +0100166 if (cs->mark_unstable)
167 cs->mark_unstable(cs);
168
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200169 /* kick clocksource_watchdog_kthread() */
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200170 if (finished_booting)
171 schedule_work(&watchdog_work);
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200172}
173
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200174/**
175 * clocksource_mark_unstable - mark clocksource unstable via watchdog
176 * @cs: clocksource to be marked unstable
177 *
Peter Zijlstra7dba33c2018-04-30 12:00:14 +0200178 * This function is called by the x86 TSC code to mark clocksources as unstable;
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200179 * it defers demotion and re-selection to a kthread.
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200180 */
181void clocksource_mark_unstable(struct clocksource *cs)
182{
183 unsigned long flags;
184
185 spin_lock_irqsave(&watchdog_lock, flags);
186 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200187 if (!list_empty(&cs->list) && list_empty(&cs->wd_list))
Thomas Gleixner7285dd72009-08-28 20:25:24 +0200188 list_add(&cs->wd_list, &watchdog_list);
189 __clocksource_unstable(cs);
190 }
191 spin_unlock_irqrestore(&watchdog_lock, flags);
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800192}
193
Paul E. McKenneydb3a34e2021-05-27 12:01:19 -0700194static ulong max_cswd_read_retries = 3;
195module_param(max_cswd_read_retries, ulong, 0644);
196
197static bool cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
198{
199 unsigned int nretries;
200 u64 wd_end, wd_delta;
201 int64_t wd_delay;
202
203 for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
204 local_irq_disable();
205 *wdnow = watchdog->read(watchdog);
206 *csnow = cs->read(cs);
207 wd_end = watchdog->read(watchdog);
208 local_irq_enable();
209
210 wd_delta = clocksource_delta(wd_end, *wdnow, watchdog->mask);
211 wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
212 watchdog->shift);
213 if (wd_delay <= WATCHDOG_MAX_SKEW) {
214 if (nretries > 1 || nretries >= max_cswd_read_retries) {
215 pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n",
216 smp_processor_id(), watchdog->name, nretries);
217 }
218 return true;
219 }
220 }
221
222 pr_warn("timekeeping watchdog on CPU%d: %s read-back delay of %lldns, attempt %d, marking unstable\n",
223 smp_processor_id(), watchdog->name, wd_delay, nretries);
224 return false;
225}
226
Kees Cooke99e88a2017-10-16 14:43:17 -0700227static void clocksource_watchdog(struct timer_list *unused)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800228{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100229 u64 csnow, wdnow, cslast, wdlast, delta;
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200230 int next_cpu, reset_pending;
Paul E. McKenneydb3a34e2021-05-27 12:01:19 -0700231 int64_t wd_nsec, cs_nsec;
232 struct clocksource *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800233
234 spin_lock(&watchdog_lock);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200235 if (!watchdog_running)
236 goto out;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800237
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200238 reset_pending = atomic_read(&watchdog_reset_pending);
239
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200240 list_for_each_entry(cs, &watchdog_list, wd_list) {
241
242 /* Clocksource already marked unstable? */
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200243 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200244 if (finished_booting)
245 schedule_work(&watchdog_work);
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200246 continue;
Martin Schwidefsky01548f42009-08-18 17:09:42 +0200247 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200248
Paul E. McKenneydb3a34e2021-05-27 12:01:19 -0700249 if (!cs_watchdog_read(cs, &csnow, &wdnow)) {
250 /* Clock readout unreliable, so give it up. */
251 __clocksource_unstable(cs);
252 continue;
253 }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700254
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200255 /* Clocksource initialized ? */
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200256 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
257 atomic_read(&watchdog_reset_pending)) {
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200258 cs->flags |= CLOCK_SOURCE_WATCHDOG;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200259 cs->wd_last = wdnow;
260 cs->cs_last = csnow;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700261 continue;
262 }
263
Thomas Gleixner3a978372014-07-16 21:05:10 +0000264 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
265 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
266 watchdog->shift);
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200267
Thomas Gleixner3a978372014-07-16 21:05:10 +0000268 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
269 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
John Stultz0b046b22015-03-11 21:16:36 -0700270 wdlast = cs->wd_last; /* save these in case we print them */
271 cslast = cs->cs_last;
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200272 cs->cs_last = csnow;
273 cs->wd_last = wdnow;
274
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200275 if (atomic_read(&watchdog_reset_pending))
276 continue;
277
Thomas Gleixnerb5199512011-06-16 16:22:08 +0200278 /* Check the deviation from the watchdog clocksource. */
Andrew Morton79211c82015-11-09 14:58:13 -0800279 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
Seiichi Ikarashi390dd672015-09-10 18:01:56 +0900280 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
281 smp_processor_id(), cs->name);
Joe Perches45bbfe62015-05-25 11:49:55 -0700282 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
John Stultz0b046b22015-03-11 21:16:36 -0700283 watchdog->name, wdnow, wdlast, watchdog->mask);
Joe Perches45bbfe62015-05-25 11:49:55 -0700284 pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
John Stultz0b046b22015-03-11 21:16:36 -0700285 cs->name, csnow, cslast, cs->mask);
286 __clocksource_unstable(cs);
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200287 continue;
288 }
289
Peter Zijlstrab421b222017-04-21 12:14:13 +0200290 if (cs == curr_clocksource && cs->tick_stable)
291 cs->tick_stable(cs);
292
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200293 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
294 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
295 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
Thomas Gleixner332962f2013-07-04 22:46:45 +0200296 /* Mark it valid for high-res. */
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200297 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner332962f2013-07-04 22:46:45 +0200298
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200299 /*
Thomas Gleixner332962f2013-07-04 22:46:45 +0200300 * clocksource_done_booting() will sort it if
301 * finished_booting is not set yet.
Martin Schwidefsky8cf4e752009-08-14 15:47:22 +0200302 */
Thomas Gleixner332962f2013-07-04 22:46:45 +0200303 if (!finished_booting)
304 continue;
305
306 /*
307 * If this is not the current clocksource let
308 * the watchdog thread reselect it. Due to the
309 * change to high res this clocksource might
310 * be preferred now. If it is the current
311 * clocksource let the tick code know about
312 * that change.
313 */
314 if (cs != curr_clocksource) {
315 cs->flags |= CLOCK_SOURCE_RESELECT;
316 schedule_work(&watchdog_work);
317 } else {
318 tick_clock_notify();
319 }
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800320 }
321 }
322
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200323 /*
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200324 * We only clear the watchdog_reset_pending, when we did a
325 * full cycle through all clocksources.
326 */
327 if (reset_pending)
328 atomic_dec(&watchdog_reset_pending);
329
330 /*
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200331 * Cycle through CPUs to check if the CPUs stay synchronized
332 * to each other.
333 */
334 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
335 if (next_cpu >= nr_cpu_ids)
336 next_cpu = cpumask_first(cpu_online_mask);
Konstantin Khlebnikovfebac332020-01-31 19:08:59 +0300337
338 /*
339 * Arm timer if not already pending: could race with concurrent
340 * pair clocksource_stop_watchdog() clocksource_start_watchdog().
341 */
342 if (!timer_pending(&watchdog_timer)) {
343 watchdog_timer.expires += WATCHDOG_INTERVAL;
344 add_timer_on(&watchdog_timer, next_cpu);
345 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200346out:
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800347 spin_unlock(&watchdog_lock);
348}
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200349
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200350static inline void clocksource_start_watchdog(void)
351{
352 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
353 return;
Kees Cooke99e88a2017-10-16 14:43:17 -0700354 timer_setup(&watchdog_timer, clocksource_watchdog, 0);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200355 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
356 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
357 watchdog_running = 1;
358}
359
360static inline void clocksource_stop_watchdog(void)
361{
362 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
363 return;
364 del_timer(&watchdog_timer);
365 watchdog_running = 0;
366}
367
Martin Schwidefsky0f8e8ef2009-08-14 15:47:23 +0200368static inline void clocksource_reset_watchdog(void)
369{
370 struct clocksource *cs;
371
372 list_for_each_entry(cs, &watchdog_list, wd_list)
373 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
374}
375
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700376static void clocksource_resume_watchdog(void)
377{
Thomas Gleixner9fb60332011-09-12 13:32:23 +0200378 atomic_inc(&watchdog_reset_pending);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700379}
380
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200381static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800382{
Peter Zijlstra5b9e8862018-04-30 12:00:11 +0200383 INIT_LIST_HEAD(&cs->wd_list);
384
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800385 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200386 /* cs is a clocksource to be watched. */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800387 list_add(&cs->wd_list, &watchdog_list);
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200388 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200389 } else {
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200390 /* cs is a watchdog. */
Thomas Gleixner948ac6d2007-03-25 14:42:51 +0200391 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800392 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800393 }
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100394}
395
396static void clocksource_select_watchdog(bool fallback)
397{
398 struct clocksource *cs, *old_wd;
399 unsigned long flags;
400
401 spin_lock_irqsave(&watchdog_lock, flags);
402 /* save current watchdog */
403 old_wd = watchdog;
404 if (fallback)
405 watchdog = NULL;
406
407 list_for_each_entry(cs, &clocksource_list, list) {
408 /* cs is a clocksource to be watched. */
409 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY)
410 continue;
411
412 /* Skip current if we were requested for a fallback. */
413 if (fallback && cs == old_wd)
414 continue;
415
416 /* Pick the best watchdog. */
417 if (!watchdog || cs->rating > watchdog->rating)
418 watchdog = cs;
419 }
420 /* If we failed to find a fallback restore the old one. */
421 if (!watchdog)
422 watchdog = old_wd;
423
424 /* If we changed the watchdog we need to reset cycles. */
425 if (watchdog != old_wd)
426 clocksource_reset_watchdog();
427
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200428 /* Check if the watchdog timer needs to be started. */
429 clocksource_start_watchdog();
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800430 spin_unlock_irqrestore(&watchdog_lock, flags);
431}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200432
433static void clocksource_dequeue_watchdog(struct clocksource *cs)
434{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +0000435 if (cs != watchdog) {
436 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
437 /* cs is a watched clocksource. */
438 list_del_init(&cs->wd_list);
439 /* Check if the watchdog timer needs to be stopped. */
440 clocksource_stop_watchdog();
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200441 }
442 }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200443}
444
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200445static int __clocksource_watchdog_kthread(void)
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200446{
447 struct clocksource *cs, *tmp;
448 unsigned long flags;
Thomas Gleixner332962f2013-07-04 22:46:45 +0200449 int select = 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200450
451 spin_lock_irqsave(&watchdog_lock, flags);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200452 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200453 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
454 list_del_init(&cs->wd_list);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200455 __clocksource_change_rating(cs, 0);
Thomas Gleixner332962f2013-07-04 22:46:45 +0200456 select = 1;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200457 }
Thomas Gleixner332962f2013-07-04 22:46:45 +0200458 if (cs->flags & CLOCK_SOURCE_RESELECT) {
459 cs->flags &= ~CLOCK_SOURCE_RESELECT;
460 select = 1;
461 }
462 }
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200463 /* Check if the watchdog timer needs to be stopped. */
464 clocksource_stop_watchdog();
Thomas Gleixner6ea41d22009-08-15 13:20:42 +0200465 spin_unlock_irqrestore(&watchdog_lock, flags);
466
Thomas Gleixner332962f2013-07-04 22:46:45 +0200467 return select;
468}
469
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200470static int clocksource_watchdog_kthread(void *data)
Thomas Gleixner332962f2013-07-04 22:46:45 +0200471{
472 mutex_lock(&clocksource_mutex);
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200473 if (__clocksource_watchdog_kthread())
Thomas Gleixner332962f2013-07-04 22:46:45 +0200474 clocksource_select();
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200475 mutex_unlock(&clocksource_mutex);
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200476 return 0;
Martin Schwidefskyc55c87c2009-08-14 15:47:25 +0200477}
478
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000479static bool clocksource_is_watchdog(struct clocksource *cs)
480{
481 return cs == watchdog;
482}
483
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200484#else /* CONFIG_CLOCKSOURCE_WATCHDOG */
485
486static void clocksource_enqueue_watchdog(struct clocksource *cs)
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800487{
488 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
489 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
490}
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700491
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100492static void clocksource_select_watchdog(bool fallback) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200493static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700494static inline void clocksource_resume_watchdog(void) { }
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200495static inline int __clocksource_watchdog_kthread(void) { return 0; }
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000496static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
Prarit Bhargava397bbf62013-02-22 15:08:56 -0500497void clocksource_mark_unstable(struct clocksource *cs) { }
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200498
Mathieu Malaterredb6f9e52018-05-16 21:59:43 +0200499static inline void clocksource_watchdog_lock(unsigned long *flags) { }
500static inline void clocksource_watchdog_unlock(unsigned long *flags) { }
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200501
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +0200502#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800503
Baolin Wang39232ed2018-07-17 15:55:16 +0800504static bool clocksource_is_suspend(struct clocksource *cs)
505{
506 return cs == suspend_clocksource;
507}
508
509static void __clocksource_suspend_select(struct clocksource *cs)
510{
511 /*
512 * Skip the clocksource which will be stopped in suspend state.
513 */
514 if (!(cs->flags & CLOCK_SOURCE_SUSPEND_NONSTOP))
515 return;
516
517 /*
518 * The nonstop clocksource can be selected as the suspend clocksource to
519 * calculate the suspend time, so it should not supply suspend/resume
520 * interfaces to suspend the nonstop clocksource when system suspends.
521 */
522 if (cs->suspend || cs->resume) {
523 pr_warn("Nonstop clocksource %s should not supply suspend/resume interfaces\n",
524 cs->name);
525 }
526
527 /* Pick the best rating. */
528 if (!suspend_clocksource || cs->rating > suspend_clocksource->rating)
529 suspend_clocksource = cs;
530}
531
532/**
533 * clocksource_suspend_select - Select the best clocksource for suspend timing
534 * @fallback: if select a fallback clocksource
535 */
536static void clocksource_suspend_select(bool fallback)
537{
538 struct clocksource *cs, *old_suspend;
539
540 old_suspend = suspend_clocksource;
541 if (fallback)
542 suspend_clocksource = NULL;
543
544 list_for_each_entry(cs, &clocksource_list, list) {
545 /* Skip current if we were requested for a fallback. */
546 if (fallback && cs == old_suspend)
547 continue;
548
549 __clocksource_suspend_select(cs);
550 }
551}
552
553/**
554 * clocksource_start_suspend_timing - Start measuring the suspend timing
555 * @cs: current clocksource from timekeeping
556 * @start_cycles: current cycles from timekeeping
557 *
558 * This function will save the start cycle values of suspend timer to calculate
559 * the suspend time when resuming system.
560 *
561 * This function is called late in the suspend process from timekeeping_suspend(),
Ingo Molnar4bf07f62021-03-22 22:39:03 +0100562 * that means processes are frozen, non-boot cpus and interrupts are disabled
Baolin Wang39232ed2018-07-17 15:55:16 +0800563 * now. It is therefore possible to start the suspend timer without taking the
564 * clocksource mutex.
565 */
566void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles)
567{
568 if (!suspend_clocksource)
569 return;
570
571 /*
572 * If current clocksource is the suspend timer, we should use the
573 * tkr_mono.cycle_last value as suspend_start to avoid same reading
574 * from suspend timer.
575 */
576 if (clocksource_is_suspend(cs)) {
577 suspend_start = start_cycles;
578 return;
579 }
580
581 if (suspend_clocksource->enable &&
582 suspend_clocksource->enable(suspend_clocksource)) {
583 pr_warn_once("Failed to enable the non-suspend-able clocksource.\n");
584 return;
585 }
586
587 suspend_start = suspend_clocksource->read(suspend_clocksource);
588}
589
590/**
591 * clocksource_stop_suspend_timing - Stop measuring the suspend timing
592 * @cs: current clocksource from timekeeping
593 * @cycle_now: current cycles from timekeeping
594 *
595 * This function will calculate the suspend time from suspend timer.
596 *
597 * Returns nanoseconds since suspend started, 0 if no usable suspend clocksource.
598 *
599 * This function is called early in the resume process from timekeeping_resume(),
600 * that means there is only one cpu, no processes are running and the interrupts
601 * are disabled. It is therefore possible to stop the suspend timer without
602 * taking the clocksource mutex.
603 */
604u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now)
605{
606 u64 now, delta, nsec = 0;
607
608 if (!suspend_clocksource)
609 return 0;
610
611 /*
612 * If current clocksource is the suspend timer, we should use the
613 * tkr_mono.cycle_last value from timekeeping as current cycle to
614 * avoid same reading from suspend timer.
615 */
616 if (clocksource_is_suspend(cs))
617 now = cycle_now;
618 else
619 now = suspend_clocksource->read(suspend_clocksource);
620
621 if (now > suspend_start) {
622 delta = clocksource_delta(now, suspend_start,
623 suspend_clocksource->mask);
624 nsec = mul_u64_u32_shr(delta, suspend_clocksource->mult,
625 suspend_clocksource->shift);
626 }
627
628 /*
629 * Disable the suspend timer to save power if current clocksource is
630 * not the suspend timer.
631 */
632 if (!clocksource_is_suspend(cs) && suspend_clocksource->disable)
633 suspend_clocksource->disable(suspend_clocksource);
634
635 return nsec;
636}
637
john stultz734efb42006-06-26 00:25:05 -0700638/**
Magnus Dammc54a42b2010-02-02 14:41:41 -0800639 * clocksource_suspend - suspend the clocksource(s)
640 */
641void clocksource_suspend(void)
642{
643 struct clocksource *cs;
644
645 list_for_each_entry_reverse(cs, &clocksource_list, list)
646 if (cs->suspend)
647 cs->suspend(cs);
648}
649
650/**
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700651 * clocksource_resume - resume the clocksource(s)
652 */
653void clocksource_resume(void)
654{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -0700655 struct clocksource *cs;
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700656
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200657 list_for_each_entry(cs, &clocksource_list, list)
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700658 if (cs->resume)
Magnus Damm17622332010-02-02 14:41:39 -0800659 cs->resume(cs);
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700660
661 clocksource_resume_watchdog();
Thomas Gleixnerb52f52a2007-05-09 02:35:15 -0700662}
663
664/**
Jason Wessel7c3078b2008-02-15 14:55:54 -0600665 * clocksource_touch_watchdog - Update watchdog
666 *
667 * Update the watchdog after exception contexts such as kgdb so as not
Thomas Gleixner7b7422a2010-01-26 12:51:10 +0100668 * to incorrectly trip the watchdog. This might fail when the kernel
669 * was stopped in code which holds watchdog_lock.
Jason Wessel7c3078b2008-02-15 14:55:54 -0600670 */
671void clocksource_touch_watchdog(void)
672{
673 clocksource_resume_watchdog();
674}
675
john stultz734efb42006-06-26 00:25:05 -0700676/**
John Stultzd65670a2011-10-31 17:06:35 -0400677 * clocksource_max_adjustment- Returns max adjustment amount
678 * @cs: Pointer to clocksource
679 *
680 */
681static u32 clocksource_max_adjustment(struct clocksource *cs)
682{
683 u64 ret;
684 /*
Jim Cromie88b28ad2012-03-14 21:28:56 -0600685 * We won't try to correct for more than 11% adjustments (110,000 ppm),
John Stultzd65670a2011-10-31 17:06:35 -0400686 */
687 ret = (u64)cs->mult * 11;
688 do_div(ret,100);
689 return (u32)ret;
690}
691
692/**
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700693 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
694 * @mult: cycle to nanosecond multiplier
695 * @shift: cycle to nanosecond divisor (power of two)
696 * @maxadj: maximum adjustment value to mult (~11%)
697 * @mask: bitmask for two's complement subtraction of non 64 bit counters
John Stultzfb82fe22015-03-11 21:16:31 -0700698 * @max_cyc: maximum cycle value before potential overflow (does not include
699 * any safety margin)
John Stultz362fde02015-03-11 21:16:30 -0700700 *
John Stultz8e56f332015-04-01 20:34:39 -0700701 * NOTE: This function includes a safety margin of 50%, in other words, we
702 * return half the number of nanoseconds the hardware counter can technically
703 * cover. This is done so that we can potentially detect problems caused by
704 * delayed timers or bad hardware, which might result in time intervals that
Zhen Lei571af552015-08-25 14:42:53 +0800705 * are larger than what the math used can handle without overflows.
Jon Hunter98962462009-08-18 12:45:10 -0500706 */
John Stultzfb82fe22015-03-11 21:16:31 -0700707u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
Jon Hunter98962462009-08-18 12:45:10 -0500708{
709 u64 max_nsecs, max_cycles;
710
711 /*
712 * Calculate the maximum number of cycles that we can pass to the
John Stultz6086e342015-03-11 21:16:29 -0700713 * cyc2ns() function without overflowing a 64-bit result.
Jon Hunter98962462009-08-18 12:45:10 -0500714 */
John Stultz6086e342015-03-11 21:16:29 -0700715 max_cycles = ULLONG_MAX;
716 do_div(max_cycles, mult+maxadj);
Jon Hunter98962462009-08-18 12:45:10 -0500717
718 /*
719 * The actual maximum number of cycles we can defer the clocksource is
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700720 * determined by the minimum of max_cycles and mask.
John Stultzd65670a2011-10-31 17:06:35 -0400721 * Note: Here we subtract the maxadj to make sure we don't sleep for
722 * too long if there's a large negative adjustment.
Jon Hunter98962462009-08-18 12:45:10 -0500723 */
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700724 max_cycles = min(max_cycles, mask);
725 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
Jon Hunter98962462009-08-18 12:45:10 -0500726
John Stultzfb82fe22015-03-11 21:16:31 -0700727 /* return the max_cycles value as well if requested */
728 if (max_cyc)
729 *max_cyc = max_cycles;
730
John Stultz362fde02015-03-11 21:16:30 -0700731 /* Return 50% of the actual maximum, so we can detect bad values */
732 max_nsecs >>= 1;
733
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700734 return max_nsecs;
735}
736
737/**
John Stultzfb82fe22015-03-11 21:16:31 -0700738 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
739 * @cs: Pointer to clocksource to be updated
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700740 *
741 */
John Stultzfb82fe22015-03-11 21:16:31 -0700742static inline void clocksource_update_max_deferment(struct clocksource *cs)
Stephen Boyd87d8b9e2013-07-18 16:21:14 -0700743{
John Stultzfb82fe22015-03-11 21:16:31 -0700744 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
745 cs->maxadj, cs->mask,
746 &cs->max_cycles);
Jon Hunter98962462009-08-18 12:45:10 -0500747}
748
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000749static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000750{
751 struct clocksource *cs;
752
753 if (!finished_booting || list_empty(&clocksource_list))
754 return NULL;
755
756 /*
757 * We pick the clocksource with the highest rating. If oneshot
758 * mode is active, we pick the highres valid clocksource with
759 * the best rating.
760 */
761 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000762 if (skipcur && cs == curr_clocksource)
763 continue;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000764 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
765 continue;
766 return cs;
767 }
768 return NULL;
769}
770
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000771static void __clocksource_select(bool skipcur)
john stultz734efb42006-06-26 00:25:05 -0700772{
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000773 bool oneshot = tick_oneshot_mode_active();
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200774 struct clocksource *best, *cs;
Thomas Gleixner5d8b34f2007-02-16 01:27:43 -0800775
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000776 /* Find the best suitable clocksource */
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000777 best = clocksource_find_best(oneshot, skipcur);
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000778 if (!best)
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200779 return;
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000780
Baolin Wang7f852afe2018-01-17 14:01:28 +0800781 if (!strlen(override_name))
782 goto found;
783
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200784 /* Check for the override clocksource. */
785 list_for_each_entry(cs, &clocksource_list, list) {
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000786 if (skipcur && cs == curr_clocksource)
787 continue;
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200788 if (strcmp(cs->name, override_name) != 0)
789 continue;
790 /*
791 * Check to make sure we don't switch to a non-highres
792 * capable clocksource if the tick code is in oneshot
793 * mode (highres or nohz)
794 */
Thomas Gleixner5d33b882013-04-25 20:31:43 +0000795 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200796 /* Override clocksource cannot be used. */
Kyle Walker36374582016-08-06 12:07:30 -0400797 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
798 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
799 cs->name);
800 override_name[0] = 0;
801 } else {
802 /*
803 * The override cannot be currently verified.
804 * Deferring to let the watchdog check.
805 */
806 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
807 cs->name);
808 }
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200809 } else
810 /* Override clocksource can be used. */
811 best = cs;
812 break;
813 }
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000814
Baolin Wang7f852afe2018-01-17 14:01:28 +0800815found:
Thomas Gleixnerba919d12013-04-25 20:31:44 +0000816 if (curr_clocksource != best && !timekeeping_notify(best)) {
817 pr_info("Switched to clocksource %s\n", best->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200818 curr_clocksource = best;
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200819 }
john stultz734efb42006-06-26 00:25:05 -0700820}
821
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000822/**
823 * clocksource_select - Select the best clocksource available
824 *
825 * Private function. Must hold clocksource_mutex when called.
826 *
827 * Select the clocksource with the best rating, or the clocksource,
828 * which is selected by userspace override.
829 */
830static void clocksource_select(void)
831{
Guillaume Gomezcfed4322015-09-23 13:19:19 +0200832 __clocksource_select(false);
Thomas Gleixnerf5a2e342013-04-25 20:31:45 +0000833}
834
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000835static void clocksource_select_fallback(void)
836{
Guillaume Gomezcfed4322015-09-23 13:19:19 +0200837 __clocksource_select(true);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +0000838}
839
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200840/*
841 * clocksource_done_booting - Called near the end of core bootup
842 *
843 * Hack to avoid lots of clocksource churn at boot time.
844 * We use fs_initcall because we want this to start before
845 * device_initcall but after subsys_initcall.
846 */
847static int __init clocksource_done_booting(void)
848{
john stultzad6759f2010-03-01 12:34:43 -0800849 mutex_lock(&clocksource_mutex);
850 curr_clocksource = clocksource_default_clock();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200851 finished_booting = 1;
Thomas Gleixner54a6bc02009-09-14 19:49:02 +0200852 /*
853 * Run the watchdog first to eliminate unstable clock sources
854 */
Peter Zijlstrae2c631ba2018-09-05 10:41:58 +0200855 __clocksource_watchdog_kthread();
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200856 clocksource_select();
Thomas Gleixnere6c73302009-09-14 19:51:11 +0200857 mutex_unlock(&clocksource_mutex);
Martin Schwidefsky75c51582009-08-14 15:47:30 +0200858 return 0;
859}
860fs_initcall(clocksource_done_booting);
861
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800862/*
863 * Enqueue the clocksource sorted by rating
john stultz734efb42006-06-26 00:25:05 -0700864 */
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200865static void clocksource_enqueue(struct clocksource *cs)
john stultz734efb42006-06-26 00:25:05 -0700866{
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200867 struct list_head *entry = &clocksource_list;
868 struct clocksource *tmp;
john stultz734efb42006-06-26 00:25:05 -0700869
Minfei Huang0fb71d32016-04-25 17:20:28 +0800870 list_for_each_entry(tmp, &clocksource_list, list) {
Thomas Gleixner92c7e002007-02-16 01:27:33 -0800871 /* Keep track of the place, where to insert */
Minfei Huang0fb71d32016-04-25 17:20:28 +0800872 if (tmp->rating < cs->rating)
873 break;
874 entry = &tmp->list;
875 }
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200876 list_add(&cs->list, entry);
john stultz734efb42006-06-26 00:25:05 -0700877}
878
John Stultzd7e81c22010-05-07 18:07:38 -0700879/**
John Stultzfba9e072015-03-11 21:16:40 -0700880 * __clocksource_update_freq_scale - Used update clocksource with new freq
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900881 * @cs: clocksource to be registered
John Stultz852db462010-07-13 17:56:28 -0700882 * @scale: Scale factor multiplied against freq to get clocksource hz
883 * @freq: clocksource frequency (cycles per second) divided by scale
884 *
885 * This should only be called from the clocksource->enable() method.
886 *
887 * This *SHOULD NOT* be called directly! Please use the
John Stultzfba9e072015-03-11 21:16:40 -0700888 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
889 * functions.
John Stultz852db462010-07-13 17:56:28 -0700890 */
John Stultzfba9e072015-03-11 21:16:40 -0700891void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
John Stultz852db462010-07-13 17:56:28 -0700892{
Thomas Gleixnerc0e299b2011-05-20 10:50:52 +0200893 u64 sec;
John Stultzf8935982015-03-11 21:16:37 -0700894
John Stultz852db462010-07-13 17:56:28 -0700895 /*
John Stultzf8935982015-03-11 21:16:37 -0700896 * Default clocksources are *special* and self-define their mult/shift.
897 * But, you're not special, so you should specify a freq value.
John Stultz852db462010-07-13 17:56:28 -0700898 */
John Stultzf8935982015-03-11 21:16:37 -0700899 if (freq) {
900 /*
901 * Calc the maximum number of seconds which we can run before
902 * wrapping around. For clocksources which have a mask > 32-bit
903 * we need to limit the max sleep time to have a good
904 * conversion precision. 10 minutes is still a reasonable
905 * amount. That results in a shift value of 24 for a
906 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
907 * ~ 0.06ppm granularity for NTP.
908 */
909 sec = cs->mask;
910 do_div(sec, freq);
911 do_div(sec, scale);
912 if (!sec)
913 sec = 1;
914 else if (sec > 600 && cs->mask > UINT_MAX)
915 sec = 600;
Thomas Gleixner724ed532011-05-18 21:33:40 +0000916
John Stultzf8935982015-03-11 21:16:37 -0700917 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
918 NSEC_PER_SEC / scale, sec * scale);
919 }
John Stultzd65670a2011-10-31 17:06:35 -0400920 /*
John Stultz362fde02015-03-11 21:16:30 -0700921 * Ensure clocksources that have large 'mult' values don't overflow
922 * when adjusted.
John Stultzd65670a2011-10-31 17:06:35 -0400923 */
924 cs->maxadj = clocksource_max_adjustment(cs);
John Stultzf8935982015-03-11 21:16:37 -0700925 while (freq && ((cs->mult + cs->maxadj < cs->mult)
926 || (cs->mult - cs->maxadj > cs->mult))) {
John Stultzd65670a2011-10-31 17:06:35 -0400927 cs->mult >>= 1;
928 cs->shift--;
929 cs->maxadj = clocksource_max_adjustment(cs);
930 }
931
John Stultzf8935982015-03-11 21:16:37 -0700932 /*
933 * Only warn for *special* clocksources that self-define
934 * their mult/shift values and don't specify a freq.
935 */
936 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
937 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
938 cs->name);
939
John Stultzfb82fe22015-03-11 21:16:31 -0700940 clocksource_update_max_deferment(cs);
John Stultz8cc8c522015-03-11 21:16:39 -0700941
Joe Perches45bbfe62015-05-25 11:49:55 -0700942 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
943 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
John Stultz852db462010-07-13 17:56:28 -0700944}
John Stultzfba9e072015-03-11 21:16:40 -0700945EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
John Stultz852db462010-07-13 17:56:28 -0700946
947/**
John Stultzd7e81c22010-05-07 18:07:38 -0700948 * __clocksource_register_scale - Used to install new clocksources
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +0900949 * @cs: clocksource to be registered
John Stultzd7e81c22010-05-07 18:07:38 -0700950 * @scale: Scale factor multiplied against freq to get clocksource hz
951 * @freq: clocksource frequency (cycles per second) divided by scale
952 *
953 * Returns -EBUSY if registration fails, zero otherwise.
954 *
955 * This *SHOULD NOT* be called directly! Please use the
956 * clocksource_register_hz() or clocksource_register_khz helper functions.
957 */
958int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
959{
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200960 unsigned long flags;
John Stultzd7e81c22010-05-07 18:07:38 -0700961
Thomas Gleixnerd67f34c2018-09-17 14:45:34 +0200962 clocksource_arch_init(cs);
963
Thomas Gleixnerb2c67cb2020-12-09 14:09:27 +0800964 if (WARN_ON_ONCE((unsigned int)cs->id >= CSID_MAX))
965 cs->id = CSID_GENERIC;
Thomas Gleixner5d51bee2020-02-07 13:38:55 +0100966 if (cs->vdso_clock_mode < 0 ||
967 cs->vdso_clock_mode >= VDSO_CLOCKMODE_MAX) {
968 pr_warn("clocksource %s registered with invalid VDSO mode %d. Disabling VDSO support.\n",
969 cs->name, cs->vdso_clock_mode);
970 cs->vdso_clock_mode = VDSO_CLOCKMODE_NONE;
971 }
Thomas Gleixner5d51bee2020-02-07 13:38:55 +0100972
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400973 /* Initialize mult/shift and max_idle_ns */
John Stultzfba9e072015-03-11 21:16:40 -0700974 __clocksource_update_freq_scale(cs, scale, freq);
John Stultzd7e81c22010-05-07 18:07:38 -0700975
James Hartleybe278e92014-09-18 15:59:07 +0100976 /* Add clocksource to the clocksource list */
John Stultzd7e81c22010-05-07 18:07:38 -0700977 mutex_lock(&clocksource_mutex);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200978
979 clocksource_watchdog_lock(&flags);
John Stultzd7e81c22010-05-07 18:07:38 -0700980 clocksource_enqueue(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700981 clocksource_enqueue_watchdog(cs);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +0200982 clocksource_watchdog_unlock(&flags);
983
john stultze05b2ef2011-05-04 18:16:50 -0700984 clocksource_select();
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +0100985 clocksource_select_watchdog(false);
Baolin Wang39232ed2018-07-17 15:55:16 +0800986 __clocksource_suspend_select(cs);
John Stultzd7e81c22010-05-07 18:07:38 -0700987 mutex_unlock(&clocksource_mutex);
988 return 0;
989}
990EXPORT_SYMBOL_GPL(__clocksource_register_scale);
991
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200992static void __clocksource_change_rating(struct clocksource *cs, int rating)
993{
994 list_del(&cs->list);
995 cs->rating = rating;
996 clocksource_enqueue(cs);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +0200997}
998
john stultz734efb42006-06-26 00:25:05 -0700999/**
Thomas Gleixner92c7e002007-02-16 01:27:33 -08001000 * clocksource_change_rating - Change the rating of a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +09001001 * @cs: clocksource to be changed
1002 * @rating: new rating
john stultz734efb42006-06-26 00:25:05 -07001003 */
Thomas Gleixner92c7e002007-02-16 01:27:33 -08001004void clocksource_change_rating(struct clocksource *cs, int rating)
john stultz734efb42006-06-26 00:25:05 -07001005{
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +02001006 unsigned long flags;
1007
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001008 mutex_lock(&clocksource_mutex);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +02001009 clocksource_watchdog_lock(&flags);
Thomas Gleixnerd0981a12009-08-19 11:26:09 +02001010 __clocksource_change_rating(cs, rating);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +02001011 clocksource_watchdog_unlock(&flags);
1012
Thomas Gleixner332962f2013-07-04 22:46:45 +02001013 clocksource_select();
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +01001014 clocksource_select_watchdog(false);
Baolin Wang39232ed2018-07-17 15:55:16 +08001015 clocksource_suspend_select(false);
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001016 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001017}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +02001018EXPORT_SYMBOL(clocksource_change_rating);
john stultz734efb42006-06-26 00:25:05 -07001019
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001020/*
1021 * Unbind clocksource @cs. Called with clocksource_mutex held
1022 */
1023static int clocksource_unbind(struct clocksource *cs)
1024{
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +02001025 unsigned long flags;
1026
Vitaly Kuznetsovbbf66d82016-01-22 18:31:53 +01001027 if (clocksource_is_watchdog(cs)) {
1028 /* Select and try to install a replacement watchdog. */
1029 clocksource_select_watchdog(true);
1030 if (clocksource_is_watchdog(cs))
1031 return -EBUSY;
1032 }
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001033
1034 if (cs == curr_clocksource) {
1035 /* Select and try to install a replacement clock source */
1036 clocksource_select_fallback();
1037 if (curr_clocksource == cs)
1038 return -EBUSY;
1039 }
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +02001040
Baolin Wang39232ed2018-07-17 15:55:16 +08001041 if (clocksource_is_suspend(cs)) {
1042 /*
1043 * Select and try to install a replacement suspend clocksource.
1044 * If no replacement suspend clocksource, we will just let the
1045 * clocksource go and have no suspend clocksource.
1046 */
1047 clocksource_suspend_select(true);
1048 }
1049
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +02001050 clocksource_watchdog_lock(&flags);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001051 clocksource_dequeue_watchdog(cs);
1052 list_del_init(&cs->list);
Peter Zijlstra2aae7bc2018-04-23 17:28:55 +02001053 clocksource_watchdog_unlock(&flags);
1054
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001055 return 0;
1056}
1057
Thomas Gleixner4713e22c2008-01-30 13:30:02 +01001058/**
1059 * clocksource_unregister - remove a registered clocksource
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +09001060 * @cs: clocksource to be unregistered
Thomas Gleixner4713e22c2008-01-30 13:30:02 +01001061 */
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +00001062int clocksource_unregister(struct clocksource *cs)
Thomas Gleixner4713e22c2008-01-30 13:30:02 +01001063{
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +00001064 int ret = 0;
1065
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001066 mutex_lock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +00001067 if (!list_empty(&cs->list))
1068 ret = clocksource_unbind(cs);
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001069 mutex_unlock(&clocksource_mutex);
Thomas Gleixnera89c7ed2013-04-25 20:31:46 +00001070 return ret;
Thomas Gleixner4713e22c2008-01-30 13:30:02 +01001071}
Martin Schwidefskyfb63a0e2009-08-14 15:47:24 +02001072EXPORT_SYMBOL(clocksource_unregister);
Thomas Gleixner4713e22c2008-01-30 13:30:02 +01001073
Daniel Walker2b013702006-12-10 02:21:30 -08001074#ifdef CONFIG_SYSFS
john stultz734efb42006-06-26 00:25:05 -07001075/**
Baolin Wange87821d2018-01-17 14:01:29 +08001076 * current_clocksource_show - sysfs interface for current clocksource
john stultz734efb42006-06-26 00:25:05 -07001077 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +09001078 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -07001079 * @buf: char buffer to be filled with clocksource list
1080 *
1081 * Provides sysfs interface for listing current clocksource.
1082 */
Baolin Wange87821d2018-01-17 14:01:29 +08001083static ssize_t current_clocksource_show(struct device *dev,
1084 struct device_attribute *attr,
1085 char *buf)
john stultz734efb42006-06-26 00:25:05 -07001086{
Miao Xie5e2cb102008-02-06 01:36:53 -08001087 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -07001088
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001089 mutex_lock(&clocksource_mutex);
Miao Xie5e2cb102008-02-06 01:36:53 -08001090 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001091 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001092
Miao Xie5e2cb102008-02-06 01:36:53 -08001093 return count;
john stultz734efb42006-06-26 00:25:05 -07001094}
1095
Patrick Palka891292a2013-10-11 13:11:55 -04001096ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
Thomas Gleixner29b54072013-04-25 20:31:45 +00001097{
1098 size_t ret = cnt;
1099
1100 /* strings from sysfs write are not 0 terminated! */
1101 if (!cnt || cnt >= CS_NAME_LEN)
1102 return -EINVAL;
1103
1104 /* strip of \n: */
1105 if (buf[cnt-1] == '\n')
1106 cnt--;
1107 if (cnt > 0)
1108 memcpy(dst, buf, cnt);
1109 dst[cnt] = 0;
1110 return ret;
1111}
1112
john stultz734efb42006-06-26 00:25:05 -07001113/**
Baolin Wange87821d2018-01-17 14:01:29 +08001114 * current_clocksource_store - interface for manually overriding clocksource
john stultz734efb42006-06-26 00:25:05 -07001115 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +09001116 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -07001117 * @buf: name of override clocksource
1118 * @count: length of buffer
1119 *
1120 * Takes input from sysfs interface for manually overriding the default
Uwe Kleine-Königb71a8eb2009-10-06 12:42:51 +02001121 * clocksource selection.
john stultz734efb42006-06-26 00:25:05 -07001122 */
Baolin Wange87821d2018-01-17 14:01:29 +08001123static ssize_t current_clocksource_store(struct device *dev,
1124 struct device_attribute *attr,
1125 const char *buf, size_t count)
john stultz734efb42006-06-26 00:25:05 -07001126{
Elad Wexler233bcb42013-09-12 13:28:54 +03001127 ssize_t ret;
john stultz734efb42006-06-26 00:25:05 -07001128
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001129 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001130
Thomas Gleixner03e13cf2013-04-25 20:31:50 +00001131 ret = sysfs_get_uname(buf, override_name, count);
Thomas Gleixner29b54072013-04-25 20:31:45 +00001132 if (ret >= 0)
1133 clocksource_select();
john stultz734efb42006-06-26 00:25:05 -07001134
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001135 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001136
1137 return ret;
1138}
Baolin Wange87821d2018-01-17 14:01:29 +08001139static DEVICE_ATTR_RW(current_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001140
1141/**
Baolin Wange87821d2018-01-17 14:01:29 +08001142 * unbind_clocksource_store - interface for manually unbinding clocksource
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001143 * @dev: unused
1144 * @attr: unused
1145 * @buf: unused
1146 * @count: length of buffer
1147 *
1148 * Takes input from sysfs interface for manually unbinding a clocksource.
1149 */
Baolin Wange87821d2018-01-17 14:01:29 +08001150static ssize_t unbind_clocksource_store(struct device *dev,
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001151 struct device_attribute *attr,
1152 const char *buf, size_t count)
1153{
1154 struct clocksource *cs;
1155 char name[CS_NAME_LEN];
Elad Wexler233bcb42013-09-12 13:28:54 +03001156 ssize_t ret;
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001157
Thomas Gleixner03e13cf2013-04-25 20:31:50 +00001158 ret = sysfs_get_uname(buf, name, count);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001159 if (ret < 0)
1160 return ret;
1161
1162 ret = -ENODEV;
1163 mutex_lock(&clocksource_mutex);
1164 list_for_each_entry(cs, &clocksource_list, list) {
1165 if (strcmp(cs->name, name))
1166 continue;
1167 ret = clocksource_unbind(cs);
1168 break;
1169 }
1170 mutex_unlock(&clocksource_mutex);
1171
1172 return ret ? ret : count;
1173}
Baolin Wange87821d2018-01-17 14:01:29 +08001174static DEVICE_ATTR_WO(unbind_clocksource);
Thomas Gleixner7eaeb342013-04-25 20:31:46 +00001175
1176/**
Baolin Wange87821d2018-01-17 14:01:29 +08001177 * available_clocksource_show - sysfs interface for listing clocksource
john stultz734efb42006-06-26 00:25:05 -07001178 * @dev: unused
Kusanagi Kouichib1b73d02011-12-19 18:13:19 +09001179 * @attr: unused
john stultz734efb42006-06-26 00:25:05 -07001180 * @buf: char buffer to be filled with clocksource list
1181 *
1182 * Provides sysfs interface for listing registered clocksources
1183 */
Baolin Wange87821d2018-01-17 14:01:29 +08001184static ssize_t available_clocksource_show(struct device *dev,
1185 struct device_attribute *attr,
1186 char *buf)
john stultz734efb42006-06-26 00:25:05 -07001187{
Matthias Kaehlcke2e197582007-10-18 23:39:58 -07001188 struct clocksource *src;
Miao Xie5e2cb102008-02-06 01:36:53 -08001189 ssize_t count = 0;
john stultz734efb42006-06-26 00:25:05 -07001190
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001191 mutex_lock(&clocksource_mutex);
Matthias Kaehlcke2e197582007-10-18 23:39:58 -07001192 list_for_each_entry(src, &clocksource_list, list) {
Thomas Gleixnercd6d95d2009-06-12 11:29:27 +02001193 /*
1194 * Don't show non-HRES clocksource if the tick code is
1195 * in one shot mode (highres=on or nohz=on)
1196 */
1197 if (!tick_oneshot_mode_active() ||
1198 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
john stultz3f685352009-01-21 22:53:22 -07001199 count += snprintf(buf + count,
Miao Xie5e2cb102008-02-06 01:36:53 -08001200 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1201 "%s ", src->name);
john stultz734efb42006-06-26 00:25:05 -07001202 }
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001203 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001204
Miao Xie5e2cb102008-02-06 01:36:53 -08001205 count += snprintf(buf + count,
1206 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
john stultz734efb42006-06-26 00:25:05 -07001207
Miao Xie5e2cb102008-02-06 01:36:53 -08001208 return count;
john stultz734efb42006-06-26 00:25:05 -07001209}
Baolin Wange87821d2018-01-17 14:01:29 +08001210static DEVICE_ATTR_RO(available_clocksource);
john stultz734efb42006-06-26 00:25:05 -07001211
Baolin Wang27263e82018-01-17 14:01:30 +08001212static struct attribute *clocksource_attrs[] = {
1213 &dev_attr_current_clocksource.attr,
1214 &dev_attr_unbind_clocksource.attr,
1215 &dev_attr_available_clocksource.attr,
1216 NULL
1217};
1218ATTRIBUTE_GROUPS(clocksource);
1219
Kay Sieversd369a5d2011-12-14 15:28:51 -08001220static struct bus_type clocksource_subsys = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01001221 .name = "clocksource",
Kay Sieversd369a5d2011-12-14 15:28:51 -08001222 .dev_name = "clocksource",
john stultz734efb42006-06-26 00:25:05 -07001223};
1224
Kay Sieversd369a5d2011-12-14 15:28:51 -08001225static struct device device_clocksource = {
john stultz734efb42006-06-26 00:25:05 -07001226 .id = 0,
Kay Sieversd369a5d2011-12-14 15:28:51 -08001227 .bus = &clocksource_subsys,
Baolin Wang27263e82018-01-17 14:01:30 +08001228 .groups = clocksource_groups,
john stultz734efb42006-06-26 00:25:05 -07001229};
1230
john stultzad596172006-06-26 00:25:06 -07001231static int __init init_clocksource_sysfs(void)
john stultz734efb42006-06-26 00:25:05 -07001232{
Kay Sieversd369a5d2011-12-14 15:28:51 -08001233 int error = subsys_system_register(&clocksource_subsys, NULL);
john stultz734efb42006-06-26 00:25:05 -07001234
1235 if (!error)
Kay Sieversd369a5d2011-12-14 15:28:51 -08001236 error = device_register(&device_clocksource);
Baolin Wang27263e82018-01-17 14:01:30 +08001237
john stultz734efb42006-06-26 00:25:05 -07001238 return error;
1239}
1240
1241device_initcall(init_clocksource_sysfs);
Daniel Walker2b013702006-12-10 02:21:30 -08001242#endif /* CONFIG_SYSFS */
john stultz734efb42006-06-26 00:25:05 -07001243
1244/**
1245 * boot_override_clocksource - boot clock override
1246 * @str: override name
1247 *
1248 * Takes a clocksource= boot argument and uses it
1249 * as the clocksource override name.
1250 */
1251static int __init boot_override_clocksource(char* str)
1252{
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001253 mutex_lock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001254 if (str)
1255 strlcpy(override_name, str, sizeof(override_name));
Martin Schwidefsky75c51582009-08-14 15:47:30 +02001256 mutex_unlock(&clocksource_mutex);
john stultz734efb42006-06-26 00:25:05 -07001257 return 1;
1258}
1259
1260__setup("clocksource=", boot_override_clocksource);
1261
1262/**
1263 * boot_override_clock - Compatibility layer for deprecated boot option
1264 * @str: override name
1265 *
1266 * DEPRECATED! Takes a clock= boot argument and uses it
1267 * as the clocksource override name
1268 */
1269static int __init boot_override_clock(char* str)
1270{
john stultz5d0cf412006-06-26 00:25:12 -07001271 if (!strcmp(str, "pmtmr")) {
Joe Perches45bbfe62015-05-25 11:49:55 -07001272 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
john stultz5d0cf412006-06-26 00:25:12 -07001273 return boot_override_clocksource("acpi_pm");
1274 }
Joe Perches45bbfe62015-05-25 11:49:55 -07001275 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
john stultz734efb42006-06-26 00:25:05 -07001276 return boot_override_clocksource(str);
1277}
1278
1279__setup("clock=", boot_override_clock);