tsc_64.c 7.6 KB

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