xref: /openbmc/linux/kernel/time/clocksource.c (revision d65670a7)
1734efb46Sjohn stultz /*
2734efb46Sjohn stultz  * linux/kernel/time/clocksource.c
3734efb46Sjohn stultz  *
4734efb46Sjohn stultz  * This file contains the functions which manage clocksource drivers.
5734efb46Sjohn stultz  *
6734efb46Sjohn stultz  * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
7734efb46Sjohn stultz  *
8734efb46Sjohn stultz  * This program is free software; you can redistribute it and/or modify
9734efb46Sjohn stultz  * it under the terms of the GNU General Public License as published by
10734efb46Sjohn stultz  * the Free Software Foundation; either version 2 of the License, or
11734efb46Sjohn stultz  * (at your option) any later version.
12734efb46Sjohn stultz  *
13734efb46Sjohn stultz  * This program is distributed in the hope that it will be useful,
14734efb46Sjohn stultz  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15734efb46Sjohn stultz  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16734efb46Sjohn stultz  * GNU General Public License for more details.
17734efb46Sjohn stultz  *
18734efb46Sjohn stultz  * You should have received a copy of the GNU General Public License
19734efb46Sjohn stultz  * along with this program; if not, write to the Free Software
20734efb46Sjohn stultz  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21734efb46Sjohn stultz  *
22734efb46Sjohn stultz  * TODO WishList:
23734efb46Sjohn stultz  *   o Allow clocksource drivers to be unregistered
24734efb46Sjohn stultz  */
25734efb46Sjohn stultz 
26734efb46Sjohn stultz #include <linux/clocksource.h>
27734efb46Sjohn stultz #include <linux/sysdev.h>
28734efb46Sjohn stultz #include <linux/init.h>
29734efb46Sjohn stultz #include <linux/module.h>
30dc29a365SMathieu Desnoyers #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
3179bf2bb3SThomas Gleixner #include <linux/tick.h>
3201548f4dSMartin Schwidefsky #include <linux/kthread.h>
33734efb46Sjohn stultz 
34a038a353SPatrick Ohly void timecounter_init(struct timecounter *tc,
35a038a353SPatrick Ohly 		      const struct cyclecounter *cc,
36a038a353SPatrick Ohly 		      u64 start_tstamp)
37a038a353SPatrick Ohly {
38a038a353SPatrick Ohly 	tc->cc = cc;
39a038a353SPatrick Ohly 	tc->cycle_last = cc->read(cc);
40a038a353SPatrick Ohly 	tc->nsec = start_tstamp;
41a038a353SPatrick Ohly }
423586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_init);
43a038a353SPatrick Ohly 
44a038a353SPatrick Ohly /**
45a038a353SPatrick Ohly  * timecounter_read_delta - get nanoseconds since last call of this function
46a038a353SPatrick Ohly  * @tc:         Pointer to time counter
47a038a353SPatrick Ohly  *
48a038a353SPatrick Ohly  * When the underlying cycle counter runs over, this will be handled
49a038a353SPatrick Ohly  * correctly as long as it does not run over more than once between
50a038a353SPatrick Ohly  * calls.
51a038a353SPatrick Ohly  *
52a038a353SPatrick Ohly  * The first call to this function for a new time counter initializes
53a038a353SPatrick Ohly  * the time tracking and returns an undefined result.
54a038a353SPatrick Ohly  */
55a038a353SPatrick Ohly static u64 timecounter_read_delta(struct timecounter *tc)
56a038a353SPatrick Ohly {
57a038a353SPatrick Ohly 	cycle_t cycle_now, cycle_delta;
58a038a353SPatrick Ohly 	u64 ns_offset;
59a038a353SPatrick Ohly 
60a038a353SPatrick Ohly 	/* read cycle counter: */
61a038a353SPatrick Ohly 	cycle_now = tc->cc->read(tc->cc);
62a038a353SPatrick Ohly 
63a038a353SPatrick Ohly 	/* calculate the delta since the last timecounter_read_delta(): */
64a038a353SPatrick Ohly 	cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
65a038a353SPatrick Ohly 
66a038a353SPatrick Ohly 	/* convert to nanoseconds: */
67a038a353SPatrick Ohly 	ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
68a038a353SPatrick Ohly 
69a038a353SPatrick Ohly 	/* update time stamp of timecounter_read_delta() call: */
70a038a353SPatrick Ohly 	tc->cycle_last = cycle_now;
71a038a353SPatrick Ohly 
72a038a353SPatrick Ohly 	return ns_offset;
73a038a353SPatrick Ohly }
74a038a353SPatrick Ohly 
75a038a353SPatrick Ohly u64 timecounter_read(struct timecounter *tc)
76a038a353SPatrick Ohly {
77a038a353SPatrick Ohly 	u64 nsec;
78a038a353SPatrick Ohly 
79a038a353SPatrick Ohly 	/* increment time by nanoseconds since last call */
80a038a353SPatrick Ohly 	nsec = timecounter_read_delta(tc);
81a038a353SPatrick Ohly 	nsec += tc->nsec;
82a038a353SPatrick Ohly 	tc->nsec = nsec;
83a038a353SPatrick Ohly 
84a038a353SPatrick Ohly 	return nsec;
85a038a353SPatrick Ohly }
863586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_read);
87a038a353SPatrick Ohly 
88a038a353SPatrick Ohly u64 timecounter_cyc2time(struct timecounter *tc,
89a038a353SPatrick Ohly 			 cycle_t cycle_tstamp)
90a038a353SPatrick Ohly {
91a038a353SPatrick Ohly 	u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
92a038a353SPatrick Ohly 	u64 nsec;
93a038a353SPatrick Ohly 
94a038a353SPatrick Ohly 	/*
95a038a353SPatrick Ohly 	 * Instead of always treating cycle_tstamp as more recent
96a038a353SPatrick Ohly 	 * than tc->cycle_last, detect when it is too far in the
97a038a353SPatrick Ohly 	 * future and treat it as old time stamp instead.
98a038a353SPatrick Ohly 	 */
99a038a353SPatrick Ohly 	if (cycle_delta > tc->cc->mask / 2) {
100a038a353SPatrick Ohly 		cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
101a038a353SPatrick Ohly 		nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
102a038a353SPatrick Ohly 	} else {
103a038a353SPatrick Ohly 		nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
104a038a353SPatrick Ohly 	}
105a038a353SPatrick Ohly 
106a038a353SPatrick Ohly 	return nsec;
107a038a353SPatrick Ohly }
1083586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_cyc2time);
109a038a353SPatrick Ohly 
1107d2f944aSThomas Gleixner /**
1117d2f944aSThomas Gleixner  * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
1127d2f944aSThomas Gleixner  * @mult:	pointer to mult variable
1137d2f944aSThomas Gleixner  * @shift:	pointer to shift variable
1147d2f944aSThomas Gleixner  * @from:	frequency to convert from
1157d2f944aSThomas Gleixner  * @to:		frequency to convert to
1165fdade95SNicolas Pitre  * @maxsec:	guaranteed runtime conversion range in seconds
1177d2f944aSThomas Gleixner  *
1187d2f944aSThomas Gleixner  * The function evaluates the shift/mult pair for the scaled math
1197d2f944aSThomas Gleixner  * operations of clocksources and clockevents.
1207d2f944aSThomas Gleixner  *
1217d2f944aSThomas Gleixner  * @to and @from are frequency values in HZ. For clock sources @to is
1227d2f944aSThomas Gleixner  * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
1237d2f944aSThomas Gleixner  * event @to is the counter frequency and @from is NSEC_PER_SEC.
1247d2f944aSThomas Gleixner  *
1255fdade95SNicolas Pitre  * The @maxsec conversion range argument controls the time frame in
1267d2f944aSThomas Gleixner  * seconds which must be covered by the runtime conversion with the
1277d2f944aSThomas Gleixner  * calculated mult and shift factors. This guarantees that no 64bit
1287d2f944aSThomas Gleixner  * overflow happens when the input value of the conversion is
1297d2f944aSThomas Gleixner  * multiplied with the calculated mult factor. Larger ranges may
1307d2f944aSThomas Gleixner  * reduce the conversion accuracy by chosing smaller mult and shift
1317d2f944aSThomas Gleixner  * factors.
1327d2f944aSThomas Gleixner  */
1337d2f944aSThomas Gleixner void
1345fdade95SNicolas Pitre clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
1357d2f944aSThomas Gleixner {
1367d2f944aSThomas Gleixner 	u64 tmp;
1377d2f944aSThomas Gleixner 	u32 sft, sftacc= 32;
1387d2f944aSThomas Gleixner 
1397d2f944aSThomas Gleixner 	/*
1407d2f944aSThomas Gleixner 	 * Calculate the shift factor which is limiting the conversion
1417d2f944aSThomas Gleixner 	 * range:
1427d2f944aSThomas Gleixner 	 */
1435fdade95SNicolas Pitre 	tmp = ((u64)maxsec * from) >> 32;
1447d2f944aSThomas Gleixner 	while (tmp) {
1457d2f944aSThomas Gleixner 		tmp >>=1;
1467d2f944aSThomas Gleixner 		sftacc--;
1477d2f944aSThomas Gleixner 	}
1487d2f944aSThomas Gleixner 
1497d2f944aSThomas Gleixner 	/*
1507d2f944aSThomas Gleixner 	 * Find the conversion shift/mult pair which has the best
1517d2f944aSThomas Gleixner 	 * accuracy and fits the maxsec conversion range:
1527d2f944aSThomas Gleixner 	 */
1537d2f944aSThomas Gleixner 	for (sft = 32; sft > 0; sft--) {
1547d2f944aSThomas Gleixner 		tmp = (u64) to << sft;
155b5776c4aSjohn stultz 		tmp += from / 2;
1567d2f944aSThomas Gleixner 		do_div(tmp, from);
1577d2f944aSThomas Gleixner 		if ((tmp >> sftacc) == 0)
1587d2f944aSThomas Gleixner 			break;
1597d2f944aSThomas Gleixner 	}
1607d2f944aSThomas Gleixner 	*mult = tmp;
1617d2f944aSThomas Gleixner 	*shift = sft;
1627d2f944aSThomas Gleixner }
1637d2f944aSThomas Gleixner 
164734efb46Sjohn stultz /*[Clocksource internal variables]---------
165734efb46Sjohn stultz  * curr_clocksource:
166f1b82746SMartin Schwidefsky  *	currently selected clocksource.
167734efb46Sjohn stultz  * clocksource_list:
168734efb46Sjohn stultz  *	linked list with the registered clocksources
16975c5158fSMartin Schwidefsky  * clocksource_mutex:
17075c5158fSMartin Schwidefsky  *	protects manipulations to curr_clocksource and the clocksource_list
171734efb46Sjohn stultz  * override_name:
172734efb46Sjohn stultz  *	Name of the user-specified clocksource.
173734efb46Sjohn stultz  */
174f1b82746SMartin Schwidefsky static struct clocksource *curr_clocksource;
175734efb46Sjohn stultz static LIST_HEAD(clocksource_list);
17675c5158fSMartin Schwidefsky static DEFINE_MUTEX(clocksource_mutex);
177734efb46Sjohn stultz static char override_name[32];
17854a6bc0bSThomas Gleixner static int finished_booting;
179734efb46Sjohn stultz 
1805d8b34fdSThomas Gleixner #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
181f79e0258SMartin Schwidefsky static void clocksource_watchdog_work(struct work_struct *work);
182f79e0258SMartin Schwidefsky 
1835d8b34fdSThomas Gleixner static LIST_HEAD(watchdog_list);
1845d8b34fdSThomas Gleixner static struct clocksource *watchdog;
1855d8b34fdSThomas Gleixner static struct timer_list watchdog_timer;
186f79e0258SMartin Schwidefsky static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
1875d8b34fdSThomas Gleixner static DEFINE_SPINLOCK(watchdog_lock);
188fb63a0ebSMartin Schwidefsky static int watchdog_running;
1899fb60336SThomas Gleixner static atomic_t watchdog_reset_pending;
190b52f52a0SThomas Gleixner 
19101548f4dSMartin Schwidefsky static int clocksource_watchdog_kthread(void *data);
192d0981a1bSThomas Gleixner static void __clocksource_change_rating(struct clocksource *cs, int rating);
193c55c87c8SMartin Schwidefsky 
1945d8b34fdSThomas Gleixner /*
19535c35d1aSDaniel Walker  * Interval: 0.5sec Threshold: 0.0625s
1965d8b34fdSThomas Gleixner  */
1975d8b34fdSThomas Gleixner #define WATCHDOG_INTERVAL (HZ >> 1)
19835c35d1aSDaniel Walker #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
1995d8b34fdSThomas Gleixner 
20001548f4dSMartin Schwidefsky static void clocksource_watchdog_work(struct work_struct *work)
20101548f4dSMartin Schwidefsky {
20201548f4dSMartin Schwidefsky 	/*
20301548f4dSMartin Schwidefsky 	 * If kthread_run fails the next watchdog scan over the
20401548f4dSMartin Schwidefsky 	 * watchdog_list will find the unstable clock again.
20501548f4dSMartin Schwidefsky 	 */
20601548f4dSMartin Schwidefsky 	kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
20701548f4dSMartin Schwidefsky }
20801548f4dSMartin Schwidefsky 
2097285dd7fSThomas Gleixner static void __clocksource_unstable(struct clocksource *cs)
2107285dd7fSThomas Gleixner {
2117285dd7fSThomas Gleixner 	cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
2127285dd7fSThomas Gleixner 	cs->flags |= CLOCK_SOURCE_UNSTABLE;
21354a6bc0bSThomas Gleixner 	if (finished_booting)
2147285dd7fSThomas Gleixner 		schedule_work(&watchdog_work);
2157285dd7fSThomas Gleixner }
2167285dd7fSThomas Gleixner 
2178cf4e750SMartin Schwidefsky static void clocksource_unstable(struct clocksource *cs, int64_t delta)
2185d8b34fdSThomas Gleixner {
2195d8b34fdSThomas Gleixner 	printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
2205d8b34fdSThomas Gleixner 	       cs->name, delta);
2217285dd7fSThomas Gleixner 	__clocksource_unstable(cs);
2227285dd7fSThomas Gleixner }
2237285dd7fSThomas Gleixner 
2247285dd7fSThomas Gleixner /**
2257285dd7fSThomas Gleixner  * clocksource_mark_unstable - mark clocksource unstable via watchdog
2267285dd7fSThomas Gleixner  * @cs:		clocksource to be marked unstable
2277285dd7fSThomas Gleixner  *
2287285dd7fSThomas Gleixner  * This function is called instead of clocksource_change_rating from
2297285dd7fSThomas Gleixner  * cpu hotplug code to avoid a deadlock between the clocksource mutex
2307285dd7fSThomas Gleixner  * and the cpu hotplug mutex. It defers the update of the clocksource
2317285dd7fSThomas Gleixner  * to the watchdog thread.
2327285dd7fSThomas Gleixner  */
2337285dd7fSThomas Gleixner void clocksource_mark_unstable(struct clocksource *cs)
2347285dd7fSThomas Gleixner {
2357285dd7fSThomas Gleixner 	unsigned long flags;
2367285dd7fSThomas Gleixner 
2377285dd7fSThomas Gleixner 	spin_lock_irqsave(&watchdog_lock, flags);
2387285dd7fSThomas Gleixner 	if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
2397285dd7fSThomas Gleixner 		if (list_empty(&cs->wd_list))
2407285dd7fSThomas Gleixner 			list_add(&cs->wd_list, &watchdog_list);
2417285dd7fSThomas Gleixner 		__clocksource_unstable(cs);
2427285dd7fSThomas Gleixner 	}
2437285dd7fSThomas Gleixner 	spin_unlock_irqrestore(&watchdog_lock, flags);
2445d8b34fdSThomas Gleixner }
2455d8b34fdSThomas Gleixner 
2465d8b34fdSThomas Gleixner static void clocksource_watchdog(unsigned long data)
2475d8b34fdSThomas Gleixner {
248c55c87c8SMartin Schwidefsky 	struct clocksource *cs;
2495d8b34fdSThomas Gleixner 	cycle_t csnow, wdnow;
2505d8b34fdSThomas Gleixner 	int64_t wd_nsec, cs_nsec;
2519fb60336SThomas Gleixner 	int next_cpu, reset_pending;
2525d8b34fdSThomas Gleixner 
2535d8b34fdSThomas Gleixner 	spin_lock(&watchdog_lock);
254fb63a0ebSMartin Schwidefsky 	if (!watchdog_running)
255fb63a0ebSMartin Schwidefsky 		goto out;
2565d8b34fdSThomas Gleixner 
2579fb60336SThomas Gleixner 	reset_pending = atomic_read(&watchdog_reset_pending);
2589fb60336SThomas Gleixner 
259c55c87c8SMartin Schwidefsky 	list_for_each_entry(cs, &watchdog_list, wd_list) {
260c55c87c8SMartin Schwidefsky 
261c55c87c8SMartin Schwidefsky 		/* Clocksource already marked unstable? */
26201548f4dSMartin Schwidefsky 		if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
26354a6bc0bSThomas Gleixner 			if (finished_booting)
26401548f4dSMartin Schwidefsky 				schedule_work(&watchdog_work);
265c55c87c8SMartin Schwidefsky 			continue;
26601548f4dSMartin Schwidefsky 		}
267c55c87c8SMartin Schwidefsky 
268b5199515SThomas Gleixner 		local_irq_disable();
2698e19608eSMagnus Damm 		csnow = cs->read(cs);
270b5199515SThomas Gleixner 		wdnow = watchdog->read(watchdog);
271b5199515SThomas Gleixner 		local_irq_enable();
272b52f52a0SThomas Gleixner 
2738cf4e750SMartin Schwidefsky 		/* Clocksource initialized ? */
2749fb60336SThomas Gleixner 		if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
2759fb60336SThomas Gleixner 		    atomic_read(&watchdog_reset_pending)) {
2768cf4e750SMartin Schwidefsky 			cs->flags |= CLOCK_SOURCE_WATCHDOG;
277b5199515SThomas Gleixner 			cs->wd_last = wdnow;
278b5199515SThomas Gleixner 			cs->cs_last = csnow;
279b52f52a0SThomas Gleixner 			continue;
280b52f52a0SThomas Gleixner 		}
281b52f52a0SThomas Gleixner 
282b5199515SThomas Gleixner 		wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask,
283b5199515SThomas Gleixner 					     watchdog->mult, watchdog->shift);
284b5199515SThomas Gleixner 
285b5199515SThomas Gleixner 		cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
286155ec602SMartin Schwidefsky 					     cs->mask, cs->mult, cs->shift);
287b5199515SThomas Gleixner 		cs->cs_last = csnow;
288b5199515SThomas Gleixner 		cs->wd_last = wdnow;
289b5199515SThomas Gleixner 
2909fb60336SThomas Gleixner 		if (atomic_read(&watchdog_reset_pending))
2919fb60336SThomas Gleixner 			continue;
2929fb60336SThomas Gleixner 
293b5199515SThomas Gleixner 		/* Check the deviation from the watchdog clocksource. */
2949fb60336SThomas Gleixner 		if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
2958cf4e750SMartin Schwidefsky 			clocksource_unstable(cs, cs_nsec - wd_nsec);
2968cf4e750SMartin Schwidefsky 			continue;
2978cf4e750SMartin Schwidefsky 		}
2988cf4e750SMartin Schwidefsky 
2998cf4e750SMartin Schwidefsky 		if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
3008cf4e750SMartin Schwidefsky 		    (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
3015d8b34fdSThomas Gleixner 		    (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
3025d8b34fdSThomas Gleixner 			cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
30379bf2bb3SThomas Gleixner 			/*
3048cf4e750SMartin Schwidefsky 			 * We just marked the clocksource as highres-capable,
3058cf4e750SMartin Schwidefsky 			 * notify the rest of the system as well so that we
3068cf4e750SMartin Schwidefsky 			 * transition into high-res mode:
30779bf2bb3SThomas Gleixner 			 */
30879bf2bb3SThomas Gleixner 			tick_clock_notify();
3095d8b34fdSThomas Gleixner 		}
3105d8b34fdSThomas Gleixner 	}
3115d8b34fdSThomas Gleixner 
3126993fc5bSAndi Kleen 	/*
3139fb60336SThomas Gleixner 	 * We only clear the watchdog_reset_pending, when we did a
3149fb60336SThomas Gleixner 	 * full cycle through all clocksources.
3159fb60336SThomas Gleixner 	 */
3169fb60336SThomas Gleixner 	if (reset_pending)
3179fb60336SThomas Gleixner 		atomic_dec(&watchdog_reset_pending);
3189fb60336SThomas Gleixner 
3199fb60336SThomas Gleixner 	/*
320c55c87c8SMartin Schwidefsky 	 * Cycle through CPUs to check if the CPUs stay synchronized
321c55c87c8SMartin Schwidefsky 	 * to each other.
3226993fc5bSAndi Kleen 	 */
323c55c87c8SMartin Schwidefsky 	next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
324cad0e458SMike Travis 	if (next_cpu >= nr_cpu_ids)
3256b954823SRusty Russell 		next_cpu = cpumask_first(cpu_online_mask);
3266993fc5bSAndi Kleen 	watchdog_timer.expires += WATCHDOG_INTERVAL;
3276993fc5bSAndi Kleen 	add_timer_on(&watchdog_timer, next_cpu);
328fb63a0ebSMartin Schwidefsky out:
3295d8b34fdSThomas Gleixner 	spin_unlock(&watchdog_lock);
3305d8b34fdSThomas Gleixner }
3310f8e8ef7SMartin Schwidefsky 
332fb63a0ebSMartin Schwidefsky static inline void clocksource_start_watchdog(void)
333fb63a0ebSMartin Schwidefsky {
334fb63a0ebSMartin Schwidefsky 	if (watchdog_running || !watchdog || list_empty(&watchdog_list))
335fb63a0ebSMartin Schwidefsky 		return;
336fb63a0ebSMartin Schwidefsky 	init_timer(&watchdog_timer);
337fb63a0ebSMartin Schwidefsky 	watchdog_timer.function = clocksource_watchdog;
338fb63a0ebSMartin Schwidefsky 	watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
339fb63a0ebSMartin Schwidefsky 	add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
340fb63a0ebSMartin Schwidefsky 	watchdog_running = 1;
341fb63a0ebSMartin Schwidefsky }
342fb63a0ebSMartin Schwidefsky 
343fb63a0ebSMartin Schwidefsky static inline void clocksource_stop_watchdog(void)
344fb63a0ebSMartin Schwidefsky {
345fb63a0ebSMartin Schwidefsky 	if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
346fb63a0ebSMartin Schwidefsky 		return;
347fb63a0ebSMartin Schwidefsky 	del_timer(&watchdog_timer);
348fb63a0ebSMartin Schwidefsky 	watchdog_running = 0;
349fb63a0ebSMartin Schwidefsky }
350fb63a0ebSMartin Schwidefsky 
3510f8e8ef7SMartin Schwidefsky static inline void clocksource_reset_watchdog(void)
3520f8e8ef7SMartin Schwidefsky {
3530f8e8ef7SMartin Schwidefsky 	struct clocksource *cs;
3540f8e8ef7SMartin Schwidefsky 
3550f8e8ef7SMartin Schwidefsky 	list_for_each_entry(cs, &watchdog_list, wd_list)
3560f8e8ef7SMartin Schwidefsky 		cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
3570f8e8ef7SMartin Schwidefsky }
3580f8e8ef7SMartin Schwidefsky 
359b52f52a0SThomas Gleixner static void clocksource_resume_watchdog(void)
360b52f52a0SThomas Gleixner {
3619fb60336SThomas Gleixner 	atomic_inc(&watchdog_reset_pending);
362b52f52a0SThomas Gleixner }
363b52f52a0SThomas Gleixner 
364fb63a0ebSMartin Schwidefsky static void clocksource_enqueue_watchdog(struct clocksource *cs)
3655d8b34fdSThomas Gleixner {
3665d8b34fdSThomas Gleixner 	unsigned long flags;
3675d8b34fdSThomas Gleixner 
3685d8b34fdSThomas Gleixner 	spin_lock_irqsave(&watchdog_lock, flags);
3695d8b34fdSThomas Gleixner 	if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
370fb63a0ebSMartin Schwidefsky 		/* cs is a clocksource to be watched. */
3715d8b34fdSThomas Gleixner 		list_add(&cs->wd_list, &watchdog_list);
372fb63a0ebSMartin Schwidefsky 		cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
373948ac6d7SThomas Gleixner 	} else {
374fb63a0ebSMartin Schwidefsky 		/* cs is a watchdog. */
375948ac6d7SThomas Gleixner 		if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
3765d8b34fdSThomas Gleixner 			cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
377fb63a0ebSMartin Schwidefsky 		/* Pick the best watchdog. */
3785d8b34fdSThomas Gleixner 		if (!watchdog || cs->rating > watchdog->rating) {
3795d8b34fdSThomas Gleixner 			watchdog = cs;
3805d8b34fdSThomas Gleixner 			/* Reset watchdog cycles */
3810f8e8ef7SMartin Schwidefsky 			clocksource_reset_watchdog();
3825d8b34fdSThomas Gleixner 		}
3835d8b34fdSThomas Gleixner 	}
384fb63a0ebSMartin Schwidefsky 	/* Check if the watchdog timer needs to be started. */
385fb63a0ebSMartin Schwidefsky 	clocksource_start_watchdog();
3865d8b34fdSThomas Gleixner 	spin_unlock_irqrestore(&watchdog_lock, flags);
3875d8b34fdSThomas Gleixner }
388fb63a0ebSMartin Schwidefsky 
389fb63a0ebSMartin Schwidefsky static void clocksource_dequeue_watchdog(struct clocksource *cs)
390fb63a0ebSMartin Schwidefsky {
391fb63a0ebSMartin Schwidefsky 	struct clocksource *tmp;
392fb63a0ebSMartin Schwidefsky 	unsigned long flags;
393fb63a0ebSMartin Schwidefsky 
394fb63a0ebSMartin Schwidefsky 	spin_lock_irqsave(&watchdog_lock, flags);
395fb63a0ebSMartin Schwidefsky 	if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
396fb63a0ebSMartin Schwidefsky 		/* cs is a watched clocksource. */
397fb63a0ebSMartin Schwidefsky 		list_del_init(&cs->wd_list);
398fb63a0ebSMartin Schwidefsky 	} else if (cs == watchdog) {
399fb63a0ebSMartin Schwidefsky 		/* Reset watchdog cycles */
400fb63a0ebSMartin Schwidefsky 		clocksource_reset_watchdog();
401fb63a0ebSMartin Schwidefsky 		/* Current watchdog is removed. Find an alternative. */
402fb63a0ebSMartin Schwidefsky 		watchdog = NULL;
403fb63a0ebSMartin Schwidefsky 		list_for_each_entry(tmp, &clocksource_list, list) {
404fb63a0ebSMartin Schwidefsky 			if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY)
405fb63a0ebSMartin Schwidefsky 				continue;
406fb63a0ebSMartin Schwidefsky 			if (!watchdog || tmp->rating > watchdog->rating)
407fb63a0ebSMartin Schwidefsky 				watchdog = tmp;
408fb63a0ebSMartin Schwidefsky 		}
409fb63a0ebSMartin Schwidefsky 	}
410fb63a0ebSMartin Schwidefsky 	cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
411fb63a0ebSMartin Schwidefsky 	/* Check if the watchdog timer needs to be stopped. */
412fb63a0ebSMartin Schwidefsky 	clocksource_stop_watchdog();
413fb63a0ebSMartin Schwidefsky 	spin_unlock_irqrestore(&watchdog_lock, flags);
414fb63a0ebSMartin Schwidefsky }
415fb63a0ebSMartin Schwidefsky 
41601548f4dSMartin Schwidefsky static int clocksource_watchdog_kthread(void *data)
417c55c87c8SMartin Schwidefsky {
418c55c87c8SMartin Schwidefsky 	struct clocksource *cs, *tmp;
419c55c87c8SMartin Schwidefsky 	unsigned long flags;
4206ea41d25SThomas Gleixner 	LIST_HEAD(unstable);
421c55c87c8SMartin Schwidefsky 
422d0981a1bSThomas Gleixner 	mutex_lock(&clocksource_mutex);
423c55c87c8SMartin Schwidefsky 	spin_lock_irqsave(&watchdog_lock, flags);
424c55c87c8SMartin Schwidefsky 	list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list)
425c55c87c8SMartin Schwidefsky 		if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
426c55c87c8SMartin Schwidefsky 			list_del_init(&cs->wd_list);
4276ea41d25SThomas Gleixner 			list_add(&cs->wd_list, &unstable);
428c55c87c8SMartin Schwidefsky 		}
429c55c87c8SMartin Schwidefsky 	/* Check if the watchdog timer needs to be stopped. */
430c55c87c8SMartin Schwidefsky 	clocksource_stop_watchdog();
4316ea41d25SThomas Gleixner 	spin_unlock_irqrestore(&watchdog_lock, flags);
4326ea41d25SThomas Gleixner 
4336ea41d25SThomas Gleixner 	/* Needs to be done outside of watchdog lock */
4346ea41d25SThomas Gleixner 	list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
4356ea41d25SThomas Gleixner 		list_del_init(&cs->wd_list);
436d0981a1bSThomas Gleixner 		__clocksource_change_rating(cs, 0);
4376ea41d25SThomas Gleixner 	}
438d0981a1bSThomas Gleixner 	mutex_unlock(&clocksource_mutex);
43901548f4dSMartin Schwidefsky 	return 0;
440c55c87c8SMartin Schwidefsky }
441c55c87c8SMartin Schwidefsky 
442fb63a0ebSMartin Schwidefsky #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
443fb63a0ebSMartin Schwidefsky 
444fb63a0ebSMartin Schwidefsky static void clocksource_enqueue_watchdog(struct clocksource *cs)
4455d8b34fdSThomas Gleixner {
4465d8b34fdSThomas Gleixner 	if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
4475d8b34fdSThomas Gleixner 		cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
4485d8b34fdSThomas Gleixner }
449b52f52a0SThomas Gleixner 
450fb63a0ebSMartin Schwidefsky static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
451b52f52a0SThomas Gleixner static inline void clocksource_resume_watchdog(void) { }
45254a6bc0bSThomas Gleixner static inline int clocksource_watchdog_kthread(void *data) { return 0; }
453fb63a0ebSMartin Schwidefsky 
454fb63a0ebSMartin Schwidefsky #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
4555d8b34fdSThomas Gleixner 
456734efb46Sjohn stultz /**
457c54a42b1SMagnus Damm  * clocksource_suspend - suspend the clocksource(s)
458c54a42b1SMagnus Damm  */
459c54a42b1SMagnus Damm void clocksource_suspend(void)
460c54a42b1SMagnus Damm {
461c54a42b1SMagnus Damm 	struct clocksource *cs;
462c54a42b1SMagnus Damm 
463c54a42b1SMagnus Damm 	list_for_each_entry_reverse(cs, &clocksource_list, list)
464c54a42b1SMagnus Damm 		if (cs->suspend)
465c54a42b1SMagnus Damm 			cs->suspend(cs);
466c54a42b1SMagnus Damm }
467c54a42b1SMagnus Damm 
468c54a42b1SMagnus Damm /**
469b52f52a0SThomas Gleixner  * clocksource_resume - resume the clocksource(s)
470b52f52a0SThomas Gleixner  */
471b52f52a0SThomas Gleixner void clocksource_resume(void)
472b52f52a0SThomas Gleixner {
4732e197586SMatthias Kaehlcke 	struct clocksource *cs;
474b52f52a0SThomas Gleixner 
47575c5158fSMartin Schwidefsky 	list_for_each_entry(cs, &clocksource_list, list)
476b52f52a0SThomas Gleixner 		if (cs->resume)
47717622339SMagnus Damm 			cs->resume(cs);
478b52f52a0SThomas Gleixner 
479b52f52a0SThomas Gleixner 	clocksource_resume_watchdog();
480b52f52a0SThomas Gleixner }
481b52f52a0SThomas Gleixner 
482b52f52a0SThomas Gleixner /**
4837c3078b6SJason Wessel  * clocksource_touch_watchdog - Update watchdog
4847c3078b6SJason Wessel  *
4857c3078b6SJason Wessel  * Update the watchdog after exception contexts such as kgdb so as not
4867b7422a5SThomas Gleixner  * to incorrectly trip the watchdog. This might fail when the kernel
4877b7422a5SThomas Gleixner  * was stopped in code which holds watchdog_lock.
4887c3078b6SJason Wessel  */
4897c3078b6SJason Wessel void clocksource_touch_watchdog(void)
4907c3078b6SJason Wessel {
4917c3078b6SJason Wessel 	clocksource_resume_watchdog();
4927c3078b6SJason Wessel }
4937c3078b6SJason Wessel 
494734efb46Sjohn stultz /**
495*d65670a7SJohn Stultz  * clocksource_max_adjustment- Returns max adjustment amount
496*d65670a7SJohn Stultz  * @cs:         Pointer to clocksource
497*d65670a7SJohn Stultz  *
498*d65670a7SJohn Stultz  */
499*d65670a7SJohn Stultz static u32 clocksource_max_adjustment(struct clocksource *cs)
500*d65670a7SJohn Stultz {
501*d65670a7SJohn Stultz 	u64 ret;
502*d65670a7SJohn Stultz 	/*
503*d65670a7SJohn Stultz 	 * We won't try to correct for more then 11% adjustments (110,000 ppm),
504*d65670a7SJohn Stultz 	 */
505*d65670a7SJohn Stultz 	ret = (u64)cs->mult * 11;
506*d65670a7SJohn Stultz 	do_div(ret,100);
507*d65670a7SJohn Stultz 	return (u32)ret;
508*d65670a7SJohn Stultz }
509*d65670a7SJohn Stultz 
510*d65670a7SJohn Stultz /**
51198962465SJon Hunter  * clocksource_max_deferment - Returns max time the clocksource can be deferred
51298962465SJon Hunter  * @cs:         Pointer to clocksource
51398962465SJon Hunter  *
51498962465SJon Hunter  */
51598962465SJon Hunter static u64 clocksource_max_deferment(struct clocksource *cs)
51698962465SJon Hunter {
51798962465SJon Hunter 	u64 max_nsecs, max_cycles;
51898962465SJon Hunter 
51998962465SJon Hunter 	/*
52098962465SJon Hunter 	 * Calculate the maximum number of cycles that we can pass to the
52198962465SJon Hunter 	 * cyc2ns function without overflowing a 64-bit signed result. The
522*d65670a7SJohn Stultz 	 * maximum number of cycles is equal to ULLONG_MAX/(cs->mult+cs->maxadj)
523*d65670a7SJohn Stultz 	 * which is equivalent to the below.
524*d65670a7SJohn Stultz 	 * max_cycles < (2^63)/(cs->mult + cs->maxadj)
525*d65670a7SJohn Stultz 	 * max_cycles < 2^(log2((2^63)/(cs->mult + cs->maxadj)))
526*d65670a7SJohn Stultz 	 * max_cycles < 2^(log2(2^63) - log2(cs->mult + cs->maxadj))
527*d65670a7SJohn Stultz 	 * max_cycles < 2^(63 - log2(cs->mult + cs->maxadj))
528*d65670a7SJohn Stultz 	 * max_cycles < 1 << (63 - log2(cs->mult + cs->maxadj))
52998962465SJon Hunter 	 * Please note that we add 1 to the result of the log2 to account for
53098962465SJon Hunter 	 * any rounding errors, ensure the above inequality is satisfied and
53198962465SJon Hunter 	 * no overflow will occur.
53298962465SJon Hunter 	 */
533*d65670a7SJohn Stultz 	max_cycles = 1ULL << (63 - (ilog2(cs->mult + cs->maxadj) + 1));
53498962465SJon Hunter 
53598962465SJon Hunter 	/*
53698962465SJon Hunter 	 * The actual maximum number of cycles we can defer the clocksource is
53798962465SJon Hunter 	 * determined by the minimum of max_cycles and cs->mask.
538*d65670a7SJohn Stultz 	 * Note: Here we subtract the maxadj to make sure we don't sleep for
539*d65670a7SJohn Stultz 	 * too long if there's a large negative adjustment.
54098962465SJon Hunter 	 */
54198962465SJon Hunter 	max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
542*d65670a7SJohn Stultz 	max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult - cs->maxadj,
543*d65670a7SJohn Stultz 					cs->shift);
54498962465SJon Hunter 
54598962465SJon Hunter 	/*
54698962465SJon Hunter 	 * To ensure that the clocksource does not wrap whilst we are idle,
54798962465SJon Hunter 	 * limit the time the clocksource can be deferred by 12.5%. Please
54898962465SJon Hunter 	 * note a margin of 12.5% is used because this can be computed with
54998962465SJon Hunter 	 * a shift, versus say 10% which would require division.
55098962465SJon Hunter 	 */
55198962465SJon Hunter 	return max_nsecs - (max_nsecs >> 5);
55298962465SJon Hunter }
55398962465SJon Hunter 
554592913ecSJohn Stultz #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
555734efb46Sjohn stultz 
556734efb46Sjohn stultz /**
557f1b82746SMartin Schwidefsky  * clocksource_select - Select the best clocksource available
558734efb46Sjohn stultz  *
55975c5158fSMartin Schwidefsky  * Private function. Must hold clocksource_mutex when called.
560734efb46Sjohn stultz  *
56192c7e002SThomas Gleixner  * Select the clocksource with the best rating, or the clocksource,
56292c7e002SThomas Gleixner  * which is selected by userspace override.
563734efb46Sjohn stultz  */
564f1b82746SMartin Schwidefsky static void clocksource_select(void)
565734efb46Sjohn stultz {
566f1b82746SMartin Schwidefsky 	struct clocksource *best, *cs;
5675d8b34fdSThomas Gleixner 
56875c5158fSMartin Schwidefsky 	if (!finished_booting || list_empty(&clocksource_list))
569f1b82746SMartin Schwidefsky 		return;
570f1b82746SMartin Schwidefsky 	/* First clocksource on the list has the best rating. */
571f1b82746SMartin Schwidefsky 	best = list_first_entry(&clocksource_list, struct clocksource, list);
572f1b82746SMartin Schwidefsky 	/* Check for the override clocksource. */
573f1b82746SMartin Schwidefsky 	list_for_each_entry(cs, &clocksource_list, list) {
574f1b82746SMartin Schwidefsky 		if (strcmp(cs->name, override_name) != 0)
575f1b82746SMartin Schwidefsky 			continue;
576f1b82746SMartin Schwidefsky 		/*
577f1b82746SMartin Schwidefsky 		 * Check to make sure we don't switch to a non-highres
578f1b82746SMartin Schwidefsky 		 * capable clocksource if the tick code is in oneshot
579f1b82746SMartin Schwidefsky 		 * mode (highres or nohz)
580f1b82746SMartin Schwidefsky 		 */
581f1b82746SMartin Schwidefsky 		if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
582f1b82746SMartin Schwidefsky 		    tick_oneshot_mode_active()) {
583f1b82746SMartin Schwidefsky 			/* Override clocksource cannot be used. */
584f1b82746SMartin Schwidefsky 			printk(KERN_WARNING "Override clocksource %s is not "
585f1b82746SMartin Schwidefsky 			       "HRT compatible. Cannot switch while in "
586f1b82746SMartin Schwidefsky 			       "HRT/NOHZ mode\n", cs->name);
587f1b82746SMartin Schwidefsky 			override_name[0] = 0;
588f1b82746SMartin Schwidefsky 		} else
589f1b82746SMartin Schwidefsky 			/* Override clocksource can be used. */
590f1b82746SMartin Schwidefsky 			best = cs;
591f1b82746SMartin Schwidefsky 		break;
592734efb46Sjohn stultz 	}
59375c5158fSMartin Schwidefsky 	if (curr_clocksource != best) {
59475c5158fSMartin Schwidefsky 		printk(KERN_INFO "Switching to clocksource %s\n", best->name);
59575c5158fSMartin Schwidefsky 		curr_clocksource = best;
59675c5158fSMartin Schwidefsky 		timekeeping_notify(curr_clocksource);
597f1b82746SMartin Schwidefsky 	}
59875c5158fSMartin Schwidefsky }
59975c5158fSMartin Schwidefsky 
600592913ecSJohn Stultz #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
60154a6bc0bSThomas Gleixner 
60254a6bc0bSThomas Gleixner static inline void clocksource_select(void) { }
60354a6bc0bSThomas Gleixner 
60454a6bc0bSThomas Gleixner #endif
60554a6bc0bSThomas Gleixner 
60675c5158fSMartin Schwidefsky /*
60775c5158fSMartin Schwidefsky  * clocksource_done_booting - Called near the end of core bootup
60875c5158fSMartin Schwidefsky  *
60975c5158fSMartin Schwidefsky  * Hack to avoid lots of clocksource churn at boot time.
61075c5158fSMartin Schwidefsky  * We use fs_initcall because we want this to start before
61175c5158fSMartin Schwidefsky  * device_initcall but after subsys_initcall.
61275c5158fSMartin Schwidefsky  */
61375c5158fSMartin Schwidefsky static int __init clocksource_done_booting(void)
61475c5158fSMartin Schwidefsky {
615ad6759fbSjohn stultz 	mutex_lock(&clocksource_mutex);
616ad6759fbSjohn stultz 	curr_clocksource = clocksource_default_clock();
617ad6759fbSjohn stultz 	mutex_unlock(&clocksource_mutex);
618ad6759fbSjohn stultz 
61975c5158fSMartin Schwidefsky 	finished_booting = 1;
62054a6bc0bSThomas Gleixner 
62154a6bc0bSThomas Gleixner 	/*
62254a6bc0bSThomas Gleixner 	 * Run the watchdog first to eliminate unstable clock sources
62354a6bc0bSThomas Gleixner 	 */
62454a6bc0bSThomas Gleixner 	clocksource_watchdog_kthread(NULL);
62554a6bc0bSThomas Gleixner 
626e6c73305SThomas Gleixner 	mutex_lock(&clocksource_mutex);
62775c5158fSMartin Schwidefsky 	clocksource_select();
628e6c73305SThomas Gleixner 	mutex_unlock(&clocksource_mutex);
62975c5158fSMartin Schwidefsky 	return 0;
63075c5158fSMartin Schwidefsky }
63175c5158fSMartin Schwidefsky fs_initcall(clocksource_done_booting);
632f1b82746SMartin Schwidefsky 
63392c7e002SThomas Gleixner /*
63492c7e002SThomas Gleixner  * Enqueue the clocksource sorted by rating
635734efb46Sjohn stultz  */
636f1b82746SMartin Schwidefsky static void clocksource_enqueue(struct clocksource *cs)
637734efb46Sjohn stultz {
638f1b82746SMartin Schwidefsky 	struct list_head *entry = &clocksource_list;
639f1b82746SMartin Schwidefsky 	struct clocksource *tmp;
640734efb46Sjohn stultz 
641f1b82746SMartin Schwidefsky 	list_for_each_entry(tmp, &clocksource_list, list)
64292c7e002SThomas Gleixner 		/* Keep track of the place, where to insert */
643f1b82746SMartin Schwidefsky 		if (tmp->rating >= cs->rating)
644f1b82746SMartin Schwidefsky 			entry = &tmp->list;
645f1b82746SMartin Schwidefsky 	list_add(&cs->list, entry);
646734efb46Sjohn stultz }
647734efb46Sjohn stultz 
648d7e81c26SJohn Stultz /**
649852db46dSJohn Stultz  * __clocksource_updatefreq_scale - Used update clocksource with new freq
650852db46dSJohn Stultz  * @t:		clocksource to be registered
651852db46dSJohn Stultz  * @scale:	Scale factor multiplied against freq to get clocksource hz
652852db46dSJohn Stultz  * @freq:	clocksource frequency (cycles per second) divided by scale
653852db46dSJohn Stultz  *
654852db46dSJohn Stultz  * This should only be called from the clocksource->enable() method.
655852db46dSJohn Stultz  *
656852db46dSJohn Stultz  * This *SHOULD NOT* be called directly! Please use the
657852db46dSJohn Stultz  * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
658852db46dSJohn Stultz  */
659852db46dSJohn Stultz void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
660852db46dSJohn Stultz {
661c0e299b1SThomas Gleixner 	u64 sec;
662852db46dSJohn Stultz 	/*
663724ed53eSThomas Gleixner 	 * Calc the maximum number of seconds which we can run before
664724ed53eSThomas Gleixner 	 * wrapping around. For clocksources which have a mask > 32bit
665724ed53eSThomas Gleixner 	 * we need to limit the max sleep time to have a good
666724ed53eSThomas Gleixner 	 * conversion precision. 10 minutes is still a reasonable
667724ed53eSThomas Gleixner 	 * amount. That results in a shift value of 24 for a
668724ed53eSThomas Gleixner 	 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
669724ed53eSThomas Gleixner 	 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
670724ed53eSThomas Gleixner 	 * margin as we do in clocksource_max_deferment()
671852db46dSJohn Stultz 	 */
672724ed53eSThomas Gleixner 	sec = (cs->mask - (cs->mask >> 5));
673724ed53eSThomas Gleixner 	do_div(sec, freq);
674724ed53eSThomas Gleixner 	do_div(sec, scale);
675724ed53eSThomas Gleixner 	if (!sec)
676724ed53eSThomas Gleixner 		sec = 1;
677724ed53eSThomas Gleixner 	else if (sec > 600 && cs->mask > UINT_MAX)
678724ed53eSThomas Gleixner 		sec = 600;
679724ed53eSThomas Gleixner 
680852db46dSJohn Stultz 	clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
681724ed53eSThomas Gleixner 			       NSEC_PER_SEC / scale, sec * scale);
682*d65670a7SJohn Stultz 
683*d65670a7SJohn Stultz 	/*
684*d65670a7SJohn Stultz 	 * for clocksources that have large mults, to avoid overflow.
685*d65670a7SJohn Stultz 	 * Since mult may be adjusted by ntp, add an safety extra margin
686*d65670a7SJohn Stultz 	 *
687*d65670a7SJohn Stultz 	 */
688*d65670a7SJohn Stultz 	cs->maxadj = clocksource_max_adjustment(cs);
689*d65670a7SJohn Stultz 	while ((cs->mult + cs->maxadj < cs->mult)
690*d65670a7SJohn Stultz 		|| (cs->mult - cs->maxadj > cs->mult)) {
691*d65670a7SJohn Stultz 		cs->mult >>= 1;
692*d65670a7SJohn Stultz 		cs->shift--;
693*d65670a7SJohn Stultz 		cs->maxadj = clocksource_max_adjustment(cs);
694*d65670a7SJohn Stultz 	}
695*d65670a7SJohn Stultz 
696852db46dSJohn Stultz 	cs->max_idle_ns = clocksource_max_deferment(cs);
697852db46dSJohn Stultz }
698852db46dSJohn Stultz EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
699852db46dSJohn Stultz 
700852db46dSJohn Stultz /**
701d7e81c26SJohn Stultz  * __clocksource_register_scale - Used to install new clocksources
702d7e81c26SJohn Stultz  * @t:		clocksource to be registered
703d7e81c26SJohn Stultz  * @scale:	Scale factor multiplied against freq to get clocksource hz
704d7e81c26SJohn Stultz  * @freq:	clocksource frequency (cycles per second) divided by scale
705d7e81c26SJohn Stultz  *
706d7e81c26SJohn Stultz  * Returns -EBUSY if registration fails, zero otherwise.
707d7e81c26SJohn Stultz  *
708d7e81c26SJohn Stultz  * This *SHOULD NOT* be called directly! Please use the
709d7e81c26SJohn Stultz  * clocksource_register_hz() or clocksource_register_khz helper functions.
710d7e81c26SJohn Stultz  */
711d7e81c26SJohn Stultz int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
712d7e81c26SJohn Stultz {
713d7e81c26SJohn Stultz 
714b595076aSUwe Kleine-König 	/* Initialize mult/shift and max_idle_ns */
715852db46dSJohn Stultz 	__clocksource_updatefreq_scale(cs, scale, freq);
716d7e81c26SJohn Stultz 
717852db46dSJohn Stultz 	/* Add clocksource to the clcoksource list */
718d7e81c26SJohn Stultz 	mutex_lock(&clocksource_mutex);
719d7e81c26SJohn Stultz 	clocksource_enqueue(cs);
720d7e81c26SJohn Stultz 	clocksource_enqueue_watchdog(cs);
721e05b2efbSjohn stultz 	clocksource_select();
722d7e81c26SJohn Stultz 	mutex_unlock(&clocksource_mutex);
723d7e81c26SJohn Stultz 	return 0;
724d7e81c26SJohn Stultz }
725d7e81c26SJohn Stultz EXPORT_SYMBOL_GPL(__clocksource_register_scale);
726d7e81c26SJohn Stultz 
727d7e81c26SJohn Stultz 
728734efb46Sjohn stultz /**
729a2752549Sjohn stultz  * clocksource_register - Used to install new clocksources
730734efb46Sjohn stultz  * @t:		clocksource to be registered
731734efb46Sjohn stultz  *
732734efb46Sjohn stultz  * Returns -EBUSY if registration fails, zero otherwise.
733734efb46Sjohn stultz  */
734f1b82746SMartin Schwidefsky int clocksource_register(struct clocksource *cs)
735734efb46Sjohn stultz {
736*d65670a7SJohn Stultz 	/* calculate max adjustment for given mult/shift */
737*d65670a7SJohn Stultz 	cs->maxadj = clocksource_max_adjustment(cs);
738*d65670a7SJohn Stultz 	WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
739*d65670a7SJohn Stultz 		"Clocksource %s might overflow on 11%% adjustment\n",
740*d65670a7SJohn Stultz 		cs->name);
741*d65670a7SJohn Stultz 
74298962465SJon Hunter 	/* calculate max idle time permitted for this clocksource */
74398962465SJon Hunter 	cs->max_idle_ns = clocksource_max_deferment(cs);
74498962465SJon Hunter 
74575c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
746f1b82746SMartin Schwidefsky 	clocksource_enqueue(cs);
747fb63a0ebSMartin Schwidefsky 	clocksource_enqueue_watchdog(cs);
748e05b2efbSjohn stultz 	clocksource_select();
74975c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
750f1b82746SMartin Schwidefsky 	return 0;
751734efb46Sjohn stultz }
752a2752549Sjohn stultz EXPORT_SYMBOL(clocksource_register);
753734efb46Sjohn stultz 
754d0981a1bSThomas Gleixner static void __clocksource_change_rating(struct clocksource *cs, int rating)
755d0981a1bSThomas Gleixner {
756d0981a1bSThomas Gleixner 	list_del(&cs->list);
757d0981a1bSThomas Gleixner 	cs->rating = rating;
758d0981a1bSThomas Gleixner 	clocksource_enqueue(cs);
759d0981a1bSThomas Gleixner 	clocksource_select();
760d0981a1bSThomas Gleixner }
761d0981a1bSThomas Gleixner 
762734efb46Sjohn stultz /**
76392c7e002SThomas Gleixner  * clocksource_change_rating - Change the rating of a registered clocksource
764734efb46Sjohn stultz  */
76592c7e002SThomas Gleixner void clocksource_change_rating(struct clocksource *cs, int rating)
766734efb46Sjohn stultz {
76775c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
768d0981a1bSThomas Gleixner 	__clocksource_change_rating(cs, rating);
76975c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
770734efb46Sjohn stultz }
771fb63a0ebSMartin Schwidefsky EXPORT_SYMBOL(clocksource_change_rating);
772734efb46Sjohn stultz 
7734713e22cSThomas Gleixner /**
7744713e22cSThomas Gleixner  * clocksource_unregister - remove a registered clocksource
7754713e22cSThomas Gleixner  */
7764713e22cSThomas Gleixner void clocksource_unregister(struct clocksource *cs)
7774713e22cSThomas Gleixner {
77875c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
779fb63a0ebSMartin Schwidefsky 	clocksource_dequeue_watchdog(cs);
7804713e22cSThomas Gleixner 	list_del(&cs->list);
781f1b82746SMartin Schwidefsky 	clocksource_select();
78275c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
7834713e22cSThomas Gleixner }
784fb63a0ebSMartin Schwidefsky EXPORT_SYMBOL(clocksource_unregister);
7854713e22cSThomas Gleixner 
7862b013700SDaniel Walker #ifdef CONFIG_SYSFS
787734efb46Sjohn stultz /**
788734efb46Sjohn stultz  * sysfs_show_current_clocksources - sysfs interface for current clocksource
789734efb46Sjohn stultz  * @dev:	unused
790734efb46Sjohn stultz  * @buf:	char buffer to be filled with clocksource list
791734efb46Sjohn stultz  *
792734efb46Sjohn stultz  * Provides sysfs interface for listing current clocksource.
793734efb46Sjohn stultz  */
794734efb46Sjohn stultz static ssize_t
7954a0b2b4dSAndi Kleen sysfs_show_current_clocksources(struct sys_device *dev,
7964a0b2b4dSAndi Kleen 				struct sysdev_attribute *attr, char *buf)
797734efb46Sjohn stultz {
7985e2cb101SMiao Xie 	ssize_t count = 0;
799734efb46Sjohn stultz 
80075c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
8015e2cb101SMiao Xie 	count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
80275c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
803734efb46Sjohn stultz 
8045e2cb101SMiao Xie 	return count;
805734efb46Sjohn stultz }
806734efb46Sjohn stultz 
807734efb46Sjohn stultz /**
808734efb46Sjohn stultz  * sysfs_override_clocksource - interface for manually overriding clocksource
809734efb46Sjohn stultz  * @dev:	unused
810734efb46Sjohn stultz  * @buf:	name of override clocksource
811734efb46Sjohn stultz  * @count:	length of buffer
812734efb46Sjohn stultz  *
813734efb46Sjohn stultz  * Takes input from sysfs interface for manually overriding the default
814b71a8eb0SUwe Kleine-König  * clocksource selection.
815734efb46Sjohn stultz  */
816734efb46Sjohn stultz static ssize_t sysfs_override_clocksource(struct sys_device *dev,
8174a0b2b4dSAndi Kleen 					  struct sysdev_attribute *attr,
818734efb46Sjohn stultz 					  const char *buf, size_t count)
819734efb46Sjohn stultz {
820734efb46Sjohn stultz 	size_t ret = count;
82192c7e002SThomas Gleixner 
822734efb46Sjohn stultz 	/* strings from sysfs write are not 0 terminated! */
823734efb46Sjohn stultz 	if (count >= sizeof(override_name))
824734efb46Sjohn stultz 		return -EINVAL;
825734efb46Sjohn stultz 
826734efb46Sjohn stultz 	/* strip of \n: */
827734efb46Sjohn stultz 	if (buf[count-1] == '\n')
828734efb46Sjohn stultz 		count--;
829734efb46Sjohn stultz 
83075c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
831734efb46Sjohn stultz 
83292c7e002SThomas Gleixner 	if (count > 0)
833734efb46Sjohn stultz 		memcpy(override_name, buf, count);
834734efb46Sjohn stultz 	override_name[count] = 0;
835f1b82746SMartin Schwidefsky 	clocksource_select();
836734efb46Sjohn stultz 
83775c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
838734efb46Sjohn stultz 
839734efb46Sjohn stultz 	return ret;
840734efb46Sjohn stultz }
841734efb46Sjohn stultz 
842734efb46Sjohn stultz /**
843734efb46Sjohn stultz  * sysfs_show_available_clocksources - sysfs interface for listing clocksource
844734efb46Sjohn stultz  * @dev:	unused
845734efb46Sjohn stultz  * @buf:	char buffer to be filled with clocksource list
846734efb46Sjohn stultz  *
847734efb46Sjohn stultz  * Provides sysfs interface for listing registered clocksources
848734efb46Sjohn stultz  */
849734efb46Sjohn stultz static ssize_t
8504a0b2b4dSAndi Kleen sysfs_show_available_clocksources(struct sys_device *dev,
8514a0b2b4dSAndi Kleen 				  struct sysdev_attribute *attr,
8524a0b2b4dSAndi Kleen 				  char *buf)
853734efb46Sjohn stultz {
8542e197586SMatthias Kaehlcke 	struct clocksource *src;
8555e2cb101SMiao Xie 	ssize_t count = 0;
856734efb46Sjohn stultz 
85775c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
8582e197586SMatthias Kaehlcke 	list_for_each_entry(src, &clocksource_list, list) {
859cd6d95d8SThomas Gleixner 		/*
860cd6d95d8SThomas Gleixner 		 * Don't show non-HRES clocksource if the tick code is
861cd6d95d8SThomas Gleixner 		 * in one shot mode (highres=on or nohz=on)
862cd6d95d8SThomas Gleixner 		 */
863cd6d95d8SThomas Gleixner 		if (!tick_oneshot_mode_active() ||
8643f68535aSjohn stultz 		    (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
8655e2cb101SMiao Xie 			count += snprintf(buf + count,
8665e2cb101SMiao Xie 				  max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
8675e2cb101SMiao Xie 				  "%s ", src->name);
868734efb46Sjohn stultz 	}
86975c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
870734efb46Sjohn stultz 
8715e2cb101SMiao Xie 	count += snprintf(buf + count,
8725e2cb101SMiao Xie 			  max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
873734efb46Sjohn stultz 
8745e2cb101SMiao Xie 	return count;
875734efb46Sjohn stultz }
876734efb46Sjohn stultz 
877734efb46Sjohn stultz /*
878734efb46Sjohn stultz  * Sysfs setup bits:
879734efb46Sjohn stultz  */
8804f95f81aSHeiko Carstens static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
881734efb46Sjohn stultz 		   sysfs_override_clocksource);
882734efb46Sjohn stultz 
8834f95f81aSHeiko Carstens static SYSDEV_ATTR(available_clocksource, 0444,
884734efb46Sjohn stultz 		   sysfs_show_available_clocksources, NULL);
885734efb46Sjohn stultz 
886734efb46Sjohn stultz static struct sysdev_class clocksource_sysclass = {
887af5ca3f4SKay Sievers 	.name = "clocksource",
888734efb46Sjohn stultz };
889734efb46Sjohn stultz 
890734efb46Sjohn stultz static struct sys_device device_clocksource = {
891734efb46Sjohn stultz 	.id	= 0,
892734efb46Sjohn stultz 	.cls	= &clocksource_sysclass,
893734efb46Sjohn stultz };
894734efb46Sjohn stultz 
895ad596171Sjohn stultz static int __init init_clocksource_sysfs(void)
896734efb46Sjohn stultz {
897734efb46Sjohn stultz 	int error = sysdev_class_register(&clocksource_sysclass);
898734efb46Sjohn stultz 
899734efb46Sjohn stultz 	if (!error)
900734efb46Sjohn stultz 		error = sysdev_register(&device_clocksource);
901734efb46Sjohn stultz 	if (!error)
902734efb46Sjohn stultz 		error = sysdev_create_file(
903734efb46Sjohn stultz 				&device_clocksource,
904734efb46Sjohn stultz 				&attr_current_clocksource);
905734efb46Sjohn stultz 	if (!error)
906734efb46Sjohn stultz 		error = sysdev_create_file(
907734efb46Sjohn stultz 				&device_clocksource,
908734efb46Sjohn stultz 				&attr_available_clocksource);
909734efb46Sjohn stultz 	return error;
910734efb46Sjohn stultz }
911734efb46Sjohn stultz 
912734efb46Sjohn stultz device_initcall(init_clocksource_sysfs);
9132b013700SDaniel Walker #endif /* CONFIG_SYSFS */
914734efb46Sjohn stultz 
915734efb46Sjohn stultz /**
916734efb46Sjohn stultz  * boot_override_clocksource - boot clock override
917734efb46Sjohn stultz  * @str:	override name
918734efb46Sjohn stultz  *
919734efb46Sjohn stultz  * Takes a clocksource= boot argument and uses it
920734efb46Sjohn stultz  * as the clocksource override name.
921734efb46Sjohn stultz  */
922734efb46Sjohn stultz static int __init boot_override_clocksource(char* str)
923734efb46Sjohn stultz {
92475c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
925734efb46Sjohn stultz 	if (str)
926734efb46Sjohn stultz 		strlcpy(override_name, str, sizeof(override_name));
92775c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
928734efb46Sjohn stultz 	return 1;
929734efb46Sjohn stultz }
930734efb46Sjohn stultz 
931734efb46Sjohn stultz __setup("clocksource=", boot_override_clocksource);
932734efb46Sjohn stultz 
933734efb46Sjohn stultz /**
934734efb46Sjohn stultz  * boot_override_clock - Compatibility layer for deprecated boot option
935734efb46Sjohn stultz  * @str:	override name
936734efb46Sjohn stultz  *
937734efb46Sjohn stultz  * DEPRECATED! Takes a clock= boot argument and uses it
938734efb46Sjohn stultz  * as the clocksource override name
939734efb46Sjohn stultz  */
940734efb46Sjohn stultz static int __init boot_override_clock(char* str)
941734efb46Sjohn stultz {
9425d0cf410Sjohn stultz 	if (!strcmp(str, "pmtmr")) {
9435d0cf410Sjohn stultz 		printk("Warning: clock=pmtmr is deprecated. "
9445d0cf410Sjohn stultz 			"Use clocksource=acpi_pm.\n");
9455d0cf410Sjohn stultz 		return boot_override_clocksource("acpi_pm");
9465d0cf410Sjohn stultz 	}
9475d0cf410Sjohn stultz 	printk("Warning! clock= boot option is deprecated. "
9485d0cf410Sjohn stultz 		"Use clocksource=xyz\n");
949734efb46Sjohn stultz 	return boot_override_clocksource(str);
950734efb46Sjohn stultz }
951734efb46Sjohn stultz 
952734efb46Sjohn stultz __setup("clock=", boot_override_clock);
953