Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  drivers/cpufreq/cpufreq_ondemand.c
0004  *
0005  *  Copyright (C)  2001 Russell King
0006  *            (C)  2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
0007  *                      Jun Nakajima <jun.nakajima@intel.com>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/cpu.h>
0013 #include <linux/percpu-defs.h>
0014 #include <linux/slab.h>
0015 #include <linux/tick.h>
0016 #include <linux/sched/cpufreq.h>
0017 
0018 #include "cpufreq_ondemand.h"
0019 
0020 /* On-demand governor macros */
0021 #define DEF_FREQUENCY_UP_THRESHOLD      (80)
0022 #define DEF_SAMPLING_DOWN_FACTOR        (1)
0023 #define MAX_SAMPLING_DOWN_FACTOR        (100000)
0024 #define MICRO_FREQUENCY_UP_THRESHOLD        (95)
0025 #define MICRO_FREQUENCY_MIN_SAMPLE_RATE     (10000)
0026 #define MIN_FREQUENCY_UP_THRESHOLD      (1)
0027 #define MAX_FREQUENCY_UP_THRESHOLD      (100)
0028 
0029 static struct od_ops od_ops;
0030 
0031 static unsigned int default_powersave_bias;
0032 
0033 /*
0034  * Not all CPUs want IO time to be accounted as busy; this depends on how
0035  * efficient idling at a higher frequency/voltage is.
0036  * Pavel Machek says this is not so for various generations of AMD and old
0037  * Intel systems.
0038  * Mike Chan (android.com) claims this is also not true for ARM.
0039  * Because of this, whitelist specific known (series) of CPUs by default, and
0040  * leave all others up to the user.
0041  */
0042 static int should_io_be_busy(void)
0043 {
0044 #if defined(CONFIG_X86)
0045     /*
0046      * For Intel, Core 2 (model 15) and later have an efficient idle.
0047      */
0048     if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
0049             boot_cpu_data.x86 == 6 &&
0050             boot_cpu_data.x86_model >= 15)
0051         return 1;
0052 #endif
0053     return 0;
0054 }
0055 
0056 /*
0057  * Find right freq to be set now with powersave_bias on.
0058  * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
0059  * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
0060  */
0061 static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
0062         unsigned int freq_next, unsigned int relation)
0063 {
0064     unsigned int freq_req, freq_reduc, freq_avg;
0065     unsigned int freq_hi, freq_lo;
0066     unsigned int index;
0067     unsigned int delay_hi_us;
0068     struct policy_dbs_info *policy_dbs = policy->governor_data;
0069     struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
0070     struct dbs_data *dbs_data = policy_dbs->dbs_data;
0071     struct od_dbs_tuners *od_tuners = dbs_data->tuners;
0072     struct cpufreq_frequency_table *freq_table = policy->freq_table;
0073 
0074     if (!freq_table) {
0075         dbs_info->freq_lo = 0;
0076         dbs_info->freq_lo_delay_us = 0;
0077         return freq_next;
0078     }
0079 
0080     index = cpufreq_frequency_table_target(policy, freq_next, relation);
0081     freq_req = freq_table[index].frequency;
0082     freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
0083     freq_avg = freq_req - freq_reduc;
0084 
0085     /* Find freq bounds for freq_avg in freq_table */
0086     index = cpufreq_table_find_index_h(policy, freq_avg,
0087                        relation & CPUFREQ_RELATION_E);
0088     freq_lo = freq_table[index].frequency;
0089     index = cpufreq_table_find_index_l(policy, freq_avg,
0090                        relation & CPUFREQ_RELATION_E);
0091     freq_hi = freq_table[index].frequency;
0092 
0093     /* Find out how long we have to be in hi and lo freqs */
0094     if (freq_hi == freq_lo) {
0095         dbs_info->freq_lo = 0;
0096         dbs_info->freq_lo_delay_us = 0;
0097         return freq_lo;
0098     }
0099     delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
0100     delay_hi_us += (freq_hi - freq_lo) / 2;
0101     delay_hi_us /= freq_hi - freq_lo;
0102     dbs_info->freq_hi_delay_us = delay_hi_us;
0103     dbs_info->freq_lo = freq_lo;
0104     dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
0105     return freq_hi;
0106 }
0107 
0108 static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
0109 {
0110     struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
0111 
0112     dbs_info->freq_lo = 0;
0113 }
0114 
0115 static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
0116 {
0117     struct policy_dbs_info *policy_dbs = policy->governor_data;
0118     struct dbs_data *dbs_data = policy_dbs->dbs_data;
0119     struct od_dbs_tuners *od_tuners = dbs_data->tuners;
0120 
0121     if (od_tuners->powersave_bias)
0122         freq = od_ops.powersave_bias_target(policy, freq,
0123                             CPUFREQ_RELATION_HE);
0124     else if (policy->cur == policy->max)
0125         return;
0126 
0127     __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
0128             CPUFREQ_RELATION_LE : CPUFREQ_RELATION_HE);
0129 }
0130 
0131 /*
0132  * Every sampling_rate, we check, if current idle time is less than 20%
0133  * (default), then we try to increase frequency. Else, we adjust the frequency
0134  * proportional to load.
0135  */
0136 static void od_update(struct cpufreq_policy *policy)
0137 {
0138     struct policy_dbs_info *policy_dbs = policy->governor_data;
0139     struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
0140     struct dbs_data *dbs_data = policy_dbs->dbs_data;
0141     struct od_dbs_tuners *od_tuners = dbs_data->tuners;
0142     unsigned int load = dbs_update(policy);
0143 
0144     dbs_info->freq_lo = 0;
0145 
0146     /* Check for frequency increase */
0147     if (load > dbs_data->up_threshold) {
0148         /* If switching to max speed, apply sampling_down_factor */
0149         if (policy->cur < policy->max)
0150             policy_dbs->rate_mult = dbs_data->sampling_down_factor;
0151         dbs_freq_increase(policy, policy->max);
0152     } else {
0153         /* Calculate the next frequency proportional to load */
0154         unsigned int freq_next, min_f, max_f;
0155 
0156         min_f = policy->cpuinfo.min_freq;
0157         max_f = policy->cpuinfo.max_freq;
0158         freq_next = min_f + load * (max_f - min_f) / 100;
0159 
0160         /* No longer fully busy, reset rate_mult */
0161         policy_dbs->rate_mult = 1;
0162 
0163         if (od_tuners->powersave_bias)
0164             freq_next = od_ops.powersave_bias_target(policy,
0165                                  freq_next,
0166                                  CPUFREQ_RELATION_LE);
0167 
0168         __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_CE);
0169     }
0170 }
0171 
0172 static unsigned int od_dbs_update(struct cpufreq_policy *policy)
0173 {
0174     struct policy_dbs_info *policy_dbs = policy->governor_data;
0175     struct dbs_data *dbs_data = policy_dbs->dbs_data;
0176     struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
0177     int sample_type = dbs_info->sample_type;
0178 
0179     /* Common NORMAL_SAMPLE setup */
0180     dbs_info->sample_type = OD_NORMAL_SAMPLE;
0181     /*
0182      * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
0183      * it then.
0184      */
0185     if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
0186         __cpufreq_driver_target(policy, dbs_info->freq_lo,
0187                     CPUFREQ_RELATION_HE);
0188         return dbs_info->freq_lo_delay_us;
0189     }
0190 
0191     od_update(policy);
0192 
0193     if (dbs_info->freq_lo) {
0194         /* Setup SUB_SAMPLE */
0195         dbs_info->sample_type = OD_SUB_SAMPLE;
0196         return dbs_info->freq_hi_delay_us;
0197     }
0198 
0199     return dbs_data->sampling_rate * policy_dbs->rate_mult;
0200 }
0201 
0202 /************************** sysfs interface ************************/
0203 static struct dbs_governor od_dbs_gov;
0204 
0205 static ssize_t io_is_busy_store(struct gov_attr_set *attr_set, const char *buf,
0206                 size_t count)
0207 {
0208     struct dbs_data *dbs_data = to_dbs_data(attr_set);
0209     unsigned int input;
0210     int ret;
0211 
0212     ret = sscanf(buf, "%u", &input);
0213     if (ret != 1)
0214         return -EINVAL;
0215     dbs_data->io_is_busy = !!input;
0216 
0217     /* we need to re-evaluate prev_cpu_idle */
0218     gov_update_cpu_data(dbs_data);
0219 
0220     return count;
0221 }
0222 
0223 static ssize_t up_threshold_store(struct gov_attr_set *attr_set,
0224                   const char *buf, size_t count)
0225 {
0226     struct dbs_data *dbs_data = to_dbs_data(attr_set);
0227     unsigned int input;
0228     int ret;
0229     ret = sscanf(buf, "%u", &input);
0230 
0231     if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
0232             input < MIN_FREQUENCY_UP_THRESHOLD) {
0233         return -EINVAL;
0234     }
0235 
0236     dbs_data->up_threshold = input;
0237     return count;
0238 }
0239 
0240 static ssize_t sampling_down_factor_store(struct gov_attr_set *attr_set,
0241                       const char *buf, size_t count)
0242 {
0243     struct dbs_data *dbs_data = to_dbs_data(attr_set);
0244     struct policy_dbs_info *policy_dbs;
0245     unsigned int input;
0246     int ret;
0247     ret = sscanf(buf, "%u", &input);
0248 
0249     if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
0250         return -EINVAL;
0251 
0252     dbs_data->sampling_down_factor = input;
0253 
0254     /* Reset down sampling multiplier in case it was active */
0255     list_for_each_entry(policy_dbs, &attr_set->policy_list, list) {
0256         /*
0257          * Doing this without locking might lead to using different
0258          * rate_mult values in od_update() and od_dbs_update().
0259          */
0260         mutex_lock(&policy_dbs->update_mutex);
0261         policy_dbs->rate_mult = 1;
0262         mutex_unlock(&policy_dbs->update_mutex);
0263     }
0264 
0265     return count;
0266 }
0267 
0268 static ssize_t ignore_nice_load_store(struct gov_attr_set *attr_set,
0269                       const char *buf, size_t count)
0270 {
0271     struct dbs_data *dbs_data = to_dbs_data(attr_set);
0272     unsigned int input;
0273     int ret;
0274 
0275     ret = sscanf(buf, "%u", &input);
0276     if (ret != 1)
0277         return -EINVAL;
0278 
0279     if (input > 1)
0280         input = 1;
0281 
0282     if (input == dbs_data->ignore_nice_load) { /* nothing to do */
0283         return count;
0284     }
0285     dbs_data->ignore_nice_load = input;
0286 
0287     /* we need to re-evaluate prev_cpu_idle */
0288     gov_update_cpu_data(dbs_data);
0289 
0290     return count;
0291 }
0292 
0293 static ssize_t powersave_bias_store(struct gov_attr_set *attr_set,
0294                     const char *buf, size_t count)
0295 {
0296     struct dbs_data *dbs_data = to_dbs_data(attr_set);
0297     struct od_dbs_tuners *od_tuners = dbs_data->tuners;
0298     struct policy_dbs_info *policy_dbs;
0299     unsigned int input;
0300     int ret;
0301     ret = sscanf(buf, "%u", &input);
0302 
0303     if (ret != 1)
0304         return -EINVAL;
0305 
0306     if (input > 1000)
0307         input = 1000;
0308 
0309     od_tuners->powersave_bias = input;
0310 
0311     list_for_each_entry(policy_dbs, &attr_set->policy_list, list)
0312         ondemand_powersave_bias_init(policy_dbs->policy);
0313 
0314     return count;
0315 }
0316 
0317 gov_show_one_common(sampling_rate);
0318 gov_show_one_common(up_threshold);
0319 gov_show_one_common(sampling_down_factor);
0320 gov_show_one_common(ignore_nice_load);
0321 gov_show_one_common(io_is_busy);
0322 gov_show_one(od, powersave_bias);
0323 
0324 gov_attr_rw(sampling_rate);
0325 gov_attr_rw(io_is_busy);
0326 gov_attr_rw(up_threshold);
0327 gov_attr_rw(sampling_down_factor);
0328 gov_attr_rw(ignore_nice_load);
0329 gov_attr_rw(powersave_bias);
0330 
0331 static struct attribute *od_attrs[] = {
0332     &sampling_rate.attr,
0333     &up_threshold.attr,
0334     &sampling_down_factor.attr,
0335     &ignore_nice_load.attr,
0336     &powersave_bias.attr,
0337     &io_is_busy.attr,
0338     NULL
0339 };
0340 ATTRIBUTE_GROUPS(od);
0341 
0342 /************************** sysfs end ************************/
0343 
0344 static struct policy_dbs_info *od_alloc(void)
0345 {
0346     struct od_policy_dbs_info *dbs_info;
0347 
0348     dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
0349     return dbs_info ? &dbs_info->policy_dbs : NULL;
0350 }
0351 
0352 static void od_free(struct policy_dbs_info *policy_dbs)
0353 {
0354     kfree(to_dbs_info(policy_dbs));
0355 }
0356 
0357 static int od_init(struct dbs_data *dbs_data)
0358 {
0359     struct od_dbs_tuners *tuners;
0360     u64 idle_time;
0361     int cpu;
0362 
0363     tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
0364     if (!tuners)
0365         return -ENOMEM;
0366 
0367     cpu = get_cpu();
0368     idle_time = get_cpu_idle_time_us(cpu, NULL);
0369     put_cpu();
0370     if (idle_time != -1ULL) {
0371         /* Idle micro accounting is supported. Use finer thresholds */
0372         dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
0373     } else {
0374         dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
0375     }
0376 
0377     dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
0378     dbs_data->ignore_nice_load = 0;
0379     tuners->powersave_bias = default_powersave_bias;
0380     dbs_data->io_is_busy = should_io_be_busy();
0381 
0382     dbs_data->tuners = tuners;
0383     return 0;
0384 }
0385 
0386 static void od_exit(struct dbs_data *dbs_data)
0387 {
0388     kfree(dbs_data->tuners);
0389 }
0390 
0391 static void od_start(struct cpufreq_policy *policy)
0392 {
0393     struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
0394 
0395     dbs_info->sample_type = OD_NORMAL_SAMPLE;
0396     ondemand_powersave_bias_init(policy);
0397 }
0398 
0399 static struct od_ops od_ops = {
0400     .powersave_bias_target = generic_powersave_bias_target,
0401 };
0402 
0403 static struct dbs_governor od_dbs_gov = {
0404     .gov = CPUFREQ_DBS_GOVERNOR_INITIALIZER("ondemand"),
0405     .kobj_type = { .default_groups = od_groups },
0406     .gov_dbs_update = od_dbs_update,
0407     .alloc = od_alloc,
0408     .free = od_free,
0409     .init = od_init,
0410     .exit = od_exit,
0411     .start = od_start,
0412 };
0413 
0414 #define CPU_FREQ_GOV_ONDEMAND   (od_dbs_gov.gov)
0415 
0416 static void od_set_powersave_bias(unsigned int powersave_bias)
0417 {
0418     unsigned int cpu;
0419     cpumask_var_t done;
0420 
0421     if (!alloc_cpumask_var(&done, GFP_KERNEL))
0422         return;
0423 
0424     default_powersave_bias = powersave_bias;
0425     cpumask_clear(done);
0426 
0427     cpus_read_lock();
0428     for_each_online_cpu(cpu) {
0429         struct cpufreq_policy *policy;
0430         struct policy_dbs_info *policy_dbs;
0431         struct dbs_data *dbs_data;
0432         struct od_dbs_tuners *od_tuners;
0433 
0434         if (cpumask_test_cpu(cpu, done))
0435             continue;
0436 
0437         policy = cpufreq_cpu_get_raw(cpu);
0438         if (!policy || policy->governor != &CPU_FREQ_GOV_ONDEMAND)
0439             continue;
0440 
0441         policy_dbs = policy->governor_data;
0442         if (!policy_dbs)
0443             continue;
0444 
0445         cpumask_or(done, done, policy->cpus);
0446 
0447         dbs_data = policy_dbs->dbs_data;
0448         od_tuners = dbs_data->tuners;
0449         od_tuners->powersave_bias = default_powersave_bias;
0450     }
0451     cpus_read_unlock();
0452 
0453     free_cpumask_var(done);
0454 }
0455 
0456 void od_register_powersave_bias_handler(unsigned int (*f)
0457         (struct cpufreq_policy *, unsigned int, unsigned int),
0458         unsigned int powersave_bias)
0459 {
0460     od_ops.powersave_bias_target = f;
0461     od_set_powersave_bias(powersave_bias);
0462 }
0463 EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
0464 
0465 void od_unregister_powersave_bias_handler(void)
0466 {
0467     od_ops.powersave_bias_target = generic_powersave_bias_target;
0468     od_set_powersave_bias(0);
0469 }
0470 EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
0471 
0472 MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
0473 MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
0474 MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
0475     "Low Latency Frequency Transition capable processors");
0476 MODULE_LICENSE("GPL");
0477 
0478 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
0479 struct cpufreq_governor *cpufreq_default_governor(void)
0480 {
0481     return &CPU_FREQ_GOV_ONDEMAND;
0482 }
0483 #endif
0484 
0485 cpufreq_governor_init(CPU_FREQ_GOV_ONDEMAND);
0486 cpufreq_governor_exit(CPU_FREQ_GOV_ONDEMAND);