fbnic_time.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) Meta Platforms, Inc. and affiliates. */
  3. #include <linux/bitfield.h>
  4. #include <linux/jiffies.h>
  5. #include <linux/limits.h>
  6. #include <linux/ptp_clock_kernel.h>
  7. #include <linux/timer.h>
  8. #include "fbnic.h"
  9. #include "fbnic_csr.h"
  10. #include "fbnic_netdev.h"
  11. /* FBNIC timing & PTP implementation
  12. * Datapath uses truncated 40b timestamps for scheduling and event reporting.
  13. * We need to promote those to full 64b, hence we periodically cache the top
  14. * 32bit of the HW time counter. Since this makes our time reporting non-atomic
  15. * we leave the HW clock free running and adjust time offsets in SW as needed.
  16. * Time offset is 64bit - we need a seq counter for 32bit machines.
  17. * Time offset and the cache of top bits are independent so we don't need
  18. * a coherent snapshot of both - READ_ONCE()/WRITE_ONCE() + writer side lock
  19. * are enough.
  20. */
  21. /* Period of refresh of top bits of timestamp, give ourselves a 8x margin.
  22. * This should translate to once a minute.
  23. * The use of nsecs_to_jiffies() should be safe for a <=40b nsec value.
  24. */
  25. #define FBNIC_TS_HIGH_REFRESH_JIF nsecs_to_jiffies((1ULL << 40) / 16)
  26. static struct fbnic_dev *fbnic_from_ptp_info(struct ptp_clock_info *ptp)
  27. {
  28. return container_of(ptp, struct fbnic_dev, ptp_info);
  29. }
  30. /* This function is "slow" because we could try guessing which high part
  31. * is correct based on low instead of re-reading, and skip reading @hi
  32. * twice altogether if @lo is far enough from 0.
  33. */
  34. static u64 __fbnic_time_get_slow(struct fbnic_dev *fbd)
  35. {
  36. u32 hi, lo;
  37. lockdep_assert_held(&fbd->time_lock);
  38. do {
  39. hi = fbnic_rd32(fbd, FBNIC_PTP_CTR_VAL_HI);
  40. lo = fbnic_rd32(fbd, FBNIC_PTP_CTR_VAL_LO);
  41. } while (hi != fbnic_rd32(fbd, FBNIC_PTP_CTR_VAL_HI));
  42. return (u64)hi << 32 | lo;
  43. }
  44. static void __fbnic_time_set_addend(struct fbnic_dev *fbd, u64 addend)
  45. {
  46. lockdep_assert_held(&fbd->time_lock);
  47. fbnic_wr32(fbd, FBNIC_PTP_ADD_VAL_NS,
  48. FIELD_PREP(FBNIC_PTP_ADD_VAL_NS_MASK, addend >> 32));
  49. fbnic_wr32(fbd, FBNIC_PTP_ADD_VAL_SUBNS, (u32)addend);
  50. }
  51. static void fbnic_ptp_fresh_check(struct fbnic_dev *fbd)
  52. {
  53. if (time_is_after_jiffies(fbd->last_read +
  54. FBNIC_TS_HIGH_REFRESH_JIF * 3 / 2))
  55. return;
  56. dev_warn(fbd->dev, "NIC timestamp refresh stall, delayed by %lu sec\n",
  57. (jiffies - fbd->last_read - FBNIC_TS_HIGH_REFRESH_JIF) / HZ);
  58. }
  59. static void fbnic_ptp_refresh_time(struct fbnic_dev *fbd, struct fbnic_net *fbn)
  60. {
  61. unsigned long flags;
  62. u32 hi;
  63. spin_lock_irqsave(&fbd->time_lock, flags);
  64. hi = fbnic_rd32(fbn->fbd, FBNIC_PTP_CTR_VAL_HI);
  65. if (!fbnic_present(fbd))
  66. goto out; /* Don't bother handling, reset is pending */
  67. /* Let's keep high cached value a bit lower to avoid race with
  68. * incoming timestamps. The logic in fbnic_ts40_to_ns() will
  69. * take care of overflow in this case. It will make cached time
  70. * ~1 minute lower and incoming timestamp will always be later
  71. * then cached time.
  72. */
  73. WRITE_ONCE(fbn->time_high, hi - 16);
  74. fbd->last_read = jiffies;
  75. out:
  76. spin_unlock_irqrestore(&fbd->time_lock, flags);
  77. }
  78. static long fbnic_ptp_do_aux_work(struct ptp_clock_info *ptp)
  79. {
  80. struct fbnic_dev *fbd = fbnic_from_ptp_info(ptp);
  81. struct fbnic_net *fbn;
  82. fbn = netdev_priv(fbd->netdev);
  83. fbnic_ptp_fresh_check(fbd);
  84. fbnic_ptp_refresh_time(fbd, fbn);
  85. return FBNIC_TS_HIGH_REFRESH_JIF;
  86. }
  87. static int fbnic_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  88. {
  89. struct fbnic_dev *fbd = fbnic_from_ptp_info(ptp);
  90. u64 addend, dclk_period;
  91. unsigned long flags;
  92. /* d_clock is 600 MHz; which in Q16.32 fixed point ns is: */
  93. dclk_period = (((u64)1000000000) << 32) / FBNIC_CLOCK_FREQ;
  94. addend = adjust_by_scaled_ppm(dclk_period, scaled_ppm);
  95. spin_lock_irqsave(&fbd->time_lock, flags);
  96. __fbnic_time_set_addend(fbd, addend);
  97. fbnic_wr32(fbd, FBNIC_PTP_ADJUST, FBNIC_PTP_ADJUST_ADDEND_SET);
  98. /* Flush, make sure FBNIC_PTP_ADD_VAL_* is stable for at least 4 clks */
  99. fbnic_rd32(fbd, FBNIC_PTP_SPARE);
  100. spin_unlock_irqrestore(&fbd->time_lock, flags);
  101. return fbnic_present(fbd) ? 0 : -EIO;
  102. }
  103. static int fbnic_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  104. {
  105. struct fbnic_dev *fbd = fbnic_from_ptp_info(ptp);
  106. struct fbnic_net *fbn;
  107. unsigned long flags;
  108. fbn = netdev_priv(fbd->netdev);
  109. spin_lock_irqsave(&fbd->time_lock, flags);
  110. u64_stats_update_begin(&fbn->time_seq);
  111. WRITE_ONCE(fbn->time_offset, READ_ONCE(fbn->time_offset) + delta);
  112. u64_stats_update_end(&fbn->time_seq);
  113. spin_unlock_irqrestore(&fbd->time_lock, flags);
  114. return 0;
  115. }
  116. static int
  117. fbnic_ptp_gettimex64(struct ptp_clock_info *ptp, struct timespec64 *ts,
  118. struct ptp_system_timestamp *sts)
  119. {
  120. struct fbnic_dev *fbd = fbnic_from_ptp_info(ptp);
  121. struct fbnic_net *fbn;
  122. unsigned long flags;
  123. u64 time_ns;
  124. u32 hi, lo;
  125. fbn = netdev_priv(fbd->netdev);
  126. spin_lock_irqsave(&fbd->time_lock, flags);
  127. do {
  128. hi = fbnic_rd32(fbd, FBNIC_PTP_CTR_VAL_HI);
  129. ptp_read_system_prets(sts);
  130. lo = fbnic_rd32(fbd, FBNIC_PTP_CTR_VAL_LO);
  131. ptp_read_system_postts(sts);
  132. /* Similarly to comment above __fbnic_time_get_slow()
  133. * - this can be optimized if needed.
  134. */
  135. } while (hi != fbnic_rd32(fbd, FBNIC_PTP_CTR_VAL_HI));
  136. time_ns = ((u64)hi << 32 | lo) + fbn->time_offset;
  137. spin_unlock_irqrestore(&fbd->time_lock, flags);
  138. if (!fbnic_present(fbd))
  139. return -EIO;
  140. *ts = ns_to_timespec64(time_ns);
  141. return 0;
  142. }
  143. static int
  144. fbnic_ptp_settime64(struct ptp_clock_info *ptp, const struct timespec64 *ts)
  145. {
  146. struct fbnic_dev *fbd = fbnic_from_ptp_info(ptp);
  147. struct fbnic_net *fbn;
  148. unsigned long flags;
  149. u64 dev_ns, host_ns;
  150. int ret;
  151. fbn = netdev_priv(fbd->netdev);
  152. host_ns = timespec64_to_ns(ts);
  153. spin_lock_irqsave(&fbd->time_lock, flags);
  154. dev_ns = __fbnic_time_get_slow(fbd);
  155. if (fbnic_present(fbd)) {
  156. u64_stats_update_begin(&fbn->time_seq);
  157. WRITE_ONCE(fbn->time_offset, host_ns - dev_ns);
  158. u64_stats_update_end(&fbn->time_seq);
  159. ret = 0;
  160. } else {
  161. ret = -EIO;
  162. }
  163. spin_unlock_irqrestore(&fbd->time_lock, flags);
  164. return ret;
  165. }
  166. static const struct ptp_clock_info fbnic_ptp_info = {
  167. .owner = THIS_MODULE,
  168. /* 1,000,000,000 - 1 PPB to ensure increment is positive
  169. * after max negative adjustment.
  170. */
  171. .max_adj = 999999999,
  172. .do_aux_work = fbnic_ptp_do_aux_work,
  173. .adjfine = fbnic_ptp_adjfine,
  174. .adjtime = fbnic_ptp_adjtime,
  175. .gettimex64 = fbnic_ptp_gettimex64,
  176. .settime64 = fbnic_ptp_settime64,
  177. };
  178. static void fbnic_ptp_reset(struct fbnic_dev *fbd)
  179. {
  180. struct fbnic_net *fbn = netdev_priv(fbd->netdev);
  181. u64 dclk_period;
  182. fbnic_wr32(fbd, FBNIC_PTP_CTRL,
  183. FBNIC_PTP_CTRL_EN |
  184. FIELD_PREP(FBNIC_PTP_CTRL_TICK_IVAL, 1));
  185. /* d_clock is 600 MHz; which in Q16.32 fixed point ns is: */
  186. dclk_period = (((u64)1000000000) << 32) / FBNIC_CLOCK_FREQ;
  187. __fbnic_time_set_addend(fbd, dclk_period);
  188. fbnic_wr32(fbd, FBNIC_PTP_INIT_HI, 0);
  189. fbnic_wr32(fbd, FBNIC_PTP_INIT_LO, 0);
  190. fbnic_wr32(fbd, FBNIC_PTP_ADJUST, FBNIC_PTP_ADJUST_INIT);
  191. fbnic_wr32(fbd, FBNIC_PTP_CTRL,
  192. FBNIC_PTP_CTRL_EN |
  193. FBNIC_PTP_CTRL_TQS_OUT_EN |
  194. FIELD_PREP(FBNIC_PTP_CTRL_MAC_OUT_IVAL, 3) |
  195. FIELD_PREP(FBNIC_PTP_CTRL_TICK_IVAL, 1));
  196. fbnic_rd32(fbd, FBNIC_PTP_SPARE);
  197. fbn->time_offset = 0;
  198. fbn->time_high = 0;
  199. }
  200. void fbnic_time_init(struct fbnic_net *fbn)
  201. {
  202. /* This is not really a statistic, but the locking primitive fits
  203. * our usecase perfectly, we need an atomic 8 bytes READ_ONCE() /
  204. * WRITE_ONCE() behavior.
  205. */
  206. u64_stats_init(&fbn->time_seq);
  207. }
  208. int fbnic_time_start(struct fbnic_net *fbn)
  209. {
  210. fbnic_ptp_refresh_time(fbn->fbd, fbn);
  211. /* Assume that fbnic_ptp_do_aux_work() will never be called if not
  212. * scheduled here
  213. */
  214. return ptp_schedule_worker(fbn->fbd->ptp, FBNIC_TS_HIGH_REFRESH_JIF);
  215. }
  216. void fbnic_time_stop(struct fbnic_net *fbn)
  217. {
  218. ptp_cancel_worker_sync(fbn->fbd->ptp);
  219. fbnic_ptp_fresh_check(fbn->fbd);
  220. }
  221. int fbnic_ptp_setup(struct fbnic_dev *fbd)
  222. {
  223. struct device *dev = fbd->dev;
  224. unsigned long flags;
  225. spin_lock_init(&fbd->time_lock);
  226. spin_lock_irqsave(&fbd->time_lock, flags); /* Appease lockdep */
  227. fbnic_ptp_reset(fbd);
  228. spin_unlock_irqrestore(&fbd->time_lock, flags);
  229. memcpy(&fbd->ptp_info, &fbnic_ptp_info, sizeof(fbnic_ptp_info));
  230. fbd->ptp = ptp_clock_register(&fbd->ptp_info, dev);
  231. if (IS_ERR(fbd->ptp))
  232. dev_err(dev, "Failed to register PTP: %pe\n", fbd->ptp);
  233. return PTR_ERR_OR_ZERO(fbd->ptp);
  234. }
  235. void fbnic_ptp_destroy(struct fbnic_dev *fbd)
  236. {
  237. if (!fbd->ptp)
  238. return;
  239. ptp_clock_unregister(fbd->ptp);
  240. }