xref: /openbmc/linux/kernel/time/clocksource.c (revision 891292a7)
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 
26d369a5d8SKay Sievers #include <linux/device.h>
27734efb46Sjohn stultz #include <linux/clocksource.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 
3403e13cf5SThomas Gleixner #include "tick-internal.h"
3503e13cf5SThomas Gleixner 
36a038a353SPatrick Ohly void timecounter_init(struct timecounter *tc,
37a038a353SPatrick Ohly 		      const struct cyclecounter *cc,
38a038a353SPatrick Ohly 		      u64 start_tstamp)
39a038a353SPatrick Ohly {
40a038a353SPatrick Ohly 	tc->cc = cc;
41a038a353SPatrick Ohly 	tc->cycle_last = cc->read(cc);
42a038a353SPatrick Ohly 	tc->nsec = start_tstamp;
43a038a353SPatrick Ohly }
443586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_init);
45a038a353SPatrick Ohly 
46a038a353SPatrick Ohly /**
47a038a353SPatrick Ohly  * timecounter_read_delta - get nanoseconds since last call of this function
48a038a353SPatrick Ohly  * @tc:         Pointer to time counter
49a038a353SPatrick Ohly  *
50a038a353SPatrick Ohly  * When the underlying cycle counter runs over, this will be handled
51a038a353SPatrick Ohly  * correctly as long as it does not run over more than once between
52a038a353SPatrick Ohly  * calls.
53a038a353SPatrick Ohly  *
54a038a353SPatrick Ohly  * The first call to this function for a new time counter initializes
55a038a353SPatrick Ohly  * the time tracking and returns an undefined result.
56a038a353SPatrick Ohly  */
57a038a353SPatrick Ohly static u64 timecounter_read_delta(struct timecounter *tc)
58a038a353SPatrick Ohly {
59a038a353SPatrick Ohly 	cycle_t cycle_now, cycle_delta;
60a038a353SPatrick Ohly 	u64 ns_offset;
61a038a353SPatrick Ohly 
62a038a353SPatrick Ohly 	/* read cycle counter: */
63a038a353SPatrick Ohly 	cycle_now = tc->cc->read(tc->cc);
64a038a353SPatrick Ohly 
65a038a353SPatrick Ohly 	/* calculate the delta since the last timecounter_read_delta(): */
66a038a353SPatrick Ohly 	cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
67a038a353SPatrick Ohly 
68a038a353SPatrick Ohly 	/* convert to nanoseconds: */
69a038a353SPatrick Ohly 	ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
70a038a353SPatrick Ohly 
71a038a353SPatrick Ohly 	/* update time stamp of timecounter_read_delta() call: */
72a038a353SPatrick Ohly 	tc->cycle_last = cycle_now;
73a038a353SPatrick Ohly 
74a038a353SPatrick Ohly 	return ns_offset;
75a038a353SPatrick Ohly }
76a038a353SPatrick Ohly 
77a038a353SPatrick Ohly u64 timecounter_read(struct timecounter *tc)
78a038a353SPatrick Ohly {
79a038a353SPatrick Ohly 	u64 nsec;
80a038a353SPatrick Ohly 
81a038a353SPatrick Ohly 	/* increment time by nanoseconds since last call */
82a038a353SPatrick Ohly 	nsec = timecounter_read_delta(tc);
83a038a353SPatrick Ohly 	nsec += tc->nsec;
84a038a353SPatrick Ohly 	tc->nsec = nsec;
85a038a353SPatrick Ohly 
86a038a353SPatrick Ohly 	return nsec;
87a038a353SPatrick Ohly }
883586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_read);
89a038a353SPatrick Ohly 
90a038a353SPatrick Ohly u64 timecounter_cyc2time(struct timecounter *tc,
91a038a353SPatrick Ohly 			 cycle_t cycle_tstamp)
92a038a353SPatrick Ohly {
93a038a353SPatrick Ohly 	u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
94a038a353SPatrick Ohly 	u64 nsec;
95a038a353SPatrick Ohly 
96a038a353SPatrick Ohly 	/*
97a038a353SPatrick Ohly 	 * Instead of always treating cycle_tstamp as more recent
98a038a353SPatrick Ohly 	 * than tc->cycle_last, detect when it is too far in the
99a038a353SPatrick Ohly 	 * future and treat it as old time stamp instead.
100a038a353SPatrick Ohly 	 */
101a038a353SPatrick Ohly 	if (cycle_delta > tc->cc->mask / 2) {
102a038a353SPatrick Ohly 		cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
103a038a353SPatrick Ohly 		nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
104a038a353SPatrick Ohly 	} else {
105a038a353SPatrick Ohly 		nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
106a038a353SPatrick Ohly 	}
107a038a353SPatrick Ohly 
108a038a353SPatrick Ohly 	return nsec;
109a038a353SPatrick Ohly }
1103586e0a9SDavid S. Miller EXPORT_SYMBOL_GPL(timecounter_cyc2time);
111a038a353SPatrick Ohly 
1127d2f944aSThomas Gleixner /**
1137d2f944aSThomas Gleixner  * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
1147d2f944aSThomas Gleixner  * @mult:	pointer to mult variable
1157d2f944aSThomas Gleixner  * @shift:	pointer to shift variable
1167d2f944aSThomas Gleixner  * @from:	frequency to convert from
1177d2f944aSThomas Gleixner  * @to:		frequency to convert to
1185fdade95SNicolas Pitre  * @maxsec:	guaranteed runtime conversion range in seconds
1197d2f944aSThomas Gleixner  *
1207d2f944aSThomas Gleixner  * The function evaluates the shift/mult pair for the scaled math
1217d2f944aSThomas Gleixner  * operations of clocksources and clockevents.
1227d2f944aSThomas Gleixner  *
1237d2f944aSThomas Gleixner  * @to and @from are frequency values in HZ. For clock sources @to is
1247d2f944aSThomas Gleixner  * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
1257d2f944aSThomas Gleixner  * event @to is the counter frequency and @from is NSEC_PER_SEC.
1267d2f944aSThomas Gleixner  *
1275fdade95SNicolas Pitre  * The @maxsec conversion range argument controls the time frame in
1287d2f944aSThomas Gleixner  * seconds which must be covered by the runtime conversion with the
1297d2f944aSThomas Gleixner  * calculated mult and shift factors. This guarantees that no 64bit
1307d2f944aSThomas Gleixner  * overflow happens when the input value of the conversion is
1317d2f944aSThomas Gleixner  * multiplied with the calculated mult factor. Larger ranges may
1327d2f944aSThomas Gleixner  * reduce the conversion accuracy by chosing smaller mult and shift
1337d2f944aSThomas Gleixner  * factors.
1347d2f944aSThomas Gleixner  */
1357d2f944aSThomas Gleixner void
1365fdade95SNicolas Pitre clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
1377d2f944aSThomas Gleixner {
1387d2f944aSThomas Gleixner 	u64 tmp;
1397d2f944aSThomas Gleixner 	u32 sft, sftacc= 32;
1407d2f944aSThomas Gleixner 
1417d2f944aSThomas Gleixner 	/*
1427d2f944aSThomas Gleixner 	 * Calculate the shift factor which is limiting the conversion
1437d2f944aSThomas Gleixner 	 * range:
1447d2f944aSThomas Gleixner 	 */
1455fdade95SNicolas Pitre 	tmp = ((u64)maxsec * from) >> 32;
1467d2f944aSThomas Gleixner 	while (tmp) {
1477d2f944aSThomas Gleixner 		tmp >>=1;
1487d2f944aSThomas Gleixner 		sftacc--;
1497d2f944aSThomas Gleixner 	}
1507d2f944aSThomas Gleixner 
1517d2f944aSThomas Gleixner 	/*
1527d2f944aSThomas Gleixner 	 * Find the conversion shift/mult pair which has the best
1537d2f944aSThomas Gleixner 	 * accuracy and fits the maxsec conversion range:
1547d2f944aSThomas Gleixner 	 */
1557d2f944aSThomas Gleixner 	for (sft = 32; sft > 0; sft--) {
1567d2f944aSThomas Gleixner 		tmp = (u64) to << sft;
157b5776c4aSjohn stultz 		tmp += from / 2;
1587d2f944aSThomas Gleixner 		do_div(tmp, from);
1597d2f944aSThomas Gleixner 		if ((tmp >> sftacc) == 0)
1607d2f944aSThomas Gleixner 			break;
1617d2f944aSThomas Gleixner 	}
1627d2f944aSThomas Gleixner 	*mult = tmp;
1637d2f944aSThomas Gleixner 	*shift = sft;
1647d2f944aSThomas Gleixner }
1657d2f944aSThomas Gleixner 
166734efb46Sjohn stultz /*[Clocksource internal variables]---------
167734efb46Sjohn stultz  * curr_clocksource:
168f1b82746SMartin Schwidefsky  *	currently selected clocksource.
169734efb46Sjohn stultz  * clocksource_list:
170734efb46Sjohn stultz  *	linked list with the registered clocksources
17175c5158fSMartin Schwidefsky  * clocksource_mutex:
17275c5158fSMartin Schwidefsky  *	protects manipulations to curr_clocksource and the clocksource_list
173734efb46Sjohn stultz  * override_name:
174734efb46Sjohn stultz  *	Name of the user-specified clocksource.
175734efb46Sjohn stultz  */
176f1b82746SMartin Schwidefsky static struct clocksource *curr_clocksource;
177734efb46Sjohn stultz static LIST_HEAD(clocksource_list);
17875c5158fSMartin Schwidefsky static DEFINE_MUTEX(clocksource_mutex);
17929b54078SThomas Gleixner static char override_name[CS_NAME_LEN];
18054a6bc0bSThomas Gleixner static int finished_booting;
181734efb46Sjohn stultz 
1825d8b34fdSThomas Gleixner #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
183f79e0258SMartin Schwidefsky static void clocksource_watchdog_work(struct work_struct *work);
184332962f2SThomas Gleixner static void clocksource_select(void);
185f79e0258SMartin Schwidefsky 
1865d8b34fdSThomas Gleixner static LIST_HEAD(watchdog_list);
1875d8b34fdSThomas Gleixner static struct clocksource *watchdog;
1885d8b34fdSThomas Gleixner static struct timer_list watchdog_timer;
189f79e0258SMartin Schwidefsky static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
1905d8b34fdSThomas Gleixner static DEFINE_SPINLOCK(watchdog_lock);
191fb63a0ebSMartin Schwidefsky static int watchdog_running;
1929fb60336SThomas Gleixner static atomic_t watchdog_reset_pending;
193b52f52a0SThomas Gleixner 
19401548f4dSMartin Schwidefsky static int clocksource_watchdog_kthread(void *data);
195d0981a1bSThomas Gleixner static void __clocksource_change_rating(struct clocksource *cs, int rating);
196c55c87c8SMartin Schwidefsky 
1975d8b34fdSThomas Gleixner /*
19835c35d1aSDaniel Walker  * Interval: 0.5sec Threshold: 0.0625s
1995d8b34fdSThomas Gleixner  */
2005d8b34fdSThomas Gleixner #define WATCHDOG_INTERVAL (HZ >> 1)
20135c35d1aSDaniel Walker #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
2025d8b34fdSThomas Gleixner 
20301548f4dSMartin Schwidefsky static void clocksource_watchdog_work(struct work_struct *work)
20401548f4dSMartin Schwidefsky {
20501548f4dSMartin Schwidefsky 	/*
20601548f4dSMartin Schwidefsky 	 * If kthread_run fails the next watchdog scan over the
20701548f4dSMartin Schwidefsky 	 * watchdog_list will find the unstable clock again.
20801548f4dSMartin Schwidefsky 	 */
20901548f4dSMartin Schwidefsky 	kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog");
21001548f4dSMartin Schwidefsky }
21101548f4dSMartin Schwidefsky 
2127285dd7fSThomas Gleixner static void __clocksource_unstable(struct clocksource *cs)
2137285dd7fSThomas Gleixner {
2147285dd7fSThomas Gleixner 	cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
2157285dd7fSThomas Gleixner 	cs->flags |= CLOCK_SOURCE_UNSTABLE;
21654a6bc0bSThomas Gleixner 	if (finished_booting)
2177285dd7fSThomas Gleixner 		schedule_work(&watchdog_work);
2187285dd7fSThomas Gleixner }
2197285dd7fSThomas Gleixner 
2208cf4e750SMartin Schwidefsky static void clocksource_unstable(struct clocksource *cs, int64_t delta)
2215d8b34fdSThomas Gleixner {
2225d8b34fdSThomas Gleixner 	printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
2235d8b34fdSThomas Gleixner 	       cs->name, delta);
2247285dd7fSThomas Gleixner 	__clocksource_unstable(cs);
2257285dd7fSThomas Gleixner }
2267285dd7fSThomas Gleixner 
2277285dd7fSThomas Gleixner /**
2287285dd7fSThomas Gleixner  * clocksource_mark_unstable - mark clocksource unstable via watchdog
2297285dd7fSThomas Gleixner  * @cs:		clocksource to be marked unstable
2307285dd7fSThomas Gleixner  *
2317285dd7fSThomas Gleixner  * This function is called instead of clocksource_change_rating from
2327285dd7fSThomas Gleixner  * cpu hotplug code to avoid a deadlock between the clocksource mutex
2337285dd7fSThomas Gleixner  * and the cpu hotplug mutex. It defers the update of the clocksource
2347285dd7fSThomas Gleixner  * to the watchdog thread.
2357285dd7fSThomas Gleixner  */
2367285dd7fSThomas Gleixner void clocksource_mark_unstable(struct clocksource *cs)
2377285dd7fSThomas Gleixner {
2387285dd7fSThomas Gleixner 	unsigned long flags;
2397285dd7fSThomas Gleixner 
2407285dd7fSThomas Gleixner 	spin_lock_irqsave(&watchdog_lock, flags);
2417285dd7fSThomas Gleixner 	if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) {
2427285dd7fSThomas Gleixner 		if (list_empty(&cs->wd_list))
2437285dd7fSThomas Gleixner 			list_add(&cs->wd_list, &watchdog_list);
2447285dd7fSThomas Gleixner 		__clocksource_unstable(cs);
2457285dd7fSThomas Gleixner 	}
2467285dd7fSThomas Gleixner 	spin_unlock_irqrestore(&watchdog_lock, flags);
2475d8b34fdSThomas Gleixner }
2485d8b34fdSThomas Gleixner 
2495d8b34fdSThomas Gleixner static void clocksource_watchdog(unsigned long data)
2505d8b34fdSThomas Gleixner {
251c55c87c8SMartin Schwidefsky 	struct clocksource *cs;
2525d8b34fdSThomas Gleixner 	cycle_t csnow, wdnow;
2535d8b34fdSThomas Gleixner 	int64_t wd_nsec, cs_nsec;
2549fb60336SThomas Gleixner 	int next_cpu, reset_pending;
2555d8b34fdSThomas Gleixner 
2565d8b34fdSThomas Gleixner 	spin_lock(&watchdog_lock);
257fb63a0ebSMartin Schwidefsky 	if (!watchdog_running)
258fb63a0ebSMartin Schwidefsky 		goto out;
2595d8b34fdSThomas Gleixner 
2609fb60336SThomas Gleixner 	reset_pending = atomic_read(&watchdog_reset_pending);
2619fb60336SThomas Gleixner 
262c55c87c8SMartin Schwidefsky 	list_for_each_entry(cs, &watchdog_list, wd_list) {
263c55c87c8SMartin Schwidefsky 
264c55c87c8SMartin Schwidefsky 		/* Clocksource already marked unstable? */
26501548f4dSMartin Schwidefsky 		if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
26654a6bc0bSThomas Gleixner 			if (finished_booting)
26701548f4dSMartin Schwidefsky 				schedule_work(&watchdog_work);
268c55c87c8SMartin Schwidefsky 			continue;
26901548f4dSMartin Schwidefsky 		}
270c55c87c8SMartin Schwidefsky 
271b5199515SThomas Gleixner 		local_irq_disable();
2728e19608eSMagnus Damm 		csnow = cs->read(cs);
273b5199515SThomas Gleixner 		wdnow = watchdog->read(watchdog);
274b5199515SThomas Gleixner 		local_irq_enable();
275b52f52a0SThomas Gleixner 
2768cf4e750SMartin Schwidefsky 		/* Clocksource initialized ? */
2779fb60336SThomas Gleixner 		if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
2789fb60336SThomas Gleixner 		    atomic_read(&watchdog_reset_pending)) {
2798cf4e750SMartin Schwidefsky 			cs->flags |= CLOCK_SOURCE_WATCHDOG;
280b5199515SThomas Gleixner 			cs->wd_last = wdnow;
281b5199515SThomas Gleixner 			cs->cs_last = csnow;
282b52f52a0SThomas Gleixner 			continue;
283b52f52a0SThomas Gleixner 		}
284b52f52a0SThomas Gleixner 
285b5199515SThomas Gleixner 		wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask,
286b5199515SThomas Gleixner 					     watchdog->mult, watchdog->shift);
287b5199515SThomas Gleixner 
288b5199515SThomas Gleixner 		cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
289155ec602SMartin Schwidefsky 					     cs->mask, cs->mult, cs->shift);
290b5199515SThomas Gleixner 		cs->cs_last = csnow;
291b5199515SThomas Gleixner 		cs->wd_last = wdnow;
292b5199515SThomas Gleixner 
2939fb60336SThomas Gleixner 		if (atomic_read(&watchdog_reset_pending))
2949fb60336SThomas Gleixner 			continue;
2959fb60336SThomas Gleixner 
296b5199515SThomas Gleixner 		/* Check the deviation from the watchdog clocksource. */
2979fb60336SThomas Gleixner 		if ((abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD)) {
2988cf4e750SMartin Schwidefsky 			clocksource_unstable(cs, cs_nsec - wd_nsec);
2998cf4e750SMartin Schwidefsky 			continue;
3008cf4e750SMartin Schwidefsky 		}
3018cf4e750SMartin Schwidefsky 
3028cf4e750SMartin Schwidefsky 		if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
3038cf4e750SMartin Schwidefsky 		    (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
3045d8b34fdSThomas Gleixner 		    (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
305332962f2SThomas Gleixner 			/* Mark it valid for high-res. */
3065d8b34fdSThomas Gleixner 			cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
307332962f2SThomas Gleixner 
30879bf2bb3SThomas Gleixner 			/*
309332962f2SThomas Gleixner 			 * clocksource_done_booting() will sort it if
310332962f2SThomas Gleixner 			 * finished_booting is not set yet.
31179bf2bb3SThomas Gleixner 			 */
312332962f2SThomas Gleixner 			if (!finished_booting)
313332962f2SThomas Gleixner 				continue;
314332962f2SThomas Gleixner 
315332962f2SThomas Gleixner 			/*
316332962f2SThomas Gleixner 			 * If this is not the current clocksource let
317332962f2SThomas Gleixner 			 * the watchdog thread reselect it. Due to the
318332962f2SThomas Gleixner 			 * change to high res this clocksource might
319332962f2SThomas Gleixner 			 * be preferred now. If it is the current
320332962f2SThomas Gleixner 			 * clocksource let the tick code know about
321332962f2SThomas Gleixner 			 * that change.
322332962f2SThomas Gleixner 			 */
323332962f2SThomas Gleixner 			if (cs != curr_clocksource) {
324332962f2SThomas Gleixner 				cs->flags |= CLOCK_SOURCE_RESELECT;
325332962f2SThomas Gleixner 				schedule_work(&watchdog_work);
326332962f2SThomas Gleixner 			} else {
32779bf2bb3SThomas Gleixner 				tick_clock_notify();
3285d8b34fdSThomas Gleixner 			}
3295d8b34fdSThomas Gleixner 		}
330332962f2SThomas Gleixner 	}
3315d8b34fdSThomas Gleixner 
3326993fc5bSAndi Kleen 	/*
3339fb60336SThomas Gleixner 	 * We only clear the watchdog_reset_pending, when we did a
3349fb60336SThomas Gleixner 	 * full cycle through all clocksources.
3359fb60336SThomas Gleixner 	 */
3369fb60336SThomas Gleixner 	if (reset_pending)
3379fb60336SThomas Gleixner 		atomic_dec(&watchdog_reset_pending);
3389fb60336SThomas Gleixner 
3399fb60336SThomas Gleixner 	/*
340c55c87c8SMartin Schwidefsky 	 * Cycle through CPUs to check if the CPUs stay synchronized
341c55c87c8SMartin Schwidefsky 	 * to each other.
3426993fc5bSAndi Kleen 	 */
343c55c87c8SMartin Schwidefsky 	next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
344cad0e458SMike Travis 	if (next_cpu >= nr_cpu_ids)
3456b954823SRusty Russell 		next_cpu = cpumask_first(cpu_online_mask);
3466993fc5bSAndi Kleen 	watchdog_timer.expires += WATCHDOG_INTERVAL;
3476993fc5bSAndi Kleen 	add_timer_on(&watchdog_timer, next_cpu);
348fb63a0ebSMartin Schwidefsky out:
3495d8b34fdSThomas Gleixner 	spin_unlock(&watchdog_lock);
3505d8b34fdSThomas Gleixner }
3510f8e8ef7SMartin Schwidefsky 
352fb63a0ebSMartin Schwidefsky static inline void clocksource_start_watchdog(void)
353fb63a0ebSMartin Schwidefsky {
354fb63a0ebSMartin Schwidefsky 	if (watchdog_running || !watchdog || list_empty(&watchdog_list))
355fb63a0ebSMartin Schwidefsky 		return;
356fb63a0ebSMartin Schwidefsky 	init_timer(&watchdog_timer);
357fb63a0ebSMartin Schwidefsky 	watchdog_timer.function = clocksource_watchdog;
358fb63a0ebSMartin Schwidefsky 	watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
359fb63a0ebSMartin Schwidefsky 	add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
360fb63a0ebSMartin Schwidefsky 	watchdog_running = 1;
361fb63a0ebSMartin Schwidefsky }
362fb63a0ebSMartin Schwidefsky 
363fb63a0ebSMartin Schwidefsky static inline void clocksource_stop_watchdog(void)
364fb63a0ebSMartin Schwidefsky {
365fb63a0ebSMartin Schwidefsky 	if (!watchdog_running || (watchdog && !list_empty(&watchdog_list)))
366fb63a0ebSMartin Schwidefsky 		return;
367fb63a0ebSMartin Schwidefsky 	del_timer(&watchdog_timer);
368fb63a0ebSMartin Schwidefsky 	watchdog_running = 0;
369fb63a0ebSMartin Schwidefsky }
370fb63a0ebSMartin Schwidefsky 
3710f8e8ef7SMartin Schwidefsky static inline void clocksource_reset_watchdog(void)
3720f8e8ef7SMartin Schwidefsky {
3730f8e8ef7SMartin Schwidefsky 	struct clocksource *cs;
3740f8e8ef7SMartin Schwidefsky 
3750f8e8ef7SMartin Schwidefsky 	list_for_each_entry(cs, &watchdog_list, wd_list)
3760f8e8ef7SMartin Schwidefsky 		cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
3770f8e8ef7SMartin Schwidefsky }
3780f8e8ef7SMartin Schwidefsky 
379b52f52a0SThomas Gleixner static void clocksource_resume_watchdog(void)
380b52f52a0SThomas Gleixner {
3819fb60336SThomas Gleixner 	atomic_inc(&watchdog_reset_pending);
382b52f52a0SThomas Gleixner }
383b52f52a0SThomas Gleixner 
384fb63a0ebSMartin Schwidefsky static void clocksource_enqueue_watchdog(struct clocksource *cs)
3855d8b34fdSThomas Gleixner {
3865d8b34fdSThomas Gleixner 	unsigned long flags;
3875d8b34fdSThomas Gleixner 
3885d8b34fdSThomas Gleixner 	spin_lock_irqsave(&watchdog_lock, flags);
3895d8b34fdSThomas Gleixner 	if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
390fb63a0ebSMartin Schwidefsky 		/* cs is a clocksource to be watched. */
3915d8b34fdSThomas Gleixner 		list_add(&cs->wd_list, &watchdog_list);
392fb63a0ebSMartin Schwidefsky 		cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
393948ac6d7SThomas Gleixner 	} else {
394fb63a0ebSMartin Schwidefsky 		/* cs is a watchdog. */
395948ac6d7SThomas Gleixner 		if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
3965d8b34fdSThomas Gleixner 			cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
397fb63a0ebSMartin Schwidefsky 		/* Pick the best watchdog. */
3985d8b34fdSThomas Gleixner 		if (!watchdog || cs->rating > watchdog->rating) {
3995d8b34fdSThomas Gleixner 			watchdog = cs;
4005d8b34fdSThomas Gleixner 			/* Reset watchdog cycles */
4010f8e8ef7SMartin Schwidefsky 			clocksource_reset_watchdog();
4025d8b34fdSThomas Gleixner 		}
4035d8b34fdSThomas Gleixner 	}
404fb63a0ebSMartin Schwidefsky 	/* Check if the watchdog timer needs to be started. */
405fb63a0ebSMartin Schwidefsky 	clocksource_start_watchdog();
4065d8b34fdSThomas Gleixner 	spin_unlock_irqrestore(&watchdog_lock, flags);
4075d8b34fdSThomas Gleixner }
408fb63a0ebSMartin Schwidefsky 
409fb63a0ebSMartin Schwidefsky static void clocksource_dequeue_watchdog(struct clocksource *cs)
410fb63a0ebSMartin Schwidefsky {
411fb63a0ebSMartin Schwidefsky 	unsigned long flags;
412fb63a0ebSMartin Schwidefsky 
413fb63a0ebSMartin Schwidefsky 	spin_lock_irqsave(&watchdog_lock, flags);
414a89c7edbSThomas Gleixner 	if (cs != watchdog) {
415fb63a0ebSMartin Schwidefsky 		if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
416fb63a0ebSMartin Schwidefsky 			/* cs is a watched clocksource. */
417fb63a0ebSMartin Schwidefsky 			list_del_init(&cs->wd_list);
418fb63a0ebSMartin Schwidefsky 			/* Check if the watchdog timer needs to be stopped. */
419fb63a0ebSMartin Schwidefsky 			clocksource_stop_watchdog();
420a89c7edbSThomas Gleixner 		}
421a89c7edbSThomas Gleixner 	}
422fb63a0ebSMartin Schwidefsky 	spin_unlock_irqrestore(&watchdog_lock, flags);
423fb63a0ebSMartin Schwidefsky }
424fb63a0ebSMartin Schwidefsky 
425332962f2SThomas Gleixner static int __clocksource_watchdog_kthread(void)
426c55c87c8SMartin Schwidefsky {
427c55c87c8SMartin Schwidefsky 	struct clocksource *cs, *tmp;
428c55c87c8SMartin Schwidefsky 	unsigned long flags;
4296ea41d25SThomas Gleixner 	LIST_HEAD(unstable);
430332962f2SThomas Gleixner 	int select = 0;
431c55c87c8SMartin Schwidefsky 
432c55c87c8SMartin Schwidefsky 	spin_lock_irqsave(&watchdog_lock, flags);
433332962f2SThomas Gleixner 	list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
434c55c87c8SMartin Schwidefsky 		if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
435c55c87c8SMartin Schwidefsky 			list_del_init(&cs->wd_list);
4366ea41d25SThomas Gleixner 			list_add(&cs->wd_list, &unstable);
437332962f2SThomas Gleixner 			select = 1;
438332962f2SThomas Gleixner 		}
439332962f2SThomas Gleixner 		if (cs->flags & CLOCK_SOURCE_RESELECT) {
440332962f2SThomas Gleixner 			cs->flags &= ~CLOCK_SOURCE_RESELECT;
441332962f2SThomas Gleixner 			select = 1;
442332962f2SThomas Gleixner 		}
443c55c87c8SMartin Schwidefsky 	}
444c55c87c8SMartin Schwidefsky 	/* Check if the watchdog timer needs to be stopped. */
445c55c87c8SMartin Schwidefsky 	clocksource_stop_watchdog();
4466ea41d25SThomas Gleixner 	spin_unlock_irqrestore(&watchdog_lock, flags);
4476ea41d25SThomas Gleixner 
4486ea41d25SThomas Gleixner 	/* Needs to be done outside of watchdog lock */
4496ea41d25SThomas Gleixner 	list_for_each_entry_safe(cs, tmp, &unstable, wd_list) {
4506ea41d25SThomas Gleixner 		list_del_init(&cs->wd_list);
451d0981a1bSThomas Gleixner 		__clocksource_change_rating(cs, 0);
4526ea41d25SThomas Gleixner 	}
453332962f2SThomas Gleixner 	return select;
454332962f2SThomas Gleixner }
455332962f2SThomas Gleixner 
456332962f2SThomas Gleixner static int clocksource_watchdog_kthread(void *data)
457332962f2SThomas Gleixner {
458332962f2SThomas Gleixner 	mutex_lock(&clocksource_mutex);
459332962f2SThomas Gleixner 	if (__clocksource_watchdog_kthread())
460332962f2SThomas Gleixner 		clocksource_select();
461d0981a1bSThomas Gleixner 	mutex_unlock(&clocksource_mutex);
46201548f4dSMartin Schwidefsky 	return 0;
463c55c87c8SMartin Schwidefsky }
464c55c87c8SMartin Schwidefsky 
4657eaeb343SThomas Gleixner static bool clocksource_is_watchdog(struct clocksource *cs)
4667eaeb343SThomas Gleixner {
4677eaeb343SThomas Gleixner 	return cs == watchdog;
4687eaeb343SThomas Gleixner }
4697eaeb343SThomas Gleixner 
470fb63a0ebSMartin Schwidefsky #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
471fb63a0ebSMartin Schwidefsky 
472fb63a0ebSMartin Schwidefsky static void clocksource_enqueue_watchdog(struct clocksource *cs)
4735d8b34fdSThomas Gleixner {
4745d8b34fdSThomas Gleixner 	if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
4755d8b34fdSThomas Gleixner 		cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
4765d8b34fdSThomas Gleixner }
477b52f52a0SThomas Gleixner 
478fb63a0ebSMartin Schwidefsky static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
479b52f52a0SThomas Gleixner static inline void clocksource_resume_watchdog(void) { }
480332962f2SThomas Gleixner static inline int __clocksource_watchdog_kthread(void) { return 0; }
4817eaeb343SThomas Gleixner static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
482397bbf6dSPrarit Bhargava void clocksource_mark_unstable(struct clocksource *cs) { }
483fb63a0ebSMartin Schwidefsky 
484fb63a0ebSMartin Schwidefsky #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
4855d8b34fdSThomas Gleixner 
486734efb46Sjohn stultz /**
487c54a42b1SMagnus Damm  * clocksource_suspend - suspend the clocksource(s)
488c54a42b1SMagnus Damm  */
489c54a42b1SMagnus Damm void clocksource_suspend(void)
490c54a42b1SMagnus Damm {
491c54a42b1SMagnus Damm 	struct clocksource *cs;
492c54a42b1SMagnus Damm 
493c54a42b1SMagnus Damm 	list_for_each_entry_reverse(cs, &clocksource_list, list)
494c54a42b1SMagnus Damm 		if (cs->suspend)
495c54a42b1SMagnus Damm 			cs->suspend(cs);
496c54a42b1SMagnus Damm }
497c54a42b1SMagnus Damm 
498c54a42b1SMagnus Damm /**
499b52f52a0SThomas Gleixner  * clocksource_resume - resume the clocksource(s)
500b52f52a0SThomas Gleixner  */
501b52f52a0SThomas Gleixner void clocksource_resume(void)
502b52f52a0SThomas Gleixner {
5032e197586SMatthias Kaehlcke 	struct clocksource *cs;
504b52f52a0SThomas Gleixner 
50575c5158fSMartin Schwidefsky 	list_for_each_entry(cs, &clocksource_list, list)
506b52f52a0SThomas Gleixner 		if (cs->resume)
50717622339SMagnus Damm 			cs->resume(cs);
508b52f52a0SThomas Gleixner 
509b52f52a0SThomas Gleixner 	clocksource_resume_watchdog();
510b52f52a0SThomas Gleixner }
511b52f52a0SThomas Gleixner 
512b52f52a0SThomas Gleixner /**
5137c3078b6SJason Wessel  * clocksource_touch_watchdog - Update watchdog
5147c3078b6SJason Wessel  *
5157c3078b6SJason Wessel  * Update the watchdog after exception contexts such as kgdb so as not
5167b7422a5SThomas Gleixner  * to incorrectly trip the watchdog. This might fail when the kernel
5177b7422a5SThomas Gleixner  * was stopped in code which holds watchdog_lock.
5187c3078b6SJason Wessel  */
5197c3078b6SJason Wessel void clocksource_touch_watchdog(void)
5207c3078b6SJason Wessel {
5217c3078b6SJason Wessel 	clocksource_resume_watchdog();
5227c3078b6SJason Wessel }
5237c3078b6SJason Wessel 
524734efb46Sjohn stultz /**
525d65670a7SJohn Stultz  * clocksource_max_adjustment- Returns max adjustment amount
526d65670a7SJohn Stultz  * @cs:         Pointer to clocksource
527d65670a7SJohn Stultz  *
528d65670a7SJohn Stultz  */
529d65670a7SJohn Stultz static u32 clocksource_max_adjustment(struct clocksource *cs)
530d65670a7SJohn Stultz {
531d65670a7SJohn Stultz 	u64 ret;
532d65670a7SJohn Stultz 	/*
53388b28adfSJim Cromie 	 * We won't try to correct for more than 11% adjustments (110,000 ppm),
534d65670a7SJohn Stultz 	 */
535d65670a7SJohn Stultz 	ret = (u64)cs->mult * 11;
536d65670a7SJohn Stultz 	do_div(ret,100);
537d65670a7SJohn Stultz 	return (u32)ret;
538d65670a7SJohn Stultz }
539d65670a7SJohn Stultz 
540d65670a7SJohn Stultz /**
54187d8b9ebSStephen Boyd  * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted
54287d8b9ebSStephen Boyd  * @mult:	cycle to nanosecond multiplier
54387d8b9ebSStephen Boyd  * @shift:	cycle to nanosecond divisor (power of two)
54487d8b9ebSStephen Boyd  * @maxadj:	maximum adjustment value to mult (~11%)
54587d8b9ebSStephen Boyd  * @mask:	bitmask for two's complement subtraction of non 64 bit counters
54698962465SJon Hunter  */
54787d8b9ebSStephen Boyd u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask)
54898962465SJon Hunter {
54998962465SJon Hunter 	u64 max_nsecs, max_cycles;
55098962465SJon Hunter 
55198962465SJon Hunter 	/*
55298962465SJon Hunter 	 * Calculate the maximum number of cycles that we can pass to the
55398962465SJon Hunter 	 * cyc2ns function without overflowing a 64-bit signed result. The
55487d8b9ebSStephen Boyd 	 * maximum number of cycles is equal to ULLONG_MAX/(mult+maxadj)
555d65670a7SJohn Stultz 	 * which is equivalent to the below.
55687d8b9ebSStephen Boyd 	 * max_cycles < (2^63)/(mult + maxadj)
55787d8b9ebSStephen Boyd 	 * max_cycles < 2^(log2((2^63)/(mult + maxadj)))
55887d8b9ebSStephen Boyd 	 * max_cycles < 2^(log2(2^63) - log2(mult + maxadj))
55987d8b9ebSStephen Boyd 	 * max_cycles < 2^(63 - log2(mult + maxadj))
56087d8b9ebSStephen Boyd 	 * max_cycles < 1 << (63 - log2(mult + maxadj))
56198962465SJon Hunter 	 * Please note that we add 1 to the result of the log2 to account for
56298962465SJon Hunter 	 * any rounding errors, ensure the above inequality is satisfied and
56398962465SJon Hunter 	 * no overflow will occur.
56498962465SJon Hunter 	 */
56587d8b9ebSStephen Boyd 	max_cycles = 1ULL << (63 - (ilog2(mult + maxadj) + 1));
56698962465SJon Hunter 
56798962465SJon Hunter 	/*
56898962465SJon Hunter 	 * The actual maximum number of cycles we can defer the clocksource is
56987d8b9ebSStephen Boyd 	 * determined by the minimum of max_cycles and mask.
570d65670a7SJohn Stultz 	 * Note: Here we subtract the maxadj to make sure we don't sleep for
571d65670a7SJohn Stultz 	 * too long if there's a large negative adjustment.
57298962465SJon Hunter 	 */
57387d8b9ebSStephen Boyd 	max_cycles = min(max_cycles, mask);
57487d8b9ebSStephen Boyd 	max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
57598962465SJon Hunter 
57687d8b9ebSStephen Boyd 	return max_nsecs;
57787d8b9ebSStephen Boyd }
57887d8b9ebSStephen Boyd 
57987d8b9ebSStephen Boyd /**
58087d8b9ebSStephen Boyd  * clocksource_max_deferment - Returns max time the clocksource can be deferred
58187d8b9ebSStephen Boyd  * @cs:         Pointer to clocksource
58287d8b9ebSStephen Boyd  *
58387d8b9ebSStephen Boyd  */
58487d8b9ebSStephen Boyd static u64 clocksource_max_deferment(struct clocksource *cs)
58587d8b9ebSStephen Boyd {
58687d8b9ebSStephen Boyd 	u64 max_nsecs;
58787d8b9ebSStephen Boyd 
58887d8b9ebSStephen Boyd 	max_nsecs = clocks_calc_max_nsecs(cs->mult, cs->shift, cs->maxadj,
58987d8b9ebSStephen Boyd 					  cs->mask);
59098962465SJon Hunter 	/*
59198962465SJon Hunter 	 * To ensure that the clocksource does not wrap whilst we are idle,
59298962465SJon Hunter 	 * limit the time the clocksource can be deferred by 12.5%. Please
59398962465SJon Hunter 	 * note a margin of 12.5% is used because this can be computed with
59498962465SJon Hunter 	 * a shift, versus say 10% which would require division.
59598962465SJon Hunter 	 */
596b1f91966SYang Honggang (Joseph) 	return max_nsecs - (max_nsecs >> 3);
59798962465SJon Hunter }
59898962465SJon Hunter 
599592913ecSJohn Stultz #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
600734efb46Sjohn stultz 
601f5a2e343SThomas Gleixner static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
6025d33b883SThomas Gleixner {
6035d33b883SThomas Gleixner 	struct clocksource *cs;
6045d33b883SThomas Gleixner 
6055d33b883SThomas Gleixner 	if (!finished_booting || list_empty(&clocksource_list))
6065d33b883SThomas Gleixner 		return NULL;
6075d33b883SThomas Gleixner 
6085d33b883SThomas Gleixner 	/*
6095d33b883SThomas Gleixner 	 * We pick the clocksource with the highest rating. If oneshot
6105d33b883SThomas Gleixner 	 * mode is active, we pick the highres valid clocksource with
6115d33b883SThomas Gleixner 	 * the best rating.
6125d33b883SThomas Gleixner 	 */
6135d33b883SThomas Gleixner 	list_for_each_entry(cs, &clocksource_list, list) {
614f5a2e343SThomas Gleixner 		if (skipcur && cs == curr_clocksource)
615f5a2e343SThomas Gleixner 			continue;
6165d33b883SThomas Gleixner 		if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
6175d33b883SThomas Gleixner 			continue;
6185d33b883SThomas Gleixner 		return cs;
6195d33b883SThomas Gleixner 	}
6205d33b883SThomas Gleixner 	return NULL;
6215d33b883SThomas Gleixner }
6225d33b883SThomas Gleixner 
623f5a2e343SThomas Gleixner static void __clocksource_select(bool skipcur)
624734efb46Sjohn stultz {
6255d33b883SThomas Gleixner 	bool oneshot = tick_oneshot_mode_active();
626f1b82746SMartin Schwidefsky 	struct clocksource *best, *cs;
6275d8b34fdSThomas Gleixner 
6285d33b883SThomas Gleixner 	/* Find the best suitable clocksource */
629f5a2e343SThomas Gleixner 	best = clocksource_find_best(oneshot, skipcur);
6305d33b883SThomas Gleixner 	if (!best)
631f1b82746SMartin Schwidefsky 		return;
6325d33b883SThomas Gleixner 
633f1b82746SMartin Schwidefsky 	/* Check for the override clocksource. */
634f1b82746SMartin Schwidefsky 	list_for_each_entry(cs, &clocksource_list, list) {
635f5a2e343SThomas Gleixner 		if (skipcur && cs == curr_clocksource)
636f5a2e343SThomas Gleixner 			continue;
637f1b82746SMartin Schwidefsky 		if (strcmp(cs->name, override_name) != 0)
638f1b82746SMartin Schwidefsky 			continue;
639f1b82746SMartin Schwidefsky 		/*
640f1b82746SMartin Schwidefsky 		 * Check to make sure we don't switch to a non-highres
641f1b82746SMartin Schwidefsky 		 * capable clocksource if the tick code is in oneshot
642f1b82746SMartin Schwidefsky 		 * mode (highres or nohz)
643f1b82746SMartin Schwidefsky 		 */
6445d33b883SThomas Gleixner 		if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
645f1b82746SMartin Schwidefsky 			/* Override clocksource cannot be used. */
646f1b82746SMartin Schwidefsky 			printk(KERN_WARNING "Override clocksource %s is not "
647f1b82746SMartin Schwidefsky 			       "HRT compatible. Cannot switch while in "
648f1b82746SMartin Schwidefsky 			       "HRT/NOHZ mode\n", cs->name);
649f1b82746SMartin Schwidefsky 			override_name[0] = 0;
650f1b82746SMartin Schwidefsky 		} else
651f1b82746SMartin Schwidefsky 			/* Override clocksource can be used. */
652f1b82746SMartin Schwidefsky 			best = cs;
653f1b82746SMartin Schwidefsky 		break;
654734efb46Sjohn stultz 	}
655ba919d1cSThomas Gleixner 
656ba919d1cSThomas Gleixner 	if (curr_clocksource != best && !timekeeping_notify(best)) {
657ba919d1cSThomas Gleixner 		pr_info("Switched to clocksource %s\n", best->name);
65875c5158fSMartin Schwidefsky 		curr_clocksource = best;
659f1b82746SMartin Schwidefsky 	}
66075c5158fSMartin Schwidefsky }
66175c5158fSMartin Schwidefsky 
662f5a2e343SThomas Gleixner /**
663f5a2e343SThomas Gleixner  * clocksource_select - Select the best clocksource available
664f5a2e343SThomas Gleixner  *
665f5a2e343SThomas Gleixner  * Private function. Must hold clocksource_mutex when called.
666f5a2e343SThomas Gleixner  *
667f5a2e343SThomas Gleixner  * Select the clocksource with the best rating, or the clocksource,
668f5a2e343SThomas Gleixner  * which is selected by userspace override.
669f5a2e343SThomas Gleixner  */
670f5a2e343SThomas Gleixner static void clocksource_select(void)
671f5a2e343SThomas Gleixner {
672f5a2e343SThomas Gleixner 	return __clocksource_select(false);
673f5a2e343SThomas Gleixner }
674f5a2e343SThomas Gleixner 
6757eaeb343SThomas Gleixner static void clocksource_select_fallback(void)
6767eaeb343SThomas Gleixner {
6777eaeb343SThomas Gleixner 	return __clocksource_select(true);
6787eaeb343SThomas Gleixner }
6797eaeb343SThomas Gleixner 
680592913ecSJohn Stultz #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
68154a6bc0bSThomas Gleixner 
68254a6bc0bSThomas Gleixner static inline void clocksource_select(void) { }
6831eaff672SThomas Gleixner static inline void clocksource_select_fallback(void) { }
68454a6bc0bSThomas Gleixner 
68554a6bc0bSThomas Gleixner #endif
68654a6bc0bSThomas Gleixner 
68775c5158fSMartin Schwidefsky /*
68875c5158fSMartin Schwidefsky  * clocksource_done_booting - Called near the end of core bootup
68975c5158fSMartin Schwidefsky  *
69075c5158fSMartin Schwidefsky  * Hack to avoid lots of clocksource churn at boot time.
69175c5158fSMartin Schwidefsky  * We use fs_initcall because we want this to start before
69275c5158fSMartin Schwidefsky  * device_initcall but after subsys_initcall.
69375c5158fSMartin Schwidefsky  */
69475c5158fSMartin Schwidefsky static int __init clocksource_done_booting(void)
69575c5158fSMartin Schwidefsky {
696ad6759fbSjohn stultz 	mutex_lock(&clocksource_mutex);
697ad6759fbSjohn stultz 	curr_clocksource = clocksource_default_clock();
69875c5158fSMartin Schwidefsky 	finished_booting = 1;
69954a6bc0bSThomas Gleixner 	/*
70054a6bc0bSThomas Gleixner 	 * Run the watchdog first to eliminate unstable clock sources
70154a6bc0bSThomas Gleixner 	 */
702332962f2SThomas Gleixner 	__clocksource_watchdog_kthread();
70375c5158fSMartin Schwidefsky 	clocksource_select();
704e6c73305SThomas Gleixner 	mutex_unlock(&clocksource_mutex);
70575c5158fSMartin Schwidefsky 	return 0;
70675c5158fSMartin Schwidefsky }
70775c5158fSMartin Schwidefsky fs_initcall(clocksource_done_booting);
708f1b82746SMartin Schwidefsky 
70992c7e002SThomas Gleixner /*
71092c7e002SThomas Gleixner  * Enqueue the clocksource sorted by rating
711734efb46Sjohn stultz  */
712f1b82746SMartin Schwidefsky static void clocksource_enqueue(struct clocksource *cs)
713734efb46Sjohn stultz {
714f1b82746SMartin Schwidefsky 	struct list_head *entry = &clocksource_list;
715f1b82746SMartin Schwidefsky 	struct clocksource *tmp;
716734efb46Sjohn stultz 
717f1b82746SMartin Schwidefsky 	list_for_each_entry(tmp, &clocksource_list, list)
71892c7e002SThomas Gleixner 		/* Keep track of the place, where to insert */
719f1b82746SMartin Schwidefsky 		if (tmp->rating >= cs->rating)
720f1b82746SMartin Schwidefsky 			entry = &tmp->list;
721f1b82746SMartin Schwidefsky 	list_add(&cs->list, entry);
722734efb46Sjohn stultz }
723734efb46Sjohn stultz 
724d7e81c26SJohn Stultz /**
725852db46dSJohn Stultz  * __clocksource_updatefreq_scale - Used update clocksource with new freq
726b1b73d09SKusanagi Kouichi  * @cs:		clocksource to be registered
727852db46dSJohn Stultz  * @scale:	Scale factor multiplied against freq to get clocksource hz
728852db46dSJohn Stultz  * @freq:	clocksource frequency (cycles per second) divided by scale
729852db46dSJohn Stultz  *
730852db46dSJohn Stultz  * This should only be called from the clocksource->enable() method.
731852db46dSJohn Stultz  *
732852db46dSJohn Stultz  * This *SHOULD NOT* be called directly! Please use the
733852db46dSJohn Stultz  * clocksource_updatefreq_hz() or clocksource_updatefreq_khz helper functions.
734852db46dSJohn Stultz  */
735852db46dSJohn Stultz void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
736852db46dSJohn Stultz {
737c0e299b1SThomas Gleixner 	u64 sec;
738852db46dSJohn Stultz 	/*
739724ed53eSThomas Gleixner 	 * Calc the maximum number of seconds which we can run before
740724ed53eSThomas Gleixner 	 * wrapping around. For clocksources which have a mask > 32bit
741724ed53eSThomas Gleixner 	 * we need to limit the max sleep time to have a good
742724ed53eSThomas Gleixner 	 * conversion precision. 10 minutes is still a reasonable
743724ed53eSThomas Gleixner 	 * amount. That results in a shift value of 24 for a
744724ed53eSThomas Gleixner 	 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
745724ed53eSThomas Gleixner 	 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
746724ed53eSThomas Gleixner 	 * margin as we do in clocksource_max_deferment()
747852db46dSJohn Stultz 	 */
748b1f91966SYang Honggang (Joseph) 	sec = (cs->mask - (cs->mask >> 3));
749724ed53eSThomas Gleixner 	do_div(sec, freq);
750724ed53eSThomas Gleixner 	do_div(sec, scale);
751724ed53eSThomas Gleixner 	if (!sec)
752724ed53eSThomas Gleixner 		sec = 1;
753724ed53eSThomas Gleixner 	else if (sec > 600 && cs->mask > UINT_MAX)
754724ed53eSThomas Gleixner 		sec = 600;
755724ed53eSThomas Gleixner 
756852db46dSJohn Stultz 	clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
757724ed53eSThomas Gleixner 			       NSEC_PER_SEC / scale, sec * scale);
758d65670a7SJohn Stultz 
759d65670a7SJohn Stultz 	/*
760d65670a7SJohn Stultz 	 * for clocksources that have large mults, to avoid overflow.
761d65670a7SJohn Stultz 	 * Since mult may be adjusted by ntp, add an safety extra margin
762d65670a7SJohn Stultz 	 *
763d65670a7SJohn Stultz 	 */
764d65670a7SJohn Stultz 	cs->maxadj = clocksource_max_adjustment(cs);
765d65670a7SJohn Stultz 	while ((cs->mult + cs->maxadj < cs->mult)
766d65670a7SJohn Stultz 		|| (cs->mult - cs->maxadj > cs->mult)) {
767d65670a7SJohn Stultz 		cs->mult >>= 1;
768d65670a7SJohn Stultz 		cs->shift--;
769d65670a7SJohn Stultz 		cs->maxadj = clocksource_max_adjustment(cs);
770d65670a7SJohn Stultz 	}
771d65670a7SJohn Stultz 
772852db46dSJohn Stultz 	cs->max_idle_ns = clocksource_max_deferment(cs);
773852db46dSJohn Stultz }
774852db46dSJohn Stultz EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
775852db46dSJohn Stultz 
776852db46dSJohn Stultz /**
777d7e81c26SJohn Stultz  * __clocksource_register_scale - Used to install new clocksources
778b1b73d09SKusanagi Kouichi  * @cs:		clocksource to be registered
779d7e81c26SJohn Stultz  * @scale:	Scale factor multiplied against freq to get clocksource hz
780d7e81c26SJohn Stultz  * @freq:	clocksource frequency (cycles per second) divided by scale
781d7e81c26SJohn Stultz  *
782d7e81c26SJohn Stultz  * Returns -EBUSY if registration fails, zero otherwise.
783d7e81c26SJohn Stultz  *
784d7e81c26SJohn Stultz  * This *SHOULD NOT* be called directly! Please use the
785d7e81c26SJohn Stultz  * clocksource_register_hz() or clocksource_register_khz helper functions.
786d7e81c26SJohn Stultz  */
787d7e81c26SJohn Stultz int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
788d7e81c26SJohn Stultz {
789d7e81c26SJohn Stultz 
790b595076aSUwe Kleine-König 	/* Initialize mult/shift and max_idle_ns */
791852db46dSJohn Stultz 	__clocksource_updatefreq_scale(cs, scale, freq);
792d7e81c26SJohn Stultz 
793852db46dSJohn Stultz 	/* Add clocksource to the clcoksource list */
794d7e81c26SJohn Stultz 	mutex_lock(&clocksource_mutex);
795d7e81c26SJohn Stultz 	clocksource_enqueue(cs);
796d7e81c26SJohn Stultz 	clocksource_enqueue_watchdog(cs);
797e05b2efbSjohn stultz 	clocksource_select();
798d7e81c26SJohn Stultz 	mutex_unlock(&clocksource_mutex);
799d7e81c26SJohn Stultz 	return 0;
800d7e81c26SJohn Stultz }
801d7e81c26SJohn Stultz EXPORT_SYMBOL_GPL(__clocksource_register_scale);
802d7e81c26SJohn Stultz 
803d7e81c26SJohn Stultz 
804734efb46Sjohn stultz /**
805a2752549Sjohn stultz  * clocksource_register - Used to install new clocksources
806b1b73d09SKusanagi Kouichi  * @cs:		clocksource to be registered
807734efb46Sjohn stultz  *
808734efb46Sjohn stultz  * Returns -EBUSY if registration fails, zero otherwise.
809734efb46Sjohn stultz  */
810f1b82746SMartin Schwidefsky int clocksource_register(struct clocksource *cs)
811734efb46Sjohn stultz {
812d65670a7SJohn Stultz 	/* calculate max adjustment for given mult/shift */
813d65670a7SJohn Stultz 	cs->maxadj = clocksource_max_adjustment(cs);
814d65670a7SJohn Stultz 	WARN_ONCE(cs->mult + cs->maxadj < cs->mult,
815d65670a7SJohn Stultz 		"Clocksource %s might overflow on 11%% adjustment\n",
816d65670a7SJohn Stultz 		cs->name);
817d65670a7SJohn Stultz 
81898962465SJon Hunter 	/* calculate max idle time permitted for this clocksource */
81998962465SJon Hunter 	cs->max_idle_ns = clocksource_max_deferment(cs);
82098962465SJon Hunter 
82175c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
822f1b82746SMartin Schwidefsky 	clocksource_enqueue(cs);
823fb63a0ebSMartin Schwidefsky 	clocksource_enqueue_watchdog(cs);
824e05b2efbSjohn stultz 	clocksource_select();
82575c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
826f1b82746SMartin Schwidefsky 	return 0;
827734efb46Sjohn stultz }
828a2752549Sjohn stultz EXPORT_SYMBOL(clocksource_register);
829734efb46Sjohn stultz 
830d0981a1bSThomas Gleixner static void __clocksource_change_rating(struct clocksource *cs, int rating)
831d0981a1bSThomas Gleixner {
832d0981a1bSThomas Gleixner 	list_del(&cs->list);
833d0981a1bSThomas Gleixner 	cs->rating = rating;
834d0981a1bSThomas Gleixner 	clocksource_enqueue(cs);
835d0981a1bSThomas Gleixner }
836d0981a1bSThomas Gleixner 
837734efb46Sjohn stultz /**
83892c7e002SThomas Gleixner  * clocksource_change_rating - Change the rating of a registered clocksource
839b1b73d09SKusanagi Kouichi  * @cs:		clocksource to be changed
840b1b73d09SKusanagi Kouichi  * @rating:	new rating
841734efb46Sjohn stultz  */
84292c7e002SThomas Gleixner void clocksource_change_rating(struct clocksource *cs, int rating)
843734efb46Sjohn stultz {
84475c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
845d0981a1bSThomas Gleixner 	__clocksource_change_rating(cs, rating);
846332962f2SThomas Gleixner 	clocksource_select();
84775c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
848734efb46Sjohn stultz }
849fb63a0ebSMartin Schwidefsky EXPORT_SYMBOL(clocksource_change_rating);
850734efb46Sjohn stultz 
8517eaeb343SThomas Gleixner /*
8527eaeb343SThomas Gleixner  * Unbind clocksource @cs. Called with clocksource_mutex held
8537eaeb343SThomas Gleixner  */
8547eaeb343SThomas Gleixner static int clocksource_unbind(struct clocksource *cs)
8557eaeb343SThomas Gleixner {
8567eaeb343SThomas Gleixner 	/*
8577eaeb343SThomas Gleixner 	 * I really can't convince myself to support this on hardware
8587eaeb343SThomas Gleixner 	 * designed by lobotomized monkeys.
8597eaeb343SThomas Gleixner 	 */
8607eaeb343SThomas Gleixner 	if (clocksource_is_watchdog(cs))
8617eaeb343SThomas Gleixner 		return -EBUSY;
8627eaeb343SThomas Gleixner 
8637eaeb343SThomas Gleixner 	if (cs == curr_clocksource) {
8647eaeb343SThomas Gleixner 		/* Select and try to install a replacement clock source */
8657eaeb343SThomas Gleixner 		clocksource_select_fallback();
8667eaeb343SThomas Gleixner 		if (curr_clocksource == cs)
8677eaeb343SThomas Gleixner 			return -EBUSY;
8687eaeb343SThomas Gleixner 	}
8697eaeb343SThomas Gleixner 	clocksource_dequeue_watchdog(cs);
8707eaeb343SThomas Gleixner 	list_del_init(&cs->list);
8717eaeb343SThomas Gleixner 	return 0;
8727eaeb343SThomas Gleixner }
8737eaeb343SThomas Gleixner 
8744713e22cSThomas Gleixner /**
8754713e22cSThomas Gleixner  * clocksource_unregister - remove a registered clocksource
876b1b73d09SKusanagi Kouichi  * @cs:	clocksource to be unregistered
8774713e22cSThomas Gleixner  */
878a89c7edbSThomas Gleixner int clocksource_unregister(struct clocksource *cs)
8794713e22cSThomas Gleixner {
880a89c7edbSThomas Gleixner 	int ret = 0;
881a89c7edbSThomas Gleixner 
88275c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
883a89c7edbSThomas Gleixner 	if (!list_empty(&cs->list))
884a89c7edbSThomas Gleixner 		ret = clocksource_unbind(cs);
88575c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
886a89c7edbSThomas Gleixner 	return ret;
8874713e22cSThomas Gleixner }
888fb63a0ebSMartin Schwidefsky EXPORT_SYMBOL(clocksource_unregister);
8894713e22cSThomas Gleixner 
8902b013700SDaniel Walker #ifdef CONFIG_SYSFS
891734efb46Sjohn stultz /**
892734efb46Sjohn stultz  * sysfs_show_current_clocksources - sysfs interface for current clocksource
893734efb46Sjohn stultz  * @dev:	unused
894b1b73d09SKusanagi Kouichi  * @attr:	unused
895734efb46Sjohn stultz  * @buf:	char buffer to be filled with clocksource list
896734efb46Sjohn stultz  *
897734efb46Sjohn stultz  * Provides sysfs interface for listing current clocksource.
898734efb46Sjohn stultz  */
899734efb46Sjohn stultz static ssize_t
900d369a5d8SKay Sievers sysfs_show_current_clocksources(struct device *dev,
901d369a5d8SKay Sievers 				struct device_attribute *attr, char *buf)
902734efb46Sjohn stultz {
9035e2cb101SMiao Xie 	ssize_t count = 0;
904734efb46Sjohn stultz 
90575c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
9065e2cb101SMiao Xie 	count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
90775c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
908734efb46Sjohn stultz 
9095e2cb101SMiao Xie 	return count;
910734efb46Sjohn stultz }
911734efb46Sjohn stultz 
912891292a7SPatrick Palka ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
91329b54078SThomas Gleixner {
91429b54078SThomas Gleixner 	size_t ret = cnt;
91529b54078SThomas Gleixner 
91629b54078SThomas Gleixner 	/* strings from sysfs write are not 0 terminated! */
91729b54078SThomas Gleixner 	if (!cnt || cnt >= CS_NAME_LEN)
91829b54078SThomas Gleixner 		return -EINVAL;
91929b54078SThomas Gleixner 
92029b54078SThomas Gleixner 	/* strip of \n: */
92129b54078SThomas Gleixner 	if (buf[cnt-1] == '\n')
92229b54078SThomas Gleixner 		cnt--;
92329b54078SThomas Gleixner 	if (cnt > 0)
92429b54078SThomas Gleixner 		memcpy(dst, buf, cnt);
92529b54078SThomas Gleixner 	dst[cnt] = 0;
92629b54078SThomas Gleixner 	return ret;
92729b54078SThomas Gleixner }
92829b54078SThomas Gleixner 
929734efb46Sjohn stultz /**
930734efb46Sjohn stultz  * sysfs_override_clocksource - interface for manually overriding clocksource
931734efb46Sjohn stultz  * @dev:	unused
932b1b73d09SKusanagi Kouichi  * @attr:	unused
933734efb46Sjohn stultz  * @buf:	name of override clocksource
934734efb46Sjohn stultz  * @count:	length of buffer
935734efb46Sjohn stultz  *
936734efb46Sjohn stultz  * Takes input from sysfs interface for manually overriding the default
937b71a8eb0SUwe Kleine-König  * clocksource selection.
938734efb46Sjohn stultz  */
939d369a5d8SKay Sievers static ssize_t sysfs_override_clocksource(struct device *dev,
940d369a5d8SKay Sievers 					  struct device_attribute *attr,
941734efb46Sjohn stultz 					  const char *buf, size_t count)
942734efb46Sjohn stultz {
943233bcb41SElad Wexler 	ssize_t ret;
944734efb46Sjohn stultz 
94575c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
946734efb46Sjohn stultz 
94703e13cf5SThomas Gleixner 	ret = sysfs_get_uname(buf, override_name, count);
94829b54078SThomas Gleixner 	if (ret >= 0)
949f1b82746SMartin Schwidefsky 		clocksource_select();
950734efb46Sjohn stultz 
95175c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
952734efb46Sjohn stultz 
953734efb46Sjohn stultz 	return ret;
954734efb46Sjohn stultz }
955734efb46Sjohn stultz 
956734efb46Sjohn stultz /**
9577eaeb343SThomas Gleixner  * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource
9587eaeb343SThomas Gleixner  * @dev:	unused
9597eaeb343SThomas Gleixner  * @attr:	unused
9607eaeb343SThomas Gleixner  * @buf:	unused
9617eaeb343SThomas Gleixner  * @count:	length of buffer
9627eaeb343SThomas Gleixner  *
9637eaeb343SThomas Gleixner  * Takes input from sysfs interface for manually unbinding a clocksource.
9647eaeb343SThomas Gleixner  */
9657eaeb343SThomas Gleixner static ssize_t sysfs_unbind_clocksource(struct device *dev,
9667eaeb343SThomas Gleixner 					struct device_attribute *attr,
9677eaeb343SThomas Gleixner 					const char *buf, size_t count)
9687eaeb343SThomas Gleixner {
9697eaeb343SThomas Gleixner 	struct clocksource *cs;
9707eaeb343SThomas Gleixner 	char name[CS_NAME_LEN];
971233bcb41SElad Wexler 	ssize_t ret;
9727eaeb343SThomas Gleixner 
97303e13cf5SThomas Gleixner 	ret = sysfs_get_uname(buf, name, count);
9747eaeb343SThomas Gleixner 	if (ret < 0)
9757eaeb343SThomas Gleixner 		return ret;
9767eaeb343SThomas Gleixner 
9777eaeb343SThomas Gleixner 	ret = -ENODEV;
9787eaeb343SThomas Gleixner 	mutex_lock(&clocksource_mutex);
9797eaeb343SThomas Gleixner 	list_for_each_entry(cs, &clocksource_list, list) {
9807eaeb343SThomas Gleixner 		if (strcmp(cs->name, name))
9817eaeb343SThomas Gleixner 			continue;
9827eaeb343SThomas Gleixner 		ret = clocksource_unbind(cs);
9837eaeb343SThomas Gleixner 		break;
9847eaeb343SThomas Gleixner 	}
9857eaeb343SThomas Gleixner 	mutex_unlock(&clocksource_mutex);
9867eaeb343SThomas Gleixner 
9877eaeb343SThomas Gleixner 	return ret ? ret : count;
9887eaeb343SThomas Gleixner }
9897eaeb343SThomas Gleixner 
9907eaeb343SThomas Gleixner /**
991734efb46Sjohn stultz  * sysfs_show_available_clocksources - sysfs interface for listing clocksource
992734efb46Sjohn stultz  * @dev:	unused
993b1b73d09SKusanagi Kouichi  * @attr:	unused
994734efb46Sjohn stultz  * @buf:	char buffer to be filled with clocksource list
995734efb46Sjohn stultz  *
996734efb46Sjohn stultz  * Provides sysfs interface for listing registered clocksources
997734efb46Sjohn stultz  */
998734efb46Sjohn stultz static ssize_t
999d369a5d8SKay Sievers sysfs_show_available_clocksources(struct device *dev,
1000d369a5d8SKay Sievers 				  struct device_attribute *attr,
10014a0b2b4dSAndi Kleen 				  char *buf)
1002734efb46Sjohn stultz {
10032e197586SMatthias Kaehlcke 	struct clocksource *src;
10045e2cb101SMiao Xie 	ssize_t count = 0;
1005734efb46Sjohn stultz 
100675c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
10072e197586SMatthias Kaehlcke 	list_for_each_entry(src, &clocksource_list, list) {
1008cd6d95d8SThomas Gleixner 		/*
1009cd6d95d8SThomas Gleixner 		 * Don't show non-HRES clocksource if the tick code is
1010cd6d95d8SThomas Gleixner 		 * in one shot mode (highres=on or nohz=on)
1011cd6d95d8SThomas Gleixner 		 */
1012cd6d95d8SThomas Gleixner 		if (!tick_oneshot_mode_active() ||
10133f68535aSjohn stultz 		    (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
10145e2cb101SMiao Xie 			count += snprintf(buf + count,
10155e2cb101SMiao Xie 				  max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
10165e2cb101SMiao Xie 				  "%s ", src->name);
1017734efb46Sjohn stultz 	}
101875c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
1019734efb46Sjohn stultz 
10205e2cb101SMiao Xie 	count += snprintf(buf + count,
10215e2cb101SMiao Xie 			  max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
1022734efb46Sjohn stultz 
10235e2cb101SMiao Xie 	return count;
1024734efb46Sjohn stultz }
1025734efb46Sjohn stultz 
1026734efb46Sjohn stultz /*
1027734efb46Sjohn stultz  * Sysfs setup bits:
1028734efb46Sjohn stultz  */
1029d369a5d8SKay Sievers static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
1030734efb46Sjohn stultz 		   sysfs_override_clocksource);
1031734efb46Sjohn stultz 
10327eaeb343SThomas Gleixner static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
10337eaeb343SThomas Gleixner 
1034d369a5d8SKay Sievers static DEVICE_ATTR(available_clocksource, 0444,
1035734efb46Sjohn stultz 		   sysfs_show_available_clocksources, NULL);
1036734efb46Sjohn stultz 
1037d369a5d8SKay Sievers static struct bus_type clocksource_subsys = {
1038af5ca3f4SKay Sievers 	.name = "clocksource",
1039d369a5d8SKay Sievers 	.dev_name = "clocksource",
1040734efb46Sjohn stultz };
1041734efb46Sjohn stultz 
1042d369a5d8SKay Sievers static struct device device_clocksource = {
1043734efb46Sjohn stultz 	.id	= 0,
1044d369a5d8SKay Sievers 	.bus	= &clocksource_subsys,
1045734efb46Sjohn stultz };
1046734efb46Sjohn stultz 
1047ad596171Sjohn stultz static int __init init_clocksource_sysfs(void)
1048734efb46Sjohn stultz {
1049d369a5d8SKay Sievers 	int error = subsys_system_register(&clocksource_subsys, NULL);
1050734efb46Sjohn stultz 
1051734efb46Sjohn stultz 	if (!error)
1052d369a5d8SKay Sievers 		error = device_register(&device_clocksource);
1053734efb46Sjohn stultz 	if (!error)
1054d369a5d8SKay Sievers 		error = device_create_file(
1055734efb46Sjohn stultz 				&device_clocksource,
1056d369a5d8SKay Sievers 				&dev_attr_current_clocksource);
1057734efb46Sjohn stultz 	if (!error)
10587eaeb343SThomas Gleixner 		error = device_create_file(&device_clocksource,
10597eaeb343SThomas Gleixner 					   &dev_attr_unbind_clocksource);
10607eaeb343SThomas Gleixner 	if (!error)
1061d369a5d8SKay Sievers 		error = device_create_file(
1062734efb46Sjohn stultz 				&device_clocksource,
1063d369a5d8SKay Sievers 				&dev_attr_available_clocksource);
1064734efb46Sjohn stultz 	return error;
1065734efb46Sjohn stultz }
1066734efb46Sjohn stultz 
1067734efb46Sjohn stultz device_initcall(init_clocksource_sysfs);
10682b013700SDaniel Walker #endif /* CONFIG_SYSFS */
1069734efb46Sjohn stultz 
1070734efb46Sjohn stultz /**
1071734efb46Sjohn stultz  * boot_override_clocksource - boot clock override
1072734efb46Sjohn stultz  * @str:	override name
1073734efb46Sjohn stultz  *
1074734efb46Sjohn stultz  * Takes a clocksource= boot argument and uses it
1075734efb46Sjohn stultz  * as the clocksource override name.
1076734efb46Sjohn stultz  */
1077734efb46Sjohn stultz static int __init boot_override_clocksource(char* str)
1078734efb46Sjohn stultz {
107975c5158fSMartin Schwidefsky 	mutex_lock(&clocksource_mutex);
1080734efb46Sjohn stultz 	if (str)
1081734efb46Sjohn stultz 		strlcpy(override_name, str, sizeof(override_name));
108275c5158fSMartin Schwidefsky 	mutex_unlock(&clocksource_mutex);
1083734efb46Sjohn stultz 	return 1;
1084734efb46Sjohn stultz }
1085734efb46Sjohn stultz 
1086734efb46Sjohn stultz __setup("clocksource=", boot_override_clocksource);
1087734efb46Sjohn stultz 
1088734efb46Sjohn stultz /**
1089734efb46Sjohn stultz  * boot_override_clock - Compatibility layer for deprecated boot option
1090734efb46Sjohn stultz  * @str:	override name
1091734efb46Sjohn stultz  *
1092734efb46Sjohn stultz  * DEPRECATED! Takes a clock= boot argument and uses it
1093734efb46Sjohn stultz  * as the clocksource override name
1094734efb46Sjohn stultz  */
1095734efb46Sjohn stultz static int __init boot_override_clock(char* str)
1096734efb46Sjohn stultz {
10975d0cf410Sjohn stultz 	if (!strcmp(str, "pmtmr")) {
10985d0cf410Sjohn stultz 		printk("Warning: clock=pmtmr is deprecated. "
10995d0cf410Sjohn stultz 			"Use clocksource=acpi_pm.\n");
11005d0cf410Sjohn stultz 		return boot_override_clocksource("acpi_pm");
11015d0cf410Sjohn stultz 	}
11025d0cf410Sjohn stultz 	printk("Warning! clock= boot option is deprecated. "
11035d0cf410Sjohn stultz 		"Use clocksource=xyz\n");
1104734efb46Sjohn stultz 	return boot_override_clocksource(str);
1105734efb46Sjohn stultz }
1106734efb46Sjohn stultz 
1107734efb46Sjohn stultz __setup("clock=", boot_override_clock);
1108