timer_hpet.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * This code largely moved from arch/i386/kernel/time.c.
  3. * See comments there for proper credits.
  4. */
  5. #include <linux/spinlock.h>
  6. #include <linux/init.h>
  7. #include <linux/timex.h>
  8. #include <linux/errno.h>
  9. #include <linux/string.h>
  10. #include <linux/jiffies.h>
  11. #include <asm/timer.h>
  12. #include <asm/io.h>
  13. #include <asm/processor.h>
  14. #include "io_ports.h"
  15. #include "mach_timer.h"
  16. #include <asm/hpet.h>
  17. static unsigned long hpet_usec_quotient __read_mostly; /* convert hpet clks to usec */
  18. static unsigned long tsc_hpet_quotient __read_mostly; /* convert tsc to hpet clks */
  19. static unsigned long hpet_last; /* hpet counter value at last tick*/
  20. static unsigned long last_tsc_low; /* lsb 32 bits of Time Stamp Counter */
  21. static unsigned long last_tsc_high; /* msb 32 bits of Time Stamp Counter */
  22. static unsigned long long monotonic_base;
  23. static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
  24. /* convert from cycles(64bits) => nanoseconds (64bits)
  25. * basic equation:
  26. * ns = cycles / (freq / ns_per_sec)
  27. * ns = cycles * (ns_per_sec / freq)
  28. * ns = cycles * (10^9 / (cpu_mhz * 10^6))
  29. * ns = cycles * (10^3 / cpu_mhz)
  30. *
  31. * Then we use scaling math (suggested by george@mvista.com) to get:
  32. * ns = cycles * (10^3 * SC / cpu_mhz) / SC
  33. * ns = cycles * cyc2ns_scale / SC
  34. *
  35. * And since SC is a constant power of two, we can convert the div
  36. * into a shift.
  37. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  38. */
  39. static unsigned long cyc2ns_scale;
  40. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  41. static inline void set_cyc2ns_scale(unsigned long cpu_mhz)
  42. {
  43. cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
  44. }
  45. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  46. {
  47. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  48. }
  49. static unsigned long long monotonic_clock_hpet(void)
  50. {
  51. unsigned long long last_offset, this_offset, base;
  52. unsigned seq;
  53. /* atomically read monotonic base & last_offset */
  54. do {
  55. seq = read_seqbegin(&monotonic_lock);
  56. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  57. base = monotonic_base;
  58. } while (read_seqretry(&monotonic_lock, seq));
  59. /* Read the Time Stamp Counter */
  60. rdtscll(this_offset);
  61. /* return the value in ns */
  62. return base + cycles_2_ns(this_offset - last_offset);
  63. }
  64. static unsigned long get_offset_hpet(void)
  65. {
  66. register unsigned long eax, edx;
  67. eax = hpet_readl(HPET_COUNTER);
  68. eax -= hpet_last; /* hpet delta */
  69. eax = min(hpet_tick, eax);
  70. /*
  71. * Time offset = (hpet delta) * ( usecs per HPET clock )
  72. * = (hpet delta) * ( usecs per tick / HPET clocks per tick)
  73. * = (hpet delta) * ( hpet_usec_quotient ) / (2^32)
  74. *
  75. * Where,
  76. * hpet_usec_quotient = (2^32 * usecs per tick)/HPET clocks per tick
  77. *
  78. * Using a mull instead of a divl saves some cycles in critical path.
  79. */
  80. ASM_MUL64_REG(eax, edx, hpet_usec_quotient, eax);
  81. /* our adjusted time offset in microseconds */
  82. return edx;
  83. }
  84. static void mark_offset_hpet(void)
  85. {
  86. unsigned long long this_offset, last_offset;
  87. unsigned long offset;
  88. write_seqlock(&monotonic_lock);
  89. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  90. rdtsc(last_tsc_low, last_tsc_high);
  91. if (hpet_use_timer)
  92. offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
  93. else
  94. offset = hpet_readl(HPET_COUNTER);
  95. if (unlikely(((offset - hpet_last) >= (2*hpet_tick)) && (hpet_last != 0))) {
  96. int lost_ticks = ((offset - hpet_last) / hpet_tick) - 1;
  97. jiffies_64 += lost_ticks;
  98. }
  99. hpet_last = offset;
  100. /* update the monotonic base value */
  101. this_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  102. monotonic_base += cycles_2_ns(this_offset - last_offset);
  103. write_sequnlock(&monotonic_lock);
  104. }
  105. static void delay_hpet(unsigned long loops)
  106. {
  107. unsigned long hpet_start, hpet_end;
  108. unsigned long eax;
  109. /* loops is the number of cpu cycles. Convert it to hpet clocks */
  110. ASM_MUL64_REG(eax, loops, tsc_hpet_quotient, loops);
  111. hpet_start = hpet_readl(HPET_COUNTER);
  112. do {
  113. rep_nop();
  114. hpet_end = hpet_readl(HPET_COUNTER);
  115. } while ((hpet_end - hpet_start) < (loops));
  116. }
  117. static struct timer_opts timer_hpet;
  118. static int __init init_hpet(char* override)
  119. {
  120. unsigned long result, remain;
  121. /* check clock override */
  122. if (override[0] && strncmp(override,"hpet",4))
  123. return -ENODEV;
  124. if (!is_hpet_enabled())
  125. return -ENODEV;
  126. printk("Using HPET for gettimeofday\n");
  127. if (cpu_has_tsc) {
  128. unsigned long tsc_quotient = calibrate_tsc_hpet(&tsc_hpet_quotient);
  129. if (tsc_quotient) {
  130. /* report CPU clock rate in Hz.
  131. * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
  132. * clock/second. Our precision is about 100 ppm.
  133. */
  134. { unsigned long eax=0, edx=1000;
  135. ASM_DIV64_REG(cpu_khz, edx, tsc_quotient,
  136. eax, edx);
  137. printk("Detected %u.%03u MHz processor.\n",
  138. cpu_khz / 1000, cpu_khz % 1000);
  139. }
  140. set_cyc2ns_scale(cpu_khz/1000);
  141. }
  142. /* set this only when cpu_has_tsc */
  143. timer_hpet.read_timer = read_timer_tsc;
  144. }
  145. /*
  146. * Math to calculate hpet to usec multiplier
  147. * Look for the comments at get_offset_hpet()
  148. */
  149. ASM_DIV64_REG(result, remain, hpet_tick, 0, KERNEL_TICK_USEC);
  150. if (remain > (hpet_tick >> 1))
  151. result++; /* rounding the result */
  152. hpet_usec_quotient = result;
  153. return 0;
  154. }
  155. static int hpet_resume(void)
  156. {
  157. write_seqlock(&monotonic_lock);
  158. /* Assume this is the last mark offset time */
  159. rdtsc(last_tsc_low, last_tsc_high);
  160. if (hpet_use_timer)
  161. hpet_last = hpet_readl(HPET_T0_CMP) - hpet_tick;
  162. else
  163. hpet_last = hpet_readl(HPET_COUNTER);
  164. write_sequnlock(&monotonic_lock);
  165. return 0;
  166. }
  167. /************************************************************/
  168. /* tsc timer_opts struct */
  169. static struct timer_opts timer_hpet __read_mostly = {
  170. .name = "hpet",
  171. .mark_offset = mark_offset_hpet,
  172. .get_offset = get_offset_hpet,
  173. .monotonic_clock = monotonic_clock_hpet,
  174. .delay = delay_hpet,
  175. .resume = hpet_resume,
  176. };
  177. struct init_timer_opts __initdata timer_hpet_init = {
  178. .init = init_hpet,
  179. .opts = &timer_hpet,
  180. };