timer_tsc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * This code largely moved from arch/i386/kernel/time.c.
  3. * See comments there for proper credits.
  4. *
  5. * 2004-06-25 Jesper Juhl
  6. * moved mark_offset_tsc below cpufreq_delayed_get to avoid gcc 3.4
  7. * failing to inline.
  8. */
  9. #include <linux/spinlock.h>
  10. #include <linux/init.h>
  11. #include <linux/timex.h>
  12. #include <linux/errno.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/string.h>
  15. #include <linux/jiffies.h>
  16. #include <asm/timer.h>
  17. #include <asm/io.h>
  18. /* processor.h for distable_tsc flag */
  19. #include <asm/processor.h>
  20. #include "io_ports.h"
  21. #include "mach_timer.h"
  22. #include <asm/hpet.h>
  23. #include <asm/i8253.h>
  24. #ifdef CONFIG_HPET_TIMER
  25. static unsigned long hpet_usec_quotient;
  26. static unsigned long hpet_last;
  27. static struct timer_opts timer_tsc;
  28. #endif
  29. static int use_tsc;
  30. /* Number of usecs that the last interrupt was delayed */
  31. static int delay_at_last_interrupt;
  32. static unsigned long last_tsc_low; /* lsb 32 bits of Time Stamp Counter */
  33. static unsigned long last_tsc_high; /* msb 32 bits of Time Stamp Counter */
  34. static unsigned long long monotonic_base;
  35. static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
  36. /* Avoid compensating for lost ticks before TSCs are synched */
  37. static int detect_lost_ticks;
  38. static int __init start_lost_tick_compensation(void)
  39. {
  40. detect_lost_ticks = 1;
  41. return 0;
  42. }
  43. late_initcall(start_lost_tick_compensation);
  44. /* convert from cycles(64bits) => nanoseconds (64bits)
  45. * basic equation:
  46. * ns = cycles / (freq / ns_per_sec)
  47. * ns = cycles * (ns_per_sec / freq)
  48. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  49. * ns = cycles * (10^6 / cpu_khz)
  50. *
  51. * Then we use scaling math (suggested by george@mvista.com) to get:
  52. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  53. * ns = cycles * cyc2ns_scale / SC
  54. *
  55. * And since SC is a constant power of two, we can convert the div
  56. * into a shift.
  57. *
  58. * We can use khz divisor instead of mhz to keep a better percision, since
  59. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  60. * (mathieu.desnoyers@polymtl.ca)
  61. *
  62. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  63. */
  64. static unsigned long cyc2ns_scale __read_mostly;
  65. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  66. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  67. {
  68. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  69. }
  70. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  71. {
  72. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  73. }
  74. static int count2; /* counter for mark_offset_tsc() */
  75. /* Cached *multiplier* to convert TSC counts to microseconds.
  76. * (see the equation below).
  77. * Equal to 2^32 * (1 / (clocks per usec) ).
  78. * Initialized in time_init.
  79. */
  80. static unsigned long fast_gettimeoffset_quotient;
  81. static unsigned long get_offset_tsc(void)
  82. {
  83. register unsigned long eax, edx;
  84. /* Read the Time Stamp Counter */
  85. rdtsc(eax,edx);
  86. /* .. relative to previous jiffy (32 bits is enough) */
  87. eax -= last_tsc_low; /* tsc_low delta */
  88. /*
  89. * Time offset = (tsc_low delta) * fast_gettimeoffset_quotient
  90. * = (tsc_low delta) * (usecs_per_clock)
  91. * = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)
  92. *
  93. * Using a mull instead of a divl saves up to 31 clock cycles
  94. * in the critical path.
  95. */
  96. __asm__("mull %2"
  97. :"=a" (eax), "=d" (edx)
  98. :"rm" (fast_gettimeoffset_quotient),
  99. "0" (eax));
  100. /* our adjusted time offset in microseconds */
  101. return delay_at_last_interrupt + edx;
  102. }
  103. static unsigned long long monotonic_clock_tsc(void)
  104. {
  105. unsigned long long last_offset, this_offset, base;
  106. unsigned seq;
  107. /* atomically read monotonic base & last_offset */
  108. do {
  109. seq = read_seqbegin(&monotonic_lock);
  110. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  111. base = monotonic_base;
  112. } while (read_seqretry(&monotonic_lock, seq));
  113. /* Read the Time Stamp Counter */
  114. rdtscll(this_offset);
  115. /* return the value in ns */
  116. return base + cycles_2_ns(this_offset - last_offset);
  117. }
  118. static void delay_tsc(unsigned long loops)
  119. {
  120. unsigned long bclock, now;
  121. rdtscl(bclock);
  122. do
  123. {
  124. rep_nop();
  125. rdtscl(now);
  126. } while ((now-bclock) < loops);
  127. }
  128. #ifdef CONFIG_HPET_TIMER
  129. static void mark_offset_tsc_hpet(void)
  130. {
  131. unsigned long long this_offset, last_offset;
  132. unsigned long offset, temp, hpet_current;
  133. write_seqlock(&monotonic_lock);
  134. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  135. /*
  136. * It is important that these two operations happen almost at
  137. * the same time. We do the RDTSC stuff first, since it's
  138. * faster. To avoid any inconsistencies, we need interrupts
  139. * disabled locally.
  140. */
  141. /*
  142. * Interrupts are just disabled locally since the timer irq
  143. * has the SA_INTERRUPT flag set. -arca
  144. */
  145. /* read Pentium cycle counter */
  146. hpet_current = hpet_readl(HPET_COUNTER);
  147. rdtsc(last_tsc_low, last_tsc_high);
  148. /* lost tick compensation */
  149. offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
  150. if (unlikely(((offset - hpet_last) > hpet_tick) && (hpet_last != 0))
  151. && detect_lost_ticks) {
  152. int lost_ticks = (offset - hpet_last) / hpet_tick;
  153. jiffies_64 += lost_ticks;
  154. }
  155. hpet_last = hpet_current;
  156. /* update the monotonic base value */
  157. this_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  158. monotonic_base += cycles_2_ns(this_offset - last_offset);
  159. write_sequnlock(&monotonic_lock);
  160. /* calculate delay_at_last_interrupt */
  161. /*
  162. * Time offset = (hpet delta) * ( usecs per HPET clock )
  163. * = (hpet delta) * ( usecs per tick / HPET clocks per tick)
  164. * = (hpet delta) * ( hpet_usec_quotient ) / (2^32)
  165. * Where,
  166. * hpet_usec_quotient = (2^32 * usecs per tick)/HPET clocks per tick
  167. */
  168. delay_at_last_interrupt = hpet_current - offset;
  169. ASM_MUL64_REG(temp, delay_at_last_interrupt,
  170. hpet_usec_quotient, delay_at_last_interrupt);
  171. }
  172. #endif
  173. static void mark_offset_tsc(void)
  174. {
  175. unsigned long lost,delay;
  176. unsigned long delta = last_tsc_low;
  177. int count;
  178. int countmp;
  179. static int count1 = 0;
  180. unsigned long long this_offset, last_offset;
  181. static int lost_count = 0;
  182. write_seqlock(&monotonic_lock);
  183. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  184. /*
  185. * It is important that these two operations happen almost at
  186. * the same time. We do the RDTSC stuff first, since it's
  187. * faster. To avoid any inconsistencies, we need interrupts
  188. * disabled locally.
  189. */
  190. /*
  191. * Interrupts are just disabled locally since the timer irq
  192. * has the SA_INTERRUPT flag set. -arca
  193. */
  194. /* read Pentium cycle counter */
  195. rdtsc(last_tsc_low, last_tsc_high);
  196. spin_lock(&i8253_lock);
  197. outb_p(0x00, PIT_MODE); /* latch the count ASAP */
  198. count = inb_p(PIT_CH0); /* read the latched count */
  199. count |= inb(PIT_CH0) << 8;
  200. /*
  201. * VIA686a test code... reset the latch if count > max + 1
  202. * from timer_pit.c - cjb
  203. */
  204. if (count > LATCH) {
  205. outb_p(0x34, PIT_MODE);
  206. outb_p(LATCH & 0xff, PIT_CH0);
  207. outb(LATCH >> 8, PIT_CH0);
  208. count = LATCH - 1;
  209. }
  210. spin_unlock(&i8253_lock);
  211. if (pit_latch_buggy) {
  212. /* get center value of last 3 time lutch */
  213. if ((count2 >= count && count >= count1)
  214. || (count1 >= count && count >= count2)) {
  215. count2 = count1; count1 = count;
  216. } else if ((count1 >= count2 && count2 >= count)
  217. || (count >= count2 && count2 >= count1)) {
  218. countmp = count;count = count2;
  219. count2 = count1;count1 = countmp;
  220. } else {
  221. count2 = count1; count1 = count; count = count1;
  222. }
  223. }
  224. /* lost tick compensation */
  225. delta = last_tsc_low - delta;
  226. {
  227. register unsigned long eax, edx;
  228. eax = delta;
  229. __asm__("mull %2"
  230. :"=a" (eax), "=d" (edx)
  231. :"rm" (fast_gettimeoffset_quotient),
  232. "0" (eax));
  233. delta = edx;
  234. }
  235. delta += delay_at_last_interrupt;
  236. lost = delta/(1000000/HZ);
  237. delay = delta%(1000000/HZ);
  238. if (lost >= 2 && detect_lost_ticks) {
  239. jiffies_64 += lost-1;
  240. /* sanity check to ensure we're not always losing ticks */
  241. if (lost_count++ > 100) {
  242. printk(KERN_WARNING "Losing too many ticks!\n");
  243. printk(KERN_WARNING "TSC cannot be used as a timesource. \n");
  244. printk(KERN_WARNING "Possible reasons for this are:\n");
  245. printk(KERN_WARNING " You're running with Speedstep,\n");
  246. printk(KERN_WARNING " You don't have DMA enabled for your hard disk (see hdparm),\n");
  247. printk(KERN_WARNING " Incorrect TSC synchronization on an SMP system (see dmesg).\n");
  248. printk(KERN_WARNING "Falling back to a sane timesource now.\n");
  249. clock_fallback();
  250. }
  251. } else
  252. lost_count = 0;
  253. /* update the monotonic base value */
  254. this_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  255. monotonic_base += cycles_2_ns(this_offset - last_offset);
  256. write_sequnlock(&monotonic_lock);
  257. /* calculate delay_at_last_interrupt */
  258. count = ((LATCH-1) - count) * TICK_SIZE;
  259. delay_at_last_interrupt = (count + LATCH/2) / LATCH;
  260. /* catch corner case where tick rollover occured
  261. * between tsc and pit reads (as noted when
  262. * usec delta is > 90% # of usecs/tick)
  263. */
  264. if (lost && abs(delay - delay_at_last_interrupt) > (900000/HZ))
  265. jiffies_64++;
  266. }
  267. static int __init init_tsc(char* override)
  268. {
  269. /* check clock override */
  270. if (override[0] && strncmp(override,"tsc",3)) {
  271. #ifdef CONFIG_HPET_TIMER
  272. if (is_hpet_enabled()) {
  273. printk(KERN_ERR "Warning: clock= override failed. Defaulting to tsc\n");
  274. } else
  275. #endif
  276. {
  277. return -ENODEV;
  278. }
  279. }
  280. /*
  281. * If we have APM enabled or the CPU clock speed is variable
  282. * (CPU stops clock on HLT or slows clock to save power)
  283. * then the TSC timestamps may diverge by up to 1 jiffy from
  284. * 'real time' but nothing will break.
  285. * The most frequent case is that the CPU is "woken" from a halt
  286. * state by the timer interrupt itself, so we get 0 error. In the
  287. * rare cases where a driver would "wake" the CPU and request a
  288. * timestamp, the maximum error is < 1 jiffy. But timestamps are
  289. * still perfectly ordered.
  290. * Note that the TSC counter will be reset if APM suspends
  291. * to disk; this won't break the kernel, though, 'cuz we're
  292. * smart. See arch/i386/kernel/apm.c.
  293. */
  294. /*
  295. * Firstly we have to do a CPU check for chips with
  296. * a potentially buggy TSC. At this point we haven't run
  297. * the ident/bugs checks so we must run this hook as it
  298. * may turn off the TSC flag.
  299. *
  300. * NOTE: this doesn't yet handle SMP 486 machines where only
  301. * some CPU's have a TSC. Thats never worked and nobody has
  302. * moaned if you have the only one in the world - you fix it!
  303. */
  304. count2 = LATCH; /* initialize counter for mark_offset_tsc() */
  305. if (cpu_has_tsc) {
  306. unsigned long tsc_quotient;
  307. #ifdef CONFIG_HPET_TIMER
  308. if (is_hpet_enabled() && hpet_use_timer) {
  309. unsigned long result, remain;
  310. printk("Using TSC for gettimeofday\n");
  311. tsc_quotient = calibrate_tsc_hpet(NULL);
  312. timer_tsc.mark_offset = &mark_offset_tsc_hpet;
  313. /*
  314. * Math to calculate hpet to usec multiplier
  315. * Look for the comments at get_offset_tsc_hpet()
  316. */
  317. ASM_DIV64_REG(result, remain, hpet_tick,
  318. 0, KERNEL_TICK_USEC);
  319. if (remain > (hpet_tick >> 1))
  320. result++; /* rounding the result */
  321. hpet_usec_quotient = result;
  322. } else
  323. #endif
  324. {
  325. tsc_quotient = calibrate_tsc();
  326. }
  327. if (tsc_quotient) {
  328. fast_gettimeoffset_quotient = tsc_quotient;
  329. use_tsc = 1;
  330. /*
  331. * We could be more selective here I suspect
  332. * and just enable this for the next intel chips ?
  333. */
  334. /* report CPU clock rate in Hz.
  335. * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
  336. * clock/second. Our precision is about 100 ppm.
  337. */
  338. { unsigned long eax=0, edx=1000;
  339. __asm__("divl %2"
  340. :"=a" (cpu_khz), "=d" (edx)
  341. :"r" (tsc_quotient),
  342. "0" (eax), "1" (edx));
  343. printk("Detected %u.%03u MHz processor.\n",
  344. cpu_khz / 1000, cpu_khz % 1000);
  345. }
  346. set_cyc2ns_scale(cpu_khz);
  347. return 0;
  348. }
  349. }
  350. return -ENODEV;
  351. }
  352. static int tsc_resume(void)
  353. {
  354. write_seqlock(&monotonic_lock);
  355. /* Assume this is the last mark offset time */
  356. rdtsc(last_tsc_low, last_tsc_high);
  357. #ifdef CONFIG_HPET_TIMER
  358. if (is_hpet_enabled() && hpet_use_timer)
  359. hpet_last = hpet_readl(HPET_COUNTER);
  360. #endif
  361. write_sequnlock(&monotonic_lock);
  362. return 0;
  363. }
  364. /************************************************************/
  365. /* tsc timer_opts struct */
  366. static struct timer_opts timer_tsc = {
  367. .name = "tsc",
  368. .mark_offset = mark_offset_tsc,
  369. .get_offset = get_offset_tsc,
  370. .monotonic_clock = monotonic_clock_tsc,
  371. .delay = delay_tsc,
  372. .read_timer = read_timer_tsc,
  373. .resume = tsc_resume,
  374. };
  375. struct init_timer_opts __initdata timer_tsc_init = {
  376. .init = init_tsc,
  377. .opts = &timer_tsc,
  378. };