/arch/i386/kernel/timers/common.c

https://bitbucket.org/evzijst/gittest · C · 160 lines · 93 code · 27 blank · 40 comment · 11 complexity · 613d46aa7883bac23b02d65dd6b6e747 MD5 · raw file

  1. /*
  2. * Common functions used across the timers go here
  3. */
  4. #include <linux/init.h>
  5. #include <linux/timex.h>
  6. #include <linux/errno.h>
  7. #include <linux/jiffies.h>
  8. #include <asm/io.h>
  9. #include <asm/timer.h>
  10. #include <asm/hpet.h>
  11. #include "mach_timer.h"
  12. /* ------ Calibrate the TSC -------
  13. * Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset().
  14. * Too much 64-bit arithmetic here to do this cleanly in C, and for
  15. * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
  16. * output busy loop as low as possible. We avoid reading the CTC registers
  17. * directly because of the awkward 8-bit access mechanism of the 82C54
  18. * device.
  19. */
  20. #define CALIBRATE_TIME (5 * 1000020/HZ)
  21. unsigned long __init calibrate_tsc(void)
  22. {
  23. mach_prepare_counter();
  24. {
  25. unsigned long startlow, starthigh;
  26. unsigned long endlow, endhigh;
  27. unsigned long count;
  28. rdtsc(startlow,starthigh);
  29. mach_countup(&count);
  30. rdtsc(endlow,endhigh);
  31. /* Error: ECTCNEVERSET */
  32. if (count <= 1)
  33. goto bad_ctc;
  34. /* 64-bit subtract - gcc just messes up with long longs */
  35. __asm__("subl %2,%0\n\t"
  36. "sbbl %3,%1"
  37. :"=a" (endlow), "=d" (endhigh)
  38. :"g" (startlow), "g" (starthigh),
  39. "0" (endlow), "1" (endhigh));
  40. /* Error: ECPUTOOFAST */
  41. if (endhigh)
  42. goto bad_ctc;
  43. /* Error: ECPUTOOSLOW */
  44. if (endlow <= CALIBRATE_TIME)
  45. goto bad_ctc;
  46. __asm__("divl %2"
  47. :"=a" (endlow), "=d" (endhigh)
  48. :"r" (endlow), "0" (0), "1" (CALIBRATE_TIME));
  49. return endlow;
  50. }
  51. /*
  52. * The CTC wasn't reliable: we got a hit on the very first read,
  53. * or the CPU was so fast/slow that the quotient wouldn't fit in
  54. * 32 bits..
  55. */
  56. bad_ctc:
  57. return 0;
  58. }
  59. #ifdef CONFIG_HPET_TIMER
  60. /* ------ Calibrate the TSC using HPET -------
  61. * Return 2^32 * (1 / (TSC clocks per usec)) for getting the CPU freq.
  62. * Second output is parameter 1 (when non NULL)
  63. * Set 2^32 * (1 / (tsc per HPET clk)) for delay_hpet().
  64. * calibrate_tsc() calibrates the processor TSC by comparing
  65. * it to the HPET timer of known frequency.
  66. * Too much 64-bit arithmetic here to do this cleanly in C
  67. */
  68. #define CALIBRATE_CNT_HPET (5 * hpet_tick)
  69. #define CALIBRATE_TIME_HPET (5 * KERNEL_TICK_USEC)
  70. unsigned long __init calibrate_tsc_hpet(unsigned long *tsc_hpet_quotient_ptr)
  71. {
  72. unsigned long tsc_startlow, tsc_starthigh;
  73. unsigned long tsc_endlow, tsc_endhigh;
  74. unsigned long hpet_start, hpet_end;
  75. unsigned long result, remain;
  76. hpet_start = hpet_readl(HPET_COUNTER);
  77. rdtsc(tsc_startlow, tsc_starthigh);
  78. do {
  79. hpet_end = hpet_readl(HPET_COUNTER);
  80. } while ((hpet_end - hpet_start) < CALIBRATE_CNT_HPET);
  81. rdtsc(tsc_endlow, tsc_endhigh);
  82. /* 64-bit subtract - gcc just messes up with long longs */
  83. __asm__("subl %2,%0\n\t"
  84. "sbbl %3,%1"
  85. :"=a" (tsc_endlow), "=d" (tsc_endhigh)
  86. :"g" (tsc_startlow), "g" (tsc_starthigh),
  87. "0" (tsc_endlow), "1" (tsc_endhigh));
  88. /* Error: ECPUTOOFAST */
  89. if (tsc_endhigh)
  90. goto bad_calibration;
  91. /* Error: ECPUTOOSLOW */
  92. if (tsc_endlow <= CALIBRATE_TIME_HPET)
  93. goto bad_calibration;
  94. ASM_DIV64_REG(result, remain, tsc_endlow, 0, CALIBRATE_TIME_HPET);
  95. if (remain > (tsc_endlow >> 1))
  96. result++; /* rounding the result */
  97. if (tsc_hpet_quotient_ptr) {
  98. unsigned long tsc_hpet_quotient;
  99. ASM_DIV64_REG(tsc_hpet_quotient, remain, tsc_endlow, 0,
  100. CALIBRATE_CNT_HPET);
  101. if (remain > (tsc_endlow >> 1))
  102. tsc_hpet_quotient++; /* rounding the result */
  103. *tsc_hpet_quotient_ptr = tsc_hpet_quotient;
  104. }
  105. return result;
  106. bad_calibration:
  107. /*
  108. * the CPU was so fast/slow that the quotient wouldn't fit in
  109. * 32 bits..
  110. */
  111. return 0;
  112. }
  113. #endif
  114. /* calculate cpu_khz */
  115. void __init init_cpu_khz(void)
  116. {
  117. if (cpu_has_tsc) {
  118. unsigned long tsc_quotient = calibrate_tsc();
  119. if (tsc_quotient) {
  120. /* report CPU clock rate in Hz.
  121. * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
  122. * clock/second. Our precision is about 100 ppm.
  123. */
  124. { unsigned long eax=0, edx=1000;
  125. __asm__("divl %2"
  126. :"=a" (cpu_khz), "=d" (edx)
  127. :"r" (tsc_quotient),
  128. "0" (eax), "1" (edx));
  129. printk("Detected %lu.%03lu MHz processor.\n", cpu_khz / 1000, cpu_khz % 1000);
  130. }
  131. }
  132. }
  133. }