tsc.c 9.0 KB

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