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 | */ |
| 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 | */ |
| 28 | static __cpuinitdata atomic_t start_count; |
| 29 | static __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 | */ |
| 36 | static __cpuinitdata raw_spinlock_t sync_lock = __RAW_SPIN_LOCK_UNLOCKED; |
| 37 | static __cpuinitdata cycles_t last_tsc; |
| 38 | static __cpuinitdata cycles_t max_warp; |
| 39 | static __cpuinitdata int nr_warps; |
| 40 | |
| 41 | /* |
| 42 | * TSC-warp measurement loop running on both CPUs: |
| 43 | */ |
| 44 | static __cpuinit void check_tsc_warp(void) |
| 45 | { |
| 46 | cycles_t start, now, prev, end; |
| 47 | int i; |
| 48 | |
Andi Kleen | 6d63de8 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 49 | start = get_cycles(); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 50 | /* |
| 51 | * The measurement runs for 20 msecs: |
| 52 | */ |
| 53 | end = start + tsc_khz * 20ULL; |
| 54 | now = start; |
| 55 | |
| 56 | for (i = 0; ; i++) { |
| 57 | /* |
| 58 | * We take the global lock, measure TSC, save the |
| 59 | * previous TSC that was measured (possibly on |
| 60 | * another CPU) and update the previous TSC timestamp. |
| 61 | */ |
| 62 | __raw_spin_lock(&sync_lock); |
| 63 | prev = last_tsc; |
Andi Kleen | 6d63de8 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 64 | now = get_cycles(); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 65 | last_tsc = now; |
| 66 | __raw_spin_unlock(&sync_lock); |
| 67 | |
| 68 | /* |
| 69 | * Be nice every now and then (and also check whether |
Ingo Molnar | df43510 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 70 | * measurement is done [we also insert a 10 million |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 71 | * loops safety exit, so we dont lock up in case the |
| 72 | * TSC readout is totally broken]): |
| 73 | */ |
| 74 | if (unlikely(!(i & 7))) { |
Ingo Molnar | df43510 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 75 | if (now > end || i > 10000000) |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 76 | break; |
| 77 | cpu_relax(); |
| 78 | touch_nmi_watchdog(); |
| 79 | } |
| 80 | /* |
| 81 | * Outside the critical section we can now see whether |
| 82 | * we saw a time-warp of the TSC going backwards: |
| 83 | */ |
| 84 | if (unlikely(prev > now)) { |
| 85 | __raw_spin_lock(&sync_lock); |
| 86 | max_warp = max(max_warp, prev - now); |
| 87 | nr_warps++; |
| 88 | __raw_spin_unlock(&sync_lock); |
| 89 | } |
Ingo Molnar | ad8ca49 | 2008-01-30 13:33:24 +0100 | [diff] [blame] | 90 | } |
Arjan van de Ven | bde78a7 | 2008-07-08 09:51:56 -0700 | [diff] [blame] | 91 | WARN(!(now-start), |
| 92 | "Warning: zero tsc calibration delta: %Ld [max: %Ld]\n", |
Ingo Molnar | ad8ca49 | 2008-01-30 13:33:24 +0100 | [diff] [blame] | 93 | now-start, end-start); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Source CPU calls into this - it waits for the freshly booted |
| 98 | * target CPU to arrive and then starts the measurement: |
| 99 | */ |
| 100 | void __cpuinit check_tsc_sync_source(int cpu) |
| 101 | { |
| 102 | int cpus = 2; |
| 103 | |
| 104 | /* |
| 105 | * No need to check if we already know that the TSC is not |
| 106 | * synchronized: |
| 107 | */ |
| 108 | if (unsynchronized_tsc()) |
| 109 | return; |
| 110 | |
Alok Kataria | eca0cd0 | 2008-10-31 12:01:58 -0700 | [diff] [blame^] | 111 | if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) { |
| 112 | printk(KERN_INFO |
| 113 | "Skipping synchronization checks as TSC is reliable.\n"); |
| 114 | return; |
| 115 | } |
| 116 | |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 117 | printk(KERN_INFO "checking TSC synchronization [CPU#%d -> CPU#%d]:", |
| 118 | smp_processor_id(), cpu); |
| 119 | |
| 120 | /* |
| 121 | * Reset it - in case this is a second bootup: |
| 122 | */ |
| 123 | atomic_set(&stop_count, 0); |
| 124 | |
| 125 | /* |
| 126 | * Wait for the target to arrive: |
| 127 | */ |
| 128 | while (atomic_read(&start_count) != cpus-1) |
| 129 | cpu_relax(); |
| 130 | /* |
| 131 | * Trigger the target to continue into the measurement too: |
| 132 | */ |
| 133 | atomic_inc(&start_count); |
| 134 | |
| 135 | check_tsc_warp(); |
| 136 | |
| 137 | while (atomic_read(&stop_count) != cpus-1) |
| 138 | cpu_relax(); |
| 139 | |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 140 | if (nr_warps) { |
| 141 | printk("\n"); |
| 142 | printk(KERN_WARNING "Measured %Ld cycles TSC warp between CPUs," |
| 143 | " turning off TSC clock.\n", max_warp); |
| 144 | mark_tsc_unstable("check_tsc_sync_source failed"); |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 145 | } else { |
| 146 | printk(" passed.\n"); |
| 147 | } |
| 148 | |
| 149 | /* |
Mike Galbraith | 4c6b8b4 | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 150 | * Reset it - just in case we boot another CPU later: |
| 151 | */ |
| 152 | atomic_set(&start_count, 0); |
| 153 | nr_warps = 0; |
| 154 | max_warp = 0; |
| 155 | last_tsc = 0; |
| 156 | |
| 157 | /* |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 158 | * Let the target continue with the bootup: |
| 159 | */ |
| 160 | atomic_inc(&stop_count); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Freshly booted CPUs call into this: |
| 165 | */ |
| 166 | void __cpuinit check_tsc_sync_target(void) |
| 167 | { |
| 168 | int cpus = 2; |
| 169 | |
Alok Kataria | eca0cd0 | 2008-10-31 12:01:58 -0700 | [diff] [blame^] | 170 | if (unsynchronized_tsc() || boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) |
Thomas Gleixner | 250c227 | 2007-10-11 11:17:24 +0200 | [diff] [blame] | 171 | return; |
| 172 | |
| 173 | /* |
| 174 | * Register this CPU's participation and wait for the |
| 175 | * source CPU to start the measurement: |
| 176 | */ |
| 177 | atomic_inc(&start_count); |
| 178 | while (atomic_read(&start_count) != cpus) |
| 179 | cpu_relax(); |
| 180 | |
| 181 | check_tsc_warp(); |
| 182 | |
| 183 | /* |
| 184 | * Ok, we are done: |
| 185 | */ |
| 186 | atomic_inc(&stop_count); |
| 187 | |
| 188 | /* |
| 189 | * Wait for the source CPU to print stuff: |
| 190 | */ |
| 191 | while (atomic_read(&stop_count) != cpus) |
| 192 | cpu_relax(); |
| 193 | } |
| 194 | #undef NR_LOOPS |
| 195 | |