ntp: Cleanup xtime references in ntp.c
[safe/jmp/linux-2.6] / kernel / time / clocksource.c
index 5697155..e85c234 100644 (file)
@@ -39,7 +39,7 @@ void timecounter_init(struct timecounter *tc,
        tc->cycle_last = cc->read(cc);
        tc->nsec = start_tstamp;
 }
-EXPORT_SYMBOL(timecounter_init);
+EXPORT_SYMBOL_GPL(timecounter_init);
 
 /**
  * timecounter_read_delta - get nanoseconds since last call of this function
@@ -83,7 +83,7 @@ u64 timecounter_read(struct timecounter *tc)
 
        return nsec;
 }
-EXPORT_SYMBOL(timecounter_read);
+EXPORT_SYMBOL_GPL(timecounter_read);
 
 u64 timecounter_cyc2time(struct timecounter *tc,
                         cycle_t cycle_tstamp)
@@ -105,7 +105,60 @@ u64 timecounter_cyc2time(struct timecounter *tc,
 
        return nsec;
 }
-EXPORT_SYMBOL(timecounter_cyc2time);
+EXPORT_SYMBOL_GPL(timecounter_cyc2time);
+
+/**
+ * clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocks
+ * @mult:      pointer to mult variable
+ * @shift:     pointer to shift variable
+ * @from:      frequency to convert from
+ * @to:                frequency to convert to
+ * @minsec:    guaranteed runtime conversion range in seconds
+ *
+ * The function evaluates the shift/mult pair for the scaled math
+ * operations of clocksources and clockevents.
+ *
+ * @to and @from are frequency values in HZ. For clock sources @to is
+ * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
+ * event @to is the counter frequency and @from is NSEC_PER_SEC.
+ *
+ * The @minsec conversion range argument controls the time frame in
+ * seconds which must be covered by the runtime conversion with the
+ * calculated mult and shift factors. This guarantees that no 64bit
+ * overflow happens when the input value of the conversion is
+ * multiplied with the calculated mult factor. Larger ranges may
+ * reduce the conversion accuracy by chosing smaller mult and shift
+ * factors.
+ */
+void
+clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec)
+{
+       u64 tmp;
+       u32 sft, sftacc= 32;
+
+       /*
+        * Calculate the shift factor which is limiting the conversion
+        * range:
+        */
+       tmp = ((u64)minsec * from) >> 32;
+       while (tmp) {
+               tmp >>=1;
+               sftacc--;
+       }
+
+       /*
+        * Find the conversion shift/mult pair which has the best
+        * accuracy and fits the maxsec conversion range:
+        */
+       for (sft = 32; sft > 0; sft--) {
+               tmp = (u64) to << sft;
+               do_div(tmp, from);
+               if ((tmp >> sftacc) == 0)
+                       break;
+       }
+       *mult = tmp;
+       *shift = sft;
+}
 
 /*[Clocksource internal variables]---------
  * curr_clocksource:
@@ -121,6 +174,7 @@ static struct clocksource *curr_clocksource;
 static LIST_HEAD(clocksource_list);
 static DEFINE_MUTEX(clocksource_mutex);
 static char override_name[32];
+static int finished_booting;
 
 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
 static void clocksource_watchdog_work(struct work_struct *work);
@@ -155,7 +209,8 @@ static void __clocksource_unstable(struct clocksource *cs)
 {
        cs->flags &= ~(CLOCK_SOURCE_VALID_FOR_HRES | CLOCK_SOURCE_WATCHDOG);
        cs->flags |= CLOCK_SOURCE_UNSTABLE;
-       schedule_work(&watchdog_work);
+       if (finished_booting)
+               schedule_work(&watchdog_work);
 }
 
 static void clocksource_unstable(struct clocksource *cs, int64_t delta)
@@ -207,7 +262,8 @@ static void clocksource_watchdog(unsigned long data)
 
                /* Clocksource already marked unstable? */
                if (cs->flags & CLOCK_SOURCE_UNSTABLE) {
-                       schedule_work(&watchdog_work);
+                       if (finished_booting)
+                               schedule_work(&watchdog_work);
                        continue;
                }
 
@@ -380,6 +436,7 @@ static void clocksource_enqueue_watchdog(struct clocksource *cs)
 
 static inline void clocksource_dequeue_watchdog(struct clocksource *cs) { }
 static inline void clocksource_resume_watchdog(void) { }
