tsc.c 4.8 KB

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