tsc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include <linux/kernel.h>
  2. #include <linux/sched.h>
  3. #include <linux/init.h>
  4. #include <linux/module.h>
  5. #include <linux/timer.h>
  6. #include <linux/acpi_pmtmr.h>
  7. #include <linux/cpufreq.h>
  8. #include <linux/dmi.h>
  9. #include <linux/delay.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/percpu.h>
  12. #include <asm/hpet.h>
  13. #include <asm/timer.h>
  14. #include <asm/vgtod.h>
  15. #include <asm/time.h>
  16. #include <asm/delay.h>
  17. unsigned int cpu_khz; /* TSC clocks / usec, not used here */
  18. EXPORT_SYMBOL(cpu_khz);
  19. unsigned int tsc_khz;
  20. EXPORT_SYMBOL(tsc_khz);
  21. /*
  22. * TSC can be unstable due to cpufreq or due to unsynced TSCs
  23. */
  24. static int tsc_unstable;
  25. /* native_sched_clock() is called before tsc_init(), so
  26. we must start with the TSC soft disabled to prevent
  27. erroneous rdtsc usage on !cpu_has_tsc processors */
  28. static int tsc_disabled = -1;
  29. /*
  30. * Scheduler clock - returns current time in nanosec units.
  31. */
  32. u64 native_sched_clock(void)
  33. {
  34. u64 this_offset;
  35. /*
  36. * Fall back to jiffies if there's no TSC available:
  37. * ( But note that we still use it if the TSC is marked
  38. * unstable. We do this because unlike Time Of Day,
  39. * the scheduler clock tolerates small errors and it's
  40. * very important for it to be as fast as the platform
  41. * can achive it. )
  42. */
  43. if (unlikely(tsc_disabled)) {
  44. /* No locking but a rare wrong value is not a big deal: */
  45. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  46. }
  47. /* read the Time Stamp Counter: */
  48. rdtscll(this_offset);
  49. /* return the value in ns */
  50. return cycles_2_ns(this_offset);
  51. }
  52. /* We need to define a real function for sched_clock, to override the
  53. weak default version */
  54. #ifdef CONFIG_PARAVIRT
  55. unsigned long long sched_clock(void)
  56. {
  57. return paravirt_sched_clock();
  58. }
  59. #else
  60. unsigned long long
  61. sched_clock(void) __attribute__((alias("native_sched_clock")));
  62. #endif
  63. int check_tsc_unstable(void)
  64. {
  65. return tsc_unstable;
  66. }
  67. EXPORT_SYMBOL_GPL(check_tsc_unstable);
  68. #ifdef CONFIG_X86_TSC
  69. int __init notsc_setup(char *str)
  70. {
  71. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  72. "cannot disable TSC completely.\n");
  73. tsc_disabled = 1;
  74. return 1;
  75. }
  76. #else
  77. /*
  78. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  79. * in cpu/common.c
  80. */
  81. int __init notsc_setup(char *str)
  82. {
  83. setup_clear_cpu_cap(X86_FEATURE_TSC);
  84. return 1;
  85. }
  86. #endif
  87. __setup("notsc", notsc_setup);
  88. #define MAX_RETRIES 5
  89. #define SMI_TRESHOLD 50000
  90. /*
  91. * Read TSC and the reference counters. Take care of SMI disturbance
  92. */
  93. static u64 __init tsc_read_refs(u64 *pm, u64 *hpet)
  94. {
  95. u64 t1, t2;
  96. int i;
  97. for (i = 0; i < MAX_RETRIES; i++) {
  98. t1 = get_cycles();
  99. if (hpet)
  100. *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  101. else
  102. *pm = acpi_pm_read_early();
  103. t2 = get_cycles();
  104. if ((t2 - t1) < SMI_TRESHOLD)
  105. return t2;
  106. }
  107. return ULLONG_MAX;
  108. }
  109. /**
  110. * native_calibrate_tsc - calibrate the tsc on boot
  111. */
  112. unsigned long native_calibrate_tsc(void)
  113. {
  114. unsigned long flags;
  115. u64 tsc1, tsc2, tr1, tr2, delta, pm1, pm2, hpet1, hpet2;
  116. int hpet = is_hpet_enabled();
  117. unsigned int tsc_khz_val = 0;
  118. local_irq_save(flags);
  119. tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
  120. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  121. outb(0xb0, 0x43);
  122. outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  123. outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  124. tr1 = get_cycles();
  125. while ((inb(0x61) & 0x20) == 0);
  126. tr2 = get_cycles();
  127. tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
  128. local_irq_restore(flags);
  129. /*
  130. * Preset the result with the raw and inaccurate PIT
  131. * calibration value
  132. */
  133. delta = (tr2 - tr1);
  134. do_div(delta, 50);
  135. tsc_khz_val = delta;
  136. /* hpet or pmtimer available ? */
  137. if (!hpet && !pm1 && !pm2) {
  138. printk(KERN_INFO "TSC calibrated against PIT\n");
  139. goto out;
  140. }
  141. /* Check, whether the sampling was disturbed by an SMI */
  142. if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX) {
  143. printk(KERN_WARNING "TSC calibration disturbed by SMI, "
  144. "using PIT calibration result\n");
  145. goto out;
  146. }
  147. tsc2 = (tsc2 - tsc1) * 1000000LL;
  148. if (hpet) {
  149. printk(KERN_INFO "TSC calibrated against HPET\n");
  150. if (hpet2 < hpet1)
  151. hpet2 += 0x100000000ULL;
  152. hpet2 -= hpet1;
  153. tsc1 = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
  154. do_div(tsc1, 1000000);
  155. } else {
  156. printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
  157. if (pm2 < pm1)
  158. pm2 += (u64)ACPI_PM_OVRRUN;
  159. pm2 -= pm1;
  160. tsc1 = pm2 * 1000000000LL;
  161. do_div(tsc1, PMTMR_TICKS_PER_SEC);
  162. }
  163. do_div(tsc2, tsc1);
  164. tsc_khz_val = tsc2;
  165. out:
  166. return tsc_khz_val;
  167. }
  168. #ifdef CONFIG_X86_32
  169. /* Only called from the Powernow K7 cpu freq driver */
  170. int recalibrate_cpu_khz(void)
  171. {
  172. #ifndef CONFIG_SMP
  173. unsigned long cpu_khz_old = cpu_khz;
  174. if (cpu_has_tsc) {
  175. tsc_khz = calibrate_tsc();
  176. cpu_khz = tsc_khz;
  177. cpu_data(0).loops_per_jiffy =
  178. cpufreq_scale(cpu_data(0).loops_per_jiffy,
  179. cpu_khz_old, cpu_khz);
  180. return 0;
  181. } else
  182. return -ENODEV;
  183. #else
  184. return -ENODEV;
  185. #endif
  186. }
  187. EXPORT_SYMBOL(recalibrate_cpu_khz);
  188. #endif /* CONFIG_X86_32 */
  189. /* Accelerators for sched_clock()
  190. * convert from cycles(64bits) => nanoseconds (64bits)
  191. * basic equation:
  192. * ns = cycles / (freq / ns_per_sec)
  193. * ns = cycles * (ns_per_sec / freq)
  194. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  195. * ns = cycles * (10^6 / cpu_khz)
  196. *
  197. * Then we use scaling math (suggested by george@mvista.com) to get:
  198. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  199. * ns = cycles * cyc2ns_scale / SC
  200. *
  201. * And since SC is a constant power of two, we can convert the div
  202. * into a shift.
  203. *
  204. * We can use khz divisor instead of mhz to keep a better precision, since
  205. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  206. * (mathieu.desnoyers@polymtl.ca)
  207. *
  208. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  209. */
  210. DEFINE_PER_CPU(unsigned long, cyc2ns);
  211. static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
  212. {
  213. unsigned long long tsc_now, ns_now;
  214. unsigned long flags, *scale;
  215. local_irq_save(flags);
  216. sched_clock_idle_sleep_event();
  217. scale = &per_cpu(cyc2ns, cpu);
  218. rdtscll(tsc_now);
  219. ns_now = __cycles_2_ns(tsc_now);
  220. if (cpu_khz)
  221. *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
  222. sched_clock_idle_wakeup_event(0);
  223. local_irq_restore(flags);
  224. }
  225. #ifdef CONFIG_CPU_FREQ
  226. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  227. * changes.
  228. *
  229. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  230. * not that important because current Opteron setups do not support
  231. * scaling on SMP anyroads.
  232. *
  233. * Should fix up last_tsc too. Currently gettimeofday in the
  234. * first tick after the change will be slightly wrong.
  235. */
  236. static unsigned int ref_freq;
  237. static unsigned long loops_per_jiffy_ref;
  238. static unsigned long tsc_khz_ref;
  239. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  240. void *data)
  241. {
  242. struct cpufreq_freqs *freq = data;
  243. unsigned long *lpj, dummy;
  244. if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
  245. return 0;
  246. lpj = &dummy;
  247. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  248. #ifdef CONFIG_SMP
  249. lpj = &cpu_data(freq->cpu).loops_per_jiffy;
  250. #else
  251. lpj = &boot_cpu_data.loops_per_jiffy;
  252. #endif
  253. if (!ref_freq) {
  254. ref_freq = freq->old;
  255. loops_per_jiffy_ref = *lpj;
  256. tsc_khz_ref = tsc_khz;
  257. }
  258. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  259. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  260. (val == CPUFREQ_RESUMECHANGE)) {
  261. *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  262. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  263. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  264. mark_tsc_unstable("cpufreq changes");
  265. }
  266. set_cyc2ns_scale(tsc_khz_ref, freq->cpu);
  267. return 0;
  268. }
  269. static struct notifier_block time_cpufreq_notifier_block = {
  270. .notifier_call = time_cpufreq_notifier
  271. };
  272. static int __init cpufreq_tsc(void)
  273. {
  274. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  275. CPUFREQ_TRANSITION_NOTIFIER);
  276. return 0;
  277. }
  278. core_initcall(cpufreq_tsc);
  279. #endif /* CONFIG_CPU_FREQ */
  280. /* clocksource code */
  281. static struct clocksource clocksource_tsc;
  282. /*
  283. * We compare the TSC to the cycle_last value in the clocksource
  284. * structure to avoid a nasty time-warp. This can be observed in a
  285. * very small window right after one CPU updated cycle_last under
  286. * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
  287. * is smaller than the cycle_last reference value due to a TSC which
  288. * is slighty behind. This delta is nowhere else observable, but in
  289. * that case it results in a forward time jump in the range of hours
  290. * due to the unsigned delta calculation of the time keeping core
  291. * code, which is necessary to support wrapping clocksources like pm
  292. * timer.
  293. */
  294. static cycle_t read_tsc(void)
  295. {
  296. cycle_t ret = (cycle_t)get_cycles();
  297. return ret >= clocksource_tsc.cycle_last ?
  298. ret : clocksource_tsc.cycle_last;
  299. }
  300. static cycle_t __vsyscall_fn vread_tsc(void)
  301. {
  302. cycle_t ret = (cycle_t)vget_cycles();
  303. return ret >= __vsyscall_gtod_data.clock.cycle_last ?
  304. ret : __vsyscall_gtod_data.clock.cycle_last;
  305. }
  306. static struct clocksource clocksource_tsc = {
  307. .name = "tsc",
  308. .rating = 300,
  309. .read = read_tsc,
  310. .mask = CLOCKSOURCE_MASK(64),
  311. .shift = 22,
  312. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  313. CLOCK_SOURCE_MUST_VERIFY,
  314. #ifdef CONFIG_X86_64
  315. .vread = vread_tsc,
  316. #endif
  317. };
  318. void mark_tsc_unstable(char *reason)
  319. {
  320. if (!tsc_unstable) {
  321. tsc_unstable = 1;
  322. printk("Marking TSC unstable due to %s\n", reason);
  323. /* Change only the rating, when not registered */
  324. if (clocksource_tsc.mult)
  325. clocksource_change_rating(&clocksource_tsc, 0);
  326. else
  327. clocksource_tsc.rating = 0;
  328. }
  329. }
  330. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  331. static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
  332. {
  333. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  334. d->ident);
  335. tsc_unstable = 1;
  336. return 0;
  337. }
  338. /* List of systems that have known TSC problems */
  339. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  340. {
  341. .callback = dmi_mark_tsc_unstable,
  342. .ident = "IBM Thinkpad 380XD",
  343. .matches = {
  344. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  345. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  346. },
  347. },
  348. {}
  349. };
  350. /*
  351. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  352. */
  353. #ifdef CONFIG_MGEODE_LX
  354. /* RTSC counts during suspend */
  355. #define RTSC_SUSP 0x100
  356. static void __init check_geode_tsc_reliable(void)
  357. {
  358. unsigned long res_low, res_high;
  359. rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
  360. if (res_low & RTSC_SUSP)
  361. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  362. }
  363. #else
  364. static inline void check_geode_tsc_reliable(void) { }
  365. #endif
  366. /*
  367. * Make an educated guess if the TSC is trustworthy and synchronized
  368. * over all CPUs.
  369. */
  370. __cpuinit int unsynchronized_tsc(void)
  371. {
  372. if (!cpu_has_tsc || tsc_unstable)
  373. return 1;
  374. #ifdef CONFIG_SMP
  375. if (apic_is_clustered_box())
  376. return 1;
  377. #endif
  378. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  379. return 0;
  380. /*
  381. * Intel systems are normally all synchronized.
  382. * Exceptions must mark TSC as unstable:
  383. */
  384. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  385. /* assume multi socket systems are not synchronized: */
  386. if (num_possible_cpus() > 1)
  387. tsc_unstable = 1;
  388. }
  389. return tsc_unstable;
  390. }
  391. static void __init init_tsc_clocksource(void)
  392. {
  393. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  394. clocksource_tsc.shift);
  395. /* lower the rating if we already know its unstable: */
  396. if (check_tsc_unstable()) {
  397. clocksource_tsc.rating = 0;
  398. clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
  399. }
  400. clocksource_register(&clocksource_tsc);
  401. }
  402. void __init tsc_init(void)
  403. {
  404. u64 lpj;
  405. int cpu;
  406. if (!cpu_has_tsc)
  407. return;
  408. tsc_khz = calibrate_tsc();
  409. cpu_khz = tsc_khz;
  410. if (!tsc_khz) {
  411. mark_tsc_unstable("could not calculate TSC khz");
  412. return;
  413. }
  414. #ifdef CONFIG_X86_64
  415. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  416. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD))
  417. cpu_khz = calibrate_cpu();
  418. #endif
  419. lpj = ((u64)tsc_khz * 1000);
  420. do_div(lpj, HZ);
  421. lpj_fine = lpj;
  422. printk("Detected %lu.%03lu MHz processor.\n",
  423. (unsigned long)cpu_khz / 1000,
  424. (unsigned long)cpu_khz % 1000);
  425. /*
  426. * Secondary CPUs do not run through tsc_init(), so set up
  427. * all the scale factors for all CPUs, assuming the same
  428. * speed as the bootup CPU. (cpufreq notifiers will fix this
  429. * up if their speed diverges)
  430. */
  431. for_each_possible_cpu(cpu)
  432. set_cyc2ns_scale(cpu_khz, cpu);
  433. if (tsc_disabled > 0)
  434. return;
  435. /* now allow native_sched_clock() to use rdtsc */
  436. tsc_disabled = 0;
  437. use_tsc_delay();
  438. /* Check and install the TSC clocksource */
  439. dmi_check_system(bad_tsc_dmi_table);
  440. if (unsynchronized_tsc())
  441. mark_tsc_unstable("TSCs unsynchronized");
  442. check_geode_tsc_reliable();
  443. init_tsc_clocksource();
  444. }