tsc_32.c 11 KB

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