Commit | Line | Data |
---|---|---|
734efb46 | 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 | |
734efb46 | 24 | */ |
25 | ||
26 | #include <linux/clocksource.h> | |
27 | #include <linux/sysdev.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/module.h> | |
dc29a365 | 30 | #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ |
79bf2bb3 | 31 | #include <linux/tick.h> |
734efb46 | 32 | |
a038a353 PO |
33 | void timecounter_init(struct timecounter *tc, |
34 | const struct cyclecounter *cc, | |
35 | u64 start_tstamp) | |
36 | { | |
37 | tc->cc = cc; | |
38 | tc->cycle_last = cc->read(cc); | |
39 | tc->nsec = start_tstamp; | |
40 | } | |
41 | EXPORT_SYMBOL(timecounter_init); | |
42 | ||
43 | /** | |
44 | * timecounter_read_delta - get nanoseconds since last call of this function | |
45 | * @tc: Pointer to time counter | |
46 | * | |
47 | * When the underlying cycle counter runs over, this will be handled | |
48 | * correctly as long as it does not run over more than once between | |
49 | * calls. | |
50 | * | |
51 | * The first call to this function for a new time counter initializes | |
52 | * the time tracking and returns an undefined result. | |
53 | */ | |
54 | static u64 timecounter_read_delta(struct timecounter *tc) | |
55 | { | |
56 | cycle_t cycle_now, cycle_delta; | |
57 | u64 ns_offset; | |
58 | ||
59 | /* read cycle counter: */ | |
60 | cycle_now = tc->cc->read(tc->cc); | |
61 | ||
62 | /* calculate the delta since the last timecounter_read_delta(): */ | |
63 | cycle_delta = (cycle_now - tc->cycle_last) & tc->cc->mask; | |
64 | ||
65 | /* convert to nanoseconds: */ | |
66 | ns_offset = cyclecounter_cyc2ns(tc->cc, cycle_delta); | |
67 | ||
68 | /* update time stamp of timecounter_read_delta() call: */ | |
69 | tc->cycle_last = cycle_now; | |
70 | ||
71 | return ns_offset; | |
72 | } | |
73 | ||
74 | u64 timecounter_read(struct timecounter *tc) | |
75 | { | |
76 | u64 nsec; | |
77 | ||
78 | /* increment time by nanoseconds since last call */ | |
79 | nsec = timecounter_read_delta(tc); | |
80 | nsec += tc->nsec; | |
81 | tc->nsec = nsec; | |
82 | ||
83 | return nsec; | |
84 | } | |
85 | EXPORT_SYMBOL(timecounter_read); | |
86 | ||
87 | u64 timecounter_cyc2time(struct timecounter *tc, | |
88 | cycle_t cycle_tstamp) | |
89 | { | |
90 | u64 cycle_delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask; | |
91 | u64 nsec; | |
92 | ||
93 | /* | |
94 | * Instead of always treating cycle_tstamp as more recent | |
95 | * than tc->cycle_last, detect when it is too far in the | |
96 | * future and treat it as old time stamp instead. | |
97 | */ | |
98 | if (cycle_delta > tc->cc->mask / 2) { | |
99 | cycle_delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask; | |
100 | nsec = tc->nsec - cyclecounter_cyc2ns(tc->cc, cycle_delta); | |
101 | } else { | |
102 | nsec = cyclecounter_cyc2ns(tc->cc, cycle_delta) + tc->nsec; | |
103 | } | |
104 | ||
105 | return nsec; | |
106 | } | |
107 | EXPORT_SYMBOL(timecounter_cyc2time); | |
108 | ||
734efb46 | 109 | /*[Clocksource internal variables]--------- |
110 | * curr_clocksource: | |
f1b82746 | 111 | * currently selected clocksource. |
734efb46 | 112 | * next_clocksource: |
113 | * pending next selected clocksource. | |
114 | * clocksource_list: | |
115 | * linked list with the registered clocksources | |
116 | * clocksource_lock: | |
117 | * protects manipulations to curr_clocksource and next_clocksource | |
118 | * and the clocksource_list | |
119 | * override_name: | |
120 | * Name of the user-specified clocksource. | |
121 | */ | |
f1b82746 | 122 | static struct clocksource *curr_clocksource; |
734efb46 | 123 | static struct clocksource *next_clocksource; |
124 | static LIST_HEAD(clocksource_list); | |
125 | static DEFINE_SPINLOCK(clocksource_lock); | |
126 | static char override_name[32]; | |
127 | static int finished_booting; | |
128 | ||
6bb74df4 | 129 | /* clocksource_done_booting - Called near the end of core bootup |
734efb46 | 130 | * |
6bb74df4 | 131 | * Hack to avoid lots of clocksource churn at boot time. |
132 | * We use fs_initcall because we want this to start before | |
133 | * device_initcall but after subsys_initcall. | |
734efb46 | 134 | */ |
ad596171 | 135 | static int __init clocksource_done_booting(void) |
734efb46 | 136 | { |
137 | finished_booting = 1; | |
138 | return 0; | |
139 | } | |
6bb74df4 | 140 | fs_initcall(clocksource_done_booting); |
734efb46 | 141 | |
5d8b34fd TG |
142 | #ifdef CONFIG_CLOCKSOURCE_WATCHDOG |
143 | static LIST_HEAD(watchdog_list); | |
144 | static struct clocksource *watchdog; | |
145 | static struct timer_list watchdog_timer; | |
c55c87c8 | 146 | static struct work_struct watchdog_work; |
5d8b34fd TG |
147 | static DEFINE_SPINLOCK(watchdog_lock); |
148 | static cycle_t watchdog_last; | |
fb63a0eb | 149 | static int watchdog_running; |
b52f52a0 | 150 | |
c55c87c8 MS |
151 | static void clocksource_watchdog_work(struct work_struct *work); |
152 | ||
5d8b34fd | 153 | /* |
35c35d1a | 154 | * Interval: 0.5sec Threshold: 0.0625s |
5d8b34fd TG |
155 | */ |
156 | #define WATCHDOG_INTERVAL (HZ >> 1) | |
35c35d1a | 157 | #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4) |
5d8b34fd | 158 | |
8cf4e750 | 159 | static void clocksource_unstable(struct clocksource *cs, int64_t delta) |
5d8b34fd | 160 | { |
5d8b34fd TG |
161 | printk(KERN_WARNING "Clocksource %s unstable (delta = %Ld ns)\n", |
162 | cs->name, delta); | |
163 | cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); | |
c55c87c8 MS |
164 | cs->flags |= CLOCK_SOURCE_UNSTABLE; |
165 | schedule_work(&watchdog_work); | |
5d8b34fd TG |
166 | } |
167 | ||
168 | static void clocksource_watchdog(unsigned long data) | |
169 | { | |
c55c87c8 | 170 | struct clocksource *cs; |
5d8b34fd TG |
171 | cycle_t csnow, wdnow; |
172 | int64_t wd_nsec, cs_nsec; | |
c55c87c8 | 173 | int next_cpu; |
5d8b34fd TG |
174 | |
175 | spin_lock(&watchdog_lock); | |
fb63a0eb MS |
176 | if (!watchdog_running) |
177 | goto out; | |
5d8b34fd | 178 | |
8e19608e | 179 | wdnow = watchdog->read(watchdog); |
5d8b34fd TG |
180 | wd_nsec = cyc2ns(watchdog, (wdnow - watchdog_last) & watchdog->mask); |
181 | watchdog_last = wdnow; | |
182 | ||
c55c87c8 MS |
183 | list_for_each_entry(cs, &watchdog_list, wd_list) { |
184 | ||
185 | /* Clocksource already marked unstable? */ | |
186 | if (cs->flags & CLOCK_SOURCE_UNSTABLE) | |
187 | continue; | |
188 | ||
8e19608e | 189 | csnow = cs->read(cs); |
b52f52a0 | 190 | |
8cf4e750 MS |
191 | /* Clocksource initialized ? */ |
192 | if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) { | |
193 | cs->flags |= CLOCK_SOURCE_WATCHDOG; | |
b52f52a0 TG |
194 | cs->wd_last = csnow; |
195 | continue; | |
196 | } | |
197 | ||
8cf4e750 MS |
198 | /* Check the deviation from the watchdog clocksource. */ |
199 | cs_nsec = cyc2ns(cs, (csnow - cs->wd_last) & cs->mask); | |
200 | cs->wd_last = csnow; | |
201 | if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) { | |
202 | clocksource_unstable(cs, cs_nsec - wd_nsec); | |
203 | continue; | |
204 | } | |
205 | ||
206 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && | |
207 | (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && | |
208 | (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { | |
209 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; | |
210 | /* | |
211 | * We just marked the clocksource as highres-capable, | |
212 | * notify the rest of the system as well so that we | |
213 | * transition into high-res mode: | |
214 | */ | |
215 | tick_clock_notify(); | |
5d8b34fd TG |
216 | } |
217 | } | |
218 | ||
c55c87c8 MS |
219 | /* |
220 | * Cycle through CPUs to check if the CPUs stay synchronized | |
221 | * to each other. | |
222 | */ | |
223 | next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask); | |
224 | if (next_cpu >= nr_cpu_ids) | |
225 | next_cpu = cpumask_first(cpu_online_mask); | |
226 | watchdog_timer.expires += WATCHDOG_INTERVAL; | |
227 | add_timer_on(&watchdog_timer, next_cpu); | |
fb63a0eb | 228 | out: |
5d8b34fd TG |
229 | spin_unlock(&watchdog_lock); |
230 | } | |
0f8e8ef7 | 231 | |
fb63a0eb MS |
232 | static inline void clocksource_start_watchdog(void) |
233 | { | |
234 | if (watchdog_running || !watchdog || list_empty(&watchdog_list)) | |
235 | return; | |
c55c87c8 | 236 | INIT_WORK(&watchdog_work, clocksource_watchdog_work); |
fb63a0eb MS |
237 | init_timer(&watchdog_timer); |
238 | watchdog_timer.function = clocksource_watchdog; | |
239 | watchdog_last = watchdog->read(watchdog); | |
240 | watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; | |
241 | add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask)); | |
242 | watchdog_running = 1; | |
243 | } | |
244 | ||
245 | static inline void clocksource_stop_watchdog(void) | |
246 | { | |
247 | if (!watchdog_running || (watchdog && !list_empty(&watchdog_list))) | |
248 | return; | |
249 | del_timer(&watchdog_timer); | |
250 | watchdog_running = 0; | |
251 | } | |
252 | ||
0f8e8ef7 MS |
253 | static inline void clocksource_reset_watchdog(void) |
254 | { | |
255 | struct clocksource *cs; | |
256 | ||
257 | list_for_each_entry(cs, &watchdog_list, wd_list) | |
258 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; | |
259 | } | |
260 | ||
b52f52a0 TG |
261 | static void clocksource_resume_watchdog(void) |
262 | { | |
0f8e8ef7 MS |
263 | unsigned long flags; |
264 | ||
265 | spin_lock_irqsave(&watchdog_lock, flags); | |
266 | clocksource_reset_watchdog(); | |
267 | spin_unlock_irqrestore(&watchdog_lock, flags); | |
b52f52a0 TG |
268 | } |
269 | ||
fb63a0eb | 270 | static void clocksource_enqueue_watchdog(struct clocksource *cs) |
5d8b34fd | 271 | { |
5d8b34fd TG |
272 | unsigned long flags; |
273 | ||
274 | spin_lock_irqsave(&watchdog_lock, flags); | |
275 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { | |
fb63a0eb | 276 | /* cs is a clocksource to be watched. */ |
5d8b34fd | 277 | list_add(&cs->wd_list, &watchdog_list); |
fb63a0eb | 278 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; |
948ac6d7 | 279 | } else { |
fb63a0eb | 280 | /* cs is a watchdog. */ |
948ac6d7 | 281 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) |
5d8b34fd | 282 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
fb63a0eb | 283 | /* Pick the best watchdog. */ |
5d8b34fd | 284 | if (!watchdog || cs->rating > watchdog->rating) { |
5d8b34fd | 285 | watchdog = cs; |
5d8b34fd | 286 | /* Reset watchdog cycles */ |
0f8e8ef7 | 287 | clocksource_reset_watchdog(); |
5d8b34fd TG |
288 | } |
289 | } | |
fb63a0eb MS |
290 | /* Check if the watchdog timer needs to be started. */ |
291 | clocksource_start_watchdog(); | |
5d8b34fd TG |
292 | spin_unlock_irqrestore(&watchdog_lock, flags); |
293 | } | |
fb63a0eb MS |
294 | |
295 | static void clocksource_dequeue_watchdog(struct clocksource *cs) | |
296 | { | |
297 | struct clocksource *tmp; | |
298 | unsigned long flags; | |
299 | ||
300 | spin_lock_irqsave(&watchdog_lock, flags); | |
301 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { | |
302 | /* cs is a watched clocksource. */ | |
303 | list_del_init(&cs->wd_list); | |
304 | } else if (cs == watchdog) { | |
305 | /* Reset watchdog cycles */ | |
306 | clocksource_reset_watchdog(); | |
307 | /* Current watchdog is removed. Find an alternative. */ | |
308 | watchdog = NULL; | |
309 | list_for_each_entry(tmp, &clocksource_list, list) { | |
310 | if (tmp == cs || tmp->flags & CLOCK_SOURCE_MUST_VERIFY) | |
311 | continue; | |
312 | if (!watchdog || tmp->rating > watchdog->rating) | |
313 | watchdog = tmp; | |
314 | } | |
315 | } | |
316 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; | |
317 | /* Check if the watchdog timer needs to be stopped. */ | |
318 | clocksource_stop_watchdog(); | |
319 | spin_unlock_irqrestore(&watchdog_lock, flags); | |
320 | } | |
321 | ||
c55c87c8 MS |
322 | static void clocksource_watchdog_work(struct work_struct *work) |
323 | { | |
324 | struct clocksource *cs, *tmp; | |
325 | unsigned long flags; | |
326 | ||
327 | spin_lock_irqsave(&watchdog_lock, flags); | |
328 | list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) | |
329 | if (cs->flags & CLOCK_SOURCE_UNSTABLE) { | |
330 | list_del_init(&cs->wd_list); | |
331 | clocksource_change_rating(cs, 0); | |
332 | } | |
333 | /* Check if the watchdog timer needs to be stopped. */ | |
334 | clocksource_stop_watchdog(); | |
335 | spin_unlock(&watchdog_lock); | |
336 | } | |
337 | ||
fb63a0eb MS |
338 | #else /* CONFIG_CLOCKSOURCE_WATCHDOG */ |
339 | ||
340 | static void clocksource_enqueue_watchdog(struct clocksource *cs) | |
5d8b34fd TG |
341 | { |
342 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) | |
343 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; | |
344 | } | |
b52f52a0 | 345 | |
fb63a0eb | 346 | static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { } |
b52f52a0 | 347 | static inline void clocksource_resume_watchdog(void) { } |
fb63a0eb MS |
348 | |
349 | #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */ | |
5d8b34fd | 350 | |
b52f52a0 TG |
351 | /** |
352 | * clocksource_resume - resume the clocksource(s) | |
353 | */ | |
354 | void clocksource_resume(void) | |
355 | { | |
2e197586 | 356 | struct clocksource *cs; |
b52f52a0 TG |
357 | unsigned long flags; |
358 | ||
359 | spin_lock_irqsave(&clocksource_lock, flags); | |
360 | ||
2e197586 | 361 | list_for_each_entry(cs, &clocksource_list, list) { |
b52f52a0 TG |
362 | if (cs->resume) |
363 | cs->resume(); | |
364 | } | |
365 | ||
366 | clocksource_resume_watchdog(); | |
367 | ||
368 | spin_unlock_irqrestore(&clocksource_lock, flags); | |
369 | } | |
370 | ||
7c3078b6 JW |
371 | /** |
372 | * clocksource_touch_watchdog - Update watchdog | |
373 | * | |
374 | * Update the watchdog after exception contexts such as kgdb so as not | |
375 | * to incorrectly trip the watchdog. | |
376 | * | |
377 | */ | |
378 | void clocksource_touch_watchdog(void) | |
379 | { | |
380 | clocksource_resume_watchdog(); | |
381 | } | |
382 | ||
f1b82746 | 383 | #ifdef CONFIG_GENERIC_TIME |
734efb46 | 384 | /** |
a2752549 | 385 | * clocksource_get_next - Returns the selected clocksource |
734efb46 | 386 | * |
387 | */ | |
a2752549 | 388 | struct clocksource *clocksource_get_next(void) |
734efb46 | 389 | { |
390 | unsigned long flags; | |
391 | ||
392 | spin_lock_irqsave(&clocksource_lock, flags); | |
393 | if (next_clocksource && finished_booting) { | |
394 | curr_clocksource = next_clocksource; | |
395 | next_clocksource = NULL; | |
396 | } | |
397 | spin_unlock_irqrestore(&clocksource_lock, flags); | |
398 | ||
399 | return curr_clocksource; | |
400 | } | |
401 | ||
402 | /** | |
f1b82746 | 403 | * clocksource_select - Select the best clocksource available |
734efb46 | 404 | * |
405 | * Private function. Must hold clocksource_lock when called. | |
406 | * | |
92c7e002 TG |
407 | * Select the clocksource with the best rating, or the clocksource, |
408 | * which is selected by userspace override. | |
734efb46 | 409 | */ |
f1b82746 | 410 | static void clocksource_select(void) |
734efb46 | 411 | { |
f1b82746 | 412 | struct clocksource *best, *cs; |
5d8b34fd | 413 | |
92c7e002 | 414 | if (list_empty(&clocksource_list)) |
f1b82746 MS |
415 | return; |
416 | /* First clocksource on the list has the best rating. */ | |
417 | best = list_first_entry(&clocksource_list, struct clocksource, list); | |
418 | /* Check for the override clocksource. */ | |
419 | list_for_each_entry(cs, &clocksource_list, list) { | |
420 | if (strcmp(cs->name, override_name) != 0) | |
421 | continue; | |
422 | /* | |
423 | * Check to make sure we don't switch to a non-highres | |
424 | * capable clocksource if the tick code is in oneshot | |
425 | * mode (highres or nohz) | |
426 | */ | |
427 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && | |
428 | tick_oneshot_mode_active()) { | |
429 | /* Override clocksource cannot be used. */ | |
430 | printk(KERN_WARNING "Override clocksource %s is not " | |
431 | "HRT compatible. Cannot switch while in " | |
432 | "HRT/NOHZ mode\n", cs->name); | |
433 | override_name[0] = 0; | |
434 | } else | |
435 | /* Override clocksource can be used. */ | |
436 | best = cs; | |
437 | break; | |
438 | } | |
439 | if (curr_clocksource != best) | |
440 | next_clocksource = best; | |
441 | } | |
734efb46 | 442 | |
f1b82746 | 443 | #else /* CONFIG_GENERIC_TIME */ |
734efb46 | 444 | |
f1b82746 | 445 | static void clocksource_select(void) { } |
5d8b34fd | 446 | |
f1b82746 | 447 | #endif |
734efb46 | 448 | |
92c7e002 TG |
449 | /* |
450 | * Enqueue the clocksource sorted by rating | |
734efb46 | 451 | */ |
f1b82746 | 452 | static void clocksource_enqueue(struct clocksource *cs) |
734efb46 | 453 | { |
f1b82746 MS |
454 | struct list_head *entry = &clocksource_list; |
455 | struct clocksource *tmp; | |
92c7e002 | 456 | |
f1b82746 | 457 | list_for_each_entry(tmp, &clocksource_list, list) |
92c7e002 | 458 | /* Keep track of the place, where to insert */ |
f1b82746 MS |
459 | if (tmp->rating >= cs->rating) |
460 | entry = &tmp->list; | |
461 | list_add(&cs->list, entry); | |
734efb46 | 462 | } |
463 | ||
464 | /** | |
a2752549 | 465 | * clocksource_register - Used to install new clocksources |
734efb46 | 466 | * @t: clocksource to be registered |
467 | * | |
468 | * Returns -EBUSY if registration fails, zero otherwise. | |
469 | */ | |
f1b82746 | 470 | int clocksource_register(struct clocksource *cs) |
734efb46 | 471 | { |
734efb46 | 472 | unsigned long flags; |
473 | ||
474 | spin_lock_irqsave(&clocksource_lock, flags); | |
f1b82746 MS |
475 | clocksource_enqueue(cs); |
476 | clocksource_select(); | |
734efb46 | 477 | spin_unlock_irqrestore(&clocksource_lock, flags); |
fb63a0eb | 478 | clocksource_enqueue_watchdog(cs); |
f1b82746 | 479 | return 0; |
734efb46 | 480 | } |
a2752549 | 481 | EXPORT_SYMBOL(clocksource_register); |
734efb46 | 482 | |
483 | /** | |
92c7e002 | 484 | * clocksource_change_rating - Change the rating of a registered clocksource |
734efb46 | 485 | */ |
92c7e002 | 486 | void clocksource_change_rating(struct clocksource *cs, int rating) |
734efb46 | 487 | { |
488 | unsigned long flags; | |
489 | ||
490 | spin_lock_irqsave(&clocksource_lock, flags); | |
92c7e002 | 491 | list_del(&cs->list); |
5d8b34fd | 492 | cs->rating = rating; |
92c7e002 | 493 | clocksource_enqueue(cs); |
f1b82746 | 494 | clocksource_select(); |
734efb46 | 495 | spin_unlock_irqrestore(&clocksource_lock, flags); |
496 | } | |
fb63a0eb | 497 | EXPORT_SYMBOL(clocksource_change_rating); |
734efb46 | 498 | |
4713e22c TG |
499 | /** |
500 | * clocksource_unregister - remove a registered clocksource | |
501 | */ | |
502 | void clocksource_unregister(struct clocksource *cs) | |
503 | { | |
504 | unsigned long flags; | |
505 | ||
fb63a0eb | 506 | clocksource_dequeue_watchdog(cs); |
4713e22c TG |
507 | spin_lock_irqsave(&clocksource_lock, flags); |
508 | list_del(&cs->list); | |
f1b82746 | 509 | clocksource_select(); |
4713e22c TG |
510 | spin_unlock_irqrestore(&clocksource_lock, flags); |
511 | } | |
fb63a0eb | 512 | EXPORT_SYMBOL(clocksource_unregister); |
4713e22c | 513 | |
2b013700 | 514 | #ifdef CONFIG_SYSFS |
734efb46 | 515 | /** |
516 | * sysfs_show_current_clocksources - sysfs interface for current clocksource | |
517 | * @dev: unused | |
518 | * @buf: char buffer to be filled with clocksource list | |
519 | * | |
520 | * Provides sysfs interface for listing current clocksource. | |
521 | */ | |
522 | static ssize_t | |
4a0b2b4d AK |
523 | sysfs_show_current_clocksources(struct sys_device *dev, |
524 | struct sysdev_attribute *attr, char *buf) | |
734efb46 | 525 | { |
5e2cb101 | 526 | ssize_t count = 0; |
734efb46 | 527 | |
528 | spin_lock_irq(&clocksource_lock); | |
5e2cb101 | 529 | count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name); |
734efb46 | 530 | spin_unlock_irq(&clocksource_lock); |
531 | ||
5e2cb101 | 532 | return count; |
734efb46 | 533 | } |
534 | ||
535 | /** | |
536 | * sysfs_override_clocksource - interface for manually overriding clocksource | |
537 | * @dev: unused | |
538 | * @buf: name of override clocksource | |
539 | * @count: length of buffer | |
540 | * | |
541 | * Takes input from sysfs interface for manually overriding the default | |
542 | * clocksource selction. | |
543 | */ | |
544 | static ssize_t sysfs_override_clocksource(struct sys_device *dev, | |
4a0b2b4d | 545 | struct sysdev_attribute *attr, |
734efb46 | 546 | const char *buf, size_t count) |
547 | { | |
548 | size_t ret = count; | |
92c7e002 | 549 | |
734efb46 | 550 | /* strings from sysfs write are not 0 terminated! */ |
551 | if (count >= sizeof(override_name)) | |
552 | return -EINVAL; | |
553 | ||
554 | /* strip of \n: */ | |
555 | if (buf[count-1] == '\n') | |
556 | count--; | |
734efb46 | 557 | |
558 | spin_lock_irq(&clocksource_lock); | |
559 | ||
92c7e002 TG |
560 | if (count > 0) |
561 | memcpy(override_name, buf, count); | |
734efb46 | 562 | override_name[count] = 0; |
f1b82746 | 563 | clocksource_select(); |
734efb46 | 564 | |
565 | spin_unlock_irq(&clocksource_lock); | |
566 | ||
567 | return ret; | |
568 | } | |
569 | ||
570 | /** | |
571 | * sysfs_show_available_clocksources - sysfs interface for listing clocksource | |
572 | * @dev: unused | |
573 | * @buf: char buffer to be filled with clocksource list | |
574 | * | |
575 | * Provides sysfs interface for listing registered clocksources | |
576 | */ | |
577 | static ssize_t | |
4a0b2b4d AK |
578 | sysfs_show_available_clocksources(struct sys_device *dev, |
579 | struct sysdev_attribute *attr, | |
580 | char *buf) | |
734efb46 | 581 | { |
2e197586 | 582 | struct clocksource *src; |
5e2cb101 | 583 | ssize_t count = 0; |
734efb46 | 584 | |
585 | spin_lock_irq(&clocksource_lock); | |
2e197586 | 586 | list_for_each_entry(src, &clocksource_list, list) { |
cd6d95d8 TG |
587 | /* |
588 | * Don't show non-HRES clocksource if the tick code is | |
589 | * in one shot mode (highres=on or nohz=on) | |
590 | */ | |
591 | if (!tick_oneshot_mode_active() || | |
592 | (src->flags & CLOCK_SOURCE_VALID_FOR_HRES)) | |
3f68535a | 593 | count += snprintf(buf + count, |
5e2cb101 MX |
594 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), |
595 | "%s ", src->name); | |
734efb46 | 596 | } |
597 | spin_unlock_irq(&clocksource_lock); | |
598 | ||
5e2cb101 MX |
599 | count += snprintf(buf + count, |
600 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n"); | |
734efb46 | 601 | |
5e2cb101 | 602 | return count; |
734efb46 | 603 | } |
604 | ||
605 | /* | |
606 | * Sysfs setup bits: | |
607 | */ | |
4f95f81a | 608 | static SYSDEV_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources, |
f5f1a24a | 609 | sysfs_override_clocksource); |
734efb46 | 610 | |
4f95f81a | 611 | static SYSDEV_ATTR(available_clocksource, 0444, |
f5f1a24a | 612 | sysfs_show_available_clocksources, NULL); |
734efb46 | 613 | |
614 | static struct sysdev_class clocksource_sysclass = { | |
af5ca3f4 | 615 | .name = "clocksource", |
734efb46 | 616 | }; |
617 | ||
618 | static struct sys_device device_clocksource = { | |
619 | .id = 0, | |
620 | .cls = &clocksource_sysclass, | |
621 | }; | |
622 | ||
ad596171 | 623 | static int __init init_clocksource_sysfs(void) |
734efb46 | 624 | { |
625 | int error = sysdev_class_register(&clocksource_sysclass); | |
626 | ||
627 | if (!error) | |
628 | error = sysdev_register(&device_clocksource); | |
629 | if (!error) | |
630 | error = sysdev_create_file( | |
631 | &device_clocksource, | |
632 | &attr_current_clocksource); | |
633 | if (!error) | |
634 | error = sysdev_create_file( | |
635 | &device_clocksource, | |
636 | &attr_available_clocksource); | |
637 | return error; | |
638 | } | |
639 | ||
640 | device_initcall(init_clocksource_sysfs); | |
2b013700 | 641 | #endif /* CONFIG_SYSFS */ |
734efb46 | 642 | |
643 | /** | |
644 | * boot_override_clocksource - boot clock override | |
645 | * @str: override name | |
646 | * | |
647 | * Takes a clocksource= boot argument and uses it | |
648 | * as the clocksource override name. | |
649 | */ | |
650 | static int __init boot_override_clocksource(char* str) | |
651 | { | |
652 | unsigned long flags; | |
653 | spin_lock_irqsave(&clocksource_lock, flags); | |
654 | if (str) | |
655 | strlcpy(override_name, str, sizeof(override_name)); | |
656 | spin_unlock_irqrestore(&clocksource_lock, flags); | |
657 | return 1; | |
658 | } | |
659 | ||
660 | __setup("clocksource=", boot_override_clocksource); | |
661 | ||
662 | /** | |
663 | * boot_override_clock - Compatibility layer for deprecated boot option | |
664 | * @str: override name | |
665 | * | |
666 | * DEPRECATED! Takes a clock= boot argument and uses it | |
667 | * as the clocksource override name | |
668 | */ | |
669 | static int __init boot_override_clock(char* str) | |
670 | { | |
5d0cf410 | 671 | if (!strcmp(str, "pmtmr")) { |
672 | printk("Warning: clock=pmtmr is deprecated. " | |
673 | "Use clocksource=acpi_pm.\n"); | |
674 | return boot_override_clocksource("acpi_pm"); | |
675 | } | |
676 | printk("Warning! clock= boot option is deprecated. " | |
677 | "Use clocksource=xyz\n"); | |
734efb46 | 678 | return boot_override_clocksource(str); |
679 | } | |
680 | ||
681 | __setup("clock=", boot_override_clock); |