gettimeofday.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2015 Imagination Technologies
  3. * Author: Alex Smith <alex.smith@imgtec.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include "vdso.h"
  11. #include <linux/compiler.h>
  12. #include <linux/time.h>
  13. #include <asm/clocksource.h>
  14. #include <asm/io.h>
  15. #include <asm/unistd.h>
  16. #include <asm/vdso.h>
  17. #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
  18. static __always_inline long gettimeofday_fallback(struct timeval *_tv,
  19. struct timezone *_tz)
  20. {
  21. register struct timezone *tz asm("a1") = _tz;
  22. register struct timeval *tv asm("a0") = _tv;
  23. register long ret asm("v0");
  24. register long nr asm("v0") = __NR_gettimeofday;
  25. register long error asm("a3");
  26. asm volatile(
  27. " syscall\n"
  28. : "=r" (ret), "=r" (error)
  29. : "r" (tv), "r" (tz), "r" (nr)
  30. : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
  31. "$14", "$15", "$24", "$25", "hi", "lo", "memory");
  32. return error ? -ret : ret;
  33. }
  34. #endif
  35. static __always_inline long clock_gettime_fallback(clockid_t _clkid,
  36. struct timespec *_ts)
  37. {
  38. register struct timespec *ts asm("a1") = _ts;
  39. register clockid_t clkid asm("a0") = _clkid;
  40. register long ret asm("v0");
  41. register long nr asm("v0") = __NR_clock_gettime;
  42. register long error asm("a3");
  43. asm volatile(
  44. " syscall\n"
  45. : "=r" (ret), "=r" (error)
  46. : "r" (clkid), "r" (ts), "r" (nr)
  47. : "$1", "$3", "$8", "$9", "$10", "$11", "$12", "$13",
  48. "$14", "$15", "$24", "$25", "hi", "lo", "memory");
  49. return error ? -ret : ret;
  50. }
  51. static __always_inline int do_realtime_coarse(struct timespec *ts,
  52. const union mips_vdso_data *data)
  53. {
  54. u32 start_seq;
  55. do {
  56. start_seq = vdso_data_read_begin(data);
  57. ts->tv_sec = data->xtime_sec;
  58. ts->tv_nsec = data->xtime_nsec >> data->cs_shift;
  59. } while (vdso_data_read_retry(data, start_seq));
  60. return 0;
  61. }
  62. static __always_inline int do_monotonic_coarse(struct timespec *ts,
  63. const union mips_vdso_data *data)
  64. {
  65. u32 start_seq;
  66. u64 to_mono_sec;
  67. u64 to_mono_nsec;
  68. do {
  69. start_seq = vdso_data_read_begin(data);
  70. ts->tv_sec = data->xtime_sec;
  71. ts->tv_nsec = data->xtime_nsec >> data->cs_shift;
  72. to_mono_sec = data->wall_to_mono_sec;
  73. to_mono_nsec = data->wall_to_mono_nsec;
  74. } while (vdso_data_read_retry(data, start_seq));
  75. ts->tv_sec += to_mono_sec;
  76. timespec_add_ns(ts, to_mono_nsec);
  77. return 0;
  78. }
  79. #ifdef CONFIG_CSRC_R4K
  80. static __always_inline u64 read_r4k_count(void)
  81. {
  82. unsigned int count;
  83. __asm__ __volatile__(
  84. " .set push\n"
  85. " .set mips32r2\n"
  86. " rdhwr %0, $2\n"
  87. " .set pop\n"
  88. : "=r" (count));
  89. return count;
  90. }
  91. #endif
  92. #ifdef CONFIG_CLKSRC_MIPS_GIC
  93. static __always_inline u64 read_gic_count(const union mips_vdso_data *data)
  94. {
  95. void __iomem *gic = get_gic(data);
  96. u32 hi, hi2, lo;
  97. do {
  98. hi = __raw_readl(gic + sizeof(lo));
  99. lo = __raw_readl(gic);
  100. hi2 = __raw_readl(gic + sizeof(lo));
  101. } while (hi2 != hi);
  102. return (((u64)hi) << 32) + lo;
  103. }
  104. #endif
  105. static __always_inline u64 get_ns(const union mips_vdso_data *data)
  106. {
  107. u64 cycle_now, delta, nsec;
  108. switch (data->clock_mode) {
  109. #ifdef CONFIG_CSRC_R4K
  110. case VDSO_CLOCK_R4K:
  111. cycle_now = read_r4k_count();
  112. break;
  113. #endif
  114. #ifdef CONFIG_CLKSRC_MIPS_GIC
  115. case VDSO_CLOCK_GIC:
  116. cycle_now = read_gic_count(data);
  117. break;
  118. #endif
  119. default:
  120. return 0;
  121. }
  122. delta = (cycle_now - data->cs_cycle_last) & data->cs_mask;
  123. nsec = (delta * data->cs_mult) + data->xtime_nsec;
  124. nsec >>= data->cs_shift;
  125. return nsec;
  126. }
  127. static __always_inline int do_realtime(struct timespec *ts,
  128. const union mips_vdso_data *data)
  129. {
  130. u32 start_seq;
  131. u64 ns;
  132. do {
  133. start_seq = vdso_data_read_begin(data);
  134. if (data->clock_mode == VDSO_CLOCK_NONE)
  135. return -ENOSYS;
  136. ts->tv_sec = data->xtime_sec;
  137. ns = get_ns(data);
  138. } while (vdso_data_read_retry(data, start_seq));
  139. ts->tv_nsec = 0;
  140. timespec_add_ns(ts, ns);
  141. return 0;
  142. }
  143. static __always_inline int do_monotonic(struct timespec *ts,
  144. const union mips_vdso_data *data)
  145. {
  146. u32 start_seq;
  147. u64 ns;
  148. u64 to_mono_sec;
  149. u64 to_mono_nsec;
  150. do {
  151. start_seq = vdso_data_read_begin(data);
  152. if (data->clock_mode == VDSO_CLOCK_NONE)
  153. return -ENOSYS;
  154. ts->tv_sec = data->xtime_sec;
  155. ns = get_ns(data);
  156. to_mono_sec = data->wall_to_mono_sec;
  157. to_mono_nsec = data->wall_to_mono_nsec;
  158. } while (vdso_data_read_retry(data, start_seq));
  159. ts->tv_sec += to_mono_sec;
  160. ts->tv_nsec = 0;
  161. timespec_add_ns(ts, ns + to_mono_nsec);
  162. return 0;
  163. }
  164. #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
  165. /*
  166. * This is behind the ifdef so that we don't provide the symbol when there's no
  167. * possibility of there being a usable clocksource, because there's nothing we
  168. * can do without it. When libc fails the symbol lookup it should fall back on
  169. * the standard syscall path.
  170. */
  171. int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  172. {
  173. const union mips_vdso_data *data = get_vdso_data();
  174. struct timespec ts;
  175. int ret;
  176. ret = do_realtime(&ts, data);
  177. if (ret)
  178. return gettimeofday_fallback(tv, tz);
  179. if (tv) {
  180. tv->tv_sec = ts.tv_sec;
  181. tv->tv_usec = ts.tv_nsec / 1000;
  182. }
  183. if (tz) {
  184. tz->tz_minuteswest = data->tz_minuteswest;
  185. tz->tz_dsttime = data->tz_dsttime;
  186. }
  187. return 0;
  188. }
  189. #endif /* CONFIG_MIPS_CLOCK_VSYSCALL */
  190. int __vdso_clock_gettime(clockid_t clkid, struct timespec *ts)
  191. {
  192. const union mips_vdso_data *data = get_vdso_data();
  193. int ret = -1;
  194. switch (clkid) {
  195. case CLOCK_REALTIME_COARSE:
  196. ret = do_realtime_coarse(ts, data);
  197. break;
  198. case CLOCK_MONOTONIC_COARSE:
  199. ret = do_monotonic_coarse(ts, data);
  200. break;
  201. case CLOCK_REALTIME:
  202. ret = do_realtime(ts, data);
  203. break;
  204. case CLOCK_MONOTONIC:
  205. ret = do_monotonic(ts, data);
  206. break;
  207. default:
  208. break;
  209. }
  210. if (ret)
  211. ret = clock_gettime_fallback(clkid, ts);
  212. return ret;
  213. }