tsc_sync.c 5.5 KB

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