Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 1 | /* |
Dave Jones | 835c34a | 2007-10-12 21:10:53 -0400 | [diff] [blame] | 2 | * check TSC synchronization. |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 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 | */ |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 17 | #include <linux/topology.h> |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/kernel.h> |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 20 | #include <linux/smp.h> |
| 21 | #include <linux/nmi.h> |
| 22 | #include <asm/tsc.h> |
| 23 | |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 24 | struct tsc_adjust { |
Thomas Gleixner | 1d0095f | 2016-11-19 13:47:37 +0000 | [diff] [blame] | 25 | s64 bootval; |
| 26 | s64 adjusted; |
| 27 | unsigned long nextcheck; |
| 28 | bool warned; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static DEFINE_PER_CPU(struct tsc_adjust, tsc_adjust); |
| 32 | |
mike.travis@hpe.com | 341102c | 2017-10-12 11:32:02 -0500 | [diff] [blame] | 33 | /* |
| 34 | * TSC's on different sockets may be reset asynchronously. |
| 35 | * This may cause the TSC ADJUST value on socket 0 to be NOT 0. |
| 36 | */ |
| 37 | bool __read_mostly tsc_async_resets; |
| 38 | |
| 39 | void mark_tsc_async_resets(char *reason) |
| 40 | { |
| 41 | if (tsc_async_resets) |
| 42 | return; |
| 43 | tsc_async_resets = true; |
| 44 | pr_info("tsc: Marking TSC async resets true due to %s\n", reason); |
| 45 | } |
| 46 | |
Thomas Gleixner | 6a36958 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 47 | void tsc_verify_tsc_adjust(bool resume) |
Thomas Gleixner | 1d0095f | 2016-11-19 13:47:37 +0000 | [diff] [blame] | 48 | { |
| 49 | struct tsc_adjust *adj = this_cpu_ptr(&tsc_adjust); |
| 50 | s64 curval; |
| 51 | |
| 52 | if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST)) |
| 53 | return; |
| 54 | |
mike.travis@hpe.com | 9514ece | 2017-10-12 11:32:03 -0500 | [diff] [blame^] | 55 | /* Skip unnecessary error messages if TSC already unstable */ |
| 56 | if (check_tsc_unstable()) |
| 57 | return; |
| 58 | |
Thomas Gleixner | 1d0095f | 2016-11-19 13:47:37 +0000 | [diff] [blame] | 59 | /* Rate limit the MSR check */ |
Thomas Gleixner | 6a36958 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 60 | if (!resume && time_before(jiffies, adj->nextcheck)) |
Thomas Gleixner | 1d0095f | 2016-11-19 13:47:37 +0000 | [diff] [blame] | 61 | return; |
| 62 | |
| 63 | adj->nextcheck = jiffies + HZ; |
| 64 | |
| 65 | rdmsrl(MSR_IA32_TSC_ADJUST, curval); |
| 66 | if (adj->adjusted == curval) |
| 67 | return; |
| 68 | |
| 69 | /* Restore the original value */ |
| 70 | wrmsrl(MSR_IA32_TSC_ADJUST, adj->adjusted); |
| 71 | |
Thomas Gleixner | 6a36958 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 72 | if (!adj->warned || resume) { |
Thomas Gleixner | 1d0095f | 2016-11-19 13:47:37 +0000 | [diff] [blame] | 73 | pr_warn(FW_BUG "TSC ADJUST differs: CPU%u %lld --> %lld. Restoring\n", |
| 74 | smp_processor_id(), adj->adjusted, curval); |
| 75 | adj->warned = true; |
| 76 | } |
| 77 | } |
| 78 | |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 79 | static void tsc_sanitize_first_cpu(struct tsc_adjust *cur, s64 bootval, |
| 80 | unsigned int cpu, bool bootcpu) |
| 81 | { |
| 82 | /* |
| 83 | * First online CPU in a package stores the boot value in the |
| 84 | * adjustment value. This value might change later via the sync |
| 85 | * mechanism. If that fails we still can yell about boot values not |
| 86 | * being consistent. |
| 87 | * |
| 88 | * On the boot cpu we just force set the ADJUST value to 0 if it's |
| 89 | * non zero. We don't do that on non boot cpus because physical |
| 90 | * hotplug should have set the ADJUST register to a value > 0 so |
| 91 | * the TSC is in sync with the already running cpus. |
mike.travis@hpe.com | 341102c | 2017-10-12 11:32:02 -0500 | [diff] [blame] | 92 | * |
| 93 | * Also don't force the ADJUST value to zero if that is a valid value |
| 94 | * for socket 0 as determined by the system arch. This is required |
| 95 | * when multiple sockets are reset asynchronously with each other |
| 96 | * and socket 0 may not have an TSC ADJUST value of 0. |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 97 | */ |
Peter Zijlstra | 855615e | 2017-05-31 17:52:04 +0200 | [diff] [blame] | 98 | if (bootcpu && bootval != 0) { |
mike.travis@hpe.com | 341102c | 2017-10-12 11:32:02 -0500 | [diff] [blame] | 99 | if (likely(!tsc_async_resets)) { |
| 100 | pr_warn(FW_BUG "TSC ADJUST: CPU%u: %lld force to 0\n", |
| 101 | cpu, bootval); |
| 102 | wrmsrl(MSR_IA32_TSC_ADJUST, 0); |
| 103 | bootval = 0; |
| 104 | } else { |
| 105 | pr_info("TSC ADJUST: CPU%u: %lld NOT forced to 0\n", |
| 106 | cpu, bootval); |
| 107 | } |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 108 | } |
| 109 | cur->adjusted = bootval; |
| 110 | } |
| 111 | |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 112 | #ifndef CONFIG_SMP |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 113 | bool __init tsc_store_and_check_tsc_adjust(bool bootcpu) |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 114 | { |
Thomas Gleixner | b836554 | 2016-11-29 20:28:31 +0100 | [diff] [blame] | 115 | struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust); |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 116 | s64 bootval; |
| 117 | |
| 118 | if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST)) |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 119 | return false; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 120 | |
mike.travis@hpe.com | 9514ece | 2017-10-12 11:32:03 -0500 | [diff] [blame^] | 121 | /* Skip unnecessary error messages if TSC already unstable */ |
| 122 | if (check_tsc_unstable()) |
| 123 | return false; |
| 124 | |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 125 | rdmsrl(MSR_IA32_TSC_ADJUST, bootval); |
| 126 | cur->bootval = bootval; |
Thomas Gleixner | 1d0095f | 2016-11-19 13:47:37 +0000 | [diff] [blame] | 127 | cur->nextcheck = jiffies + HZ; |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 128 | tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(), bootcpu); |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 129 | return false; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | #else /* !CONFIG_SMP */ |
| 133 | |
| 134 | /* |
| 135 | * Store and check the TSC ADJUST MSR if available |
| 136 | */ |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 137 | bool tsc_store_and_check_tsc_adjust(bool bootcpu) |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 138 | { |
| 139 | struct tsc_adjust *ref, *cur = this_cpu_ptr(&tsc_adjust); |
| 140 | unsigned int refcpu, cpu = smp_processor_id(); |
Thomas Gleixner | 31f8a65 | 2016-12-01 13:26:58 +0100 | [diff] [blame] | 141 | struct cpumask *mask; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 142 | s64 bootval; |
| 143 | |
| 144 | if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST)) |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 145 | return false; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 146 | |
| 147 | rdmsrl(MSR_IA32_TSC_ADJUST, bootval); |
| 148 | cur->bootval = bootval; |
Thomas Gleixner | 1d0095f | 2016-11-19 13:47:37 +0000 | [diff] [blame] | 149 | cur->nextcheck = jiffies + HZ; |
| 150 | cur->warned = false; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
mike.travis@hpe.com | 341102c | 2017-10-12 11:32:02 -0500 | [diff] [blame] | 153 | * If a non-zero TSC value for socket 0 may be valid then the default |
| 154 | * adjusted value cannot assumed to be zero either. |
| 155 | */ |
| 156 | if (tsc_async_resets) |
| 157 | cur->adjusted = bootval; |
| 158 | |
| 159 | /* |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 160 | * Check whether this CPU is the first in a package to come up. In |
| 161 | * this case do not check the boot value against another package |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 162 | * because the new package might have been physically hotplugged, |
| 163 | * where TSC_ADJUST is expected to be different. When called on the |
| 164 | * boot CPU topology_core_cpumask() might not be available yet. |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 165 | */ |
Thomas Gleixner | 31f8a65 | 2016-12-01 13:26:58 +0100 | [diff] [blame] | 166 | mask = topology_core_cpumask(cpu); |
| 167 | refcpu = mask ? cpumask_any_but(mask, cpu) : nr_cpu_ids; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 168 | |
| 169 | if (refcpu >= nr_cpu_ids) { |
Thomas Gleixner | 5bae156 | 2016-12-13 13:14:17 +0000 | [diff] [blame] | 170 | tsc_sanitize_first_cpu(cur, bootval, smp_processor_id(), |
| 171 | bootcpu); |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 172 | return false; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | ref = per_cpu_ptr(&tsc_adjust, refcpu); |
| 176 | /* |
| 177 | * Compare the boot value and complain if it differs in the |
| 178 | * package. |
| 179 | */ |
| 180 | if (bootval != ref->bootval) { |
Thomas Gleixner | 16588f6 | 2016-12-18 15:06:27 +0100 | [diff] [blame] | 181 | pr_warn(FW_BUG "TSC ADJUST differs: Reference CPU%u: %lld CPU%u: %lld\n", |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 182 | refcpu, ref->bootval, cpu, bootval); |
| 183 | } |
| 184 | /* |
| 185 | * The TSC_ADJUST values in a package must be the same. If the boot |
| 186 | * value on this newly upcoming CPU differs from the adjustment |
| 187 | * value of the already online CPU in this package, set it to that |
| 188 | * adjusted value. |
| 189 | */ |
| 190 | if (bootval != ref->adjusted) { |
| 191 | pr_warn("TSC ADJUST synchronize: Reference CPU%u: %lld CPU%u: %lld\n", |
| 192 | refcpu, ref->adjusted, cpu, bootval); |
| 193 | cur->adjusted = ref->adjusted; |
| 194 | wrmsrl(MSR_IA32_TSC_ADJUST, ref->adjusted); |
| 195 | } |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 196 | /* |
| 197 | * We have the TSCs forced to be in sync on this package. Skip sync |
| 198 | * test: |
| 199 | */ |
| 200 | return true; |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 203 | /* |
| 204 | * Entry/exit counters that make sure that both CPUs |
| 205 | * run the measurement code at once: |
| 206 | */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 207 | static atomic_t start_count; |
| 208 | static atomic_t stop_count; |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 209 | static atomic_t skip_test; |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 210 | static atomic_t test_runs; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * We use a raw spinlock in this exceptional case, because |
| 214 | * we want to have the fastest, inlined, non-debug version |
| 215 | * of a critical section, to be able to prove TSC time-warps: |
| 216 | */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 217 | static arch_spinlock_t sync_lock = __ARCH_SPIN_LOCK_UNLOCKED; |
Ingo Molnar | 643bec9 | 2009-05-07 09:12:50 +0200 | [diff] [blame] | 218 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 219 | static cycles_t last_tsc; |
| 220 | static cycles_t max_warp; |
| 221 | static int nr_warps; |
Thomas Gleixner | bec8520 | 2016-11-19 13:47:35 +0000 | [diff] [blame] | 222 | static int random_warps; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 223 | |
| 224 | /* |
Andy Lutomirski | eee6946 | 2015-06-25 18:44:09 +0200 | [diff] [blame] | 225 | * TSC-warp measurement loop running on both CPUs. This is not called |
| 226 | * if there is no TSC. |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 227 | */ |
Thomas Gleixner | 76d3b8515 | 2016-11-19 13:47:41 +0000 | [diff] [blame] | 228 | static cycles_t check_tsc_warp(unsigned int timeout) |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 229 | { |
Thomas Gleixner | 76d3b8515 | 2016-11-19 13:47:41 +0000 | [diff] [blame] | 230 | cycles_t start, now, prev, end, cur_max_warp = 0; |
Thomas Gleixner | bec8520 | 2016-11-19 13:47:35 +0000 | [diff] [blame] | 231 | int i, cur_warps = 0; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 232 | |
Andy Lutomirski | eee6946 | 2015-06-25 18:44:09 +0200 | [diff] [blame] | 233 | start = rdtsc_ordered(); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 234 | /* |
Suresh Siddha | b0e5c77 | 2012-02-06 18:32:20 -0800 | [diff] [blame] | 235 | * The measurement runs for 'timeout' msecs: |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 236 | */ |
Suresh Siddha | b0e5c77 | 2012-02-06 18:32:20 -0800 | [diff] [blame] | 237 | end = start + (cycles_t) tsc_khz * timeout; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 238 | now = start; |
| 239 | |
| 240 | for (i = 0; ; i++) { |
| 241 | /* |
| 242 | * We take the global lock, measure TSC, save the |
| 243 | * previous TSC that was measured (possibly on |
| 244 | * another CPU) and update the previous TSC timestamp. |
| 245 | */ |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 246 | arch_spin_lock(&sync_lock); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 247 | prev = last_tsc; |
Andy Lutomirski | eee6946 | 2015-06-25 18:44:09 +0200 | [diff] [blame] | 248 | now = rdtsc_ordered(); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 249 | last_tsc = now; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 250 | arch_spin_unlock(&sync_lock); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * Be nice every now and then (and also check whether |
Ingo Molnar | df43510 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 254 | * measurement is done [we also insert a 10 million |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 255 | * loops safety exit, so we dont lock up in case the |
| 256 | * TSC readout is totally broken]): |
| 257 | */ |
| 258 | if (unlikely(!(i & 7))) { |
Ingo Molnar | df43510 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 259 | if (now > end || i > 10000000) |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 260 | break; |
| 261 | cpu_relax(); |
| 262 | touch_nmi_watchdog(); |
| 263 | } |
| 264 | /* |
| 265 | * Outside the critical section we can now see whether |
| 266 | * we saw a time-warp of the TSC going backwards: |
| 267 | */ |
| 268 | if (unlikely(prev > now)) { |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 269 | arch_spin_lock(&sync_lock); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 270 | max_warp = max(max_warp, prev - now); |
Thomas Gleixner | 76d3b8515 | 2016-11-19 13:47:41 +0000 | [diff] [blame] | 271 | cur_max_warp = max_warp; |
Thomas Gleixner | bec8520 | 2016-11-19 13:47:35 +0000 | [diff] [blame] | 272 | /* |
| 273 | * Check whether this bounces back and forth. Only |
| 274 | * one CPU should observe time going backwards. |
| 275 | */ |
| 276 | if (cur_warps != nr_warps) |
| 277 | random_warps++; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 278 | nr_warps++; |
Thomas Gleixner | bec8520 | 2016-11-19 13:47:35 +0000 | [diff] [blame] | 279 | cur_warps = nr_warps; |
Thomas Gleixner | 0199c4e | 2009-12-02 20:01:25 +0100 | [diff] [blame] | 280 | arch_spin_unlock(&sync_lock); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 281 | } |
Ingo Molnar | ad8ca49 | 2008-01-30 13:33:24 +0100 | [diff] [blame] | 282 | } |
Arjan van de Ven | bde78a7 | 2008-07-08 09:51:56 -0700 | [diff] [blame] | 283 | WARN(!(now-start), |
| 284 | "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n", |
Ingo Molnar | ad8ca49 | 2008-01-30 13:33:24 +0100 | [diff] [blame] | 285 | now-start, end-start); |
Thomas Gleixner | 76d3b8515 | 2016-11-19 13:47:41 +0000 | [diff] [blame] | 286 | return cur_max_warp; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* |
Suresh Siddha | b0e5c77 | 2012-02-06 18:32:20 -0800 | [diff] [blame] | 290 | * If the target CPU coming online doesn't have any of its core-siblings |
| 291 | * online, a timeout of 20msec will be used for the TSC-warp measurement |
| 292 | * loop. Otherwise a smaller timeout of 2msec will be used, as we have some |
| 293 | * information about this socket already (and this information grows as we |
| 294 | * have more and more logical-siblings in that socket). |
| 295 | * |
| 296 | * Ideally we should be able to skip the TSC sync check on the other |
| 297 | * core-siblings, if the first logical CPU in a socket passed the sync test. |
| 298 | * But as the TSC is per-logical CPU and can potentially be modified wrongly |
| 299 | * by the bios, TSC sync test for smaller duration should be able |
| 300 | * to catch such errors. Also this will catch the condition where all the |
| 301 | * cores in the socket doesn't get reset at the same time. |
| 302 | */ |
| 303 | static inline unsigned int loop_timeout(int cpu) |
| 304 | { |
Bartosz Golaszewski | 7d79a7b | 2015-05-26 15:11:35 +0200 | [diff] [blame] | 305 | return (cpumask_weight(topology_core_cpumask(cpu)) > 1) ? 2 : 20; |
Suresh Siddha | b0e5c77 | 2012-02-06 18:32:20 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 309 | * Source CPU calls into this - it waits for the freshly booted |
| 310 | * target CPU to arrive and then starts the measurement: |
| 311 | */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 312 | void check_tsc_sync_source(int cpu) |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 313 | { |
| 314 | int cpus = 2; |
| 315 | |
| 316 | /* |
| 317 | * No need to check if we already know that the TSC is not |
Andy Lutomirski | eee6946 | 2015-06-25 18:44:09 +0200 | [diff] [blame] | 318 | * synchronized or if we have no TSC. |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 319 | */ |
| 320 | if (unsynchronized_tsc()) |
| 321 | return; |
| 322 | |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 323 | /* |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 324 | * Set the maximum number of test runs to |
| 325 | * 1 if the CPU does not provide the TSC_ADJUST MSR |
| 326 | * 3 if the MSR is available, so the target can try to adjust |
| 327 | */ |
| 328 | if (!boot_cpu_has(X86_FEATURE_TSC_ADJUST)) |
| 329 | atomic_set(&test_runs, 1); |
| 330 | else |
| 331 | atomic_set(&test_runs, 3); |
| 332 | retry: |
| 333 | /* |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 334 | * Wait for the target to start or to skip the test: |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 335 | */ |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 336 | while (atomic_read(&start_count) != cpus - 1) { |
| 337 | if (atomic_read(&skip_test) > 0) { |
| 338 | atomic_set(&skip_test, 0); |
| 339 | return; |
| 340 | } |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 341 | cpu_relax(); |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 344 | /* |
| 345 | * Trigger the target to continue into the measurement too: |
| 346 | */ |
| 347 | atomic_inc(&start_count); |
| 348 | |
Suresh Siddha | b0e5c77 | 2012-02-06 18:32:20 -0800 | [diff] [blame] | 349 | check_tsc_warp(loop_timeout(cpu)); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 350 | |
| 351 | while (atomic_read(&stop_count) != cpus-1) |
| 352 | cpu_relax(); |
| 353 | |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 354 | /* |
| 355 | * If the test was successful set the number of runs to zero and |
| 356 | * stop. If not, decrement the number of runs an check if we can |
| 357 | * retry. In case of random warps no retry is attempted. |
| 358 | */ |
| 359 | if (!nr_warps) { |
| 360 | atomic_set(&test_runs, 0); |
| 361 | |
| 362 | pr_debug("TSC synchronization [CPU#%d -> CPU#%d]: passed\n", |
| 363 | smp_processor_id(), cpu); |
| 364 | |
| 365 | } else if (atomic_dec_and_test(&test_runs) || random_warps) { |
| 366 | /* Force it to 0 if random warps brought us here */ |
| 367 | atomic_set(&test_runs, 0); |
| 368 | |
Mike Travis | 9b3660a | 2009-11-17 18:22:16 -0600 | [diff] [blame] | 369 | pr_warning("TSC synchronization [CPU#%d -> CPU#%d]:\n", |
| 370 | smp_processor_id(), cpu); |
Ingo Molnar | 643bec9 | 2009-05-07 09:12:50 +0200 | [diff] [blame] | 371 | pr_warning("Measured %Ld cycles TSC warp between CPUs, " |
| 372 | "turning off TSC clock.\n", max_warp); |
Thomas Gleixner | bec8520 | 2016-11-19 13:47:35 +0000 | [diff] [blame] | 373 | if (random_warps) |
| 374 | pr_warning("TSC warped randomly between CPUs\n"); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 375 | mark_tsc_unstable("check_tsc_sync_source failed"); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* |
Mike Galbraith | 4c6b8b4 | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 379 | * Reset it - just in case we boot another CPU later: |
| 380 | */ |
| 381 | atomic_set(&start_count, 0); |
Thomas Gleixner | bec8520 | 2016-11-19 13:47:35 +0000 | [diff] [blame] | 382 | random_warps = 0; |
Mike Galbraith | 4c6b8b4 | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 383 | nr_warps = 0; |
| 384 | max_warp = 0; |
| 385 | last_tsc = 0; |
| 386 | |
| 387 | /* |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 388 | * Let the target continue with the bootup: |
| 389 | */ |
| 390 | atomic_inc(&stop_count); |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | * Retry, if there is a chance to do so. |
| 394 | */ |
| 395 | if (atomic_read(&test_runs) > 0) |
| 396 | goto retry; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Freshly booted CPUs call into this: |
| 401 | */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 402 | void check_tsc_sync_target(void) |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 403 | { |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 404 | struct tsc_adjust *cur = this_cpu_ptr(&tsc_adjust); |
| 405 | unsigned int cpu = smp_processor_id(); |
| 406 | cycles_t cur_max_warp, gbl_max_warp; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 407 | int cpus = 2; |
| 408 | |
Andy Lutomirski | eee6946 | 2015-06-25 18:44:09 +0200 | [diff] [blame] | 409 | /* Also aborts if there is no TSC. */ |
Thomas Gleixner | 5f2e71e | 2017-02-09 16:08:42 +0100 | [diff] [blame] | 410 | if (unsynchronized_tsc()) |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 411 | return; |
| 412 | |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 413 | /* |
| 414 | * Store, verify and sanitize the TSC adjust register. If |
| 415 | * successful skip the test. |
Thomas Gleixner | 5f2e71e | 2017-02-09 16:08:42 +0100 | [diff] [blame] | 416 | * |
| 417 | * The test is also skipped when the TSC is marked reliable. This |
| 418 | * is true for SoCs which have no fallback clocksource. On these |
| 419 | * SoCs the TSC is frequency synchronized, but still the TSC ADJUST |
| 420 | * register might have been wreckaged by the BIOS.. |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 421 | */ |
Thomas Gleixner | 5f2e71e | 2017-02-09 16:08:42 +0100 | [diff] [blame] | 422 | if (tsc_store_and_check_tsc_adjust(false) || tsc_clocksource_reliable) { |
Thomas Gleixner | a36f513 | 2016-11-19 13:47:39 +0000 | [diff] [blame] | 423 | atomic_inc(&skip_test); |
| 424 | return; |
| 425 | } |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 426 | |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 427 | retry: |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 428 | /* |
| 429 | * Register this CPU's participation and wait for the |
| 430 | * source CPU to start the measurement: |
| 431 | */ |
| 432 | atomic_inc(&start_count); |
| 433 | while (atomic_read(&start_count) != cpus) |
| 434 | cpu_relax(); |
| 435 | |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 436 | cur_max_warp = check_tsc_warp(loop_timeout(cpu)); |
| 437 | |
| 438 | /* |
| 439 | * Store the maximum observed warp value for a potential retry: |
| 440 | */ |
| 441 | gbl_max_warp = max_warp; |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * Ok, we are done: |
| 445 | */ |
| 446 | atomic_inc(&stop_count); |
| 447 | |
| 448 | /* |
| 449 | * Wait for the source CPU to print stuff: |
| 450 | */ |
| 451 | while (atomic_read(&stop_count) != cpus) |
| 452 | cpu_relax(); |
Thomas Gleixner | 4c5e3c6 | 2016-11-19 13:47:40 +0000 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * Reset it for the next sync test: |
| 456 | */ |
| 457 | atomic_set(&stop_count, 0); |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 458 | |
| 459 | /* |
| 460 | * Check the number of remaining test runs. If not zero, the test |
| 461 | * failed and a retry with adjusted TSC is possible. If zero the |
| 462 | * test was either successful or failed terminally. |
| 463 | */ |
| 464 | if (!atomic_read(&test_runs)) |
| 465 | return; |
| 466 | |
| 467 | /* |
| 468 | * If the warp value of this CPU is 0, then the other CPU |
| 469 | * observed time going backwards so this TSC was ahead and |
| 470 | * needs to move backwards. |
| 471 | */ |
| 472 | if (!cur_max_warp) |
| 473 | cur_max_warp = -gbl_max_warp; |
| 474 | |
| 475 | /* |
| 476 | * Add the result to the previous adjustment value. |
| 477 | * |
| 478 | * The adjustement value is slightly off by the overhead of the |
| 479 | * sync mechanism (observed values are ~200 TSC cycles), but this |
| 480 | * really depends on CPU, node distance and frequency. So |
| 481 | * compensating for this is hard to get right. Experiments show |
| 482 | * that the warp is not longer detectable when the observed warp |
| 483 | * value is used. In the worst case the adjustment needs to go |
| 484 | * through a 3rd run for fine tuning. |
| 485 | */ |
| 486 | cur->adjusted += cur_max_warp; |
Thomas Gleixner | 8c9b9d8 | 2016-12-18 15:09:29 +0100 | [diff] [blame] | 487 | |
Thomas Gleixner | cc4db26 | 2016-11-19 13:47:43 +0000 | [diff] [blame] | 488 | pr_warn("TSC ADJUST compensate: CPU%u observed %lld warp. Adjust: %lld\n", |
| 489 | cpu, cur_max_warp, cur->adjusted); |
| 490 | |
| 491 | wrmsrl(MSR_IA32_TSC_ADJUST, cur->adjusted); |
| 492 | goto retry; |
| 493 | |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 494 | } |
Thomas Gleixner | 8b223bc | 2016-11-19 13:47:36 +0000 | [diff] [blame] | 495 | |
| 496 | #endif /* CONFIG_SMP */ |