]>
Commit | Line | Data |
---|---|---|
734efb46 JS |
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> | |
dc29a365 | 31 | #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ |
734efb46 JS |
32 | |
33 | /* XXX - Would like a better way for initializing curr_clocksource */ | |
34 | extern 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 | */ | |
49 | static struct clocksource *curr_clocksource = &clocksource_jiffies; | |
50 | static struct clocksource *next_clocksource; | |
51 | static LIST_HEAD(clocksource_list); | |
52 | static DEFINE_SPINLOCK(clocksource_lock); | |
53 | static char override_name[32]; | |
54 | static int finished_booting; | |
55 | ||
56 | /* clocksource_done_booting - Called near the end of bootup | |
57 | * | |
58 | * Hack to avoid lots of clocksource churn at boot time | |
59 | */ | |
ad596171 | 60 | static int __init clocksource_done_booting(void) |
734efb46 JS |
61 | { |
62 | finished_booting = 1; | |
63 | return 0; | |
64 | } | |
65 | ||
66 | late_initcall(clocksource_done_booting); | |
67 | ||
68 | /** | |
a2752549 | 69 | * clocksource_get_next - Returns the selected clocksource |
734efb46 JS |
70 | * |
71 | */ | |
a2752549 | 72 | struct clocksource *clocksource_get_next(void) |
734efb46 JS |
73 | { |
74 | unsigned long flags; | |
75 | ||
76 | spin_lock_irqsave(&clocksource_lock, flags); | |
77 | if (next_clocksource && finished_booting) { | |
78 | curr_clocksource = next_clocksource; | |
79 | next_clocksource = NULL; | |
80 | } | |
81 | spin_unlock_irqrestore(&clocksource_lock, flags); | |
82 | ||
83 | return curr_clocksource; | |
84 | } | |
85 | ||
86 | /** | |
87 | * select_clocksource - Finds the best registered clocksource. | |
88 | * | |
89 | * Private function. Must hold clocksource_lock when called. | |
90 | * | |
91 | * Looks through the list of registered clocksources, returning | |
92 | * the one with the highest rating value. If there is a clocksource | |
93 | * name that matches the override string, it returns that clocksource. | |
94 | */ | |
95 | static struct clocksource *select_clocksource(void) | |
96 | { | |
97 | struct clocksource *best = NULL; | |
98 | struct list_head *tmp; | |
99 | ||
100 | list_for_each(tmp, &clocksource_list) { | |
101 | struct clocksource *src; | |
102 | ||
103 | src = list_entry(tmp, struct clocksource, list); | |
104 | if (!best) | |
105 | best = src; | |
106 | ||
107 | /* check for override: */ | |
108 | if (strlen(src->name) == strlen(override_name) && | |
109 | !strcmp(src->name, override_name)) { | |
110 | best = src; | |
111 | break; | |
112 | } | |
113 | /* pick the highest rating: */ | |
114 | if (src->rating > best->rating) | |
115 | best = src; | |
116 | } | |
117 | ||
118 | return best; | |
119 | } | |
120 | ||
121 | /** | |
122 | * is_registered_source - Checks if clocksource is registered | |
123 | * @c: pointer to a clocksource | |
124 | * | |
125 | * Private helper function. Must hold clocksource_lock when called. | |
126 | * | |
127 | * Returns one if the clocksource is already registered, zero otherwise. | |
128 | */ | |
129 | static int is_registered_source(struct clocksource *c) | |
130 | { | |
131 | int len = strlen(c->name); | |
132 | struct list_head *tmp; | |
133 | ||
134 | list_for_each(tmp, &clocksource_list) { | |
135 | struct clocksource *src; | |
136 | ||
137 | src = list_entry(tmp, struct clocksource, list); | |
138 | if (strlen(src->name) == len && !strcmp(src->name, c->name)) | |
139 | return 1; | |
140 | } | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | /** | |
a2752549 | 146 | * clocksource_register - Used to install new clocksources |
734efb46 JS |
147 | * @t: clocksource to be registered |
148 | * | |
149 | * Returns -EBUSY if registration fails, zero otherwise. | |
150 | */ | |
a2752549 | 151 | int clocksource_register(struct clocksource *c) |
734efb46 JS |
152 | { |
153 | int ret = 0; | |
154 | unsigned long flags; | |
155 | ||
156 | spin_lock_irqsave(&clocksource_lock, flags); | |
157 | /* check if clocksource is already registered */ | |
158 | if (is_registered_source(c)) { | |
159 | printk("register_clocksource: Cannot register %s. " | |
f5f1a24a | 160 | "Already registered!", c->name); |
734efb46 JS |
161 | ret = -EBUSY; |
162 | } else { | |
163 | /* register it */ | |
164 | list_add(&c->list, &clocksource_list); | |
165 | /* scan the registered clocksources, and pick the best one */ | |
166 | next_clocksource = select_clocksource(); | |
167 | } | |
168 | spin_unlock_irqrestore(&clocksource_lock, flags); | |
169 | return ret; | |
170 | } | |
a2752549 | 171 | EXPORT_SYMBOL(clocksource_register); |
734efb46 JS |
172 | |
173 | /** | |
a2752549 | 174 | * clocksource_reselect - Rescan list for next clocksource |
734efb46 JS |
175 | * |
176 | * A quick helper function to be used if a clocksource changes its | |
5d0cf410 | 177 | * rating. Forces the clocksource list to be re-scanned for the best |
734efb46 JS |
178 | * clocksource. |
179 | */ | |
a2752549 | 180 | void clocksource_reselect(void) |
734efb46 JS |
181 | { |
182 | unsigned long flags; | |
183 | ||
184 | spin_lock_irqsave(&clocksource_lock, flags); | |
185 | next_clocksource = select_clocksource(); | |
186 | spin_unlock_irqrestore(&clocksource_lock, flags); | |
187 | } | |
a2752549 | 188 | EXPORT_SYMBOL(clocksource_reselect); |
734efb46 | 189 | |
2b013700 | 190 | #ifdef CONFIG_SYSFS |
734efb46 JS |
191 | /** |
192 | * sysfs_show_current_clocksources - sysfs interface for current clocksource | |
193 | * @dev: unused | |
194 | * @buf: char buffer to be filled with clocksource list | |
195 | * | |
196 | * Provides sysfs interface for listing current clocksource. | |
197 | */ | |
198 | static ssize_t | |
199 | sysfs_show_current_clocksources(struct sys_device *dev, char *buf) | |
200 | { | |
201 | char *curr = buf; | |
202 | ||
203 | spin_lock_irq(&clocksource_lock); | |
204 | curr += sprintf(curr, "%s ", curr_clocksource->name); | |
205 | spin_unlock_irq(&clocksource_lock); | |
206 | ||
207 | curr += sprintf(curr, "\n"); | |
208 | ||
209 | return curr - buf; | |
210 | } | |
211 | ||
212 | /** | |
213 | * sysfs_override_clocksource - interface for manually overriding clocksource | |
214 | * @dev: unused | |
215 | * @buf: name of override clocksource | |
216 | * @count: length of buffer | |
217 | * | |
218 | * Takes input from sysfs interface for manually overriding the default | |
219 | * clocksource selction. | |
220 | */ | |
221 | static ssize_t sysfs_override_clocksource(struct sys_device *dev, | |
222 | const char *buf, size_t count) | |
223 | { | |
224 | size_t ret = count; | |
225 | /* strings from sysfs write are not 0 terminated! */ | |
226 | if (count >= sizeof(override_name)) | |
227 | return -EINVAL; | |
228 | ||
229 | /* strip of \n: */ | |
230 | if (buf[count-1] == '\n') | |
231 | count--; | |
232 | if (count < 1) | |
233 | return -EINVAL; | |
234 | ||
235 | spin_lock_irq(&clocksource_lock); | |
236 | ||
237 | /* copy the name given: */ | |
238 | memcpy(override_name, buf, count); | |
239 | override_name[count] = 0; | |
240 | ||
241 | /* try to select it: */ | |
242 | next_clocksource = select_clocksource(); | |
243 | ||
244 | spin_unlock_irq(&clocksource_lock); | |
245 | ||
246 | return ret; | |
247 | } | |
248 | ||
249 | /** | |
250 | * sysfs_show_available_clocksources - sysfs interface for listing clocksource | |
251 | * @dev: unused | |
252 | * @buf: char buffer to be filled with clocksource list | |
253 | * | |
254 | * Provides sysfs interface for listing registered clocksources | |
255 | */ | |
256 | static ssize_t | |
257 | sysfs_show_available_clocksources(struct sys_device *dev, char *buf) | |
258 | { | |
259 | struct list_head *tmp; | |
260 | char *curr = buf; | |
261 | ||
262 | spin_lock_irq(&clocksource_lock); | |
263 | list_for_each(tmp, &clocksource_list) { | |
264 | struct clocksource *src; | |
265 | ||
266 | src = list_entry(tmp, struct clocksource, list); | |
267 | curr += sprintf(curr, "%s ", src->name); | |
268 | } | |
269 | spin_unlock_irq(&clocksource_lock); | |
270 | ||
271 | curr += sprintf(curr, "\n"); | |
272 | ||
273 | return curr - buf; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Sysfs setup bits: | |
278 | */ | |
279 | static SYSDEV_ATTR(current_clocksource, 0600, sysfs_show_current_clocksources, | |
f5f1a24a | 280 | sysfs_override_clocksource); |
734efb46 JS |
281 | |
282 | static SYSDEV_ATTR(available_clocksource, 0600, | |
f5f1a24a | 283 | sysfs_show_available_clocksources, NULL); |
734efb46 JS |
284 | |
285 | static struct sysdev_class clocksource_sysclass = { | |
286 | set_kset_name("clocksource"), | |
287 | }; | |
288 | ||
289 | static struct sys_device device_clocksource = { | |
290 | .id = 0, | |
291 | .cls = &clocksource_sysclass, | |
292 | }; | |
293 | ||
ad596171 | 294 | static int __init init_clocksource_sysfs(void) |
734efb46 JS |
295 | { |
296 | int error = sysdev_class_register(&clocksource_sysclass); | |
297 | ||
298 | if (!error) | |
299 | error = sysdev_register(&device_clocksource); | |
300 | if (!error) | |
301 | error = sysdev_create_file( | |
302 | &device_clocksource, | |
303 | &attr_current_clocksource); | |
304 | if (!error) | |
305 | error = sysdev_create_file( | |
306 | &device_clocksource, | |
307 | &attr_available_clocksource); | |
308 | return error; | |
309 | } | |
310 | ||
311 | device_initcall(init_clocksource_sysfs); | |
2b013700 | 312 | #endif /* CONFIG_SYSFS */ |
734efb46 JS |
313 | |
314 | /** | |
315 | * boot_override_clocksource - boot clock override | |
316 | * @str: override name | |
317 | * | |
318 | * Takes a clocksource= boot argument and uses it | |
319 | * as the clocksource override name. | |
320 | */ | |
321 | static int __init boot_override_clocksource(char* str) | |
322 | { | |
323 | unsigned long flags; | |
324 | spin_lock_irqsave(&clocksource_lock, flags); | |
325 | if (str) | |
326 | strlcpy(override_name, str, sizeof(override_name)); | |
327 | spin_unlock_irqrestore(&clocksource_lock, flags); | |
328 | return 1; | |
329 | } | |
330 | ||
331 | __setup("clocksource=", boot_override_clocksource); | |
332 | ||
333 | /** | |
334 | * boot_override_clock - Compatibility layer for deprecated boot option | |
335 | * @str: override name | |
336 | * | |
337 | * DEPRECATED! Takes a clock= boot argument and uses it | |
338 | * as the clocksource override name | |
339 | */ | |
340 | static int __init boot_override_clock(char* str) | |
341 | { | |
5d0cf410 JS |
342 | if (!strcmp(str, "pmtmr")) { |
343 | printk("Warning: clock=pmtmr is deprecated. " | |
344 | "Use clocksource=acpi_pm.\n"); | |
345 | return boot_override_clocksource("acpi_pm"); | |
346 | } | |
347 | printk("Warning! clock= boot option is deprecated. " | |
348 | "Use clocksource=xyz\n"); | |
734efb46 JS |
349 | return boot_override_clocksource(str); |
350 | } | |
351 | ||
352 | __setup("clock=", boot_override_clock); |