tsc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include <linux/kernel.h>
  2. #include <linux/sched.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/timer.h>
  6. #include <linux/acpi_pmtmr.h>
  7. #include <linux/cpufreq.h>
  8. #include <asm/hpet.h>
  9. unsigned int cpu_khz; /* TSC clocks / usec, not used here */
  10. EXPORT_SYMBOL(cpu_khz);
  11. unsigned int tsc_khz;
  12. EXPORT_SYMBOL(tsc_khz);
  13. /*
  14. * TSC can be unstable due to cpufreq or due to unsynced TSCs
  15. */
  16. int tsc_unstable;
  17. /* native_sched_clock() is called before tsc_init(), so
  18. we must start with the TSC soft disabled to prevent
  19. erroneous rdtsc usage on !cpu_has_tsc processors */
  20. int tsc_disabled = -1;
  21. /*
  22. * Scheduler clock - returns current time in nanosec units.
  23. */
  24. u64 native_sched_clock(void)
  25. {
  26. u64 this_offset;
  27. /*
  28. * Fall back to jiffies if there's no TSC available:
  29. * ( But note that we still use it if the TSC is marked
  30. * unstable. We do this because unlike Time Of Day,
  31. * the scheduler clock tolerates small errors and it's
  32. * very important for it to be as fast as the platform
  33. * can achive it. )
  34. */
  35. if (unlikely(tsc_disabled)) {
  36. /* No locking but a rare wrong value is not a big deal: */
  37. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  38. }
  39. /* read the Time Stamp Counter: */
  40. rdtscll(this_offset);
  41. /* return the value in ns */
  42. return cycles_2_ns(this_offset);
  43. }
  44. /* We need to define a real function for sched_clock, to override the
  45. weak default version */
  46. #ifdef CONFIG_PARAVIRT
  47. unsigned long long sched_clock(void)
  48. {
  49. return paravirt_sched_clock();
  50. }
  51. #else
  52. unsigned long long
  53. sched_clock(void) __attribute__((alias("native_sched_clock")));
  54. #endif
  55. int check_tsc_unstable(void)
  56. {
  57. return tsc_unstable;
  58. }
  59. EXPORT_SYMBOL_GPL(check_tsc_unstable);
  60. #ifdef CONFIG_X86_TSC
  61. int __init notsc_setup(char *str)
  62. {
  63. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  64. "cannot disable TSC completely.\n");
  65. tsc_disabled = 1;
  66. return 1;
  67. }
  68. #else
  69. /*
  70. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  71. * in cpu/common.c
  72. */
  73. int __init notsc_setup(char *str)
  74. {
  75. setup_clear_cpu_cap(X86_FEATURE_TSC);
  76. return 1;
  77. }
  78. #endif
  79. __setup("notsc", notsc_setup);
  80. #define MAX_RETRIES 5
  81. #define SMI_TRESHOLD 50000
  82. /*
  83. * Read TSC and the reference counters. Take care of SMI disturbance
  84. */
  85. static u64 __init tsc_read_refs(u64 *pm, u64 *hpet)
  86. {
  87. u64 t1, t2;
  88. int i;
  89. for (i = 0; i < MAX_RETRIES; i++) {
  90. t1 = get_cycles();
  91. if (hpet)
  92. *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  93. else
  94. *pm = acpi_pm_read_early();
  95. t2 = get_cycles();
  96. if ((t2 - t1) < SMI_TRESHOLD)
  97. return t2;
  98. }
  99. return ULLONG_MAX;
  100. }
  101. /**
  102. * tsc_calibrate - calibrate the tsc on boot
  103. */
  104. static unsigned int __init tsc_calibrate(void)
  105. {
  106. unsigned long flags;
  107. u64 tsc1, tsc2, tr1, tr2, delta, pm1, pm2, hpet1, hpet2;
  108. int hpet = is_hpet_enabled();
  109. unsigned int tsc_khz_val = 0;
  110. local_irq_save(flags);
  111. tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
  112. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  113. outb(0xb0, 0x43);
  114. outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  115. outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  116. tr1 = get_cycles();
  117. while ((inb(0x61) & 0x20) == 0);
  118. tr2 = get_cycles();
  119. tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
  120. local_irq_restore(flags);
  121. /*
  122. * Preset the result with the raw and inaccurate PIT
  123. * calibration value
  124. */
  125. delta = (tr2 - tr1);
  126. do_div(delta, 50);
  127. tsc_khz_val = delta;
  128. /* hpet or pmtimer available ? */
  129. if (!hpet && !pm1 && !pm2) {
  130. printk(KERN_INFO "TSC calibrated against PIT\n");
  131. goto out;
  132. }
  133. /* Check, whether the sampling was disturbed by an SMI */
  134. if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX) {
  135. printk(KERN_WARNING "TSC calibration disturbed by SMI, "
  136. "using PIT calibration result\n");
  137. goto out;
  138. }
  139. tsc2 = (tsc2 - tsc1) * 1000000LL;
  140. if (hpet) {
  141. printk(KERN_INFO "TSC calibrated against HPET\n");
  142. if (hpet2 < hpet1)
  143. hpet2 += 0x100000000ULL;
  144. hpet2 -= hpet1;
  145. tsc1 = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
  146. do_div(tsc1, 1000000);
  147. } else {
  148. printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
  149. if (pm2 < pm1)
  150. pm2 += (u64)ACPI_PM_OVRRUN;
  151. pm2 -= pm1;
  152. tsc1 = pm2 * 1000000000LL;
  153. do_div(tsc1, PMTMR_TICKS_PER_SEC);
  154. }
  155. do_div(tsc2, tsc1);
  156. tsc_khz_val = tsc2;
  157. out:
  158. return tsc_khz_val;
  159. }
  160. unsigned long native_calculate_cpu_khz(void)
  161. {
  162. return tsc_calibrate();
  163. }
  164. #ifdef CONFIG_X86_32
  165. /* Only called from the Powernow K7 cpu freq driver */
  166. int recalibrate_cpu_khz(void)
  167. {
  168. #ifndef CONFIG_SMP
  169. unsigned long cpu_khz_old = cpu_khz;
  170. if (cpu_has_tsc) {
  171. cpu_khz = calculate_cpu_khz();
  172. tsc_khz = cpu_khz;
  173. cpu_data(0).loops_per_jiffy =
  174. cpufreq_scale(cpu_data(0).loops_per_jiffy,
  175. cpu_khz_old, cpu_khz);
  176. return 0;
  177. } else
  178. return -ENODEV;
  179. #else
  180. return -ENODEV;
  181. #endif
  182. }
  183. EXPORT_SYMBOL(recalibrate_cpu_khz);
  184. #endif /* CONFIG_X86_32 */
  185. /* Accelerators for sched_clock()
  186. * convert from cycles(64bits) => nanoseconds (64bits)
  187. * basic equation:
  188. * ns = cycles / (freq / ns_per_sec)
  189. * ns = cycles * (ns_per_sec / freq)
  190. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  191. * ns = cycles * (10^6 / cpu_khz)
  192. *
  193. * Then we use scaling math (suggested by george@mvista.com) to get:
  194. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  195. * ns = cycles * cyc2ns_scale / SC
  196. *
  197. * And since SC is a constant power of two, we can convert the div
  198. * into a shift.
  199. *
  200. * We can use khz divisor instead of mhz to keep a better precision, since
  201. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  202. * (mathieu.desnoyers@polymtl.ca)
  203. *
  204. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  205. */
  206. DEFINE_PER_CPU(unsigned long, cyc2ns);
  207. void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
  208. {
  209. unsigned long long tsc_now, ns_now;
  210. unsigned long flags, *scale;
  211. local_irq_save(flags);
  212. sched_clock_idle_sleep_event();
  213. scale = &per_cpu(cyc2ns, cpu);
  214. rdtscll(tsc_now);
  215. ns_now = __cycles_2_ns(tsc_now);
  216. if (cpu_khz)
  217. *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
  218. sched_clock_idle_wakeup_event(0);
  219. local_irq_restore(flags);
  220. }
  221. #ifdef CONFIG_CPU_FREQ
  222. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  223. * changes.
  224. *
  225. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  226. * not that important because current Opteron setups do not support
  227. * scaling on SMP anyroads.
  228. *
  229. * Should fix up last_tsc too. Currently gettimeofday in the
  230. * first tick after the change will be slightly wrong.
  231. */
  232. static unsigned int ref_freq;
  233. static unsigned long loops_per_jiffy_ref;
  234. static unsigned long tsc_khz_ref;
  235. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  236. void *data)
  237. {
  238. struct cpufreq_freqs *freq = data;
  239. unsigned long *lpj, dummy;
  240. if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
  241. return 0;
  242. lpj = &dummy;
  243. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  244. #ifdef CONFIG_SMP
  245. lpj = &cpu_data(freq->cpu).loops_per_jiffy;
  246. #else
  247. lpj = &boot_cpu_data.loops_per_jiffy;
  248. #endif
  249. if (!ref_freq) {
  250. ref_freq = freq->old;
  251. loops_per_jiffy_ref = *lpj;
  252. tsc_khz_ref = tsc_khz;
  253. }
  254. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  255. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  256. (val == CPUFREQ_RESUMECHANGE)) {
  257. *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  258. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  259. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  260. mark_tsc_unstable("cpufreq changes");
  261. }
  262. set_cyc2ns_scale(tsc_khz_ref, freq->cpu);
  263. return 0;
  264. }
  265. static struct notifier_block time_cpufreq_notifier_block = {
  266. .notifier_call = time_cpufreq_notifier
  267. };
  268. static int __init cpufreq_tsc(void)
  269. {
  270. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  271. CPUFREQ_TRANSITION_NOTIFIER);
  272. return 0;
  273. }
  274. core_initcall(cpufreq_tsc);
  275. #endif /* CONFIG_CPU_FREQ */