tsc_sync.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * check TSC synchronization.
  3. *
  4. * Copyright (C) 2006, Red Hat, Inc., Ingo Molnar
  5. *
  6. * We check whether all boot CPUs have their TSC's synchronized,
  7. * print a warning if not and turn off the TSC clock-source.
  8. *
  9. * The warp-check is point-to-point between two CPUs, the CPU
  10. * initiating the bootup is the 'source CPU', the freshly booting
  11. * CPU is the 'target CPU'.
  12. *
  13. * Only two CPUs may participate - they can enter in any order.
  14. * ( The serial nature of the boot logic and the CPU hotplug lock
  15. * protects against more than 2 CPUs entering this code. )
  16. */
  17. #include <linux/spinlock.h>
  18. #include <linux/kernel.h>
  19. #include <linux/smp.h>
  20. #include <linux/nmi.h>
  21. #include <asm/tsc.h>
  22. /*
  23. * Entry/exit counters that make sure that both CPUs
  24. * run the measurement code at once:
  25. */
  26. static atomic_t start_count;
  27. static atomic_t stop_count;
  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 TSC time-warps:
  32. */
  33. static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  34. static cycles_t last_tsc;
  35. static cycles_t max_warp;
  36. static int nr_warps;
  37. /*
  38. * TSC-warp measurement loop running on both CPUs:
  39. */
  40. static void check_tsc_warp(unsigned int timeout)
  41. {
  42. cycles_t start, now, prev, end;
  43. int i;
  44. rdtsc_barrier();
  45. start = get_cycles();
  46. rdtsc_barrier();
  47. /*
  48. * The measurement runs for 'timeout' msecs:
  49. */
  50. end = start + (cycles_t) tsc_khz * timeout;
  51. now = start;
  52. for (i = 0; ; i++) {
  53. /*
  54. * We take the global lock, measure TSC, save the
  55. * previous TSC that was measured (possibly on
  56. * another CPU) and update the previous TSC timestamp.
  57. */
  58. arch_spin_lock(&sync_lock);
  59. prev = last_tsc;
  60. rdtsc_barrier();
  61. now = get_cycles();
  62. rdtsc_barrier();
  63. last_tsc = now;
  64. arch_spin_unlock(&sync_lock);
  65. /*
  66. * Be nice every now and then (and also check whether
  67. * measurement is done [we also insert a 10 million
  68. * loops safety exit, so we dont lock up in case the
  69. * TSC readout is totally broken]):
  70. */
  71. if (unlikely(!(i & 7))) {
  72. if (now > end || i > 10000000)
  73. break;
  74. cpu_relax();
  75. touch_nmi_watchdog();
  76. }
  77. /*
  78. * Outside the critical section we can now see whether
  79. * we saw a time-warp of the TSC going backwards:
  80. */
  81. if (unlikely(prev > now)) {
  82. arch_spin_lock(&sync_lock);
  83. max_warp = max(max_warp, prev - now);
  84. nr_warps++;
  85. arch_spin_unlock(&sync_lock);
  86. }
  87. }
  88. WARN(!(now-start),
  89. "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n",
  90. now-start, end-start);
  91. }
  92. /*
  93. * If the target CPU coming online doesn't have any of its core-siblings
  94. * online, a timeout of 20msec will be used for the TSC-warp measurement
  95. * loop. Otherwise a smaller timeout of 2msec will be used, as we have some
  96. * information about this socket already (and this information grows as we
  97. * have more and more logical-siblings in that socket).
  98. *
  99. * Ideally we should be able to skip the TSC sync check on the other
  100. * core-siblings, if the first logical CPU in a socket passed the sync test.
  101. * But as the TSC is per-logical CPU and can potentially be modified wrongly
  102. * by the bios, TSC sync test for smaller duration should be able
  103. * to catch such errors. Also this will catch the condition where all the
  104. * cores in the socket doesn't get reset at the same time.
  105. */
  106. static inline unsigned int loop_timeout(int cpu)
  107. {
  108. return (cpumask_weight(cpu_core_mask(cpu)) > 1) ? 2 : 20;
  109. }
  110. /*
  111. * Source CPU calls into this - it waits for the freshly booted
  112. * target CPU to arrive and then starts the measurement:
  113. */
  114. void check_tsc_sync_source(int cpu)
  115. {
  116. int cpus = 2;
  117. /*
  118. * No need to check if we already know that the TSC is not
  119. * synchronized:
  120. */
  121. if (unsynchronized_tsc())
  122. return;
  123. if (tsc_clocksource_reliable) {
  124. if (cpu == (nr_cpu_ids-1) || system_state != SYSTEM_BOOTING)
  125. pr_info(
  126. "Skipped synchronization checks as TSC is reliable.\n");
  127. return;
  128. }
  129. /*
  130. * Reset it - in case this is a second bootup:
  131. */
  132. atomic_set(&stop_count, 0);
  133. /*
  134. * Wait for the target to arrive:
  135. */
  136. while (atomic_read(&start_count) != cpus-1)
  137. cpu_relax();
  138. /*
  139. * Trigger the target to continue into the measurement too:
  140. */
  141. atomic_inc(&start_count);
  142. check_tsc_warp(loop_timeout(cpu));
  143. while (atomic_read(&stop_count) != cpus-1)
  144. cpu_relax();
  145. if (nr_warps) {
  146. pr_warning("TSC synchronization [CPU#%d -> CPU#%d]:\n",
  147. smp_processor_id(), cpu);
  148. pr_warning("Measured %Ld cycles TSC warp between CPUs, "
  149. "turning off TSC clock.\n", max_warp);
  150. mark_tsc_unstable("check_tsc_sync_source failed");
  151. } else {
  152. pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n",
  153. smp_processor_id(), cpu);
  154. }
  155. /*
  156. * Reset it - just in case we boot another CPU later:
  157. */
  158. atomic_set(&start_count, 0);
  159. nr_warps = 0;
  160. max_warp = 0;
  161. last_tsc = 0;
  162. /*
  163. * Let the target continue with the bootup:
  164. */
  165. atomic_inc(&stop_count);
  166. }
  167. /*
  168. * Freshly booted CPUs call into this:
  169. */
  170. void check_tsc_sync_target(void)
  171. {
  172. int cpus = 2;
  173. if (unsynchronized_tsc() || tsc_clocksource_reliable)
  174. return;
  175. /*
  176. * Register this CPU's participation and wait for the
  177. * source CPU to start the measurement:
  178. */
  179. atomic_inc(&start_count);
  180. while (atomic_read(&start_count) != cpus)
  181. cpu_relax();
  182. check_tsc_warp(loop_timeout(smp_processor_id()));
  183. /*
  184. * Ok, we are done:
  185. */
  186. atomic_inc(&stop_count);
  187. /*
  188. * Wait for the source CPU to print stuff:
  189. */
  190. while (atomic_read(&stop_count) != cpus)
  191. cpu_relax();
  192. }