calibrate.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* calibrate.c: default delay calibration
  2. *
  3. * Excised from init/main.c
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/jiffies.h>
  7. #include <linux/delay.h>
  8. #include <linux/init.h>
  9. #include <linux/timex.h>
  10. #include <linux/smp.h>
  11. #include <linux/percpu.h>
  12. unsigned long lpj_fine;
  13. unsigned long preset_lpj;
  14. static int __init lpj_setup(char *str)
  15. {
  16. preset_lpj = simple_strtoul(str,NULL,0);
  17. return 1;
  18. }
  19. __setup("lpj=", lpj_setup);
  20. #ifdef ARCH_HAS_READ_CURRENT_TIMER
  21. /* This routine uses the read_current_timer() routine and gets the
  22. * loops per jiffy directly, instead of guessing it using delay().
  23. * Also, this code tries to handle non-maskable asynchronous events
  24. * (like SMIs)
  25. */
  26. #define DELAY_CALIBRATION_TICKS ((HZ < 100) ? 1 : (HZ/100))
  27. #define MAX_DIRECT_CALIBRATION_RETRIES 5
  28. static unsigned long __cpuinit calibrate_delay_direct(void)
  29. {
  30. unsigned long pre_start, start, post_start;
  31. unsigned long pre_end, end, post_end;
  32. unsigned long start_jiffies;
  33. unsigned long timer_rate_min, timer_rate_max;
  34. unsigned long good_timer_sum = 0;
  35. unsigned long good_timer_count = 0;
  36. unsigned long measured_times[MAX_DIRECT_CALIBRATION_RETRIES];
  37. int max = -1; /* index of measured_times with max/min values or not set */
  38. int min = -1;
  39. int i;
  40. if (read_current_timer(&pre_start) < 0 )
  41. return 0;
  42. /*
  43. * A simple loop like
  44. * while ( jiffies < start_jiffies+1)
  45. * start = read_current_timer();
  46. * will not do. As we don't really know whether jiffy switch
  47. * happened first or timer_value was read first. And some asynchronous
  48. * event can happen between these two events introducing errors in lpj.
  49. *
  50. * So, we do
  51. * 1. pre_start <- When we are sure that jiffy switch hasn't happened
  52. * 2. check jiffy switch
  53. * 3. start <- timer value before or after jiffy switch
  54. * 4. post_start <- When we are sure that jiffy switch has happened
  55. *
  56. * Note, we don't know anything about order of 2 and 3.
  57. * Now, by looking at post_start and pre_start difference, we can
  58. * check whether any asynchronous event happened or not
  59. */
  60. for (i = 0; i < MAX_DIRECT_CALIBRATION_RETRIES; i++) {
  61. pre_start = 0;
  62. read_current_timer(&start);
  63. start_jiffies = jiffies;
  64. while (time_before_eq(jiffies, start_jiffies + 1)) {
  65. pre_start = start;
  66. read_current_timer(&start);
  67. }
  68. read_current_timer(&post_start);
  69. pre_end = 0;
  70. end = post_start;
  71. while (time_before_eq(jiffies, start_jiffies + 1 +
  72. DELAY_CALIBRATION_TICKS)) {
  73. pre_end = end;
  74. read_current_timer(&end);
  75. }
  76. read_current_timer(&post_end);
  77. timer_rate_max = (post_end - pre_start) /
  78. DELAY_CALIBRATION_TICKS;
  79. timer_rate_min = (pre_end - post_start) /
  80. DELAY_CALIBRATION_TICKS;
  81. /*
  82. * If the upper limit and lower limit of the timer_rate is
  83. * >= 12.5% apart, redo calibration.
  84. */
  85. if (start >= post_end)
  86. printk(KERN_NOTICE "calibrate_delay_direct() ignoring "
  87. "timer_rate as we had a TSC wrap around"
  88. " start=%lu >=post_end=%lu\n",
  89. start, post_end);
  90. if (start < post_end && pre_start != 0 && pre_end != 0 &&
  91. (timer_rate_max - timer_rate_min) < (timer_rate_max >> 3)) {
  92. good_timer_count++;
  93. good_timer_sum += timer_rate_max;
  94. measured_times[i] = timer_rate_max;
  95. if (max < 0 || timer_rate_max > measured_times[max])
  96. max = i;
  97. if (min < 0 || timer_rate_max < measured_times[min])
  98. min = i;
  99. } else
  100. measured_times[i] = 0;
  101. }
  102. /*
  103. * Find the maximum & minimum - if they differ too much throw out the
  104. * one with the largest difference from the mean and try again...
  105. */
  106. while (good_timer_count > 1) {
  107. unsigned long estimate;
  108. unsigned long maxdiff;
  109. /* compute the estimate */
  110. estimate = (good_timer_sum/good_timer_count);
  111. maxdiff = estimate >> 3;
  112. /* if range is within 12% let's take it */
  113. if ((measured_times[max] - measured_times[min]) < maxdiff)
  114. return estimate;
  115. /* ok - drop the worse value and try again... */
  116. good_timer_sum = 0;
  117. good_timer_count = 0;
  118. if ((measured_times[max] - estimate) <
  119. (estimate - measured_times[min])) {
  120. printk(KERN_NOTICE "calibrate_delay_direct() dropping "
  121. "min bogoMips estimate %d = %lu\n",
  122. min, measured_times[min]);
  123. measured_times[min] = 0;
  124. min = max;
  125. } else {
  126. printk(KERN_NOTICE "calibrate_delay_direct() dropping "
  127. "max bogoMips estimate %d = %lu\n",
  128. max, measured_times[max]);
  129. measured_times[max] = 0;
  130. max = min;
  131. }
  132. for (i = 0; i < MAX_DIRECT_CALIBRATION_RETRIES; i++) {
  133. if (measured_times[i] == 0)
  134. continue;
  135. good_timer_count++;
  136. good_timer_sum += measured_times[i];
  137. if (measured_times[i] < measured_times[min])
  138. min = i;
  139. if (measured_times[i] > measured_times[max])
  140. max = i;
  141. }
  142. }
  143. printk(KERN_NOTICE "calibrate_delay_direct() failed to get a good "
  144. "estimate for loops_per_jiffy.\nProbably due to long platform "
  145. "interrupts. Consider using \"lpj=\" boot option.\n");
  146. return 0;
  147. }
  148. #else
  149. static unsigned long __cpuinit calibrate_delay_direct(void) {return 0;}
  150. #endif
  151. /*
  152. * This is the number of bits of precision for the loops_per_jiffy. Each
  153. * time we refine our estimate after the first takes 1.5/HZ seconds, so try
  154. * to start with a good estimate.
  155. * For the boot cpu we can skip the delay calibration and assign it a value
  156. * calculated based on the timer frequency.
  157. * For the rest of the CPUs we cannot assume that the timer frequency is same as
  158. * the cpu frequency, hence do the calibration for those.
  159. */
  160. #define LPS_PREC 8
  161. static unsigned long __cpuinit calibrate_delay_converge(void)
  162. {
  163. /* First stage - slowly accelerate to find initial bounds */
  164. unsigned long lpj, lpj_base, ticks, loopadd, loopadd_base, chop_limit;
  165. int trials = 0, band = 0, trial_in_band = 0;
  166. lpj = (1<<12);
  167. /* wait for "start of" clock tick */
  168. ticks = jiffies;
  169. while (ticks == jiffies)
  170. ; /* nothing */
  171. /* Go .. */
  172. ticks = jiffies;
  173. do {
  174. if (++trial_in_band == (1<<band)) {
  175. ++band;
  176. trial_in_band = 0;
  177. }
  178. __delay(lpj * band);
  179. trials += band;
  180. } while (ticks == jiffies);
  181. /*
  182. * We overshot, so retreat to a clear underestimate. Then estimate
  183. * the largest likely undershoot. This defines our chop bounds.
  184. */
  185. trials -= band;
  186. loopadd_base = lpj * band;
  187. lpj_base = lpj * trials;
  188. recalibrate:
  189. lpj = lpj_base;
  190. loopadd = loopadd_base;
  191. /*
  192. * Do a binary approximation to get lpj set to
  193. * equal one clock (up to LPS_PREC bits)
  194. */
  195. chop_limit = lpj >> LPS_PREC;
  196. while (loopadd > chop_limit) {
  197. lpj += loopadd;
  198. ticks = jiffies;
  199. while (ticks == jiffies)
  200. ; /* nothing */
  201. ticks = jiffies;
  202. __delay(lpj);
  203. if (jiffies != ticks) /* longer than 1 tick */
  204. lpj -= loopadd;
  205. loopadd >>= 1;
  206. }
  207. /*
  208. * If we incremented every single time possible, presume we've
  209. * massively underestimated initially, and retry with a higher
  210. * start, and larger range. (Only seen on x86_64, due to SMIs)
  211. */
  212. if (lpj + loopadd * 2 == lpj_base + loopadd_base * 2) {
  213. lpj_base = lpj;
  214. loopadd_base <<= 2;
  215. goto recalibrate;
  216. }
  217. return lpj;
  218. }
  219. static DEFINE_PER_CPU(unsigned long, cpu_loops_per_jiffy) = { 0 };
  220. /*
  221. * Check if cpu calibration delay is already known. For example,
  222. * some processors with multi-core sockets may have all cores
  223. * with the same calibration delay.
  224. *
  225. * Architectures should override this function if a faster calibration
  226. * method is available.
  227. */
  228. unsigned long __attribute__((weak)) __cpuinit calibrate_delay_is_known(void)
  229. {
  230. return 0;
  231. }
  232. void __cpuinit calibrate_delay(void)
  233. {
  234. unsigned long lpj;
  235. static bool printed;
  236. int this_cpu = smp_processor_id();
  237. if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
  238. lpj = per_cpu(cpu_loops_per_jiffy, this_cpu);
  239. if (!printed)
  240. pr_info("Calibrating delay loop (skipped) "
  241. "already calibrated this CPU");
  242. } else if (preset_lpj) {
  243. lpj = preset_lpj;
  244. if (!printed)
  245. pr_info("Calibrating delay loop (skipped) "
  246. "preset value.. ");
  247. } else if ((!printed) && lpj_fine) {
  248. lpj = lpj_fine;
  249. pr_info("Calibrating delay loop (skipped), "
  250. "value calculated using timer frequency.. ");
  251. } else if ((lpj = calibrate_delay_is_known())) {
  252. ;
  253. } else if ((lpj = calibrate_delay_direct()) != 0) {
  254. if (!printed)
  255. pr_info("Calibrating delay using timer "
  256. "specific routine.. ");
  257. } else {
  258. if (!printed)
  259. pr_info("Calibrating delay loop... ");
  260. lpj = calibrate_delay_converge();
  261. }
  262. per_cpu(cpu_loops_per_jiffy, this_cpu) = lpj;
  263. if (!printed)
  264. pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
  265. lpj/(500000/HZ),
  266. (lpj/(5000/HZ)) % 100, lpj);
  267. loops_per_jiffy = lpj;
  268. printed = true;
  269. }