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