tsc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <asm/hpet.h>
  8. unsigned int cpu_khz; /* TSC clocks / usec, not used here */
  9. EXPORT_SYMBOL(cpu_khz);
  10. unsigned int tsc_khz;
  11. EXPORT_SYMBOL(tsc_khz);
  12. /*
  13. * TSC can be unstable due to cpufreq or due to unsynced TSCs
  14. */
  15. int tsc_unstable;
  16. /* native_sched_clock() is called before tsc_init(), so
  17. we must start with the TSC soft disabled to prevent
  18. erroneous rdtsc usage on !cpu_has_tsc processors */
  19. int tsc_disabled = -1;
  20. /*
  21. * Scheduler clock - returns current time in nanosec units.
  22. */
  23. u64 native_sched_clock(void)
  24. {
  25. u64 this_offset;
  26. /*
  27. * Fall back to jiffies if there's no TSC available:
  28. * ( But note that we still use it if the TSC is marked
  29. * unstable. We do this because unlike Time Of Day,
  30. * the scheduler clock tolerates small errors and it's
  31. * very important for it to be as fast as the platform
  32. * can achive it. )
  33. */
  34. if (unlikely(tsc_disabled)) {
  35. /* No locking but a rare wrong value is not a big deal: */
  36. return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
  37. }
  38. /* read the Time Stamp Counter: */
  39. rdtscll(this_offset);
  40. /* return the value in ns */
  41. return cycles_2_ns(this_offset);
  42. }
  43. /* We need to define a real function for sched_clock, to override the
  44. weak default version */
  45. #ifdef CONFIG_PARAVIRT
  46. unsigned long long sched_clock(void)
  47. {
  48. return paravirt_sched_clock();
  49. }
  50. #else
  51. unsigned long long
  52. sched_clock(void) __attribute__((alias("native_sched_clock")));
  53. #endif
  54. int check_tsc_unstable(void)
  55. {
  56. return tsc_unstable;
  57. }
  58. EXPORT_SYMBOL_GPL(check_tsc_unstable);
  59. #ifdef CONFIG_X86_TSC
  60. int __init notsc_setup(char *str)
  61. {
  62. printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
  63. "cannot disable TSC completely.\n");
  64. tsc_disabled = 1;
  65. return 1;
  66. }
  67. #else
  68. /*
  69. * disable flag for tsc. Takes effect by clearing the TSC cpu flag
  70. * in cpu/common.c
  71. */
  72. int __init notsc_setup(char *str)
  73. {
  74. setup_clear_cpu_cap(X86_FEATURE_TSC);
  75. return 1;
  76. }
  77. #endif
  78. __setup("notsc", notsc_setup);
  79. #define MAX_RETRIES 5
  80. #define SMI_TRESHOLD 50000
  81. /*
  82. * Read TSC and the reference counters. Take care of SMI disturbance
  83. */
  84. static u64 __init tsc_read_refs(u64 *pm, u64 *hpet)
  85. {
  86. u64 t1, t2;
  87. int i;
  88. for (i = 0; i < MAX_RETRIES; i++) {
  89. t1 = get_cycles();
  90. if (hpet)
  91. *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
  92. else
  93. *pm = acpi_pm_read_early();
  94. t2 = get_cycles();
  95. if ((t2 - t1) < SMI_TRESHOLD)
  96. return t2;
  97. }
  98. return ULLONG_MAX;
  99. }
  100. /**
  101. * tsc_calibrate - calibrate the tsc on boot
  102. */
  103. static unsigned int __init tsc_calibrate(void)
  104. {
  105. unsigned long flags;
  106. u64 tsc1, tsc2, tr1, tr2, delta, pm1, pm2, hpet1, hpet2;
  107. int hpet = is_hpet_enabled();
  108. unsigned int tsc_khz_val = 0;
  109. local_irq_save(flags);
  110. tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
  111. outb((inb(0x61) & ~0x02) | 0x01, 0x61);
  112. outb(0xb0, 0x43);
  113. outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
  114. outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
  115. tr1 = get_cycles();
  116. while ((inb(0x61) & 0x20) == 0);
  117. tr2 = get_cycles();
  118. tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
  119. local_irq_restore(flags);
  120. /*
  121. * Preset the result with the raw and inaccurate PIT
  122. * calibration value
  123. */
  124. delta = (tr2 - tr1);
  125. do_div(delta, 50);
  126. tsc_khz_val = delta;
  127. /* hpet or pmtimer available ? */
  128. if (!hpet && !pm1 && !pm2) {
  129. printk(KERN_INFO "TSC calibrated against PIT\n");
  130. goto out;
  131. }
  132. /* Check, whether the sampling was disturbed by an SMI */
  133. if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX) {
  134. printk(KERN_WARNING "TSC calibration disturbed by SMI, "
  135. "using PIT calibration result\n");
  136. goto out;
  137. }
  138. tsc2 = (tsc2 - tsc1) * 1000000LL;
  139. if (hpet) {
  140. printk(KERN_INFO "TSC calibrated against HPET\n");
  141. if (hpet2 < hpet1)
  142. hpet2 += 0x100000000ULL;
  143. hpet2 -= hpet1;
  144. tsc1 = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
  145. do_div(tsc1, 1000000);
  146. } else {
  147. printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
  148. if (pm2 < pm1)
  149. pm2 += (u64)ACPI_PM_OVRRUN;
  150. pm2 -= pm1;
  151. tsc1 = pm2 * 1000000000LL;
  152. do_div(tsc1, PMTMR_TICKS_PER_SEC);
  153. }
  154. do_div(tsc2, tsc1);
  155. tsc_khz_val = tsc2;
  156. out:
  157. return tsc_khz_val;
  158. }
  159. unsigned long native_calculate_cpu_khz(void)
  160. {
  161. return tsc_calibrate();
  162. }
  163. #ifdef CONFIG_X86_32
  164. /* Only called from the Powernow K7 cpu freq driver */
  165. int recalibrate_cpu_khz(void)
  166. {
  167. #ifndef CONFIG_SMP
  168. unsigned long cpu_khz_old = cpu_khz;
  169. if (cpu_has_tsc) {
  170. cpu_khz = calculate_cpu_khz();
  171. tsc_khz = cpu_khz;
  172. cpu_data(0).loops_per_jiffy =
  173. cpufreq_scale(cpu_data(0).loops_per_jiffy,
  174. cpu_khz_old, cpu_khz);
  175. return 0;
  176. } else
  177. return -ENODEV;
  178. #else
  179. return -ENODEV;
  180. #endif
  181. }
  182. EXPORT_SYMBOL(recalibrate_cpu_khz);
  183. #endif /* CONFIG_X86_32 */