tsc_64.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 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. static int tsc_unstable;
  67. inline int check_tsc_unstable(void)
  68. {
  69. return tsc_unstable;
  70. }
  71. #ifdef CONFIG_CPU_FREQ
  72. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  73. * changes.
  74. *
  75. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  76. * not that important because current Opteron setups do not support
  77. * scaling on SMP anyroads.
  78. *
  79. * Should fix up last_tsc too. Currently gettimeofday in the
  80. * first tick after the change will be slightly wrong.
  81. */
  82. static unsigned int ref_freq;
  83. static unsigned long loops_per_jiffy_ref;
  84. static unsigned long tsc_khz_ref;
  85. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  86. void *data)
  87. {
  88. struct cpufreq_freqs *freq = data;
  89. unsigned long *lpj, dummy;
  90. if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
  91. return 0;
  92. lpj = &dummy;
  93. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  94. #ifdef CONFIG_SMP
  95. lpj = &cpu_data(freq->cpu).loops_per_jiffy;
  96. #else
  97. lpj = &boot_cpu_data.loops_per_jiffy;
  98. #endif
  99. if (!ref_freq) {
  100. ref_freq = freq->old;
  101. loops_per_jiffy_ref = *lpj;
  102. tsc_khz_ref = tsc_khz;
  103. }
  104. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  105. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  106. (val == CPUFREQ_RESUMECHANGE)) {
  107. *lpj =
  108. cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  109. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  110. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  111. mark_tsc_unstable("cpufreq changes");
  112. }
  113. preempt_disable();
  114. set_cyc2ns_scale(tsc_khz_ref, smp_processor_id());
  115. preempt_enable();
  116. return 0;
  117. }
  118. static struct notifier_block time_cpufreq_notifier_block = {
  119. .notifier_call = time_cpufreq_notifier
  120. };
  121. static int __init cpufreq_tsc(void)
  122. {
  123. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  124. CPUFREQ_TRANSITION_NOTIFIER);
  125. return 0;
  126. }
  127. core_initcall(cpufreq_tsc);
  128. #endif
  129. #define MAX_RETRIES 5
  130. #define SMI_TRESHOLD 50000
  131. /*
  132. * Read TSC and the reference counters. Take care of SMI disturbance
  133. */
  134. static unsigned long __init tsc_read_refs(unsigned long *pm,
  135. unsigned long *hpet)
  136. {
  137. unsigned long t1, t2;
  138. int i;
  139. for (i = 0; i < MAX_RETRIES; i++) {
  140. t1 = get_cycles_sync();
  141. if (hpet)
  142. *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  143. else
  144. *pm = acpi_pm_read_early();
  145. t2 = get_cycles_sync();
  146. if ((t2 - t1) < SMI_TRESHOLD)
  147. return t2;
  148. }
  149. return ULONG_MAX;
  150. }
  151. /**
  152. * tsc_calibrate - calibrate the tsc on boot
  153. */
  154. void __init tsc_calibrate(void)
  155. {
  156. unsigned long flags, tsc1, tsc2, tr1, tr2, pm1, pm2, hpet1, hpet2;
  157. int hpet = is_hpet_enabled(), cpu;
  158. local_irq_save(flags);
  159. tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
  160. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  161. outb(0xb0, 0x43);
  162. outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  163. outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  164. tr1 = get_cycles_sync();
  165. while ((inb(0x61) & 0x20) == 0);
  166. tr2 = get_cycles_sync();
  167. tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
  168. local_irq_restore(flags);
  169. /*
  170. * Preset the result with the raw and inaccurate PIT
  171. * calibration value
  172. */
  173. tsc_khz = (tr2 - tr1) / 50;
  174. /* hpet or pmtimer available ? */
  175. if (!hpet && !pm1 && !pm2) {
  176. printk(KERN_INFO "TSC calibrated against PIT\n");
  177. return;
  178. }
  179. /* Check, whether the sampling was disturbed by an SMI */
  180. if (tsc1 == ULONG_MAX || tsc2 == ULONG_MAX) {
  181. printk(KERN_WARNING "TSC calibration disturbed by SMI, "
  182. "using PIT calibration result\n");
  183. return;
  184. }
  185. tsc2 = (tsc2 - tsc1) * 1000000L;
  186. if (hpet) {
  187. printk(KERN_INFO "TSC calibrated against HPET\n");
  188. if (hpet2 < hpet1)
  189. hpet2 += 0x100000000;
  190. hpet2 -= hpet1;
  191. tsc1 = (hpet2 * hpet_readl(HPET_PERIOD)) / 1000000;
  192. } else {
  193. printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
  194. if (pm2 < pm1)
  195. pm2 += ACPI_PM_OVRRUN;
  196. pm2 -= pm1;
  197. tsc1 = (pm2 * 1000000000) / PMTMR_TICKS_PER_SEC;
  198. }
  199. tsc_khz = tsc2 / tsc1;
  200. for_each_possible_cpu(cpu)
  201. set_cyc2ns_scale(tsc_khz, cpu);
  202. }
  203. /*
  204. * Make an educated guess if the TSC is trustworthy and synchronized
  205. * over all CPUs.
  206. */
  207. __cpuinit int unsynchronized_tsc(void)
  208. {
  209. if (tsc_unstable)
  210. return 1;
  211. #ifdef CONFIG_SMP
  212. if (apic_is_clustered_box())
  213. return 1;
  214. #endif
  215. /* Most intel systems have synchronized TSCs except for
  216. multi node systems */
  217. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  218. #ifdef CONFIG_ACPI
  219. /* But TSC doesn't tick in C3 so don't use it there */
  220. if (acpi_gbl_FADT.header.length > 0 &&
  221. acpi_gbl_FADT.C3latency < 1000)
  222. return 1;
  223. #endif
  224. return 0;
  225. }
  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_sync();
  239. return ret;
  240. }
  241. static cycle_t __vsyscall_fn vread_tsc(void)
  242. {
  243. cycle_t ret = (cycle_t)get_cycles_sync();
  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. }