tsc_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. #include <linux/sched.h>
  2. #include <linux/clocksource.h>
  3. #include <linux/workqueue.h>
  4. #include <linux/cpufreq.h>
  5. #include <linux/jiffies.h>
  6. #include <linux/init.h>
  7. #include <linux/dmi.h>
  8. #include <linux/percpu.h>
  9. #include <asm/delay.h>
  10. #include <asm/tsc.h>
  11. #include <asm/io.h>
  12. #include <asm/timer.h>
  13. #include "mach_timer.h"
  14. static int tsc_enabled;
  15. /*
  16. * On some systems the TSC frequency does not
  17. * change with the cpu frequency. So we need
  18. * an extra value to store the TSC freq
  19. */
  20. unsigned int tsc_khz;
  21. EXPORT_SYMBOL_GPL(tsc_khz);
  22. #ifdef CONFIG_X86_TSC
  23. static int __init tsc_setup(char *str)
  24. {
  25. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  26. "cannot disable TSC completely.\n");
  27. mark_tsc_unstable("user disabled TSC");
  28. return 1;
  29. }
  30. #else
  31. /*
  32. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  33. * in cpu/common.c
  34. */
  35. static int __init tsc_setup(char *str)
  36. {
  37. setup_clear_cpu_cap(X86_FEATURE_TSC);
  38. return 1;
  39. }
  40. #endif
  41. __setup("notsc", tsc_setup);
  42. /*
  43. * code to mark and check if the TSC is unstable
  44. * due to cpufreq or due to unsynced TSCs
  45. */
  46. static int tsc_unstable;
  47. int check_tsc_unstable(void)
  48. {
  49. return tsc_unstable;
  50. }
  51. EXPORT_SYMBOL_GPL(check_tsc_unstable);
  52. /* Accelerators for sched_clock()
  53. * convert from cycles(64bits) => nanoseconds (64bits)
  54. * basic equation:
  55. * ns = cycles / (freq / ns_per_sec)
  56. * ns = cycles * (ns_per_sec / freq)
  57. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  58. * ns = cycles * (10^6 / cpu_khz)
  59. *
  60. * Then we use scaling math (suggested by george@mvista.com) to get:
  61. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  62. * ns = cycles * cyc2ns_scale / SC
  63. *
  64. * And since SC is a constant power of two, we can convert the div
  65. * into a shift.
  66. *
  67. * We can use khz divisor instead of mhz to keep a better precision, since
  68. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  69. * (mathieu.desnoyers@polymtl.ca)
  70. *
  71. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  72. */
  73. DEFINE_PER_CPU(unsigned long, cyc2ns);
  74. static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
  75. {
  76. unsigned long long tsc_now, ns_now;
  77. unsigned long flags, *scale;
  78. local_irq_save(flags);
  79. sched_clock_idle_sleep_event();
  80. scale = &per_cpu(cyc2ns, cpu);
  81. rdtscll(tsc_now);
  82. ns_now = __cycles_2_ns(tsc_now);
  83. if (cpu_khz)
  84. *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
  85. /*
  86. * Start smoothly with the new frequency:
  87. */
  88. sched_clock_idle_wakeup_event(0);
  89. local_irq_restore(flags);
  90. }
  91. /*
  92. * Scheduler clock - returns current time in nanosec units.
  93. */
  94. unsigned long long native_sched_clock(void)
  95. {
  96. unsigned long long this_offset;
  97. /*
  98. * Fall back to jiffies if there's no TSC available:
  99. * ( But note that we still use it if the TSC is marked
  100. * unstable. We do this because unlike Time Of Day,
  101. * the scheduler clock tolerates small errors and it's
  102. * very important for it to be as fast as the platform
  103. * can achive it. )
  104. */
  105. if (unlikely(!tsc_enabled && !tsc_unstable))
  106. /* No locking but a rare wrong value is not a big deal: */
  107. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  108. /* read the Time Stamp Counter: */
  109. rdtscll(this_offset);
  110. /* return the value in ns */
  111. return cycles_2_ns(this_offset);
  112. }
  113. /* We need to define a real function for sched_clock, to override the
  114. weak default version */
  115. #ifdef CONFIG_PARAVIRT
  116. unsigned long long sched_clock(void)
  117. {
  118. return paravirt_sched_clock();
  119. }
  120. #else
  121. unsigned long long sched_clock(void)
  122. __attribute__((alias("native_sched_clock")));
  123. #endif
  124. unsigned long native_calculate_cpu_khz(void)
  125. {
  126. unsigned long long start, end;
  127. unsigned long count;
  128. u64 delta64 = (u64)ULLONG_MAX;
  129. int i;
  130. unsigned long flags;
  131. local_irq_save(flags);
  132. /* run 3 times to ensure the cache is warm and to get an accurate reading */
  133. for (i = 0; i < 3; i++) {
  134. mach_prepare_counter();
  135. rdtscll(start);
  136. mach_countup(&count);
  137. rdtscll(end);
  138. /*
  139. * Error: ECTCNEVERSET
  140. * The CTC wasn't reliable: we got a hit on the very first read,
  141. * or the CPU was so fast/slow that the quotient wouldn't fit in
  142. * 32 bits..
  143. */
  144. if (count <= 1)
  145. continue;
  146. /* cpu freq too slow: */
  147. if ((end - start) <= CALIBRATE_TIME_MSEC)
  148. continue;
  149. /*
  150. * We want the minimum time of all runs in case one of them
  151. * is inaccurate due to SMI or other delay
  152. */
  153. delta64 = min(delta64, (end - start));
  154. }
  155. /* cpu freq too fast (or every run was bad): */
  156. if (delta64 > (1ULL<<32))
  157. goto err;
  158. delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
  159. do_div(delta64,CALIBRATE_TIME_MSEC);
  160. local_irq_restore(flags);
  161. return (unsigned long)delta64;
  162. err:
  163. local_irq_restore(flags);
  164. return 0;
  165. }
  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. #ifdef CONFIG_CPU_FREQ
  185. /*
  186. * if the CPU frequency is scaled, TSC-based delays will need a different
  187. * loops_per_jiffy value to function properly.
  188. */
  189. static unsigned int ref_freq;
  190. static unsigned long loops_per_jiffy_ref;
  191. static unsigned long cpu_khz_ref;
  192. static int
  193. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  194. {
  195. struct cpufreq_freqs *freq = data;
  196. if (!ref_freq) {
  197. if (!freq->old){
  198. ref_freq = freq->new;
  199. return 0;
  200. }
  201. ref_freq = freq->old;
  202. loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
  203. cpu_khz_ref = cpu_khz;
  204. }
  205. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  206. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  207. (val == CPUFREQ_RESUMECHANGE)) {
  208. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  209. cpu_data(freq->cpu).loops_per_jiffy =
  210. cpufreq_scale(loops_per_jiffy_ref,
  211. ref_freq, freq->new);
  212. if (cpu_khz) {
  213. if (num_online_cpus() == 1)
  214. cpu_khz = cpufreq_scale(cpu_khz_ref,
  215. ref_freq, freq->new);
  216. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  217. tsc_khz = cpu_khz;
  218. set_cyc2ns_scale(cpu_khz, freq->cpu);
  219. /*
  220. * TSC based sched_clock turns
  221. * to junk w/ cpufreq
  222. */
  223. mark_tsc_unstable("cpufreq changes");
  224. }
  225. }
  226. }
  227. return 0;
  228. }
  229. static struct notifier_block time_cpufreq_notifier_block = {
  230. .notifier_call = time_cpufreq_notifier
  231. };
  232. static int __init cpufreq_tsc(void)
  233. {
  234. return cpufreq_register_notifier(&time_cpufreq_notifier_block,
  235. CPUFREQ_TRANSITION_NOTIFIER);
  236. }
  237. core_initcall(cpufreq_tsc);
  238. #endif
  239. /* clock source code */
  240. static unsigned long current_tsc_khz;
  241. static struct clocksource clocksource_tsc;
  242. /*
  243. * We compare the TSC to the cycle_last value in the clocksource
  244. * structure to avoid a nasty time-warp issue. This can be observed in
  245. * a very small window right after one CPU updated cycle_last under
  246. * xtime lock and the other CPU reads a TSC value which is smaller
  247. * than the cycle_last reference value due to a TSC which is slighty
  248. * behind. This delta is nowhere else observable, but in that case it
  249. * results in a forward time jump in the range of hours due to the
  250. * unsigned delta calculation of the time keeping core code, which is
  251. * necessary to support wrapping clocksources like pm timer.
  252. */
  253. static cycle_t read_tsc(void)
  254. {
  255. cycle_t ret;
  256. rdtscll(ret);
  257. return ret >= clocksource_tsc.cycle_last ?
  258. ret : clocksource_tsc.cycle_last;
  259. }
  260. static struct clocksource clocksource_tsc = {
  261. .name = "tsc",
  262. .rating = 300,
  263. .read = read_tsc,
  264. .mask = CLOCKSOURCE_MASK(64),
  265. .mult = 0, /* to be set */
  266. .shift = 22,
  267. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  268. CLOCK_SOURCE_MUST_VERIFY,
  269. };
  270. void mark_tsc_unstable(char *reason)
  271. {
  272. if (!tsc_unstable) {
  273. tsc_unstable = 1;
  274. tsc_enabled = 0;
  275. printk("Marking TSC unstable due to: %s.\n", reason);
  276. /* Can be called before registration */
  277. if (clocksource_tsc.mult)
  278. clocksource_change_rating(&clocksource_tsc, 0);
  279. else
  280. clocksource_tsc.rating = 0;
  281. }
  282. }
  283. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  284. static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
  285. {
  286. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  287. d->ident);
  288. tsc_unstable = 1;
  289. return 0;
  290. }
  291. /* List of systems that have known TSC problems */
  292. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  293. {
  294. .callback = dmi_mark_tsc_unstable,
  295. .ident = "IBM Thinkpad 380XD",
  296. .matches = {
  297. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  298. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  299. },
  300. },
  301. {}
  302. };
  303. /*
  304. * Make an educated guess if the TSC is trustworthy and synchronized
  305. * over all CPUs.
  306. */
  307. __cpuinit int unsynchronized_tsc(void)
  308. {
  309. if (!cpu_has_tsc || tsc_unstable)
  310. return 1;
  311. /* Anything with constant TSC should be synchronized */
  312. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  313. return 0;
  314. /*
  315. * Intel systems are normally all synchronized.
  316. * Exceptions must mark TSC as unstable:
  317. */
  318. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  319. /* assume multi socket systems are not synchronized: */
  320. if (num_possible_cpus() > 1)
  321. tsc_unstable = 1;
  322. }
  323. return tsc_unstable;
  324. }
  325. /*
  326. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  327. */
  328. #ifdef CONFIG_MGEODE_LX
  329. /* RTSC counts during suspend */
  330. #define RTSC_SUSP 0x100
  331. static void __init check_geode_tsc_reliable(void)
  332. {
  333. unsigned long res_low, res_high;
  334. rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
  335. if (res_low & RTSC_SUSP)
  336. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  337. }
  338. #else
  339. static inline void check_geode_tsc_reliable(void) { }
  340. #endif
  341. void __init tsc_init(void)
  342. {
  343. int cpu;
  344. if (!cpu_has_tsc)
  345. return;
  346. cpu_khz = calculate_cpu_khz();
  347. tsc_khz = cpu_khz;
  348. if (!cpu_khz) {
  349. mark_tsc_unstable("could not calculate TSC khz");
  350. return;
  351. }
  352. printk("Detected %lu.%03lu MHz processor.\n",
  353. (unsigned long)cpu_khz / 1000,
  354. (unsigned long)cpu_khz % 1000);
  355. /*
  356. * Secondary CPUs do not run through tsc_init(), so set up
  357. * all the scale factors for all CPUs, assuming the same
  358. * speed as the bootup CPU. (cpufreq notifiers will fix this
  359. * up if their speed diverges)
  360. */
  361. for_each_possible_cpu(cpu)
  362. set_cyc2ns_scale(cpu_khz, cpu);
  363. use_tsc_delay();
  364. /* Check and install the TSC clocksource */
  365. dmi_check_system(bad_tsc_dmi_table);
  366. unsynchronized_tsc();
  367. check_geode_tsc_reliable();
  368. current_tsc_khz = tsc_khz;
  369. clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
  370. clocksource_tsc.shift);
  371. /* lower the rating if we already know its unstable: */
  372. if (check_tsc_unstable()) {
  373. clocksource_tsc.rating = 0;
  374. clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
  375. } else
  376. tsc_enabled = 1;
  377. clocksource_register(&clocksource_tsc);
  378. }