tsc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. EXPORT_SYMBOL_GPL(tsc_khz);
  26. int tsc_disable;
  27. #ifdef CONFIG_X86_TSC
  28. static int __init tsc_setup(char *str)
  29. {
  30. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  31. "cannot disable TSC.\n");
  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. tsc_disable = 1;
  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. /* Accellerators 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 percision, 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. unsigned long cyc2ns_scale __read_mostly;
  78. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  79. static inline void set_cyc2ns_scale(unsigned long cpu_khz)
  80. {
  81. cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
  82. }
  83. /*
  84. * Scheduler clock - returns current time in nanosec units.
  85. */
  86. unsigned long long native_sched_clock(void)
  87. {
  88. unsigned long long this_offset;
  89. /*
  90. * Fall back to jiffies if there's no TSC available:
  91. * ( But note that we still use it if the TSC is marked
  92. * unstable. We do this because unlike Time Of Day,
  93. * the scheduler clock tolerates small errors and it's
  94. * very important for it to be as fast as the platform
  95. * can achive it. )
  96. */
  97. if (unlikely(!tsc_enabled && !tsc_unstable))
  98. /* No locking but a rare wrong value is not a big deal: */
  99. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  100. /* read the Time Stamp Counter: */
  101. rdtscll(this_offset);
  102. /* return the value in ns */
  103. return cycles_2_ns(this_offset);
  104. }
  105. /* We need to define a real function for sched_clock, to override the
  106. weak default version */
  107. #ifdef CONFIG_PARAVIRT
  108. unsigned long long sched_clock(void)
  109. {
  110. return paravirt_sched_clock();
  111. }
  112. #else
  113. unsigned long long sched_clock(void)
  114. __attribute__((alias("native_sched_clock")));
  115. #endif
  116. unsigned long native_calculate_cpu_khz(void)
  117. {
  118. unsigned long long start, end;
  119. unsigned long count;
  120. u64 delta64;
  121. int i;
  122. unsigned long flags;
  123. local_irq_save(flags);
  124. /* run 3 times to ensure the cache is warm */
  125. for (i = 0; i < 3; i++) {
  126. mach_prepare_counter();
  127. rdtscll(start);
  128. mach_countup(&count);
  129. rdtscll(end);
  130. }
  131. /*
  132. * Error: ECTCNEVERSET
  133. * The CTC wasn't reliable: we got a hit on the very first read,
  134. * or the CPU was so fast/slow that the quotient wouldn't fit in
  135. * 32 bits..
  136. */
  137. if (count <= 1)
  138. goto err;
  139. delta64 = end - start;
  140. /* cpu freq too fast: */
  141. if (delta64 > (1ULL<<32))
  142. goto err;
  143. /* cpu freq too slow: */
  144. if (delta64 <= CALIBRATE_TIME_MSEC)
  145. goto err;
  146. delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
  147. do_div(delta64,CALIBRATE_TIME_MSEC);
  148. local_irq_restore(flags);
  149. return (unsigned long)delta64;
  150. err:
  151. local_irq_restore(flags);
  152. return 0;
  153. }
  154. int recalibrate_cpu_khz(void)
  155. {
  156. #ifndef CONFIG_SMP
  157. unsigned long cpu_khz_old = cpu_khz;
  158. if (cpu_has_tsc) {
  159. cpu_khz = calculate_cpu_khz();
  160. tsc_khz = cpu_khz;
  161. cpu_data[0].loops_per_jiffy =
  162. cpufreq_scale(cpu_data[0].loops_per_jiffy,
  163. cpu_khz_old, cpu_khz);
  164. return 0;
  165. } else
  166. return -ENODEV;
  167. #else
  168. return -ENODEV;
  169. #endif
  170. }
  171. EXPORT_SYMBOL(recalibrate_cpu_khz);
  172. #ifdef CONFIG_CPU_FREQ
  173. /*
  174. * if the CPU frequency is scaled, TSC-based delays will need a different
  175. * loops_per_jiffy value to function properly.
  176. */
  177. static unsigned int ref_freq = 0;
  178. static unsigned long loops_per_jiffy_ref = 0;
  179. static unsigned long cpu_khz_ref = 0;
  180. static int
  181. time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
  182. {
  183. struct cpufreq_freqs *freq = data;
  184. if (!ref_freq) {
  185. if (!freq->old){
  186. ref_freq = freq->new;
  187. return 0;
  188. }
  189. ref_freq = freq->old;
  190. loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
  191. cpu_khz_ref = cpu_khz;
  192. }
  193. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  194. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  195. (val == CPUFREQ_RESUMECHANGE)) {
  196. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  197. cpu_data[freq->cpu].loops_per_jiffy =
  198. cpufreq_scale(loops_per_jiffy_ref,
  199. ref_freq, freq->new);
  200. if (cpu_khz) {
  201. if (num_online_cpus() == 1)
  202. cpu_khz = cpufreq_scale(cpu_khz_ref,
  203. ref_freq, freq->new);
  204. if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
  205. tsc_khz = cpu_khz;
  206. set_cyc2ns_scale(cpu_khz);
  207. /*
  208. * TSC based sched_clock turns
  209. * to junk w/ cpufreq
  210. */
  211. mark_tsc_unstable("cpufreq changes");
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217. static struct notifier_block time_cpufreq_notifier_block = {
  218. .notifier_call = time_cpufreq_notifier
  219. };
  220. static int __init cpufreq_tsc(void)
  221. {
  222. return cpufreq_register_notifier(&time_cpufreq_notifier_block,
  223. CPUFREQ_TRANSITION_NOTIFIER);
  224. }
  225. core_initcall(cpufreq_tsc);
  226. #endif
  227. /* clock source code */
  228. static unsigned long current_tsc_khz = 0;
  229. static cycle_t read_tsc(void)
  230. {
  231. cycle_t ret;
  232. rdtscll(ret);
  233. return ret;
  234. }
  235. static struct clocksource clocksource_tsc = {
  236. .name = "tsc",
  237. .rating = 300,
  238. .read = read_tsc,
  239. .mask = CLOCKSOURCE_MASK(64),
  240. .mult = 0, /* to be set */
  241. .shift = 22,
  242. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  243. CLOCK_SOURCE_MUST_VERIFY,
  244. };
  245. void mark_tsc_unstable(char *reason)
  246. {
  247. if (!tsc_unstable) {
  248. tsc_unstable = 1;
  249. tsc_enabled = 0;
  250. printk("Marking TSC unstable due to: %s.\n", reason);
  251. /* Can be called before registration */
  252. if (clocksource_tsc.mult)
  253. clocksource_change_rating(&clocksource_tsc, 0);
  254. else
  255. clocksource_tsc.rating = 0;
  256. }
  257. }
  258. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  259. static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
  260. {
  261. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  262. d->ident);
  263. tsc_unstable = 1;
  264. return 0;
  265. }
  266. /* List of systems that have known TSC problems */
  267. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  268. {
  269. .callback = dmi_mark_tsc_unstable,
  270. .ident = "IBM Thinkpad 380XD",
  271. .matches = {
  272. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  273. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  274. },
  275. },
  276. {}
  277. };
  278. /*
  279. * Make an educated guess if the TSC is trustworthy and synchronized
  280. * over all CPUs.
  281. */
  282. __cpuinit int unsynchronized_tsc(void)
  283. {
  284. if (!cpu_has_tsc || tsc_unstable)
  285. return 1;
  286. /*
  287. * Intel systems are normally all synchronized.
  288. * Exceptions must mark TSC as unstable:
  289. */
  290. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  291. /* assume multi socket systems are not synchronized: */
  292. if (num_possible_cpus() > 1)
  293. tsc_unstable = 1;
  294. }
  295. return tsc_unstable;
  296. }
  297. /*
  298. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  299. */
  300. #ifdef CONFIG_MGEODE_LX
  301. /* RTSC counts during suspend */
  302. #define RTSC_SUSP 0x100
  303. static void __init check_geode_tsc_reliable(void)
  304. {
  305. unsigned long val;
  306. rdmsrl(MSR_GEODE_BUSCONT_CONF0, val);
  307. if ((val & RTSC_SUSP))
  308. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  309. }
  310. #else
  311. static inline void check_geode_tsc_reliable(void) { }
  312. #endif
  313. void __init tsc_init(void)
  314. {
  315. if (!cpu_has_tsc || tsc_disable)
  316. goto out_no_tsc;
  317. cpu_khz = calculate_cpu_khz();
  318. tsc_khz = cpu_khz;
  319. if (!cpu_khz)
  320. goto out_no_tsc;
  321. printk("Detected %lu.%03lu MHz processor.\n",
  322. (unsigned long)cpu_khz / 1000,
  323. (unsigned long)cpu_khz % 1000);
  324. set_cyc2ns_scale(cpu_khz);
  325. use_tsc_delay();
  326. /* Check and install the TSC clocksource */
  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. } else
  338. tsc_enabled = 1;
  339. clocksource_register(&clocksource_tsc);
  340. return;
  341. out_no_tsc:
  342. /*
  343. * Set the tsc_disable flag if there's no TSC support, this
  344. * makes it a fast flag for the kernel to see whether it
  345. * should be using the TSC.
  346. */
  347. tsc_disable = 1;
  348. }