tsc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 tsc_read_refs(u64 *p, int 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. *p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  101. else
  102. *p = 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. * Calculate the TSC frequency from HPET reference
  111. */
  112. static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2)
  113. {
  114. u64 tmp;
  115. if (hpet2 < hpet1)
  116. hpet2 += 0x100000000ULL;
  117. hpet2 -= hpet1;
  118. tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
  119. do_div(tmp, 1000000);
  120. do_div(deltatsc, tmp);
  121. return (unsigned long) deltatsc;
  122. }
  123. /*
  124. * Calculate the TSC frequency from PMTimer reference
  125. */
  126. static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2)
  127. {
  128. u64 tmp;
  129. if (!pm1 && !pm2)
  130. return ULONG_MAX;
  131. if (pm2 < pm1)
  132. pm2 += (u64)ACPI_PM_OVRRUN;
  133. pm2 -= pm1;
  134. tmp = pm2 * 1000000000LL;
  135. do_div(tmp, PMTMR_TICKS_PER_SEC);
  136. do_div(deltatsc, tmp);
  137. return (unsigned long) deltatsc;
  138. }
  139. #define CAL_MS 50
  140. #define CAL_LATCH (CLOCK_TICK_RATE / (1000 / CAL_MS))
  141. #define CAL_PIT_LOOPS 5000
  142. /*
  143. * Try to calibrate the TSC against the Programmable
  144. * Interrupt Timer and return the frequency of the TSC
  145. * in kHz.
  146. *
  147. * Return ULONG_MAX on failure to calibrate.
  148. */
  149. static unsigned long pit_calibrate_tsc(void)
  150. {
  151. u64 tsc, t1, t2, delta;
  152. unsigned long tscmin, tscmax;
  153. int pitcnt;
  154. /* Set the Gate high, disable speaker */
  155. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  156. /*
  157. * Setup CTC channel 2* for mode 0, (interrupt on terminal
  158. * count mode), binary count. Set the latch register to 50ms
  159. * (LSB then MSB) to begin countdown.
  160. */
  161. outb(0xb0, 0x43);
  162. outb(CAL_LATCH & 0xff, 0x42);
  163. outb(CAL_LATCH >> 8, 0x42);
  164. tsc = t1 = t2 = get_cycles();
  165. pitcnt = 0;
  166. tscmax = 0;
  167. tscmin = ULONG_MAX;
  168. while ((inb(0x61) & 0x20) == 0) {
  169. t2 = get_cycles();
  170. delta = t2 - tsc;
  171. tsc = t2;
  172. if ((unsigned long) delta < tscmin)
  173. tscmin = (unsigned int) delta;
  174. if ((unsigned long) delta > tscmax)
  175. tscmax = (unsigned int) delta;
  176. pitcnt++;
  177. }
  178. /*
  179. * Sanity checks:
  180. *
  181. * If we were not able to read the PIT more than PIT_MIN_LOOPS
  182. * times, then we have been hit by a massive SMI
  183. *
  184. * If the maximum is 10 times larger than the minimum,
  185. * then we got hit by an SMI as well.
  186. */
  187. if (pitcnt < CAL_PIT_LOOPS || tscmax > 10 * tscmin)
  188. return ULONG_MAX;
  189. /* Calculate the PIT value */
  190. delta = t2 - t1;
  191. do_div(delta, CAL_MS);
  192. return delta;
  193. }
  194. /**
  195. * native_calibrate_tsc - calibrate the tsc on boot
  196. */
  197. unsigned long native_calibrate_tsc(void)
  198. {
  199. u64 tsc1, tsc2, delta, ref1, ref2;
  200. unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX;
  201. unsigned long flags;
  202. int hpet = is_hpet_enabled(), i;
  203. /*
  204. * Run 5 calibration loops to get the lowest frequency value
  205. * (the best estimate). We use two different calibration modes
  206. * here:
  207. *
  208. * 1) PIT loop. We set the PIT Channel 2 to oneshot mode and
  209. * load a timeout of 50ms. We read the time right after we
  210. * started the timer and wait until the PIT count down reaches
  211. * zero. In each wait loop iteration we read the TSC and check
  212. * the delta to the previous read. We keep track of the min
  213. * and max values of that delta. The delta is mostly defined
  214. * by the IO time of the PIT access, so we can detect when a
  215. * SMI/SMM disturbance happend between the two reads. If the
  216. * maximum time is significantly larger than the minimum time,
  217. * then we discard the result and have another try.
  218. *
  219. * 2) Reference counter. If available we use the HPET or the
  220. * PMTIMER as a reference to check the sanity of that value.
  221. * We use separate TSC readouts and check inside of the
  222. * reference read for a SMI/SMM disturbance. We dicard
  223. * disturbed values here as well. We do that around the PIT
  224. * calibration delay loop as we have to wait for a certain
  225. * amount of time anyway.
  226. */
  227. for (i = 0; i < 5; i++) {
  228. unsigned long tsc_pit_khz;
  229. /*
  230. * Read the start value and the reference count of
  231. * hpet/pmtimer when available. Then do the PIT
  232. * calibration, which will take at least 50ms, and
  233. * read the end value.
  234. */
  235. local_irq_save(flags);
  236. tsc1 = tsc_read_refs(&ref1, hpet);
  237. tsc_pit_khz = pit_calibrate_tsc();
  238. tsc2 = tsc_read_refs(&ref2, hpet);
  239. local_irq_restore(flags);
  240. /* Pick the lowest PIT TSC calibration so far */
  241. tsc_pit_min = min(tsc_pit_min, tsc_pit_khz);
  242. /* hpet or pmtimer available ? */
  243. if (!hpet && !ref1 && !ref2)
  244. continue;
  245. /* Check, whether the sampling was disturbed by an SMI */
  246. if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX)
  247. continue;
  248. tsc2 = (tsc2 - tsc1) * 1000000LL;
  249. if (hpet)
  250. tsc2 = calc_hpet_ref(tsc2, ref1, ref2);
  251. else
  252. tsc2 = calc_pmtimer_ref(tsc2, ref1, ref2);
  253. tsc_ref_min = min(tsc_ref_min, (unsigned long) tsc2);
  254. }
  255. /*
  256. * Now check the results.
  257. */
  258. if (tsc_pit_min == ULONG_MAX) {
  259. /* PIT gave no useful value */
  260. printk(KERN_WARNING "TSC: PIT calibration failed due to "
  261. "SMI disturbance.\n");
  262. /* We don't have an alternative source, disable TSC */
  263. if (!hpet && !ref1 && !ref2) {
  264. printk("TSC: No reference (HPET/PMTIMER) available\n");
  265. return 0;
  266. }
  267. /* The alternative source failed as well, disable TSC */
  268. if (tsc_ref_min == ULONG_MAX) {
  269. printk(KERN_WARNING "TSC: HPET/PMTIMER calibration "
  270. "failed due to SMI disturbance.\n");
  271. return 0;
  272. }
  273. /* Use the alternative source */
  274. printk(KERN_INFO "TSC: using %s reference calibration\n",
  275. hpet ? "HPET" : "PMTIMER");
  276. return tsc_ref_min;
  277. }
  278. /* We don't have an alternative source, use the PIT calibration value */
  279. if (!hpet && !ref1 && !ref2) {
  280. printk(KERN_INFO "TSC: Using PIT calibration value\n");
  281. return tsc_pit_min;
  282. }
  283. /* The alternative source failed, use the PIT calibration value */
  284. if (tsc_ref_min == ULONG_MAX) {
  285. printk(KERN_WARNING "TSC: HPET/PMTIMER calibration failed due "
  286. "to SMI disturbance. Using PIT calibration\n");
  287. return tsc_pit_min;
  288. }
  289. /* Check the reference deviation */
  290. delta = ((u64) tsc_pit_min) * 100;
  291. do_div(delta, tsc_ref_min);
  292. /*
  293. * If both calibration results are inside a 5% window, the we
  294. * use the lower frequency of those as it is probably the
  295. * closest estimate.
  296. */
  297. if (delta >= 95 && delta <= 105) {
  298. printk(KERN_INFO "TSC: PIT calibration confirmed by %s.\n",
  299. hpet ? "HPET" : "PMTIMER");
  300. printk(KERN_INFO "TSC: using %s calibration value\n",
  301. tsc_pit_min <= tsc_ref_min ? "PIT" :
  302. hpet ? "HPET" : "PMTIMER");
  303. return tsc_pit_min <= tsc_ref_min ? tsc_pit_min : tsc_ref_min;
  304. }
  305. printk(KERN_WARNING "TSC: PIT calibration deviates from %s: %lu %lu.\n",
  306. hpet ? "HPET" : "PMTIMER", tsc_pit_min, tsc_ref_min);
  307. /*
  308. * The calibration values differ too much. In doubt, we use
  309. * the PIT value as we know that there are PMTIMERs around
  310. * running at double speed.
  311. */
  312. printk(KERN_INFO "TSC: Using PIT calibration value\n");
  313. return tsc_pit_min;
  314. }
  315. #ifdef CONFIG_X86_32
  316. /* Only called from the Powernow K7 cpu freq driver */
  317. int recalibrate_cpu_khz(void)
  318. {
  319. #ifndef CONFIG_SMP
  320. unsigned long cpu_khz_old = cpu_khz;
  321. if (cpu_has_tsc) {
  322. tsc_khz = calibrate_tsc();
  323. cpu_khz = tsc_khz;
  324. cpu_data(0).loops_per_jiffy =
  325. cpufreq_scale(cpu_data(0).loops_per_jiffy,
  326. cpu_khz_old, cpu_khz);
  327. return 0;
  328. } else
  329. return -ENODEV;
  330. #else
  331. return -ENODEV;
  332. #endif
  333. }
  334. EXPORT_SYMBOL(recalibrate_cpu_khz);
  335. #endif /* CONFIG_X86_32 */
  336. /* Accelerators for sched_clock()
  337. * convert from cycles(64bits) => nanoseconds (64bits)
  338. * basic equation:
  339. * ns = cycles / (freq / ns_per_sec)
  340. * ns = cycles * (ns_per_sec / freq)
  341. * ns = cycles * (10^9 / (cpu_khz * 10^3))
  342. * ns = cycles * (10^6 / cpu_khz)
  343. *
  344. * Then we use scaling math (suggested by george@mvista.com) to get:
  345. * ns = cycles * (10^6 * SC / cpu_khz) / SC
  346. * ns = cycles * cyc2ns_scale / SC
  347. *
  348. * And since SC is a constant power of two, we can convert the div
  349. * into a shift.
  350. *
  351. * We can use khz divisor instead of mhz to keep a better precision, since
  352. * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
  353. * (mathieu.desnoyers@polymtl.ca)
  354. *
  355. * -johnstul@us.ibm.com "math is hard, lets go shopping!"
  356. */
  357. DEFINE_PER_CPU(unsigned long, cyc2ns);
  358. static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
  359. {
  360. unsigned long long tsc_now, ns_now;
  361. unsigned long flags, *scale;
  362. local_irq_save(flags);
  363. sched_clock_idle_sleep_event();
  364. scale = &per_cpu(cyc2ns, cpu);
  365. rdtscll(tsc_now);
  366. ns_now = __cycles_2_ns(tsc_now);
  367. if (cpu_khz)
  368. *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
  369. sched_clock_idle_wakeup_event(0);
  370. local_irq_restore(flags);
  371. }
  372. #ifdef CONFIG_CPU_FREQ
  373. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  374. * changes.
  375. *
  376. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  377. * not that important because current Opteron setups do not support
  378. * scaling on SMP anyroads.
  379. *
  380. * Should fix up last_tsc too. Currently gettimeofday in the
  381. * first tick after the change will be slightly wrong.
  382. */
  383. static unsigned int ref_freq;
  384. static unsigned long loops_per_jiffy_ref;
  385. static unsigned long tsc_khz_ref;
  386. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  387. void *data)
  388. {
  389. struct cpufreq_freqs *freq = data;
  390. unsigned long *lpj, dummy;
  391. if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
  392. return 0;
  393. lpj = &dummy;
  394. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  395. #ifdef CONFIG_SMP
  396. lpj = &cpu_data(freq->cpu).loops_per_jiffy;
  397. #else
  398. lpj = &boot_cpu_data.loops_per_jiffy;
  399. #endif
  400. if (!ref_freq) {
  401. ref_freq = freq->old;
  402. loops_per_jiffy_ref = *lpj;
  403. tsc_khz_ref = tsc_khz;
  404. }
  405. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  406. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  407. (val == CPUFREQ_RESUMECHANGE)) {
  408. *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  409. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  410. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  411. mark_tsc_unstable("cpufreq changes");
  412. }
  413. set_cyc2ns_scale(tsc_khz, freq->cpu);
  414. return 0;
  415. }
  416. static struct notifier_block time_cpufreq_notifier_block = {
  417. .notifier_call = time_cpufreq_notifier
  418. };
  419. static int __init cpufreq_tsc(void)
  420. {
  421. if (!cpu_has_tsc)
  422. return 0;
  423. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  424. return 0;
  425. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  426. CPUFREQ_TRANSITION_NOTIFIER);
  427. return 0;
  428. }
  429. core_initcall(cpufreq_tsc);
  430. #endif /* CONFIG_CPU_FREQ */
  431. /* clocksource code */
  432. static struct clocksource clocksource_tsc;
  433. /*
  434. * We compare the TSC to the cycle_last value in the clocksource
  435. * structure to avoid a nasty time-warp. This can be observed in a
  436. * very small window right after one CPU updated cycle_last under
  437. * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
  438. * is smaller than the cycle_last reference value due to a TSC which
  439. * is slighty behind. This delta is nowhere else observable, but in
  440. * that case it results in a forward time jump in the range of hours
  441. * due to the unsigned delta calculation of the time keeping core
  442. * code, which is necessary to support wrapping clocksources like pm
  443. * timer.
  444. */
  445. static cycle_t read_tsc(void)
  446. {
  447. cycle_t ret = (cycle_t)get_cycles();
  448. return ret >= clocksource_tsc.cycle_last ?
  449. ret : clocksource_tsc.cycle_last;
  450. }
  451. #ifdef CONFIG_X86_64
  452. static cycle_t __vsyscall_fn vread_tsc(void)
  453. {
  454. cycle_t ret = (cycle_t)vget_cycles();
  455. return ret >= __vsyscall_gtod_data.clock.cycle_last ?
  456. ret : __vsyscall_gtod_data.clock.cycle_last;
  457. }
  458. #endif
  459. static struct clocksource clocksource_tsc = {
  460. .name = "tsc",
  461. .rating = 300,
  462. .read = read_tsc,
  463. .mask = CLOCKSOURCE_MASK(64),
  464. .shift = 22,
  465. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  466. CLOCK_SOURCE_MUST_VERIFY,
  467. #ifdef CONFIG_X86_64
  468. .vread = vread_tsc,
  469. #endif
  470. };
  471. void mark_tsc_unstable(char *reason)
  472. {
  473. if (!tsc_unstable) {
  474. tsc_unstable = 1;
  475. printk("Marking TSC unstable due to %s\n", reason);
  476. /* Change only the rating, when not registered */
  477. if (clocksource_tsc.mult)
  478. clocksource_change_rating(&clocksource_tsc, 0);
  479. else
  480. clocksource_tsc.rating = 0;
  481. }
  482. }
  483. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  484. static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
  485. {
  486. printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
  487. d->ident);
  488. tsc_unstable = 1;
  489. return 0;
  490. }
  491. /* List of systems that have known TSC problems */
  492. static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
  493. {
  494. .callback = dmi_mark_tsc_unstable,
  495. .ident = "IBM Thinkpad 380XD",
  496. .matches = {
  497. DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
  498. DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
  499. },
  500. },
  501. {}
  502. };
  503. /*
  504. * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
  505. */
  506. #ifdef CONFIG_MGEODE_LX
  507. /* RTSC counts during suspend */
  508. #define RTSC_SUSP 0x100
  509. static void __init check_geode_tsc_reliable(void)
  510. {
  511. unsigned long res_low, res_high;
  512. rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
  513. if (res_low & RTSC_SUSP)
  514. clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
  515. }
  516. #else
  517. static inline void check_geode_tsc_reliable(void) { }
  518. #endif
  519. /*
  520. * Make an educated guess if the TSC is trustworthy and synchronized
  521. * over all CPUs.
  522. */
  523. __cpuinit int unsynchronized_tsc(void)
  524. {
  525. if (!cpu_has_tsc || tsc_unstable)
  526. return 1;
  527. #ifdef CONFIG_SMP
  528. if (apic_is_clustered_box())
  529. return 1;
  530. #endif
  531. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  532. return 0;
  533. /*
  534. * Intel systems are normally all synchronized.
  535. * Exceptions must mark TSC as unstable:
  536. */
  537. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
  538. /* assume multi socket systems are not synchronized: */
  539. if (num_possible_cpus() > 1)
  540. tsc_unstable = 1;
  541. }
  542. return tsc_unstable;
  543. }
  544. static void __init init_tsc_clocksource(void)
  545. {
  546. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  547. clocksource_tsc.shift);
  548. /* lower the rating if we already know its unstable: */
  549. if (check_tsc_unstable()) {
  550. clocksource_tsc.rating = 0;
  551. clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
  552. }
  553. clocksource_register(&clocksource_tsc);
  554. }
  555. void __init tsc_init(void)
  556. {
  557. u64 lpj;
  558. int cpu;
  559. if (!cpu_has_tsc)
  560. return;
  561. tsc_khz = calibrate_tsc();
  562. cpu_khz = tsc_khz;
  563. if (!tsc_khz) {
  564. mark_tsc_unstable("could not calculate TSC khz");
  565. return;
  566. }
  567. #ifdef CONFIG_X86_64
  568. if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
  569. (boot_cpu_data.x86_vendor == X86_VENDOR_AMD))
  570. cpu_khz = calibrate_cpu();
  571. #endif
  572. lpj = ((u64)tsc_khz * 1000);
  573. do_div(lpj, HZ);
  574. lpj_fine = lpj;
  575. printk("Detected %lu.%03lu MHz processor.\n",
  576. (unsigned long)cpu_khz / 1000,
  577. (unsigned long)cpu_khz % 1000);
  578. /*
  579. * Secondary CPUs do not run through tsc_init(), so set up
  580. * all the scale factors for all CPUs, assuming the same
  581. * speed as the bootup CPU. (cpufreq notifiers will fix this
  582. * up if their speed diverges)
  583. */
  584. for_each_possible_cpu(cpu)
  585. set_cyc2ns_scale(cpu_khz, cpu);
  586. if (tsc_disabled > 0)
  587. return;
  588. /* now allow native_sched_clock() to use rdtsc */
  589. tsc_disabled = 0;
  590. use_tsc_delay();
  591. /* Check and install the TSC clocksource */
  592. dmi_check_system(bad_tsc_dmi_table);
  593. if (unsynchronized_tsc())
  594. mark_tsc_unstable("TSCs unsynchronized");
  595. check_geode_tsc_reliable();
  596. init_tsc_clocksource();
  597. }