tsc_64.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include <linux/kernel.h>
  2. #include <linux/sched.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/init.h>
  5. #include <linux/clocksource.h>
  6. #include <linux/time.h>
  7. #include <linux/acpi.h>
  8. #include <linux/cpufreq.h>
  9. #include <linux/acpi_pmtmr.h>
  10. #include <asm/hpet.h>
  11. #include <asm/timex.h>
  12. #include <asm/timer.h>
  13. #include <asm/vgtod.h>
  14. extern int tsc_unstable;
  15. extern int tsc_disabled;
  16. /* Accelerators for sched_clock()
  17. * convert from cycles(64bits) => nanoseconds (64bits)
  18. * basic equation:
  19. * ns = cycles / (freq / ns_per_sec)
  20. * ns = cycles * (ns_per_sec / freq)
  21. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  22. * ns = cycles * (10^6 / cpu_khz)
  23. *
  24. * Then we use scaling math (suggested by george@mvista.com) to get:
  25. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  26. * ns = cycles * cyc2ns_scale / SC
  27. *
  28. * And since SC is a constant power of two, we can convert the div
  29. * into a shift.
  30. *
  31. * We can use khz divisor instead of mhz to keep a better precision, since
  32. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  33. * (mathieu.desnoyers@polymtl.ca)
  34. *
  35. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  36. */
  37. DEFINE_PER_CPU(unsigned long, cyc2ns);
  38. static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
  39. {
  40. unsigned long long tsc_now, ns_now;
  41. unsigned long flags, *scale;
  42. local_irq_save(flags);
  43. sched_clock_idle_sleep_event();
  44. scale = &per_cpu(cyc2ns, cpu);
  45. rdtscll(tsc_now);
  46. ns_now = __cycles_2_ns(tsc_now);
  47. if (cpu_khz)
  48. *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
  49. sched_clock_idle_wakeup_event(0);
  50. local_irq_restore(flags);
  51. }
  52. #ifdef CONFIG_CPU_FREQ
  53. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  54. * changes.
  55. *
  56. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  57. * not that important because current Opteron setups do not support
  58. * scaling on SMP anyroads.
  59. *
  60. * Should fix up last_tsc too. Currently gettimeofday in the
  61. * first tick after the change will be slightly wrong.
  62. */
  63. static unsigned int ref_freq;
  64. static unsigned long loops_per_jiffy_ref;
  65. static unsigned long tsc_khz_ref;
  66. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  67. void *data)
  68. {
  69. struct cpufreq_freqs *freq = data;
  70. unsigned long *lpj, dummy;
  71. if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
  72. return 0;
  73. lpj = &dummy;
  74. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  75. #ifdef CONFIG_SMP
  76. lpj = &cpu_data(freq->cpu).loops_per_jiffy;
  77. #else
  78. lpj = &boot_cpu_data.loops_per_jiffy;
  79. #endif
  80. if (!ref_freq) {
  81. ref_freq = freq->old;
  82. loops_per_jiffy_ref = *lpj;
  83. tsc_khz_ref = tsc_khz;
  84. }
  85. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  86. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  87. (val == CPUFREQ_RESUMECHANGE)) {
  88. *lpj =
  89. cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  90. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  91. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  92. mark_tsc_unstable("cpufreq changes");
  93. }
  94. set_cyc2ns_scale(tsc_khz_ref, freq->cpu);
  95. return 0;
  96. }
  97. static struct notifier_block time_cpufreq_notifier_block = {
  98. .notifier_call = time_cpufreq_notifier
  99. };
  100. static int __init cpufreq_tsc(void)
  101. {
  102. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  103. CPUFREQ_TRANSITION_NOTIFIER);
  104. return 0;
  105. }
  106. core_initcall(cpufreq_tsc);
  107. #endif
  108. #define MAX_RETRIES 5
  109. #define SMI_TRESHOLD 50000
  110. /*
  111. * Read TSC and the reference counters. Take care of SMI disturbance
  112. */
  113. static unsigned long __init tsc_read_refs(unsigned long *pm,
  114. unsigned long *hpet)
  115. {
  116. unsigned long t1, t2;
  117. int i;
  118. for (i = 0; i < MAX_RETRIES; i++) {
  119. t1 = get_cycles();
  120. if (hpet)
  121. *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  122. else
  123. *pm = acpi_pm_read_early();
  124. t2 = get_cycles();
  125. if ((t2 - t1) < SMI_TRESHOLD)
  126. return t2;
  127. }
  128. return ULONG_MAX;
  129. }
  130. /**
  131. * tsc_calibrate - calibrate the tsc on boot
  132. */
  133. void __init tsc_calibrate(void)
  134. {
  135. unsigned long flags, tsc1, tsc2, tr1, tr2, pm1, pm2, hpet1, hpet2;
  136. int hpet = is_hpet_enabled(), cpu;
  137. local_irq_save(flags);
  138. tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
  139. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  140. outb(0xb0, 0x43);
  141. outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  142. outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  143. tr1 = get_cycles();
  144. while ((inb(0x61) & 0x20) == 0);
  145. tr2 = get_cycles();
  146. tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
  147. local_irq_restore(flags);
  148. /*
  149. * Preset the result with the raw and inaccurate PIT
  150. * calibration value
  151. */
  152. tsc_khz = (tr2 - tr1) / 50;
  153. /* hpet or pmtimer available ? */
  154. if (!hpet && !pm1 && !pm2) {
  155. printk(KERN_INFO "TSC calibrated against PIT\n");
  156. goto out;
  157. }
  158. /* Check, whether the sampling was disturbed by an SMI */
  159. if (tsc1 == ULONG_MAX || tsc2 == ULONG_MAX) {
  160. printk(KERN_WARNING "TSC calibration disturbed by SMI, "
  161. "using PIT calibration result\n");
  162. goto out;
  163. }
  164. tsc2 = (tsc2 - tsc1) * 1000000L;
  165. if (hpet) {
  166. printk(KERN_INFO "TSC calibrated against HPET\n");
  167. if (hpet2 < hpet1)
  168. hpet2 += 0x100000000UL;
  169. hpet2 -= hpet1;
  170. tsc1 = (hpet2 * hpet_readl(HPET_PERIOD)) / 1000000;
  171. } else {
  172. printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
  173. if (pm2 < pm1)
  174. pm2 += ACPI_PM_OVRRUN;
  175. pm2 -= pm1;
  176. tsc1 = (pm2 * 1000000000) / PMTMR_TICKS_PER_SEC;
  177. }
  178. tsc_khz = tsc2 / tsc1;
  179. out:
  180. for_each_possible_cpu(cpu)
  181. set_cyc2ns_scale(tsc_khz, cpu);
  182. }
  183. /*
  184. * Make an educated guess if the TSC is trustworthy and synchronized
  185. * over all CPUs.
  186. */
  187. __cpuinit int unsynchronized_tsc(void)
  188. {
  189. if (tsc_unstable)
  190. return 1;
  191. #ifdef CONFIG_SMP
  192. if (apic_is_clustered_box())
  193. return 1;
  194. #endif
  195. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  196. return 0;
  197. /* Assume multi socket systems are not synchronized */
  198. return num_present_cpus() > 1;
  199. }
  200. static struct clocksource clocksource_tsc;
  201. /*
  202. * We compare the TSC to the cycle_last value in the clocksource
  203. * structure to avoid a nasty time-warp. This can be observed in a
  204. * very small window right after one CPU updated cycle_last under
  205. * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
  206. * is smaller than the cycle_last reference value due to a TSC which
  207. * is slighty behind. This delta is nowhere else observable, but in
  208. * that case it results in a forward time jump in the range of hours
  209. * due to the unsigned delta calculation of the time keeping core
  210. * code, which is necessary to support wrapping clocksources like pm
  211. * timer.
  212. */
  213. static cycle_t read_tsc(void)
  214. {
  215. cycle_t ret = (cycle_t)get_cycles();
  216. return ret >= clocksource_tsc.cycle_last ?
  217. ret : clocksource_tsc.cycle_last;
  218. }
  219. static cycle_t __vsyscall_fn vread_tsc(void)
  220. {
  221. cycle_t ret = (cycle_t)vget_cycles();
  222. return ret >= __vsyscall_gtod_data.clock.cycle_last ?
  223. ret : __vsyscall_gtod_data.clock.cycle_last;
  224. }
  225. static struct clocksource clocksource_tsc = {
  226. .name = "tsc",
  227. .rating = 300,
  228. .read = read_tsc,
  229. .mask = CLOCKSOURCE_MASK(64),
  230. .shift = 22,
  231. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  232. CLOCK_SOURCE_MUST_VERIFY,
  233. .vread = vread_tsc,
  234. };
  235. void mark_tsc_unstable(char *reason)
  236. {
  237. if (!tsc_unstable) {
  238. tsc_unstable = 1;
  239. printk("Marking TSC unstable due to %s\n", reason);
  240. /* Change only the rating, when not registered */
  241. if (clocksource_tsc.mult)
  242. clocksource_change_rating(&clocksource_tsc, 0);
  243. else
  244. clocksource_tsc.rating = 0;
  245. }
  246. }
  247. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  248. void __init init_tsc_clocksource(void)
  249. {
  250. if (tsc_disabled > 0)
  251. return;
  252. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  253. clocksource_tsc.shift);
  254. if (check_tsc_unstable())
  255. clocksource_tsc.rating = 0;
  256. clocksource_register(&clocksource_tsc);
  257. }