1/*
2 * linux/kernel/time/clocksource.c
3 *
4 * This file contains the functions which manage clocksource drivers.
5 *
6 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * TODO WishList:
23 *   o Allow clocksource drivers to be unregistered
24 *   o get rid of clocksource_jiffies extern
25 */
26
27#include <linux/clocksource.h>
28#include <linux/sysdev.h>
29#include <linux/init.h>
30#include <linux/module.h>
31#include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
32#include <linux/tick.h>
33
34extern struct clocksource clocksource_jiffies;
35
36/*[Clocksource internal variables]---------
37 * curr_clocksource:
38 *	currently selected clocksource. Initialized to clocksource_jiffies.
39 * next_clocksource:
40 *	pending next selected clocksource.
41 * clocksource_list:
42 *	linked list with the registered clocksources
43 * clocksource_lock:
44 *	protects manipulations to curr_clocksource and next_clocksource
45 *	and the clocksource_list
46 * override_name:
47 *	Name of the user-specified clocksource.
48 */
49static struct clocksource *curr_clocksource = &clocksource_jiffies;
50static struct clocksource *next_clocksource;
51static struct clocksource *clocksource_override;
52static LIST_HEAD(clocksource_list);
53static DEFINE_SPINLOCK(clocksource_lock);
54static char override_name[32];
55static int finished_booting;
56
57/* clocksource_done_booting - Called near the end of core bootup
58 *
59 * Hack to avoid lots of clocksource churn at boot time.
60 * We use fs_initcall because we want this to start before
61 * device_initcall but after subsys_initcall.
62 */
63static int __init clocksource_done_booting(void)
64{
65	finished_booting = 1;
66	return 0;
67}
68fs_initcall(clocksource_done_booting);
69
70#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
71static LIST_HEAD(watchdog_list);
72static struct clocksource *watchdog;
73static struct timer_list watchdog_timer;
74static DEFINE_SPINLOCK(watchdog_lock);
75static cycle_t watchdog_last;
76static unsigned long watchdog_resumed;
77
78/*
79 * Interval: 0.5sec Threshold: 0.0625s
80 */
81#define WATCHDOG_INTERVAL (HZ >> 1)
82#define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
83
84static void clocksource_ratewd(struct clocksource *cs, int64_t delta)
85{
86	if (delta > -WATCHDOG_THRESHOLD && delta < WATCHDOG_THRESHOLD)
87		return;
88
89	printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n",
90	       cs->name, delta);
91	cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
92	clocksource_change_rating(cs, 0);
93	cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
94	list_del(&cs->wd_list);
95}
96
97static void clocksource_watchdog(unsigned long data)
98{
99	struct clocksource *cs, *tmp;
100	cycle_t csnow, wdnow;
101	int64_t wd_nsec, cs_nsec;
102	int resumed;
103
104	spin_lock(&watchdog_lock);
105
106	resumed = test_and_clear_bit(0, &watchdog_resumed);
107
108	wdnow = watchdog->read();
109	wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask);
110	watchdog_last = wdnow;
111
112	list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
113		csnow = cs->read();
114
115		if (unlikely(resumed)) {
116			cs->wd_last = csnow;
117			continue;
118		}
119
120		/* Initialized ? */
121		if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
122			if ((cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
123			    (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
124				cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
125				/*
126				 * We just marked the clocksource as
127				 * highres-capable, notify the rest of the
128				 * system as well so that we transition
129				 * into high-res mode:
130				 */
131				tick_clock_notify();
132			}
133			cs->flags |= CLOCK_SOURCE_WATCHDOG;
134			cs->wd_last = csnow;
135		} else {
136			cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask);
137			cs->wd_last = csnow;
138			/* Check the delta. Might remove from the list ! */
139			clocksource_ratewd(cs, cs_nsec - wd_nsec);
140		}
141	}
142
143	if (!list_empty(&watchdog_list)) {
144		__mod_timer(&watchdog_timer,
145			    watchdog_timer.expires + WATCHDOG_INTERVAL);
146	}
147	spin_unlock(&watchdog_lock);
148}
149static void clocksource_resume_watchdog(void)
150{
151	set_bit(0, &watchdog_resumed);
152}
153
154static void clocksource_check_watchdog(struct clocksource *cs)
155{
156	struct clocksource *cse;
157	unsigned long flags;
158
159	spin_lock_irqsave(&watchdog_lock, flags);
160	if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
161		int started = !list_empty(&watchdog_list);
162
163		list_add(&cs->wd_list, &watchdog_list);
164		if (!started && watchdog) {
165			watchdog_last = watchdog->read();
166			watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
167			add_timer(&watchdog_timer);
168		}
169	} else {
170		if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
171			cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
172
173		if (!watchdog || cs->rating > watchdog->rating) {
174			if (watchdog)
175				del_timer(&watchdog_timer);
176			watchdog = cs;
177			init_timer(&watchdog_timer);
178			watchdog_timer.function = clocksource_watchdog;
179
180			/* Reset watchdog cycles */
181			list_for_each_entry(cse, &watchdog_list, wd_list)
182				cse->flags &= ~CLOCK_SOURCE_WATCHDOG;
183			/* Start if list is not empty */
184			if (!list_empty(&watchdog_list)) {
185				watchdog_last = watchdog->read();
186				watchdog_timer.expires =
187					jiffies + WATCHDOG_INTERVAL;
188				add_timer(&watchdog_timer);
189			}
190		}
191	}
192	spin_unlock_irqrestore(&watchdog_lock, flags);
193}
194#else
195static void clocksource_check_watchdog(struct clocksource *cs)
196{
197	if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
198		cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
199}
200
201static inline void clocksource_resume_watchdog(void) { }
202#endif
203
204/**
205 * clocksource_resume - resume the clocksource(s)
206 */
207void clocksource_resume(void)
208{
209	struct list_head *tmp;
210	unsigned long flags;
211
212	spin_lock_irqsave(&clocksource_lock, flags);
213
214	list_for_each(tmp, &clocksource_list) {
215		struct clocksource *cs;
216
217		cs = list_entry(tmp, struct clocksource, list);
218		if (cs->resume)
219			cs->resume();
220	}
221
222	clocksource_resume_watchdog();
223
224	spin_unlock_irqrestore(&clocksource_lock, flags);
225}
226
227/**
228 * clocksource_get_next - Returns the selected clocksource
229 *
230 */
231struct clocksource *clocksource_get_next(void)
232{
233	unsigned long flags;
234
235	spin_lock_irqsave(&clocksource_lock, flags);
236	if (next_clocksource && finished_booting) {
237		curr_clocksource = next_clocksource;
238		next_clocksource = NULL;
239	}
240	spin_unlock_irqrestore(&clocksource_lock, flags);
241
242	return curr_clocksource;
243}
244
245/**
246 * select_clocksource - Selects the best registered clocksource.
247 *
248 * Private function. Must hold clocksource_lock when called.
249 *
250 * Select the clocksource with the best rating, or the clocksource,
251 * which is selected by userspace override.
252 */
253static struct clocksource *select_clocksource(void)
254{
255	struct clocksource *next;
256
257	if (list_empty(&clocksource_list))
258		return NULL;
259
260	if (clocksource_override)
261		next = clocksource_override;
262	else
263		next = list_entry(clocksource_list.next, struct clocksource,
264				  list);
265
266	if (next == curr_clocksource)
267		return NULL;
268
269	return next;
270}
271
272/*
273 * Enqueue the clocksource sorted by rating
274 */
275static int clocksource_enqueue(struct clocksource *c)
276{
277	struct list_head *tmp, *entry = &clocksource_list;
278
279	list_for_each(tmp, &clocksource_list) {
280		struct clocksource *cs;
281
282		cs = list_entry(tmp, struct clocksource, list);
283		if (cs == c)
284			return -EBUSY;
285		/* Keep track of the place, where to insert */
286		if (cs->rating >= c->rating)
287			entry = tmp;
288	}
289	list_add(&c->list, entry);
290
291	if (strlen(c->name) == strlen(override_name) &&
292	    !strcmp(c->name, override_name))
293		clocksource_override = c;
294
295	return 0;
296}
297
298/**
299 * clocksource_register - Used to install new clocksources
300 * @t:		clocksource to be registered
301 *
302 * Returns -EBUSY if registration fails, zero otherwise.
303 */
304int clocksource_register(struct clocksource *c)
305{
306	unsigned long flags;
307	int ret;
308
309	spin_lock_irqsave(&clocksource_lock, flags);
310	ret = clocksource_enqueue(c);
311	if (!ret)
312		next_clocksource = select_clocksource();
313	spin_unlock_irqrestore(&clocksource_lock, flags);
314	if (!ret)
315		clocksource_check_watchdog(c);
316	return ret;
317}
318EXPORT_SYMBOL(clocksource_register);
319
320/**
321 * clocksource_change_rating - Change the rating of a registered clocksource
322 *
323 */
324void clocksource_change_rating(struct clocksource *cs, int rating)
325{
326	unsigned long flags;
327
328	spin_lock_irqsave(&clocksource_lock, flags);
329	list_del(&cs->list);
330	cs->rating = rating;
331	clocksource_enqueue(cs);
332	next_clocksource = select_clocksource();
333	spin_unlock_irqrestore(&clocksource_lock, flags);
334}
335
336#ifdef CONFIG_SYSFS
337/**
338 * sysfs_show_current_clocksources - sysfs interface for current clocksource
339 * @dev:	unused
340 * @buf:	char buffer to be filled with clocksource list
341 *
342 * Provides sysfs interface for listing current clocksource.
343 */
344static ssize_t
345sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
346{
347	char *curr = buf;
348
349	spin_lock_irq(&clocksource_lock);
350	curr += sprintf(curr, "%s ", curr_clocksource->name);
351	spin_unlock_irq(&clocksource_lock);
352
353	curr += sprintf(curr, "\n");
354
355	return curr - buf;
356}
357
358/**
359 * sysfs_override_clocksource - interface for manually overriding clocksource
360 * @dev:	unused
361 * @buf:	name of override clocksource
362 * @count:	length of buffer
363 *
364 * Takes input from sysfs interface for manually overriding the default
365 * clocksource selction.
366 */
367static ssize_t sysfs_override_clocksource(struct sys_device *dev,
368					  const char *buf, size_t count)
369{
370	struct clocksource *ovr = NULL;
371	struct list_head *tmp;
372	size_t ret = count;
373	int len;
374
375	/* strings from sysfs write are not 0 terminated! */
376	if (count >= sizeof(override_name))
377		return -EINVAL;
378
379	/* strip of \n: */
380	if (buf[count-1] == '\n')
381		count--;
382
383	spin_lock_irq(&clocksource_lock);
384
385	if (count > 0)
386		memcpy(override_name, buf, count);
387	override_name[count] = 0;
388
389	len = strlen(override_name);
390	if (len) {
391		ovr = clocksource_override;
392		/* try to select it: */
393		list_for_each(tmp, &clocksource_list) {
394			struct clocksource *cs;
395
396			cs = list_entry(tmp, struct clocksource, list);
397			if (strlen(cs->name) == len &&
398			    !strcmp(cs->name, override_name))
399				ovr = cs;
400		}
401	}
402
403	/* Reselect, when the override name has changed */
404	if (ovr != clocksource_override) {
405		clocksource_override = ovr;
406		next_clocksource = select_clocksource();
407	}
408
409	spin_unlock_irq(&clocksource_lock);
410
411	return ret;
412}
413
414/**
415 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
416 * @dev:	unused
417 * @buf:	char buffer to be filled with clocksource list
418 *
419 * Provides sysfs interface for listing registered clocksources
420 */
421static ssize_t
422sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
423{
424	struct list_head *tmp;
425	char *curr = buf;
426
427	spin_lock_irq(&clocksource_lock);
428	list_for_each(tmp, &clocksource_list) {
429		struct clocksource *src;
430
431		src = list_entry(tmp, struct clocksource, list);
432		curr += sprintf(curr, "%s ", src->name);
433	}
434	spin_unlock_irq(&clocksource_lock);
435
436	curr += sprintf(curr, "\n");
437
438	return curr - buf;
439}
440
441/*
442 * Sysfs setup bits:
443 */
444static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
445		   sysfs_override_clocksource);
446
447static SYSDEV_ATTR(available_clocksource, 0600,
448		   sysfs_show_available_clocksources, NULL);
449
450static struct sysdev_class clocksource_sysclass = {
451	set_kset_name("clocksource"),
452};
453
454static struct sys_device device_clocksource = {
455	.id	= 0,
456	.cls	= &clocksource_sysclass,
457};
458
459static int __init init_clocksource_sysfs(void)
460{
461	int error = sysdev_class_register(&clocksource_sysclass);
462
463	if (!error)
464		error = sysdev_register(&device_clocksource);
465	if (!error)
466		error = sysdev_create_file(
467				&device_clocksource,
468				&attr_current_clocksource);
469	if (!error)
470		error = sysdev_create_file(
471				&device_clocksource,
472				&attr_available_clocksource);
473	return error;
474}
475
476device_initcall(init_clocksource_sysfs);
477#endif /* CONFIG_SYSFS */
478
479/**
480 * boot_override_clocksource - boot clock override
481 * @str:	override name
482 *
483 * Takes a clocksource= boot argument and uses it
484 * as the clocksource override name.
485 */
486static int __init boot_override_clocksource(char* str)
487{
488	unsigned long flags;
489	spin_lock_irqsave(&clocksource_lock, flags);
490	if (str)
491		strlcpy(override_name, str, sizeof(override_name));
492	spin_unlock_irqrestore(&clocksource_lock, flags);
493	return 1;
494}
495
496__setup("clocksource=", boot_override_clocksource);
497
498/**
499 * boot_override_clock - Compatibility layer for deprecated boot option
500 * @str:	override name
501 *
502 * DEPRECATED! Takes a clock= boot argument and uses it
503 * as the clocksource override name
504 */
505static int __init boot_override_clock(char* str)
506{
507	if (!strcmp(str, "pmtmr")) {
508		printk("Warning: clock=pmtmr is deprecated. "
509			"Use clocksource=acpi_pm.\n");
510		return boot_override_clocksource("acpi_pm");
511	}
512	printk("Warning! clock= boot option is deprecated. "
513		"Use clocksource=xyz\n");
514	return boot_override_clocksource(str);
515}
516
517__setup("clock=", boot_override_clock);
518