calibrate.c 8.6 KB

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