]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - kernel/time/clocksource.c
clocksource: fix compilation if no GENERIC_TIME
[karo-tx-linux.git] / kernel / time / clocksource.c
index 09113347d3281c8de133e4223dec7afa806e41e5..ecc7adbf0918626982201f0710b0abbccc452abf 100644 (file)
@@ -394,15 +394,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);
 }
 
 /**
@@ -417,6 +413,47 @@ void clocksource_touch_watchdog(void)
        clocksource_resume_watchdog();
 }
 
+/**
+ * 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;
+
+       /*
+        * 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
 
 /**
@@ -515,6 +552,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();