calibrate.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 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 calibrate_delay_direct(void)
  150. {
  151. return 0;
  152. }
  153. #endif
  154. /*
  155. * This is the number of bits of precision for the loops_per_jiffy. Each
  156. * time we refine our estimate after the first takes 1.5/HZ seconds, so try
  157. * to start with a good estimate.
  158. * For the boot cpu we can skip the delay calibration and assign it a value
  159. * calculated based on the timer frequency.
  160. * For the rest of the CPUs we cannot assume that the timer frequency is same as
  161. * the cpu frequency, hence do the calibration for those.
  162. */
  163. #define LPS_PREC 8
  164. static unsigned long calibrate_delay_converge(void)
  165. {
  166. /* First stage - slowly accelerate to find initial bounds */
  167. unsigned long lpj, lpj_base, ticks, loopadd, loopadd_base, chop_limit;
  168. int trials = 0, band = 0, trial_in_band = 0;
  169. lpj = (1<<12);
  170. /* wait for "start of" clock tick */
  171. ticks = jiffies;
  172. while (ticks == jiffies)
  173. ; /* nothing */
  174. /* Go .. */
  175. ticks = jiffies;
  176. do {
  177. if (++trial_in_band == (1<<band)) {
  178. ++band;
  179. trial_in_band = 0;
  180. }
  181. __delay(lpj * band);
  182. trials += band;
  183. } while (ticks == jiffies);
  184. /*
  185. * We overshot, so retreat to a clear underestimate. Then estimate
  186. * the largest likely undershoot. This defines our chop bounds.
  187. */
  188. trials -= band;
  189. loopadd_base = lpj * band;
  190. lpj_base = lpj * trials;
  191. recalibrate:
  192. lpj = lpj_base;
  193. loopadd = loopadd_base;
  194. /*
  195. * Do a binary approximation to get lpj set to
  196. * equal one clock (up to LPS_PREC bits)
  197. */
  198. chop_limit = lpj >> LPS_PREC;
  199. while (loopadd > chop_limit) {
  200. lpj += loopadd;
  201. ticks = jiffies;
  202. while (ticks == jiffies)
  203. ; /* nothing */
  204. ticks = jiffies;
  205. __delay(lpj);
  206. if (jiffies != ticks) /* longer than 1 tick */
  207. lpj -= loopadd;
  208. loopadd >>= 1;
  209. }
  210. /*
  211. * If we incremented every single time possible, presume we've
  212. * massively underestimated initially, and retry with a higher
  213. * start, and larger range. (Only seen on x86_64, due to SMIs)
  214. */
  215. if (lpj + loopadd * 2 == lpj_base + loopadd_base * 2) {
  216. lpj_base = lpj;
  217. loopadd_base <<= 2;
  218. goto recalibrate;
  219. }
  220. return lpj;
  221. }
  222. static DEFINE_PER_CPU(unsigned long, cpu_loops_per_jiffy) = { 0 };
  223. /*
  224. * Check if cpu calibration delay is already known. For example,
  225. * some processors with multi-core sockets may have all cores
  226. * with the same calibration delay.
  227. *
  228. * Architectures should override this function if a faster calibration
  229. * method is available.
  230. */
  231. unsigned long __attribute__((weak)) calibrate_delay_is_known(void)
  232. {
  233. return 0;
  234. }
  235. /*
  236. * Indicate the cpu delay calibration is done. This can be used by
  237. * architectures to stop accepting delay timer registrations after this point.
  238. */
  239. void __attribute__((weak)) calibration_delay_done(void)
  240. {
  241. }
  242. void calibrate_delay(void)
  243. {
  244. unsigned long lpj;
  245. static bool printed;
  246. int this_cpu = smp_processor_id();
  247. if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
  248. lpj = per_cpu(cpu_loops_per_jiffy, this_cpu);
  249. if (!printed)
  250. pr_info("Calibrating delay loop (skipped) "
  251. "already calibrated this CPU");
  252. } else if (preset_lpj) {
  253. lpj = preset_lpj;
  254. if (!printed)
  255. pr_info("Calibrating delay loop (skipped) "
  256. "preset value.. ");
  257. } else if ((!printed) && lpj_fine) {
  258. lpj = lpj_fine;
  259. pr_info("Calibrating delay loop (skipped), "
  260. "value calculated using timer frequency.. ");
  261. } else if ((lpj = calibrate_delay_is_known())) {
  262. ;
  263. } else if ((lpj = calibrate_delay_direct()) != 0) {
  264. if (!printed)
  265. pr_info("Calibrating delay using timer "
  266. "specific routine.. ");
  267. } else {
  268. if (!printed)
  269. pr_info("Calibrating delay loop... ");
  270. lpj = calibrate_delay_converge();
  271. }
  272. per_cpu(cpu_loops_per_jiffy, this_cpu) = lpj;
  273. if (!printed)
  274. pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
  275. lpj/(500000/HZ),
  276. (lpj/(5000/HZ)) % 100, lpj);
  277. loops_per_jiffy = lpj;
  278. printed = true;
  279. calibration_delay_done();
  280. }