tsc_32.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include <linux/sched.h>
  2. #include <linux/clocksource.h>
  3. #include <linux/workqueue.h>
  4. #include <linux/delay.h>
  5. #include <linux/cpufreq.h>
  6. #include <linux/jiffies.h>
  7. #include <linux/init.h>
  8. #include <linux/dmi.h>
  9. #include <linux/percpu.h>
  10. #include <asm/delay.h>
  11. #include <asm/tsc.h>
  12. #include <asm/io.h>
  13. #include <asm/timer.h>
  14. #include "mach_timer.h"
  15. extern int tsc_unstable;
  16. extern int tsc_disabled;
  17. /* Accelerators for sched_clock()
  18. * convert from cycles(64bits) => nanoseconds (64bits)
  19. * basic equation:
  20. * ns = cycles / (freq / ns_per_sec)
  21. * ns = cycles * (ns_per_sec / freq)
  22. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  23. * ns = cycles * (10^6 / cpu_khz)
  24. *
  25. * Then we use scaling math (suggested by george@mvista.com) to get:
  26. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  27. * ns = cycles * cyc2ns_scale / SC
  28. *
  29. * And since SC is a constant power of two, we can convert the div
  30. * into a shift.
  31. *
  32. * We can use khz divisor instead of mhz to keep a better precision, since
  33. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  34. * (mathieu.desnoyers@polymtl.ca)
  35. *
  36. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  37. */
  38. DEFINE_PER_CPU(unsigned long, cyc2ns);
  39. void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
  40. {
  41. unsigned long long tsc_now, ns_now;
  42. unsigned long flags, *scale;
  43. local_irq_save(flags);
  44. sched_clock_idle_sleep_event();
  45. scale = &per_cpu(cyc2ns, cpu);
  46. rdtscll(tsc_now);
  47. ns_now = __cycles_2_ns(tsc_now);
  48. if (cpu_khz)
  49. *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
  50. /*
  51. * Start smoothly with the new frequency:
  52. */
  53. sched_clock_idle_wakeup_event(0);
  54. local_irq_restore(flags);
  55. }
  56. #ifdef CONFIG_CPU_FREQ
  57. /*
  58. * if the CPU frequency is scaled, TSC-based delays will need a different
  59. * loops_per_jiffy value to function properly.
  60. */
  61. static unsigned int ref_freq;
  62. static unsigned long loops_per_jiffy_ref;
  63. static unsigned long cpu_khz_ref;
  64. static int
  65. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  66. {
  67. struct cpufreq_freqs *freq = data;
  68. if (!ref_freq) {
  69. if (!freq->old){
  70. ref_freq = freq->new;
  71. return 0;
  72. }
  73. ref_freq = freq->old;
  74. loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
  75. cpu_khz_ref = cpu_khz;
  76. }
  77. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  78. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  79. (val == CPUFREQ_RESUMECHANGE)) {
  80. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  81. cpu_data(freq->cpu).loops_per_jiffy =
  82. cpufreq_scale(loops_per_jiffy_ref,
  83. ref_freq, freq->new);
  84. if (cpu_khz) {
  85. if (num_online_cpus() == 1)
  86. cpu_khz = cpufreq_scale(cpu_khz_ref,
  87. ref_freq, freq->new);
  88. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  89. tsc_khz = cpu_khz;
  90. set_cyc2ns_scale(cpu_khz, freq->cpu);
  91. /*
  92. * TSC based sched_clock turns
  93. * to junk w/ cpufreq
  94. */
  95. mark_tsc_unstable("cpufreq changes");
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101. static struct notifier_block time_cpufreq_notifier_block = {
  102. .notifier_call = time_cpufreq_notifier
  103. };
  104. static int __init cpufreq_tsc(void)
  105. {
  106. return cpufreq_register_notifier(&time_cpufreq_notifier_block,
  107. CPUFREQ_TRANSITION_NOTIFIER);
  108. }
  109. core_initcall(cpufreq_tsc);
  110. #endif
  111. /* clock source code */
  112. static struct clocksource clocksource_tsc;
  113. /*
  114. * We compare the TSC to the cycle_last value in the clocksource
  115. * structure to avoid a nasty time-warp issue. This can be observed in
  116. * a very small window right after one CPU updated cycle_last under
  117. * xtime lock and the other CPU reads a TSC value which is smaller
  118. * than the cycle_last reference value due to a TSC which is slighty
  119. * behind. This delta is nowhere else observable, but in that case it
  120. * results in a forward time jump in the range of hours due to the
  121. * unsigned delta calculation of the time keeping core code, which is
  122. * necessary to support wrapping clocksources like pm timer.
  123. */
  124. static cycle_t read_tsc(void)
  125. {
  126. cycle_t ret;
  127. rdtscll(ret);
  128. return ret >= clocksource_tsc.cycle_last ?
  129. ret : clocksource_tsc.cycle_last;
  130. }
  131. static struct clocksource clocksource_tsc = {
  132. .name = "tsc",
  133. .rating = 300,
  134. .read = read_tsc,
  135. .mask = CLOCKSOURCE_MASK(64),
  136. .mult = 0, /* to be set */
  137. .shift = 22,
  138. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  139. CLOCK_SOURCE_MUST_VERIFY,
  140. };
  141. void mark_tsc_unstable(char *reason)
  142. {
  143. if (!tsc_unstable) {
  144. tsc_unstable = 1;
  145. printk("Marking TSC unstable due to: %s.\n", reason);
  146. /* Can be called before registration */
  147. if (clocksource_tsc.mult)
  148. clocksource_change_rating(&clocksource_tsc, 0);
  149. else
  150. clocksource_tsc.rating = 0;
  151. }
  152. }
  153. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  154. static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
  155. {
  156. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  157. d->ident);
  158. tsc_unstable = 1;
  159. return 0;
  160. }
  161. /* List of systems that have known TSC problems */
  162. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  163. {
  164. .callback = dmi_mark_tsc_unstable,
  165. .ident = "IBM Thinkpad 380XD",
  166. .matches = {
  167. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  168. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  169. },
  170. },
  171. {}
  172. };
  173. /*
  174. * Make an educated guess if the TSC is trustworthy and synchronized
  175. * over all CPUs.
  176. */
  177. __cpuinit int unsynchronized_tsc(void)
  178. {
  179. if (!cpu_has_tsc || tsc_unstable)
  180. return 1;
  181. /* Anything with constant TSC should be synchronized */
  182. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  183. return 0;
  184. /*
  185. * Intel systems are normally all synchronized.
  186. * Exceptions must mark TSC as unstable:
  187. */
  188. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  189. /* assume multi socket systems are not synchronized: */
  190. if (num_possible_cpus() > 1)
  191. tsc_unstable = 1;
  192. }
  193. return tsc_unstable;
  194. }
  195. /*
  196. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  197. */
  198. #ifdef CONFIG_MGEODE_LX
  199. /* RTSC counts during suspend */
  200. #define RTSC_SUSP 0x100
  201. static void __init check_geode_tsc_reliable(void)
  202. {
  203. unsigned long res_low, res_high;
  204. rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
  205. if (res_low & RTSC_SUSP)
  206. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  207. }
  208. #else
  209. static inline void check_geode_tsc_reliable(void) { }
  210. #endif
  211. void __init tsc_init(void)
  212. {
  213. int cpu;
  214. u64 lpj;
  215. if (!cpu_has_tsc || tsc_disabled > 0)
  216. return;
  217. cpu_khz = calculate_cpu_khz();
  218. tsc_khz = cpu_khz;
  219. if (!cpu_khz) {
  220. mark_tsc_unstable("could not calculate TSC khz");
  221. return;
  222. }
  223. lpj = ((u64)tsc_khz * 1000);
  224. do_div(lpj, HZ);
  225. lpj_fine = lpj;
  226. /* now allow native_sched_clock() to use rdtsc */
  227. tsc_disabled = 0;
  228. printk("Detected %lu.%03lu MHz processor.\n",
  229. (unsigned long)cpu_khz / 1000,
  230. (unsigned long)cpu_khz % 1000);
  231. /*
  232. * Secondary CPUs do not run through tsc_init(), so set up
  233. * all the scale factors for all CPUs, assuming the same
  234. * speed as the bootup CPU. (cpufreq notifiers will fix this
  235. * up if their speed diverges)
  236. */
  237. for_each_possible_cpu(cpu)
  238. set_cyc2ns_scale(cpu_khz, cpu);
  239. use_tsc_delay();
  240. /* Check and install the TSC clocksource */
  241. dmi_check_system(bad_tsc_dmi_table);
  242. unsynchronized_tsc();
  243. check_geode_tsc_reliable();
  244. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  245. clocksource_tsc.shift);
  246. /* lower the rating if we already know its unstable: */
  247. if (check_tsc_unstable()) {
  248. clocksource_tsc.rating = 0;
  249. clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
  250. }
  251. clocksource_register(&clocksource_tsc);
  252. }