Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: John Stultz <john.stultz@linaro.org>
···8383 return ktime_set(ts.tv_sec, ts.tv_nsec);8484}85858686+/* convert a timespec64 to ktime_t format: */8787+static inline ktime_t timespec64_to_ktime(struct timespec64 ts)8888+{8989+ return ktime_set(ts.tv_sec, ts.tv_nsec);9090+}9191+8692/* convert a timeval to ktime_t format: */8793static inline ktime_t timeval_to_ktime(struct timeval tv)8894{···97919892/* Map the ktime_t to timespec conversion to ns_to_timespec function */9993#define ktime_to_timespec(kt) ns_to_timespec((kt).tv64)9494+9595+/* Map the ktime_t to timespec conversion to ns_to_timespec function */9696+#define ktime_to_timespec64(kt) ns_to_timespec64((kt).tv64)1009710198/* Map the ktime_t to timeval conversion to ns_to_timeval function */10299#define ktime_to_timeval(kt) ns_to_timeval((kt).tv64)···216207{217208 if (kt.tv64) {218209 *ts = ktime_to_timespec(kt);210210+ return true;211211+ } else {212212+ return false;213213+ }214214+}215215+216216+/**217217+ * ktime_to_timespec64_cond - convert a ktime_t variable to timespec64218218+ * format only if the variable contains data219219+ * @kt: the ktime_t variable to convert220220+ * @ts: the timespec variable to store the result in221221+ *222222+ * Return: %true if there was a successful conversion, %false if kt was 0.223223+ */224224+static inline __must_check bool ktime_to_timespec64_cond(const ktime_t kt,225225+ struct timespec64 *ts)226226+{227227+ if (kt.tv64) {228228+ *ts = ktime_to_timespec64(kt);219229 return true;220230 } else {221231 return false;
···420420}421421EXPORT_SYMBOL(ns_to_timeval);422422423423+#if BITS_PER_LONG == 32424424+/**425425+ * set_normalized_timespec - set timespec sec and nsec parts and normalize426426+ *427427+ * @ts: pointer to timespec variable to be set428428+ * @sec: seconds to set429429+ * @nsec: nanoseconds to set430430+ *431431+ * Set seconds and nanoseconds field of a timespec variable and432432+ * normalize to the timespec storage format433433+ *434434+ * Note: The tv_nsec part is always in the range of435435+ * 0 <= tv_nsec < NSEC_PER_SEC436436+ * For negative values only the tv_sec field is negative !437437+ */438438+void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec)439439+{440440+ while (nsec >= NSEC_PER_SEC) {441441+ /*442442+ * The following asm() prevents the compiler from443443+ * optimising this loop into a modulo operation. See444444+ * also __iter_div_u64_rem() in include/linux/time.h445445+ */446446+ asm("" : "+rm"(nsec));447447+ nsec -= NSEC_PER_SEC;448448+ ++sec;449449+ }450450+ while (nsec < 0) {451451+ asm("" : "+rm"(nsec));452452+ nsec += NSEC_PER_SEC;453453+ --sec;454454+ }455455+ ts->tv_sec = sec;456456+ ts->tv_nsec = nsec;457457+}458458+EXPORT_SYMBOL(set_normalized_timespec64);459459+460460+/**461461+ * ns_to_timespec64 - Convert nanoseconds to timespec64462462+ * @nsec: the nanoseconds value to be converted463463+ *464464+ * Returns the timespec64 representation of the nsec parameter.465465+ */466466+struct timespec64 ns_to_timespec64(const s64 nsec)467467+{468468+ struct timespec64 ts;469469+ s32 rem;470470+471471+ if (!nsec)472472+ return (struct timespec64) {0, 0};473473+474474+ ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem);475475+ if (unlikely(rem < 0)) {476476+ ts.tv_sec--;477477+ rem += NSEC_PER_SEC;478478+ }479479+ ts.tv_nsec = rem;480480+481481+ return ts;482482+}483483+EXPORT_SYMBOL(ns_to_timespec64);484484+#endif423485/*424486 * When we convert to jiffies then we interpret incoming values425487 * the following way: