timekeeping: Provide ktime_get_raw()
authorThomas Gleixner <tglx@linutronix.de>
Wed, 16 Jul 2014 21:05:04 +0000 (21:05 +0000)
committerJohn Stultz <john.stultz@linaro.org>
Wed, 23 Jul 2014 22:01:49 +0000 (15:01 -0700)
Provide a ktime_t based interface for raw monotonic time.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Stultz <john.stultz@linaro.org>
include/linux/timekeeper_internal.h
include/linux/timekeeping.h
kernel/time/timekeeping.c

index 8e5d77a01787676759d8badf9ff2b8624d07a513..2e20275a7083f26c1427d0700d880d71accc26ed 100644 (file)
@@ -54,6 +54,9 @@ struct timekeeper {
        /* The current UTC to TAI offset in seconds */
        s32                     tai_offset;
 
+       /* Monotonic raw base time */
+       ktime_t                 base_raw;
+
        /* The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. */
        struct timespec64       raw_time;
 
index f0f12a84a31b02e7f74dc35092917d4b4c767ec8..58ad7eff83ff2082bd1738bc4965147a00527b69 100644 (file)
@@ -107,6 +107,7 @@ enum tk_offsets {
 extern ktime_t ktime_get(void);
 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
+extern ktime_t ktime_get_raw(void);
 
 /**
  * ktime_get_real - get the real (wall-) time in ktime_t format
@@ -158,6 +159,11 @@ static inline u64 ktime_get_boot_ns(void)
        return ktime_to_ns(ktime_get_boottime());
 }
 
+static inline u64 ktime_get_raw_ns(void)
+{
+       return ktime_to_ns(ktime_get_raw());
+}
+
 /*
  * Timespec interfaces utilizing the ktime based ones
  */
index 118e91e3071c29e3e427cfd0ef410e9320f39134..af8051f4420d1d733c5dba182b4d9ce60bcd75bf 100644 (file)
@@ -305,6 +305,9 @@ static inline void tk_update_ktime_data(struct timekeeper *tk)
        nsec *= NSEC_PER_SEC;
        nsec += tk->wall_to_monotonic.tv_nsec;
        tk->base_mono = ns_to_ktime(nsec);
+
+       /* Update the monotonic raw base */
+       tk->base_raw = timespec64_to_ktime(tk->raw_time);
 }
 
 /* must hold timekeeper_lock */
@@ -466,6 +469,27 @@ ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs)
 }
 EXPORT_SYMBOL_GPL(ktime_mono_to_any);
 
+/**
+ * ktime_get_raw - Returns the raw monotonic time in ktime_t format
+ */
+ktime_t ktime_get_raw(void)
+{
+       struct timekeeper *tk = &tk_core.timekeeper;
+       unsigned int seq;
+       ktime_t base;
+       s64 nsecs;
+
+       do {
+               seq = read_seqcount_begin(&tk_core.seq);
+               base = tk->base_raw;
+               nsecs = timekeeping_get_ns_raw(tk);
+
+       } while (read_seqcount_retry(&tk_core.seq, seq));
+
+       return ktime_add_ns(base, nsecs);
+}
+EXPORT_SYMBOL_GPL(ktime_get_raw);
+
 /**
  * ktime_get_ts64 - get the monotonic clock in timespec64 format
  * @ts:                pointer to timespec variable
@@ -878,6 +902,7 @@ void __init timekeeping_init(void)
        tk_set_xtime(tk, &now);
        tk->raw_time.tv_sec = 0;
        tk->raw_time.tv_nsec = 0;
+       tk->base_raw.tv64 = 0;
        if (boot.tv_sec == 0 && boot.tv_nsec == 0)
                boot = tk_xtime(tk);