tsc_32.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. static 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. unsigned long native_calculate_cpu_khz(void)
  57. {
  58. unsigned long long start, end;
  59. unsigned long count;
  60. u64 delta64 = (u64)ULLONG_MAX;
  61. int i;
  62. unsigned long flags;
  63. local_irq_save(flags);
  64. /* run 3 times to ensure the cache is warm and to get an accurate reading */
  65. for (i = 0; i < 3; i++) {
  66. mach_prepare_counter();
  67. rdtscll(start);
  68. mach_countup(&count);
  69. rdtscll(end);
  70. /*
  71. * Error: ECTCNEVERSET
  72. * The CTC wasn't reliable: we got a hit on the very first read,
  73. * or the CPU was so fast/slow that the quotient wouldn't fit in
  74. * 32 bits..
  75. */
  76. if (count <= 1)
  77. continue;
  78. /* cpu freq too slow: */
  79. if ((end - start) <= CALIBRATE_TIME_MSEC)
  80. continue;
  81. /*
  82. * We want the minimum time of all runs in case one of them
  83. * is inaccurate due to SMI or other delay
  84. */
  85. delta64 = min(delta64, (end - start));
  86. }
  87. /* cpu freq too fast (or every run was bad): */
  88. if (delta64 > (1ULL<<32))
  89. goto err;
  90. delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
  91. do_div(delta64,CALIBRATE_TIME_MSEC);
  92. local_irq_restore(flags);
  93. return (unsigned long)delta64;
  94. err:
  95. local_irq_restore(flags);
  96. return 0;
  97. }
  98. int recalibrate_cpu_khz(void)
  99. {
  100. #ifndef CONFIG_SMP
  101. unsigned long cpu_khz_old = cpu_khz;
  102. if (cpu_has_tsc) {
  103. cpu_khz = calculate_cpu_khz();
  104. tsc_khz = cpu_khz;
  105. cpu_data(0).loops_per_jiffy =
  106. cpufreq_scale(cpu_data(0).loops_per_jiffy,
  107. cpu_khz_old, cpu_khz);
  108. return 0;
  109. } else
  110. return -ENODEV;
  111. #else
  112. return -ENODEV;
  113. #endif
  114. }
  115. EXPORT_SYMBOL(recalibrate_cpu_khz);
  116. #ifdef CONFIG_CPU_FREQ
  117. /*
  118. * if the CPU frequency is scaled, TSC-based delays will need a different
  119. * loops_per_jiffy value to function properly.
  120. */
  121. static unsigned int ref_freq;
  122. static unsigned long loops_per_jiffy_ref;
  123. static unsigned long cpu_khz_ref;
  124. static int
  125. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  126. {
  127. struct cpufreq_freqs *freq = data;
  128. if (!ref_freq) {
  129. if (!freq->old){
  130. ref_freq = freq->new;
  131. return 0;
  132. }
  133. ref_freq = freq->old;
  134. loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
  135. cpu_khz_ref = cpu_khz;
  136. }
  137. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  138. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  139. (val == CPUFREQ_RESUMECHANGE)) {
  140. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  141. cpu_data(freq->cpu).loops_per_jiffy =
  142. cpufreq_scale(loops_per_jiffy_ref,
  143. ref_freq, freq->new);
  144. if (cpu_khz) {
  145. if (num_online_cpus() == 1)
  146. cpu_khz = cpufreq_scale(cpu_khz_ref,
  147. ref_freq, freq->new);
  148. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  149. tsc_khz = cpu_khz;
  150. set_cyc2ns_scale(cpu_khz, freq->cpu);
  151. /*
  152. * TSC based sched_clock turns
  153. * to junk w/ cpufreq
  154. */
  155. mark_tsc_unstable("cpufreq changes");
  156. }
  157. }
  158. }
  159. return 0;
  160. }
  161. static struct notifier_block time_cpufreq_notifier_block = {
  162. .notifier_call = time_cpufreq_notifier
  163. };
  164. static int __init cpufreq_tsc(void)
  165. {
  166. return cpufreq_register_notifier(&time_cpufreq_notifier_block,
  167. CPUFREQ_TRANSITION_NOTIFIER);
  168. }
  169. core_initcall(cpufreq_tsc);
  170. #endif
  171. /* clock source code */
  172. static struct clocksource clocksource_tsc;
  173. /*
  174. * We compare the TSC to the cycle_last value in the clocksource
  175. * structure to avoid a nasty time-warp issue. This can be observed in
  176. * a very small window right after one CPU updated cycle_last under
  177. * xtime lock and the other CPU reads a TSC value which is smaller
  178. * than the cycle_last reference value due to a TSC which is slighty
  179. * behind. This delta is nowhere else observable, but in that case it
  180. * results in a forward time jump in the range of hours due to the
  181. * unsigned delta calculation of the time keeping core code, which is
  182. * necessary to support wrapping clocksources like pm timer.
  183. */
  184. static cycle_t read_tsc(void)
  185. {
  186. cycle_t ret;
  187. rdtscll(ret);
  188. return ret >= clocksource_tsc.cycle_last ?
  189. ret : clocksource_tsc.cycle_last;
  190. }
  191. static struct clocksource clocksource_tsc = {
  192. .name = "tsc",
  193. .rating = 300,
  194. .read = read_tsc,
  195. .mask = CLOCKSOURCE_MASK(64),
  196. .mult = 0, /* to be set */
  197. .shift = 22,
  198. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  199. CLOCK_SOURCE_MUST_VERIFY,
  200. };
  201. void mark_tsc_unstable(char *reason)
  202. {
  203. if (!tsc_unstable) {
  204. tsc_unstable = 1;
  205. printk("Marking TSC unstable due to: %s.\n", reason);
  206. /* Can be called before registration */
  207. if (clocksource_tsc.mult)
  208. clocksource_change_rating(&clocksource_tsc, 0);
  209. else
  210. clocksource_tsc.rating = 0;
  211. }
  212. }
  213. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  214. static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
  215. {
  216. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  217. d->ident);
  218. tsc_unstable = 1;
  219. return 0;
  220. }
  221. /* List of systems that have known TSC problems */
  222. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  223. {
  224. .callback = dmi_mark_tsc_unstable,
  225. .ident = "IBM Thinkpad 380XD",
  226. .matches = {
  227. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  228. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  229. },
  230. },
  231. {}
  232. };
  233. /*
  234. * Make an educated guess if the TSC is trustworthy and synchronized
  235. * over all CPUs.
  236. */
  237. __cpuinit int unsynchronized_tsc(void)
  238. {
  239. if (!cpu_has_tsc || tsc_unstable)
  240. return 1;
  241. /* Anything with constant TSC should be synchronized */
  242. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  243. return 0;
  244. /*
  245. * Intel systems are normally all synchronized.
  246. * Exceptions must mark TSC as unstable:
  247. */
  248. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  249. /* assume multi socket systems are not synchronized: */
  250. if (num_possible_cpus() > 1)
  251. tsc_unstable = 1;
  252. }
  253. return tsc_unstable;
  254. }
  255. /*
  256. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  257. */
  258. #ifdef CONFIG_MGEODE_LX
  259. /* RTSC counts during suspend */
  260. #define RTSC_SUSP 0x100
  261. static void __init check_geode_tsc_reliable(void)
  262. {
  263. unsigned long res_low, res_high;
  264. rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
  265. if (res_low & RTSC_SUSP)
  266. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  267. }
  268. #else
  269. static inline void check_geode_tsc_reliable(void) { }
  270. #endif
  271. void __init tsc_init(void)
  272. {
  273. int cpu;
  274. u64 lpj;
  275. if (!cpu_has_tsc || tsc_disabled > 0)
  276. return;
  277. cpu_khz = calculate_cpu_khz();
  278. tsc_khz = cpu_khz;
  279. if (!cpu_khz) {
  280. mark_tsc_unstable("could not calculate TSC khz");
  281. return;
  282. }
  283. lpj = ((u64)tsc_khz * 1000);
  284. do_div(lpj, HZ);
  285. lpj_fine = lpj;
  286. /* now allow native_sched_clock() to use rdtsc */
  287. tsc_disabled = 0;
  288. printk("Detected %lu.%03lu MHz processor.\n",
  289. (unsigned long)cpu_khz / 1000,
  290. (unsigned long)cpu_khz % 1000);
  291. /*
  292. * Secondary CPUs do not run through tsc_init(), so set up
  293. * all the scale factors for all CPUs, assuming the same
  294. * speed as the bootup CPU. (cpufreq notifiers will fix this
  295. * up if their speed diverges)
  296. */
  297. for_each_possible_cpu(cpu)
  298. set_cyc2ns_scale(cpu_khz, cpu);
  299. use_tsc_delay();
  300. /* Check and install the TSC clocksource */
  301. dmi_check_system(bad_tsc_dmi_table);
  302. unsynchronized_tsc();
  303. check_geode_tsc_reliable();
  304. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  305. clocksource_tsc.shift);
  306. /* lower the rating if we already know its unstable: */
  307. if (check_tsc_unstable()) {
  308. clocksource_tsc.rating = 0;
  309. clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
  310. }
  311. clocksource_register(&clocksource_tsc);
  312. }