0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/topology.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/kernel.h>
0021 #include <linux/smp.h>
0022 #include <linux/nmi.h>
0023 #include <asm/tsc.h>
0024
0025 struct tsc_adjust {
0026 s64 bootval;
0027 s64 adjusted;
0028 unsigned long nextcheck;
0029 bool warned;
0030 };
0031
0032 static DEFINE_PER_CPU(struct tsc_adjust, tsc_adjust);
0033 static struct timer_list tsc_sync_check_timer;
0034
0035
0036
0037
0038
0039 bool __read_mostly tsc_async_resets;
0040
0041 void mark_tsc_async_resets(char *reason)
0042 {
0043 if (tsc_async_resets)
0044 return;
0045 tsc_async_resets = true;
0046 pr_info("tsc: Marking TSC async resets true due to %s\n", reason);
0047 }
0048
0049 void tsc_verify_tsc_adjust(bool resume)
0050 {
0051 struct tsc_adjust *adj = this_cpu_ptr(&tsc_adjust);
0052 s64 curval;
0053
0054 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
0055 return;
0056
0057
0058 if (check_tsc_unstable())
0059 return;
0060
0061
0062 if (!resume && time_before(jiffies, adj->nextcheck))
0063 return;
0064
0065 adj->nextcheck = jiffies + HZ;
0066
0067 rdmsrl(MSR_IA32_TSC_ADJUST, curval);
0068 if (adj->adjusted == curval)
0069 return;
0070
0071
0072 wrmsrl(MSR_IA32_TSC_ADJUST, adj->adjusted);
0073
0074 if (!adj->warned || resume) {
0075 pr_warn(FW_BUG "TSC ADJUST differs: CPU%u %lld --> %lld. Restoring\n",
0076 smp_processor_id(), adj->adjusted, curval);
0077 adj->warned = true;
0078 }
0079 }
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091 #define SYNC_CHECK_INTERVAL (HZ * 600)
0092
0093 static void tsc_sync_check_timer_fn(struct timer_list *unused)
0094 {
0095 int next_cpu;
0096
0097 tsc_verify_tsc_adjust(false);
0098
0099
0100 next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
0101 if (next_cpu >= nr_cpu_ids)
0102 next_cpu = cpumask_first(cpu_online_mask);
0103
0104 tsc_sync_check_timer.expires += SYNC_CHECK_INTERVAL;
0105 add_timer_on(&tsc_sync_check_timer, next_cpu);
0106 }
0107
0108 static int __init start_sync_check_timer(void)
0109 {
0110 if (!cpu_feature_enabled(X86_FEATURE_TSC_ADJUST) || tsc_clocksource_reliable)
0111 return 0;
0112
0113 timer_setup(&tsc_sync_check_timer, tsc_sync_check_timer_fn, 0);
0114 tsc_sync_check_timer.expires = jiffies + SYNC_CHECK_INTERVAL;
0115 add_timer(&tsc_sync_check_timer);
0116
0117 return 0;
0118 }
0119 late_initcall(start_sync_check_timer);
0120
0121 static void tsc_sanitize_first_cpu(struct tsc_adjust *cur, s64 bootval,
0122 unsigned int cpu, bool bootcpu)
0123 {
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140 if (bootcpu && bootval != 0) {
0141 if (likely(!tsc_async_resets)) {
0142 pr_warn(FW_BUG "TSC ADJUST: CPU%u: %lld force to 0\n",
0143 cpu, bootval);
0144 wrmsrl(MSR_IA32_TSC_ADJUST, 0);
0145 bootval = 0;
0146 } else {
0147 pr_info("TSC ADJUST: CPU%u: %lld NOT forced to 0\n",
0148 cpu, bootval);
0149 }
0150 }
0151 cur->adjusted = bootval;
0152 }
0153
0154 #ifndef CONFIG_SMP
0155 bool __init tsc_store_and_check_tsc_adjust(bool bootcpu)
0156 {
0157 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
0158 s64 bootval;
0159
0160 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
0161 return false;
0162
0163
0164 if (check_tsc_unstable())
0165 return false;
0166
0167 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
0168 cur->bootval = bootval;
0169 cur->nextcheck = jiffies + HZ;
0170 tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(), bootcpu);
0171 return false;
0172 }
0173
0174 #else
0175
0176
0177
0178
0179 bool tsc_store_and_check_tsc_adjust(bool bootcpu)
0180 {
0181 struct tsc_adjust *ref, *cur = this_cpu_ptr(&tsc_adjust);
0182 unsigned int refcpu, cpu = smp_processor_id();
0183 struct cpumask *mask;
0184 s64 bootval;
0185
0186 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
0187 return false;
0188
0189 rdmsrl(MSR_IA32_TSC_ADJUST, bootval);
0190 cur->bootval = bootval;
0191 cur->nextcheck = jiffies + HZ;
0192 cur->warned = false;
0193
0194
0195
0196
0197
0198 if (tsc_async_resets)
0199 cur->adjusted = bootval;
0200
0201
0202
0203
0204
0205
0206
0207
0208 mask = topology_core_cpumask(cpu);
0209 refcpu = mask ? cpumask_any_but(mask, cpu) : nr_cpu_ids;
0210
0211 if (refcpu >= nr_cpu_ids) {
0212 tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(),
0213 bootcpu);
0214 return false;
0215 }
0216
0217 ref = per_cpu_ptr(&tsc_adjust, refcpu);
0218
0219
0220
0221
0222 if (bootval != ref->bootval)
0223 printk_once(FW_BUG "TSC ADJUST differs within socket(s), fixing all errors\n");
0224
0225
0226
0227
0228
0229
0230
0231 if (bootval != ref->adjusted) {
0232 cur->adjusted = ref->adjusted;
0233 wrmsrl(MSR_IA32_TSC_ADJUST, ref->adjusted);
0234 }
0235
0236
0237
0238
0239 return true;
0240 }
0241
0242
0243
0244
0245
0246 static atomic_t start_count;
0247 static atomic_t stop_count;
0248 static atomic_t skip_test;
0249 static atomic_t test_runs;
0250
0251
0252
0253
0254
0255
0256 static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED;
0257
0258 static cycles_t last_tsc;
0259 static cycles_t max_warp;
0260 static int nr_warps;
0261 static int random_warps;
0262
0263
0264
0265
0266
0267 static cycles_t check_tsc_warp(unsigned int timeout)
0268 {
0269 cycles_t start, now, prev, end, cur_max_warp = 0;
0270 int i, cur_warps = 0;
0271
0272 start = rdtsc_ordered();
0273
0274
0275
0276 end = start + (cycles_t) tsc_khz * timeout;
0277
0278 for (i = 0; ; i++) {
0279
0280
0281
0282
0283
0284 arch_spin_lock(&sync_lock);
0285 prev = last_tsc;
0286 now = rdtsc_ordered();
0287 last_tsc = now;
0288 arch_spin_unlock(&sync_lock);
0289
0290
0291
0292
0293
0294
0295
0296 if (unlikely(!(i & 7))) {
0297 if (now > end || i > 10000000)
0298 break;
0299 cpu_relax();
0300 touch_nmi_watchdog();
0301 }
0302
0303
0304
0305
0306 if (unlikely(prev > now)) {
0307 arch_spin_lock(&sync_lock);
0308 max_warp = max(max_warp, prev - now);
0309 cur_max_warp = max_warp;
0310
0311
0312
0313
0314 if (cur_warps != nr_warps)
0315 random_warps++;
0316 nr_warps++;
0317 cur_warps = nr_warps;
0318 arch_spin_unlock(&sync_lock);
0319 }
0320 }
0321 WARN(!(now-start),
0322 "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n",
0323 now-start, end-start);
0324 return cur_max_warp;
0325 }
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341 static inline unsigned int loop_timeout(int cpu)
0342 {
0343 return (cpumask_weight(topology_core_cpumask(cpu)) > 1) ? 2 : 20;
0344 }
0345
0346
0347
0348
0349
0350 void check_tsc_sync_source(int cpu)
0351 {
0352 int cpus = 2;
0353
0354
0355
0356
0357
0358 if (unsynchronized_tsc())
0359 return;
0360
0361
0362
0363
0364
0365
0366 if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST))
0367 atomic_set(&test_runs, 1);
0368 else
0369 atomic_set(&test_runs, 3);
0370 retry:
0371
0372
0373
0374 while (atomic_read(&start_count) != cpus - 1) {
0375 if (atomic_read(&skip_test) > 0) {
0376 atomic_set(&skip_test, 0);
0377 return;
0378 }
0379 cpu_relax();
0380 }
0381
0382
0383
0384
0385 atomic_inc(&start_count);
0386
0387 check_tsc_warp(loop_timeout(cpu));
0388
0389 while (atomic_read(&stop_count) != cpus-1)
0390 cpu_relax();
0391
0392
0393
0394
0395
0396
0397 if (!nr_warps) {
0398 atomic_set(&test_runs, 0);
0399
0400 pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n",
0401 smp_processor_id(), cpu);
0402
0403 } else if (atomic_dec_and_test(&test_runs) || random_warps) {
0404
0405 atomic_set(&test_runs, 0);
0406
0407 pr_warn("TSC synchronization [CPU#%d -> CPU#%d]:\n",
0408 smp_processor_id(), cpu);
0409 pr_warn("Measured %Ld cycles TSC warp between CPUs, "
0410 "turning off TSC clock.\n", max_warp);
0411 if (random_warps)
0412 pr_warn("TSC warped randomly between CPUs\n");
0413 mark_tsc_unstable("check_tsc_sync_source failed");
0414 }
0415
0416
0417
0418
0419 atomic_set(&start_count, 0);
0420 random_warps = 0;
0421 nr_warps = 0;
0422 max_warp = 0;
0423 last_tsc = 0;
0424
0425
0426
0427
0428 atomic_inc(&stop_count);
0429
0430
0431
0432
0433 if (atomic_read(&test_runs) > 0)
0434 goto retry;
0435 }
0436
0437
0438
0439
0440 void check_tsc_sync_target(void)
0441 {
0442 struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust);
0443 unsigned int cpu = smp_processor_id();
0444 cycles_t cur_max_warp, gbl_max_warp;
0445 int cpus = 2;
0446
0447
0448 if (unsynchronized_tsc())
0449 return;
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460 if (tsc_store_and_check_tsc_adjust(false) || tsc_clocksource_reliable) {
0461 atomic_inc(&skip_test);
0462 return;
0463 }
0464
0465 retry:
0466
0467
0468
0469
0470 atomic_inc(&start_count);
0471 while (atomic_read(&start_count) != cpus)
0472 cpu_relax();
0473
0474 cur_max_warp = check_tsc_warp(loop_timeout(cpu));
0475
0476
0477
0478
0479 gbl_max_warp = max_warp;
0480
0481
0482
0483
0484 atomic_inc(&stop_count);
0485
0486
0487
0488
0489 while (atomic_read(&stop_count) != cpus)
0490 cpu_relax();
0491
0492
0493
0494
0495 atomic_set(&stop_count, 0);
0496
0497
0498
0499
0500
0501
0502 if (!atomic_read(&test_runs))
0503 return;
0504
0505
0506
0507
0508
0509
0510 if (!cur_max_warp)
0511 cur_max_warp = -gbl_max_warp;
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524 cur->adjusted += cur_max_warp;
0525
0526 pr_warn("TSC ADJUST compensate: CPU%u observed %lld warp. Adjust: %lld\n",
0527 cpu, cur_max_warp, cur->adjusted);
0528
0529 wrmsrl(MSR_IA32_TSC_ADJUST, cur->adjusted);
0530 goto retry;
0531
0532 }
0533
0534 #endif