tsc_64.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. static int notsc __initdata = 0;
  13. unsigned int cpu_khz; /* TSC clocks / usec, not used here */
  14. EXPORT_SYMBOL(cpu_khz);
  15. unsigned int tsc_khz;
  16. EXPORT_SYMBOL(tsc_khz);
  17. static unsigned int cyc2ns_scale __read_mostly;
  18. static inline void set_cyc2ns_scale(unsigned long khz)
  19. {
  20. cyc2ns_scale = (NSEC_PER_MSEC << NS_SCALE) / khz;
  21. }
  22. static unsigned long long cycles_2_ns(unsigned long long cyc)
  23. {
  24. return (cyc * cyc2ns_scale) >> NS_SCALE;
  25. }
  26. unsigned long long sched_clock(void)
  27. {
  28. unsigned long a = 0;
  29. /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
  30. * which means it is not completely exact and may not be monotonous
  31. * between CPUs. But the errors should be too small to matter for
  32. * scheduling purposes.
  33. */
  34. rdtscll(a);
  35. return cycles_2_ns(a);
  36. }
  37. static int tsc_unstable;
  38. inline int check_tsc_unstable(void)
  39. {
  40. return tsc_unstable;
  41. }
  42. #ifdef CONFIG_CPU_FREQ
  43. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  44. * changes.
  45. *
  46. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  47. * not that important because current Opteron setups do not support
  48. * scaling on SMP anyroads.
  49. *
  50. * Should fix up last_tsc too. Currently gettimeofday in the
  51. * first tick after the change will be slightly wrong.
  52. */
  53. static unsigned int ref_freq;
  54. static unsigned long loops_per_jiffy_ref;
  55. static unsigned long tsc_khz_ref;
  56. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  57. void *data)
  58. {
  59. struct cpufreq_freqs *freq = data;
  60. unsigned long *lpj, dummy;
  61. if (cpu_has(&cpu_data[freq->cpu], X86_FEATURE_CONSTANT_TSC))
  62. return 0;
  63. lpj = &dummy;
  64. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  65. #ifdef CONFIG_SMP
  66. lpj = &cpu_data[freq->cpu].loops_per_jiffy;
  67. #else
  68. lpj = &boot_cpu_data.loops_per_jiffy;
  69. #endif
  70. if (!ref_freq) {
  71. ref_freq = freq->old;
  72. loops_per_jiffy_ref = *lpj;
  73. tsc_khz_ref = tsc_khz;
  74. }
  75. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  76. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  77. (val == CPUFREQ_RESUMECHANGE)) {
  78. *lpj =
  79. cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  80. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  81. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  82. mark_tsc_unstable("cpufreq changes");
  83. }
  84. set_cyc2ns_scale(tsc_khz_ref);
  85. return 0;
  86. }
  87. static struct notifier_block time_cpufreq_notifier_block = {
  88. .notifier_call = time_cpufreq_notifier
  89. };
  90. static int __init cpufreq_tsc(void)
  91. {
  92. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  93. CPUFREQ_TRANSITION_NOTIFIER);
  94. return 0;
  95. }
  96. core_initcall(cpufreq_tsc);
  97. #endif
  98. #define MAX_RETRIES 5
  99. #define SMI_TRESHOLD 50000
  100. /*
  101. * Read TSC and the reference counters. Take care of SMI disturbance
  102. */
  103. static unsigned long __init tsc_read_refs(unsigned long *pm,
  104. unsigned long *hpet)
  105. {
  106. unsigned long t1, t2;
  107. int i;
  108. for (i = 0; i < MAX_RETRIES; i++) {
  109. t1 = get_cycles_sync();
  110. if (hpet)
  111. *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  112. else
  113. *pm = acpi_pm_read_early();
  114. t2 = get_cycles_sync();
  115. if ((t2 - t1) < SMI_TRESHOLD)
  116. return t2;
  117. }
  118. return ULONG_MAX;
  119. }
  120. /**
  121. * tsc_calibrate - calibrate the tsc on boot
  122. */
  123. void __init tsc_calibrate(void)
  124. {
  125. unsigned long flags, tsc1, tsc2, tr1, tr2, pm1, pm2, hpet1, hpet2;
  126. int hpet = is_hpet_enabled();
  127. local_irq_save(flags);
  128. tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
  129. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  130. outb(0xb0, 0x43);
  131. outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  132. outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  133. tr1 = get_cycles_sync();
  134. while ((inb(0x61) & 0x20) == 0);
  135. tr2 = get_cycles_sync();
  136. tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
  137. local_irq_restore(flags);
  138. /*
  139. * Preset the result with the raw and inaccurate PIT
  140. * calibration value
  141. */
  142. tsc_khz = (tr2 - tr1) / 50;
  143. /* hpet or pmtimer available ? */
  144. if (!hpet && !pm1 && !pm2) {
  145. printk(KERN_INFO "TSC calibrated against PIT\n");
  146. return;
  147. }
  148. /* Check, whether the sampling was disturbed by an SMI */
  149. if (tsc1 == ULONG_MAX || tsc2 == ULONG_MAX) {
  150. printk(KERN_WARNING "TSC calibration disturbed by SMI, "
  151. "using PIT calibration result\n");
  152. return;
  153. }
  154. tsc2 = (tsc2 - tsc1) * 1000000L;
  155. if (hpet) {
  156. printk(KERN_INFO "TSC calibrated against HPET\n");
  157. if (hpet2 < hpet1)
  158. hpet2 += 0x100000000;
  159. hpet2 -= hpet1;
  160. tsc1 = (hpet2 * hpet_readl(HPET_PERIOD)) / 1000000;
  161. } else {
  162. printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
  163. if (pm2 < pm1)
  164. pm2 += ACPI_PM_OVRRUN;
  165. pm2 -= pm1;
  166. tsc1 = (pm2 * 1000000000) / PMTMR_TICKS_PER_SEC;
  167. }
  168. tsc_khz = tsc2 / tsc1;
  169. set_cyc2ns_scale(tsc_khz);
  170. }
  171. /*
  172. * Make an educated guess if the TSC is trustworthy and synchronized
  173. * over all CPUs.
  174. */
  175. __cpuinit int unsynchronized_tsc(void)
  176. {
  177. if (tsc_unstable)
  178. return 1;
  179. #ifdef CONFIG_SMP
  180. if (apic_is_clustered_box())
  181. return 1;
  182. #endif
  183. /* Most intel systems have synchronized TSCs except for
  184. multi node systems */
  185. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  186. #ifdef CONFIG_ACPI
  187. /* But TSC doesn't tick in C3 so don't use it there */
  188. if (acpi_gbl_FADT.header.length > 0 &&
  189. acpi_gbl_FADT.C3latency < 1000)
  190. return 1;
  191. #endif
  192. return 0;
  193. }
  194. /* Assume multi socket systems are not synchronized */
  195. return num_present_cpus() > 1;
  196. }
  197. int __init notsc_setup(char *s)
  198. {
  199. notsc = 1;
  200. return 1;
  201. }
  202. __setup("notsc", notsc_setup);
  203. /* clock source code: */
  204. static cycle_t read_tsc(void)
  205. {
  206. cycle_t ret = (cycle_t)get_cycles_sync();
  207. return ret;
  208. }
  209. static cycle_t __vsyscall_fn vread_tsc(void)
  210. {
  211. cycle_t ret = (cycle_t)get_cycles_sync();
  212. return ret;
  213. }
  214. static struct clocksource clocksource_tsc = {
  215. .name = "tsc",
  216. .rating = 300,
  217. .read = read_tsc,
  218. .mask = CLOCKSOURCE_MASK(64),
  219. .shift = 22,
  220. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  221. CLOCK_SOURCE_MUST_VERIFY,
  222. .vread = vread_tsc,
  223. };
  224. void mark_tsc_unstable(char *reason)
  225. {
  226. if (!tsc_unstable) {
  227. tsc_unstable = 1;
  228. printk("Marking TSC unstable due to %s\n", reason);
  229. /* Change only the rating, when not registered */
  230. if (clocksource_tsc.mult)
  231. clocksource_change_rating(&clocksource_tsc, 0);
  232. else
  233. clocksource_tsc.rating = 0;
  234. }
  235. }
  236. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  237. void __init init_tsc_clocksource(void)
  238. {
  239. if (!notsc) {
  240. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  241. clocksource_tsc.shift);
  242. if (check_tsc_unstable())
  243. clocksource_tsc.rating = 0;
  244. clocksource_register(&clocksource_tsc);
  245. }
  246. }