1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * This file contains the functions which manage clocksource drivers.
4 *
5 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/device.h>
11 #include <linux/clocksource.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
15 #include <linux/tick.h>
16 #include <linux/kthread.h>
17
18 #include "tick-internal.h"
19 #include "timekeeping_internal.h"
20
21 /**
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
27 * @maxsec: guaranteed runtime conversion range in seconds
28 *
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 *
36 * The @maxsec conversion range argument controls the time frame in
37 * 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
41 * reduce the conversion accuracy by chosing smaller mult and shift
42 * factors.
43 */
44 void
clocks_calc_mult_shift(u32 * mult,u32 * shift,u32 from,u32 to,u32 maxsec)45 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
46 {
47 u64 tmp;
48 u32 sft, sftacc= 32;
49
50 /*
51 * Calculate the shift factor which is limiting the conversion
52 * range:
53 */
54 tmp = ((u64)maxsec * from) >> 32;
55 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;
66 tmp += from / 2;
67 do_div(tmp, from);
68 if ((tmp >> sftacc) == 0)
69 break;
70 }
71 *mult = tmp;
72 *shift = sft;
73 }
74 EXPORT_SYMBOL_GPL(clocks_calc_mult_shift);
75
76 /*[Clocksource internal variables]---------
77 * curr_clocksource:
78 * currently selected clocksource.
79 * suspend_clocksource:
80 * used to calculate the suspend time.
81 * clocksource_list:
82 * linked list with the registered clocksources
83 * clocksource_mutex:
84 * protects manipulations to curr_clocksource and the clocksource_list
85 * override_name:
86 * Name of the user-specified clocksource.
87 */
88 static struct clocksource *curr_clocksource;
89 static struct clocksource *suspend_clocksource;
90 static LIST_HEAD(clocksource_list);
91 static DEFINE_MUTEX(clocksource_mutex);
92 static char override_name[CS_NAME_LEN];
93 static int finished_booting;
94 static u64 suspend_start;
95
96 /*
97 * Threshold: 0.0312s, when doubled: 0.0625s.
98 * Also a default for cs->uncertainty_margin when registering clocks.
99 */
100 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 5)
101
102 /*
103 * Maximum permissible delay between two readouts of the watchdog
104 * clocksource surrounding a read of the clocksource being validated.
105 * This delay could be due to SMIs, NMIs, or to VCPU preemptions. Used as
106 * a lower bound for cs->uncertainty_margin values when registering clocks.
107 */
108 #define WATCHDOG_MAX_SKEW (100 * NSEC_PER_USEC)
109
110 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
111 static void clocksource_watchdog_work(struct work_struct *work);
112 static void clocksource_select(void);
113
114 static LIST_HEAD(watchdog_list);
115 static struct clocksource *watchdog;
116 static struct timer_list watchdog_timer;
117 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
118 static DEFINE_SPINLOCK(watchdog_lock);
119 static int watchdog_running;
120 static atomic_t watchdog_reset_pending;
121
clocksource_watchdog_lock(unsigned long * flags)122 static inline void clocksource_watchdog_lock(unsigned long *flags)
123 {
124 spin_lock_irqsave(&watchdog_lock, *flags);
125 }
126
clocksource_watchdog_unlock(unsigned long * flags)127 static inline void clocksource_watchdog_unlock(unsigned long *flags)
128 {
129 spin_unlock_irqrestore(&watchdog_lock, *flags);
130 }
131
132 static int clocksource_watchdog_kthread(void *data);
133 static void __clocksource_change_rating(struct clocksource *cs, int rating);
134
135 /*
136 * Interval: 0.5sec.
137 */
138 #define WATCHDOG_INTERVAL (HZ >> 1)
139
clocksource_watchdog_work(struct work_struct * work)140 static void clocksource_watchdog_work(struct work_struct *work)
141 {
142 /*
143 * We cannot directly run clocksource_watchdog_kthread() here, because
144 * clocksource_select() calls timekeeping_notify() which uses
145 * stop_machine(). One cannot use stop_machine() from a workqueue() due
146 * lock inversions wrt CPU hotplug.
147 *
148 * Also, we only ever run this work once or twice during the lifetime
149 * of the kernel, so there is no point in creating a more permanent
150 * kthread for this.
151 *
152 * If kthread_run fails the next watchdog scan over the
153 * watchdog_list will find the unstable clock again.
154 */
155 kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
156 }
157
__clocksource_unstable(struct clocksource * cs)158 static void __clocksource_unstable(struct clocksource *cs)
159 {
160 cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
161 cs->flags |= CLOCK_SOURCE_UNSTABLE;
162
163 /*
164 * If the clocksource is registered clocksource_watchdog_kthread() will
165 * re-rate and re-select.
166 */
167 if (list_empty(&cs->list)) {
168 cs->rating = 0;
169 return;
170 }
171
172 if (cs->mark_unstable)
173 cs->mark_unstable(cs);
174
175 /* kick clocksource_watchdog_kthread() */
176 if (finished_booting)
177 schedule_work(&watchdog_work);
178 }
179
180 /**
181 * clocksource_mark_unstable - mark clocksource unstable via watchdog
182 * @cs: clocksource to be marked unstable
183 *
184 * This function is called by the x86 TSC code to mark clocksources as unstable;
185 * it defers demotion and re-selection to a kthread.
186 */
clocksource_mark_unstable(struct clocksource * cs)187 void clocksource_mark_unstable(struct clocksource *cs)
188 {
189 unsigned long flags;
190
191 spin_lock_irqsave(&watchdog_lock, flags);
192 if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
193 if (!list_empty(&cs->list) && list_empty(&cs->wd_list))
194 list_add(&cs->wd_list, &watchdog_list);
195 __clocksource_unstable(cs);
196 }
197 spin_unlock_irqrestore(&watchdog_lock, flags);
198 }
199
200 static ulong max_cswd_read_retries = 3;
201 module_param(max_cswd_read_retries, ulong, 0644);
202
203 enum wd_read_status {
204 WD_READ_SUCCESS,
205 WD_READ_UNSTABLE,
206 WD_READ_SKIP
207 };
208
cs_watchdog_read(struct clocksource * cs,u64 * csnow,u64 * wdnow)209 static enum wd_read_status cs_watchdog_read(struct clocksource *cs, u64 *csnow, u64 *wdnow)
210 {
211 unsigned int nretries;
212 u64 wd_end, wd_end2, wd_delta;
213 int64_t wd_delay, wd_seq_delay;
214
215 for (nretries = 0; nretries <= max_cswd_read_retries; nretries++) {
216 local_irq_disable();
217 *wdnow = watchdog->read(watchdog);
218 *csnow = cs->read(cs);
219 wd_end = watchdog->read(watchdog);
220 wd_end2 = watchdog->read(watchdog);
221 local_irq_enable();
222
223 wd_delta = clocksource_delta(wd_end, *wdnow, watchdog->mask);
224 wd_delay = clocksource_cyc2ns(wd_delta, watchdog->mult,
225 watchdog->shift);
226 if (wd_delay <= WATCHDOG_MAX_SKEW) {
227 if (nretries > 1 || nretries >= max_cswd_read_retries) {
228 pr_warn("timekeeping watchdog on CPU%d: %s retried %d times before success\n",
229 smp_processor_id(), watchdog->name, nretries);
230 }
231 return WD_READ_SUCCESS;
232 }
233
234 /*
235 * Now compute delay in consecutive watchdog read to see if
236 * there is too much external interferences that cause
237 * significant delay in reading both clocksource and watchdog.
238 *
239 * If consecutive WD read-back delay > WATCHDOG_MAX_SKEW/2,
240 * report system busy, reinit the watchdog and skip the current
241 * watchdog test.
242 */
243 wd_delta = clocksource_delta(wd_end2, wd_end, watchdog->mask);
244 wd_seq_delay = clocksource_cyc2ns(wd_delta, watchdog->mult, watchdog->shift);
245 if (wd_seq_delay > WATCHDOG_MAX_SKEW/2)
246 goto skip_test;
247 }
248
249 pr_warn("timekeeping watchdog on CPU%d: %s read-back delay of %lldns, attempt %d, marking unstable\n",
250 smp_processor_id(), watchdog->name, wd_delay, nretries);
251 return WD_READ_UNSTABLE;
252
253 skip_test:
254 pr_info("timekeeping watchdog on CPU%d: %s wd-wd read-back delay of %lldns\n",
255 smp_processor_id(), watchdog->name, wd_seq_delay);
256 pr_info("wd-%s-wd read-back delay of %lldns, clock-skew test skipped!\n",
257 cs->name, wd_delay);
258 return WD_READ_SKIP;
259 }
260
261 static u64 csnow_mid;
262 static cpumask_t cpus_ahead;
263 static cpumask_t cpus_behind;
264
clocksource_verify_one_cpu(void * csin)265 static void clocksource_verify_one_cpu(void *csin)
266 {
267 struct clocksource *cs = (struct clocksource *)csin;
268
269 csnow_mid = cs->read(cs);
270 }
271
clocksource_verify_percpu(struct clocksource * cs)272 static void clocksource_verify_percpu(struct clocksource *cs)
273 {
274 int64_t cs_nsec, cs_nsec_max = 0, cs_nsec_min = LLONG_MAX;
275 u64 csnow_begin, csnow_end;
276 int cpu, testcpu;
277 s64 delta;
278
279 cpumask_clear(&cpus_ahead);
280 cpumask_clear(&cpus_behind);
281 preempt_disable();
282 testcpu = smp_processor_id();
283 pr_warn("Checking clocksource %s synchronization from CPU %d.\n", cs->name, testcpu);
284 for_each_online_cpu(cpu) {
285 if (cpu == testcpu)
286 continue;
287 csnow_begin = cs->read(cs);
288 smp_call_function_single(cpu, clocksource_verify_one_cpu, cs, 1);
289 csnow_end = cs->read(cs);
290 delta = (s64)((csnow_mid - csnow_begin) & cs->mask);
291 if (delta < 0)
292 cpumask_set_cpu(cpu, &cpus_behind);
293 delta = (csnow_end - csnow_mid) & cs->mask;
294 if (delta < 0)
295 cpumask_set_cpu(cpu, &cpus_ahead);
296 delta = clocksource_delta(csnow_end, csnow_begin, cs->mask);
297 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
298 if (cs_nsec > cs_nsec_max)
299 cs_nsec_max = cs_nsec;
300 if (cs_nsec < cs_nsec_min)
301 cs_nsec_min = cs_nsec;
302 }
303 preempt_enable();
304 if (!cpumask_empty(&cpus_ahead))
305 pr_warn(" CPUs %*pbl ahead of CPU %d for clocksource %s.\n",
306 cpumask_pr_args(&cpus_ahead), testcpu, cs->name);
307 if (!cpumask_empty(&cpus_behind))
308 pr_warn(" CPUs %*pbl behind CPU %d for clocksource %s.\n",
309 cpumask_pr_args(&cpus_behind), testcpu, cs->name);
310 if (!cpumask_empty(&cpus_ahead) || !cpumask_empty(&cpus_behind))
311 pr_warn(" CPU %d check durations %lldns - %lldns for clocksource %s.\n",
312 testcpu, cs_nsec_min, cs_nsec_max, cs->name);
313 }
314
clocksource_watchdog(struct timer_list * unused)315 static void clocksource_watchdog(struct timer_list *unused)
316 {
317 u64 csnow, wdnow, cslast, wdlast, delta;
318 int next_cpu, reset_pending;
319 int64_t wd_nsec, cs_nsec;
320 struct clocksource *cs;
321 enum wd_read_status read_ret;
322 u32 md;
323
324 spin_lock(&watchdog_lock);
325 if (!watchdog_running)
326 goto out;
327
328 reset_pending = atomic_read(&watchdog_reset_pending);
329
330 list_for_each_entry(cs, &watchdog_list, wd_list) {
331
332 /* Clocksource already marked unstable? */
333 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
334 if (finished_booting)
335 schedule_work(&watchdog_work);
336 continue;
337 }
338
339 read_ret = cs_watchdog_read(cs, &csnow, &wdnow);
340
341 if (read_ret != WD_READ_SUCCESS) {
342 if (read_ret == WD_READ_UNSTABLE)
343 /* Clock readout unreliable, so give it up. */
344 __clocksource_unstable(cs);
345 continue;
346 }
347
348 /* Clocksource initialized ? */
349 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
350 atomic_read(&watchdog_reset_pending)) {
351 cs->flags |= CLOCK_SOURCE_WATCHDOG;
352 cs->wd_last = wdnow;
353 cs->cs_last = csnow;
354 continue;
355 }
356
357 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
358 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
359 watchdog->shift);
360
361 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
362 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
363 wdlast = cs->wd_last; /* save these in case we print them */
364 cslast = cs->cs_last;
365 cs->cs_last = csnow;
366 cs->wd_last = wdnow;
367
368 if (atomic_read(&watchdog_reset_pending))
369 continue;
370
371 /* Check the deviation from the watchdog clocksource. */
372 md = cs->uncertainty_margin + watchdog->uncertainty_margin;
373 if (abs(cs_nsec - wd_nsec) > md) {
374 pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n",
375 smp_processor_id(), cs->name);
376 pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
377 watchdog->name, wdnow, wdlast, watchdog->mask);
378 pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
379 cs->name, csnow, cslast, cs->mask);
380 __clocksource_unstable(cs);
381 continue;
382 }
383
384 if (cs == curr_clocksource && cs->tick_stable)
385 cs->tick_stable(cs);
386
387 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
388 (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
389 (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
390 /* Mark it valid for high-res. */
391 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
392
393 /*
394 * clocksource_done_booting() will sort it if
395 * finished_booting is not set yet.
396 */
397 if (!finished_booting)
398 continue;
399
400 /*
401 * If this is not the current clocksource let
402 * the watchdog thread reselect it. Due to the
403 * change to high res this clocksource might
404 * be preferred now. If it is the current
405 * clocksource let the tick code know about
406 * that change.
407 */
408 if (cs != curr_clocksource) {
409 cs->flags |= CLOCK_SOURCE_RESELECT;
410 schedule_work(&watchdog_work);
411 } else {
412 tick_clock_notify();
413 }
414 }
415 }
416
417 /*
418 * We only clear the watchdog_reset_pending, when we did a
419 * full cycle through all clocksources.
420 */
421 if (reset_pending)
422 atomic_dec(&watchdog_reset_pending);
423
424 /*
425 * Cycle through CPUs to check if the CPUs stay synchronized
426 * to each other.
427 */
428 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
429 if (next_cpu >= nr_cpu_ids)
430 next_cpu = cpumask_first(cpu_online_mask);
431
432 /*
433 * Arm timer if not already pending: could race with concurrent
434 * pair clocksource_stop_watchdog() clocksource_start_watchdog().
435 */
436 if (!timer_pending(&watchdog_timer)) {
437 watchdog_timer.expires += WATCHDOG_INTERVAL;
438 add_timer_on(&watchdog_timer, next_cpu);
439 }
440 out:
441 spin_unlock(&watchdog_lock);
442 }
443
clocksource_start_watchdog(void)444 static inline void clocksource_start_watchdog(void)
445 {
446 if (watchdog_running || !watchdog || list_empty(&watchdog_list))
447 return;
448 timer_setup(&watchdog_timer, clocksource_watchdog, 0);
449 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
450 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
451 watchdog_running = 1;
452 }
453
clocksource_stop_watchdog(void)454 static inline void clocksource_stop_watchdog(void)
455 {
456 if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
457 return;
458 del_timer(&watchdog_timer);
459 watchdog_running = 0;
460 }
461
clocksource_reset_watchdog(void)462 static inline void clocksource_reset_watchdog(void)
463 {
464 struct clocksource *cs;
465
466 list_for_each_entry(cs, &watchdog_list, wd_list)
467 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
468 }
469
clocksource_resume_watchdog(void)470 static void clocksource_resume_watchdog(void)
471 {
472 atomic_inc(&watchdog_reset_pending);
473 }
474
clocksource_enqueue_watchdog(struct clocksource * cs)475 static void clocksource_enqueue_watchdog(struct clocksource *cs)
476 {
477 INIT_LIST_HEAD(&cs->wd_list);
478
479 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
480 /* cs is a clocksource to be watched. */
481 list_add(&cs->wd_list, &watchdog_list);
482 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
483 } else {
484 /* cs is a watchdog. */
485 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
486 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
487 }
488 }
489
clocksource_select_watchdog(bool fallback)490 static void clocksource_select_watchdog(bool fallback)
491 {
492 struct clocksource *cs, *old_wd;
493 unsigned long flags;
494
495 spin_lock_irqsave(&watchdog_lock, flags);
496 /* save current watchdog */
497 old_wd = watchdog;
498 if (fallback)
499 watchdog = NULL;
500
501 list_for_each_entry(cs, &clocksource_list, list) {
502 /* cs is a clocksource to be watched. */
503 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY)
504 continue;
505
506 /* Skip current if we were requested for a fallback. */
507 if (fallback && cs == old_wd)
508 continue;
509
510 /* Pick the best watchdog. */
511 if (!watchdog || cs->rating > watchdog->rating)
512 watchdog = cs;
513 }
514 /* If we failed to find a fallback restore the old one. */
515 if (!watchdog)
516 watchdog = old_wd;
517
518 /* If we changed the watchdog we need to reset cycles. */
519 if (watchdog != old_wd)
520 clocksource_reset_watchdog();
521
522 /* Check if the watchdog timer needs to be started. */
523 clocksource_start_watchdog();
524 spin_unlock_irqrestore(&watchdog_lock, flags);
525 }
526
clocksource_dequeue_watchdog(struct clocksource * cs)527 static void clocksource_dequeue_watchdog(struct clocksource *cs)
528 {
529 if (cs != watchdog) {
530 if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
531 /* cs is a watched clocksource. */
532 list_del_init(&cs->wd_list);
533 /* Check if the watchdog timer needs to be stopped. */
534 clocksource_stop_watchdog();
535 }
536 }
537 }
538
__clocksource_watchdog_kthread(void)539 static int __clocksource_watchdog_kthread(void)
540 {
541 struct clocksource *cs, *tmp;
542 unsigned long flags;
543 int select = 0;
544
545 /* Do any required per-CPU skew verification. */
546 if (curr_clocksource &&
547 curr_clocksource->flags & CLOCK_SOURCE_UNSTABLE &&
548 curr_clocksource->flags & CLOCK_SOURCE_VERIFY_PERCPU)
549 clocksource_verify_percpu(curr_clocksource);
550
551 spin_lock_irqsave(&watchdog_lock, flags);
552 list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
553 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
554 list_del_init(&cs->wd_list);
555 __clocksource_change_rating(cs, 0);
556 select = 1;
557 }
558 if (cs->flags & CLOCK_SOURCE_RESELECT) {
559 cs->flags &= ~CLOCK_SOURCE_RESELECT;
560 select = 1;
561 }
562 }
563 /* Check if the watchdog timer needs to be stopped. */
564 clocksource_stop_watchdog();
565 spin_unlock_irqrestore(&watchdog_lock, flags);
566
567 return select;
568 }
569
clocksource_watchdog_kthread(void * data)570 static int clocksource_watchdog_kthread(void *data)
571 {
572 mutex_lock(&clocksource_mutex);
573 if (__clocksource_watchdog_kthread())
574 clocksource_select();
575 mutex_unlock(&clocksource_mutex);
576 return 0;
577 }
578
clocksource_is_watchdog(struct clocksource * cs)579 static bool clocksource_is_watchdog(struct clocksource *cs)
580 {
581 return cs == watchdog;
582 }
583
584 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
585
clocksource_enqueue_watchdog(struct clocksource * cs)586 static void clocksource_enqueue_watchdog(struct clocksource *cs)
587 {
588 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
589 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
590 }
591
clocksource_select_watchdog(bool fallback)592 static void clocksource_select_watchdog(bool fallback) { }
clocksource_dequeue_watchdog(struct clocksource * cs)593 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
clocksource_resume_watchdog(void)594 static inline void clocksource_resume_watchdog(void) { }
__clocksource_watchdog_kthread(void)595 static inline int __clocksource_watchdog_kthread(void) { return 0; }
clocksource_is_watchdog(struct clocksource * cs)596 static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
clocksource_mark_unstable(struct clocksource * cs)597 void clocksource_mark_unstable(struct clocksource *cs) { }
598
clocksource_watchdog_lock(unsigned long * flags)599 static inline void clocksource_watchdog_lock(unsigned long *flags) { }
clocksource_watchdog_unlock(unsigned long * flags)600 static inline void clocksource_watchdog_unlock(unsigned long *flags) { }
601
602 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
603
clocksource_is_suspend(struct clocksource * cs)604 static bool clocksource_is_suspend(struct clocksource *cs)
605 {
606 return cs == suspend_clocksource;
607 }
608
__clocksource_suspend_select(struct clocksource * cs)609 static void __clocksource_suspend_select(struct clocksource *cs)
610 {
611 /*
612 * Skip the clocksource which will be stopped in suspend state.
613 */
614 if (!(cs->flags & CLOCK_SOURCE_SUSPEND_NONSTOP))
615 return;
616
617 /*
618 * The nonstop clocksource can be selected as the suspend clocksource to
619 * calculate the suspend time, so it should not supply suspend/resume
620 * interfaces to suspend the nonstop clocksource when system suspends.
621 */
622 if (cs->suspend || cs->resume) {
623 pr_warn("Nonstop clocksource %s should not supply suspend/resume interfaces\n",
624 cs->name);
625 }
626
627 /* Pick the best rating. */
628 if (!suspend_clocksource || cs->rating > suspend_clocksource->rating)
629 suspend_clocksource = cs;
630 }
631
632 /**
633 * clocksource_suspend_select - Select the best clocksource for suspend timing
634 * @fallback: if select a fallback clocksource
635 */
clocksource_suspend_select(bool fallback)636 static void clocksource_suspend_select(bool fallback)
637 {
638 struct clocksource *cs, *old_suspend;
639
640 old_suspend = suspend_clocksource;
641 if (fallback)
642 suspend_clocksource = NULL;
643
644 list_for_each_entry(cs, &clocksource_list, list) {
645 /* Skip current if we were requested for a fallback. */
646 if (fallback && cs == old_suspend)
647 continue;
648
649 __clocksource_suspend_select(cs);
650 }
651 }
652
653 /**
654 * clocksource_start_suspend_timing - Start measuring the suspend timing
655 * @cs: current clocksource from timekeeping
656 * @start_cycles: current cycles from timekeeping
657 *
658 * This function will save the start cycle values of suspend timer to calculate
659 * the suspend time when resuming system.
660 *
661 * This function is called late in the suspend process from timekeeping_suspend(),
662 * that means processes are freezed, non-boot cpus and interrupts are disabled
663 * now. It is therefore possible to start the suspend timer without taking the
664 * clocksource mutex.
665 */
clocksource_start_suspend_timing(struct clocksource * cs,u64 start_cycles)666 void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles)
667 {
668 if (!suspend_clocksource)
669 return;
670
671 /*
672 * If current clocksource is the suspend timer, we should use the
673 * tkr_mono.cycle_last value as suspend_start to avoid same reading
674 * from suspend timer.
675 */
676 if (clocksource_is_suspend(cs)) {
677 suspend_start = start_cycles;
678 return;
679 }
680
681 if (suspend_clocksource->enable &&
682 suspend_clocksource->enable(suspend_clocksource)) {
683 pr_warn_once("Failed to enable the non-suspend-able clocksource.\n");
684 return;
685 }
686
687 suspend_start = suspend_clocksource->read(suspend_clocksource);
688 }
689
690 /**
691 * clocksource_stop_suspend_timing - Stop measuring the suspend timing
692 * @cs: current clocksource from timekeeping
693 * @cycle_now: current cycles from timekeeping
694 *
695 * This function will calculate the suspend time from suspend timer.
696 *
697 * Returns nanoseconds since suspend started, 0 if no usable suspend clocksource.
698 *
699 * This function is called early in the resume process from timekeeping_resume(),
700 * that means there is only one cpu, no processes are running and the interrupts
701 * are disabled. It is therefore possible to stop the suspend timer without
702 * taking the clocksource mutex.
703 */
clocksource_stop_suspend_timing(struct clocksource * cs,u64 cycle_now)704 u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now)
705 {
706 u64 now, delta, nsec = 0;
707
708 if (!suspend_clocksource)
709 return 0;
710
711 /*
712 * If current clocksource is the suspend timer, we should use the
713 * tkr_mono.cycle_last value from timekeeping as current cycle to
714 * avoid same reading from suspend timer.
715 */
716 if (clocksource_is_suspend(cs))
717 now = cycle_now;
718 else
719 now = suspend_clocksource->read(suspend_clocksource);
720
721 if (now > suspend_start) {
722 delta = clocksource_delta(now, suspend_start,
723 suspend_clocksource->mask);
724 nsec = mul_u64_u32_shr(delta, suspend_clocksource->mult,
725 suspend_clocksource->shift);
726 }
727
728 /*
729 * Disable the suspend timer to save power if current clocksource is
730 * not the suspend timer.
731 */
732 if (!clocksource_is_suspend(cs) && suspend_clocksource->disable)
733 suspend_clocksource->disable(suspend_clocksource);
734
735 return nsec;
736 }
737
738 /**
739 * clocksource_suspend - suspend the clocksource(s)
740 */
clocksource_suspend(void)741 void clocksource_suspend(void)
742 {
743 struct clocksource *cs;
744
745 list_for_each_entry_reverse(cs, &clocksource_list, list)
746 if (cs->suspend)
747 cs->suspend(cs);
748 }
749
750 /**
751 * clocksource_resume - resume the clocksource(s)
752 */
clocksource_resume(void)753 void clocksource_resume(void)
754 {
755 struct clocksource *cs;
756
757 list_for_each_entry(cs, &clocksource_list, list)
758 if (cs->resume)
759 cs->resume(cs);
760
761 clocksource_resume_watchdog();
762 }
763
764 /**
765 * clocksource_touch_watchdog - Update watchdog
766 *
767 * Update the watchdog after exception contexts such as kgdb so as not
768 * to incorrectly trip the watchdog. This might fail when the kernel
769 * was stopped in code which holds watchdog_lock.
770 */
clocksource_touch_watchdog(void)771 void clocksource_touch_watchdog(void)
772 {
773 clocksource_resume_watchdog();
774 }
775
776 /**
777 * clocksource_max_adjustment- Returns max adjustment amount
778 * @cs: Pointer to clocksource
779 *
780 */
clocksource_max_adjustment(struct clocksource * cs)781 static u32 clocksource_max_adjustment(struct clocksource *cs)
782 {
783 u64 ret;
784 /*
785 * We won't try to correct for more than 11% adjustments (110,000 ppm),
786 */
787 ret = (u64)cs->mult * 11;
788 do_div(ret,100);
789 return (u32)ret;
790 }
791
792 /**
793 * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
794 * @mult: cycle to nanosecond multiplier
795 * @shift: cycle to nanosecond divisor (power of two)
796 * @maxadj: maximum adjustment value to mult (~11%)
797 * @mask: bitmask for two's complement subtraction of non 64 bit counters
798 * @max_cyc: maximum cycle value before potential overflow (does not include
799 * any safety margin)
800 *
801 * NOTE: This function includes a safety margin of 50%, in other words, we
802 * return half the number of nanoseconds the hardware counter can technically
803 * cover. This is done so that we can potentially detect problems caused by
804 * delayed timers or bad hardware, which might result in time intervals that
805 * are larger than what the math used can handle without overflows.
806 */
clocks_calc_max_nsecs(u32 mult,u32 shift,u32 maxadj,u64 mask,u64 * max_cyc)807 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
808 {
809 u64 max_nsecs, max_cycles;
810
811 /*
812 * Calculate the maximum number of cycles that we can pass to the
813 * cyc2ns() function without overflowing a 64-bit result.
814 */
815 max_cycles = ULLONG_MAX;
816 do_div(max_cycles, mult+maxadj);
817
818 /*
819 * The actual maximum number of cycles we can defer the clocksource is
820 * determined by the minimum of max_cycles and mask.
821 * Note: Here we subtract the maxadj to make sure we don't sleep for
822 * too long if there's a large negative adjustment.
823 */
824 max_cycles = min(max_cycles, mask);
825 max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
826
827 /* return the max_cycles value as well if requested */
828 if (max_cyc)
829 *max_cyc = max_cycles;
830
831 /* Return 50% of the actual maximum, so we can detect bad values */
832 max_nsecs >>= 1;
833
834 return max_nsecs;
835 }
836
837 /**
838 * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
839 * @cs: Pointer to clocksource to be updated
840 *
841 */
clocksource_update_max_deferment(struct clocksource * cs)842 static inline void clocksource_update_max_deferment(struct clocksource *cs)
843 {
844 cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
845 cs->maxadj, cs->mask,
846 &cs->max_cycles);
847 }
848
849 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
850
clocksource_find_best(bool oneshot,bool skipcur)851 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
852 {
853 struct clocksource *cs;
854
855 if (!finished_booting || list_empty(&clocksource_list))
856 return NULL;
857
858 /*
859 * We pick the clocksource with the highest rating. If oneshot
860 * mode is active, we pick the highres valid clocksource with
861 * the best rating.
862 */
863 list_for_each_entry(cs, &clocksource_list, list) {
864 if (skipcur && cs == curr_clocksource)
865 continue;
866 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
867 continue;
868 return cs;
869 }
870 return NULL;
871 }
872
__clocksource_select(bool skipcur)873 static void __clocksource_select(bool skipcur)
874 {
875 bool oneshot = tick_oneshot_mode_active();
876 struct clocksource *best, *cs;
877
878 /* Find the best suitable clocksource */
879 best = clocksource_find_best(oneshot, skipcur);
880 if (!best)
881 return;
882
883 if (!strlen(override_name))
884 goto found;
885
886 /* Check for the override clocksource. */
887 list_for_each_entry(cs, &clocksource_list, list) {
888 if (skipcur && cs == curr_clocksource)
889 continue;
890 if (strcmp(cs->name, override_name) != 0)
891 continue;
892 /*
893 * Check to make sure we don't switch to a non-highres
894 * capable clocksource if the tick code is in oneshot
895 * mode (highres or nohz)
896 */
897 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
898 /* Override clocksource cannot be used. */
899 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
900 pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n",
901 cs->name);
902 override_name[0] = 0;
903 } else {
904 /*
905 * The override cannot be currently verified.
906 * Deferring to let the watchdog check.
907 */
908 pr_info("Override clocksource %s is not currently HRT compatible - deferring\n",
909 cs->name);
910 }
911 } else
912 /* Override clocksource can be used. */
913 best = cs;
914 break;
915 }
916
917 found:
918 if (curr_clocksource != best && !timekeeping_notify(best)) {
919 pr_info("Switched to clocksource %s\n", best->name);
920 curr_clocksource = best;
921 }
922 }
923
924 /**
925 * clocksource_select - Select the best clocksource available
926 *
927 * Private function. Must hold clocksource_mutex when called.
928 *
929 * Select the clocksource with the best rating, or the clocksource,
930 * which is selected by userspace override.
931 */
clocksource_select(void)932 static void clocksource_select(void)
933 {
934 __clocksource_select(false);
935 }
936
clocksource_select_fallback(void)937 static void clocksource_select_fallback(void)
938 {
939 __clocksource_select(true);
940 }
941
942 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
clocksource_select(void)943 static inline void clocksource_select(void) { }
clocksource_select_fallback(void)944 static inline void clocksource_select_fallback(void) { }
945
946 #endif
947
948 /*
949 * clocksource_done_booting - Called near the end of core bootup
950 *
951 * Hack to avoid lots of clocksource churn at boot time.
952 * We use fs_initcall because we want this to start before
953 * device_initcall but after subsys_initcall.
954 */
clocksource_done_booting(void)955 static int __init clocksource_done_booting(void)
956 {
957 mutex_lock(&clocksource_mutex);
958 curr_clocksource = clocksource_default_clock();
959 finished_booting = 1;
960 /*
961 * Run the watchdog first to eliminate unstable clock sources
962 */
963 __clocksource_watchdog_kthread();
964 clocksource_select();
965 mutex_unlock(&clocksource_mutex);
966 return 0;
967 }
968 fs_initcall(clocksource_done_booting);
969
970 /*
971 * Enqueue the clocksource sorted by rating
972 */
clocksource_enqueue(struct clocksource * cs)973 static void clocksource_enqueue(struct clocksource *cs)
974 {
975 struct list_head *entry = &clocksource_list;
976 struct clocksource *tmp;
977
978 list_for_each_entry(tmp, &clocksource_list, list) {
979 /* Keep track of the place, where to insert */
980 if (tmp->rating < cs->rating)
981 break;
982 entry = &tmp->list;
983 }
984 list_add(&cs->list, entry);
985 }
986
987 /**
988 * __clocksource_update_freq_scale - Used update clocksource with new freq
989 * @cs: clocksource to be registered
990 * @scale: Scale factor multiplied against freq to get clocksource hz
991 * @freq: clocksource frequency (cycles per second) divided by scale
992 *
993 * This should only be called from the clocksource->enable() method.
994 *
995 * This *SHOULD NOT* be called directly! Please use the
996 * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
997 * functions.
998 */
__clocksource_update_freq_scale(struct clocksource * cs,u32 scale,u32 freq)999 void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
1000 {
1001 u64 sec;
1002
1003 /*
1004 * Default clocksources are *special* and self-define their mult/shift.
1005 * But, you're not special, so you should specify a freq value.
1006 */
1007 if (freq) {
1008 /*
1009 * Calc the maximum number of seconds which we can run before
1010 * wrapping around. For clocksources which have a mask > 32-bit
1011 * we need to limit the max sleep time to have a good
1012 * conversion precision. 10 minutes is still a reasonable
1013 * amount. That results in a shift value of 24 for a
1014 * clocksource with mask >= 40-bit and f >= 4GHz. That maps to
1015 * ~ 0.06ppm granularity for NTP.
1016 */
1017 sec = cs->mask;
1018 do_div(sec, freq);
1019 do_div(sec, scale);
1020 if (!sec)
1021 sec = 1;
1022 else if (sec > 600 && cs->mask > UINT_MAX)
1023 sec = 600;
1024
1025 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
1026 NSEC_PER_SEC / scale, sec * scale);
1027 }
1028
1029 /*
1030 * If the uncertainty margin is not specified, calculate it.
1031 * If both scale and freq are non-zero, calculate the clock
1032 * period, but bound below at 2*WATCHDOG_MAX_SKEW. However,
1033 * if either of scale or freq is zero, be very conservative and
1034 * take the tens-of-milliseconds WATCHDOG_THRESHOLD value for the
1035 * uncertainty margin. Allow stupidly small uncertainty margins
1036 * to be specified by the caller for testing purposes, but warn
1037 * to discourage production use of this capability.
1038 */
1039 if (scale && freq && !cs->uncertainty_margin) {
1040 cs->uncertainty_margin = NSEC_PER_SEC / (scale * freq);
1041 if (cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW)
1042 cs->uncertainty_margin = 2 * WATCHDOG_MAX_SKEW;
1043 } else if (!cs->uncertainty_margin) {
1044 cs->uncertainty_margin = WATCHDOG_THRESHOLD;
1045 }
1046 WARN_ON_ONCE(cs->uncertainty_margin < 2 * WATCHDOG_MAX_SKEW);
1047
1048 /*
1049 * Ensure clocksources that have large 'mult' values don't overflow
1050 * when adjusted.
1051 */
1052 cs->maxadj = clocksource_max_adjustment(cs);
1053 while (freq && ((cs->mult + cs->maxadj < cs->mult)
1054 || (cs->mult - cs->maxadj > cs->mult))) {
1055 cs->mult >>= 1;
1056 cs->shift--;
1057 cs->maxadj = clocksource_max_adjustment(cs);
1058 }
1059
1060 /*
1061 * Only warn for *special* clocksources that self-define
1062 * their mult/shift values and don't specify a freq.
1063 */
1064 WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
1065 "timekeeping: Clocksource %s might overflow on 11%% adjustment\n",
1066 cs->name);
1067
1068 clocksource_update_max_deferment(cs);
1069
1070 pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n",
1071 cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns);
1072 }
1073 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
1074
1075 /**
1076 * __clocksource_register_scale - Used to install new clocksources
1077 * @cs: clocksource to be registered
1078 * @scale: Scale factor multiplied against freq to get clocksource hz
1079 * @freq: clocksource frequency (cycles per second) divided by scale
1080 *
1081 * Returns -EBUSY if registration fails, zero otherwise.
1082 *
1083 * This *SHOULD NOT* be called directly! Please use the
1084 * clocksource_register_hz() or clocksource_register_khz helper functions.
1085 */
__clocksource_register_scale(struct clocksource * cs,u32 scale,u32 freq)1086 int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
1087 {
1088 unsigned long flags;
1089
1090 clocksource_arch_init(cs);
1091
1092 if (cs->vdso_clock_mode < 0 ||
1093 cs->vdso_clock_mode >= VDSO_CLOCKMODE_MAX) {
1094 pr_warn("clocksource %s registered with invalid VDSO mode %d. Disabling VDSO support.\n",
1095 cs->name, cs->vdso_clock_mode);
1096 cs->vdso_clock_mode = VDSO_CLOCKMODE_NONE;
1097 }
1098
1099 /* Initialize mult/shift and max_idle_ns */
1100 __clocksource_update_freq_scale(cs, scale, freq);
1101
1102 /* Add clocksource to the clocksource list */
1103 mutex_lock(&clocksource_mutex);
1104
1105 clocksource_watchdog_lock(&flags);
1106 clocksource_enqueue(cs);
1107 clocksource_enqueue_watchdog(cs);
1108 clocksource_watchdog_unlock(&flags);
1109
1110 clocksource_select();
1111 clocksource_select_watchdog(false);
1112 __clocksource_suspend_select(cs);
1113 mutex_unlock(&clocksource_mutex);
1114 return 0;
1115 }
1116 EXPORT_SYMBOL_GPL(__clocksource_register_scale);
1117
__clocksource_change_rating(struct clocksource * cs,int rating)1118 static void __clocksource_change_rating(struct clocksource *cs, int rating)
1119 {
1120 list_del(&cs->list);
1121 cs->rating = rating;
1122 clocksource_enqueue(cs);
1123 }
1124
1125 /**
1126 * clocksource_change_rating - Change the rating of a registered clocksource
1127 * @cs: clocksource to be changed
1128 * @rating: new rating
1129 */
clocksource_change_rating(struct clocksource * cs,int rating)1130 void clocksource_change_rating(struct clocksource *cs, int rating)
1131 {
1132 unsigned long flags;
1133
1134 mutex_lock(&clocksource_mutex);
1135 clocksource_watchdog_lock(&flags);
1136 __clocksource_change_rating(cs, rating);
1137 clocksource_watchdog_unlock(&flags);
1138
1139 clocksource_select();
1140 clocksource_select_watchdog(false);
1141 clocksource_suspend_select(false);
1142 mutex_unlock(&clocksource_mutex);
1143 }
1144 EXPORT_SYMBOL(clocksource_change_rating);
1145
1146 /*
1147 * Unbind clocksource @cs. Called with clocksource_mutex held
1148 */
clocksource_unbind(struct clocksource * cs)1149 static int clocksource_unbind(struct clocksource *cs)
1150 {
1151 unsigned long flags;
1152
1153 if (clocksource_is_watchdog(cs)) {
1154 /* Select and try to install a replacement watchdog. */
1155 clocksource_select_watchdog(true);
1156 if (clocksource_is_watchdog(cs))
1157 return -EBUSY;
1158 }
1159
1160 if (cs == curr_clocksource) {
1161 /* Select and try to install a replacement clock source */
1162 clocksource_select_fallback();
1163 if (curr_clocksource == cs)
1164 return -EBUSY;
1165 }
1166
1167 if (clocksource_is_suspend(cs)) {
1168 /*
1169 * Select and try to install a replacement suspend clocksource.
1170 * If no replacement suspend clocksource, we will just let the
1171 * clocksource go and have no suspend clocksource.
1172 */
1173 clocksource_suspend_select(true);
1174 }
1175
1176 clocksource_watchdog_lock(&flags);
1177 clocksource_dequeue_watchdog(cs);
1178 list_del_init(&cs->list);
1179 clocksource_watchdog_unlock(&flags);
1180
1181 return 0;
1182 }
1183
1184 /**
1185 * clocksource_unregister - remove a registered clocksource
1186 * @cs: clocksource to be unregistered
1187 */
clocksource_unregister(struct clocksource * cs)1188 int clocksource_unregister(struct clocksource *cs)
1189 {
1190 int ret = 0;
1191
1192 mutex_lock(&clocksource_mutex);
1193 if (!list_empty(&cs->list))
1194 ret = clocksource_unbind(cs);
1195 mutex_unlock(&clocksource_mutex);
1196 return ret;
1197 }
1198 EXPORT_SYMBOL(clocksource_unregister);
1199
1200 #ifdef CONFIG_SYSFS
1201 /**
1202 * current_clocksource_show - sysfs interface for current clocksource
1203 * @dev: unused
1204 * @attr: unused
1205 * @buf: char buffer to be filled with clocksource list
1206 *
1207 * Provides sysfs interface for listing current clocksource.
1208 */
current_clocksource_show(struct device * dev,struct device_attribute * attr,char * buf)1209 static ssize_t current_clocksource_show(struct device *dev,
1210 struct device_attribute *attr,
1211 char *buf)
1212 {
1213 ssize_t count = 0;
1214
1215 mutex_lock(&clocksource_mutex);
1216 count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
1217 mutex_unlock(&clocksource_mutex);
1218
1219 return count;
1220 }
1221
sysfs_get_uname(const char * buf,char * dst,size_t cnt)1222 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
1223 {
1224 size_t ret = cnt;
1225
1226 /* strings from sysfs write are not 0 terminated! */
1227 if (!cnt || cnt >= CS_NAME_LEN)
1228 return -EINVAL;
1229
1230 /* strip of \n: */
1231 if (buf[cnt-1] == '\n')
1232 cnt--;
1233 if (cnt > 0)
1234 memcpy(dst, buf, cnt);
1235 dst[cnt] = 0;
1236 return ret;
1237 }
1238
1239 /**
1240 * current_clocksource_store - interface for manually overriding clocksource
1241 * @dev: unused
1242 * @attr: unused
1243 * @buf: name of override clocksource
1244 * @count: length of buffer
1245 *
1246 * Takes input from sysfs interface for manually overriding the default
1247 * clocksource selection.
1248 */
current_clocksource_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1249 static ssize_t current_clocksource_store(struct device *dev,
1250 struct device_attribute *attr,
1251 const char *buf, size_t count)
1252 {
1253 ssize_t ret;
1254
1255 mutex_lock(&clocksource_mutex);
1256
1257 ret = sysfs_get_uname(buf, override_name, count);
1258 if (ret >= 0)
1259 clocksource_select();
1260
1261 mutex_unlock(&clocksource_mutex);
1262
1263 return ret;
1264 }
1265 static DEVICE_ATTR_RW(current_clocksource);
1266
1267 /**
1268 * unbind_clocksource_store - interface for manually unbinding clocksource
1269 * @dev: unused
1270 * @attr: unused
1271 * @buf: unused
1272 * @count: length of buffer
1273 *
1274 * Takes input from sysfs interface for manually unbinding a clocksource.
1275 */
unbind_clocksource_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1276 static ssize_t unbind_clocksource_store(struct device *dev,
1277 struct device_attribute *attr,
1278 const char *buf, size_t count)
1279 {
1280 struct clocksource *cs;
1281 char name[CS_NAME_LEN];
1282 ssize_t ret;
1283
1284 ret = sysfs_get_uname(buf, name, count);
1285 if (ret < 0)
1286 return ret;
1287
1288 ret = -ENODEV;
1289 mutex_lock(&clocksource_mutex);
1290 list_for_each_entry(cs, &clocksource_list, list) {
1291 if (strcmp(cs->name, name))
1292 continue;
1293 ret = clocksource_unbind(cs);
1294 break;
1295 }
1296 mutex_unlock(&clocksource_mutex);
1297
1298 return ret ? ret : count;
1299 }
1300 static DEVICE_ATTR_WO(unbind_clocksource);
1301
1302 /**
1303 * available_clocksource_show - sysfs interface for listing clocksource
1304 * @dev: unused
1305 * @attr: unused
1306 * @buf: char buffer to be filled with clocksource list
1307 *
1308 * Provides sysfs interface for listing registered clocksources
1309 */
available_clocksource_show(struct device * dev,struct device_attribute * attr,char * buf)1310 static ssize_t available_clocksource_show(struct device *dev,
1311 struct device_attribute *attr,
1312 char *buf)
1313 {
1314 struct clocksource *src;
1315 ssize_t count = 0;
1316
1317 mutex_lock(&clocksource_mutex);
1318 list_for_each_entry(src, &clocksource_list, list) {
1319 /*
1320 * Don't show non-HRES clocksource if the tick code is
1321 * in one shot mode (highres=on or nohz=on)
1322 */
1323 if (!tick_oneshot_mode_active() ||
1324 (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
1325 count += snprintf(buf + count,
1326 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1327 "%s ", src->name);
1328 }
1329 mutex_unlock(&clocksource_mutex);
1330
1331 count += snprintf(buf + count,
1332 max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
1333
1334 return count;
1335 }
1336 static DEVICE_ATTR_RO(available_clocksource);
1337
1338 static struct attribute *clocksource_attrs[] = {
1339 &dev_attr_current_clocksource.attr,
1340 &dev_attr_unbind_clocksource.attr,
1341 &dev_attr_available_clocksource.attr,
1342 NULL
1343 };
1344 ATTRIBUTE_GROUPS(clocksource);
1345
1346 static struct bus_type clocksource_subsys = {
1347 .name = "clocksource",
1348 .dev_name = "clocksource",
1349 };
1350
1351 static struct device device_clocksource = {
1352 .id = 0,
1353 .bus = &clocksource_subsys,
1354 .groups = clocksource_groups,
1355 };
1356
init_clocksource_sysfs(void)1357 static int __init init_clocksource_sysfs(void)
1358 {
1359 int error = subsys_system_register(&clocksource_subsys, NULL);
1360
1361 if (!error)
1362 error = device_register(&device_clocksource);
1363
1364 return error;
1365 }
1366
1367 device_initcall(init_clocksource_sysfs);
1368 #endif /* CONFIG_SYSFS */
1369
1370 /**
1371 * boot_override_clocksource - boot clock override
1372 * @str: override name
1373 *
1374 * Takes a clocksource= boot argument and uses it
1375 * as the clocksource override name.
1376 */
boot_override_clocksource(char * str)1377 static int __init boot_override_clocksource(char* str)
1378 {
1379 mutex_lock(&clocksource_mutex);
1380 if (str)
1381 strlcpy(override_name, str, sizeof(override_name));
1382 mutex_unlock(&clocksource_mutex);
1383 return 1;
1384 }
1385
1386 __setup("clocksource=", boot_override_clocksource);
1387
1388 /**
1389 * boot_override_clock - Compatibility layer for deprecated boot option
1390 * @str: override name
1391 *
1392 * DEPRECATED! Takes a clock= boot argument and uses it
1393 * as the clocksource override name
1394 */
boot_override_clock(char * str)1395 static int __init boot_override_clock(char* str)
1396 {
1397 if (!strcmp(str, "pmtmr")) {
1398 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
1399 return boot_override_clocksource("acpi_pm");
1400 }
1401 pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
1402 return boot_override_clocksource(str);
1403 }
1404
1405 __setup("clock=", boot_override_clock);
1406