2 * linux/kernel/time/clocksource.c
4 * This file contains the functions which manage clocksource drivers.
6 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
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.
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.
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.
23 * o Allow clocksource drivers to be unregistered
24 * o get rid of clocksource_jiffies extern
27 #include <linux/clocksource.h>
28 #include <linux/sysdev.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
32 /* XXX - Would like a better way for initializing curr_clocksource */
33 extern struct clocksource clocksource_jiffies;
35 /*[Clocksource internal variables]---------
37 * currently selected clocksource. Initialized to clocksource_jiffies.
39 * pending next selected clocksource.
41 * linked list with the registered clocksources
43 * protects manipulations to curr_clocksource and next_clocksource
44 * and the clocksource_list
46 * Name of the user-specified clocksource.
48 static struct clocksource *curr_clocksource = &clocksource_jiffies;
49 static struct clocksource *next_clocksource;
50 static LIST_HEAD(clocksource_list);
51 static DEFINE_SPINLOCK(clocksource_lock);
52 static char override_name[32];
53 static int finished_booting;
55 /* clocksource_done_booting - Called near the end of bootup
57 * Hack to avoid lots of clocksource churn at boot time
59 static int __init clocksource_done_booting(void)
65 late_initcall(clocksource_done_booting);
68 * clocksource_get_next - Returns the selected clocksource
71 struct clocksource *clocksource_get_next(void)
75 spin_lock_irqsave(&clocksource_lock, flags);
76 if (next_clocksource && finished_booting) {
77 curr_clocksource = next_clocksource;
78 next_clocksource = NULL;
80 spin_unlock_irqrestore(&clocksource_lock, flags);
82 return curr_clocksource;
86 * select_clocksource - Finds the best registered clocksource.
88 * Private function. Must hold clocksource_lock when called.
90 * Looks through the list of registered clocksources, returning
91 * the one with the highest rating value. If there is a clocksource
92 * name that matches the override string, it returns that clocksource.
94 static struct clocksource *select_clocksource(void)
96 struct clocksource *best = NULL;
97 struct list_head *tmp;
99 list_for_each(tmp, &clocksource_list) {
100 struct clocksource *src;
102 src = list_entry(tmp, struct clocksource, list);
106 /* check for override: */
107 if (strlen(src->name) == strlen(override_name) &&
108 !strcmp(src->name, override_name)) {
112 /* pick the highest rating: */
113 if (src->rating > best->rating)
121 * is_registered_source - Checks if clocksource is registered
122 * @c: pointer to a clocksource
124 * Private helper function. Must hold clocksource_lock when called.
126 * Returns one if the clocksource is already registered, zero otherwise.
128 static int is_registered_source(struct clocksource *c)
130 int len = strlen(c->name);
131 struct list_head *tmp;
133 list_for_each(tmp, &clocksource_list) {
134 struct clocksource *src;
136 src = list_entry(tmp, struct clocksource, list);
137 if (strlen(src->name) == len && !strcmp(src->name, c->name))
145 * clocksource_register - Used to install new clocksources
146 * @t: clocksource to be registered
148 * Returns -EBUSY if registration fails, zero otherwise.
150 int clocksource_register(struct clocksource *c)
155 spin_lock_irqsave(&clocksource_lock, flags);
156 /* check if clocksource is already registered */
157 if (is_registered_source(c)) {
158 printk("register_clocksource: Cannot register %s. "
159 "Already registered!", c->name);
163 list_add(&c->list, &clocksource_list);
164 /* scan the registered clocksources, and pick the best one */
165 next_clocksource = select_clocksource();
167 spin_unlock_irqrestore(&clocksource_lock, flags);
170 EXPORT_SYMBOL(clocksource_register);
173 * clocksource_reselect - Rescan list for next clocksource
175 * A quick helper function to be used if a clocksource changes its
176 * rating. Forces the clocksource list to be re-scanned for the best
179 void clocksource_reselect(void)
183 spin_lock_irqsave(&clocksource_lock, flags);
184 next_clocksource = select_clocksource();
185 spin_unlock_irqrestore(&clocksource_lock, flags);
187 EXPORT_SYMBOL(clocksource_reselect);
191 * sysfs_show_current_clocksources - sysfs interface for current clocksource
193 * @buf: char buffer to be filled with clocksource list
195 * Provides sysfs interface for listing current clocksource.
198 sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
202 spin_lock_irq(&clocksource_lock);
203 curr += sprintf(curr, "%s ", curr_clocksource->name);
204 spin_unlock_irq(&clocksource_lock);
206 curr += sprintf(curr, "\n");
212 * sysfs_override_clocksource - interface for manually overriding clocksource
214 * @buf: name of override clocksource
215 * @count: length of buffer
217 * Takes input from sysfs interface for manually overriding the default
218 * clocksource selction.
220 static ssize_t sysfs_override_clocksource(struct sys_device *dev,
221 const char *buf, size_t count)
224 /* strings from sysfs write are not 0 terminated! */
225 if (count >= sizeof(override_name))
229 if (buf[count-1] == '\n')
234 spin_lock_irq(&clocksource_lock);
236 /* copy the name given: */
237 memcpy(override_name, buf, count);
238 override_name[count] = 0;
240 /* try to select it: */
241 next_clocksource = select_clocksource();
243 spin_unlock_irq(&clocksource_lock);
249 * sysfs_show_available_clocksources - sysfs interface for listing clocksource
251 * @buf: char buffer to be filled with clocksource list
253 * Provides sysfs interface for listing registered clocksources
256 sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
258 struct list_head *tmp;
261 spin_lock_irq(&clocksource_lock);
262 list_for_each(tmp, &clocksource_list) {
263 struct clocksource *src;
265 src = list_entry(tmp, struct clocksource, list);
266 curr += sprintf(curr, "%s ", src->name);
268 spin_unlock_irq(&clocksource_lock);
270 curr += sprintf(curr, "\n");
278 static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources,
279 sysfs_override_clocksource);
281 static SYSDEV_ATTR(available_clocksource, 0600,
282 sysfs_show_available_clocksources, NULL);
284 static struct sysdev_class clocksource_sysclass = {
285 set_kset_name("clocksource"),
288 static struct sys_device device_clocksource = {
290 .cls = &clocksource_sysclass,
293 static int __init init_clocksource_sysfs(void)
295 int error = sysdev_class_register(&clocksource_sysclass);
298 error = sysdev_register(&device_clocksource);
300 error = sysdev_create_file(
302 &attr_current_clocksource);
304 error = sysdev_create_file(
306 &attr_available_clocksource);
310 device_initcall(init_clocksource_sysfs);
311 #endif /* CONFIG_SYSFS */
314 * boot_override_clocksource - boot clock override
315 * @str: override name
317 * Takes a clocksource= boot argument and uses it
318 * as the clocksource override name.
320 static int __init boot_override_clocksource(char* str)
323 spin_lock_irqsave(&clocksource_lock, flags);
325 strlcpy(override_name, str, sizeof(override_name));
326 spin_unlock_irqrestore(&clocksource_lock, flags);
330 __setup("clocksource=", boot_override_clocksource);
333 * boot_override_clock - Compatibility layer for deprecated boot option
334 * @str: override name
336 * DEPRECATED! Takes a clock= boot argument and uses it
337 * as the clocksource override name
339 static int __init boot_override_clock(char* str)
341 if (!strcmp(str, "pmtmr")) {
342 printk("Warning: clock=pmtmr is deprecated. "
343 "Use clocksource=acpi_pm.\n");
344 return boot_override_clocksource("acpi_pm");
346 printk("Warning! clock= boot option is deprecated. "
347 "Use clocksource=xyz\n");
348 return boot_override_clocksource(str);
351 __setup("clock=", boot_override_clock);