xref: /openbmc/linux/kernel/time/clocksource.c (revision f1b82746)
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>
32734efb46Sjohn stultz 
33a038a353SPatrick Ohly void timecounter_init(struct timecounter *tc,
34a038a353SPatrick Ohly 		      const struct cyclecounter *cc,
35a038a353SPatrick Ohly 		      u64 start_tstamp)
36a038a353SPatrick Ohly {
37a038a353SPatrick Ohly 	tc->cc = cc;
38a038a353SPatrick Ohly 	tc->cycle_last = cc->read(cc);
39a038a353SPatrick Ohly 	tc->nsec = start_tstamp;
40a038a353SPatrick Ohly }
41a038a353SPatrick Ohly EXPORT_SYMBOL(timecounter_init);
42a038a353SPatrick Ohly 
43a038a353SPatrick Ohly /**
44a038a353SPatrick Ohly  * timecounter_read_delta - get nanoseconds since last call of this function
45a038a353SPatrick Ohly  * @tc:         Pointer to time counter
46a038a353SPatrick Ohly  *
47a038a353SPatrick Ohly  * When the underlying cycle counter runs over, this will be handled
48a038a353SPatrick Ohly  * correctly as long as it does not run over more than once between
49a038a353SPatrick Ohly  * calls.
50a038a353SPatrick Ohly  *
51a038a353SPatrick Ohly  * The first call to this function for a new time counter initializes
52a038a353SPatrick Ohly  * the time tracking and returns an undefined result.
53a038a353SPatrick Ohly  */
54a038a353SPatrick Ohly static u64 timecounter_read_delta(struct timecounter *tc)
55a038a353SPatrick Ohly {
56a038a353SPatrick Ohly 	cycle_t cycle_now, cycle_delta;
57a038a353SPatrick Ohly 	u64 ns_offset;
58a038a353SPatrick Ohly 
59a038a353SPatrick Ohly 	/* read cycle counter: */
60a038a353SPatrick Ohly 	cycle_now = tc->cc->read(tc->cc);
61a038a353SPatrick Ohly 
62a038a353SPatrick Ohly 	/* calculate the delta since the last timecounter_read_delta(): */
63a038a353SPatrick Ohly 	cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask;
64a038a353SPatrick Ohly 
65a038a353SPatrick Ohly 	/* convert to nanoseconds: */
66a038a353SPatrick Ohly 	ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta);
67a038a353SPatrick Ohly 
68a038a353SPatrick Ohly 	/* update time stamp of timecounter_read_delta() call: */
69a038a353SPatrick Ohly 	tc->cycle_last = cycle_now;
70a038a353SPatrick Ohly 
71a038a353SPatrick Ohly 	return ns_offset;
72a038a353SPatrick Ohly }
73a038a353SPatrick Ohly 
74a038a353SPatrick Ohly u64 timecounter_read(struct timecounter *tc)
75a038a353SPatrick Ohly {
76a038a353SPatrick Ohly 	u64 nsec;
77a038a353SPatrick Ohly 
78a038a353SPatrick Ohly 	/* increment time by nanoseconds since last call */
79a038a353SPatrick Ohly 	nsec = timecounter_read_delta(tc);
80a038a353SPatrick Ohly 	nsec += tc->nsec;
81a038a353SPatrick Ohly 	tc->nsec = nsec;
82a038a353SPatrick Ohly 
83a038a353SPatrick Ohly 	return nsec;
84a038a353SPatrick Ohly }
85a038a353SPatrick Ohly EXPORT_SYMBOL(timecounter_read);
86a038a353SPatrick Ohly 
87a038a353SPatrick Ohly u64 timecounter_cyc2time(struct timecounter *tc,
88a038a353SPatrick Ohly 			 cycle_t cycle_tstamp)
89a038a353SPatrick Ohly {
90a038a353SPatrick Ohly 	u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
91a038a353SPatrick Ohly 	u64 nsec;
92a038a353SPatrick Ohly 
93a038a353SPatrick Ohly 	/*
94a038a353SPatrick Ohly 	 * Instead of always treating cycle_tstamp as more recent
95a038a353SPatrick Ohly 	 * than tc->cycle_last, detect when it is too far in the
96a038a353SPatrick Ohly 	 * future and treat it as old time stamp instead.
97a038a353SPatrick Ohly 	 */
98a038a353SPatrick Ohly 	if (cycle_delta > tc->cc->mask / 2) {
99a038a353SPatrick Ohly 		cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
100a038a353SPatrick Ohly 		nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta);
101a038a353SPatrick Ohly 	} else {
102a038a353SPatrick Ohly 		nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec;
103a038a353SPatrick Ohly 	}
104a038a353SPatrick Ohly 
105a038a353SPatrick Ohly 	return nsec;
106a038a353SPatrick Ohly }
107a038a353SPatrick Ohly EXPORT_SYMBOL(timecounter_cyc2time);
108a038a353SPatrick Ohly 
109734efb46Sjohn stultz /*[Clocksource internal variables]---------
110734efb46Sjohn stultz  * curr_clocksource:
111f1b82746SMartin Schwidefsky  *	currently selected clocksource.
112734efb46Sjohn stultz  * next_clocksource:
113734efb46Sjohn stultz  *	pending next selected clocksource.
114734efb46Sjohn stultz  * clocksource_list:
115734efb46Sjohn stultz  *	linked list with the registered clocksources
116734efb46Sjohn stultz  * clocksource_lock:
117734efb46Sjohn stultz  *	protects manipulations to curr_clocksource and next_clocksource
118734efb46Sjohn stultz  *	and the clocksource_list
119734efb46Sjohn stultz  * override_name:
120734efb46Sjohn stultz  *	Name of the user-specified clocksource.
121734efb46Sjohn stultz  */
122f1b82746SMartin Schwidefsky static struct clocksource *curr_clocksource;
123734efb46Sjohn stultz static struct clocksource *next_clocksource;
124734efb46Sjohn stultz static LIST_HEAD(clocksource_list);
125734efb46Sjohn stultz static DEFINE_SPINLOCK(clocksource_lock);
126734efb46Sjohn stultz static char override_name[32];
127734efb46Sjohn stultz static int finished_booting;
128734efb46Sjohn stultz 
1296bb74df4Sjohn stultz /* clocksource_done_booting - Called near the end of core bootup
130734efb46Sjohn stultz  *
1316bb74df4Sjohn stultz  * Hack to avoid lots of clocksource churn at boot time.
1326bb74df4Sjohn stultz  * We use fs_initcall because we want this to start before
1336bb74df4Sjohn stultz  * device_initcall but after subsys_initcall.
134734efb46Sjohn stultz  */
135ad596171Sjohn stultz static int __init clocksource_done_booting(void)
136734efb46Sjohn stultz {
137734efb46Sjohn stultz 	finished_booting = 1;
138734efb46Sjohn stultz 	return 0;
139734efb46Sjohn stultz }
1406bb74df4Sjohn stultz fs_initcall(clocksource_done_booting);
141734efb46Sjohn stultz 
1425d8b34fdSThomas Gleixner #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
1435d8b34fdSThomas Gleixner static LIST_HEAD(watchdog_list);
1445d8b34fdSThomas Gleixner static struct clocksource *watchdog;
1455d8b34fdSThomas Gleixner static struct timer_list watchdog_timer;
1465d8b34fdSThomas Gleixner static DEFINE_SPINLOCK(watchdog_lock);
1475d8b34fdSThomas Gleixner static cycle_t watchdog_last;
1488f89441bSThomas Gleixner static unsigned long watchdog_resumed;
149b52f52a0SThomas Gleixner 
1505d8b34fdSThomas Gleixner /*
15135c35d1aSDaniel Walker  * Interval: 0.5sec Threshold: 0.0625s
1525d8b34fdSThomas Gleixner  */
1535d8b34fdSThomas Gleixner #define WATCHDOG_INTERVAL (HZ >> 1)
15435c35d1aSDaniel Walker #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
1555d8b34fdSThomas Gleixner 
1565d8b34fdSThomas Gleixner static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
1575d8b34fdSThomas Gleixner {
15835c35d1aSDaniel Walker 	if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
1595d8b34fdSThomas Gleixner 		return;
1605d8b34fdSThomas Gleixner 
1615d8b34fdSThomas Gleixner 	printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
1625d8b34fdSThomas Gleixner 	       cs->name, delta);
1635d8b34fdSThomas Gleixner 	cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
1645d8b34fdSThomas Gleixner 	clocksource_change_rating(cs, 0);
1655d8b34fdSThomas Gleixner 	list_del(&cs->wd_list);
1665d8b34fdSThomas Gleixner }
1675d8b34fdSThomas Gleixner 
1685d8b34fdSThomas Gleixner static void clocksource_watchdog(unsigned long data)
1695d8b34fdSThomas Gleixner {
1705d8b34fdSThomas Gleixner 	struct clocksource *cs, *tmp;
1715d8b34fdSThomas Gleixner 	cycle_t csnow, wdnow;
1725d8b34fdSThomas Gleixner 	int64_t wd_nsec, cs_nsec;
173b52f52a0SThomas Gleixner 	int resumed;
1745d8b34fdSThomas Gleixner 
1755d8b34fdSThomas Gleixner 	spin_lock(&watchdog_lock);
1765d8b34fdSThomas Gleixner 
1778f89441bSThomas Gleixner 	resumed = test_and_clear_bit(0, &watchdog_resumed);
178b52f52a0SThomas Gleixner 
1798e19608eSMagnus Damm 	wdnow = watchdog->read(watchdog);
1805d8b34fdSThomas Gleixner 	wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
1815d8b34fdSThomas Gleixner 	watchdog_last = wdnow;
1825d8b34fdSThomas Gleixner 
1835d8b34fdSThomas Gleixner 	list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
1848e19608eSMagnus Damm 		csnow = cs->read(cs);
185b52f52a0SThomas Gleixner 
186b52f52a0SThomas Gleixner 		if (unlikely(resumed)) {
187b52f52a0SThomas Gleixner 			cs->wd_last = csnow;
188b52f52a0SThomas Gleixner 			continue;
189b52f52a0SThomas Gleixner 		}
190b52f52a0SThomas Gleixner 
1915d8b34fdSThomas Gleixner 		/* Initialized ? */
1925d8b34fdSThomas Gleixner 		if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
1935d8b34fdSThomas Gleixner 			if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
1945d8b34fdSThomas Gleixner 			    (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
1955d8b34fdSThomas Gleixner 				cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
19679bf2bb3SThomas Gleixner 				/*
19779bf2bb3SThomas Gleixner 				 * We just marked the clocksource as
19879bf2bb3SThomas Gleixner 				 * highres-capable, notify the rest of the
19979bf2bb3SThomas Gleixner 				 * system as well so that we transition
20079bf2bb3SThomas Gleixner 				 * into high-res mode:
20179bf2bb3SThomas Gleixner 				 */
20279bf2bb3SThomas Gleixner 				tick_clock_notify();
2035d8b34fdSThomas Gleixner 			}
2045d8b34fdSThomas Gleixner 			cs->flags |= CLOCK_SOURCE_WATCHDOG;
2055d8b34fdSThomas Gleixner 			cs->wd_last = csnow;
2065d8b34fdSThomas Gleixner 		} else {
2075d8b34fdSThomas Gleixner 			cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
2085d8b34fdSThomas Gleixner 			cs->wd_last = csnow;
2095d8b34fdSThomas Gleixner 			/* Check the delta. Might remove from the list ! */
2105d8b34fdSThomas Gleixner 			clocksource_ratewd(cs, cs_nsec - wd_nsec);
2115d8b34fdSThomas Gleixner 		}
2125d8b34fdSThomas Gleixner 	}
2135d8b34fdSThomas Gleixner 
2145d8b34fdSThomas Gleixner 	if (!list_empty(&watchdog_list)) {
2156993fc5bSAndi Kleen 		/*
2166993fc5bSAndi Kleen 		 * Cycle through CPUs to check if the CPUs stay
2176993fc5bSAndi Kleen 		 * synchronized to each other.
2186993fc5bSAndi Kleen 		 */
2195db0e1e9SRusty Russell 		int next_cpu = cpumask_next(raw_smp_processor_id(),
2205db0e1e9SRusty Russell 					    cpu_online_mask);
2216993fc5bSAndi Kleen 
222cad0e458SMike Travis 		if (next_cpu >= nr_cpu_ids)
2236b954823SRusty Russell 			next_cpu = cpumask_first(cpu_online_mask);
2246993fc5bSAndi Kleen 		watchdog_timer.expires += WATCHDOG_INTERVAL;
2256993fc5bSAndi Kleen 		add_timer_on(&watchdog_timer, next_cpu);
2265d8b34fdSThomas Gleixner 	}
2275d8b34fdSThomas Gleixner 	spin_unlock(&watchdog_lock);
2285d8b34fdSThomas Gleixner }
229b52f52a0SThomas Gleixner static void clocksource_resume_watchdog(void)
230b52f52a0SThomas Gleixner {
2318f89441bSThomas Gleixner 	set_bit(0, &watchdog_resumed);
232b52f52a0SThomas Gleixner }
233b52f52a0SThomas Gleixner 
2345d8b34fdSThomas Gleixner static void clocksource_check_watchdog(struct clocksource *cs)
2355d8b34fdSThomas Gleixner {
2365d8b34fdSThomas Gleixner 	struct clocksource *cse;
2375d8b34fdSThomas Gleixner 	unsigned long flags;
2385d8b34fdSThomas Gleixner 
2395d8b34fdSThomas Gleixner 	spin_lock_irqsave(&watchdog_lock, flags);
2405d8b34fdSThomas Gleixner 	if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
2415d8b34fdSThomas Gleixner 		int started = !list_empty(&watchdog_list);
2425d8b34fdSThomas Gleixner 
2435d8b34fdSThomas Gleixner 		list_add(&cs->wd_list, &watchdog_list);
2445d8b34fdSThomas Gleixner 		if (!started && watchdog) {
2458e19608eSMagnus Damm 			watchdog_last = watchdog->read(watchdog);
2465d8b34fdSThomas Gleixner 			watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
2476993fc5bSAndi Kleen 			add_timer_on(&watchdog_timer,
2486b954823SRusty Russell 				     cpumask_first(cpu_online_mask));
2495d8b34fdSThomas Gleixner 		}
250948ac6d7SThomas Gleixner 	} else {
251948ac6d7SThomas Gleixner 		if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
2525d8b34fdSThomas Gleixner 			cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
2535d8b34fdSThomas Gleixner 
2545d8b34fdSThomas Gleixner 		if (!watchdog || cs->rating > watchdog->rating) {
2555d8b34fdSThomas Gleixner 			if (watchdog)
2565d8b34fdSThomas Gleixner 				del_timer(&watchdog_timer);
2575d8b34fdSThomas Gleixner 			watchdog = cs;
258898a19deSThomas Gleixner 			init_timer(&watchdog_timer);
2595d8b34fdSThomas Gleixner 			watchdog_timer.function = clocksource_watchdog;
2605d8b34fdSThomas Gleixner 
2615d8b34fdSThomas Gleixner 			/* Reset watchdog cycles */
2625d8b34fdSThomas Gleixner 			list_for_each_entry(cse, &watchdog_list, wd_list)
2635d8b34fdSThomas Gleixner 				cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
2645d8b34fdSThomas Gleixner 			/* Start if list is not empty */
2655d8b34fdSThomas Gleixner 			if (!list_empty(&watchdog_list)) {
2668e19608eSMagnus Damm 				watchdog_last = watchdog->read(watchdog);
2675d8b34fdSThomas Gleixner 				watchdog_timer.expires =
2685d8b34fdSThomas Gleixner 					jiffies + WATCHDOG_INTERVAL;
2696993fc5bSAndi Kleen 				add_timer_on(&watchdog_timer,
2706b954823SRusty Russell 					     cpumask_first(cpu_online_mask));
2715d8b34fdSThomas Gleixner 			}
2725d8b34fdSThomas Gleixner 		}
2735d8b34fdSThomas Gleixner 	}
2745d8b34fdSThomas Gleixner 	spin_unlock_irqrestore(&watchdog_lock, flags);
2755d8b34fdSThomas Gleixner }
2765d8b34fdSThomas Gleixner #else
2775d8b34fdSThomas Gleixner static void clocksource_check_watchdog(struct clocksource *cs)
2785d8b34fdSThomas Gleixner {
2795d8b34fdSThomas Gleixner 	if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
2805d8b34fdSThomas Gleixner 		cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
2815d8b34fdSThomas Gleixner }
282b52f52a0SThomas Gleixner 
283b52f52a0SThomas Gleixner static inline void clocksource_resume_watchdog(void) { }
2845d8b34fdSThomas Gleixner #endif
2855d8b34fdSThomas Gleixner 
286734efb46Sjohn stultz /**
287b52f52a0SThomas Gleixner  * clocksource_resume - resume the clocksource(s)
288b52f52a0SThomas Gleixner  */
289b52f52a0SThomas Gleixner void clocksource_resume(void)
290b52f52a0SThomas Gleixner {
2912e197586SMatthias Kaehlcke 	struct clocksource *cs;
292b52f52a0SThomas Gleixner 	unsigned long flags;
293b52f52a0SThomas Gleixner 
294b52f52a0SThomas Gleixner 	spin_lock_irqsave(&clocksource_lock, flags);
295b52f52a0SThomas Gleixner 
2962e197586SMatthias Kaehlcke 	list_for_each_entry(cs, &clocksource_list, list) {
297b52f52a0SThomas Gleixner 		if (cs->resume)
298b52f52a0SThomas Gleixner 			cs->resume();
299b52f52a0SThomas Gleixner 	}
300b52f52a0SThomas Gleixner 
301b52f52a0SThomas Gleixner 	clocksource_resume_watchdog();
302b52f52a0SThomas Gleixner 
303b52f52a0SThomas Gleixner 	spin_unlock_irqrestore(&clocksource_lock, flags);
304b52f52a0SThomas Gleixner }
305b52f52a0SThomas Gleixner 
306b52f52a0SThomas Gleixner /**
3077c3078b6SJason Wessel  * clocksource_touch_watchdog - Update watchdog
3087c3078b6SJason Wessel  *
3097c3078b6SJason Wessel  * Update the watchdog after exception contexts such as kgdb so as not
3107c3078b6SJason Wessel  * to incorrectly trip the watchdog.
3117c3078b6SJason Wessel  *
3127c3078b6SJason Wessel  */
3137c3078b6SJason Wessel void clocksource_touch_watchdog(void)
3147c3078b6SJason Wessel {
3157c3078b6SJason Wessel 	clocksource_resume_watchdog();
3167c3078b6SJason Wessel }
3177c3078b6SJason Wessel 
318f1b82746SMartin Schwidefsky #ifdef CONFIG_GENERIC_TIME
3197c3078b6SJason Wessel /**
320a2752549Sjohn stultz  * clocksource_get_next - Returns the selected clocksource
321734efb46Sjohn stultz  *
322734efb46Sjohn stultz  */
323a2752549Sjohn stultz struct clocksource *clocksource_get_next(void)
324734efb46Sjohn stultz {
325734efb46Sjohn stultz 	unsigned long flags;
326734efb46Sjohn stultz 
327734efb46Sjohn stultz 	spin_lock_irqsave(&clocksource_lock, flags);
328734efb46Sjohn stultz 	if (next_clocksource && finished_booting) {
329734efb46Sjohn stultz 		curr_clocksource = next_clocksource;
330734efb46Sjohn stultz 		next_clocksource = NULL;
331734efb46Sjohn stultz 	}
332734efb46Sjohn stultz 	spin_unlock_irqrestore(&clocksource_lock, flags);
333734efb46Sjohn stultz 
334734efb46Sjohn stultz 	return curr_clocksource;
335734efb46Sjohn stultz }
336734efb46Sjohn stultz 
337734efb46Sjohn stultz /**
338f1b82746SMartin Schwidefsky  * clocksource_select - Select the best clocksource available
339734efb46Sjohn stultz  *
340734efb46Sjohn stultz  * Private function. Must hold clocksource_lock when called.
341734efb46Sjohn stultz  *
34292c7e002SThomas Gleixner  * Select the clocksource with the best rating, or the clocksource,
34392c7e002SThomas Gleixner  * which is selected by userspace override.
344734efb46Sjohn stultz  */
345f1b82746SMartin Schwidefsky static void clocksource_select(void)
346734efb46Sjohn stultz {
347f1b82746SMartin Schwidefsky 	struct clocksource *best, *cs;
3485d8b34fdSThomas Gleixner 
34992c7e002SThomas Gleixner 	if (list_empty(&clocksource_list))
350f1b82746SMartin Schwidefsky 		return;
351f1b82746SMartin Schwidefsky 	/* First clocksource on the list has the best rating. */
352f1b82746SMartin Schwidefsky 	best = list_first_entry(&clocksource_list, struct clocksource, list);
353f1b82746SMartin Schwidefsky 	/* Check for the override clocksource. */
354f1b82746SMartin Schwidefsky 	list_for_each_entry(cs, &clocksource_list, list) {
355f1b82746SMartin Schwidefsky 		if (strcmp(cs->name, override_name) != 0)
356f1b82746SMartin Schwidefsky 			continue;
357f1b82746SMartin Schwidefsky 		/*
358f1b82746SMartin Schwidefsky 		 * Check to make sure we don't switch to a non-highres
359f1b82746SMartin Schwidefsky 		 * capable clocksource if the tick code is in oneshot
360f1b82746SMartin Schwidefsky 		 * mode (highres or nohz)
361f1b82746SMartin Schwidefsky 		 */
362f1b82746SMartin Schwidefsky 		if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
363f1b82746SMartin Schwidefsky 		    tick_oneshot_mode_active()) {
364f1b82746SMartin Schwidefsky 			/* Override clocksource cannot be used. */
365f1b82746SMartin Schwidefsky 			printk(KERN_WARNING "Override clocksource %s is not "
366f1b82746SMartin Schwidefsky 			       "HRT compatible. Cannot switch while in "
367f1b82746SMartin Schwidefsky 			       "HRT/NOHZ mode\n", cs->name);
368f1b82746SMartin Schwidefsky 			override_name[0] = 0;
369f1b82746SMartin Schwidefsky 		} else
370f1b82746SMartin Schwidefsky 			/* Override clocksource can be used. */
371f1b82746SMartin Schwidefsky 			best = cs;
372f1b82746SMartin Schwidefsky 		break;
373734efb46Sjohn stultz 	}
374f1b82746SMartin Schwidefsky 	if (curr_clocksource != best)
375f1b82746SMartin Schwidefsky 		next_clocksource = best;
376f1b82746SMartin Schwidefsky }
377f1b82746SMartin Schwidefsky 
378f1b82746SMartin Schwidefsky #else /* CONFIG_GENERIC_TIME */
379f1b82746SMartin Schwidefsky 
380f1b82746SMartin Schwidefsky static void clocksource_select(void) { }
381f1b82746SMartin Schwidefsky 
382f1b82746SMartin Schwidefsky #endif
383734efb46Sjohn stultz 
38492c7e002SThomas Gleixner /*
38592c7e002SThomas Gleixner  * Enqueue the clocksource sorted by rating
386734efb46Sjohn stultz  */
387f1b82746SMartin Schwidefsky static void clocksource_enqueue(struct clocksource *cs)
388734efb46Sjohn stultz {
389f1b82746SMartin Schwidefsky 	struct list_head *entry = &clocksource_list;
390f1b82746SMartin Schwidefsky 	struct clocksource *tmp;
391734efb46Sjohn stultz 
392f1b82746SMartin Schwidefsky 	list_for_each_entry(tmp, &clocksource_list, list)
39392c7e002SThomas Gleixner 		/* Keep track of the place, where to insert */
394f1b82746SMartin Schwidefsky 		if (tmp->rating >= cs->rating)
395f1b82746SMartin Schwidefsky 			entry = &tmp->list;
396f1b82746SMartin Schwidefsky 	list_add(&cs->list, entry);
397734efb46Sjohn stultz }
398734efb46Sjohn stultz 
399734efb46Sjohn stultz /**
400a2752549Sjohn stultz  * clocksource_register - Used to install new clocksources
401734efb46Sjohn stultz  * @t:		clocksource to be registered
402734efb46Sjohn stultz  *
403734efb46Sjohn stultz  * Returns -EBUSY if registration fails, zero otherwise.
404734efb46Sjohn stultz  */
405f1b82746SMartin Schwidefsky int clocksource_register(struct clocksource *cs)
406734efb46Sjohn stultz {
407734efb46Sjohn stultz 	unsigned long flags;
408734efb46Sjohn stultz 
409734efb46Sjohn stultz 	spin_lock_irqsave(&clocksource_lock, flags);
410f1b82746SMartin Schwidefsky 	clocksource_enqueue(cs);
411f1b82746SMartin Schwidefsky 	clocksource_select();
412734efb46Sjohn stultz 	spin_unlock_irqrestore(&clocksource_lock, flags);
413f1b82746SMartin Schwidefsky 	clocksource_check_watchdog(cs);
414f1b82746SMartin Schwidefsky 	return 0;
415734efb46Sjohn stultz }
416a2752549Sjohn stultz EXPORT_SYMBOL(clocksource_register);
417734efb46Sjohn stultz 
418734efb46Sjohn stultz /**
41992c7e002SThomas Gleixner  * clocksource_change_rating - Change the rating of a registered clocksource
420734efb46Sjohn stultz  *
421734efb46Sjohn stultz  */
42292c7e002SThomas Gleixner void clocksource_change_rating(struct clocksource *cs, int rating)
423734efb46Sjohn stultz {
424734efb46Sjohn stultz 	unsigned long flags;
425734efb46Sjohn stultz 
426734efb46Sjohn stultz 	spin_lock_irqsave(&clocksource_lock, flags);
42792c7e002SThomas Gleixner 	list_del(&cs->list);
4285d8b34fdSThomas Gleixner 	cs->rating = rating;
42992c7e002SThomas Gleixner 	clocksource_enqueue(cs);
430f1b82746SMartin Schwidefsky 	clocksource_select();
431734efb46Sjohn stultz 	spin_unlock_irqrestore(&clocksource_lock, flags);
432734efb46Sjohn stultz }
433734efb46Sjohn stultz 
4344713e22cSThomas Gleixner /**
4354713e22cSThomas Gleixner  * clocksource_unregister - remove a registered clocksource
4364713e22cSThomas Gleixner  */
4374713e22cSThomas Gleixner void clocksource_unregister(struct clocksource *cs)
4384713e22cSThomas Gleixner {
4394713e22cSThomas Gleixner 	unsigned long flags;
4404713e22cSThomas Gleixner 
4414713e22cSThomas Gleixner 	spin_lock_irqsave(&clocksource_lock, flags);
4424713e22cSThomas Gleixner 	list_del(&cs->list);
443f1b82746SMartin Schwidefsky 	clocksource_select();
4444713e22cSThomas Gleixner 	spin_unlock_irqrestore(&clocksource_lock, flags);
4454713e22cSThomas Gleixner }
4464713e22cSThomas Gleixner 
4472b013700SDaniel Walker #ifdef CONFIG_SYSFS
448734efb46Sjohn stultz /**
449734efb46Sjohn stultz  * sysfs_show_current_clocksources - sysfs interface for current clocksource
450734efb46Sjohn stultz  * @dev:	unused
451734efb46Sjohn stultz  * @buf:	char buffer to be filled with clocksource list
452734efb46Sjohn stultz  *
453734efb46Sjohn stultz  * Provides sysfs interface for listing current clocksource.
454734efb46Sjohn stultz  */
455734efb46Sjohn stultz static ssize_t
4564a0b2b4dSAndi Kleen sysfs_show_current_clocksources(struct sys_device *dev,
4574a0b2b4dSAndi Kleen 				struct sysdev_attribute *attr, char *buf)
458734efb46Sjohn stultz {
4595e2cb101SMiao Xie 	ssize_t count = 0;
460734efb46Sjohn stultz 
461734efb46Sjohn stultz 	spin_lock_irq(&clocksource_lock);
4625e2cb101SMiao Xie 	count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
463734efb46Sjohn stultz 	spin_unlock_irq(&clocksource_lock);
464734efb46Sjohn stultz 
4655e2cb101SMiao Xie 	return count;
466734efb46Sjohn stultz }
467734efb46Sjohn stultz 
468734efb46Sjohn stultz /**
469734efb46Sjohn stultz  * sysfs_override_clocksource - interface for manually overriding clocksource
470734efb46Sjohn stultz  * @dev:	unused
471734efb46Sjohn stultz  * @buf:	name of override clocksource
472734efb46Sjohn stultz  * @count:	length of buffer
473734efb46Sjohn stultz  *
474734efb46Sjohn stultz  * Takes input from sysfs interface for manually overriding the default
475734efb46Sjohn stultz  * clocksource selction.
476734efb46Sjohn stultz  */
477734efb46Sjohn stultz static ssize_t sysfs_override_clocksource(struct sys_device *dev,
4784a0b2b4dSAndi Kleen 					  struct sysdev_attribute *attr,
479734efb46Sjohn stultz 					  const char *buf, size_t count)
480734efb46Sjohn stultz {
481734efb46Sjohn stultz 	size_t ret = count;
48292c7e002SThomas Gleixner 
483734efb46Sjohn stultz 	/* strings from sysfs write are not 0 terminated! */
484734efb46Sjohn stultz 	if (count >= sizeof(override_name))
485734efb46Sjohn stultz 		return -EINVAL;
486734efb46Sjohn stultz 
487734efb46Sjohn stultz 	/* strip of \n: */
488734efb46Sjohn stultz 	if (buf[count-1] == '\n')
489734efb46Sjohn stultz 		count--;
490734efb46Sjohn stultz 
491734efb46Sjohn stultz 	spin_lock_irq(&clocksource_lock);
492734efb46Sjohn stultz 
49392c7e002SThomas Gleixner 	if (count > 0)
494734efb46Sjohn stultz 		memcpy(override_name, buf, count);
495734efb46Sjohn stultz 	override_name[count] = 0;
496f1b82746SMartin Schwidefsky 	clocksource_select();
497734efb46Sjohn stultz 
498734efb46Sjohn stultz 	spin_unlock_irq(&clocksource_lock);
499734efb46Sjohn stultz 
500734efb46Sjohn stultz 	return ret;
501734efb46Sjohn stultz }
502734efb46Sjohn stultz 
503734efb46Sjohn stultz /**
504734efb46Sjohn stultz  * sysfs_show_available_clocksources - sysfs interface for listing clocksource
505734efb46Sjohn stultz  * @dev:	unused
506734efb46Sjohn stultz  * @buf:	char buffer to be filled with clocksource list
507734efb46Sjohn stultz  *
508734efb46Sjohn stultz  * Provides sysfs interface for listing registered clocksources
509734efb46Sjohn stultz  */
510734efb46Sjohn stultz static ssize_t
5114a0b2b4dSAndi Kleen sysfs_show_available_clocksources(struct sys_device *dev,
5124a0b2b4dSAndi Kleen 				  struct sysdev_attribute *attr,
5134a0b2b4dSAndi Kleen 				  char *buf)
514734efb46Sjohn stultz {
5152e197586SMatthias Kaehlcke 	struct clocksource *src;
5165e2cb101SMiao Xie 	ssize_t count = 0;
517734efb46Sjohn stultz 
518734efb46Sjohn stultz 	spin_lock_irq(&clocksource_lock);
5192e197586SMatthias Kaehlcke 	list_for_each_entry(src, &clocksource_list, list) {
520cd6d95d8SThomas Gleixner 		/*
521cd6d95d8SThomas Gleixner 		 * Don't show non-HRES clocksource if the tick code is
522cd6d95d8SThomas Gleixner 		 * in one shot mode (highres=on or nohz=on)
523cd6d95d8SThomas Gleixner 		 */
524cd6d95d8SThomas Gleixner 		if (!tick_oneshot_mode_active() ||
5253f68535aSjohn stultz 		    (src->flags & CLOCK_SOURCE_VALID_FOR_HRES))
5265e2cb101SMiao Xie 			count += snprintf(buf + count,
5275e2cb101SMiao Xie 				  max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
5285e2cb101SMiao Xie 				  "%s ", src->name);
529734efb46Sjohn stultz 	}
530734efb46Sjohn stultz 	spin_unlock_irq(&clocksource_lock);
531734efb46Sjohn stultz 
5325e2cb101SMiao Xie 	count += snprintf(buf + count,
5335e2cb101SMiao Xie 			  max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
534734efb46Sjohn stultz 
5355e2cb101SMiao Xie 	return count;
536734efb46Sjohn stultz }
537734efb46Sjohn stultz 
538734efb46Sjohn stultz /*
539734efb46Sjohn stultz  * Sysfs setup bits:
540734efb46Sjohn stultz  */
5414f95f81aSHeiko Carstens static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
542734efb46Sjohn stultz 		   sysfs_override_clocksource);
543734efb46Sjohn stultz 
5444f95f81aSHeiko Carstens static SYSDEV_ATTR(available_clocksource, 0444,
545734efb46Sjohn stultz 		   sysfs_show_available_clocksources, NULL);
546734efb46Sjohn stultz 
547734efb46Sjohn stultz static struct sysdev_class clocksource_sysclass = {
548af5ca3f4SKay Sievers 	.name = "clocksource",
549734efb46Sjohn stultz };
550734efb46Sjohn stultz 
551734efb46Sjohn stultz static struct sys_device device_clocksource = {
552734efb46Sjohn stultz 	.id	= 0,
553734efb46Sjohn stultz 	.cls	= &clocksource_sysclass,
554734efb46Sjohn stultz };
555734efb46Sjohn stultz 
556ad596171Sjohn stultz static int __init init_clocksource_sysfs(void)
557734efb46Sjohn stultz {
558734efb46Sjohn stultz 	int error = sysdev_class_register(&clocksource_sysclass);
559734efb46Sjohn stultz 
560734efb46Sjohn stultz 	if (!error)
561734efb46Sjohn stultz 		error = sysdev_register(&device_clocksource);
562734efb46Sjohn stultz 	if (!error)
563734efb46Sjohn stultz 		error = sysdev_create_file(
564734efb46Sjohn stultz 				&device_clocksource,
565734efb46Sjohn stultz 				&attr_current_clocksource);
566734efb46Sjohn stultz 	if (!error)
567734efb46Sjohn stultz 		error = sysdev_create_file(
568734efb46Sjohn stultz 				&device_clocksource,
569734efb46Sjohn stultz 				&attr_available_clocksource);
570734efb46Sjohn stultz 	return error;
571734efb46Sjohn stultz }
572734efb46Sjohn stultz 
573734efb46Sjohn stultz device_initcall(init_clocksource_sysfs);
5742b013700SDaniel Walker #endif /* CONFIG_SYSFS */
575734efb46Sjohn stultz 
576734efb46Sjohn stultz /**
577734efb46Sjohn stultz  * boot_override_clocksource - boot clock override
578734efb46Sjohn stultz  * @str:	override name
579734efb46Sjohn stultz  *
580734efb46Sjohn stultz  * Takes a clocksource= boot argument and uses it
581734efb46Sjohn stultz  * as the clocksource override name.
582734efb46Sjohn stultz  */
583734efb46Sjohn stultz static int __init boot_override_clocksource(char* str)
584734efb46Sjohn stultz {
585734efb46Sjohn stultz 	unsigned long flags;
586734efb46Sjohn stultz 	spin_lock_irqsave(&clocksource_lock, flags);
587734efb46Sjohn stultz 	if (str)
588734efb46Sjohn stultz 		strlcpy(override_name, str, sizeof(override_name));
589734efb46Sjohn stultz 	spin_unlock_irqrestore(&clocksource_lock, flags);
590734efb46Sjohn stultz 	return 1;
591734efb46Sjohn stultz }
592734efb46Sjohn stultz 
593734efb46Sjohn stultz __setup("clocksource=", boot_override_clocksource);
594734efb46Sjohn stultz 
595734efb46Sjohn stultz /**
596734efb46Sjohn stultz  * boot_override_clock - Compatibility layer for deprecated boot option
597734efb46Sjohn stultz  * @str:	override name
598734efb46Sjohn stultz  *
599734efb46Sjohn stultz  * DEPRECATED! Takes a clock= boot argument and uses it
600734efb46Sjohn stultz  * as the clocksource override name
601734efb46Sjohn stultz  */
602734efb46Sjohn stultz static int __init boot_override_clock(char* str)
603734efb46Sjohn stultz {
6045d0cf410Sjohn stultz 	if (!strcmp(str, "pmtmr")) {
6055d0cf410Sjohn stultz 		printk("Warning: clock=pmtmr is deprecated. "
6065d0cf410Sjohn stultz 			"Use clocksource=acpi_pm.\n");
6075d0cf410Sjohn stultz 		return boot_override_clocksource("acpi_pm");
6085d0cf410Sjohn stultz 	}
6095d0cf410Sjohn stultz 	printk("Warning! clock= boot option is deprecated. "
6105d0cf410Sjohn stultz 		"Use clocksource=xyz\n");
611734efb46Sjohn stultz 	return boot_override_clocksource(str);
612734efb46Sjohn stultz }
613734efb46Sjohn stultz 
614734efb46Sjohn stultz __setup("clock=", boot_override_clock);
615