tsc_64.c 8.4 KB

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