+static inline int clocksource_watchdog_kthread(void *data) { return 0; }
 
 #endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
 
@@ -390,15 +447,11 @@ void clocksource_resume(void)
 {
        struct clocksource *cs;
 
-       mutex_lock(&clocksource_mutex);
-
        list_for_each_entry(cs, &clocksource_list, list)
                if (cs->resume)
                        cs->resume();
 
        clocksource_resume_watchdog();
-
-       mutex_unlock(&clocksource_mutex);
 }
 
 /**
@@ -413,9 +466,48 @@ void clocksource_touch_watchdog(void)
        clocksource_resume_watchdog();
 }
 
-#ifdef CONFIG_GENERIC_TIME
+/**
+ * clocksource_max_deferment - Returns max time the clocksource can be deferred
+ * @cs:         Pointer to clocksource
+ *
+ */
+static u64 clocksource_max_deferment(struct clocksource *cs)
+{
+       u64 max_nsecs, max_cycles;
 
-static int finished_booting;
+       /*
+        * Calculate the maximum number of cycles that we can pass to the
+        * cyc2ns function without overflowing a 64-bit signed result. The
+        * maximum number of cycles is equal to ULLONG_MAX/cs->mult which
+        * is equivalent to the below.
+        * max_cycles < (2^63)/cs->mult
+        * max_cycles < 2^(log2((2^63)/cs->mult))
+        * max_cycles < 2^(log2(2^63) - log2(cs->mult))
+        * max_cycles < 2^(63 - log2(cs->mult))
+        * max_cycles < 1 << (63 - log2(cs->mult))
+        * Please note that we add 1 to the result of the log2 to account for
+        * any rounding errors, ensure the above inequality is satisfied and
+        * no overflow will occur.
+        */
+       max_cycles = 1ULL << (63 - (ilog2(cs->mult) + 1));
+
+       /*
+        * The actual maximum number of cycles we can defer the clocksource is
+        * determined by the minimum of max_cycles and cs->mask.
+        */
+       max_cycles = min_t(u64, max_cycles, (u64) cs->mask);
+       max_nsecs = clocksource_cyc2ns(max_cycles, cs->mult, cs->shift);
+
+       /*
+        * To ensure that the clocksource does not wrap whilst we are idle,
+        * limit the time the clocksource can be deferred by 12.5%. Please
+        * note a margin of 12.5% is used because this can be computed with
+        * a shift, versus say 10% which would require division.
+        */
+       return max_nsecs - (max_nsecs >> 5);
+}
+
+#ifdef CONFIG_GENERIC_TIME
 
 /**
  * clocksource_select - Select the best clocksource available
@@ -461,6 +553,12 @@ static void clocksource_select(void)
        }
 }
 
+#else /* CONFIG_GENERIC_TIME */
+
+static inline void clocksource_select(void) { }
+
+#endif
+
 /*
  * clocksource_done_booting - Called near the end of core bootup
  *
@@ -471,17 +569,19 @@ static void clocksource_select(void)
 static int __init clocksource_done_booting(void)
 {
        finished_booting = 1;
+
+       /*
+        * Run the watchdog first to eliminate unstable clock sources
+        */
+       clocksource_watchdog_kthread(NULL);
+
+       mutex_lock(&clocksource_mutex);
        clocksource_select();
+       mutex_unlock(&clocksource_mutex);
        return 0;
 }
 fs_initcall(clocksource_done_booting);
 
-#else /* CONFIG_GENERIC_TIME */
-
-static inline void clocksource_select(void) { }
-
-#endif
-
 /*
  * Enqueue the clocksource sorted by rating
  */
@@ -505,6 +605,9 @@ static void clocksource_enqueue(struct clocksource *cs)
  */
 int clocksource_register(struct clocksource *cs)
 {
+       /* calculate max idle time permitted for this clocksource */
+       cs->max_idle_ns = clocksource_max_deferment(cs);
+
        mutex_lock(&clocksource_mutex);
        clocksource_enqueue(cs);
        clocksource_select();
@@ -574,7 +677,7 @@ sysfs_show_current_clocksources(struct sys_device *dev,
  * @count:     length of buffer
  *
  * Takes input from sysfs interface for manually overriding the default
- * clocksource selction.
+ * clocksource selection.
  */
 static ssize_t sysfs_override_clocksource(struct sys_device *dev,
                                          struct sysdev_attribute *attr,