Ingo Molnar | bb29ab2 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 1 | #include <linux/sched.h> |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 2 | #include <linux/clocksource.h> |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 3 | #include <linux/workqueue.h> |
Ingo Molnar | 6ff10de | 2008-06-24 01:19:49 +0200 | [diff] [blame] | 4 | #include <linux/delay.h> |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 5 | #include <linux/cpufreq.h> |
| 6 | #include <linux/jiffies.h> |
| 7 | #include <linux/init.h> |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 8 | #include <linux/dmi.h> |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 9 | #include <linux/percpu.h> |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 10 | |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 11 | #include <asm/delay.h> |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 12 | #include <asm/tsc.h> |
| 13 | #include <asm/io.h> |
Zachary Amsden | 6cb9a83 | 2007-03-05 00:30:35 -0800 | [diff] [blame] | 14 | #include <asm/timer.h> |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 15 | |
| 16 | #include "mach_timer.h" |
| 17 | |
Thomas Gleixner | 9ccc906 | 2008-05-13 12:31:00 +0200 | [diff] [blame] | 18 | static int tsc_disabled; |
Thomas Gleixner | d9a5c0a | 2007-03-24 23:02:49 +0100 | [diff] [blame] | 19 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 20 | /* |
| 21 | * On some systems the TSC frequency does not |
| 22 | * change with the cpu frequency. So we need |
| 23 | * an extra value to store the TSC freq |
| 24 | */ |
| 25 | unsigned int tsc_khz; |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 26 | EXPORT_SYMBOL_GPL(tsc_khz); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 27 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 28 | #ifdef CONFIG_X86_TSC |
| 29 | static int __init tsc_setup(char *str) |
| 30 | { |
| 31 | printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, " |
Thomas Gleixner | 9ccc906 | 2008-05-13 12:31:00 +0200 | [diff] [blame] | 32 | "cannot disable TSC completely.\n"); |
| 33 | tsc_disabled = 1; |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 34 | return 1; |
| 35 | } |
| 36 | #else |
| 37 | /* |
| 38 | * disable flag for tsc. Takes effect by clearing the TSC cpu flag |
| 39 | * in cpu/common.c |
| 40 | */ |
| 41 | static int __init tsc_setup(char *str) |
| 42 | { |
Andi Kleen | 404ee5b | 2008-01-30 13:33:20 +0100 | [diff] [blame] | 43 | setup_clear_cpu_cap(X86_FEATURE_TSC); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 44 | return 1; |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | __setup("notsc", tsc_setup); |
| 49 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 50 | /* |
| 51 | * code to mark and check if the TSC is unstable |
| 52 | * due to cpufreq or due to unsynced TSCs |
| 53 | */ |
| 54 | static int tsc_unstable; |
| 55 | |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 56 | int check_tsc_unstable(void) |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 57 | { |
| 58 | return tsc_unstable; |
| 59 | } |
Rusty Russell | d7e28ff | 2007-07-19 01:49:23 -0700 | [diff] [blame] | 60 | EXPORT_SYMBOL_GPL(check_tsc_unstable); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 61 | |
Simon Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 62 | /* Accelerators for sched_clock() |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 63 | * convert from cycles(64bits) => nanoseconds (64bits) |
| 64 | * basic equation: |
| 65 | * ns = cycles / (freq / ns_per_sec) |
| 66 | * ns = cycles * (ns_per_sec / freq) |
| 67 | * ns = cycles * (10^9 / (cpu_khz * 10^3)) |
| 68 | * ns = cycles * (10^6 / cpu_khz) |
| 69 | * |
| 70 | * Then we use scaling math (suggested by george@mvista.com) to get: |
| 71 | * ns = cycles * (10^6 * SC / cpu_khz) / SC |
| 72 | * ns = cycles * cyc2ns_scale / SC |
| 73 | * |
| 74 | * And since SC is a constant power of two, we can convert the div |
| 75 | * into a shift. |
| 76 | * |
Josh Triplett | 9631512 | 2007-10-20 02:23:49 +0200 | [diff] [blame] | 77 | * We can use khz divisor instead of mhz to keep a better precision, since |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 78 | * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits. |
| 79 | * (mathieu.desnoyers@polymtl.ca) |
| 80 | * |
| 81 | * -johnstul@us.ibm.com "math is hard, lets go shopping!" |
| 82 | */ |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 83 | |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 84 | DEFINE_PER_CPU(unsigned long, cyc2ns); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 85 | |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 86 | static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu) |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 87 | { |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 88 | unsigned long long tsc_now, ns_now; |
Ingo Molnar | 1725037f | 2008-03-31 14:52:15 +0200 | [diff] [blame] | 89 | unsigned long flags, *scale; |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 90 | |
| 91 | local_irq_save(flags); |
| 92 | sched_clock_idle_sleep_event(); |
| 93 | |
| 94 | scale = &per_cpu(cyc2ns, cpu); |
| 95 | |
| 96 | rdtscll(tsc_now); |
| 97 | ns_now = __cycles_2_ns(tsc_now); |
| 98 | |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 99 | if (cpu_khz) |
| 100 | *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz; |
| 101 | |
| 102 | /* |
| 103 | * Start smoothly with the new frequency: |
| 104 | */ |
| 105 | sched_clock_idle_wakeup_event(0); |
| 106 | local_irq_restore(flags); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 107 | } |
| 108 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 109 | /* |
| 110 | * Scheduler clock - returns current time in nanosec units. |
| 111 | */ |
Jeremy Fitzhardinge | 688340e | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 112 | unsigned long long native_sched_clock(void) |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 113 | { |
| 114 | unsigned long long this_offset; |
| 115 | |
| 116 | /* |
Ingo Molnar | f969098 | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 117 | * Fall back to jiffies if there's no TSC available: |
Ingo Molnar | bb29ab2 | 2007-07-09 18:51:59 +0200 | [diff] [blame] | 118 | * ( But note that we still use it if the TSC is marked |
| 119 | * unstable. We do this because unlike Time Of Day, |
| 120 | * the scheduler clock tolerates small errors and it's |
| 121 | * very important for it to be as fast as the platform |
| 122 | * can achive it. ) |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 123 | */ |
Thomas Gleixner | 9ccc906 | 2008-05-13 12:31:00 +0200 | [diff] [blame] | 124 | if (unlikely(tsc_disabled)) |
Ingo Molnar | f969098 | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 125 | /* No locking but a rare wrong value is not a big deal: */ |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 126 | return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ); |
| 127 | |
| 128 | /* read the Time Stamp Counter: */ |
Jeremy Fitzhardinge | 688340e | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 129 | rdtscll(this_offset); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 130 | |
| 131 | /* return the value in ns */ |
| 132 | return cycles_2_ns(this_offset); |
| 133 | } |
| 134 | |
Jeremy Fitzhardinge | 688340e | 2007-07-17 18:37:04 -0700 | [diff] [blame] | 135 | /* We need to define a real function for sched_clock, to override the |
| 136 | weak default version */ |
| 137 | #ifdef CONFIG_PARAVIRT |
| 138 | unsigned long long sched_clock(void) |
| 139 | { |
| 140 | return paravirt_sched_clock(); |
| 141 | } |
| 142 | #else |
| 143 | unsigned long long sched_clock(void) |
| 144 | __attribute__((alias("native_sched_clock"))); |
| 145 | #endif |
| 146 | |
Zachary Amsden | 1182d85 | 2007-03-05 00:30:36 -0800 | [diff] [blame] | 147 | unsigned long native_calculate_cpu_khz(void) |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 148 | { |
| 149 | unsigned long long start, end; |
| 150 | unsigned long count; |
Dave Johnson | edaf420 | 2007-10-23 22:37:22 +0200 | [diff] [blame] | 151 | u64 delta64 = (u64)ULLONG_MAX; |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 152 | int i; |
| 153 | unsigned long flags; |
| 154 | |
| 155 | local_irq_save(flags); |
| 156 | |
Dave Johnson | 8c66006 | 2007-10-23 22:37:22 +0200 | [diff] [blame] | 157 | /* run 3 times to ensure the cache is warm and to get an accurate reading */ |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 158 | for (i = 0; i < 3; i++) { |
| 159 | mach_prepare_counter(); |
| 160 | rdtscll(start); |
| 161 | mach_countup(&count); |
| 162 | rdtscll(end); |
Dave Johnson | 8c66006 | 2007-10-23 22:37:22 +0200 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * Error: ECTCNEVERSET |
| 166 | * The CTC wasn't reliable: we got a hit on the very first read, |
| 167 | * or the CPU was so fast/slow that the quotient wouldn't fit in |
| 168 | * 32 bits.. |
| 169 | */ |
| 170 | if (count <= 1) |
| 171 | continue; |
| 172 | |
| 173 | /* cpu freq too slow: */ |
| 174 | if ((end - start) <= CALIBRATE_TIME_MSEC) |
| 175 | continue; |
| 176 | |
| 177 | /* |
| 178 | * We want the minimum time of all runs in case one of them |
| 179 | * is inaccurate due to SMI or other delay |
| 180 | */ |
Dave Johnson | edaf420 | 2007-10-23 22:37:22 +0200 | [diff] [blame] | 181 | delta64 = min(delta64, (end - start)); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 182 | } |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 183 | |
Dave Johnson | 8c66006 | 2007-10-23 22:37:22 +0200 | [diff] [blame] | 184 | /* cpu freq too fast (or every run was bad): */ |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 185 | if (delta64 > (1ULL<<32)) |
| 186 | goto err; |
| 187 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 188 | delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */ |
| 189 | do_div(delta64,CALIBRATE_TIME_MSEC); |
| 190 | |
| 191 | local_irq_restore(flags); |
| 192 | return (unsigned long)delta64; |
| 193 | err: |
| 194 | local_irq_restore(flags); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int recalibrate_cpu_khz(void) |
| 199 | { |
| 200 | #ifndef CONFIG_SMP |
| 201 | unsigned long cpu_khz_old = cpu_khz; |
| 202 | |
| 203 | if (cpu_has_tsc) { |
| 204 | cpu_khz = calculate_cpu_khz(); |
| 205 | tsc_khz = cpu_khz; |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 206 | cpu_data(0).loops_per_jiffy = |
| 207 | cpufreq_scale(cpu_data(0).loops_per_jiffy, |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 208 | cpu_khz_old, cpu_khz); |
| 209 | return 0; |
| 210 | } else |
| 211 | return -ENODEV; |
| 212 | #else |
| 213 | return -ENODEV; |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | EXPORT_SYMBOL(recalibrate_cpu_khz); |
| 218 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 219 | #ifdef CONFIG_CPU_FREQ |
| 220 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 221 | /* |
| 222 | * if the CPU frequency is scaled, TSC-based delays will need a different |
| 223 | * loops_per_jiffy value to function properly. |
| 224 | */ |
Pavel Machek | 4bd0160 | 2008-02-19 11:02:30 +0100 | [diff] [blame] | 225 | static unsigned int ref_freq; |
| 226 | static unsigned long loops_per_jiffy_ref; |
| 227 | static unsigned long cpu_khz_ref; |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 228 | |
| 229 | static int |
| 230 | time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data) |
| 231 | { |
| 232 | struct cpufreq_freqs *freq = data; |
| 233 | |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 234 | if (!ref_freq) { |
| 235 | if (!freq->old){ |
| 236 | ref_freq = freq->new; |
Daniel Walker | df3624a | 2007-05-02 19:27:18 +0200 | [diff] [blame] | 237 | return 0; |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 238 | } |
| 239 | ref_freq = freq->old; |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 240 | loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy; |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 241 | cpu_khz_ref = cpu_khz; |
| 242 | } |
| 243 | |
| 244 | if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) || |
| 245 | (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) || |
| 246 | (val == CPUFREQ_RESUMECHANGE)) { |
| 247 | if (!(freq->flags & CPUFREQ_CONST_LOOPS)) |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 248 | cpu_data(freq->cpu).loops_per_jiffy = |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 249 | cpufreq_scale(loops_per_jiffy_ref, |
| 250 | ref_freq, freq->new); |
| 251 | |
| 252 | if (cpu_khz) { |
| 253 | |
| 254 | if (num_online_cpus() == 1) |
| 255 | cpu_khz = cpufreq_scale(cpu_khz_ref, |
| 256 | ref_freq, freq->new); |
| 257 | if (!(freq->flags & CPUFREQ_CONST_LOOPS)) { |
| 258 | tsc_khz = cpu_khz; |
Karsten Wiese | 4f41c94 | 2008-04-07 12:14:45 +0200 | [diff] [blame] | 259 | set_cyc2ns_scale(cpu_khz, freq->cpu); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 260 | /* |
| 261 | * TSC based sched_clock turns |
| 262 | * to junk w/ cpufreq |
| 263 | */ |
john stultz | 5a90cf2 | 2007-05-02 19:27:08 +0200 | [diff] [blame] | 264 | mark_tsc_unstable("cpufreq changes"); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | } |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static struct notifier_block time_cpufreq_notifier_block = { |
| 273 | .notifier_call = time_cpufreq_notifier |
| 274 | }; |
| 275 | |
| 276 | static int __init cpufreq_tsc(void) |
| 277 | { |
Thomas Gleixner | 26a08eb | 2007-02-16 01:27:32 -0800 | [diff] [blame] | 278 | return cpufreq_register_notifier(&time_cpufreq_notifier_block, |
| 279 | CPUFREQ_TRANSITION_NOTIFIER); |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 280 | } |
john stultz | 539eb11 | 2006-06-26 00:25:10 -0700 | [diff] [blame] | 281 | core_initcall(cpufreq_tsc); |
| 282 | |
| 283 | #endif |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 284 | |
| 285 | /* clock source code */ |
| 286 | |
Pavel Machek | 4bd0160 | 2008-02-19 11:02:30 +0100 | [diff] [blame] | 287 | static unsigned long current_tsc_khz; |
Thomas Gleixner | d8bb6f4 | 2008-04-01 19:45:18 +0200 | [diff] [blame] | 288 | static struct clocksource clocksource_tsc; |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 289 | |
Thomas Gleixner | d8bb6f4 | 2008-04-01 19:45:18 +0200 | [diff] [blame] | 290 | /* |
| 291 | * We compare the TSC to the cycle_last value in the clocksource |
| 292 | * structure to avoid a nasty time-warp issue. This can be observed in |
| 293 | * a very small window right after one CPU updated cycle_last under |
| 294 | * xtime lock and the other CPU reads a TSC value which is smaller |
| 295 | * than the cycle_last reference value due to a TSC which is slighty |
| 296 | * behind. This delta is nowhere else observable, but in that case it |
| 297 | * results in a forward time jump in the range of hours due to the |
| 298 | * unsigned delta calculation of the time keeping core code, which is |
| 299 | * necessary to support wrapping clocksources like pm timer. |
| 300 | */ |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 301 | static cycle_t read_tsc(void) |
| 302 | { |
| 303 | cycle_t ret; |
| 304 | |
| 305 | rdtscll(ret); |
| 306 | |
Thomas Gleixner | d8bb6f4 | 2008-04-01 19:45:18 +0200 | [diff] [blame] | 307 | return ret >= clocksource_tsc.cycle_last ? |
| 308 | ret : clocksource_tsc.cycle_last; |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static struct clocksource clocksource_tsc = { |
| 312 | .name = "tsc", |
| 313 | .rating = 300, |
| 314 | .read = read_tsc, |
Jim Cromie | 7f9f303 | 2006-06-26 00:25:15 -0700 | [diff] [blame] | 315 | .mask = CLOCKSOURCE_MASK(64), |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 316 | .mult = 0, /* to be set */ |
| 317 | .shift = 22, |
Thomas Gleixner | 73b08d2 | 2007-02-16 01:27:36 -0800 | [diff] [blame] | 318 | .flags = CLOCK_SOURCE_IS_CONTINUOUS | |
| 319 | CLOCK_SOURCE_MUST_VERIFY, |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 320 | }; |
| 321 | |
john stultz | 5a90cf2 | 2007-05-02 19:27:08 +0200 | [diff] [blame] | 322 | void mark_tsc_unstable(char *reason) |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 323 | { |
Thomas Gleixner | 7e69f2b | 2007-02-16 01:27:42 -0800 | [diff] [blame] | 324 | if (!tsc_unstable) { |
| 325 | tsc_unstable = 1; |
john stultz | 5a90cf2 | 2007-05-02 19:27:08 +0200 | [diff] [blame] | 326 | printk("Marking TSC unstable due to: %s.\n", reason); |
Thomas Gleixner | 7e69f2b | 2007-02-16 01:27:42 -0800 | [diff] [blame] | 327 | /* Can be called before registration */ |
| 328 | if (clocksource_tsc.mult) |
| 329 | clocksource_change_rating(&clocksource_tsc, 0); |
| 330 | else |
| 331 | clocksource_tsc.rating = 0; |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 332 | } |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 333 | } |
Thomas Gleixner | 7e69f2b | 2007-02-16 01:27:42 -0800 | [diff] [blame] | 334 | EXPORT_SYMBOL_GPL(mark_tsc_unstable); |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 335 | |
Jeff Garzik | 1855256 | 2007-10-03 15:15:40 -0400 | [diff] [blame] | 336 | static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d) |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 337 | { |
| 338 | printk(KERN_NOTICE "%s detected: marking TSC unstable.\n", |
Thomas Gleixner | 9ccc906 | 2008-05-13 12:31:00 +0200 | [diff] [blame] | 339 | d->ident); |
Thomas Gleixner | 7e69f2b | 2007-02-16 01:27:42 -0800 | [diff] [blame] | 340 | tsc_unstable = 1; |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /* List of systems that have known TSC problems */ |
| 345 | static struct dmi_system_id __initdata bad_tsc_dmi_table[] = { |
| 346 | { |
| 347 | .callback = dmi_mark_tsc_unstable, |
| 348 | .ident = "IBM Thinkpad 380XD", |
| 349 | .matches = { |
| 350 | DMI_MATCH(DMI_BOARD_VENDOR, "IBM"), |
| 351 | DMI_MATCH(DMI_BOARD_NAME, "2635FA0"), |
| 352 | }, |
| 353 | }, |
| 354 | {} |
| 355 | }; |
| 356 | |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 357 | /* |
| 358 | * Make an educated guess if the TSC is trustworthy and synchronized |
| 359 | * over all CPUs. |
| 360 | */ |
Ingo Molnar | 95492e4 | 2007-02-16 01:27:34 -0800 | [diff] [blame] | 361 | __cpuinit int unsynchronized_tsc(void) |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 362 | { |
Ingo Molnar | 95492e4 | 2007-02-16 01:27:34 -0800 | [diff] [blame] | 363 | if (!cpu_has_tsc || tsc_unstable) |
| 364 | return 1; |
Andi Kleen | 51fc97b | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 365 | |
| 366 | /* Anything with constant TSC should be synchronized */ |
| 367 | if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
| 368 | return 0; |
| 369 | |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 370 | /* |
| 371 | * Intel systems are normally all synchronized. |
| 372 | * Exceptions must mark TSC as unstable: |
| 373 | */ |
Thomas Gleixner | 7e69f2b | 2007-02-16 01:27:42 -0800 | [diff] [blame] | 374 | if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) { |
| 375 | /* assume multi socket systems are not synchronized: */ |
| 376 | if (num_possible_cpus() > 1) |
| 377 | tsc_unstable = 1; |
| 378 | } |
| 379 | return tsc_unstable; |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Marcelo Tosatti | 07190a0 | 2007-02-16 01:27:44 -0800 | [diff] [blame] | 382 | /* |
| 383 | * Geode_LX - the OLPC CPU has a possibly a very reliable TSC |
| 384 | */ |
| 385 | #ifdef CONFIG_MGEODE_LX |
| 386 | /* RTSC counts during suspend */ |
| 387 | #define RTSC_SUSP 0x100 |
| 388 | |
| 389 | static void __init check_geode_tsc_reliable(void) |
| 390 | { |
Ingo Molnar | f97586b | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 391 | unsigned long res_low, res_high; |
Marcelo Tosatti | 07190a0 | 2007-02-16 01:27:44 -0800 | [diff] [blame] | 392 | |
Ingo Molnar | f97586b | 2007-10-17 18:04:34 +0200 | [diff] [blame] | 393 | rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high); |
| 394 | if (res_low & RTSC_SUSP) |
Marcelo Tosatti | 07190a0 | 2007-02-16 01:27:44 -0800 | [diff] [blame] | 395 | clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY; |
| 396 | } |
| 397 | #else |
| 398 | static inline void check_geode_tsc_reliable(void) { } |
| 399 | #endif |
| 400 | |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 401 | |
| 402 | void __init tsc_init(void) |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 403 | { |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 404 | int cpu; |
Alok Kataria | 3da757d | 2008-06-20 15:06:33 -0700 | [diff] [blame] | 405 | u64 lpj; |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 406 | |
Thomas Gleixner | 9ccc906 | 2008-05-13 12:31:00 +0200 | [diff] [blame] | 407 | if (!cpu_has_tsc || tsc_disabled) { |
| 408 | /* Disable the TSC in case of !cpu_has_tsc */ |
| 409 | tsc_disabled = 1; |
Rusty Russell | 3c2047c | 2008-03-04 23:07:50 +1100 | [diff] [blame] | 410 | return; |
Thomas Gleixner | 9ccc906 | 2008-05-13 12:31:00 +0200 | [diff] [blame] | 411 | } |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 412 | |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 413 | cpu_khz = calculate_cpu_khz(); |
| 414 | tsc_khz = cpu_khz; |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 415 | |
Rusty Russell | 3c2047c | 2008-03-04 23:07:50 +1100 | [diff] [blame] | 416 | if (!cpu_khz) { |
| 417 | mark_tsc_unstable("could not calculate TSC khz"); |
Thomas Gleixner | 74dc51a | 2008-05-18 22:17:59 +0200 | [diff] [blame] | 418 | /* |
| 419 | * We need to disable the TSC completely in this case |
| 420 | * to prevent sched_clock() from using it. |
| 421 | */ |
| 422 | tsc_disabled = 1; |
Rusty Russell | 3c2047c | 2008-03-04 23:07:50 +1100 | [diff] [blame] | 423 | return; |
| 424 | } |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 425 | |
Alok Kataria | 3da757d | 2008-06-20 15:06:33 -0700 | [diff] [blame] | 426 | lpj = ((u64)tsc_khz * 1000); |
| 427 | do_div(lpj, HZ); |
Alok Kataria | f3f3149 | 2008-06-23 18:21:56 -0700 | [diff] [blame^] | 428 | lpj_fine = lpj; |
Alok Kataria | 3da757d | 2008-06-20 15:06:33 -0700 | [diff] [blame] | 429 | |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 430 | printk("Detected %lu.%03lu MHz processor.\n", |
| 431 | (unsigned long)cpu_khz / 1000, |
| 432 | (unsigned long)cpu_khz % 1000); |
| 433 | |
Guillaume Chazarain | 53d517c | 2008-01-30 13:30:06 +0100 | [diff] [blame] | 434 | /* |
| 435 | * Secondary CPUs do not run through tsc_init(), so set up |
| 436 | * all the scale factors for all CPUs, assuming the same |
| 437 | * speed as the bootup CPU. (cpufreq notifiers will fix this |
| 438 | * up if their speed diverges) |
| 439 | */ |
| 440 | for_each_possible_cpu(cpu) |
| 441 | set_cyc2ns_scale(cpu_khz, cpu); |
| 442 | |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 443 | use_tsc_delay(); |
| 444 | |
| 445 | /* Check and install the TSC clocksource */ |
| 446 | dmi_check_system(bad_tsc_dmi_table); |
| 447 | |
| 448 | unsynchronized_tsc(); |
| 449 | check_geode_tsc_reliable(); |
| 450 | current_tsc_khz = tsc_khz; |
| 451 | clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz, |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 452 | clocksource_tsc.shift); |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 453 | /* lower the rating if we already know its unstable: */ |
| 454 | if (check_tsc_unstable()) { |
| 455 | clocksource_tsc.rating = 0; |
| 456 | clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS; |
Thomas Gleixner | 9ccc906 | 2008-05-13 12:31:00 +0200 | [diff] [blame] | 457 | } |
john stultz | 6bb74df | 2007-03-05 00:30:50 -0800 | [diff] [blame] | 458 | clocksource_register(&clocksource_tsc); |
john stultz | 5d0cf41 | 2006-06-26 00:25:12 -0700 | [diff] [blame] | 459 | } |