Back to home page

LXR

 
 

    


0001 /* calibrate.c: default delay calibration
0002  *
0003  * Excised from init/main.c
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  */
0006 
0007 #include <linux/jiffies.h>
0008 #include <linux/delay.h>
0009 #include <linux/init.h>
0010 #include <linux/timex.h>
0011 #include <linux/smp.h>
0012 #include <linux/percpu.h>
0013 
0014 unsigned long lpj_fine;
0015 unsigned long preset_lpj;
0016 static int __init lpj_setup(char *str)
0017 {
0018     preset_lpj = simple_strtoul(str,NULL,0);
0019     return 1;
0020 }
0021 
0022 __setup("lpj=", lpj_setup);
0023 
0024 #ifdef ARCH_HAS_READ_CURRENT_TIMER
0025 
0026 /* This routine uses the read_current_timer() routine and gets the
0027  * loops per jiffy directly, instead of guessing it using delay().
0028  * Also, this code tries to handle non-maskable asynchronous events
0029  * (like SMIs)
0030  */
0031 #define DELAY_CALIBRATION_TICKS         ((HZ < 100) ? 1 : (HZ/100))
0032 #define MAX_DIRECT_CALIBRATION_RETRIES      5
0033 
0034 static unsigned long calibrate_delay_direct(void)
0035 {
0036     unsigned long pre_start, start, post_start;
0037     unsigned long pre_end, end, post_end;
0038     unsigned long start_jiffies;
0039     unsigned long timer_rate_min, timer_rate_max;
0040     unsigned long good_timer_sum = 0;
0041     unsigned long good_timer_count = 0;
0042     unsigned long measured_times[MAX_DIRECT_CALIBRATION_RETRIES];
0043     int max = -1; /* index of measured_times with max/min values or not set */
0044     int min = -1;
0045     int i;
0046 
0047     if (read_current_timer(&pre_start) < 0 )
0048         return 0;
0049 
0050     /*
0051      * A simple loop like
0052      *  while ( jiffies < start_jiffies+1)
0053      *      start = read_current_timer();
0054      * will not do. As we don't really know whether jiffy switch
0055      * happened first or timer_value was read first. And some asynchronous
0056      * event can happen between these two events introducing errors in lpj.
0057      *
0058      * So, we do
0059      * 1. pre_start <- When we are sure that jiffy switch hasn't happened
0060      * 2. check jiffy switch
0061      * 3. start <- timer value before or after jiffy switch
0062      * 4. post_start <- When we are sure that jiffy switch has happened
0063      *
0064      * Note, we don't know anything about order of 2 and 3.
0065      * Now, by looking at post_start and pre_start difference, we can
0066      * check whether any asynchronous event happened or not
0067      */
0068 
0069     for (i = 0; i < MAX_DIRECT_CALIBRATION_RETRIES; i++) {
0070         pre_start = 0;
0071         read_current_timer(&start);
0072         start_jiffies = jiffies;
0073         while (time_before_eq(jiffies, start_jiffies + 1)) {
0074             pre_start = start;
0075             read_current_timer(&start);
0076         }
0077         read_current_timer(&post_start);
0078 
0079         pre_end = 0;
0080         end = post_start;
0081         while (time_before_eq(jiffies, start_jiffies + 1 +
0082                            DELAY_CALIBRATION_TICKS)) {
0083             pre_end = end;
0084             read_current_timer(&end);
0085         }
0086         read_current_timer(&post_end);
0087 
0088         timer_rate_max = (post_end - pre_start) /
0089                     DELAY_CALIBRATION_TICKS;
0090         timer_rate_min = (pre_end - post_start) /
0091                     DELAY_CALIBRATION_TICKS;
0092 
0093         /*
0094          * If the upper limit and lower limit of the timer_rate is
0095          * >= 12.5% apart, redo calibration.
0096          */
0097         if (start >= post_end)
0098             printk(KERN_NOTICE "calibrate_delay_direct() ignoring "
0099                     "timer_rate as we had a TSC wrap around"
0100                     " start=%lu >=post_end=%lu\n",
0101                 start, post_end);
0102         if (start < post_end && pre_start != 0 && pre_end != 0 &&
0103             (timer_rate_max - timer_rate_min) < (timer_rate_max >> 3)) {
0104             good_timer_count++;
0105             good_timer_sum += timer_rate_max;
0106             measured_times[i] = timer_rate_max;
0107             if (max < 0 || timer_rate_max > measured_times[max])
0108                 max = i;
0109             if (min < 0 || timer_rate_max < measured_times[min])
0110                 min = i;
0111         } else
0112             measured_times[i] = 0;
0113 
0114     }
0115 
0116     /*
0117      * Find the maximum & minimum - if they differ too much throw out the
0118      * one with the largest difference from the mean and try again...
0119      */
0120     while (good_timer_count > 1) {
0121         unsigned long estimate;
0122         unsigned long maxdiff;
0123 
0124         /* compute the estimate */
0125         estimate = (good_timer_sum/good_timer_count);
0126         maxdiff = estimate >> 3;
0127 
0128         /* if range is within 12% let's take it */
0129         if ((measured_times[max] - measured_times[min]) < maxdiff)
0130             return estimate;
0131 
0132         /* ok - drop the worse value and try again... */
0133         good_timer_sum = 0;
0134         good_timer_count = 0;
0135         if ((measured_times[max] - estimate) <
0136                 (estimate - measured_times[min])) {
0137             printk(KERN_NOTICE "calibrate_delay_direct() dropping "
0138                     "min bogoMips estimate %d = %lu\n",
0139                 min, measured_times[min]);
0140             measured_times[min] = 0;
0141             min = max;
0142         } else {
0143             printk(KERN_NOTICE "calibrate_delay_direct() dropping "
0144                     "max bogoMips estimate %d = %lu\n",
0145                 max, measured_times[max]);
0146             measured_times[max] = 0;
0147             max = min;
0148         }
0149 
0150         for (i = 0; i < MAX_DIRECT_CALIBRATION_RETRIES; i++) {
0151             if (measured_times[i] == 0)
0152                 continue;
0153             good_timer_count++;
0154             good_timer_sum += measured_times[i];
0155             if (measured_times[i] < measured_times[min])
0156                 min = i;
0157             if (measured_times[i] > measured_times[max])
0158                 max = i;
0159         }
0160 
0161     }
0162 
0163     printk(KERN_NOTICE "calibrate_delay_direct() failed to get a good "
0164            "estimate for loops_per_jiffy.\nProbably due to long platform "
0165         "interrupts. Consider using \"lpj=\" boot option.\n");
0166     return 0;
0167 }
0168 #else
0169 static unsigned long calibrate_delay_direct(void)
0170 {
0171     return 0;
0172 }
0173 #endif
0174 
0175 /*
0176  * This is the number of bits of precision for the loops_per_jiffy.  Each
0177  * time we refine our estimate after the first takes 1.5/HZ seconds, so try
0178  * to start with a good estimate.
0179  * For the boot cpu we can skip the delay calibration and assign it a value
0180  * calculated based on the timer frequency.
0181  * For the rest of the CPUs we cannot assume that the timer frequency is same as
0182  * the cpu frequency, hence do the calibration for those.
0183  */
0184 #define LPS_PREC 8
0185 
0186 static unsigned long calibrate_delay_converge(void)
0187 {
0188     /* First stage - slowly accelerate to find initial bounds */
0189     unsigned long lpj, lpj_base, ticks, loopadd, loopadd_base, chop_limit;
0190     int trials = 0, band = 0, trial_in_band = 0;
0191 
0192     lpj = (1<<12);
0193 
0194     /* wait for "start of" clock tick */
0195     ticks = jiffies;
0196     while (ticks == jiffies)
0197         ; /* nothing */
0198     /* Go .. */
0199     ticks = jiffies;
0200     do {
0201         if (++trial_in_band == (1<<band)) {
0202             ++band;
0203             trial_in_band = 0;
0204         }
0205         __delay(lpj * band);
0206         trials += band;
0207     } while (ticks == jiffies);
0208     /*
0209      * We overshot, so retreat to a clear underestimate. Then estimate
0210      * the largest likely undershoot. This defines our chop bounds.
0211      */
0212     trials -= band;
0213     loopadd_base = lpj * band;
0214     lpj_base = lpj * trials;
0215 
0216 recalibrate:
0217     lpj = lpj_base;
0218     loopadd = loopadd_base;
0219 
0220     /*
0221      * Do a binary approximation to get lpj set to
0222      * equal one clock (up to LPS_PREC bits)
0223      */
0224     chop_limit = lpj >> LPS_PREC;
0225     while (loopadd > chop_limit) {
0226         lpj += loopadd;
0227         ticks = jiffies;
0228         while (ticks == jiffies)
0229             ; /* nothing */
0230         ticks = jiffies;
0231         __delay(lpj);
0232         if (jiffies != ticks)   /* longer than 1 tick */
0233             lpj -= loopadd;
0234         loopadd >>= 1;
0235     }
0236     /*
0237      * If we incremented every single time possible, presume we've
0238      * massively underestimated initially, and retry with a higher
0239      * start, and larger range. (Only seen on x86_64, due to SMIs)
0240      */
0241     if (lpj + loopadd * 2 == lpj_base + loopadd_base * 2) {
0242         lpj_base = lpj;
0243         loopadd_base <<= 2;
0244         goto recalibrate;
0245     }
0246 
0247     return lpj;
0248 }
0249 
0250 static DEFINE_PER_CPU(unsigned long, cpu_loops_per_jiffy) = { 0 };
0251 
0252 /*
0253  * Check if cpu calibration delay is already known. For example,
0254  * some processors with multi-core sockets may have all cores
0255  * with the same calibration delay.
0256  *
0257  * Architectures should override this function if a faster calibration
0258  * method is available.
0259  */
0260 unsigned long __attribute__((weak)) calibrate_delay_is_known(void)
0261 {
0262     return 0;
0263 }
0264 
0265 /*
0266  * Indicate the cpu delay calibration is done. This can be used by
0267  * architectures to stop accepting delay timer registrations after this point.
0268  */
0269 
0270 void __attribute__((weak)) calibration_delay_done(void)
0271 {
0272 }
0273 
0274 void calibrate_delay(void)
0275 {
0276     unsigned long lpj;
0277     static bool printed;
0278     int this_cpu = smp_processor_id();
0279 
0280     if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
0281         lpj = per_cpu(cpu_loops_per_jiffy, this_cpu);
0282         if (!printed)
0283             pr_info("Calibrating delay loop (skipped) "
0284                 "already calibrated this CPU");
0285     } else if (preset_lpj) {
0286         lpj = preset_lpj;
0287         if (!printed)
0288             pr_info("Calibrating delay loop (skipped) "
0289                 "preset value.. ");
0290     } else if ((!printed) && lpj_fine) {
0291         lpj = lpj_fine;
0292         pr_info("Calibrating delay loop (skipped), "
0293             "value calculated using timer frequency.. ");
0294     } else if ((lpj = calibrate_delay_is_known())) {
0295         ;
0296     } else if ((lpj = calibrate_delay_direct()) != 0) {
0297         if (!printed)
0298             pr_info("Calibrating delay using timer "
0299                 "specific routine.. ");
0300     } else {
0301         if (!printed)
0302             pr_info("Calibrating delay loop... ");
0303         lpj = calibrate_delay_converge();
0304     }
0305     per_cpu(cpu_loops_per_jiffy, this_cpu) = lpj;
0306     if (!printed)
0307         pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
0308             lpj/(500000/HZ),
0309             (lpj/(5000/HZ)) % 100, lpj);
0310 
0311     loops_per_jiffy = lpj;
0312     printed = true;
0313 
0314     calibration_delay_done();
0315 }