en_clock.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2012 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/mlx4/device.h>
  34. #include "mlx4_en.h"
  35. /* mlx4_en_read_clock - read raw cycle counter (to be used by time counter)
  36. */
  37. static cycle_t mlx4_en_read_clock(const struct cyclecounter *tc)
  38. {
  39. struct mlx4_en_dev *mdev =
  40. container_of(tc, struct mlx4_en_dev, cycles);
  41. struct mlx4_dev *dev = mdev->dev;
  42. return mlx4_read_clock(dev) & tc->mask;
  43. }
  44. u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
  45. {
  46. u64 hi, lo;
  47. struct mlx4_ts_cqe *ts_cqe = (struct mlx4_ts_cqe *)cqe;
  48. lo = (u64)be16_to_cpu(ts_cqe->timestamp_lo);
  49. hi = ((u64)be32_to_cpu(ts_cqe->timestamp_hi) + !lo) << 16;
  50. return hi | lo;
  51. }
  52. void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
  53. struct skb_shared_hwtstamps *hwts,
  54. u64 timestamp)
  55. {
  56. unsigned long flags;
  57. u64 nsec;
  58. read_lock_irqsave(&mdev->clock_lock, flags);
  59. nsec = timecounter_cyc2time(&mdev->clock, timestamp);
  60. read_unlock_irqrestore(&mdev->clock_lock, flags);
  61. memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
  62. hwts->hwtstamp = ns_to_ktime(nsec);
  63. }
  64. /**
  65. * mlx4_en_remove_timestamp - disable PTP device
  66. * @mdev: board private structure
  67. *
  68. * Stop the PTP support.
  69. **/
  70. void mlx4_en_remove_timestamp(struct mlx4_en_dev *mdev)
  71. {
  72. if (mdev->ptp_clock) {
  73. ptp_clock_unregister(mdev->ptp_clock);
  74. mdev->ptp_clock = NULL;
  75. mlx4_info(mdev, "removed PHC\n");
  76. }
  77. }
  78. void mlx4_en_ptp_overflow_check(struct mlx4_en_dev *mdev)
  79. {
  80. bool timeout = time_is_before_jiffies(mdev->last_overflow_check +
  81. mdev->overflow_period);
  82. unsigned long flags;
  83. if (timeout) {
  84. write_lock_irqsave(&mdev->clock_lock, flags);
  85. timecounter_read(&mdev->clock);
  86. write_unlock_irqrestore(&mdev->clock_lock, flags);
  87. mdev->last_overflow_check = jiffies;
  88. }
  89. }
  90. /**
  91. * mlx4_en_phc_adjfreq - adjust the frequency of the hardware clock
  92. * @ptp: ptp clock structure
  93. * @delta: Desired frequency change in parts per billion
  94. *
  95. * Adjust the frequency of the PHC cycle counter by the indicated delta from
  96. * the base frequency.
  97. **/
  98. static int mlx4_en_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta)
  99. {
  100. u64 adj;
  101. u32 diff, mult;
  102. int neg_adj = 0;
  103. unsigned long flags;
  104. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  105. ptp_clock_info);
  106. if (delta < 0) {
  107. neg_adj = 1;
  108. delta = -delta;
  109. }
  110. mult = mdev->nominal_c_mult;
  111. adj = mult;
  112. adj *= delta;
  113. diff = div_u64(adj, 1000000000ULL);
  114. write_lock_irqsave(&mdev->clock_lock, flags);
  115. timecounter_read(&mdev->clock);
  116. mdev->cycles.mult = neg_adj ? mult - diff : mult + diff;
  117. write_unlock_irqrestore(&mdev->clock_lock, flags);
  118. return 0;
  119. }
  120. /**
  121. * mlx4_en_phc_adjtime - Shift the time of the hardware clock
  122. * @ptp: ptp clock structure
  123. * @delta: Desired change in nanoseconds
  124. *
  125. * Adjust the timer by resetting the timecounter structure.
  126. **/
  127. static int mlx4_en_phc_adjtime(struct ptp_clock_info *ptp, s64 delta)
  128. {
  129. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  130. ptp_clock_info);
  131. unsigned long flags;
  132. s64 now;
  133. write_lock_irqsave(&mdev->clock_lock, flags);
  134. now = timecounter_read(&mdev->clock);
  135. now += delta;
  136. timecounter_init(&mdev->clock, &mdev->cycles, now);
  137. write_unlock_irqrestore(&mdev->clock_lock, flags);
  138. return 0;
  139. }
  140. /**
  141. * mlx4_en_phc_gettime - Reads the current time from the hardware clock
  142. * @ptp: ptp clock structure
  143. * @ts: timespec structure to hold the current time value
  144. *
  145. * Read the timecounter and return the correct value in ns after converting
  146. * it into a struct timespec.
  147. **/
  148. static int mlx4_en_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts)
  149. {
  150. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  151. ptp_clock_info);
  152. unsigned long flags;
  153. u32 remainder;
  154. u64 ns;
  155. write_lock_irqsave(&mdev->clock_lock, flags);
  156. ns = timecounter_read(&mdev->clock);
  157. write_unlock_irqrestore(&mdev->clock_lock, flags);
  158. ts->tv_sec = div_u64_rem(ns, NSEC_PER_SEC, &remainder);
  159. ts->tv_nsec = remainder;
  160. return 0;
  161. }
  162. /**
  163. * mlx4_en_phc_settime - Set the current time on the hardware clock
  164. * @ptp: ptp clock structure
  165. * @ts: timespec containing the new time for the cycle counter
  166. *
  167. * Reset the timecounter to use a new base value instead of the kernel
  168. * wall timer value.
  169. **/
  170. static int mlx4_en_phc_settime(struct ptp_clock_info *ptp,
  171. const struct timespec *ts)
  172. {
  173. struct mlx4_en_dev *mdev = container_of(ptp, struct mlx4_en_dev,
  174. ptp_clock_info);
  175. u64 ns = timespec_to_ns(ts);
  176. unsigned long flags;
  177. /* reset the timecounter */
  178. write_lock_irqsave(&mdev->clock_lock, flags);
  179. timecounter_init(&mdev->clock, &mdev->cycles, ns);
  180. write_unlock_irqrestore(&mdev->clock_lock, flags);
  181. return 0;
  182. }
  183. /**
  184. * mlx4_en_phc_enable - enable or disable an ancillary feature
  185. * @ptp: ptp clock structure
  186. * @request: Desired resource to enable or disable
  187. * @on: Caller passes one to enable or zero to disable
  188. *
  189. * Enable (or disable) ancillary features of the PHC subsystem.
  190. * Currently, no ancillary features are supported.
  191. **/
  192. static int mlx4_en_phc_enable(struct ptp_clock_info __always_unused *ptp,
  193. struct ptp_clock_request __always_unused *request,
  194. int __always_unused on)
  195. {
  196. return -EOPNOTSUPP;
  197. }
  198. static const struct ptp_clock_info mlx4_en_ptp_clock_info = {
  199. .owner = THIS_MODULE,
  200. .max_adj = 100000000,
  201. .n_alarm = 0,
  202. .n_ext_ts = 0,
  203. .n_per_out = 0,
  204. .n_pins = 0,
  205. .pps = 0,
  206. .adjfreq = mlx4_en_phc_adjfreq,
  207. .adjtime = mlx4_en_phc_adjtime,
  208. .gettime = mlx4_en_phc_gettime,
  209. .settime = mlx4_en_phc_settime,
  210. .enable = mlx4_en_phc_enable,
  211. };
  212. void mlx4_en_init_timestamp(struct mlx4_en_dev *mdev)
  213. {
  214. struct mlx4_dev *dev = mdev->dev;
  215. unsigned long flags;
  216. u64 ns;
  217. rwlock_init(&mdev->clock_lock);
  218. memset(&mdev->cycles, 0, sizeof(mdev->cycles));
  219. mdev->cycles.read = mlx4_en_read_clock;
  220. mdev->cycles.mask = CLOCKSOURCE_MASK(48);
  221. /* Using shift to make calculation more accurate. Since current HW
  222. * clock frequency is 427 MHz, and cycles are given using a 48 bits
  223. * register, the biggest shift when calculating using u64, is 14
  224. * (max_cycles * multiplier < 2^64)
  225. */
  226. mdev->cycles.shift = 14;
  227. mdev->cycles.mult =
  228. clocksource_khz2mult(1000 * dev->caps.hca_core_clock, mdev->cycles.shift);
  229. mdev->nominal_c_mult = mdev->cycles.mult;
  230. write_lock_irqsave(&mdev->clock_lock, flags);
  231. timecounter_init(&mdev->clock, &mdev->cycles,
  232. ktime_to_ns(ktime_get_real()));
  233. write_unlock_irqrestore(&mdev->clock_lock, flags);
  234. /* Calculate period in seconds to call the overflow watchdog - to make
  235. * sure counter is checked at least once every wrap around.
  236. */
  237. ns = cyclecounter_cyc2ns(&mdev->cycles, mdev->cycles.mask);
  238. do_div(ns, NSEC_PER_SEC / 2 / HZ);
  239. mdev->overflow_period = ns;
  240. /* Configure the PHC */
  241. mdev->ptp_clock_info = mlx4_en_ptp_clock_info;
  242. snprintf(mdev->ptp_clock_info.name, 16, "mlx4 ptp");
  243. mdev->ptp_clock = ptp_clock_register(&mdev->ptp_clock_info,
  244. &mdev->pdev->dev);
  245. if (IS_ERR(mdev->ptp_clock)) {
  246. mdev->ptp_clock = NULL;
  247. mlx4_err(mdev, "ptp_clock_register failed\n");
  248. } else {
  249. mlx4_info(mdev, "registered PHC clock\n");
  250. }
  251. }