tsc_sync.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 raw_spinlock_t sync_lock = __RAW_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(void)
  42. {
  43. cycles_t start, now, prev, end;
  44. int i;
  45. start = get_cycles_sync();
  46. /*
  47. * The measurement runs for 20 msecs:
  48. */
  49. end = start + tsc_khz * 20ULL;
  50. now = start;
  51. for (i = 0; ; i++) {
  52. /*
  53. * We take the global lock, measure TSC, save the
  54. * previous TSC that was measured (possibly on
  55. * another CPU) and update the previous TSC timestamp.
  56. */
  57. __raw_spin_lock(&sync_lock);
  58. prev = last_tsc;
  59. now = get_cycles_sync();
  60. last_tsc = now;
  61. __raw_spin_unlock(&sync_lock);
  62. /*
  63. * Be nice every now and then (and also check whether
  64. * measurement is done [we also insert a 100 million
  65. * loops safety exit, so we dont lock up in case the
  66. * TSC readout is totally broken]):
  67. */
  68. if (unlikely(!(i & 7))) {
  69. if (now > end || i > 100000000)
  70. break;
  71. cpu_relax();
  72. touch_nmi_watchdog();
  73. }
  74. /*
  75. * Outside the critical section we can now see whether
  76. * we saw a time-warp of the TSC going backwards:
  77. */
  78. if (unlikely(prev > now)) {
  79. __raw_spin_lock(&sync_lock);
  80. max_warp = max(max_warp, prev - now);
  81. nr_warps++;
  82. __raw_spin_unlock(&sync_lock);
  83. }
  84. }
  85. }
  86. /*
  87. * Source CPU calls into this - it waits for the freshly booted
  88. * target CPU to arrive and then starts the measurement:
  89. */
  90. void __cpuinit check_tsc_sync_source(int cpu)
  91. {
  92. int cpus = 2;
  93. /*
  94. * No need to check if we already know that the TSC is not
  95. * synchronized:
  96. */
  97. if (unsynchronized_tsc())
  98. return;
  99. printk(KERN_INFO "checking TSC synchronization [CPU#%d -> CPU#%d]:",
  100. smp_processor_id(), cpu);
  101. /*
  102. * Reset it - in case this is a second bootup:
  103. */
  104. atomic_set(&stop_count, 0);
  105. /*
  106. * Wait for the target to arrive:
  107. */
  108. while (atomic_read(&start_count) != cpus-1)
  109. cpu_relax();
  110. /*
  111. * Trigger the target to continue into the measurement too:
  112. */
  113. atomic_inc(&start_count);
  114. check_tsc_warp();
  115. while (atomic_read(&stop_count) != cpus-1)
  116. cpu_relax();
  117. /*
  118. * Reset it - just in case we boot another CPU later:
  119. */
  120. atomic_set(&start_count, 0);
  121. if (nr_warps) {
  122. printk("\n");
  123. printk(KERN_WARNING "Measured %Ld cycles TSC warp between CPUs,"
  124. " turning off TSC clock.\n", max_warp);
  125. mark_tsc_unstable("check_tsc_sync_source failed");
  126. nr_warps = 0;
  127. max_warp = 0;
  128. last_tsc = 0;
  129. } else {
  130. printk(" passed.\n");
  131. }
  132. /*
  133. * Let the target continue with the bootup:
  134. */
  135. atomic_inc(&stop_count);
  136. }
  137. /*
  138. * Freshly booted CPUs call into this:
  139. */
  140. void __cpuinit check_tsc_sync_target(void)
  141. {
  142. int cpus = 2;
  143. if (unsynchronized_tsc())
  144. return;
  145. /*
  146. * Register this CPU's participation and wait for the
  147. * source CPU to start the measurement:
  148. */
  149. atomic_inc(&start_count);
  150. while (atomic_read(&start_count) != cpus)
  151. cpu_relax();
  152. check_tsc_warp();
  153. /*
  154. * Ok, we are done:
  155. */
  156. atomic_inc(&stop_count);
  157. /*
  158. * Wait for the source CPU to print stuff:
  159. */
  160. while (atomic_read(&stop_count) != cpus)
  161. cpu_relax();
  162. }
  163. #undef NR_LOOPS