diff options
author | Ingo Molnar <mingo@elte.hu> | 2009-03-18 04:57:02 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2009-03-18 04:57:02 -0400 |
commit | 37ba317c9ed19eb126e40bbf563f2711e191a636 (patch) | |
tree | c5865768f341fc0ab41899fe9af9dc5918784cee /arch/x86/kernel/tsc.c | |
parent | 708dc5125309cd33c5daaad3026cc4ae6ef39c8b (diff) | |
parent | ee568b25ee9e160b32d1aef73d8b2ee9c05d34db (diff) |
Merge branches 'sched/cleanups' and 'linus' into sched/core
Diffstat (limited to 'arch/x86/kernel/tsc.c')
-rw-r--r-- | arch/x86/kernel/tsc.c | 110 |
1 files changed, 65 insertions, 45 deletions
diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index 9c8b71531ca8..dfa6f7b73ff5 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c | |||
@@ -274,30 +274,43 @@ static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin) | |||
274 | * use the TSC value at the transitions to calculate a pretty | 274 | * use the TSC value at the transitions to calculate a pretty |
275 | * good value for the TSC frequencty. | 275 | * good value for the TSC frequencty. |
276 | */ | 276 | */ |
277 | static inline int pit_expect_msb(unsigned char val) | 277 | static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap) |
278 | { | 278 | { |
279 | int count = 0; | 279 | int count; |
280 | u64 tsc = 0; | ||
280 | 281 | ||
281 | for (count = 0; count < 50000; count++) { | 282 | for (count = 0; count < 50000; count++) { |
282 | /* Ignore LSB */ | 283 | /* Ignore LSB */ |
283 | inb(0x42); | 284 | inb(0x42); |
284 | if (inb(0x42) != val) | 285 | if (inb(0x42) != val) |
285 | break; | 286 | break; |
287 | tsc = get_cycles(); | ||
286 | } | 288 | } |
287 | return count > 50; | 289 | *deltap = get_cycles() - tsc; |
290 | *tscp = tsc; | ||
291 | |||
292 | /* | ||
293 | * We require _some_ success, but the quality control | ||
294 | * will be based on the error terms on the TSC values. | ||
295 | */ | ||
296 | return count > 5; | ||
288 | } | 297 | } |
289 | 298 | ||
290 | /* | 299 | /* |
291 | * How many MSB values do we want to see? We aim for a | 300 | * How many MSB values do we want to see? We aim for |
292 | * 15ms calibration, which assuming a 2us counter read | 301 | * a maximum error rate of 500ppm (in practice the |
293 | * error should give us roughly 150 ppm precision for | 302 | * real error is much smaller), but refuse to spend |
294 | * the calibration. | 303 | * more than 25ms on it. |
295 | */ | 304 | */ |
296 | #define QUICK_PIT_MS 15 | 305 | #define MAX_QUICK_PIT_MS 25 |
297 | #define QUICK_PIT_ITERATIONS (QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256) | 306 | #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256) |
298 | 307 | ||
299 | static unsigned long quick_pit_calibrate(void) | 308 | static unsigned long quick_pit_calibrate(void) |
300 | { | 309 | { |
310 | int i; | ||
311 | u64 tsc, delta; | ||
312 | unsigned long d1, d2; | ||
313 | |||
301 | /* Set the Gate high, disable speaker */ | 314 | /* Set the Gate high, disable speaker */ |
302 | outb((inb(0x61) & ~0x02) | 0x01, 0x61); | 315 | outb((inb(0x61) & ~0x02) | 0x01, 0x61); |
303 | 316 | ||
@@ -316,45 +329,52 @@ static unsigned long quick_pit_calibrate(void) | |||
316 | outb(0xff, 0x42); | 329 | outb(0xff, 0x42); |
317 | outb(0xff, 0x42); | 330 | outb(0xff, 0x42); |
318 | 331 | ||
319 | if (pit_expect_msb(0xff)) { | 332 | /* |
320 | int i; | 333 | * The PIT starts counting at the next edge, so we |
321 | u64 t1, t2, delta; | 334 | * need to delay for a microsecond. The easiest way |
322 | unsigned char expect = 0xfe; | 335 | * to do that is to just read back the 16-bit counter |
323 | 336 | * once from the PIT. | |
324 | t1 = get_cycles(); | 337 | */ |
325 | for (i = 0; i < QUICK_PIT_ITERATIONS; i++, expect--) { | 338 | inb(0x42); |
326 | if (!pit_expect_msb(expect)) | 339 | inb(0x42); |
327 | goto failed; | 340 | |
341 | if (pit_expect_msb(0xff, &tsc, &d1)) { | ||
342 | for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) { | ||
343 | if (!pit_expect_msb(0xff-i, &delta, &d2)) | ||
344 | break; | ||
345 | |||
346 | /* | ||
347 | * Iterate until the error is less than 500 ppm | ||
348 | */ | ||
349 | delta -= tsc; | ||
350 | if (d1+d2 < delta >> 11) | ||
351 | goto success; | ||
328 | } | 352 | } |
329 | t2 = get_cycles(); | ||
330 | |||
331 | /* | ||
332 | * Make sure we can rely on the second TSC timestamp: | ||
333 | */ | ||
334 | if (!pit_expect_msb(expect)) | ||
335 | goto failed; | ||
336 | |||
337 | /* | ||
338 | * Ok, if we get here, then we've seen the | ||
339 | * MSB of the PIT decrement QUICK_PIT_ITERATIONS | ||
340 | * times, and each MSB had many hits, so we never | ||
341 | * had any sudden jumps. | ||
342 | * | ||
343 | * As a result, we can depend on there not being | ||
344 | * any odd delays anywhere, and the TSC reads are | ||
345 | * reliable. | ||
346 | * | ||
347 | * kHz = ticks / time-in-seconds / 1000; | ||
348 | * kHz = (t2 - t1) / (QPI * 256 / PIT_TICK_RATE) / 1000 | ||
349 | * kHz = ((t2 - t1) * PIT_TICK_RATE) / (QPI * 256 * 1000) | ||
350 | */ | ||
351 | delta = (t2 - t1)*PIT_TICK_RATE; | ||
352 | do_div(delta, QUICK_PIT_ITERATIONS*256*1000); | ||
353 | printk("Fast TSC calibration using PIT\n"); | ||
354 | return delta; | ||
355 | } | 353 | } |
356 | failed: | 354 | printk("Fast TSC calibration failed\n"); |
357 | return 0; | 355 | return 0; |
356 | |||
357 | success: | ||
358 | /* | ||
359 | * Ok, if we get here, then we've seen the | ||
360 | * MSB of the PIT decrement 'i' times, and the | ||
361 | * error has shrunk to less than 500 ppm. | ||
362 | * | ||
363 | * As a result, we can depend on there not being | ||
364 | * any odd delays anywhere, and the TSC reads are | ||
365 | * reliable (within the error). We also adjust the | ||
366 | * delta to the middle of the error bars, just | ||
367 | * because it looks nicer. | ||
368 | * | ||
369 | * kHz = ticks / time-in-seconds / 1000; | ||
370 | * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000 | ||
371 | * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000) | ||
372 | */ | ||
373 | delta += (long)(d2 - d1)/2; | ||
374 | delta *= PIT_TICK_RATE; | ||
375 | do_div(delta, i*256*1000); | ||
376 | printk("Fast TSC calibration using PIT\n"); | ||
377 | return delta; | ||
358 | } | 378 | } |
359 | 379 | ||
360 | /** | 380 | /** |