tsc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/io.h>
  15. #include <asm/timer.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. unsigned long long (*custom_sched_clock)(void);
  24. int tsc_disable;
  25. #ifdef CONFIG_X86_TSC
  26. static int __init tsc_setup(char *str)
  27. {
  28. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  29. "cannot disable TSC.\n");
  30. return 1;
  31. }
  32. #else
  33. /*
  34. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  35. * in cpu/common.c
  36. */
  37. static int __init tsc_setup(char *str)
  38. {
  39. tsc_disable = 1;
  40. return 1;
  41. }
  42. #endif
  43. __setup("notsc", tsc_setup);
  44. /*
  45. * code to mark and check if the TSC is unstable
  46. * due to cpufreq or due to unsynced TSCs
  47. */
  48. static int tsc_unstable;
  49. static inline int check_tsc_unstable(void)
  50. {
  51. return tsc_unstable;
  52. }
  53. /* Accellerators for sched_clock()
  54. * convert from cycles(64bits) => nanoseconds (64bits)
  55. * basic equation:
  56. * ns = cycles / (freq / ns_per_sec)
  57. * ns = cycles * (ns_per_sec / freq)
  58. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  59. * ns = cycles * (10^6 / cpu_khz)
  60. *
  61. * Then we use scaling math (suggested by george@mvista.com) to get:
  62. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  63. * ns = cycles * cyc2ns_scale / SC
  64. *
  65. * And since SC is a constant power of two, we can convert the div
  66. * into a shift.
  67. *
  68. * We can use khz divisor instead of mhz to keep a better percision, since
  69. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  70. * (mathieu.desnoyers@polymtl.ca)
  71. *
  72. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  73. */
  74. static unsigned long cyc2ns_scale __read_mostly;
  75. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  76. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  77. {
  78. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  79. }
  80. static inline unsigned long long cycles_2_ns(unsigned long long cyc)
  81. {
  82. return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
  83. }
  84. /*
  85. * Scheduler clock - returns current time in nanosec units.
  86. */
  87. unsigned long long sched_clock(void)
  88. {
  89. unsigned long long this_offset;
  90. /*
  91. * Fall back to jiffies if there's no TSC available:
  92. */
  93. if (unlikely(tsc_disable))
  94. /* No locking but a rare wrong value is not a big deal: */
  95. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  96. /* read the Time Stamp Counter: */
  97. get_scheduled_cycles(this_offset);
  98. /* return the value in ns */
  99. return cycles_2_ns(this_offset);
  100. }
  101. unsigned long native_calculate_cpu_khz(void)
  102. {
  103. unsigned long long start, end;
  104. unsigned long count;
  105. u64 delta64;
  106. int i;
  107. unsigned long flags;
  108. local_irq_save(flags);
  109. /* run 3 times to ensure the cache is warm */
  110. for (i = 0; i < 3; i++) {
  111. mach_prepare_counter();
  112. rdtscll(start);
  113. mach_countup(&count);
  114. rdtscll(end);
  115. }
  116. /*
  117. * Error: ECTCNEVERSET
  118. * The CTC wasn't reliable: we got a hit on the very first read,
  119. * or the CPU was so fast/slow that the quotient wouldn't fit in
  120. * 32 bits..
  121. */
  122. if (count <= 1)
  123. goto err;
  124. delta64 = end - start;
  125. /* cpu freq too fast: */
  126. if (delta64 > (1ULL<<32))
  127. goto err;
  128. /* cpu freq too slow: */
  129. if (delta64 <= CALIBRATE_TIME_MSEC)
  130. goto err;
  131. delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
  132. do_div(delta64,CALIBRATE_TIME_MSEC);
  133. local_irq_restore(flags);
  134. return (unsigned long)delta64;
  135. err:
  136. local_irq_restore(flags);
  137. return 0;
  138. }
  139. int recalibrate_cpu_khz(void)
  140. {
  141. #ifndef CONFIG_SMP
  142. unsigned long cpu_khz_old = cpu_khz;
  143. if (cpu_has_tsc) {
  144. cpu_khz = calculate_cpu_khz();
  145. tsc_khz = cpu_khz;
  146. cpu_data[0].loops_per_jiffy =
  147. cpufreq_scale(cpu_data[0].loops_per_jiffy,
  148. cpu_khz_old, cpu_khz);
  149. return 0;
  150. } else
  151. return -ENODEV;
  152. #else
  153. return -ENODEV;
  154. #endif
  155. }
  156. EXPORT_SYMBOL(recalibrate_cpu_khz);
  157. void __init tsc_init(void)
  158. {
  159. if (!cpu_has_tsc || tsc_disable)
  160. goto out_no_tsc;
  161. cpu_khz = calculate_cpu_khz();
  162. tsc_khz = cpu_khz;
  163. if (!cpu_khz)
  164. goto out_no_tsc;
  165. printk("Detected %lu.%03lu MHz processor.\n",
  166. (unsigned long)cpu_khz / 1000,
  167. (unsigned long)cpu_khz % 1000);
  168. set_cyc2ns_scale(cpu_khz);
  169. use_tsc_delay();
  170. return;
  171. out_no_tsc:
  172. /*
  173. * Set the tsc_disable flag if there's no TSC support, this
  174. * makes it a fast flag for the kernel to see whether it
  175. * should be using the TSC.
  176. */
  177. tsc_disable = 1;
  178. }
  179. #ifdef CONFIG_CPU_FREQ
  180. /*
  181. * if the CPU frequency is scaled, TSC-based delays will need a different
  182. * loops_per_jiffy value to function properly.
  183. */
  184. static unsigned int ref_freq = 0;
  185. static unsigned long loops_per_jiffy_ref = 0;
  186. static unsigned long cpu_khz_ref = 0;
  187. static int
  188. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  189. {
  190. struct cpufreq_freqs *freq = data;
  191. if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
  192. write_seqlock_irq(&xtime_lock);
  193. if (!ref_freq) {
  194. if (!freq->old){
  195. ref_freq = freq->new;
  196. goto end;
  197. }
  198. ref_freq = freq->old;
  199. loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
  200. cpu_khz_ref = cpu_khz;
  201. }
  202. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  203. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  204. (val == CPUFREQ_RESUMECHANGE)) {
  205. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  206. cpu_data[freq->cpu].loops_per_jiffy =
  207. cpufreq_scale(loops_per_jiffy_ref,
  208. ref_freq, freq->new);
  209. if (cpu_khz) {
  210. if (num_online_cpus() == 1)
  211. cpu_khz = cpufreq_scale(cpu_khz_ref,
  212. ref_freq, freq->new);
  213. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  214. tsc_khz = cpu_khz;
  215. set_cyc2ns_scale(cpu_khz);
  216. /*
  217. * TSC based sched_clock turns
  218. * to junk w/ cpufreq
  219. */
  220. mark_tsc_unstable();
  221. }
  222. }
  223. }
  224. end:
  225. if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
  226. write_sequnlock_irq(&xtime_lock);
  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 = 0;
  241. static cycle_t read_tsc(void)
  242. {
  243. cycle_t ret;
  244. rdtscll(ret);
  245. return ret;
  246. }
  247. static struct clocksource clocksource_tsc = {
  248. .name = "tsc",
  249. .rating = 300,
  250. .read = read_tsc,
  251. .mask = CLOCKSOURCE_MASK(64),
  252. .mult = 0, /* to be set */
  253. .shift = 22,
  254. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  255. CLOCK_SOURCE_MUST_VERIFY,
  256. };
  257. void mark_tsc_unstable(void)
  258. {
  259. if (!tsc_unstable) {
  260. tsc_unstable = 1;
  261. /* Can be called before registration */
  262. if (clocksource_tsc.mult)
  263. clocksource_change_rating(&clocksource_tsc, 0);
  264. else
  265. clocksource_tsc.rating = 0;
  266. }
  267. }
  268. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  269. static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
  270. {
  271. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  272. d->ident);
  273. tsc_unstable = 1;
  274. return 0;
  275. }
  276. /* List of systems that have known TSC problems */
  277. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  278. {
  279. .callback = dmi_mark_tsc_unstable,
  280. .ident = "IBM Thinkpad 380XD",
  281. .matches = {
  282. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  283. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  284. },
  285. },
  286. {}
  287. };
  288. /*
  289. * Make an educated guess if the TSC is trustworthy and synchronized
  290. * over all CPUs.
  291. */
  292. __cpuinit int unsynchronized_tsc(void)
  293. {
  294. if (!cpu_has_tsc || tsc_unstable)
  295. return 1;
  296. /*
  297. * Intel systems are normally all synchronized.
  298. * Exceptions must mark TSC as unstable:
  299. */
  300. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  301. /* assume multi socket systems are not synchronized: */
  302. if (num_possible_cpus() > 1)
  303. tsc_unstable = 1;
  304. }
  305. return tsc_unstable;
  306. }
  307. /*
  308. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  309. */
  310. #ifdef CONFIG_MGEODE_LX
  311. /* RTSC counts during suspend */
  312. #define RTSC_SUSP 0x100
  313. static void __init check_geode_tsc_reliable(void)
  314. {
  315. unsigned long val;
  316. rdmsrl(MSR_GEODE_BUSCONT_CONF0, val);
  317. if ((val & RTSC_SUSP))
  318. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  319. }
  320. #else
  321. static inline void check_geode_tsc_reliable(void) { }
  322. #endif
  323. static int __init init_tsc_clocksource(void)
  324. {
  325. if (cpu_has_tsc && tsc_khz && !tsc_disable) {
  326. /* check blacklist */
  327. dmi_check_system(bad_tsc_dmi_table);
  328. unsynchronized_tsc();
  329. check_geode_tsc_reliable();
  330. current_tsc_khz = tsc_khz;
  331. clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
  332. clocksource_tsc.shift);
  333. /* lower the rating if we already know its unstable: */
  334. if (check_tsc_unstable()) {
  335. clocksource_tsc.rating = 0;
  336. clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
  337. }
  338. return clocksource_register(&clocksource_tsc);
  339. }
  340. return 0;
  341. }
  342. module_init(init_tsc_clocksource);