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