tsc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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/clocksource.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/cpufreq.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/init.h>
  11. #include <linux/dmi.h>
  12. #include <asm/delay.h>
  13. #include <asm/tsc.h>
  14. #include <asm/delay.h>
  15. #include <asm/io.h>
  16. #include "mach_timer.h"
  17. /*
  18. * On some systems the TSC frequency does not
  19. * change with the cpu frequency. So we need
  20. * an extra value to store the TSC freq
  21. */
  22. unsigned int tsc_khz;
  23. int tsc_disable __cpuinitdata = 0;
  24. #ifdef CONFIG_X86_TSC
  25. static int __init tsc_setup(char *str)
  26. {
  27. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  28. "cannot disable TSC.\n");
  29. return 1;
  30. }
  31. #else
  32. /*
  33. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  34. * in cpu/common.c
  35. */
  36. static int __init tsc_setup(char *str)
  37. {
  38. tsc_disable = 1;
  39. return 1;
  40. }
  41. #endif
  42. __setup("notsc", tsc_setup);
  43. /*
  44. * code to mark and check if the TSC is unstable
  45. * due to cpufreq or due to unsynced TSCs
  46. */
  47. static int tsc_unstable;
  48. static inline int check_tsc_unstable(void)
  49. {
  50. return tsc_unstable;
  51. }
  52. void mark_tsc_unstable(void)
  53. {
  54. tsc_unstable = 1;
  55. }
  56. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  57. /* Accellerators for sched_clock()
  58. * convert from cycles(64bits) => nanoseconds (64bits)
  59. * basic equation:
  60. * ns = cycles / (freq / ns_per_sec)
  61. * ns = cycles * (ns_per_sec / freq)
  62. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  63. * ns = cycles * (10^6 / cpu_khz)
  64. *
  65. * Then we use scaling math (suggested by george@mvista.com) to get:
  66. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  67. * ns = cycles * cyc2ns_scale / SC
  68. *
  69. * And since SC is a constant power of two, we can convert the div
  70. * into a shift.
  71. *
  72. * We can use khz divisor instead of mhz to keep a better percision, since
  73. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  74. * (mathieu.desnoyers@polymtl.ca)
  75. *
  76. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  77. */
  78. static unsigned long cyc2ns_scale __read_mostly;
  79. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  80. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  81. {
  82. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  83. }
  84. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  85. {
  86. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  87. }
  88. /*
  89. * Scheduler clock - returns current time in nanosec units.
  90. */
  91. unsigned long long sched_clock(void)
  92. {
  93. unsigned long long this_offset;
  94. /*
  95. * in the NUMA case we dont use the TSC as they are not
  96. * synchronized across all CPUs.
  97. */
  98. #ifndef CONFIG_NUMA
  99. if (!cpu_khz || check_tsc_unstable())
  100. #endif
  101. /* no locking but a rare wrong value is not a big deal */
  102. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  103. /* read the Time Stamp Counter: */
  104. rdtscll(this_offset);
  105. /* return the value in ns */
  106. return cycles_2_ns(this_offset);
  107. }
  108. static unsigned long calculate_cpu_khz(void)
  109. {
  110. unsigned long long start, end;
  111. unsigned long count;
  112. u64 delta64;
  113. int i;
  114. unsigned long flags;
  115. local_irq_save(flags);
  116. /* run 3 times to ensure the cache is warm */
  117. for (i = 0; i < 3; i++) {
  118. mach_prepare_counter();
  119. rdtscll(start);
  120. mach_countup(&count);
  121. rdtscll(end);
  122. }
  123. /*
  124. * Error: ECTCNEVERSET
  125. * The CTC wasn't reliable: we got a hit on the very first read,
  126. * or the CPU was so fast/slow that the quotient wouldn't fit in
  127. * 32 bits..
  128. */
  129. if (count <= 1)
  130. goto err;
  131. delta64 = end - start;
  132. /* cpu freq too fast: */
  133. if (delta64 > (1ULL<<32))
  134. goto err;
  135. /* cpu freq too slow: */
  136. if (delta64 <= CALIBRATE_TIME_MSEC)
  137. goto err;
  138. delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
  139. do_div(delta64,CALIBRATE_TIME_MSEC);
  140. local_irq_restore(flags);
  141. return (unsigned long)delta64;
  142. err:
  143. local_irq_restore(flags);
  144. return 0;
  145. }
  146. int recalibrate_cpu_khz(void)
  147. {
  148. #ifndef CONFIG_SMP
  149. unsigned long cpu_khz_old = cpu_khz;
  150. if (cpu_has_tsc) {
  151. cpu_khz = calculate_cpu_khz();
  152. tsc_khz = cpu_khz;
  153. cpu_data[0].loops_per_jiffy =
  154. cpufreq_scale(cpu_data[0].loops_per_jiffy,
  155. cpu_khz_old, cpu_khz);
  156. return 0;
  157. } else
  158. return -ENODEV;
  159. #else
  160. return -ENODEV;
  161. #endif
  162. }
  163. EXPORT_SYMBOL(recalibrate_cpu_khz);
  164. void __init tsc_init(void)
  165. {
  166. if (!cpu_has_tsc || tsc_disable)
  167. return;
  168. cpu_khz = calculate_cpu_khz();
  169. tsc_khz = cpu_khz;
  170. if (!cpu_khz)
  171. return;
  172. printk("Detected %lu.%03lu MHz processor.\n",
  173. (unsigned long)cpu_khz / 1000,
  174. (unsigned long)cpu_khz % 1000);
  175. set_cyc2ns_scale(cpu_khz);
  176. use_tsc_delay();
  177. }
  178. #ifdef CONFIG_CPU_FREQ
  179. static unsigned int cpufreq_delayed_issched = 0;
  180. static unsigned int cpufreq_init = 0;
  181. static struct work_struct cpufreq_delayed_get_work;
  182. static void handle_cpufreq_delayed_get(void *v)
  183. {
  184. unsigned int cpu;
  185. for_each_online_cpu(cpu)
  186. cpufreq_get(cpu);
  187. cpufreq_delayed_issched = 0;
  188. }
  189. /*
  190. * if we notice cpufreq oddness, schedule a call to cpufreq_get() as it tries
  191. * to verify the CPU frequency the timing core thinks the CPU is running
  192. * at is still correct.
  193. */
  194. static inline void cpufreq_delayed_get(void)
  195. {
  196. if (cpufreq_init && !cpufreq_delayed_issched) {
  197. cpufreq_delayed_issched = 1;
  198. printk(KERN_DEBUG "Checking if CPU frequency changed.\n");
  199. schedule_work(&cpufreq_delayed_get_work);
  200. }
  201. }
  202. /*
  203. * if the CPU frequency is scaled, TSC-based delays will need a different
  204. * loops_per_jiffy value to function properly.
  205. */
  206. static unsigned int ref_freq = 0;
  207. static unsigned long loops_per_jiffy_ref = 0;
  208. static unsigned long cpu_khz_ref = 0;
  209. static int
  210. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  211. {
  212. struct cpufreq_freqs *freq = data;
  213. if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
  214. write_seqlock_irq(&xtime_lock);
  215. if (!ref_freq) {
  216. if (!freq->old){
  217. ref_freq = freq->new;
  218. goto end;
  219. }
  220. ref_freq = freq->old;
  221. loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
  222. cpu_khz_ref = cpu_khz;
  223. }
  224. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  225. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  226. (val == CPUFREQ_RESUMECHANGE)) {
  227. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  228. cpu_data[freq->cpu].loops_per_jiffy =
  229. cpufreq_scale(loops_per_jiffy_ref,
  230. ref_freq, freq->new);
  231. if (cpu_khz) {
  232. if (num_online_cpus() == 1)
  233. cpu_khz = cpufreq_scale(cpu_khz_ref,
  234. ref_freq, freq->new);
  235. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  236. tsc_khz = cpu_khz;
  237. set_cyc2ns_scale(cpu_khz);
  238. /*
  239. * TSC based sched_clock turns
  240. * to junk w/ cpufreq
  241. */
  242. mark_tsc_unstable();
  243. }
  244. }
  245. }
  246. end:
  247. if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
  248. write_sequnlock_irq(&xtime_lock);
  249. return 0;
  250. }
  251. static struct notifier_block time_cpufreq_notifier_block = {
  252. .notifier_call = time_cpufreq_notifier
  253. };
  254. static int __init cpufreq_tsc(void)
  255. {
  256. int ret;
  257. INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);
  258. ret = cpufreq_register_notifier(&time_cpufreq_notifier_block,
  259. CPUFREQ_TRANSITION_NOTIFIER);
  260. if (!ret)
  261. cpufreq_init = 1;
  262. return ret;
  263. }
  264. core_initcall(cpufreq_tsc);
  265. #endif
  266. /* clock source code */
  267. static unsigned long current_tsc_khz = 0;
  268. static int tsc_update_callback(void);
  269. static cycle_t read_tsc(void)
  270. {
  271. cycle_t ret;
  272. rdtscll(ret);
  273. return ret;
  274. }
  275. static struct clocksource clocksource_tsc = {
  276. .name = "tsc",
  277. .rating = 300,
  278. .read = read_tsc,
  279. .mask = CLOCKSOURCE_MASK(64),
  280. .mult = 0, /* to be set */
  281. .shift = 22,
  282. .update_callback = tsc_update_callback,
  283. .is_continuous = 1,
  284. };
  285. static int tsc_update_callback(void)
  286. {
  287. int change = 0;
  288. /* check to see if we should switch to the safe clocksource: */
  289. if (clocksource_tsc.rating != 0 && check_tsc_unstable()) {
  290. clocksource_tsc.rating = 0;
  291. clocksource_reselect();
  292. change = 1;
  293. }
  294. /* only update if tsc_khz has changed: */
  295. if (current_tsc_khz != tsc_khz) {
  296. current_tsc_khz = tsc_khz;
  297. clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
  298. clocksource_tsc.shift);
  299. change = 1;
  300. }
  301. return change;
  302. }
  303. static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
  304. {
  305. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  306. d->ident);
  307. mark_tsc_unstable();
  308. return 0;
  309. }
  310. /* List of systems that have known TSC problems */
  311. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  312. {
  313. .callback = dmi_mark_tsc_unstable,
  314. .ident = "IBM Thinkpad 380XD",
  315. .matches = {
  316. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  317. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  318. },
  319. },
  320. {}
  321. };
  322. #define TSC_FREQ_CHECK_INTERVAL (10*MSEC_PER_SEC) /* 10sec in MS */
  323. static struct timer_list verify_tsc_freq_timer;
  324. /* XXX - Probably should add locking */
  325. static void verify_tsc_freq(unsigned long unused)
  326. {
  327. static u64 last_tsc;
  328. static unsigned long last_jiffies;
  329. u64 now_tsc, interval_tsc;
  330. unsigned long now_jiffies, interval_jiffies;
  331. if (check_tsc_unstable())
  332. return;
  333. rdtscll(now_tsc);
  334. now_jiffies = jiffies;
  335. if (!last_jiffies) {
  336. goto out;
  337. }
  338. interval_jiffies = now_jiffies - last_jiffies;
  339. interval_tsc = now_tsc - last_tsc;
  340. interval_tsc *= HZ;
  341. do_div(interval_tsc, cpu_khz*1000);
  342. if (interval_tsc < (interval_jiffies * 3 / 4)) {
  343. printk("TSC appears to be running slowly. "
  344. "Marking it as unstable\n");
  345. mark_tsc_unstable();
  346. return;
  347. }
  348. out:
  349. last_tsc = now_tsc;
  350. last_jiffies = now_jiffies;
  351. /* set us up to go off on the next interval: */
  352. mod_timer(&verify_tsc_freq_timer,
  353. jiffies + msecs_to_jiffies(TSC_FREQ_CHECK_INTERVAL));
  354. }
  355. /*
  356. * Make an educated guess if the TSC is trustworthy and synchronized
  357. * over all CPUs.
  358. */
  359. static __init int unsynchronized_tsc(void)
  360. {
  361. /*
  362. * Intel systems are normally all synchronized.
  363. * Exceptions must mark TSC as unstable:
  364. */
  365. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
  366. return 0;
  367. /* assume multi socket systems are not synchronized: */
  368. return num_possible_cpus() > 1;
  369. }
  370. static int __init init_tsc_clocksource(void)
  371. {
  372. if (cpu_has_tsc && tsc_khz && !tsc_disable) {
  373. /* check blacklist */
  374. dmi_check_system(bad_tsc_dmi_table);
  375. if (unsynchronized_tsc()) /* mark unstable if unsynced */
  376. mark_tsc_unstable();
  377. current_tsc_khz = tsc_khz;
  378. clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
  379. clocksource_tsc.shift);
  380. /* lower the rating if we already know its unstable: */
  381. if (check_tsc_unstable())
  382. clocksource_tsc.rating = 0;
  383. init_timer(&verify_tsc_freq_timer);
  384. verify_tsc_freq_timer.function = verify_tsc_freq;
  385. verify_tsc_freq_timer.expires =
  386. jiffies + msecs_to_jiffies(TSC_FREQ_CHECK_INTERVAL);
  387. add_timer(&verify_tsc_freq_timer);
  388. return clocksource_register(&clocksource_tsc);
  389. }
  390. return 0;
  391. }
  392. module_init(init_tsc_clocksource);