sync-r4k.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Count register synchronisation.
  4. *
  5. * Derived from arch/x86/kernel/tsc_sync.c
  6. * Copyright (C) 2006, Red Hat, Inc., Ingo Molnar
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/irqflags.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/atomic.h>
  12. #include <linux/nmi.h>
  13. #include <linux/smp.h>
  14. #include <linux/spinlock.h>
  15. #include <asm/r4k-timer.h>
  16. #include <asm/mipsregs.h>
  17. #include <asm/time.h>
  18. #define COUNTON 100
  19. #define NR_LOOPS 3
  20. #define LOOP_TIMEOUT 20
  21. /*
  22. * Entry/exit counters that make sure that both CPUs
  23. * run the measurement code at once:
  24. */
  25. static atomic_t start_count;
  26. static atomic_t stop_count;
  27. static atomic_t test_runs;
  28. /*
  29. * We use a raw spinlock in this exceptional case, because
  30. * we want to have the fastest, inlined, non-debug version
  31. * of a critical section, to be able to prove counter time-warps:
  32. */
  33. static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  34. static uint32_t last_counter;
  35. static uint32_t max_warp;
  36. static int nr_warps;
  37. static int random_warps;
  38. /*
  39. * Counter warp measurement loop running on both CPUs.
  40. */
  41. static uint32_t check_counter_warp(void)
  42. {
  43. uint32_t start, now, prev, end, cur_max_warp = 0;
  44. int i, cur_warps = 0;
  45. start = read_c0_count();
  46. end = start + (uint32_t) mips_hpt_frequency / 1000 * LOOP_TIMEOUT;
  47. for (i = 0; ; i++) {
  48. /*
  49. * We take the global lock, measure counter, save the
  50. * previous counter that was measured (possibly on
  51. * another CPU) and update the previous counter timestamp.
  52. */
  53. arch_spin_lock(&sync_lock);
  54. prev = last_counter;
  55. now = read_c0_count();
  56. last_counter = now;
  57. arch_spin_unlock(&sync_lock);
  58. /*
  59. * Be nice every now and then (and also check whether
  60. * measurement is done [we also insert a 10 million
  61. * loops safety exit, so we dont lock up in case the
  62. * counter is totally broken]):
  63. */
  64. if (unlikely(!(i & 7))) {
  65. if (now > end || i > 10000000)
  66. break;
  67. cpu_relax();
  68. touch_nmi_watchdog();
  69. }
  70. /*
  71. * Outside the critical section we can now see whether
  72. * we saw a time-warp of the counter going backwards:
  73. */
  74. if (unlikely(prev > now)) {
  75. arch_spin_lock(&sync_lock);
  76. max_warp = max(max_warp, prev - now);
  77. cur_max_warp = max_warp;
  78. /*
  79. * Check whether this bounces back and forth. Only
  80. * one CPU should observe time going backwards.
  81. */
  82. if (cur_warps != nr_warps)
  83. random_warps++;
  84. nr_warps++;
  85. cur_warps = nr_warps;
  86. arch_spin_unlock(&sync_lock);
  87. }
  88. }
  89. WARN(!(now-start),
  90. "Warning: zero counter calibration delta: %d [max: %d]\n",
  91. now-start, end-start);
  92. return cur_max_warp;
  93. }
  94. /*
  95. * The freshly booted CPU initiates this via an async SMP function call.
  96. */
  97. static void check_counter_sync_source(void *__cpu)
  98. {
  99. unsigned int cpu = (unsigned long)__cpu;
  100. int cpus = 2;
  101. atomic_set(&test_runs, NR_LOOPS);
  102. retry:
  103. /* Wait for the target to start. */
  104. while (atomic_read(&start_count) != cpus - 1)
  105. cpu_relax();
  106. /*
  107. * Trigger the target to continue into the measurement too:
  108. */
  109. atomic_inc(&start_count);
  110. check_counter_warp();
  111. while (atomic_read(&stop_count) != cpus-1)
  112. cpu_relax();
  113. /*
  114. * If the test was successful set the number of runs to zero and
  115. * stop. If not, decrement the number of runs an check if we can
  116. * retry. In case of random warps no retry is attempted.
  117. */
  118. if (!nr_warps) {
  119. atomic_set(&test_runs, 0);
  120. pr_info("Counter synchronization [CPU#%d -> CPU#%u]: passed\n",
  121. smp_processor_id(), cpu);
  122. } else if (atomic_dec_and_test(&test_runs) || random_warps) {
  123. /* Force it to 0 if random warps brought us here */
  124. atomic_set(&test_runs, 0);
  125. pr_info("Counter synchronization [CPU#%d -> CPU#%u]:\n",
  126. smp_processor_id(), cpu);
  127. pr_info("Measured %d cycles counter warp between CPUs", max_warp);
  128. if (random_warps)
  129. pr_warn("Counter warped randomly between CPUs\n");
  130. }
  131. /*
  132. * Reset it - just in case we boot another CPU later:
  133. */
  134. atomic_set(&start_count, 0);
  135. random_warps = 0;
  136. nr_warps = 0;
  137. max_warp = 0;
  138. last_counter = 0;
  139. /*
  140. * Let the target continue with the bootup:
  141. */
  142. atomic_inc(&stop_count);
  143. /*
  144. * Retry, if there is a chance to do so.
  145. */
  146. if (atomic_read(&test_runs) > 0)
  147. goto retry;
  148. }
  149. /*
  150. * Freshly booted CPUs call into this:
  151. */
  152. void synchronise_count_slave(int cpu)
  153. {
  154. uint32_t cur_max_warp, gbl_max_warp, count;
  155. int cpus = 2;
  156. if (!cpu_has_counter || !mips_hpt_frequency)
  157. return;
  158. /* Kick the control CPU into the counter synchronization function */
  159. smp_call_function_single(cpumask_first(cpu_online_mask),
  160. check_counter_sync_source,
  161. (unsigned long *)(unsigned long)cpu, 0);
  162. retry:
  163. /*
  164. * Register this CPU's participation and wait for the
  165. * source CPU to start the measurement:
  166. */
  167. atomic_inc(&start_count);
  168. while (atomic_read(&start_count) != cpus)
  169. cpu_relax();
  170. cur_max_warp = check_counter_warp();
  171. /*
  172. * Store the maximum observed warp value for a potential retry:
  173. */
  174. gbl_max_warp = max_warp;
  175. /*
  176. * Ok, we are done:
  177. */
  178. atomic_inc(&stop_count);
  179. /*
  180. * Wait for the source CPU to print stuff:
  181. */
  182. while (atomic_read(&stop_count) != cpus)
  183. cpu_relax();
  184. /*
  185. * Reset it for the next sync test:
  186. */
  187. atomic_set(&stop_count, 0);
  188. /*
  189. * Check the number of remaining test runs. If not zero, the test
  190. * failed and a retry with adjusted counter is possible. If zero the
  191. * test was either successful or failed terminally.
  192. */
  193. if (!atomic_read(&test_runs)) {
  194. /* Arrange for an interrupt in a short while */
  195. write_c0_compare(read_c0_count() + COUNTON);
  196. return;
  197. }
  198. /*
  199. * If the warp value of this CPU is 0, then the other CPU
  200. * observed time going backwards so this counter was ahead and
  201. * needs to move backwards.
  202. */
  203. if (!cur_max_warp)
  204. cur_max_warp = -gbl_max_warp;
  205. count = read_c0_count();
  206. count += cur_max_warp;
  207. write_c0_count(count);
  208. pr_debug("Counter compensate: CPU%u observed %d warp\n", cpu, cur_max_warp);
  209. goto retry;
  210. }