1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/topology.h>
19#include <linux/spinlock.h>
20#include <linux/kernel.h>
21#include <linux/smp.h>
22#include <linux/nmi.h>
23#include <asm/tsc.h>
24
25struct tsc_adjust {
26 s64 bootval;
27 s64 adjusted;
28 unsigned long nextcheck;
29 bool warned;
30};
31
32static DEFINE_PER_CPU(struct tsc_adjust, tsc_adjust);
33
34void tsc_verify_tsc_adjust(bool resume)
35{
36 struct tsc_adjust *adj = this_cpu_ptr(&tsc_adjust);
37 s64 curval;
38
39 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
40 return;
41
42
43 if (!resume && time_before(jiffies, adj->nextcheck))
44 return;
45
46 adj->nextcheck = jiffies + HZ;
47
48 rdmsrl(MSR_IA32_TSC_ADJUST, curval);
49 if (adj->adjusted == curval)
50 return;
51
52
53 wrmsrl(MSR_IA32_TSC_ADJUST, adj->adjusted);
54
55 if (!adj->warned || resume) {
56 pr_warn(FW_BUG "TSC ADJUST differs: CPU%u %lld --> %lld. Restoring\n",
57 smp_processor_id(), adj->adjusted, curval);
58 adj->warned = true;
59 }
60}
61
62static void tsc_sanitize_first_cpu(struct tsc_adjust *cur, s64 bootval,
63 unsigned int cpu, bool bootcpu)
64{
65
66
67
68
69
70
71
72
73
74
75
76 if (bootcpu && bootval != 0) {
77 pr_warn(FW_BUG "TSC ADJUST: CPU%u: %lld force to 0\n", cpu,
78 bootval);
79 wrmsrl(MSR_IA32_TSC_ADJUST, 0);
80 bootval = 0;
81 }
82 cur->adjusted = bootval;
83}
84
85#ifndef CONFIG_SMP
86bool __init tsc_store_and_check_tsc_adjust(bool bootcpu)
87{
88 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
89 s64 bootval;
90
91 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
92 return false;
93
94 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
95 cur->bootval = bootval;
96 cur->nextcheck = jiffies + HZ;
97 tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(), bootcpu);
98 return false;
99}
100
101#else
102
103
104
105
106bool tsc_store_and_check_tsc_adjust(bool bootcpu)
107{
108 struct tsc_adjust *ref, *cur = this_cpu_ptr(&tsc_adjust);
109 unsigned int refcpu, cpu = smp_processor_id();
110 struct cpumask *mask;
111 s64 bootval;
112
113 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
114 return false;
115
116 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
117 cur->bootval = bootval;
118 cur->nextcheck = jiffies + HZ;
119 cur->warned = false;
120
121
122
123
124
125
126
127
128 mask = topology_core_cpumask(cpu);
129 refcpu = mask ? cpumask_any_but(mask, cpu) : nr_cpu_ids;
130
131 if (refcpu >= nr_cpu_ids) {
132 tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(),
133 bootcpu);
134 return false;
135 }
136
137 ref = per_cpu_ptr(&tsc_adjust, refcpu);
138
139
140
141
142 if (bootval != ref->bootval) {
143 pr_warn(FW_BUG "TSC ADJUST differs: Reference CPU%u: %lld CPU%u: %lld\n",
144 refcpu, ref->bootval, cpu, bootval);
145 }
146
147
148
149
150
151
152 if (bootval != ref->adjusted) {
153 pr_warn("TSC ADJUST synchronize: Reference CPU%u: %lld CPU%u: %lld\n",
154 refcpu, ref->adjusted, cpu, bootval);
155 cur->adjusted = ref->adjusted;
156 wrmsrl(MSR_IA32_TSC_ADJUST, ref->adjusted);
157 }
158
159
160
161
162 return true;
163}
164
165
166
167
168
169static atomic_t start_count;
170static atomic_t stop_count;
171static atomic_t skip_test;
172static atomic_t test_runs;
173
174
175
176
177
178
179static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED;
180
181static cycles_t last_tsc;
182static cycles_t max_warp;
183static int nr_warps;
184static int random_warps;
185
186
187
188
189
190static cycles_t check_tsc_warp(unsigned int timeout)
191{
192 cycles_t start, now, prev, end, cur_max_warp = 0;
193 int i, cur_warps = 0;
194
195 start = rdtsc_ordered();
196
197
198
199 end = start + (cycles_t) tsc_khz * timeout;
200 now = start;
201
202 for (i = 0; ; i++) {
203
204
205
206
207
208 arch_spin_lock(&sync_lock);
209 prev = last_tsc;
210 now = rdtsc_ordered();
211 last_tsc = now;
212 arch_spin_unlock(&sync_lock);
213
214
215
216
217
218
219
220 if (unlikely(!(i & 7))) {
221 if (now > end || i > 10000000)
222 break;
223 cpu_relax();
224 touch_nmi_watchdog();
225 }
226
227
228
229
230 if (unlikely(prev > now)) {
231 arch_spin_lock(&sync_lock);
232 max_warp = max(max_warp, prev - now);
233 cur_max_warp = max_warp;
234
235
236
237
238 if (cur_warps != nr_warps)
239 random_warps++;
240 nr_warps++;
241 cur_warps = nr_warps;
242 arch_spin_unlock(&sync_lock);
243 }
244 }
245 WARN(!(now-start),
246 "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n",
247 now-start, end-start);
248 return cur_max_warp;
249}
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265static inline unsigned int loop_timeout(int cpu)
266{
267 return (cpumask_weight(topology_core_cpumask(cpu)) > 1) ? 2 : 20;
268}
269
270
271
272
273
274void check_tsc_sync_source(int cpu)
275{
276 int cpus = 2;
277
278
279
280
281
282 if (unsynchronized_tsc())
283 return;
284
285
286
287
288
289
290 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
291 atomic_set(&test_runs, 1);
292 else
293 atomic_set(&test_runs, 3);
294retry:
295
296
297
298 while (atomic_read(&start_count) != cpus - 1) {
299 if (atomic_read(&skip_test) > 0) {
300 atomic_set(&skip_test, 0);
301 return;
302 }
303 cpu_relax();
304 }
305
306
307
308
309 atomic_inc(&start_count);
310
311 check_tsc_warp(loop_timeout(cpu));
312
313 while (atomic_read(&stop_count) != cpus-1)
314 cpu_relax();
315
316
317
318
319
320
321 if (!nr_warps) {
322 atomic_set(&test_runs, 0);
323
324 pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n",
325 smp_processor_id(), cpu);
326
327 } else if (atomic_dec_and_test(&test_runs) || random_warps) {
328
329 atomic_set(&test_runs, 0);
330
331 pr_warning("TSC synchronization [CPU#%d -> CPU#%d]:\n",
332 smp_processor_id(), cpu);
333 pr_warning("Measured %Ld cycles TSC warp between CPUs, "
334 "turning off TSC clock.\n", max_warp);
335 if (random_warps)
336 pr_warning("TSC warped randomly between CPUs\n");
337 mark_tsc_unstable("check_tsc_sync_source failed");
338 }
339
340
341
342
343 atomic_set(&start_count, 0);
344 random_warps = 0;
345 nr_warps = 0;
346 max_warp = 0;
347 last_tsc = 0;
348
349
350
351
352 atomic_inc(&stop_count);
353
354
355
356
357 if (atomic_read(&test_runs) > 0)
358 goto retry;
359}
360
361
362
363
364void check_tsc_sync_target(void)
365{
366 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
367 unsigned int cpu = smp_processor_id();
368 cycles_t cur_max_warp, gbl_max_warp;
369 int cpus = 2;
370
371
372 if (unsynchronized_tsc())
373 return;
374
375
376
377
378
379
380
381
382
383
384 if (tsc_store_and_check_tsc_adjust(false) || tsc_clocksource_reliable) {
385 atomic_inc(&skip_test);
386 return;
387 }
388
389retry:
390
391
392
393
394 atomic_inc(&start_count);
395 while (atomic_read(&start_count) != cpus)
396 cpu_relax();
397
398 cur_max_warp = check_tsc_warp(loop_timeout(cpu));
399
400
401
402
403 gbl_max_warp = max_warp;
404
405
406
407
408 atomic_inc(&stop_count);
409
410
411
412
413 while (atomic_read(&stop_count) != cpus)
414 cpu_relax();
415
416
417
418
419 atomic_set(&stop_count, 0);
420
421
422
423
424
425
426 if (!atomic_read(&test_runs))
427 return;
428
429
430
431
432
433
434 if (!cur_max_warp)
435 cur_max_warp = -gbl_max_warp;
436
437
438
439
440
441
442
443
444
445
446
447
448 cur->adjusted += cur_max_warp;
449
450 pr_warn("TSC ADJUST compensate: CPU%u observed %lld warp. Adjust: %lld\n",
451 cpu, cur_max_warp, cur->adjusted);
452
453 wrmsrl(MSR_IA32_TSC_ADJUST, cur->adjusted);
454 goto retry;
455
456}
457
458#endif
459