blob: bf36328f6ef9289c4b1a1a7d0ffd9c67878f3f32 [file] [log] [blame]
Thomas Gleixner250c2272007-10-11 11:17:24 +02001/*
Dave Jones835c34a2007-10-12 21:10:53 -04002 * check TSC synchronization.
Thomas Gleixner250c2272007-10-11 11:17:24 +02003 *
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/*
25 * Entry/exit counters that make sure that both CPUs
26 * run the measurement code at once:
27 */
28static __cpuinitdata atomic_t start_count;
29static __cpuinitdata atomic_t stop_count;
30
31/*
32 * We use a raw spinlock in this exceptional case, because
33 * we want to have the fastest, inlined, non-debug version
34 * of a critical section, to be able to prove TSC time-warps:
35 */
36static __cpuinitdata raw_spinlock_t sync_lock = __RAW_SPIN_LOCK_UNLOCKED;
37static __cpuinitdata cycles_t last_tsc;
38static __cpuinitdata cycles_t max_warp;
39static __cpuinitdata int nr_warps;
40
41/*
42 * TSC-warp measurement loop running on both CPUs:
43 */
44static __cpuinit void check_tsc_warp(void)
45{
46 cycles_t start, now, prev, end;
47 int i;
48
Venki Pallipadi93ce99e2008-11-17 14:43:58 -080049 rdtsc_barrier();
Andi Kleen6d63de82008-01-30 13:32:39 +010050 start = get_cycles();
Venki Pallipadi93ce99e2008-11-17 14:43:58 -080051 rdtsc_barrier();
Thomas Gleixner250c2272007-10-11 11:17:24 +020052 /*
53 * The measurement runs for 20 msecs:
54 */
55 end = start + tsc_khz * 20ULL;
56 now = start;
57
58 for (i = 0; ; i++) {
59 /*
60 * We take the global lock, measure TSC, save the
61 * previous TSC that was measured (possibly on
62 * another CPU) and update the previous TSC timestamp.
63 */
64 __raw_spin_lock(&sync_lock);
65 prev = last_tsc;
Venki Pallipadi93ce99e2008-11-17 14:43:58 -080066 rdtsc_barrier();
Andi Kleen6d63de82008-01-30 13:32:39 +010067 now = get_cycles();
Venki Pallipadi93ce99e2008-11-17 14:43:58 -080068 rdtsc_barrier();
Thomas Gleixner250c2272007-10-11 11:17:24 +020069 last_tsc = now;
70 __raw_spin_unlock(&sync_lock);
71
72 /*
73 * Be nice every now and then (and also check whether
Ingo Molnardf435102008-01-30 13:33:23 +010074 * measurement is done [we also insert a 10 million
Thomas Gleixner250c2272007-10-11 11:17:24 +020075 * loops safety exit, so we dont lock up in case the
76 * TSC readout is totally broken]):
77 */
78 if (unlikely(!(i & 7))) {
Ingo Molnardf435102008-01-30 13:33:23 +010079 if (now > end || i > 10000000)
Thomas Gleixner250c2272007-10-11 11:17:24 +020080 break;
81 cpu_relax();
82 touch_nmi_watchdog();
83 }
84 /*
85 * Outside the critical section we can now see whether
86 * we saw a time-warp of the TSC going backwards:
87 */
88 if (unlikely(prev > now)) {
89 __raw_spin_lock(&sync_lock);
90 max_warp = max(max_warp, prev - now);
91 nr_warps++;
92 __raw_spin_unlock(&sync_lock);
93 }
Ingo Molnarad8ca492008-01-30 13:33:24 +010094 }
Arjan van de Venbde78a72008-07-08 09:51:56 -070095 WARN(!(now-start),
96 "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n",
Ingo Molnarad8ca492008-01-30 13:33:24 +010097 now-start, end-start);
Thomas Gleixner250c2272007-10-11 11:17:24 +020098}
99
100/*
101 * Source CPU calls into this - it waits for the freshly booted
102 * target CPU to arrive and then starts the measurement:
103 */
104void __cpuinit check_tsc_sync_source(int cpu)
105{
106 int cpus = 2;
107
108 /*
109 * No need to check if we already know that the TSC is not
110 * synchronized:
111 */
112 if (unsynchronized_tsc())
113 return;
114
Alok Katariaeca0cd02008-10-31 12:01:58 -0700115 if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) {
116 printk(KERN_INFO
117 "Skipping synchronization checks as TSC is reliable.\n");
118 return;
119 }
120
Thomas Gleixner250c2272007-10-11 11:17:24 +0200121 printk(KERN_INFO "checking TSC synchronization [CPU#%d -> CPU#%d]:",
122 smp_processor_id(), cpu);
123
124 /*
125 * Reset it - in case this is a second bootup:
126 */
127 atomic_set(&stop_count, 0);
128
129 /*
130 * Wait for the target to arrive:
131 */
132 while (atomic_read(&start_count) != cpus-1)
133 cpu_relax();
134 /*
135 * Trigger the target to continue into the measurement too:
136 */
137 atomic_inc(&start_count);
138
139 check_tsc_warp();
140
141 while (atomic_read(&stop_count) != cpus-1)
142 cpu_relax();
143
Thomas Gleixner250c2272007-10-11 11:17:24 +0200144 if (nr_warps) {
145 printk("\n");
146 printk(KERN_WARNING "Measured %Ld cycles TSC warp between CPUs,"
147 " turning off TSC clock.\n", max_warp);
148 mark_tsc_unstable("check_tsc_sync_source failed");
Thomas Gleixner250c2272007-10-11 11:17:24 +0200149 } else {
150 printk(" passed.\n");
151 }
152
153 /*
Mike Galbraith4c6b8b42008-01-30 13:30:04 +0100154 * Reset it - just in case we boot another CPU later:
155 */
156 atomic_set(&start_count, 0);
157 nr_warps = 0;
158 max_warp = 0;
159 last_tsc = 0;
160
161 /*
Thomas Gleixner250c2272007-10-11 11:17:24 +0200162 * Let the target continue with the bootup:
163 */
164 atomic_inc(&stop_count);
165}
166
167/*
168 * Freshly booted CPUs call into this:
169 */
170void __cpuinit check_tsc_sync_target(void)
171{
172 int cpus = 2;
173
Alok Katariaeca0cd02008-10-31 12:01:58 -0700174 if (unsynchronized_tsc() || boot_cpu_has(X86_FEATURE_TSC_RELIABLE))
Thomas Gleixner250c2272007-10-11 11:17:24 +0200175 return;
176
177 /*
178 * Register this CPU's participation and wait for the
179 * source CPU to start the measurement:
180 */
181 atomic_inc(&start_count);
182 while (atomic_read(&start_count) != cpus)
183 cpu_relax();
184
185 check_tsc_warp();
186
187 /*
188 * Ok, we are done:
189 */
190 atomic_inc(&stop_count);
191
192 /*
193 * Wait for the source CPU to print stuff:
194 */
195 while (atomic_read(&stop_count) != cpus)
196 cpu_relax();
197}
198#undef NR_LOOPS
199