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