tsc_64.c 8.5 KB

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