~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/time/clocksource.c

Version: ~ [ linux-5.10-rc1 ] ~ [ linux-5.9.1 ] ~ [ linux-5.8.16 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.72 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.152 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.202 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.240 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.240 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * linux/kernel/time/clocksource.c
  3  *
  4  * This file contains the functions which manage clocksource drivers.
  5  *
  6  * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
  7  *
  8  * This program is free software; you can redistribute it and/or modify
  9  * it under the terms of the GNU General Public License as published by
 10  * the Free Software Foundation; either version 2 of the License, or
 11  * (at your option) any later version.
 12  *
 13  * This program is distributed in the hope that it will be useful,
 14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  * GNU General Public License for more details.
 17  *
 18  * You should have received a copy of the GNU General Public License
 19  * along with this program; if not, write to the Free Software
 20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 21  *
 22  * TODO WishList:
 23  *   o Allow clocksource drivers to be unregistered
 24  */
 25 
 26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 27 
 28 #include <linux/device.h>
 29 #include <linux/clocksource.h>
 30 #include <linux/init.h>
 31 #include <linux/module.h>
 32 #include <linux/sched.h> /* for spin_unlock_irq() using preempt_count() m68k */
 33 #include <linux/tick.h>
 34 #include <linux/kthread.h>
 35 
 36 #include "tick-internal.h"
 37 #include "timekeeping_internal.h"
 38 
 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
 45  * @maxsec:     guaranteed runtime conversion range in seconds
 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  *
 54  * The @maxsec conversion range argument controls the time frame in
 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
 63 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
 64 {
 65         u64 tmp;
 66         u32 sft, sftacc= 32;
 67 
 68         /*
 69          * Calculate the shift factor which is limiting the conversion
 70          * range:
 71          */
 72         tmp = ((u64)maxsec * from) >> 32;
 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;
 84                 tmp += from / 2;
 85                 do_div(tmp, from);
 86                 if ((tmp >> sftacc) == 0)
 87                         break;
 88         }
 89         *mult = tmp;
 90         *shift = sft;
 91 }
 92 EXPORT_SYMBOL_GPL(clocks_calc_mult_shift);
 93 
 94 /*[Clocksource internal variables]---------
 95  * curr_clocksource:
 96  *      currently selected clocksource.
 97  * clocksource_list:
 98  *      linked list with the registered clocksources
 99  * clocksource_mutex:
100  *      protects manipulations to curr_clocksource and the clocksource_list
101  * override_name:
102  *      Name of the user-specified clocksource.
103  */
104 static struct clocksource *curr_clocksource;
105 static LIST_HEAD(clocksource_list);
106 static DEFINE_MUTEX(clocksource_mutex);
107 static char override_name[CS_NAME_LEN];
108 static int finished_booting;
109 
110 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
111 static void clocksource_watchdog_work(struct work_struct *work);
112 static void clocksource_select(void);
113 
114 static LIST_HEAD(watchdog_list);
115 static struct clocksource *watchdog;
116 static struct timer_list watchdog_timer;
117 static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
118 static DEFINE_SPINLOCK(watchdog_lock);
119 static int watchdog_running;
120 static atomic_t watchdog_reset_pending;
121 
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 
132 static int clocksource_watchdog_kthread(void *data);
133 static void __clocksource_change_rating(struct clocksource *cs, int rating);
134 
135 /*
136  * Interval: 0.5sec Threshold: 0.0625s
137  */
138 #define WATCHDOG_INTERVAL (HZ >> 1)
139 #define WATCHDOG_THRESHOLD (NSEC_PER_SEC >> 4)
140 
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 
150 static void __clocksource_unstable(struct clocksource *cs)
151 {
152         cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
153         cs->flags |= CLOCK_SOURCE_UNSTABLE;
154 
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;
161                 return;
162         }
163 
164         if (cs->mark_unstable)
165                 cs->mark_unstable(cs);
166 
167         /* kick clocksource_watchdog_kthread() */
168         if (finished_booting)
169                 schedule_work(&watchdog_work);
170 }
171 
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)) {
187                 if (!list_empty(&cs->list) && list_empty(&cs->wd_list))
188                         list_add(&cs->wd_list, &watchdog_list);
189                 __clocksource_unstable(cs);
190         }
191         spin_unlock_irqrestore(&watchdog_lock, flags);
192 }
193 
194 static void clocksource_watchdog(struct timer_list *unused)
195 {
196         struct clocksource *cs;
197         u64 csnow, wdnow, cslast, wdlast, delta;
198         int64_t wd_nsec, cs_nsec;
199         int next_cpu, reset_pending;
200 
201         spin_lock(&watchdog_lock);
202         if (!watchdog_running)
203                 goto out;
204 
205         reset_pending = atomic_read(&watchdog_reset_pending);
206 
207         list_for_each_entry(cs, &watchdog_list, wd_list) {
208 
209                 /* Clocksource already marked unstable? */
210                 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
211                         if (finished_booting)
212                                 schedule_work(&watchdog_work);
213                         continue;
214                 }
215 
216                 local_irq_disable();
217                 csnow = cs->read(cs);
218                 wdnow = watchdog->read(watchdog);
219                 local_irq_enable();
220 
221                 /* Clocksource initialized ? */
222                 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG) ||
223                     atomic_read(&watchdog_reset_pending)) {
224                         cs->flags |= CLOCK_SOURCE_WATCHDOG;
225                         cs->wd_last = wdnow;
226                         cs->cs_last = csnow;
227                         continue;
228                 }
229 
230                 delta = clocksource_delta(wdnow, cs->wd_last, watchdog->mask);
231                 wd_nsec = clocksource_cyc2ns(delta, watchdog->mult,
232                                              watchdog->shift);
233 
234                 delta = clocksource_delta(csnow, cs->cs_last, cs->mask);
235                 cs_nsec = clocksource_cyc2ns(delta, cs->mult, cs->shift);
236                 wdlast = cs->wd_last; /* save these in case we print them */
237                 cslast = cs->cs_last;
238                 cs->cs_last = csnow;
239                 cs->wd_last = wdnow;
240 
241                 if (atomic_read(&watchdog_reset_pending))
242                         continue;
243 
244                 /* Check the deviation from the watchdog clocksource. */
245                 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
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);
248                         pr_warn("                      '%s' wd_now: %llx wd_last: %llx mask: %llx\n",
249                                 watchdog->name, wdnow, wdlast, watchdog->mask);
250                         pr_warn("                      '%s' cs_now: %llx cs_last: %llx mask: %llx\n",
251                                 cs->name, csnow, cslast, cs->mask);
252                         __clocksource_unstable(cs);
253                         continue;
254                 }
255 
256                 if (cs == curr_clocksource && cs->tick_stable)
257                         cs->tick_stable(cs);
258 
259                 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) &&
260                     (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS) &&
261                     (watchdog->flags & CLOCK_SOURCE_IS_CONTINUOUS)) {
262                         /* Mark it valid for high-res. */
263                         cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
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 
272                         /*
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.
279                          */
280                         if (cs != curr_clocksource) {
281                                 cs->flags |= CLOCK_SOURCE_RESELECT;
282                                 schedule_work(&watchdog_work);
283                         } else {
284                                 tick_clock_notify();
285                         }
286                 }
287         }
288 
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 
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);
305 out:
306         spin_unlock(&watchdog_lock);
307 }
308 
309 static inline void clocksource_start_watchdog(void)
310 {
311         if (watchdog_running || !watchdog || list_empty(&watchdog_list))
312                 return;
313         timer_setup(&watchdog_timer, clocksource_watchdog, 0);
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 
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 
335 static void clocksource_resume_watchdog(void)
336 {
337         atomic_inc(&watchdog_reset_pending);
338 }
339 
340 static void clocksource_enqueue_watchdog(struct clocksource *cs)
341 {
342         INIT_LIST_HEAD(&cs->wd_list);
343 
344         if (cs->flags & CLOCK_SOURCE_MUST_VERIFY) {
345                 /* cs is a clocksource to be watched. */
346                 list_add(&cs->wd_list, &watchdog_list);
347                 cs->flags &= ~CLOCK_SOURCE_WATCHDOG;
348         } else {
349                 /* cs is a watchdog. */
350                 if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
351                         cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
352         }
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 
375                 /* Pick the best watchdog. */
376                 if (!watchdog || cs->rating > watchdog->rating)
377                         watchdog = cs;
378         }
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 
387         /* Check if the watchdog timer needs to be started. */
388         clocksource_start_watchdog();
389         spin_unlock_irqrestore(&watchdog_lock, flags);
390 }
391 
392 static void clocksource_dequeue_watchdog(struct clocksource *cs)
393 {
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();
400                 }
401         }
402 }
403 
404 static int __clocksource_watchdog_kthread(void)
405 {
406         struct clocksource *cs, *tmp;
407         unsigned long flags;
408         int select = 0;
409 
410         spin_lock_irqsave(&watchdog_lock, flags);
411         list_for_each_entry_safe(cs, tmp, &watchdog_list, wd_list) {
412                 if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
413                         list_del_init(&cs->wd_list);
414                         __clocksource_change_rating(cs, 0);
415                         select = 1;
416                 }
417                 if (cs->flags & CLOCK_SOURCE_RESELECT) {
418                         cs->flags &= ~CLOCK_SOURCE_RESELECT;
419                         select = 1;
420                 }
421         }
422         /* Check if the watchdog timer needs to be stopped. */
423         clocksource_stop_watchdog();
424         spin_unlock_irqrestore(&watchdog_lock, flags);
425 
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();
434         mutex_unlock(&clocksource_mutex);
435         return 0;
436 }
437 
438 static bool clocksource_is_watchdog(struct clocksource *cs)
439 {
440         return cs == watchdog;
441 }
442 
443 #else /* CONFIG_CLOCKSOURCE_WATCHDOG */
444 
445 static void clocksource_enqueue_watchdog(struct clocksource *cs)
446 {
447         if (cs->flags & CLOCK_SOURCE_IS_CONTINUOUS)
448                 cs->flags |= CLOCK_SOURCE_VALID_FOR_HRES;
449 }
450 
451 static void clocksource_select_watchdog(bool fallback) { }
452 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
453 static inline void clocksource_resume_watchdog(void) { }
454 static inline int __clocksource_watchdog_kthread(void) { return 0; }
455 static bool clocksource_is_watchdog(struct clocksource *cs) { return false; }
456 void clocksource_mark_unstable(struct clocksource *cs) { }
457 
458 static void inline clocksource_watchdog_lock(unsigned long *flags) { }
459 static void inline clocksource_watchdog_unlock(unsigned long *flags) { }
460 
461 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
462 
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 
475 /**
476  * clocksource_resume - resume the clocksource(s)
477  */
478 void clocksource_resume(void)
479 {
480         struct clocksource *cs;
481 
482         list_for_each_entry(cs, &clocksource_list, list)
483                 if (cs->resume)
484                         cs->resume(cs);
485 
486         clocksource_resume_watchdog();
487 }
488 
489 /**
490  * clocksource_touch_watchdog - Update watchdog
491  *
492  * Update the watchdog after exception contexts such as kgdb so as not
493  * to incorrectly trip the watchdog. This might fail when the kernel
494  * was stopped in code which holds watchdog_lock.
495  */
496 void clocksource_touch_watchdog(void)
497 {
498         clocksource_resume_watchdog();
499 }
500 
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         /*
510          * We won't try to correct for more than 11% adjustments (110,000 ppm),
511          */
512         ret = (u64)cs->mult * 11;
513         do_div(ret,100);
514         return (u32)ret;
515 }
516 
517 /**
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
523  * @max_cyc:    maximum cycle value before potential overflow (does not include
524  *              any safety margin)
525  *
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
530  * are larger than what the math used can handle without overflows.
531  */
532 u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cyc)
533 {
534         u64 max_nsecs, max_cycles;
535 
536         /*
537          * Calculate the maximum number of cycles that we can pass to the
538          * cyc2ns() function without overflowing a 64-bit result.
539          */
540         max_cycles = ULLONG_MAX;
541         do_div(max_cycles, mult+maxadj);
542 
543         /*
544          * The actual maximum number of cycles we can defer the clocksource is
545          * determined by the minimum of max_cycles and mask.
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.
548          */
549         max_cycles = min(max_cycles, mask);
550         max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift);
551 
552         /* return the max_cycles value as well if requested */
553         if (max_cyc)
554                 *max_cyc = max_cycles;
555 
556         /* Return 50% of the actual maximum, so we can detect bad values */
557         max_nsecs >>= 1;
558 
559         return max_nsecs;
560 }
561 
562 /**
563  * clocksource_update_max_deferment - Updates the clocksource max_idle_ns & max_cycles
564  * @cs:         Pointer to clocksource to be updated
565  *
566  */
567 static inline void clocksource_update_max_deferment(struct clocksource *cs)
568 {
569         cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift,
570                                                 cs->maxadj, cs->mask,
571                                                 &cs->max_cycles);
572 }
573 
574 #ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
575 
576 static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)
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) {
589                 if (skipcur && cs == curr_clocksource)
590                         continue;
591                 if (oneshot && !(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES))
592                         continue;
593                 return cs;
594         }
595         return NULL;
596 }
597 
598 static void __clocksource_select(bool skipcur)
599 {
600         bool oneshot = tick_oneshot_mode_active();
601         struct clocksource *best, *cs;
602 
603         /* Find the best suitable clocksource */
604         best = clocksource_find_best(oneshot, skipcur);
605         if (!best)
606                 return;
607 
608         /* Check for the override clocksource. */
609         list_for_each_entry(cs, &clocksource_list, list) {
610                 if (skipcur && cs == curr_clocksource)
611                         continue;
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                  */
619                 if (!(cs->flags & CLOCK_SOURCE_VALID_FOR_HRES) && oneshot) {
620                         /* Override clocksource cannot be used. */
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                         }
633                 } else
634                         /* Override clocksource can be used. */
635                         best = cs;
636                 break;
637         }
638 
639         if (curr_clocksource != best && !timekeeping_notify(best)) {
640                 pr_info("Switched to clocksource %s\n", best->name);
641                 curr_clocksource = best;
642         }
643 }
644 
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 {
655         __clocksource_select(false);
656 }
657 
658 static void clocksource_select_fallback(void)
659 {
660         __clocksource_select(true);
661 }
662 
663 #else /* !CONFIG_ARCH_USES_GETTIMEOFFSET */
664 static inline void clocksource_select(void) { }
665 static inline void clocksource_select_fallback(void) { }
666 
667 #endif
668 
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 {
678         mutex_lock(&clocksource_mutex);
679         curr_clocksource = clocksource_default_clock();
680         finished_booting = 1;
681         /*
682          * Run the watchdog first to eliminate unstable clock sources
683          */
684         __clocksource_watchdog_kthread();
685         clocksource_select();
686         mutex_unlock(&clocksource_mutex);
687         return 0;
688 }
689 fs_initcall(clocksource_done_booting);
690 
691 /*
692  * Enqueue the clocksource sorted by rating
693  */
694 static void clocksource_enqueue(struct clocksource *cs)
695 {
696         struct list_head *entry = &clocksource_list;
697         struct clocksource *tmp;
698 
699         list_for_each_entry(tmp, &clocksource_list, list) {
700                 /* Keep track of the place, where to insert */
701                 if (tmp->rating < cs->rating)
702                         break;
703                 entry = &tmp->list;
704         }
705         list_add(&cs->list, entry);
706 }
707 
708 /**
709  * __clocksource_update_freq_scale - Used update clocksource with new freq
710  * @cs:         clocksource to be registered
711  * @scale:      Scale factor multiplied against freq to get clocksource hz
712  * @freq:       clocksource frequency (cycles per second) divided by scale
713  *
714  * This should only be called from the clocksource->enable() method.
715  *
716  * This *SHOULD NOT* be called directly! Please use the
717  * __clocksource_update_freq_hz() or __clocksource_update_freq_khz() helper
718  * functions.
719  */
720 void __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq)
721 {
722         u64 sec;
723 
724         /*
725          * Default clocksources are *special* and self-define their mult/shift.
726          * But, you're not special, so you should specify a freq value.
727          */
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         }
749         /*
750          * Ensure clocksources that have large 'mult' values don't overflow
751          * when adjusted.
752          */
753         cs->maxadj = clocksource_max_adjustment(cs);
754         while (freq && ((cs->mult + cs->maxadj < cs->mult)
755                 || (cs->mult - cs->maxadj > cs->mult))) {
756                 cs->mult >>= 1;
757                 cs->shift--;
758                 cs->maxadj = clocksource_max_adjustment(cs);
759         }
760 
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 
769         clocksource_update_max_deferment(cs);
770 
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);
773 }
774 EXPORT_SYMBOL_GPL(__clocksource_update_freq_scale);
775 
776 /**
777  * __clocksource_register_scale - Used to install new clocksources
778  * @cs:         clocksource to be registered
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 {
789         unsigned long flags;
790 
791         /* Initialize mult/shift and max_idle_ns */
792         __clocksource_update_freq_scale(cs, scale, freq);
793 
794         /* Add clocksource to the clocksource list */
795         mutex_lock(&clocksource_mutex);
796 
797         clocksource_watchdog_lock(&flags);
798         clocksource_enqueue(cs);
799         clocksource_enqueue_watchdog(cs);
800         clocksource_watchdog_unlock(&flags);
801 
802         clocksource_select();
803         clocksource_select_watchdog(false);
804         mutex_unlock(&clocksource_mutex);
805         return 0;
806 }
807 EXPORT_SYMBOL_GPL(__clocksource_register_scale);
808 
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);
814 }
815 
816 /**
817  * clocksource_change_rating - Change the rating of a registered clocksource
818  * @cs:         clocksource to be changed
819  * @rating:     new rating
820  */
821 void clocksource_change_rating(struct clocksource *cs, int rating)
822 {
823         unsigned long flags;
824 
825         mutex_lock(&clocksource_mutex);
826         clocksource_watchdog_lock(&flags);
827         __clocksource_change_rating(cs, rating);
828         clocksource_watchdog_unlock(&flags);
829 
830         clocksource_select();
831         clocksource_select_watchdog(false);
832         mutex_unlock(&clocksource_mutex);
833 }
834 EXPORT_SYMBOL(clocksource_change_rating);
835 
836 /*
837  * Unbind clocksource @cs. Called with clocksource_mutex held
838  */
839 static int clocksource_unbind(struct clocksource *cs)
840 {
841         unsigned long flags;
842 
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         }
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         }
856 
857         clocksource_watchdog_lock(&flags);
858         clocksource_dequeue_watchdog(cs);
859         list_del_init(&cs->list);
860         clocksource_watchdog_unlock(&flags);
861 
862         return 0;
863 }
864 
865 /**
866  * clocksource_unregister - remove a registered clocksource
867  * @cs: clocksource to be unregistered
868  */
869 int clocksource_unregister(struct clocksource *cs)
870 {
871         int ret = 0;
872 
873         mutex_lock(&clocksource_mutex);
874         if (!list_empty(&cs->list))
875                 ret = clocksource_unbind(cs);
876         mutex_unlock(&clocksource_mutex);
877         return ret;
878 }
879 EXPORT_SYMBOL(clocksource_unregister);
880 
881 #ifdef CONFIG_SYSFS
882 /**
883  * sysfs_show_current_clocksources - sysfs interface for current clocksource
884  * @dev:        unused
885  * @attr:       unused
886  * @buf:        char buffer to be filled with clocksource list
887  *
888  * Provides sysfs interface for listing current clocksource.
889  */
890 static ssize_t
891 sysfs_show_current_clocksources(struct device *dev,
892                                 struct device_attribute *attr, char *buf)
893 {
894         ssize_t count = 0;
895 
896         mutex_lock(&clocksource_mutex);
897         count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
898         mutex_unlock(&clocksource_mutex);
899 
900         return count;
901 }
902 
903 ssize_t sysfs_get_uname(const char *buf, char *dst, size_t cnt)
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 
920 /**
921  * sysfs_override_clocksource - interface for manually overriding clocksource
922  * @dev:        unused
923  * @attr:       unused
924  * @buf:        name of override clocksource
925  * @count:      length of buffer
926  *
927  * Takes input from sysfs interface for manually overriding the default
928  * clocksource selection.
929  */
930 static ssize_t sysfs_override_clocksource(struct device *dev,
931                                           struct device_attribute *attr,
932                                           const char *buf, size_t count)
933 {
934         ssize_t ret;
935 
936         mutex_lock(&clocksource_mutex);
937 
938         ret = sysfs_get_uname(buf, override_name, count);
939         if (ret >= 0)
940                 clocksource_select();
941 
942         mutex_unlock(&clocksource_mutex);
943 
944         return ret;
945 }
946 
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];
962         ssize_t ret;
963 
964         ret = sysfs_get_uname(buf, name, count);
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 
981 /**
982  * sysfs_show_available_clocksources - sysfs interface for listing clocksource
983  * @dev:        unused
984  * @attr:       unused
985  * @buf:        char buffer to be filled with clocksource list
986  *
987  * Provides sysfs interface for listing registered clocksources
988  */
989 static ssize_t
990 sysfs_show_available_clocksources(struct device *dev,
991                                   struct device_attribute *attr,
992                                   char *buf)
993 {
994         struct clocksource *src;
995         ssize_t count = 0;
996 
997         mutex_lock(&clocksource_mutex);
998         list_for_each_entry(src, &clocksource_list, list) {
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))
1005                         count += snprintf(buf + count,
1006                                   max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
1007                                   "%s ", src->name);
1008         }
1009         mutex_unlock(&clocksource_mutex);
1010 
1011         count += snprintf(buf + count,
1012                           max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
1013 
1014         return count;
1015 }
1016 
1017 /*
1018  * Sysfs setup bits:
1019  */
1020 static DEVICE_ATTR(current_clocksource, 0644, sysfs_show_current_clocksources,
1021                    sysfs_override_clocksource);
1022 
1023 static DEVICE_ATTR(unbind_clocksource, 0200, NULL, sysfs_unbind_clocksource);
1024 
1025 static DEVICE_ATTR(available_clocksource, 0444,
1026                    sysfs_show_available_clocksources, NULL);
1027 
1028 static struct bus_type clocksource_subsys = {
1029         .name = "clocksource",
1030         .dev_name = "clocksource",
1031 };
1032 
1033 static struct device device_clocksource = {
1034         .id     = 0,
1035         .bus    = &clocksource_subsys,
1036 };
1037 
1038 static int __init init_clocksource_sysfs(void)
1039 {
1040         int error = subsys_system_register(&clocksource_subsys, NULL);
1041 
1042         if (!error)
1043                 error = device_register(&device_clocksource);
1044         if (!error)
1045                 error = device_create_file(
1046                                 &device_clocksource,
1047                                 &dev_attr_current_clocksource);
1048         if (!error)
1049                 error = device_create_file(&device_clocksource,
1050                                            &dev_attr_unbind_clocksource);
1051         if (!error)
1052                 error = device_create_file(
1053                                 &device_clocksource,
1054                                 &dev_attr_available_clocksource);
1055         return error;
1056 }
1057 
1058 device_initcall(init_clocksource_sysfs);
1059 #endif /* CONFIG_SYSFS */
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 {
1070         mutex_lock(&clocksource_mutex);
1071         if (str)
1072                 strlcpy(override_name, str, sizeof(override_name));
1073         mutex_unlock(&clocksource_mutex);
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 {
1088         if (!strcmp(str, "pmtmr")) {
1089                 pr_warn("clock=pmtmr is deprecated - use clocksource=acpi_pm\n");
1090                 return boot_override_clocksource("acpi_pm");
1091         }
1092         pr_warn("clock= boot option is deprecated - use clocksource=xyz\n");
1093         return boot_override_clocksource(str);
1094 }
1095 
1096 __setup("clock=", boot_override_clock);
1097 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp