tsc_32.c 9.0 KB

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