ptp_mock.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2023 NXP
  4. *
  5. * Mock-up PTP Hardware Clock driver for virtual network devices
  6. *
  7. * Create a PTP clock which offers PTP time manipulation operations
  8. * using a timecounter/cyclecounter on top of CLOCK_MONOTONIC_RAW.
  9. */
  10. #include <linux/ptp_clock_kernel.h>
  11. #include <linux/ptp_mock.h>
  12. #include <linux/timecounter.h>
  13. /* Clamp scaled_ppm between -2,097,152,000 and 2,097,152,000,
  14. * and thus "adj" between -68,719,476 and 68,719,476
  15. */
  16. #define MOCK_PHC_MAX_ADJ_PPB 32000000
  17. /* Timestamps from ktime_get_raw() have 1 ns resolution, so the scale factor
  18. * (MULT >> SHIFT) needs to be 1. Pick SHIFT as 31 bits, which translates
  19. * MULT(freq 0) into 0x80000000.
  20. */
  21. #define MOCK_PHC_CC_SHIFT 31
  22. #define MOCK_PHC_CC_MULT (1 << MOCK_PHC_CC_SHIFT)
  23. #define MOCK_PHC_FADJ_SHIFT 9
  24. #define MOCK_PHC_FADJ_DENOMINATOR 15625ULL
  25. /* The largest cycle_delta that timecounter_read_delta() can handle without a
  26. * 64-bit overflow during the multiplication with cc->mult, given the max "adj"
  27. * we permit, is ~8.3 seconds. Make sure readouts are more frequent than that.
  28. */
  29. #define MOCK_PHC_REFRESH_INTERVAL (HZ * 5)
  30. #define info_to_phc(d) container_of((d), struct mock_phc, info)
  31. struct mock_phc {
  32. struct ptp_clock_info info;
  33. struct ptp_clock *clock;
  34. struct timecounter tc;
  35. struct cyclecounter cc;
  36. spinlock_t lock;
  37. };
  38. static u64 mock_phc_cc_read(const struct cyclecounter *cc)
  39. {
  40. return ktime_get_raw_ns();
  41. }
  42. static int mock_phc_adjfine(struct ptp_clock_info *info, long scaled_ppm)
  43. {
  44. struct mock_phc *phc = info_to_phc(info);
  45. s64 adj;
  46. adj = (s64)scaled_ppm << MOCK_PHC_FADJ_SHIFT;
  47. adj = div_s64(adj, MOCK_PHC_FADJ_DENOMINATOR);
  48. spin_lock(&phc->lock);
  49. timecounter_read(&phc->tc);
  50. phc->cc.mult = MOCK_PHC_CC_MULT + adj;
  51. spin_unlock(&phc->lock);
  52. return 0;
  53. }
  54. static int mock_phc_adjtime(struct ptp_clock_info *info, s64 delta)
  55. {
  56. struct mock_phc *phc = info_to_phc(info);
  57. spin_lock(&phc->lock);
  58. timecounter_adjtime(&phc->tc, delta);
  59. spin_unlock(&phc->lock);
  60. return 0;
  61. }
  62. static int mock_phc_settime64(struct ptp_clock_info *info,
  63. const struct timespec64 *ts)
  64. {
  65. struct mock_phc *phc = info_to_phc(info);
  66. u64 ns = timespec64_to_ns(ts);
  67. spin_lock(&phc->lock);
  68. timecounter_init(&phc->tc, &phc->cc, ns);
  69. spin_unlock(&phc->lock);
  70. return 0;
  71. }
  72. static int mock_phc_gettime64(struct ptp_clock_info *info, struct timespec64 *ts)
  73. {
  74. struct mock_phc *phc = info_to_phc(info);
  75. u64 ns;
  76. spin_lock(&phc->lock);
  77. ns = timecounter_read(&phc->tc);
  78. spin_unlock(&phc->lock);
  79. *ts = ns_to_timespec64(ns);
  80. return 0;
  81. }
  82. static long mock_phc_refresh(struct ptp_clock_info *info)
  83. {
  84. struct timespec64 ts;
  85. mock_phc_gettime64(info, &ts);
  86. return MOCK_PHC_REFRESH_INTERVAL;
  87. }
  88. int mock_phc_index(struct mock_phc *phc)
  89. {
  90. return ptp_clock_index(phc->clock);
  91. }
  92. EXPORT_SYMBOL_GPL(mock_phc_index);
  93. struct mock_phc *mock_phc_create(struct device *dev)
  94. {
  95. struct mock_phc *phc;
  96. int err;
  97. phc = kzalloc(sizeof(*phc), GFP_KERNEL);
  98. if (!phc) {
  99. err = -ENOMEM;
  100. goto out;
  101. }
  102. phc->info = (struct ptp_clock_info) {
  103. .owner = THIS_MODULE,
  104. .name = "Mock-up PTP clock",
  105. .max_adj = MOCK_PHC_MAX_ADJ_PPB,
  106. .adjfine = mock_phc_adjfine,
  107. .adjtime = mock_phc_adjtime,
  108. .gettime64 = mock_phc_gettime64,
  109. .settime64 = mock_phc_settime64,
  110. .do_aux_work = mock_phc_refresh,
  111. };
  112. phc->cc = (struct cyclecounter) {
  113. .read = mock_phc_cc_read,
  114. .mask = CYCLECOUNTER_MASK(64),
  115. .mult = MOCK_PHC_CC_MULT,
  116. .shift = MOCK_PHC_CC_SHIFT,
  117. };
  118. spin_lock_init(&phc->lock);
  119. timecounter_init(&phc->tc, &phc->cc, 0);
  120. phc->clock = ptp_clock_register(&phc->info, dev);
  121. if (IS_ERR(phc->clock)) {
  122. err = PTR_ERR(phc->clock);
  123. goto out_free_phc;
  124. }
  125. ptp_schedule_worker(phc->clock, MOCK_PHC_REFRESH_INTERVAL);
  126. return phc;
  127. out_free_phc:
  128. kfree(phc);
  129. out:
  130. return ERR_PTR(err);
  131. }
  132. EXPORT_SYMBOL_GPL(mock_phc_create);
  133. void mock_phc_destroy(struct mock_phc *phc)
  134. {
  135. ptp_clock_unregister(phc->clock);
  136. kfree(phc);
  137. }
  138. EXPORT_SYMBOL_GPL(mock_phc_destroy);
  139. MODULE_DESCRIPTION("Mock-up PTP Hardware Clock driver");
  140. MODULE_LICENSE("GPL");