tsc_64.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 flags, prev_scale, *scale;
  43. unsigned long long tsc_now, ns_now;
  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. prev_scale = *scale;
  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. inline int check_tsc_unstable(void)
  79. {
  80. return tsc_unstable;
  81. }
  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. preempt_disable();
  125. set_cyc2ns_scale(tsc_khz_ref, smp_processor_id());
  126. preempt_enable();
  127. return 0;
  128. }
  129. static struct notifier_block time_cpufreq_notifier_block = {
  130. .notifier_call = time_cpufreq_notifier
  131. };
  132. static int __init cpufreq_tsc(void)
  133. {
  134. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  135. CPUFREQ_TRANSITION_NOTIFIER);
  136. return 0;
  137. }
  138. core_initcall(cpufreq_tsc);
  139. #endif
  140. #define MAX_RETRIES 5
  141. #define SMI_TRESHOLD 50000
  142. /*
  143. * Read TSC and the reference counters. Take care of SMI disturbance
  144. */
  145. static unsigned long __init tsc_read_refs(unsigned long *pm,
  146. unsigned long *hpet)
  147. {
  148. unsigned long t1, t2;
  149. int i;
  150. for (i = 0; i < MAX_RETRIES; i++) {
  151. t1 = get_cycles();
  152. if (hpet)
  153. *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  154. else
  155. *pm = acpi_pm_read_early();
  156. t2 = get_cycles();
  157. if ((t2 - t1) < SMI_TRESHOLD)
  158. return t2;
  159. }
  160. return ULONG_MAX;
  161. }
  162. /**
  163. * tsc_calibrate - calibrate the tsc on boot
  164. */
  165. void __init tsc_calibrate(void)
  166. {
  167. unsigned long flags, tsc1, tsc2, tr1, tr2, pm1, pm2, hpet1, hpet2;
  168. int hpet = is_hpet_enabled(), cpu;
  169. local_irq_save(flags);
  170. tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
  171. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  172. outb(0xb0, 0x43);
  173. outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  174. outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  175. tr1 = get_cycles();
  176. while ((inb(0x61) & 0x20) == 0);
  177. tr2 = get_cycles();
  178. tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
  179. local_irq_restore(flags);
  180. /*
  181. * Preset the result with the raw and inaccurate PIT
  182. * calibration value
  183. */
  184. tsc_khz = (tr2 - tr1) / 50;
  185. /* hpet or pmtimer available ? */
  186. if (!hpet && !pm1 && !pm2) {
  187. printk(KERN_INFO "TSC calibrated against PIT\n");
  188. return;
  189. }
  190. /* Check, whether the sampling was disturbed by an SMI */
  191. if (tsc1 == ULONG_MAX || tsc2 == ULONG_MAX) {
  192. printk(KERN_WARNING "TSC calibration disturbed by SMI, "
  193. "using PIT calibration result\n");
  194. return;
  195. }
  196. tsc2 = (tsc2 - tsc1) * 1000000L;
  197. if (hpet) {
  198. printk(KERN_INFO "TSC calibrated against HPET\n");
  199. if (hpet2 < hpet1)
  200. hpet2 += 0x100000000;
  201. hpet2 -= hpet1;
  202. tsc1 = (hpet2 * hpet_readl(HPET_PERIOD)) / 1000000;
  203. } else {
  204. printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
  205. if (pm2 < pm1)
  206. pm2 += ACPI_PM_OVRRUN;
  207. pm2 -= pm1;
  208. tsc1 = (pm2 * 1000000000) / PMTMR_TICKS_PER_SEC;
  209. }
  210. tsc_khz = tsc2 / tsc1;
  211. for_each_possible_cpu(cpu)
  212. set_cyc2ns_scale(tsc_khz, cpu);
  213. }
  214. /*
  215. * Make an educated guess if the TSC is trustworthy and synchronized
  216. * over all CPUs.
  217. */
  218. __cpuinit int unsynchronized_tsc(void)
  219. {
  220. if (tsc_unstable)
  221. return 1;
  222. #ifdef CONFIG_SMP
  223. if (apic_is_clustered_box())
  224. return 1;
  225. #endif
  226. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  227. return 0;
  228. /* Assume multi socket systems are not synchronized */
  229. return num_present_cpus() > 1;
  230. }
  231. int __init notsc_setup(char *s)
  232. {
  233. notsc = 1;
  234. return 1;
  235. }
  236. __setup("notsc", notsc_setup);
  237. /* clock source code: */
  238. static cycle_t read_tsc(void)
  239. {
  240. cycle_t ret = (cycle_t)get_cycles();
  241. return ret;
  242. }
  243. static cycle_t __vsyscall_fn vread_tsc(void)
  244. {
  245. cycle_t ret = (cycle_t)vget_cycles();
  246. return ret;
  247. }
  248. static struct clocksource clocksource_tsc = {
  249. .name = "tsc",
  250. .rating = 300,
  251. .read = read_tsc,
  252. .mask = CLOCKSOURCE_MASK(64),
  253. .shift = 22,
  254. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  255. CLOCK_SOURCE_MUST_VERIFY,
  256. .vread = vread_tsc,
  257. };
  258. void mark_tsc_unstable(char *reason)
  259. {
  260. if (!tsc_unstable) {
  261. tsc_unstable = 1;
  262. printk("Marking TSC unstable due to %s\n", reason);
  263. /* Change only the rating, when not registered */
  264. if (clocksource_tsc.mult)
  265. clocksource_change_rating(&clocksource_tsc, 0);
  266. else
  267. clocksource_tsc.rating = 0;
  268. }
  269. }
  270. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  271. void __init init_tsc_clocksource(void)
  272. {
  273. if (!notsc) {
  274. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  275. clocksource_tsc.shift);
  276. if (check_tsc_unstable())
  277. clocksource_tsc.rating = 0;
  278. clocksource_register(&clocksource_tsc);
  279. }
  280. }