timer_tsc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 inline void cpufreq_delayed_get(void);
  30. int tsc_disable __devinitdata = 0;
  31. static int use_tsc;
  32. /* Number of usecs that the last interrupt was delayed */
  33. static int delay_at_last_interrupt;
  34. static unsigned long last_tsc_low; /* lsb 32 bits of Time Stamp Counter */
  35. static unsigned long last_tsc_high; /* msb 32 bits of Time Stamp Counter */
  36. static unsigned long long monotonic_base;
  37. static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
  38. /* convert from cycles(64bits) => nanoseconds (64bits)
  39. * basic equation:
  40. * ns = cycles / (freq / ns_per_sec)
  41. * ns = cycles * (ns_per_sec / freq)
  42. * ns = cycles * (10^9 / (cpu_mhz * 10^6))
  43. * ns = cycles * (10^3 / cpu_mhz)
  44. *
  45. * Then we use scaling math (suggested by george@mvista.com) to get:
  46. * ns = cycles * (10^3 * SC / cpu_mhz) / SC
  47. * ns = cycles * cyc2ns_scale / SC
  48. *
  49. * And since SC is a constant power of two, we can convert the div
  50. * into a shift.
  51. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  52. */
  53. static unsigned long cyc2ns_scale;
  54. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  55. static inline void set_cyc2ns_scale(unsigned long cpu_mhz)
  56. {
  57. cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
  58. }
  59. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  60. {
  61. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  62. }
  63. static int count2; /* counter for mark_offset_tsc() */
  64. /* Cached *multiplier* to convert TSC counts to microseconds.
  65. * (see the equation below).
  66. * Equal to 2^32 * (1 / (clocks per usec) ).
  67. * Initialized in time_init.
  68. */
  69. static unsigned long fast_gettimeoffset_quotient;
  70. static unsigned long get_offset_tsc(void)
  71. {
  72. register unsigned long eax, edx;
  73. /* Read the Time Stamp Counter */
  74. rdtsc(eax,edx);
  75. /* .. relative to previous jiffy (32 bits is enough) */
  76. eax -= last_tsc_low; /* tsc_low delta */
  77. /*
  78. * Time offset = (tsc_low delta) * fast_gettimeoffset_quotient
  79. * = (tsc_low delta) * (usecs_per_clock)
  80. * = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)
  81. *
  82. * Using a mull instead of a divl saves up to 31 clock cycles
  83. * in the critical path.
  84. */
  85. __asm__("mull %2"
  86. :"=a" (eax), "=d" (edx)
  87. :"rm" (fast_gettimeoffset_quotient),
  88. "0" (eax));
  89. /* our adjusted time offset in microseconds */
  90. return delay_at_last_interrupt + edx;
  91. }
  92. static unsigned long long monotonic_clock_tsc(void)
  93. {
  94. unsigned long long last_offset, this_offset, base;
  95. unsigned seq;
  96. /* atomically read monotonic base & last_offset */
  97. do {
  98. seq = read_seqbegin(&monotonic_lock);
  99. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  100. base = monotonic_base;
  101. } while (read_seqretry(&monotonic_lock, seq));
  102. /* Read the Time Stamp Counter */
  103. rdtscll(this_offset);
  104. /* return the value in ns */
  105. return base + cycles_2_ns(this_offset - last_offset);
  106. }
  107. /*
  108. * Scheduler clock - returns current time in nanosec units.
  109. */
  110. unsigned long long sched_clock(void)
  111. {
  112. unsigned long long this_offset;
  113. /*
  114. * In the NUMA case we dont use the TSC as they are not
  115. * synchronized across all CPUs.
  116. */
  117. #ifndef CONFIG_NUMA
  118. if (!use_tsc)
  119. #endif
  120. /* no locking but a rare wrong value is not a big deal */
  121. return jiffies_64 * (1000000000 / HZ);
  122. /* Read the Time Stamp Counter */
  123. rdtscll(this_offset);
  124. /* return the value in ns */
  125. return cycles_2_ns(this_offset);
  126. }
  127. static void delay_tsc(unsigned long loops)
  128. {
  129. unsigned long bclock, now;
  130. rdtscl(bclock);
  131. do
  132. {
  133. rep_nop();
  134. rdtscl(now);
  135. } while ((now-bclock) < loops);
  136. }
  137. #ifdef CONFIG_HPET_TIMER
  138. static void mark_offset_tsc_hpet(void)
  139. {
  140. unsigned long long this_offset, last_offset;
  141. unsigned long offset, temp, hpet_current;
  142. write_seqlock(&monotonic_lock);
  143. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  144. /*
  145. * It is important that these two operations happen almost at
  146. * the same time. We do the RDTSC stuff first, since it's
  147. * faster. To avoid any inconsistencies, we need interrupts
  148. * disabled locally.
  149. */
  150. /*
  151. * Interrupts are just disabled locally since the timer irq
  152. * has the SA_INTERRUPT flag set. -arca
  153. */
  154. /* read Pentium cycle counter */
  155. hpet_current = hpet_readl(HPET_COUNTER);
  156. rdtsc(last_tsc_low, last_tsc_high);
  157. /* lost tick compensation */
  158. offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
  159. if (unlikely(((offset - hpet_last) > hpet_tick) && (hpet_last != 0))) {
  160. int lost_ticks = (offset - hpet_last) / hpet_tick;
  161. jiffies_64 += lost_ticks;
  162. }
  163. hpet_last = hpet_current;
  164. /* update the monotonic base value */
  165. this_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  166. monotonic_base += cycles_2_ns(this_offset - last_offset);
  167. write_sequnlock(&monotonic_lock);
  168. /* calculate delay_at_last_interrupt */
  169. /*
  170. * Time offset = (hpet delta) * ( usecs per HPET clock )
  171. * = (hpet delta) * ( usecs per tick / HPET clocks per tick)
  172. * = (hpet delta) * ( hpet_usec_quotient ) / (2^32)
  173. * Where,
  174. * hpet_usec_quotient = (2^32 * usecs per tick)/HPET clocks per tick
  175. */
  176. delay_at_last_interrupt = hpet_current - offset;
  177. ASM_MUL64_REG(temp, delay_at_last_interrupt,
  178. hpet_usec_quotient, delay_at_last_interrupt);
  179. }
  180. #endif
  181. #ifdef CONFIG_CPU_FREQ
  182. #include <linux/workqueue.h>
  183. static unsigned int cpufreq_delayed_issched = 0;
  184. static unsigned int cpufreq_init = 0;
  185. static struct work_struct cpufreq_delayed_get_work;
  186. static void handle_cpufreq_delayed_get(void *v)
  187. {
  188. unsigned int cpu;
  189. for_each_online_cpu(cpu) {
  190. cpufreq_get(cpu);
  191. }
  192. cpufreq_delayed_issched = 0;
  193. }
  194. /* if we notice lost ticks, schedule a call to cpufreq_get() as it tries
  195. * to verify the CPU frequency the timing core thinks the CPU is running
  196. * at is still correct.
  197. */
  198. static inline void cpufreq_delayed_get(void)
  199. {
  200. if (cpufreq_init && !cpufreq_delayed_issched) {
  201. cpufreq_delayed_issched = 1;
  202. printk(KERN_DEBUG "Losing some ticks... checking if CPU frequency changed.\n");
  203. schedule_work(&cpufreq_delayed_get_work);
  204. }
  205. }
  206. /* If the CPU frequency is scaled, TSC-based delays will need a different
  207. * loops_per_jiffy value to function properly.
  208. */
  209. static unsigned int ref_freq = 0;
  210. static unsigned long loops_per_jiffy_ref = 0;
  211. #ifndef CONFIG_SMP
  212. static unsigned long fast_gettimeoffset_ref = 0;
  213. static unsigned int cpu_khz_ref = 0;
  214. #endif
  215. static int
  216. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  217. void *data)
  218. {
  219. struct cpufreq_freqs *freq = data;
  220. if (val != CPUFREQ_RESUMECHANGE)
  221. write_seqlock_irq(&xtime_lock);
  222. if (!ref_freq) {
  223. ref_freq = freq->old;
  224. loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
  225. #ifndef CONFIG_SMP
  226. fast_gettimeoffset_ref = fast_gettimeoffset_quotient;
  227. cpu_khz_ref = cpu_khz;
  228. #endif
  229. }
  230. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  231. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  232. (val == CPUFREQ_RESUMECHANGE)) {
  233. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  234. cpu_data[freq->cpu].loops_per_jiffy = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  235. #ifndef CONFIG_SMP
  236. if (cpu_khz)
  237. cpu_khz = cpufreq_scale(cpu_khz_ref, ref_freq, freq->new);
  238. if (use_tsc) {
  239. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  240. fast_gettimeoffset_quotient = cpufreq_scale(fast_gettimeoffset_ref, freq->new, ref_freq);
  241. set_cyc2ns_scale(cpu_khz/1000);
  242. }
  243. }
  244. #endif
  245. }
  246. if (val != CPUFREQ_RESUMECHANGE)
  247. write_sequnlock_irq(&xtime_lock);
  248. return 0;
  249. }
  250. static struct notifier_block time_cpufreq_notifier_block = {
  251. .notifier_call = time_cpufreq_notifier
  252. };
  253. static int __init cpufreq_tsc(void)
  254. {
  255. int ret;
  256. INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);
  257. ret = cpufreq_register_notifier(&time_cpufreq_notifier_block,
  258. CPUFREQ_TRANSITION_NOTIFIER);
  259. if (!ret)
  260. cpufreq_init = 1;
  261. return ret;
  262. }
  263. core_initcall(cpufreq_tsc);
  264. #else /* CONFIG_CPU_FREQ */
  265. static inline void cpufreq_delayed_get(void) { return; }
  266. #endif
  267. int recalibrate_cpu_khz(void)
  268. {
  269. #ifndef CONFIG_SMP
  270. unsigned int cpu_khz_old = cpu_khz;
  271. if (cpu_has_tsc) {
  272. init_cpu_khz();
  273. cpu_data[0].loops_per_jiffy =
  274. cpufreq_scale(cpu_data[0].loops_per_jiffy,
  275. cpu_khz_old,
  276. cpu_khz);
  277. return 0;
  278. } else
  279. return -ENODEV;
  280. #else
  281. return -ENODEV;
  282. #endif
  283. }
  284. EXPORT_SYMBOL(recalibrate_cpu_khz);
  285. static void mark_offset_tsc(void)
  286. {
  287. unsigned long lost,delay;
  288. unsigned long delta = last_tsc_low;
  289. int count;
  290. int countmp;
  291. static int count1 = 0;
  292. unsigned long long this_offset, last_offset;
  293. static int lost_count = 0;
  294. write_seqlock(&monotonic_lock);
  295. last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  296. /*
  297. * It is important that these two operations happen almost at
  298. * the same time. We do the RDTSC stuff first, since it's
  299. * faster. To avoid any inconsistencies, we need interrupts
  300. * disabled locally.
  301. */
  302. /*
  303. * Interrupts are just disabled locally since the timer irq
  304. * has the SA_INTERRUPT flag set. -arca
  305. */
  306. /* read Pentium cycle counter */
  307. rdtsc(last_tsc_low, last_tsc_high);
  308. spin_lock(&i8253_lock);
  309. outb_p(0x00, PIT_MODE); /* latch the count ASAP */
  310. count = inb_p(PIT_CH0); /* read the latched count */
  311. count |= inb(PIT_CH0) << 8;
  312. /*
  313. * VIA686a test code... reset the latch if count > max + 1
  314. * from timer_pit.c - cjb
  315. */
  316. if (count > LATCH) {
  317. outb_p(0x34, PIT_MODE);
  318. outb_p(LATCH & 0xff, PIT_CH0);
  319. outb(LATCH >> 8, PIT_CH0);
  320. count = LATCH - 1;
  321. }
  322. spin_unlock(&i8253_lock);
  323. if (pit_latch_buggy) {
  324. /* get center value of last 3 time lutch */
  325. if ((count2 >= count && count >= count1)
  326. || (count1 >= count && count >= count2)) {
  327. count2 = count1; count1 = count;
  328. } else if ((count1 >= count2 && count2 >= count)
  329. || (count >= count2 && count2 >= count1)) {
  330. countmp = count;count = count2;
  331. count2 = count1;count1 = countmp;
  332. } else {
  333. count2 = count1; count1 = count; count = count1;
  334. }
  335. }
  336. /* lost tick compensation */
  337. delta = last_tsc_low - delta;
  338. {
  339. register unsigned long eax, edx;
  340. eax = delta;
  341. __asm__("mull %2"
  342. :"=a" (eax), "=d" (edx)
  343. :"rm" (fast_gettimeoffset_quotient),
  344. "0" (eax));
  345. delta = edx;
  346. }
  347. delta += delay_at_last_interrupt;
  348. lost = delta/(1000000/HZ);
  349. delay = delta%(1000000/HZ);
  350. if (lost >= 2) {
  351. jiffies_64 += lost-1;
  352. /* sanity check to ensure we're not always losing ticks */
  353. if (lost_count++ > 100) {
  354. printk(KERN_WARNING "Losing too many ticks!\n");
  355. printk(KERN_WARNING "TSC cannot be used as a timesource. \n");
  356. printk(KERN_WARNING "Possible reasons for this are:\n");
  357. printk(KERN_WARNING " You're running with Speedstep,\n");
  358. printk(KERN_WARNING " You don't have DMA enabled for your hard disk (see hdparm),\n");
  359. printk(KERN_WARNING " Incorrect TSC synchronization on an SMP system (see dmesg).\n");
  360. printk(KERN_WARNING "Falling back to a sane timesource now.\n");
  361. clock_fallback();
  362. }
  363. /* ... but give the TSC a fair chance */
  364. if (lost_count > 25)
  365. cpufreq_delayed_get();
  366. } else
  367. lost_count = 0;
  368. /* update the monotonic base value */
  369. this_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
  370. monotonic_base += cycles_2_ns(this_offset - last_offset);
  371. write_sequnlock(&monotonic_lock);
  372. /* calculate delay_at_last_interrupt */
  373. count = ((LATCH-1) - count) * TICK_SIZE;
  374. delay_at_last_interrupt = (count + LATCH/2) / LATCH;
  375. /* catch corner case where tick rollover occured
  376. * between tsc and pit reads (as noted when
  377. * usec delta is > 90% # of usecs/tick)
  378. */
  379. if (lost && abs(delay - delay_at_last_interrupt) > (900000/HZ))
  380. jiffies_64++;
  381. }
  382. static int __init init_tsc(char* override)
  383. {
  384. /* check clock override */
  385. if (override[0] && strncmp(override,"tsc",3)) {
  386. #ifdef CONFIG_HPET_TIMER
  387. if (is_hpet_enabled()) {
  388. printk(KERN_ERR "Warning: clock= override failed. Defaulting to tsc\n");
  389. } else
  390. #endif
  391. {
  392. return -ENODEV;
  393. }
  394. }
  395. /*
  396. * If we have APM enabled or the CPU clock speed is variable
  397. * (CPU stops clock on HLT or slows clock to save power)
  398. * then the TSC timestamps may diverge by up to 1 jiffy from
  399. * 'real time' but nothing will break.
  400. * The most frequent case is that the CPU is "woken" from a halt
  401. * state by the timer interrupt itself, so we get 0 error. In the
  402. * rare cases where a driver would "wake" the CPU and request a
  403. * timestamp, the maximum error is < 1 jiffy. But timestamps are
  404. * still perfectly ordered.
  405. * Note that the TSC counter will be reset if APM suspends
  406. * to disk; this won't break the kernel, though, 'cuz we're
  407. * smart. See arch/i386/kernel/apm.c.
  408. */
  409. /*
  410. * Firstly we have to do a CPU check for chips with
  411. * a potentially buggy TSC. At this point we haven't run
  412. * the ident/bugs checks so we must run this hook as it
  413. * may turn off the TSC flag.
  414. *
  415. * NOTE: this doesn't yet handle SMP 486 machines where only
  416. * some CPU's have a TSC. Thats never worked and nobody has
  417. * moaned if you have the only one in the world - you fix it!
  418. */
  419. count2 = LATCH; /* initialize counter for mark_offset_tsc() */
  420. if (cpu_has_tsc) {
  421. unsigned long tsc_quotient;
  422. #ifdef CONFIG_HPET_TIMER
  423. if (is_hpet_enabled() && hpet_use_timer) {
  424. unsigned long result, remain;
  425. printk("Using TSC for gettimeofday\n");
  426. tsc_quotient = calibrate_tsc_hpet(NULL);
  427. timer_tsc.mark_offset = &mark_offset_tsc_hpet;
  428. /*
  429. * Math to calculate hpet to usec multiplier
  430. * Look for the comments at get_offset_tsc_hpet()
  431. */
  432. ASM_DIV64_REG(result, remain, hpet_tick,
  433. 0, KERNEL_TICK_USEC);
  434. if (remain > (hpet_tick >> 1))
  435. result++; /* rounding the result */
  436. hpet_usec_quotient = result;
  437. } else
  438. #endif
  439. {
  440. tsc_quotient = calibrate_tsc();
  441. }
  442. if (tsc_quotient) {
  443. fast_gettimeoffset_quotient = tsc_quotient;
  444. use_tsc = 1;
  445. /*
  446. * We could be more selective here I suspect
  447. * and just enable this for the next intel chips ?
  448. */
  449. /* report CPU clock rate in Hz.
  450. * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
  451. * clock/second. Our precision is about 100 ppm.
  452. */
  453. { unsigned long eax=0, edx=1000;
  454. __asm__("divl %2"
  455. :"=a" (cpu_khz), "=d" (edx)
  456. :"r" (tsc_quotient),
  457. "0" (eax), "1" (edx));
  458. printk("Detected %u.%03u MHz processor.\n",
  459. cpu_khz / 1000, cpu_khz % 1000);
  460. }
  461. set_cyc2ns_scale(cpu_khz/1000);
  462. return 0;
  463. }
  464. }
  465. return -ENODEV;
  466. }
  467. static int tsc_resume(void)
  468. {
  469. write_seqlock(&monotonic_lock);
  470. /* Assume this is the last mark offset time */
  471. rdtsc(last_tsc_low, last_tsc_high);
  472. #ifdef CONFIG_HPET_TIMER
  473. if (is_hpet_enabled() && hpet_use_timer)
  474. hpet_last = hpet_readl(HPET_COUNTER);
  475. #endif
  476. write_sequnlock(&monotonic_lock);
  477. return 0;
  478. }
  479. #ifndef CONFIG_X86_TSC
  480. /* disable flag for tsc. Takes effect by clearing the TSC cpu flag
  481. * in cpu/common.c */
  482. static int __init tsc_setup(char *str)
  483. {
  484. tsc_disable = 1;
  485. return 1;
  486. }
  487. #else
  488. static int __init tsc_setup(char *str)
  489. {
  490. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  491. "cannot disable TSC.\n");
  492. return 1;
  493. }
  494. #endif
  495. __setup("notsc", tsc_setup);
  496. /************************************************************/
  497. /* tsc timer_opts struct */
  498. static struct timer_opts timer_tsc = {
  499. .name = "tsc",
  500. .mark_offset = mark_offset_tsc,
  501. .get_offset = get_offset_tsc,
  502. .monotonic_clock = monotonic_clock_tsc,
  503. .delay = delay_tsc,
  504. .read_timer = read_timer_tsc,
  505. .resume = tsc_resume,
  506. };
  507. struct init_timer_opts __initdata timer_tsc_init = {
  508. .init = init_tsc,
  509. .opts = &timer_tsc,
  510. };