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