tsc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * This code largely moved from arch/i386/kernel/timer/timer_tsc.c
  3. * which was originally moved from arch/i386/kernel/time.c.
  4. * See comments there for proper credits.
  5. */
  6. #include <linux/workqueue.h>
  7. #include <linux/cpufreq.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/init.h>
  10. #include <asm/tsc.h>
  11. #include <asm/io.h>
  12. #include "mach_timer.h"
  13. /*
  14. * On some systems the TSC frequency does not
  15. * change with the cpu frequency. So we need
  16. * an extra value to store the TSC freq
  17. */
  18. unsigned int tsc_khz;
  19. int tsc_disable __cpuinitdata = 0;
  20. #ifdef CONFIG_X86_TSC
  21. static int __init tsc_setup(char *str)
  22. {
  23. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  24. "cannot disable TSC.\n");
  25. return 1;
  26. }
  27. #else
  28. /*
  29. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  30. * in cpu/common.c
  31. */
  32. static int __init tsc_setup(char *str)
  33. {
  34. tsc_disable = 1;
  35. return 1;
  36. }
  37. #endif
  38. __setup("notsc", tsc_setup);
  39. /*
  40. * code to mark and check if the TSC is unstable
  41. * due to cpufreq or due to unsynced TSCs
  42. */
  43. static int tsc_unstable;
  44. static inline int check_tsc_unstable(void)
  45. {
  46. return tsc_unstable;
  47. }
  48. void mark_tsc_unstable(void)
  49. {
  50. tsc_unstable = 1;
  51. }
  52. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  53. /* Accellerators for sched_clock()
  54. * convert from cycles(64bits) => nanoseconds (64bits)
  55. * basic equation:
  56. * ns = cycles / (freq / ns_per_sec)
  57. * ns = cycles * (ns_per_sec / freq)
  58. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  59. * ns = cycles * (10^6 / cpu_khz)
  60. *
  61. * Then we use scaling math (suggested by george@mvista.com) to get:
  62. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  63. * ns = cycles * cyc2ns_scale / SC
  64. *
  65. * And since SC is a constant power of two, we can convert the div
  66. * into a shift.
  67. *
  68. * We can use khz divisor instead of mhz to keep a better percision, since
  69. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  70. * (mathieu.desnoyers@polymtl.ca)
  71. *
  72. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  73. */
  74. static unsigned long cyc2ns_scale __read_mostly;
  75. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  76. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  77. {
  78. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  79. }
  80. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  81. {
  82. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  83. }
  84. /*
  85. * Scheduler clock - returns current time in nanosec units.
  86. */
  87. unsigned long long sched_clock(void)
  88. {
  89. unsigned long long this_offset;
  90. /*
  91. * in the NUMA case we dont use the TSC as they are not
  92. * synchronized across all CPUs.
  93. */
  94. #ifndef CONFIG_NUMA
  95. if (!cpu_khz || check_tsc_unstable())
  96. #endif
  97. /* no locking but a rare wrong value is not a big deal */
  98. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  99. /* read the Time Stamp Counter: */
  100. rdtscll(this_offset);
  101. /* return the value in ns */
  102. return cycles_2_ns(this_offset);
  103. }
  104. static unsigned long calculate_cpu_khz(void)
  105. {
  106. unsigned long long start, end;
  107. unsigned long count;
  108. u64 delta64;
  109. int i;
  110. unsigned long flags;
  111. local_irq_save(flags);
  112. /* run 3 times to ensure the cache is warm */
  113. for (i = 0; i < 3; i++) {
  114. mach_prepare_counter();
  115. rdtscll(start);
  116. mach_countup(&count);
  117. rdtscll(end);
  118. }
  119. /*
  120. * Error: ECTCNEVERSET
  121. * The CTC wasn't reliable: we got a hit on the very first read,
  122. * or the CPU was so fast/slow that the quotient wouldn't fit in
  123. * 32 bits..
  124. */
  125. if (count <= 1)
  126. goto err;
  127. delta64 = end - start;
  128. /* cpu freq too fast: */
  129. if (delta64 > (1ULL<<32))
  130. goto err;
  131. /* cpu freq too slow: */
  132. if (delta64 <= CALIBRATE_TIME_MSEC)
  133. goto err;
  134. delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
  135. do_div(delta64,CALIBRATE_TIME_MSEC);
  136. local_irq_restore(flags);
  137. return (unsigned long)delta64;
  138. err:
  139. local_irq_restore(flags);
  140. return 0;
  141. }
  142. int recalibrate_cpu_khz(void)
  143. {
  144. #ifndef CONFIG_SMP
  145. unsigned long cpu_khz_old = cpu_khz;
  146. if (cpu_has_tsc) {
  147. cpu_khz = calculate_cpu_khz();
  148. tsc_khz = cpu_khz;
  149. cpu_data[0].loops_per_jiffy =
  150. cpufreq_scale(cpu_data[0].loops_per_jiffy,
  151. cpu_khz_old, cpu_khz);
  152. return 0;
  153. } else
  154. return -ENODEV;
  155. #else
  156. return -ENODEV;
  157. #endif
  158. }
  159. EXPORT_SYMBOL(recalibrate_cpu_khz);
  160. void tsc_init(void)
  161. {
  162. if (!cpu_has_tsc || tsc_disable)
  163. return;
  164. cpu_khz = calculate_cpu_khz();
  165. tsc_khz = cpu_khz;
  166. if (!cpu_khz)
  167. return;
  168. printk("Detected %lu.%03lu MHz processor.\n",
  169. (unsigned long)cpu_khz / 1000,
  170. (unsigned long)cpu_khz % 1000);
  171. set_cyc2ns_scale(cpu_khz);
  172. }
  173. #ifdef CONFIG_CPU_FREQ
  174. static unsigned int cpufreq_delayed_issched = 0;
  175. static unsigned int cpufreq_init = 0;
  176. static struct work_struct cpufreq_delayed_get_work;
  177. static void handle_cpufreq_delayed_get(void *v)
  178. {
  179. unsigned int cpu;
  180. for_each_online_cpu(cpu)
  181. cpufreq_get(cpu);
  182. cpufreq_delayed_issched = 0;
  183. }
  184. /*
  185. * if we notice cpufreq oddness, schedule a call to cpufreq_get() as it tries
  186. * to verify the CPU frequency the timing core thinks the CPU is running
  187. * at is still correct.
  188. */
  189. static inline void cpufreq_delayed_get(void)
  190. {
  191. if (cpufreq_init && !cpufreq_delayed_issched) {
  192. cpufreq_delayed_issched = 1;
  193. printk(KERN_DEBUG "Checking if CPU frequency changed.\n");
  194. schedule_work(&cpufreq_delayed_get_work);
  195. }
  196. }
  197. /*
  198. * if the CPU frequency is scaled, TSC-based delays will need a different
  199. * loops_per_jiffy value to function properly.
  200. */
  201. static unsigned int ref_freq = 0;
  202. static unsigned long loops_per_jiffy_ref = 0;
  203. static unsigned long cpu_khz_ref = 0;
  204. static int
  205. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  206. {
  207. struct cpufreq_freqs *freq = data;
  208. if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
  209. write_seqlock_irq(&xtime_lock);
  210. if (!ref_freq) {
  211. if (!freq->old){
  212. ref_freq = freq->new;
  213. goto end;
  214. }
  215. ref_freq = freq->old;
  216. loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
  217. cpu_khz_ref = cpu_khz;
  218. }
  219. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  220. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  221. (val == CPUFREQ_RESUMECHANGE)) {
  222. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  223. cpu_data[freq->cpu].loops_per_jiffy =
  224. cpufreq_scale(loops_per_jiffy_ref,
  225. ref_freq, freq->new);
  226. if (cpu_khz) {
  227. if (num_online_cpus() == 1)
  228. cpu_khz = cpufreq_scale(cpu_khz_ref,
  229. ref_freq, freq->new);
  230. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  231. tsc_khz = cpu_khz;
  232. set_cyc2ns_scale(cpu_khz);
  233. /*
  234. * TSC based sched_clock turns
  235. * to junk w/ cpufreq
  236. */
  237. mark_tsc_unstable();
  238. }
  239. }
  240. }
  241. end:
  242. if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
  243. write_sequnlock_irq(&xtime_lock);
  244. return 0;
  245. }
  246. static struct notifier_block time_cpufreq_notifier_block = {
  247. .notifier_call = time_cpufreq_notifier
  248. };
  249. static int __init cpufreq_tsc(void)
  250. {
  251. int ret;
  252. INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);
  253. ret = cpufreq_register_notifier(&time_cpufreq_notifier_block,
  254. CPUFREQ_TRANSITION_NOTIFIER);
  255. if (!ret)
  256. cpufreq_init = 1;
  257. return ret;
  258. }
  259. core_initcall(cpufreq_tsc);
  260. #endif