tsc_64.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <linux/kernel.h>
  2. #include <linux/sched.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/init.h>
  5. #include <linux/clocksource.h>
  6. #include <linux/time.h>
  7. #include <linux/acpi.h>
  8. #include <linux/cpufreq.h>
  9. #include <linux/acpi_pmtmr.h>
  10. #include <asm/hpet.h>
  11. #include <asm/timex.h>
  12. #include <asm/timer.h>
  13. #include <asm/vgtod.h>
  14. extern int tsc_unstable;
  15. extern int tsc_disabled;
  16. /*
  17. * Make an educated guess if the TSC is trustworthy and synchronized
  18. * over all CPUs.
  19. */
  20. __cpuinit int unsynchronized_tsc(void)
  21. {
  22. if (tsc_unstable)
  23. return 1;
  24. #ifdef CONFIG_SMP
  25. if (apic_is_clustered_box())
  26. return 1;
  27. #endif
  28. if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
  29. return 0;
  30. /* Assume multi socket systems are not synchronized */
  31. return num_present_cpus() > 1;
  32. }
  33. static struct clocksource clocksource_tsc;
  34. /*
  35. * We compare the TSC to the cycle_last value in the clocksource
  36. * structure to avoid a nasty time-warp. This can be observed in a
  37. * very small window right after one CPU updated cycle_last under
  38. * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
  39. * is smaller than the cycle_last reference value due to a TSC which
  40. * is slighty behind. This delta is nowhere else observable, but in
  41. * that case it results in a forward time jump in the range of hours
  42. * due to the unsigned delta calculation of the time keeping core
  43. * code, which is necessary to support wrapping clocksources like pm
  44. * timer.
  45. */
  46. static cycle_t read_tsc(void)
  47. {
  48. cycle_t ret = (cycle_t)get_cycles();
  49. return ret >= clocksource_tsc.cycle_last ?
  50. ret : clocksource_tsc.cycle_last;
  51. }
  52. static cycle_t __vsyscall_fn vread_tsc(void)
  53. {
  54. cycle_t ret = (cycle_t)vget_cycles();
  55. return ret >= __vsyscall_gtod_data.clock.cycle_last ?
  56. ret : __vsyscall_gtod_data.clock.cycle_last;
  57. }
  58. static struct clocksource clocksource_tsc = {
  59. .name = "tsc",
  60. .rating = 300,
  61. .read = read_tsc,
  62. .mask = CLOCKSOURCE_MASK(64),
  63. .shift = 22,
  64. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  65. CLOCK_SOURCE_MUST_VERIFY,
  66. .vread = vread_tsc,
  67. };
  68. void mark_tsc_unstable(char *reason)
  69. {
  70. if (!tsc_unstable) {
  71. tsc_unstable = 1;
  72. printk("Marking TSC unstable due to %s\n", reason);
  73. /* Change only the rating, when not registered */
  74. if (clocksource_tsc.mult)
  75. clocksource_change_rating(&clocksource_tsc, 0);
  76. else
  77. clocksource_tsc.rating = 0;
  78. }
  79. }
  80. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  81. void __init init_tsc_clocksource(void)
  82. {
  83. if (tsc_disabled > 0)
  84. return;
  85. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  86. clocksource_tsc.shift);
  87. if (check_tsc_unstable())
  88. clocksource_tsc.rating = 0;
  89. clocksource_register(&clocksource_tsc);
  90. }