gettimeofday.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/irqchip/mips-gic.h>
  13. #include <linux/time.h>
  14. #include <asm/clocksource.h>
  15. #include <asm/io.h>
  16. #include <asm/mips-cm.h>
  17. #include <asm/unistd.h>
  18. #include <asm/vdso.h>
  19. static __always_inline int do_realtime_coarse(struct timespec *ts,
  20. const union mips_vdso_data *data)
  21. {
  22. u32 start_seq;
  23. do {
  24. start_seq = vdso_data_read_begin(data);
  25. ts->tv_sec = data->xtime_sec;
  26. ts->tv_nsec = data->xtime_nsec >> data->cs_shift;
  27. } while (vdso_data_read_retry(data, start_seq));
  28. return 0;
  29. }
  30. static __always_inline int do_monotonic_coarse(struct timespec *ts,
  31. const union mips_vdso_data *data)
  32. {
  33. u32 start_seq;
  34. u32 to_mono_sec;
  35. u32 to_mono_nsec;
  36. do {
  37. start_seq = vdso_data_read_begin(data);
  38. ts->tv_sec = data->xtime_sec;
  39. ts->tv_nsec = data->xtime_nsec >> data->cs_shift;
  40. to_mono_sec = data->wall_to_mono_sec;
  41. to_mono_nsec = data->wall_to_mono_nsec;
  42. } while (vdso_data_read_retry(data, start_seq));
  43. ts->tv_sec += to_mono_sec;
  44. timespec_add_ns(ts, to_mono_nsec);
  45. return 0;
  46. }
  47. #ifdef CONFIG_CSRC_R4K
  48. static __always_inline u64 read_r4k_count(void)
  49. {
  50. unsigned int count;
  51. __asm__ __volatile__(
  52. " .set push\n"
  53. " .set mips32r2\n"
  54. " rdhwr %0, $2\n"
  55. " .set pop\n"
  56. : "=r" (count));
  57. return count;
  58. }
  59. #endif
  60. #ifdef CONFIG_CLKSRC_MIPS_GIC
  61. static __always_inline u64 read_gic_count(const union mips_vdso_data *data)
  62. {
  63. void __iomem *gic = get_gic(data);
  64. u32 hi, hi2, lo;
  65. do {
  66. hi = __raw_readl(gic + GIC_UMV_SH_COUNTER_63_32_OFS);
  67. lo = __raw_readl(gic + GIC_UMV_SH_COUNTER_31_00_OFS);
  68. hi2 = __raw_readl(gic + GIC_UMV_SH_COUNTER_63_32_OFS);
  69. } while (hi2 != hi);
  70. return (((u64)hi) << 32) + lo;
  71. }
  72. #endif
  73. static __always_inline u64 get_ns(const union mips_vdso_data *data)
  74. {
  75. u64 cycle_now, delta, nsec;
  76. switch (data->clock_mode) {
  77. #ifdef CONFIG_CSRC_R4K
  78. case VDSO_CLOCK_R4K:
  79. cycle_now = read_r4k_count();
  80. break;
  81. #endif
  82. #ifdef CONFIG_CLKSRC_MIPS_GIC
  83. case VDSO_CLOCK_GIC:
  84. cycle_now = read_gic_count(data);
  85. break;
  86. #endif
  87. default:
  88. return 0;
  89. }
  90. delta = (cycle_now - data->cs_cycle_last) & data->cs_mask;
  91. nsec = (delta * data->cs_mult) + data->xtime_nsec;
  92. nsec >>= data->cs_shift;
  93. return nsec;
  94. }
  95. static __always_inline int do_realtime(struct timespec *ts,
  96. const union mips_vdso_data *data)
  97. {
  98. u32 start_seq;
  99. u64 ns;
  100. do {
  101. start_seq = vdso_data_read_begin(data);
  102. if (data->clock_mode == VDSO_CLOCK_NONE)
  103. return -ENOSYS;
  104. ts->tv_sec = data->xtime_sec;
  105. ns = get_ns(data);
  106. } while (vdso_data_read_retry(data, start_seq));
  107. ts->tv_nsec = 0;
  108. timespec_add_ns(ts, ns);
  109. return 0;
  110. }
  111. static __always_inline int do_monotonic(struct timespec *ts,
  112. const union mips_vdso_data *data)
  113. {
  114. u32 start_seq;
  115. u64 ns;
  116. u32 to_mono_sec;
  117. u32 to_mono_nsec;
  118. do {
  119. start_seq = vdso_data_read_begin(data);
  120. if (data->clock_mode == VDSO_CLOCK_NONE)
  121. return -ENOSYS;
  122. ts->tv_sec = data->xtime_sec;
  123. ns = get_ns(data);
  124. to_mono_sec = data->wall_to_mono_sec;
  125. to_mono_nsec = data->wall_to_mono_nsec;
  126. } while (vdso_data_read_retry(data, start_seq));
  127. ts->tv_sec += to_mono_sec;
  128. ts->tv_nsec = 0;
  129. timespec_add_ns(ts, ns + to_mono_nsec);
  130. return 0;
  131. }
  132. #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
  133. /*
  134. * This is behind the ifdef so that we don't provide the symbol when there's no
  135. * possibility of there being a usable clocksource, because there's nothing we
  136. * can do without it. When libc fails the symbol lookup it should fall back on
  137. * the standard syscall path.
  138. */
  139. int __vdso_gettimeofday(struct timeval *tv, struct timezone *tz)
  140. {
  141. const union mips_vdso_data *data = get_vdso_data();
  142. struct timespec ts;
  143. int ret;
  144. ret = do_realtime(&ts, data);
  145. if (ret)
  146. return ret;
  147. if (tv) {
  148. tv->tv_sec = ts.tv_sec;
  149. tv->tv_usec = ts.tv_nsec / 1000;
  150. }
  151. if (tz) {
  152. tz->tz_minuteswest = data->tz_minuteswest;
  153. tz->tz_dsttime = data->tz_dsttime;
  154. }
  155. return 0;
  156. }
  157. #endif /* CONFIG_CLKSRC_MIPS_GIC */
  158. int __vdso_clock_gettime(clockid_t clkid, struct timespec *ts)
  159. {
  160. const union mips_vdso_data *data = get_vdso_data();
  161. int ret;
  162. switch (clkid) {
  163. case CLOCK_REALTIME_COARSE:
  164. ret = do_realtime_coarse(ts, data);
  165. break;
  166. case CLOCK_MONOTONIC_COARSE:
  167. ret = do_monotonic_coarse(ts, data);
  168. break;
  169. case CLOCK_REALTIME:
  170. ret = do_realtime(ts, data);
  171. break;
  172. case CLOCK_MONOTONIC:
  173. ret = do_monotonic(ts, data);
  174. break;
  175. default:
  176. ret = -ENOSYS;
  177. break;
  178. }
  179. /* If we return -ENOSYS libc should fall back to a syscall. */
  180. return ret;
  181. }