tsc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <asm/timex.h>
  10. static int notsc __initdata = 0;
  11. unsigned int cpu_khz; /* TSC clocks / usec, not used here */
  12. EXPORT_SYMBOL(cpu_khz);
  13. unsigned int tsc_khz;
  14. EXPORT_SYMBOL(tsc_khz);
  15. static unsigned int cyc2ns_scale __read_mostly;
  16. void set_cyc2ns_scale(unsigned long khz)
  17. {
  18. cyc2ns_scale = (NSEC_PER_MSEC << NS_SCALE) / khz;
  19. }
  20. static unsigned long long cycles_2_ns(unsigned long long cyc)
  21. {
  22. return (cyc * cyc2ns_scale) >> NS_SCALE;
  23. }
  24. unsigned long long sched_clock(void)
  25. {
  26. unsigned long a = 0;
  27. /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
  28. * which means it is not completely exact and may not be monotonous
  29. * between CPUs. But the errors should be too small to matter for
  30. * scheduling purposes.
  31. */
  32. rdtscll(a);
  33. return cycles_2_ns(a);
  34. }
  35. static int tsc_unstable;
  36. inline int check_tsc_unstable(void)
  37. {
  38. return tsc_unstable;
  39. }
  40. #ifdef CONFIG_CPU_FREQ
  41. /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
  42. * changes.
  43. *
  44. * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
  45. * not that important because current Opteron setups do not support
  46. * scaling on SMP anyroads.
  47. *
  48. * Should fix up last_tsc too. Currently gettimeofday in the
  49. * first tick after the change will be slightly wrong.
  50. */
  51. static unsigned int ref_freq;
  52. static unsigned long loops_per_jiffy_ref;
  53. static unsigned long tsc_khz_ref;
  54. static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  55. void *data)
  56. {
  57. struct cpufreq_freqs *freq = data;
  58. unsigned long *lpj, dummy;
  59. if (cpu_has(&cpu_data[freq->cpu], X86_FEATURE_CONSTANT_TSC))
  60. return 0;
  61. lpj = &dummy;
  62. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  63. #ifdef CONFIG_SMP
  64. lpj = &cpu_data[freq->cpu].loops_per_jiffy;
  65. #else
  66. lpj = &boot_cpu_data.loops_per_jiffy;
  67. #endif
  68. if (!ref_freq) {
  69. ref_freq = freq->old;
  70. loops_per_jiffy_ref = *lpj;
  71. tsc_khz_ref = tsc_khz;
  72. }
  73. if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
  74. (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
  75. (val == CPUFREQ_RESUMECHANGE)) {
  76. *lpj =
  77. cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
  78. tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
  79. if (!(freq->flags & CPUFREQ_CONST_LOOPS))
  80. mark_tsc_unstable("cpufreq changes");
  81. }
  82. set_cyc2ns_scale(tsc_khz_ref);
  83. return 0;
  84. }
  85. static struct notifier_block time_cpufreq_notifier_block = {
  86. .notifier_call = time_cpufreq_notifier
  87. };
  88. static int __init cpufreq_tsc(void)
  89. {
  90. cpufreq_register_notifier(&time_cpufreq_notifier_block,
  91. CPUFREQ_TRANSITION_NOTIFIER);
  92. return 0;
  93. }
  94. core_initcall(cpufreq_tsc);
  95. #endif
  96. /*
  97. * Make an educated guess if the TSC is trustworthy and synchronized
  98. * over all CPUs.
  99. */
  100. __cpuinit int unsynchronized_tsc(void)
  101. {
  102. if (tsc_unstable)
  103. return 1;
  104. #ifdef CONFIG_SMP
  105. if (apic_is_clustered_box())
  106. return 1;
  107. #endif
  108. /* Most intel systems have synchronized TSCs except for
  109. multi node systems */
  110. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
  111. #ifdef CONFIG_ACPI
  112. /* But TSC doesn't tick in C3 so don't use it there */
  113. if (acpi_gbl_FADT.header.length > 0 &&
  114. acpi_gbl_FADT.C3latency < 1000)
  115. return 1;
  116. #endif
  117. return 0;
  118. }
  119. /* Assume multi socket systems are not synchronized */
  120. return num_present_cpus() > 1;
  121. }
  122. int __init notsc_setup(char *s)
  123. {
  124. notsc = 1;
  125. return 1;
  126. }
  127. __setup("notsc", notsc_setup);
  128. /* clock source code: */
  129. static cycle_t read_tsc(void)
  130. {
  131. cycle_t ret = (cycle_t)get_cycles_sync();
  132. return ret;
  133. }
  134. static cycle_t __vsyscall_fn vread_tsc(void)
  135. {
  136. cycle_t ret = (cycle_t)get_cycles_sync();
  137. return ret;
  138. }
  139. static struct clocksource clocksource_tsc = {
  140. .name = "tsc",
  141. .rating = 300,
  142. .read = read_tsc,
  143. .mask = CLOCKSOURCE_MASK(64),
  144. .shift = 22,
  145. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  146. CLOCK_SOURCE_MUST_VERIFY,
  147. .vread = vread_tsc,
  148. };
  149. void mark_tsc_unstable(char *reason)
  150. {
  151. if (!tsc_unstable) {
  152. tsc_unstable = 1;
  153. printk("Marking TSC unstable due to %s\n", reason);
  154. /* Change only the rating, when not registered */
  155. if (clocksource_tsc.mult)
  156. clocksource_change_rating(&clocksource_tsc, 0);
  157. else
  158. clocksource_tsc.rating = 0;
  159. }
  160. }
  161. EXPORT_SYMBOL_GPL(mark_tsc_unstable);
  162. void __init init_tsc_clocksource(void)
  163. {
  164. if (!notsc) {
  165. clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
  166. clocksource_tsc.shift);
  167. if (check_tsc_unstable())
  168. clocksource_tsc.rating = 0;
  169. clocksource_register(&clocksource_tsc);
  170. }
  171. }