allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / x86_64 / kernel / tsc_sync.c
blob355f5f506c8133d2a97108ae211f52839199510b
1 /*
2 * arch/x86_64/kernel/tsc_sync.c: check TSC synchronization.
4 * Copyright (C) 2006, Red Hat, Inc., Ingo Molnar
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.
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'.
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. )
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>
25 * Entry/exit counters that make sure that both CPUs
26 * run the measurement code at once:
28 static __cpuinitdata atomic_t start_count;
29 static __cpuinitdata atomic_t stop_count;
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:
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;
42 * TSC-warp measurement loop running on both CPUs:
44 static __cpuinit void check_tsc_warp(void)
46 cycles_t start, now, prev, end;
47 int i;
49 start = get_cycles_sync();
51 * The measurement runs for 20 msecs:
53 end = start + tsc_khz * 20ULL;
54 now = start;
56 for (i = 0; ; i++) {
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.
62 __raw_spin_lock(&sync_lock);
63 prev = last_tsc;
64 now = get_cycles_sync();
65 last_tsc = now;
66 __raw_spin_unlock(&sync_lock);
69 * Be nice every now and then (and also check whether
70 * measurement is done [we also insert a 100 million
71 * loops safety exit, so we dont lock up in case the
72 * TSC readout is totally broken]):
74 if (unlikely(!(i & 7))) {
75 if (now > end || i > 100000000)
76 break;
77 cpu_relax();
78 touch_nmi_watchdog();
81 * Outside the critical section we can now see whether
82 * we saw a time-warp of the TSC going backwards:
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);
95 * Source CPU calls into this - it waits for the freshly booted
96 * target CPU to arrive and then starts the measurement:
98 void __cpuinit check_tsc_sync_source(int cpu)
100 int cpus = 2;
103 * No need to check if we already know that the TSC is not
104 * synchronized:
106 if (unsynchronized_tsc())
107 return;
109 printk(KERN_INFO "checking TSC synchronization [CPU#%d -> CPU#%d]:",
110 smp_processor_id(), cpu);
113 * Reset it - in case this is a second bootup:
115 atomic_set(&stop_count, 0);
118 * Wait for the target to arrive:
120 while (atomic_read(&start_count) != cpus-1)
121 cpu_relax();
123 * Trigger the target to continue into the measurement too:
125 atomic_inc(&start_count);
127 check_tsc_warp();
129 while (atomic_read(&stop_count) != cpus-1)
130 cpu_relax();
133 * Reset it - just in case we boot another CPU later:
135 atomic_set(&start_count, 0);
137 if (nr_warps) {
138 printk("\n");
139 printk(KERN_WARNING "Measured %Ld cycles TSC warp between CPUs,"
140 " turning off TSC clock.\n", max_warp);
141 mark_tsc_unstable("check_tsc_sync_source failed");
142 nr_warps = 0;
143 max_warp = 0;
144 last_tsc = 0;
145 } else {
146 printk(" passed.\n");
150 * Let the target continue with the bootup:
152 atomic_inc(&stop_count);
156 * Freshly booted CPUs call into this:
158 void __cpuinit check_tsc_sync_target(void)
160 int cpus = 2;
162 if (unsynchronized_tsc())
163 return;
166 * Register this CPU's participation and wait for the
167 * source CPU to start the measurement:
169 atomic_inc(&start_count);
170 while (atomic_read(&start_count) != cpus)
171 cpu_relax();
173 check_tsc_warp();
176 * Ok, we are done:
178 atomic_inc(&stop_count);
181 * Wait for the source CPU to print stuff:
183 while (atomic_read(&stop_count) != cpus)
184 cpu_relax();
186 #undef NR_LOOPS