]>
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 | |
734efb46 JS |
24 | */ |
25 | ||
45bbfe64 JP |
26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
27 | ||
d369a5d8 | 28 | #include <linux/device.h> |
734efb46 | 29 | #include <linux/clocksource.h> |
734efb46 JS |
30 | #include <linux/init.h> |
31 | #include <linux/module.h> | |
dc29a365 | 32 | #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */ |
79bf2bb3 | 33 | #include <linux/tick.h> |
01548f4d | 34 | #include <linux/kthread.h> |
734efb46 | 35 | |
c1797baf | 36 | #include "tick-internal.h" |
3a978377 | 37 | #include "timekeeping_internal.h" |
03e13cf5 | 38 | |
7d2f944a TG |
39 | /** |
40 | * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks | |
41 | * @mult: pointer to mult variable | |
42 | * @shift: pointer to shift variable | |
43 | * @from: frequency to convert from | |
44 | * @to: frequency to convert to | |
5fdade95 | 45 | * @maxsec: guaranteed runtime conversion range in seconds |
7d2f944a TG |
46 | * |
47 | * The function evaluates the shift/mult pair for the scaled math | |
48 | * operations of clocksources and clockevents. | |
49 | * | |
50 | * @to and @from are frequency values in HZ. For clock sources @to is | |
51 | * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock | |
52 | * event @to is the counter frequency and @from is NSEC_PER_SEC. | |
53 | * | |
5fdade95 | 54 | * The @maxsec conversion range argument controls the time frame in |
7d2f944a TG |
55 | * seconds which must be covered by the runtime conversion with the |
56 | * calculated mult and shift factors. This guarantees that no 64bit | |
57 | * overflow happens when the input value of the conversion is | |
58 | * multiplied with the calculated mult factor. Larger ranges may | |
59 | * reduce the conversion accuracy by chosing smaller mult and shift | |
60 | * factors. | |
61 | */ | |
62 | void | |
5fdade95 | 63 | clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec) |
7d2f944a TG |
64 | { |
65 | u64 tmp; | |
66 | u32 sft, sftacc= 32; | |
67 | ||
68 | /* | |
69 | * Calculate the shift factor which is limiting the conversion | |
70 | * range: | |
71 | */ | |
5fdade95 | 72 | tmp = ((u64)maxsec * from) >> 32; |
7d2f944a TG |
73 | while (tmp) { |
74 | tmp >>=1; | |
75 | sftacc--; | |
76 | } | |
77 | ||
78 | /* | |
79 | * Find the conversion shift/mult pair which has the best | |
80 | * accuracy and fits the maxsec conversion range: | |
81 | */ | |
82 | for (sft = 32; sft > 0; sft--) { | |
83 | tmp = (u64) to << sft; | |
b5776c4a | 84 | tmp += from / 2; |
7d2f944a TG |
85 | do_div(tmp, from); |
86 | if ((tmp >> sftacc) == 0) | |
87 | break; | |
88 | } | |
89 | *mult = tmp; | |
90 | *shift = sft; | |
91 | } | |
5304121a | 92 | EXPORT_SYMBOL_GPL(clocks_calc_mult_shift); |
7d2f944a | 93 | |
734efb46 JS |
94 | /*[Clocksource internal variables]--------- |
95 | * curr_clocksource: | |
f1b82746 | 96 | * currently selected clocksource. |
734efb46 JS |
97 | * clocksource_list: |
98 | * linked list with the registered clocksources | |
75c5158f MS |
99 | * clocksource_mutex: |
100 | * protects manipulations to curr_clocksource and the clocksource_list | |
734efb46 JS |
101 | * override_name: |
102 | * Name of the user-specified clocksource. | |
103 | */ | |
f1b82746 | 104 | static struct clocksource *curr_clocksource; |
734efb46 | 105 | static LIST_HEAD(clocksource_list); |
75c5158f | 106 | static DEFINE_MUTEX(clocksource_mutex); |
29b54078 | 107 | static char override_name[CS_NAME_LEN]; |
54a6bc0b | 108 | static int finished_booting; |
734efb46 | 109 | |
5d8b34fd | 110 | #ifdef CONFIG_CLOCKSOURCE_WATCHDOG |
f79e0258 | 111 | static void clocksource_watchdog_work(struct work_struct *work); |
332962f2 | 112 | static void clocksource_select(void); |
f79e0258 | 113 | |
5d8b34fd TG |
114 | static LIST_HEAD(watchdog_list); |
115 | static struct clocksource *watchdog; | |
116 | static struct timer_list watchdog_timer; | |
f79e0258 | 117 | static DECLARE_WORK(watchdog_work, clocksource_watchdog_work); |
5d8b34fd | 118 | static DEFINE_SPINLOCK(watchdog_lock); |
fb63a0eb | 119 | static int watchdog_running; |
9fb60336 | 120 | static atomic_t watchdog_reset_pending; |
b52f52a0 | 121 | |
01548f4d | 122 | static int clocksource_watchdog_kthread(void *data); |
d0981a1b | 123 | static void __clocksource_change_rating(struct clocksource *cs, int rating); |
c55c87c8 | 124 | |
5d8b34fd | 125 | /* |
35c35d1a | 126 | * Interval: 0.5sec Threshold: 0.0625s |
5d8b34fd TG |
127 | */ |
128 | #define WATCHDOG_INTERVAL (HZ >> 1) | |
35c35d1a | 129 | #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4) |
5d8b34fd | 130 | |
01548f4d MS |
131 | static void clocksource_watchdog_work(struct work_struct *work) |
132 | { | |
133 | /* | |
134 | * If kthread_run fails the next watchdog scan over the | |
135 | * watchdog_list will find the unstable clock again. | |
136 | */ | |
137 | kthread_run(clocksource_watchdog_kthread, NULL, "kwatchdog"); | |
138 | } | |
139 | ||
7285dd7f | 140 | static void __clocksource_unstable(struct clocksource *cs) |
5d8b34fd | 141 | { |
5d8b34fd | 142 | cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG); |
c55c87c8 | 143 | cs->flags |= CLOCK_SOURCE_UNSTABLE; |
12907fbb TG |
144 | |
145 | if (cs->mark_unstable) | |
146 | cs->mark_unstable(cs); | |
147 | ||
54a6bc0b TG |
148 | if (finished_booting) |
149 | schedule_work(&watchdog_work); | |
5d8b34fd TG |
150 | } |
151 | ||
7285dd7f TG |
152 | /** |
153 | * clocksource_mark_unstable - mark clocksource unstable via watchdog | |
154 | * @cs: clocksource to be marked unstable | |
155 | * | |
156 | * This function is called instead of clocksource_change_rating from | |
157 | * cpu hotplug code to avoid a deadlock between the clocksource mutex | |
158 | * and the cpu hotplug mutex. It defers the update of the clocksource | |
159 | * to the watchdog thread. | |
160 | */ | |
161 | void clocksource_mark_unstable(struct clocksource *cs) | |
162 | { | |
163 | unsigned long flags; | |
164 | ||
165 | spin_lock_irqsave(&watchdog_lock, flags); | |
166 | if (!(cs->flags & CLOCK_SOURCE_UNSTABLE)) { | |
167 | if (list_empty(&cs->wd_list)) | |
168 | list_add(&cs->wd_list, &watchdog_list); | |
169 | __clocksource_unstable(cs); | |
170 | } | |
171 | spin_unlock_irqrestore(&watchdog_lock, flags); | |
172 | } | |
173 | ||
5d8b34fd TG |
174 | static void clocksource_watchdog(unsigned long data) |
175 | { | |
c55c87c8 | 176 | struct clocksource *cs; |
a5a1d1c2 | 177 | u64 csnow, wdnow, cslast, wdlast, delta; |
5d8b34fd | 178 | int64_t wd_nsec, cs_nsec; |
9fb60336 | 179 | int next_cpu, reset_pending; |
5d8b34fd TG |
180 | |
181 | spin_lock(&watchdog_lock); | |
fb63a0eb MS |
182 | if (!watchdog_running) |
183 | goto out; | |
5d8b34fd | 184 | |
9fb60336 TG |
185 | reset_pending = atomic_read(&watchdog_reset_pending); |
186 | ||
c55c87c8 MS |
187 | list_for_each_entry(cs, &watchdog_list, wd_list) { |
188 | ||
189 | /* Clocksource already marked unstable? */ | |
01548f4d | 190 | if (cs->flags & CLOCK_SOURCE_UNSTABLE) { |
54a6bc0b TG |
191 | if (finished_booting) |
192 | schedule_work(&watchdog_work); | |
c55c87c8 | 193 | continue; |
01548f4d | 194 | } |
c55c87c8 | 195 | |
b5199515 | 196 | local_irq_disable(); |
8e19608e | 197 | csnow = cs->read(cs); |
b5199515 TG |
198 | wdnow = watchdog->read(watchdog); |
199 | local_irq_enable(); | |
b52f52a0 | 200 | |
8cf4e750 | 201 | /* Clocksource initialized ? */ |
9fb60336 TG |
202 | if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) || |
203 | atomic_read(&watchdog_reset_pending)) { | |
8cf4e750 | 204 | cs->flags |= CLOCK_SOURCE_WATCHDOG; |
b5199515 TG |
205 | cs->wd_last = wdnow; |
206 | cs->cs_last = csnow; | |
b52f52a0 TG |
207 | continue; |
208 | } | |
209 | ||
3a978377 TG |
210 | delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask); |
211 | wd_nsec = clocksource_cyc2ns(delta, watchdog->mult, | |
212 | watchdog->shift); | |
b5199515 | 213 | |
3a978377 TG |
214 | delta = clocksource_delta(csnow, cs->cs_last, cs->mask); |
215 | cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift); | |
0b046b21 JS |
216 | wdlast = cs->wd_last; /* save these in case we print them */ |
217 | cslast = cs->cs_last; | |
b5199515 TG |
218 | cs->cs_last = csnow; |
219 | cs->wd_last = wdnow; | |
220 | ||
9fb60336 TG |
221 | if (atomic_read(&watchdog_reset_pending)) |
222 | continue; | |
223 | ||
b5199515 | 224 | /* Check the deviation from the watchdog clocksource. */ |
79211c8e | 225 | if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) { |
390dd67c SI |
226 | pr_warn("timekeeping watchdog on CPU%d: Marking clocksource '%s' as unstable because the skew is too large:\n", |
227 | smp_processor_id(), cs->name); | |
45bbfe64 | 228 | pr_warn(" '%s' wd_now: %llx wd_last: %llx mask: %llx\n", |
0b046b21 | 229 | watchdog->name, wdnow, wdlast, watchdog->mask); |
45bbfe64 | 230 | pr_warn(" '%s' cs_now: %llx cs_last: %llx mask: %llx\n", |
0b046b21 JS |
231 | cs->name, csnow, cslast, cs->mask); |
232 | __clocksource_unstable(cs); | |
8cf4e750 MS |
233 | continue; |
234 | } | |
235 | ||
236 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && | |
237 | (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) && | |
238 | (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) { | |
332962f2 | 239 | /* Mark it valid for high-res. */ |
8cf4e750 | 240 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
332962f2 TG |
241 | |
242 | /* | |
243 | * clocksource_done_booting() will sort it if | |
244 | * finished_booting is not set yet. | |
245 | */ | |
246 | if (!finished_booting) | |
247 | continue; | |
248 | ||
8cf4e750 | 249 | /* |
332962f2 TG |
250 | * If this is not the current clocksource let |
251 | * the watchdog thread reselect it. Due to the | |
252 | * change to high res this clocksource might | |
253 | * be preferred now. If it is the current | |
254 | * clocksource let the tick code know about | |
255 | * that change. | |
8cf4e750 | 256 | */ |
332962f2 TG |
257 | if (cs != curr_clocksource) { |
258 | cs->flags |= CLOCK_SOURCE_RESELECT; | |
259 | schedule_work(&watchdog_work); | |
260 | } else { | |
261 | tick_clock_notify(); | |
262 | } | |
5d8b34fd TG |
263 | } |
264 | } | |
265 | ||
9fb60336 TG |
266 | /* |
267 | * We only clear the watchdog_reset_pending, when we did a | |
268 | * full cycle through all clocksources. | |
269 | */ | |
270 | if (reset_pending) | |
271 | atomic_dec(&watchdog_reset_pending); | |
272 | ||
c55c87c8 MS |
273 | /* |
274 | * Cycle through CPUs to check if the CPUs stay synchronized | |
275 | * to each other. | |
276 | */ | |
277 | next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask); | |
278 | if (next_cpu >= nr_cpu_ids) | |
279 | next_cpu = cpumask_first(cpu_online_mask); | |
280 | watchdog_timer.expires += WATCHDOG_INTERVAL; | |
281 | add_timer_on(&watchdog_timer, next_cpu); | |
fb63a0eb | 282 | out: |
5d8b34fd TG |
283 | spin_unlock(&watchdog_lock); |
284 | } | |
0f8e8ef7 | 285 | |
fb63a0eb MS |
286 | static inline void clocksource_start_watchdog(void) |
287 | { | |
288 | if (watchdog_running || !watchdog || list_empty(&watchdog_list)) | |
289 | return; | |
290 | init_timer(&watchdog_timer); | |
291 | watchdog_timer.function = clocksource_watchdog; | |
fb63a0eb MS |
292 | watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; |
293 | add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask)); | |
294 | watchdog_running = 1; | |
295 | } | |
296 | ||
297 | static inline void clocksource_stop_watchdog(void) | |
298 | { | |
299 | if (!watchdog_running || (watchdog && !list_empty(&watchdog_list))) | |
300 | return; | |
301 | del_timer(&watchdog_timer); | |
302 | watchdog_running = 0; | |
303 | } | |
304 | ||
0f8e8ef7 MS |
305 | static inline void clocksource_reset_watchdog(void) |
306 | { | |
307 | struct clocksource *cs; | |
308 | ||
309 | list_for_each_entry(cs, &watchdog_list, wd_list) | |
310 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; | |
311 | } | |
312 | ||
b52f52a0 TG |
313 | static void clocksource_resume_watchdog(void) |
314 | { | |
9fb60336 | 315 | atomic_inc(&watchdog_reset_pending); |
b52f52a0 TG |
316 | } |
317 | ||
fb63a0eb | 318 | static void clocksource_enqueue_watchdog(struct clocksource *cs) |
5d8b34fd | 319 | { |
5d8b34fd TG |
320 | unsigned long flags; |
321 | ||
322 | spin_lock_irqsave(&watchdog_lock, flags); | |
323 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { | |
fb63a0eb | 324 | /* cs is a clocksource to be watched. */ |
5d8b34fd | 325 | list_add(&cs->wd_list, &watchdog_list); |
fb63a0eb | 326 | cs->flags &= ~CLOCK_SOURCE_WATCHDOG; |
948ac6d7 | 327 | } else { |
fb63a0eb | 328 | /* cs is a watchdog. */ |
948ac6d7 | 329 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) |
5d8b34fd | 330 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; |
bbf66d89 VK |
331 | } |
332 | spin_unlock_irqrestore(&watchdog_lock, flags); | |
333 | } | |
334 | ||
335 | static void clocksource_select_watchdog(bool fallback) | |
336 | { | |
337 | struct clocksource *cs, *old_wd; | |
338 | unsigned long flags; | |
339 | ||
340 | spin_lock_irqsave(&watchdog_lock, flags); | |
341 | /* save current watchdog */ | |
342 | old_wd = watchdog; | |
343 | if (fallback) | |
344 | watchdog = NULL; | |
345 | ||
346 | list_for_each_entry(cs, &clocksource_list, list) { | |
347 | /* cs is a clocksource to be watched. */ | |
348 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) | |
349 | continue; | |
350 | ||
351 | /* Skip current if we were requested for a fallback. */ | |
352 | if (fallback && cs == old_wd) | |
353 | continue; | |
354 | ||
fb63a0eb | 355 | /* Pick the best watchdog. */ |
bbf66d89 | 356 | if (!watchdog || cs->rating > watchdog->rating) |
5d8b34fd | 357 | watchdog = cs; |
5d8b34fd | 358 | } |
bbf66d89 VK |
359 | /* If we failed to find a fallback restore the old one. */ |
360 | if (!watchdog) | |
361 | watchdog = old_wd; | |
362 | ||
363 | /* If we changed the watchdog we need to reset cycles. */ | |
364 | if (watchdog != old_wd) | |
365 | clocksource_reset_watchdog(); | |
366 | ||
fb63a0eb MS |
367 | /* Check if the watchdog timer needs to be started. */ |
368 | clocksource_start_watchdog(); | |
5d8b34fd TG |
369 | spin_unlock_irqrestore(&watchdog_lock, flags); |
370 | } | |
fb63a0eb MS |
371 | |
372 | static void clocksource_dequeue_watchdog(struct clocksource *cs) | |
373 | { | |
fb63a0eb MS |
374 | unsigned long flags; |
375 | ||
376 | spin_lock_irqsave(&watchdog_lock, flags); | |
a89c7edb TG |
377 | if (cs != watchdog) { |
378 | if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) { | |
379 | /* cs is a watched clocksource. */ | |
380 | list_del_init(&cs->wd_list); | |
381 | /* Check if the watchdog timer needs to be stopped. */ | |
382 | clocksource_stop_watchdog(); | |
fb63a0eb MS |
383 | } |
384 | } | |
fb63a0eb MS |
385 | spin_unlock_irqrestore(&watchdog_lock, flags); |
386 | } | |
387 | ||
332962f2 | 388 | static int __clocksource_watchdog_kthread(void) |
c55c87c8 MS |
389 | { |
390 | struct clocksource *cs, *tmp; | |
391 | unsigned long flags; | |
6ea41d25 | 392 | LIST_HEAD(unstable); |
332962f2 | 393 | int select = 0; |
c55c87c8 MS |
394 | |
395 | spin_lock_irqsave(&watchdog_lock, flags); | |
332962f2 | 396 | list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) { |
c55c87c8 MS |
397 | if (cs->flags & CLOCK_SOURCE_UNSTABLE) { |
398 | list_del_init(&cs->wd_list); | |
6ea41d25 | 399 | list_add(&cs->wd_list, &unstable); |
332962f2 TG |
400 | select = 1; |
401 | } | |
402 | if (cs->flags & CLOCK_SOURCE_RESELECT) { | |
403 | cs->flags &= ~CLOCK_SOURCE_RESELECT; | |
404 | select = 1; | |
c55c87c8 | 405 | } |
332962f2 | 406 | } |
c55c87c8 MS |
407 | /* Check if the watchdog timer needs to be stopped. */ |
408 | clocksource_stop_watchdog(); | |
6ea41d25 TG |
409 | spin_unlock_irqrestore(&watchdog_lock, flags); |
410 | ||
411 | /* Needs to be done outside of watchdog lock */ | |
412 | list_for_each_entry_safe(cs, tmp, &unstable, wd_list) { | |
413 | list_del_init(&cs->wd_list); | |
d0981a1b | 414 | __clocksource_change_rating(cs, 0); |
6ea41d25 | 415 | } |
332962f2 TG |
416 | return select; |
417 | } | |
418 | ||
419 | static int clocksource_watchdog_kthread(void *data) | |
420 | { | |
421 | mutex_lock(&clocksource_mutex); | |
422 | if (__clocksource_watchdog_kthread()) | |
423 | clocksource_select(); | |
d0981a1b | 424 | mutex_unlock(&clocksource_mutex); |
01548f4d | 425 | return 0; |
c55c87c8 MS |
426 | } |
427 | ||
7eaeb343 TG |
428 | static bool clocksource_is_watchdog(struct clocksource *cs) |
429 | { | |
430 | return cs == watchdog; | |
431 | } | |
432 | ||
fb63a0eb MS |
433 | #else /* CONFIG_CLOCKSOURCE_WATCHDOG */ |
434 | ||
435 | static void clocksource_enqueue_watchdog(struct clocksource *cs) | |
5d8b34fd TG |
436 | { |
437 | if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) | |
438 | cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES; | |
439 | } | |
b52f52a0 | 440 | |
bbf66d89 | 441 | static void clocksource_select_watchdog(bool fallback) { } |
fb63a0eb | 442 | static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { } |
b52f52a0 | 443 | static inline void clocksource_resume_watchdog(void) { } |
332962f2 | 444 | static inline int __clocksource_watchdog_kthread(void) { return 0; } |
7eaeb343 | 445 | static bool clocksource_is_watchdog(struct clocksource *cs) { return false; } |
397bbf6d | 446 | void clocksource_mark_unstable(struct clocksource *cs) { } |
fb63a0eb MS |
447 | |
448 | #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */ | |
5d8b34fd | 449 | |
c54a42b1 MD |
450 | /** |
451 | * clocksource_suspend - suspend the clocksource(s) | |
452 | */ | |
453 | void clocksource_suspend(void) | |
454 | { | |
455 | struct clocksource *cs; | |
456 | ||
457 | list_for_each_entry_reverse(cs, &clocksource_list, list) | |
458 | if (cs->suspend) | |
459 | cs->suspend(cs); | |
460 | } | |
461 | ||
b52f52a0 TG |
462 | /** |
463 | * clocksource_resume - resume the clocksource(s) | |
464 | */ | |
465 | void clocksource_resume(void) | |
466 | { | |
2e197586 | 467 | struct clocksource *cs; |
b52f52a0 | 468 | |
75c5158f | 469 | list_for_each_entry(cs, &clocksource_list, list) |
b52f52a0 | 470 | if (cs->resume) |
17622339 | 471 | cs->resume(cs); |
b52f52a0 TG |
472 | |
473 | clocksource_resume_watchdog(); | |
b52f52a0 TG |
474 | } |
475 | ||
7c3078b6 JW |
476 | /** |
477 | * clocksource_touch_watchdog - Update watchdog | |
478 | * | |
479 | * Update the watchdog after exception contexts such as kgdb so as not | |
7b7422a5 TG |
480 | * to incorrectly trip the watchdog. This might fail when the kernel |
481 | * was stopped in code which holds watchdog_lock. | |
7c3078b6 JW |
482 | */ |
483 | void clocksource_touch_watchdog(void) | |
484 | { | |
485 | clocksource_resume_watchdog(); | |
486 | } | |
487 | ||
d65670a7 JS |
488 | /** |
489 | * clocksource_max_adjustment- Returns max adjustment amount | |
490 | * @cs: Pointer to clocksource | |
491 | * | |
492 | */ | |
493 | static u32 clocksource_max_adjustment(struct clocksource *cs) | |
494 | { | |
495 | u64 ret; | |
496 | /* | |
88b28adf | 497 | * We won't try to correct for more than 11% adjustments (110,000 ppm), |
d65670a7 JS |
498 | */ |
499 | ret = (u64)cs->mult * 11; | |
500 | do_div(ret,100); | |
501 | return (u32)ret; | |
502 | } | |
503 | ||
98962465 | 504 | /** |
87d8b9eb SB |
505 | * clocks_calc_max_nsecs - Returns maximum nanoseconds that can be converted |
506 | * @mult: cycle to nanosecond multiplier | |
507 | * @shift: cycle to nanosecond divisor (power of two) | |
508 | * @maxadj: maximum adjustment value to mult (~11%) | |
509 | * @mask: bitmask for two's complement subtraction of non 64 bit counters | |
fb82fe2f JS |
510 | * @max_cyc: maximum cycle value before potential overflow (does not include |
511 | * any safety margin) | |
362fde04 | 512 | * |
8e56f33f JS |
513 | * NOTE: This function includes a safety margin of 50%, in other words, we |
514 | * return half the number of nanoseconds the hardware counter can technically | |
515 | * cover. This is done so that we can potentially detect problems caused by | |
516 | * delayed timers or bad hardware, which might result in time intervals that | |
571af55a | 517 | * are larger than what the math used can handle without overflows. |
98962465 | 518 | */ |
fb82fe2f | 519 | u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc) |
98962465 JH |
520 | { |
521 | u64 max_nsecs, max_cycles; | |
522 | ||
523 | /* | |
524 | * Calculate the maximum number of cycles that we can pass to the | |
6086e346 | 525 | * cyc2ns() function without overflowing a 64-bit result. |
98962465 | 526 | */ |
6086e346 JS |
527 | max_cycles = ULLONG_MAX; |
528 | do_div(max_cycles, mult+maxadj); | |
98962465 JH |
529 | |
530 | /* | |
531 | * The actual maximum number of cycles we can defer the clocksource is | |
87d8b9eb | 532 | * determined by the minimum of max_cycles and mask. |
d65670a7 JS |
533 | * Note: Here we subtract the maxadj to make sure we don't sleep for |
534 | * too long if there's a large negative adjustment. | |
98962465 | 535 | */ |
87d8b9eb SB |
536 | max_cycles = min(max_cycles, mask); |
537 | max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift); | |
538 | ||
fb82fe2f JS |
539 | /* return the max_cycles value as well if requested */ |
540 | if (max_cyc) | |
541 | *max_cyc = max_cycles; | |
542 | ||
362fde04 JS |
543 | /* Return 50% of the actual maximum, so we can detect bad values */ |
544 | max_nsecs >>= 1; | |
545 | ||
87d8b9eb SB |
546 | return max_nsecs; |
547 | } | |
548 | ||
549 | /** | |
fb82fe2f JS |
550 | * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles |
551 | * @cs: Pointer to clocksource to be updated | |
87d8b9eb SB |
552 | * |
553 | */ | |
fb82fe2f | 554 | static inline void clocksource_update_max_deferment(struct clocksource *cs) |
87d8b9eb | 555 | { |
fb82fe2f JS |
556 | cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift, |
557 | cs->maxadj, cs->mask, | |
558 | &cs->max_cycles); | |
98962465 JH |
559 | } |
560 | ||
592913ec | 561 | #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET |
734efb46 | 562 | |
f5a2e343 | 563 | static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur) |
5d33b883 TG |
564 | { |
565 | struct clocksource *cs; | |
566 | ||
567 | if (!finished_booting || list_empty(&clocksource_list)) | |
568 | return NULL; | |
569 | ||
570 | /* | |
571 | * We pick the clocksource with the highest rating. If oneshot | |
572 | * mode is active, we pick the highres valid clocksource with | |
573 | * the best rating. | |
574 | */ | |
575 | list_for_each_entry(cs, &clocksource_list, list) { | |
f5a2e343 TG |
576 | if (skipcur && cs == curr_clocksource) |
577 | continue; | |
5d33b883 TG |
578 | if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES)) |
579 | continue; | |
580 | return cs; | |
581 | } | |
582 | return NULL; | |
583 | } | |
584 | ||
f5a2e343 | 585 | static void __clocksource_select(bool skipcur) |
734efb46 | 586 | { |
5d33b883 | 587 | bool oneshot = tick_oneshot_mode_active(); |
f1b82746 | 588 | struct clocksource *best, *cs; |
5d8b34fd | 589 | |
5d33b883 | 590 | /* Find the best suitable clocksource */ |
f5a2e343 | 591 | best = clocksource_find_best(oneshot, skipcur); |
5d33b883 | 592 | if (!best) |
f1b82746 | 593 | return; |
5d33b883 | 594 | |
f1b82746 MS |
595 | /* Check for the override clocksource. */ |
596 | list_for_each_entry(cs, &clocksource_list, list) { | |
f5a2e343 TG |
597 | if (skipcur && cs == curr_clocksource) |
598 | continue; | |
f1b82746 MS |
599 | if (strcmp(cs->name, override_name) != 0) |
600 | continue; | |
601 | /* | |
602 | * Check to make sure we don't switch to a non-highres | |
603 | * capable clocksource if the tick code is in oneshot | |
604 | * mode (highres or nohz) | |
605 | */ | |
5d33b883 | 606 | if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) { |
f1b82746 | 607 | /* Override clocksource cannot be used. */ |
36374583 KW |
608 | if (cs->flags & CLOCK_SOURCE_UNSTABLE) { |
609 | pr_warn("Override clocksource %s is unstable and not HRT compatible - cannot switch while in HRT/NOHZ mode\n", | |
610 | cs->name); | |
611 | override_name[0] = 0; | |
612 | } else { | |
613 | /* | |
614 | * The override cannot be currently verified. | |
615 | * Deferring to let the watchdog check. | |
616 | */ | |
617 | pr_info("Override clocksource %s is not currently HRT compatible - deferring\n", | |
618 | cs->name); | |
619 | } | |
f1b82746 MS |
620 | } else |
621 | /* Override clocksource can be used. */ | |
622 | best = cs; | |
623 | break; | |
624 | } | |
ba919d1c TG |
625 | |
626 | if (curr_clocksource != best && !timekeeping_notify(best)) { | |
627 | pr_info("Switched to clocksource %s\n", best->name); | |
75c5158f | 628 | curr_clocksource = best; |
75c5158f | 629 | } |
f1b82746 | 630 | } |
734efb46 | 631 | |
f5a2e343 TG |
632 | /** |
633 | * clocksource_select - Select the best clocksource available | |
634 | * | |
635 | * Private function. Must hold clocksource_mutex when called. | |
636 | * | |
637 | * Select the clocksource with the best rating, or the clocksource, | |
638 | * which is selected by userspace override. | |
639 | */ | |
640 | static void clocksource_select(void) | |
641 | { | |
cfed432d | 642 | __clocksource_select(false); |
f5a2e343 TG |
643 | } |
644 | ||
7eaeb343 TG |
645 | static void clocksource_select_fallback(void) |
646 | { | |
cfed432d | 647 | __clocksource_select(true); |
7eaeb343 TG |
648 | } |
649 | ||
592913ec | 650 | #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */ |
54a6bc0b | 651 | static inline void clocksource_select(void) { } |
1eaff672 | 652 | static inline void clocksource_select_fallback(void) { } |
54a6bc0b TG |
653 | |
654 | #endif | |
655 | ||
75c5158f MS |
656 | /* |
657 | * clocksource_done_booting - Called near the end of core bootup | |
658 | * | |
659 | * Hack to avoid lots of clocksource churn at boot time. | |
660 | * We use fs_initcall because we want this to start before | |
661 | * device_initcall but after subsys_initcall. | |
662 | */ | |
663 | static int __init clocksource_done_booting(void) | |
664 | { | |
ad6759fb JS |
665 | mutex_lock(&clocksource_mutex); |
666 | curr_clocksource = clocksource_default_clock(); | |
75c5158f | 667 | finished_booting = 1; |
54a6bc0b TG |
668 | /* |
669 | * Run the watchdog first to eliminate unstable clock sources | |
670 | */ | |
332962f2 | 671 | __clocksource_watchdog_kthread(); |
75c5158f | 672 | clocksource_select(); |
e6c73305 | 673 | mutex_unlock(&clocksource_mutex); |
75c5158f MS |
674 | return 0; |
675 | } | |
676 | fs_initcall(clocksource_done_booting); | |
677 | ||
92c7e002 TG |
678 | /* |
679 | * Enqueue the clocksource sorted by rating | |
734efb46 | 680 | */ |
f1b82746 | 681 | static void clocksource_enqueue(struct clocksource *cs) |
734efb46 | 682 | { |
f1b82746 MS |
683 | struct list_head *entry = &clocksource_list; |
684 | struct clocksource *tmp; | |
92c7e002 | 685 | |
0fb71d34 | 686 | list_for_each_entry(tmp, &clocksource_list, list) { |
92c7e002 | 687 | /* Keep track of the place, where to insert */ |
0fb71d34 MH |
688 | if (tmp->rating < cs->rating) |
689 | break; | |
690 | entry = &tmp->list; | |
691 | } | |
f1b82746 | 692 | list_add(&cs->list, entry); |
734efb46 JS |
693 | } |
694 | ||
d7e81c26 | 695 | /** |
fba9e072 | 696 | * __clocksource_update_freq_scale - Used update clocksource with new freq |
b1b73d09 | 697 | * @cs: clocksource to be registered |
d7e81c26 JS |
698 | * @scale: Scale factor multiplied against freq to get clocksource hz |
699 | * @freq: clocksource frequency (cycles per second) divided by scale | |
700 | * | |
852db46d | 701 | * This should only be called from the clocksource->enable() method. |
d7e81c26 JS |
702 | * |
703 | * This *SHOULD NOT* be called directly! Please use the | |
fba9e072 JS |
704 | * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper |
705 | * functions. | |
d7e81c26 | 706 | */ |
fba9e072 | 707 | void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq) |
d7e81c26 | 708 | { |
c0e299b1 | 709 | u64 sec; |
f8935983 | 710 | |
d7e81c26 | 711 | /* |
f8935983 JS |
712 | * Default clocksources are *special* and self-define their mult/shift. |
713 | * But, you're not special, so you should specify a freq value. | |
d7e81c26 | 714 | */ |
f8935983 JS |
715 | if (freq) { |
716 | /* | |
717 | * Calc the maximum number of seconds which we can run before | |
718 | * wrapping around. For clocksources which have a mask > 32-bit | |
719 | * we need to limit the max sleep time to have a good | |
720 | * conversion precision. 10 minutes is still a reasonable | |
721 | * amount. That results in a shift value of 24 for a | |
722 | * clocksource with mask >= 40-bit and f >= 4GHz. That maps to | |
723 | * ~ 0.06ppm granularity for NTP. | |
724 | */ | |
725 | sec = cs->mask; | |
726 | do_div(sec, freq); | |
727 | do_div(sec, scale); | |
728 | if (!sec) | |
729 | sec = 1; | |
730 | else if (sec > 600 && cs->mask > UINT_MAX) | |
731 | sec = 600; | |
732 | ||
733 | clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, | |
734 | NSEC_PER_SEC / scale, sec * scale); | |
735 | } | |
d65670a7 | 736 | /* |
362fde04 JS |
737 | * Ensure clocksources that have large 'mult' values don't overflow |
738 | * when adjusted. | |
d65670a7 JS |
739 | */ |
740 | cs->maxadj = clocksource_max_adjustment(cs); | |
f8935983 JS |
741 | while (freq && ((cs->mult + cs->maxadj < cs->mult) |
742 | || (cs->mult - cs->maxadj > cs->mult))) { | |
d65670a7 JS |
743 | cs->mult >>= 1; |
744 | cs->shift--; | |
745 | cs->maxadj = clocksource_max_adjustment(cs); | |
746 | } | |
747 | ||
f8935983 JS |
748 | /* |
749 | * Only warn for *special* clocksources that self-define | |
750 | * their mult/shift values and don't specify a freq. | |
751 | */ | |
752 | WARN_ONCE(cs->mult + cs->maxadj < cs->mult, | |
753 | "timekeeping: Clocksource %s might overflow on 11%% adjustment\n", | |
754 | cs->name); | |
755 | ||
fb82fe2f | 756 | clocksource_update_max_deferment(cs); |
8cc8c525 | 757 | |
45bbfe64 JP |
758 | pr_info("%s: mask: 0x%llx max_cycles: 0x%llx, max_idle_ns: %lld ns\n", |
759 | cs->name, cs->mask, cs->max_cycles, cs->max_idle_ns); | |
852db46d | 760 | } |
fba9e072 | 761 | EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale); |
852db46d JS |
762 | |
763 | /** | |
764 | * __clocksource_register_scale - Used to install new clocksources | |
b1b73d09 | 765 | * @cs: clocksource to be registered |
852db46d JS |
766 | * @scale: Scale factor multiplied against freq to get clocksource hz |
767 | * @freq: clocksource frequency (cycles per second) divided by scale | |
768 | * | |
769 | * Returns -EBUSY if registration fails, zero otherwise. | |
770 | * | |
771 | * This *SHOULD NOT* be called directly! Please use the | |
772 | * clocksource_register_hz() or clocksource_register_khz helper functions. | |
773 | */ | |
774 | int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) | |
775 | { | |
776 | ||
b595076a | 777 | /* Initialize mult/shift and max_idle_ns */ |
fba9e072 | 778 | __clocksource_update_freq_scale(cs, scale, freq); |
d7e81c26 | 779 | |
be278e98 | 780 | /* Add clocksource to the clocksource list */ |
d7e81c26 JS |
781 | mutex_lock(&clocksource_mutex); |
782 | clocksource_enqueue(cs); | |
d7e81c26 | 783 | clocksource_enqueue_watchdog(cs); |
e05b2efb | 784 | clocksource_select(); |
bbf66d89 | 785 | clocksource_select_watchdog(false); |
d7e81c26 JS |
786 | mutex_unlock(&clocksource_mutex); |
787 | return 0; | |
788 | } | |
789 | EXPORT_SYMBOL_GPL(__clocksource_register_scale); | |
790 | ||
d0981a1b TG |
791 | static void __clocksource_change_rating(struct clocksource *cs, int rating) |
792 | { | |
793 | list_del(&cs->list); | |
794 | cs->rating = rating; | |
795 | clocksource_enqueue(cs); | |
d0981a1b TG |
796 | } |
797 | ||
734efb46 | 798 | /** |
92c7e002 | 799 | * clocksource_change_rating - Change the rating of a registered clocksource |
b1b73d09 KK |
800 | * @cs: clocksource to be changed |
801 | * @rating: new rating | |
734efb46 | 802 | */ |
92c7e002 | 803 | void clocksource_change_rating(struct clocksource *cs, int rating) |
734efb46 | 804 | { |
75c5158f | 805 | mutex_lock(&clocksource_mutex); |
d0981a1b | 806 | __clocksource_change_rating(cs, rating); |
332962f2 | 807 | clocksource_select(); |
bbf66d89 | 808 | clocksource_select_watchdog(false); |
75c5158f | 809 | mutex_unlock(&clocksource_mutex); |
734efb46 | 810 | } |
fb63a0eb | 811 | EXPORT_SYMBOL(clocksource_change_rating); |
734efb46 | 812 | |
7eaeb343 TG |
813 | /* |
814 | * Unbind clocksource @cs. Called with clocksource_mutex held | |
815 | */ | |
816 | static int clocksource_unbind(struct clocksource *cs) | |
817 | { | |
bbf66d89 VK |
818 | if (clocksource_is_watchdog(cs)) { |
819 | /* Select and try to install a replacement watchdog. */ | |
820 | clocksource_select_watchdog(true); | |
821 | if (clocksource_is_watchdog(cs)) | |
822 | return -EBUSY; | |
823 | } | |
7eaeb343 TG |
824 | |
825 | if (cs == curr_clocksource) { | |
826 | /* Select and try to install a replacement clock source */ | |
827 | clocksource_select_fallback(); | |
828 | if (curr_clocksource == cs) | |
829 | return -EBUSY; | |
830 | } | |
831 | clocksource_dequeue_watchdog(cs); | |
832 | list_del_init(&cs->list); | |
833 | return 0; | |
834 | } | |
835 | ||
4713e22c TG |
836 | /** |
837 | * clocksource_unregister - remove a registered clocksource | |
b1b73d09 | 838 | * @cs: clocksource to be unregistered |
4713e22c | 839 | */ |
a89c7edb | 840 | int clocksource_unregister(struct clocksource *cs) |
4713e22c | 841 | { |
a89c7edb TG |
842 | int ret = 0; |
843 | ||
75c5158f | 844 | mutex_lock(&clocksource_mutex); |
a89c7edb TG |
845 | if (!list_empty(&cs->list)) |
846 | ret = clocksource_unbind(cs); | |
75c5158f | 847 | mutex_unlock(&clocksource_mutex); |
a89c7edb | 848 | return ret; |
4713e22c | 849 | } |
fb63a0eb | 850 | EXPORT_SYMBOL(clocksource_unregister); |
4713e22c | 851 | |
2b013700 | 852 | #ifdef CONFIG_SYSFS |
734efb46 JS |
853 | /** |
854 | * sysfs_show_current_clocksources - sysfs interface for current clocksource | |
855 | * @dev: unused | |
b1b73d09 | 856 | * @attr: unused |
734efb46 JS |
857 | * @buf: char buffer to be filled with clocksource list |
858 | * | |
859 | * Provides sysfs interface for listing current clocksource. | |
860 | */ | |
861 | static ssize_t | |
d369a5d8 KS |
862 | sysfs_show_current_clocksources(struct device *dev, |
863 | struct device_attribute *attr, char *buf) | |
734efb46 | 864 | { |
5e2cb101 | 865 | ssize_t count = 0; |
734efb46 | 866 | |
75c5158f | 867 | mutex_lock(&clocksource_mutex); |
5e2cb101 | 868 | count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name); |
75c5158f | 869 | mutex_unlock(&clocksource_mutex); |
734efb46 | 870 | |
5e2cb101 | 871 | return count; |
734efb46 JS |
872 | } |
873 | ||
891292a7 | 874 | ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt) |
29b54078 TG |
875 | { |
876 | size_t ret = cnt; | |
877 | ||
878 | /* strings from sysfs write are not 0 terminated! */ | |
879 | if (!cnt || cnt >= CS_NAME_LEN) | |
880 | return -EINVAL; | |
881 | ||
882 | /* strip of \n: */ | |
883 | if (buf[cnt-1] == '\n') | |
884 | cnt--; | |
885 | if (cnt > 0) | |
886 | memcpy(dst, buf, cnt); | |
887 | dst[cnt] = 0; | |
888 | return ret; | |
889 | } | |
890 | ||
734efb46 JS |
891 | /** |
892 | * sysfs_override_clocksource - interface for manually overriding clocksource | |
893 | * @dev: unused | |
b1b73d09 | 894 | * @attr: unused |
734efb46 JS |
895 | * @buf: name of override clocksource |
896 | * @count: length of buffer | |
897 | * | |
898 | * Takes input from sysfs interface for manually overriding the default | |
b71a8eb0 | 899 | * clocksource selection. |
734efb46 | 900 | */ |
d369a5d8 KS |
901 | static ssize_t sysfs_override_clocksource(struct device *dev, |
902 | struct device_attribute *attr, | |
734efb46 JS |
903 | const char *buf, size_t count) |
904 | { | |
233bcb41 | 905 | ssize_t ret; |
734efb46 | 906 | |
75c5158f | 907 | mutex_lock(&clocksource_mutex); |
734efb46 | 908 | |
03e13cf5 | 909 | ret = sysfs_get_uname(buf, override_name, count); |
29b54078 TG |
910 | if (ret >= 0) |
911 | clocksource_select(); | |
734efb46 | 912 | |
75c5158f | 913 | mutex_unlock(&clocksource_mutex); |
734efb46 JS |
914 | |
915 | return ret; | |
916 | } | |
917 | ||
7eaeb343 TG |
918 | /** |
919 | * sysfs_unbind_current_clocksource - interface for manually unbinding clocksource | |
920 | * @dev: unused | |
921 | * @attr: unused | |
922 | * @buf: unused | |
923 | * @count: length of buffer | |
924 | * | |
925 | * Takes input from sysfs interface for manually unbinding a clocksource. | |
926 | */ | |
927 | static ssize_t sysfs_unbind_clocksource(struct device *dev, | |
928 | struct device_attribute *attr, | |
929 | const char *buf, size_t count) | |
930 | { | |
931 | struct clocksource *cs; | |
932 | char name[CS_NAME_LEN]; | |
233bcb41 | 933 | ssize_t ret; |
7eaeb343 | 934 | |
03e13cf5 | 935 | ret = sysfs_get_uname(buf, name, count); |
7eaeb343 TG |
936 | if (ret < 0) |
937 | return ret; | |
938 | ||
939 | ret = -ENODEV; | |
940 | mutex_lock(&clocksource_mutex); | |
941 | list_for_each_entry(cs, &clocksource_list, list) { | |
942 | if (strcmp(cs->name, name)) | |
943 | continue; | |
944 | ret = clocksource_unbind(cs); | |
945 | break; | |
946 | } | |
947 | mutex_unlock(&clocksource_mutex); | |
948 | ||
949 | return ret ? ret : count; | |
950 | } | |
951 | ||
734efb46 JS |
952 | /** |
953 | * sysfs_show_available_clocksources - sysfs interface for listing clocksource | |
954 | * @dev: unused | |
b1b73d09 | 955 | * @attr: unused |
734efb46 JS |
956 | * @buf: char buffer to be filled with clocksource list |
957 | * | |
958 | * Provides sysfs interface for listing registered clocksources | |
959 | */ | |
960 | static ssize_t | |
d369a5d8 KS |
961 | sysfs_show_available_clocksources(struct device *dev, |
962 | struct device_attribute *attr, | |
4a0b2b4d | 963 | char *buf) |
734efb46 | 964 | { |
2e197586 | 965 | struct clocksource *src; |
5e2cb101 | 966 | ssize_t count = 0; |
734efb46 | 967 | |
75c5158f | 968 | mutex_lock(&clocksource_mutex); |
2e197586 | 969 | list_for_each_entry(src, &clocksource_list, list) { |
cd6d95d8 TG |
970 | /* |
971 | * Don't show non-HRES clocksource if the tick code is | |
972 | * in one shot mode (highres=on or nohz=on) | |
973 | */ | |
974 | if (!tick_oneshot_mode_active() || | |
975 | (src->flags & CLOCK_SOURCE_VALID_FOR_HRES)) | |
3f68535a | 976 | count += snprintf(buf + count, |
5e2cb101 MX |
977 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), |
978 | "%s ", src->name); | |
734efb46 | 979 | } |
75c5158f | 980 | mutex_unlock(&clocksource_mutex); |
734efb46 | 981 | |
5e2cb101 MX |
982 | count += snprintf(buf + count, |
983 | max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n"); | |
734efb46 | 984 | |
5e2cb101 | 985 | return count; |
734efb46 JS |
986 | } |
987 | ||
988 | /* | |
989 | * Sysfs setup bits: | |
990 | */ | |
d369a5d8 | 991 | static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources, |
f5f1a24a | 992 | sysfs_override_clocksource); |
734efb46 | 993 | |
7eaeb343 TG |
994 | static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource); |
995 | ||
d369a5d8 | 996 | static DEVICE_ATTR(available_clocksource, 0444, |
f5f1a24a | 997 | sysfs_show_available_clocksources, NULL); |
734efb46 | 998 | |
d369a5d8 | 999 | static struct bus_type clocksource_subsys = { |
af5ca3f4 | 1000 | .name = "clocksource", |
d369a5d8 | 1001 | .dev_name = "clocksource", |
734efb46 JS |
1002 | }; |
1003 | ||
d369a5d8 | 1004 | static struct device device_clocksource = { |
734efb46 | 1005 | .id = 0, |
d369a5d8 | 1006 | .bus = &clocksource_subsys, |
734efb46 JS |
1007 | }; |
1008 | ||
ad596171 | 1009 | static int __init init_clocksource_sysfs(void) |
734efb46 | 1010 | { |
d369a5d8 | 1011 | int error = subsys_system_register(&clocksource_subsys, NULL); |
734efb46 JS |
1012 | |
1013 | if (!error) | |
d369a5d8 | 1014 | error = device_register(&device_clocksource); |
734efb46 | 1015 | if (!error) |
d369a5d8 | 1016 | error = device_create_file( |
734efb46 | 1017 | &device_clocksource, |
d369a5d8 | 1018 | &dev_attr_current_clocksource); |
7eaeb343 TG |
1019 | if (!error) |
1020 | error = device_create_file(&device_clocksource, | |
1021 | &dev_attr_unbind_clocksource); | |
734efb46 | 1022 | if (!error) |
d369a5d8 | 1023 | error = device_create_file( |
734efb46 | 1024 | &device_clocksource, |
d369a5d8 | 1025 | &dev_attr_available_clocksource); |
734efb46 JS |
1026 | return error; |
1027 | } | |
1028 | ||
1029 | device_initcall(init_clocksource_sysfs); | |
2b013700 | 1030 | #endif /* CONFIG_SYSFS */ |
734efb46 JS |
1031 | |
1032 | /** | |
1033 | * boot_override_clocksource - boot clock override | |
1034 | * @str: override name | |
1035 | * | |
1036 | * Takes a clocksource= boot argument and uses it | |
1037 | * as the clocksource override name. | |
1038 | */ | |
1039 | static int __init boot_override_clocksource(char* str) | |
1040 | { | |
75c5158f | 1041 | mutex_lock(&clocksource_mutex); |
734efb46 JS |
1042 | if (str) |
1043 | strlcpy(override_name, str, sizeof(override_name)); | |
75c5158f | 1044 | mutex_unlock(&clocksource_mutex); |
734efb46 JS |
1045 | return 1; |
1046 | } | |
1047 | ||
1048 | __setup("clocksource=", boot_override_clocksource); | |
1049 | ||
1050 | /** | |
1051 | * boot_override_clock - Compatibility layer for deprecated boot option | |
1052 | * @str: override name | |
1053 | * | |
1054 | * DEPRECATED! Takes a clock= boot argument and uses it | |
1055 | * as the clocksource override name | |
1056 | */ | |
1057 | static int __init boot_override_clock(char* str) | |
1058 | { | |
5d0cf410 | 1059 | if (!strcmp(str, "pmtmr")) { |
45bbfe64 | 1060 | pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n"); |
5d0cf410 JS |
1061 | return boot_override_clocksource("acpi_pm"); |
1062 | } | |
45bbfe64 | 1063 | pr_warn("clock= boot option is deprecated - use clocksource=xyz\n"); |
734efb46 JS |
1064 | return boot_override_clocksource(str); |
1065 | } | |
1066 | ||
1067 | __setup("clock=", boot_override_clock); |