tsc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. #include <asm/hypervisor.h>
  18. unsigned int cpu_khz; /* TSC clocks / usec, not used here */
  19. EXPORT_SYMBOL(cpu_khz);
  20. unsigned int tsc_khz;
  21. EXPORT_SYMBOL(tsc_khz);
  22. /*
  23. * TSC can be unstable due to cpufreq or due to unsynced TSCs
  24. */
  25. static int tsc_unstable;
  26. /* native_sched_clock() is called before tsc_init(), so
  27. we must start with the TSC soft disabled to prevent
  28. erroneous rdtsc usage on !cpu_has_tsc processors */
  29. static int tsc_disabled = -1;
  30. /*
  31. * Scheduler clock - returns current time in nanosec units.
  32. */
  33. u64 native_sched_clock(void)
  34. {
  35. u64 this_offset;
  36. /*
  37. * Fall back to jiffies if there's no TSC available:
  38. * ( But note that we still use it if the TSC is marked
  39. * unstable. We do this because unlike Time Of Day,
  40. * the scheduler clock tolerates small errors and it's
  41. * very important for it to be as fast as the platform
  42. * can achive it. )
  43. */
  44. if (unlikely(tsc_disabled)) {
  45. /* No locking but a rare wrong value is not a big deal: */
  46. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  47. }
  48. /* read the Time Stamp Counter: */
  49. rdtscll(this_offset);
  50. /* return the value in ns */
  51. return cycles_2_ns(this_offset);
  52. }
  53. /* We need to define a real function for sched_clock, to override the
  54. weak default version */
  55. #ifdef CONFIG_PARAVIRT
  56. unsigned long long sched_clock(void)
  57. {
  58. return paravirt_sched_clock();
  59. }
  60. #else
  61. unsigned long long
  62. sched_clock(void) __attribute__((alias("native_sched_clock")));
  63. #endif
  64. int check_tsc_unstable(void)
  65. {
  66. return tsc_unstable;
  67. }
  68. EXPORT_SYMBOL_GPL(check_tsc_unstable);
  69. #ifdef CONFIG_X86_TSC
  70. int __init notsc_setup(char *str)
  71. {
  72. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  73. "cannot disable TSC completely.\n");
  74. tsc_disabled = 1;
  75. return 1;
  76. }
  77. #else
  78. /*
  79. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  80. * in cpu/common.c
  81. */
  82. int __init notsc_setup(char *str)
  83. {
  84. setup_clear_cpu_cap(X86_FEATURE_TSC);
  85. return 1;
  86. }
  87. #endif
  88. __setup("notsc", notsc_setup);
  89. #define MAX_RETRIES 5
  90. #define SMI_TRESHOLD 50000
  91. /*
  92. * Read TSC and the reference counters. Take care of SMI disturbance
  93. */
  94. static u64 tsc_read_refs(u64 *p, int hpet)
  95. {
  96. u64 t1, t2;
  97. int i;
  98. for (i = 0; i < MAX_RETRIES; i++) {
  99. t1 = get_cycles();
  100. if (hpet)
  101. *p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  102. else
  103. *p = acpi_pm_read_early();
  104. t2 = get_cycles();
  105. if ((t2 - t1) < SMI_TRESHOLD)
  106. return t2;
  107. }
  108. return ULLONG_MAX;
  109. }
  110. /*
  111. * Calculate the TSC frequency from HPET reference
  112. */
  113. static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2)
  114. {
  115. u64 tmp;
  116. if (hpet2 < hpet1)
  117. hpet2 += 0x100000000ULL;
  118. hpet2 -= hpet1;
  119. tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
  120. do_div(tmp, 1000000);
  121. do_div(deltatsc, tmp);
  122. return (unsigned long) deltatsc;
  123. }
  124. /*
  125. * Calculate the TSC frequency from PMTimer reference
  126. */
  127. static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2)
  128. {
  129. u64 tmp;
  130. if (!pm1 && !pm2)
  131. return ULONG_MAX;
  132. if (pm2 < pm1)
  133. pm2 += (u64)ACPI_PM_OVRRUN;
  134. pm2 -= pm1;
  135. tmp = pm2 * 1000000000LL;
  136. do_div(tmp, PMTMR_TICKS_PER_SEC);
  137. do_div(deltatsc, tmp);
  138. return (unsigned long) deltatsc;
  139. }
  140. #define CAL_MS 10
  141. #define CAL_LATCH (CLOCK_TICK_RATE / (1000 / CAL_MS))
  142. #define CAL_PIT_LOOPS 1000
  143. #define CAL2_MS 50
  144. #define CAL2_LATCH (CLOCK_TICK_RATE / (1000 / CAL2_MS))
  145. #define CAL2_PIT_LOOPS 5000
  146. /*
  147. * Try to calibrate the TSC against the Programmable
  148. * Interrupt Timer and return the frequency of the TSC
  149. * in kHz.
  150. *
  151. * Return ULONG_MAX on failure to calibrate.
  152. */
  153. static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
  154. {
  155. u64 tsc, t1, t2, delta;
  156. unsigned long tscmin, tscmax;
  157. int pitcnt;
  158. /* Set the Gate high, disable speaker */
  159. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  160. /*
  161. * Setup CTC channel 2* for mode 0, (interrupt on terminal
  162. * count mode), binary count. Set the latch register to 50ms
  163. * (LSB then MSB) to begin countdown.
  164. */
  165. outb(0xb0, 0x43);
  166. outb(latch & 0xff, 0x42);
  167. outb(latch >> 8, 0x42);
  168. tsc = t1 = t2 = get_cycles();
  169. pitcnt = 0;
  170. tscmax = 0;
  171. tscmin = ULONG_MAX;
  172. while ((inb(0x61) & 0x20) == 0) {
  173. t2 = get_cycles();
  174. delta = t2 - tsc;
  175. tsc = t2;
  176. if ((unsigned long) delta < tscmin)
  177. tscmin = (unsigned int) delta;
  178. if ((unsigned long) delta > tscmax)
  179. tscmax = (unsigned int) delta;
  180. pitcnt++;
  181. }
  182. /*
  183. * Sanity checks:
  184. *
  185. * If we were not able to read the PIT more than loopmin
  186. * times, then we have been hit by a massive SMI
  187. *
  188. * If the maximum is 10 times larger than the minimum,
  189. * then we got hit by an SMI as well.
  190. */
  191. if (pitcnt < loopmin || tscmax > 10 * tscmin)
  192. return ULONG_MAX;
  193. /* Calculate the PIT value */
  194. delta = t2 - t1;
  195. do_div(delta, ms);
  196. return delta;
  197. }
  198. /*
  199. * This reads the current MSB of the PIT counter, and
  200. * checks if we are running on sufficiently fast and
  201. * non-virtualized hardware.
  202. *
  203. * Our expectations are:
  204. *
  205. * - the PIT is running at roughly 1.19MHz
  206. *
  207. * - each IO is going to take about 1us on real hardware,
  208. * but we allow it to be much faster (by a factor of 10) or
  209. * _slightly_ slower (ie we allow up to a 2us read+counter
  210. * update - anything else implies a unacceptably slow CPU
  211. * or PIT for the fast calibration to work.
  212. *
  213. * - with 256 PIT ticks to read the value, we have 214us to
  214. * see the same MSB (and overhead like doing a single TSC
  215. * read per MSB value etc).
  216. *
  217. * - We're doing 2 reads per loop (LSB, MSB), and we expect
  218. * them each to take about a microsecond on real hardware.
  219. * So we expect a count value of around 100. But we'll be
  220. * generous, and accept anything over 50.
  221. *
  222. * - if the PIT is stuck, and we see *many* more reads, we
  223. * return early (and the next caller of pit_expect_msb()
  224. * then consider it a failure when they don't see the
  225. * next expected value).
  226. *
  227. * These expectations mean that we know that we have seen the
  228. * transition from one expected value to another with a fairly
  229. * high accuracy, and we didn't miss any events. We can thus
  230. * use the TSC value at the transitions to calculate a pretty
  231. * good value for the TSC frequencty.
  232. */
  233. static inline int pit_expect_msb(unsigned char val)
  234. {
  235. int count = 0;
  236. for (count = 0; count < 50000; count++) {
  237. /* Ignore LSB */
  238. inb(0x42);
  239. if (inb(0x42) != val)
  240. break;
  241. }
  242. return count > 50;
  243. }
  244. /*
  245. * How many MSB values do we want to see? We aim for a
  246. * 15ms calibration, which assuming a 2us counter read
  247. * error should give us roughly 150 ppm precision for
  248. * the calibration.
  249. */
  250. #define QUICK_PIT_MS 15
  251. #define QUICK_PIT_ITERATIONS (QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
  252. static unsigned long quick_pit_calibrate(void)
  253. {
  254. /* Set the Gate high, disable speaker */
  255. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  256. /*
  257. * Counter 2, mode 0 (one-shot), binary count
  258. *
  259. * NOTE! Mode 2 decrements by two (and then the
  260. * output is flipped each time, giving the same
  261. * final output frequency as a decrement-by-one),
  262. * so mode 0 is much better when looking at the
  263. * individual counts.
  264. */
  265. outb(0xb0, 0x43);
  266. /* Start at 0xffff */
  267. outb(0xff, 0x42);
  268. outb(0xff, 0x42);
  269. if (pit_expect_msb(0xff)) {
  270. int i;
  271. u64 t1, t2, delta;
  272. unsigned char expect = 0xfe;
  273. t1 = get_cycles();
  274. for (i = 0; i < QUICK_PIT_ITERATIONS; i++, expect--) {
  275. if (!pit_expect_msb(expect))
  276. goto failed;
  277. }
  278. t2 = get_cycles();
  279. /*
  280. * Make sure we can rely on the second TSC timestamp:
  281. */
  282. if (!pit_expect_msb(expect))
  283. goto failed;
  284. /*
  285. * Ok, if we get here, then we've seen the
  286. * MSB of the PIT decrement QUICK_PIT_ITERATIONS
  287. * times, and each MSB had many hits, so we never
  288. * had any sudden jumps.
  289. *
  290. * As a result, we can depend on there not being
  291. * any odd delays anywhere, and the TSC reads are
  292. * reliable.
  293. *
  294. * kHz = ticks / time-in-seconds / 1000;
  295. * kHz = (t2 - t1) / (QPI * 256 / PIT_TICK_RATE) / 1000
  296. * kHz = ((t2 - t1) * PIT_TICK_RATE) / (QPI * 256 * 1000)
  297. */
  298. delta = (t2 - t1)*PIT_TICK_RATE;
  299. do_div(delta, QUICK_PIT_ITERATIONS*256*1000);
  300. printk("Fast TSC calibration using PIT\n");
  301. return delta;
  302. }
  303. failed:
  304. return 0;
  305. }
  306. /**
  307. * native_calibrate_tsc - calibrate the tsc on boot
  308. */
  309. unsigned long native_calibrate_tsc(void)
  310. {
  311. u64 tsc1, tsc2, delta, ref1, ref2;
  312. unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX;
  313. unsigned long flags, latch, ms, fast_calibrate, tsc_khz;
  314. int hpet = is_hpet_enabled(), i, loopmin;
  315. tsc_khz = get_hypervisor_tsc_freq();
  316. if (tsc_khz) {
  317. printk(KERN_INFO "TSC: Frequency read from the hypervisor\n");
  318. return tsc_khz;
  319. }
  320. local_irq_save(flags);
  321. fast_calibrate = quick_pit_calibrate();
  322. local_irq_restore(flags);
  323. if (fast_calibrate)
  324. return fast_calibrate;
  325. /*
  326. * Run 5 calibration loops to get the lowest frequency value
  327. * (the best estimate). We use two different calibration modes
  328. * here:
  329. *
  330. * 1) PIT loop. We set the PIT Channel 2 to oneshot mode and
  331. * load a timeout of 50ms. We read the time right after we
  332. * started the timer and wait until the PIT count down reaches
  333. * zero. In each wait loop iteration we read the TSC and check
  334. * the delta to the previous read. We keep track of the min
  335. * and max values of that delta. The delta is mostly defined
  336. * by the IO time of the PIT access, so we can detect when a
  337. * SMI/SMM disturbance happend between the two reads. If the
  338. * maximum time is significantly larger than the minimum time,
  339. * then we discard the result and have another try.
  340. *
  341. * 2) Reference counter. If available we use the HPET or the
  342. * PMTIMER as a reference to check the sanity of that value.
  343. * We use separate TSC readouts and check inside of the
  344. * reference read for a SMI/SMM disturbance. We dicard
  345. * disturbed values here as well. We do that around the PIT
  346. * calibration delay loop as we have to wait for a certain
  347. * amount of time anyway.
  348. */
  349. /* Preset PIT loop values */
  350. latch = CAL_LATCH;
  351. ms = CAL_MS;
  352. loopmin = CAL_PIT_LOOPS;
  353. for (i = 0; i < 3; i++) {
  354. unsigned long tsc_pit_khz;
  355. /*
  356. * Read the start value and the reference count of
  357. * hpet/pmtimer when available. Then do the PIT
  358. * calibration, which will take at least 50ms, and
  359. * read the end value.
  360. */
  361. local_irq_save(flags);
  362. tsc1 = tsc_read_refs(&ref1, hpet);
  363. tsc_pit_khz = pit_calibrate_tsc(latch, ms, loopmin);
  364. tsc2 = tsc_read_refs(&ref2, hpet);
  365. local_irq_restore(flags);
  366. /* Pick the lowest PIT TSC calibration so far */
  367. tsc_pit_min = min(tsc_pit_min, tsc_pit_khz);
  368. /* hpet or pmtimer available ? */
  369. if (!hpet && !ref1 && !ref2)
  370. continue;
  371. /* Check, whether the sampling was disturbed by an SMI */
  372. if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX)
  373. continue;
  374. tsc2 = (tsc2 - tsc1) * 1000000LL;
  375. if (hpet)
  376. tsc2 = calc_hpet_ref(tsc2, ref1, ref2);
  377. else
  378. tsc2 = calc_pmtimer_ref(tsc2, ref1, ref2);
  379. tsc_ref_min = min(tsc_ref_min, (unsigned long) tsc2);
  380. /* Check the reference deviation */
  381. delta = ((u64) tsc_pit_min) * 100;
  382. do_div(delta, tsc_ref_min);
  383. /*
  384. * If both calibration results are inside a 10% window
  385. * then we can be sure, that the calibration
  386. * succeeded. We break out of the loop right away. We
  387. * use the reference value, as it is more precise.
  388. */
  389. if (delta >= 90 && delta <= 110) {
  390. printk(KERN_INFO
  391. "TSC: PIT calibration matches %s. %d loops\n",
  392. hpet ? "HPET" : "PMTIMER", i + 1);
  393. return tsc_ref_min;
  394. }
  395. /*
  396. * Check whether PIT failed more than once. This
  397. * happens in virtualized environments. We need to
  398. * give the virtual PC a slightly longer timeframe for
  399. * the HPET/PMTIMER to make the result precise.
  400. */
  401. if (i == 1 && tsc_pit_min == ULONG_MAX) {
  402. latch = CAL2_LATCH;
  403. ms = CAL2_MS;
  404. loopmin = CAL2_PIT_LOOPS;
  405. }
  406. }
  407. /*
  408. * Now check the results.
  409. */
  410. if (tsc_pit_min == ULONG_MAX) {
  411. /* PIT gave no useful value */
  412. printk(KERN_WARNING "TSC: Unable to calibrate against PIT\n");
  413. /* We don't have an alternative source, disable TSC */
  414. if (!hpet && !ref1 && !ref2) {
  415. printk("TSC: No reference (HPET/PMTIMER) available\n");
  416. return 0;
  417. }
  418. /* The alternative source failed as well, disable TSC */
  419. if (tsc_ref_min == ULONG_MAX) {
  420. printk(KERN_WARNING "TSC: HPET/PMTIMER calibration "
  421. "failed.\n");
  422. return 0;
  423. }
  424. /* Use the alternative source */
  425. printk(KERN_INFO "TSC: using %s reference calibration\n",
  426. hpet ? "HPET" : "PMTIMER");
  427. return tsc_ref_min;
  428. }
  429. /* We don't have an alternative source, use the PIT calibration value */
  430. if (!hpet && !ref1 && !ref2) {
  431. printk(KERN_INFO "TSC: Using PIT calibration value\n");
  432. return tsc_pit_min;
  433. }
  434. /* The alternative source failed, use the PIT calibration value */
  435. if (tsc_ref_min == ULONG_MAX) {
  436. printk(KERN_WARNING "TSC: HPET/PMTIMER calibration failed. "
  437. "Using PIT calibration\n");
  438. return tsc_pit_min;
  439. }
  440. /*
  441. * The calibration values differ too much. In doubt, we use
  442. * the PIT value as we know that there are PMTIMERs around
  443. * running at double speed. At least we let the user know:
  444. */
  445. printk(KERN_WARNING "TSC: PIT calibration deviates from %s: %lu %lu.\n",
  446. hpet ? "HPET" : "PMTIMER", tsc_pit_min, tsc_ref_min);
  447. printk(KERN_INFO "TSC: Using PIT calibration value\n");
  448. return tsc_pit_min;
  449. }
  450. #ifdef CONFIG_X86_32
  451. /* Only called from the Powernow K7 cpu freq driver */
  452. int recalibrate_cpu_khz(void)
  453. {
  454. #ifndef CONFIG_SMP
  455. unsigned long cpu_khz_old = cpu_khz;
  456. if (cpu_has_tsc) {
  457. tsc_khz = calibrate_tsc();
  458. cpu_khz = tsc_khz;
  459. cpu_data(0).loops_per_jiffy =
  460. cpufreq_scale(cpu_data(0).loops_per_jiffy,
  461. cpu_khz_old, cpu_khz);
  462. return 0;
  463. } else
  464. return -ENODEV;
  465. #else
  466. return -ENODEV;
  467. #endif
  468. }
  469. EXPORT_SYMBOL(recalibrate_cpu_khz);
  470. #endif /* CONFIG_X86_32 */
  471. /* Accelerators for sched_clock()
  472. * convert from cycles(64bits) => nanoseconds (64bits)
  473. * basic equation:
  474. * ns = cycles / (freq / ns_per_sec)
  475. * ns = cycles * (ns_per_sec / freq)
  476. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  477. * ns = cycles * (10^6 / cpu_khz)
  478. *
  479. * Then we use scaling math (suggested by george@mvista.com) to get:
  480. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  481. * ns = cycles * cyc2ns_scale / SC
  482. *
  483. * And since SC is a constant power of two, we can convert the div
  484. * into a shift.
  485. *
  486. * We can use khz divisor instead of mhz to keep a better precision, since
  487. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  488. * (mathieu.desnoyers@polymtl.ca)
  489. *
  490. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  491. */
  492. DEFINE_PER_CPU(unsigned long, cyc2ns);
  493. static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
  494. {
  495. unsigned long long tsc_now, ns_now;
  496. unsigned long flags, *scale;
  497. local_irq_save(flags);
  498. sched_clock_idle_sleep_event();
  499. scale = &per_cpu(cyc2ns, cpu);
  500. rdtscll(tsc_now);
  501. ns_now = __cycles_2_ns(tsc_now);
  502. if (cpu_khz)
  503. *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
  504. sched_clock_idle_wakeup_event(0);
  505. local_irq_restore(flags);
  506. }
  507. #ifdef CONFIG_CPU_FREQ
  508. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  509. * changes.
  510. *
  511. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  512. * not that important because current Opteron setups do not support
  513. * scaling on SMP anyroads.
  514. *
  515. * Should fix up last_tsc too. Currently gettimeofday in the
  516. * first tick after the change will be slightly wrong.
  517. */
  518. static unsigned int ref_freq;
  519. static unsigned long loops_per_jiffy_ref;
  520. static unsigned long tsc_khz_ref;
  521. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  522. void *data)
  523. {
  524. struct cpufreq_freqs *freq = data;
  525. unsigned long *lpj, dummy;
  526. if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
  527. return 0;
  528. lpj = &dummy;
  529. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  530. #ifdef CONFIG_SMP
  531. lpj = &cpu_data(freq->cpu).loops_per_jiffy;
  532. #else
  533. lpj = &boot_cpu_data.loops_per_jiffy;
  534. #endif
  535. if (!ref_freq) {
  536. ref_freq = freq->old;
  537. loops_per_jiffy_ref = *lpj;
  538. tsc_khz_ref = tsc_khz;
  539. }
  540. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  541. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  542. (val == CPUFREQ_RESUMECHANGE)) {
  543. *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  544. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  545. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  546. mark_tsc_unstable("cpufreq changes");
  547. }
  548. set_cyc2ns_scale(tsc_khz, freq->cpu);
  549. return 0;
  550. }
  551. static struct notifier_block time_cpufreq_notifier_block = {
  552. .notifier_call = time_cpufreq_notifier
  553. };
  554. static int __init cpufreq_tsc(void)
  555. {
  556. if (!cpu_has_tsc)
  557. return 0;
  558. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  559. return 0;
  560. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  561. CPUFREQ_TRANSITION_NOTIFIER);
  562. return 0;
  563. }
  564. core_initcall(cpufreq_tsc);
  565. #endif /* CONFIG_CPU_FREQ */
  566. /* clocksource code */
  567. static struct clocksource clocksource_tsc;
  568. /*
  569. * We compare the TSC to the cycle_last value in the clocksource
  570. * structure to avoid a nasty time-warp. This can be observed in a
  571. * very small window right after one CPU updated cycle_last under
  572. * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
  573. * is smaller than the cycle_last reference value due to a TSC which
  574. * is slighty behind. This delta is nowhere else observable, but in
  575. * that case it results in a forward time jump in the range of hours
  576. * due to the unsigned delta calculation of the time keeping core
  577. * code, which is necessary to support wrapping clocksources like pm
  578. * timer.
  579. */
  580. static cycle_t read_tsc(void)
  581. {
  582. cycle_t ret = (cycle_t)get_cycles();
  583. return ret >= clocksource_tsc.cycle_last ?
  584. ret : clocksource_tsc.cycle_last;
  585. }
  586. #ifdef CONFIG_X86_64
  587. static cycle_t __vsyscall_fn vread_tsc(void)
  588. {
  589. cycle_t ret = (cycle_t)vget_cycles();
  590. return ret >= __vsyscall_gtod_data.clock.cycle_last ?
  591. ret : __vsyscall_gtod_data.clock.cycle_last;
  592. }
  593. #endif
  594. static struct clocksource clocksource_tsc = {
  595. .name = "tsc",
  596. .rating = 300,
  597. .read = read_tsc,
  598. .mask = CLOCKSOURCE_MASK(64),
  599. .shift = 22,
  600. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  601. CLOCK_SOURCE_MUST_VERIFY,
  602. #ifdef CONFIG_X86_64
  603. .vread = vread_tsc,
  604. #endif
  605. };
  606. void mark_tsc_unstable(char *reason)
  607. {
  608. if (!tsc_unstable) {
  609. tsc_unstable = 1;
  610. printk("Marking TSC unstable due to %s\n", reason);
  611. /* Change only the rating, when not registered */
  612. if (clocksource_tsc.mult)
  613. clocksource_change_rating(&clocksource_tsc, 0);
  614. else
  615. clocksource_tsc.rating = 0;
  616. }
  617. }
  618. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  619. static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
  620. {
  621. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  622. d->ident);
  623. tsc_unstable = 1;
  624. return 0;
  625. }
  626. /* List of systems that have known TSC problems */
  627. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  628. {
  629. .callback = dmi_mark_tsc_unstable,
  630. .ident = "IBM Thinkpad 380XD",
  631. .matches = {
  632. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  633. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  634. },
  635. },
  636. {}
  637. };
  638. /*
  639. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  640. */
  641. #ifdef CONFIG_MGEODE_LX
  642. /* RTSC counts during suspend */
  643. #define RTSC_SUSP 0x100
  644. static void __init check_geode_tsc_reliable(void)
  645. {
  646. unsigned long res_low, res_high;
  647. rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
  648. if (res_low & RTSC_SUSP)
  649. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  650. }
  651. #else
  652. static inline void check_geode_tsc_reliable(void) { }
  653. #endif
  654. /*
  655. * Make an educated guess if the TSC is trustworthy and synchronized
  656. * over all CPUs.
  657. */
  658. __cpuinit int unsynchronized_tsc(void)
  659. {
  660. if (!cpu_has_tsc || tsc_unstable)
  661. return 1;
  662. #ifdef CONFIG_X86_SMP
  663. if (apic_is_clustered_box())
  664. return 1;
  665. #endif
  666. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  667. return 0;
  668. /*
  669. * Intel systems are normally all synchronized.
  670. * Exceptions must mark TSC as unstable:
  671. */
  672. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  673. /* assume multi socket systems are not synchronized: */
  674. if (num_possible_cpus() > 1)
  675. tsc_unstable = 1;
  676. }
  677. return tsc_unstable;
  678. }
  679. static void __init init_tsc_clocksource(void)
  680. {
  681. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  682. clocksource_tsc.shift);
  683. /* lower the rating if we already know its unstable: */
  684. if (check_tsc_unstable()) {
  685. clocksource_tsc.rating = 0;
  686. clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
  687. }
  688. clocksource_register(&clocksource_tsc);
  689. }
  690. void __init tsc_init(void)
  691. {
  692. u64 lpj;
  693. int cpu;
  694. if (!cpu_has_tsc)
  695. return;
  696. tsc_khz = calibrate_tsc();
  697. cpu_khz = tsc_khz;
  698. if (!tsc_khz) {
  699. mark_tsc_unstable("could not calculate TSC khz");
  700. return;
  701. }
  702. #ifdef CONFIG_X86_64
  703. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  704. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD))
  705. cpu_khz = calibrate_cpu();
  706. #endif
  707. lpj = ((u64)tsc_khz * 1000);
  708. do_div(lpj, HZ);
  709. lpj_fine = lpj;
  710. printk("Detected %lu.%03lu MHz processor.\n",
  711. (unsigned long)cpu_khz / 1000,
  712. (unsigned long)cpu_khz % 1000);
  713. /*
  714. * Secondary CPUs do not run through tsc_init(), so set up
  715. * all the scale factors for all CPUs, assuming the same
  716. * speed as the bootup CPU. (cpufreq notifiers will fix this
  717. * up if their speed diverges)
  718. */
  719. for_each_possible_cpu(cpu)
  720. set_cyc2ns_scale(cpu_khz, cpu);
  721. if (tsc_disabled > 0)
  722. return;
  723. /* now allow native_sched_clock() to use rdtsc */
  724. tsc_disabled = 0;
  725. use_tsc_delay();
  726. /* Check and install the TSC clocksource */
  727. dmi_check_system(bad_tsc_dmi_table);
  728. if (unsynchronized_tsc())
  729. mark_tsc_unstable("TSCs unsynchronized");
  730. check_geode_tsc_reliable();
  731. init_tsc_clocksource();
  732. }