Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/cpufreq/cpufreq_ondemand.c |
| 3 | * |
| 4 | * Copyright (C) 2001 Russell King |
| 5 | * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. |
| 6 | * Jun Nakajima <jun.nakajima@intel.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/cpufreq.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/kernel_stat.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 19 | #include <linux/kobject.h> |
| 20 | #include <linux/module.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 21 | #include <linux/mutex.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 22 | #include <linux/percpu-defs.h> |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 23 | #include <linux/slab.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 24 | #include <linux/sysfs.h> |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 25 | #include <linux/tick.h> |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 26 | #include <linux/types.h> |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 27 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 29 | #include "cpufreq_governor.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 31 | /* On-demand governor macros */ |
venkatesh.pallipadi@intel.com | e9d95bf | 2008-08-04 11:59:10 -0700 | [diff] [blame] | 32 | #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #define DEF_FREQUENCY_UP_THRESHOLD (80) |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 34 | #define DEF_SAMPLING_DOWN_FACTOR (1) |
| 35 | #define MAX_SAMPLING_DOWN_FACTOR (100000) |
venkatesh.pallipadi@intel.com | 8080091 | 2008-08-04 11:59:12 -0700 | [diff] [blame] | 36 | #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3) |
| 37 | #define MICRO_FREQUENCY_UP_THRESHOLD (95) |
Thomas Renninger | cef9615 | 2009-04-22 13:48:29 +0200 | [diff] [blame] | 38 | #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000) |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 39 | #define MIN_FREQUENCY_UP_THRESHOLD (11) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define MAX_FREQUENCY_UP_THRESHOLD (100) |
| 41 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 42 | static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info); |
Thomas Renninger | 112124a | 2009-02-04 11:55:12 +0100 | [diff] [blame] | 43 | |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 44 | static struct od_ops od_ops; |
| 45 | |
Fabio Baltieri | 3e33ee9 | 2012-11-26 18:10:12 +0000 | [diff] [blame] | 46 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
| 47 | static struct cpufreq_governor cpufreq_gov_ondemand; |
| 48 | #endif |
| 49 | |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame^] | 50 | static unsigned int default_powersave_bias; |
| 51 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 52 | static void ondemand_powersave_bias_init_cpu(int cpu) |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 53 | { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 54 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 55 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 56 | dbs_info->freq_table = cpufreq_frequency_get_table(cpu); |
| 57 | dbs_info->freq_lo = 0; |
| 58 | } |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 59 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 60 | /* |
| 61 | * Not all CPUs want IO time to be accounted as busy; this depends on how |
| 62 | * efficient idling at a higher frequency/voltage is. |
| 63 | * Pavel Machek says this is not so for various generations of AMD and old |
| 64 | * Intel systems. |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 65 | * Mike Chan (android.com) claims this is also not true for ARM. |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 66 | * Because of this, whitelist specific known (series) of CPUs by default, and |
| 67 | * leave all others up to the user. |
| 68 | */ |
| 69 | static int should_io_be_busy(void) |
| 70 | { |
| 71 | #if defined(CONFIG_X86) |
| 72 | /* |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 73 | * For Intel, Core 2 (model 15) and later have an efficient idle. |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 74 | */ |
| 75 | if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && |
| 76 | boot_cpu_data.x86 == 6 && |
| 77 | boot_cpu_data.x86_model >= 15) |
| 78 | return 1; |
| 79 | #endif |
| 80 | return 0; |
Arjan van de Ven | 6b8fcd9 | 2010-05-09 08:26:06 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 83 | /* |
| 84 | * Find right freq to be set now with powersave_bias on. |
| 85 | * Returns the freq_hi to be used right now and will set freq_hi_jiffies, |
| 86 | * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs. |
| 87 | */ |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 88 | static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 89 | unsigned int freq_next, unsigned int relation) |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 90 | { |
| 91 | unsigned int freq_req, freq_reduc, freq_avg; |
| 92 | unsigned int freq_hi, freq_lo; |
| 93 | unsigned int index = 0; |
| 94 | unsigned int jiffies_total, jiffies_hi, jiffies_lo; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 95 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 96 | policy->cpu); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 97 | struct dbs_data *dbs_data = policy->governor_data; |
| 98 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 99 | |
| 100 | if (!dbs_info->freq_table) { |
| 101 | dbs_info->freq_lo = 0; |
| 102 | dbs_info->freq_lo_jiffies = 0; |
| 103 | return freq_next; |
| 104 | } |
| 105 | |
| 106 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next, |
| 107 | relation, &index); |
| 108 | freq_req = dbs_info->freq_table[index].frequency; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 109 | freq_reduc = freq_req * od_tuners->powersave_bias / 1000; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 110 | freq_avg = freq_req - freq_reduc; |
| 111 | |
| 112 | /* Find freq bounds for freq_avg in freq_table */ |
| 113 | index = 0; |
| 114 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, |
| 115 | CPUFREQ_RELATION_H, &index); |
| 116 | freq_lo = dbs_info->freq_table[index].frequency; |
| 117 | index = 0; |
| 118 | cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg, |
| 119 | CPUFREQ_RELATION_L, &index); |
| 120 | freq_hi = dbs_info->freq_table[index].frequency; |
| 121 | |
| 122 | /* Find out how long we have to be in hi and lo freqs */ |
| 123 | if (freq_hi == freq_lo) { |
| 124 | dbs_info->freq_lo = 0; |
| 125 | dbs_info->freq_lo_jiffies = 0; |
| 126 | return freq_lo; |
| 127 | } |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 128 | jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 129 | jiffies_hi = (freq_avg - freq_lo) * jiffies_total; |
| 130 | jiffies_hi += ((freq_hi - freq_lo) / 2); |
| 131 | jiffies_hi /= (freq_hi - freq_lo); |
| 132 | jiffies_lo = jiffies_total - jiffies_hi; |
| 133 | dbs_info->freq_lo = freq_lo; |
| 134 | dbs_info->freq_lo_jiffies = jiffies_lo; |
| 135 | dbs_info->freq_hi_jiffies = jiffies_hi; |
| 136 | return freq_hi; |
| 137 | } |
| 138 | |
| 139 | static void ondemand_powersave_bias_init(void) |
| 140 | { |
| 141 | int i; |
| 142 | for_each_online_cpu(i) { |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 143 | ondemand_powersave_bias_init_cpu(i); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 147 | static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq) |
| 148 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 149 | struct dbs_data *dbs_data = p->governor_data; |
| 150 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
| 151 | |
| 152 | if (od_tuners->powersave_bias) |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 153 | freq = od_ops.powersave_bias_target(p, freq, |
| 154 | CPUFREQ_RELATION_H); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 155 | else if (p->cur == p->max) |
| 156 | return; |
| 157 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 158 | __cpufreq_driver_target(p, freq, od_tuners->powersave_bias ? |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 159 | CPUFREQ_RELATION_L : CPUFREQ_RELATION_H); |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Every sampling_rate, we check, if current idle time is less than 20% |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 164 | * (default), then we try to increase frequency. Every sampling_rate, we look |
| 165 | * for the lowest frequency which can sustain the load while keeping idle time |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 166 | * over 30%. If such a frequency exist, we try to decrease to this frequency. |
| 167 | * |
| 168 | * Any frequency increase takes it to the maximum frequency. Frequency reduction |
| 169 | * happens at minimum steps of 5% (default) of current frequency |
| 170 | */ |
| 171 | static void od_check_cpu(int cpu, unsigned int load_freq) |
| 172 | { |
| 173 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
| 174 | struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 175 | struct dbs_data *dbs_data = policy->governor_data; |
| 176 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 177 | |
| 178 | dbs_info->freq_lo = 0; |
| 179 | |
| 180 | /* Check for frequency increase */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 181 | if (load_freq > od_tuners->up_threshold * policy->cur) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 182 | /* If switching to max speed, apply sampling_down_factor */ |
| 183 | if (policy->cur < policy->max) |
| 184 | dbs_info->rate_mult = |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 185 | od_tuners->sampling_down_factor; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 186 | dbs_freq_increase(policy, policy->max); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | /* Check for frequency decrease */ |
| 191 | /* if we cannot reduce the frequency anymore, break out early */ |
| 192 | if (policy->cur == policy->min) |
| 193 | return; |
| 194 | |
| 195 | /* |
| 196 | * The optimal frequency is the frequency that is the lowest that can |
| 197 | * support the current CPU usage without triggering the up policy. To be |
| 198 | * safe, we focus 10 points under the threshold. |
| 199 | */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 200 | if (load_freq < od_tuners->adj_up_threshold |
| 201 | * policy->cur) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 202 | unsigned int freq_next; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 203 | freq_next = load_freq / od_tuners->adj_up_threshold; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 204 | |
| 205 | /* No longer fully busy, reset rate_mult */ |
| 206 | dbs_info->rate_mult = 1; |
| 207 | |
| 208 | if (freq_next < policy->min) |
| 209 | freq_next = policy->min; |
| 210 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 211 | if (!od_tuners->powersave_bias) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 212 | __cpufreq_driver_target(policy, freq_next, |
| 213 | CPUFREQ_RELATION_L); |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 214 | return; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 215 | } |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 216 | |
| 217 | freq_next = od_ops.powersave_bias_target(policy, freq_next, |
| 218 | CPUFREQ_RELATION_L); |
| 219 | __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 223 | static void od_dbs_timer(struct work_struct *work) |
| 224 | { |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 225 | struct od_cpu_dbs_info_s *dbs_info = |
| 226 | container_of(work, struct od_cpu_dbs_info_s, cdbs.work.work); |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 227 | unsigned int cpu = dbs_info->cdbs.cur_policy->cpu; |
| 228 | struct od_cpu_dbs_info_s *core_dbs_info = &per_cpu(od_cpu_dbs_info, |
| 229 | cpu); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 230 | struct dbs_data *dbs_data = dbs_info->cdbs.cur_policy->governor_data; |
| 231 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 232 | int delay = 0, sample_type = core_dbs_info->sample_type; |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 233 | bool modify_all = true; |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 234 | |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 235 | mutex_lock(&core_dbs_info->cdbs.timer_mutex); |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 236 | if (!need_load_eval(&core_dbs_info->cdbs, od_tuners->sampling_rate)) { |
| 237 | modify_all = false; |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 238 | goto max_delay; |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 239 | } |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 240 | |
| 241 | /* Common NORMAL_SAMPLE setup */ |
| 242 | core_dbs_info->sample_type = OD_NORMAL_SAMPLE; |
| 243 | if (sample_type == OD_SUB_SAMPLE) { |
| 244 | delay = core_dbs_info->freq_lo_jiffies; |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 245 | __cpufreq_driver_target(core_dbs_info->cdbs.cur_policy, |
| 246 | core_dbs_info->freq_lo, CPUFREQ_RELATION_H); |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 247 | } else { |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 248 | dbs_check_cpu(dbs_data, cpu); |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 249 | if (core_dbs_info->freq_lo) { |
| 250 | /* Setup timer for SUB_SAMPLE */ |
| 251 | core_dbs_info->sample_type = OD_SUB_SAMPLE; |
| 252 | delay = core_dbs_info->freq_hi_jiffies; |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 253 | } |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 254 | } |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 255 | |
Viresh Kumar | 9d44592 | 2013-02-27 11:06:36 +0530 | [diff] [blame] | 256 | max_delay: |
| 257 | if (!delay) |
| 258 | delay = delay_for_sampling_rate(od_tuners->sampling_rate |
| 259 | * core_dbs_info->rate_mult); |
| 260 | |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 261 | gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, delay, modify_all); |
Viresh Kumar | 4447266 | 2013-01-31 17:28:02 +0000 | [diff] [blame] | 262 | mutex_unlock(&core_dbs_info->cdbs.timer_mutex); |
Fabio Baltieri | da53d61 | 2012-12-27 14:55:40 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /************************** sysfs interface ************************/ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 266 | static struct common_dbs_data od_dbs_cdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 268 | /** |
| 269 | * update_sampling_rate - update sampling rate effective immediately if needed. |
| 270 | * @new_rate: new sampling rate |
| 271 | * |
Stratos Karafotis | 06eb09d | 2013-02-08 17:24:18 +0000 | [diff] [blame] | 272 | * If new rate is smaller than the old, simply updating |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 273 | * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the |
| 274 | * original sampling_rate was 1 second and the requested new sampling rate is 10 |
| 275 | * ms because the user needs immediate reaction from ondemand governor, but not |
| 276 | * sure if higher frequency will be required or not, then, the governor may |
| 277 | * change the sampling rate too late; up to 1 second later. Thus, if we are |
| 278 | * reducing the sampling rate, we need to make the new value effective |
| 279 | * immediately. |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 280 | */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 281 | static void update_sampling_rate(struct dbs_data *dbs_data, |
| 282 | unsigned int new_rate) |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 283 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 284 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 285 | int cpu; |
| 286 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 287 | od_tuners->sampling_rate = new_rate = max(new_rate, |
| 288 | dbs_data->min_sampling_rate); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 289 | |
| 290 | for_each_online_cpu(cpu) { |
| 291 | struct cpufreq_policy *policy; |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 292 | struct od_cpu_dbs_info_s *dbs_info; |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 293 | unsigned long next_sampling, appointed_at; |
| 294 | |
| 295 | policy = cpufreq_cpu_get(cpu); |
| 296 | if (!policy) |
| 297 | continue; |
Fabio Baltieri | 3e33ee9 | 2012-11-26 18:10:12 +0000 | [diff] [blame] | 298 | if (policy->governor != &cpufreq_gov_ondemand) { |
| 299 | cpufreq_cpu_put(policy); |
| 300 | continue; |
| 301 | } |
Fabio Baltieri | 8ee2ec5 | 2012-12-27 14:55:42 +0000 | [diff] [blame] | 302 | dbs_info = &per_cpu(od_cpu_dbs_info, cpu); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 303 | cpufreq_cpu_put(policy); |
| 304 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 305 | mutex_lock(&dbs_info->cdbs.timer_mutex); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 306 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 307 | if (!delayed_work_pending(&dbs_info->cdbs.work)) { |
| 308 | mutex_unlock(&dbs_info->cdbs.timer_mutex); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 309 | continue; |
| 310 | } |
| 311 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 312 | next_sampling = jiffies + usecs_to_jiffies(new_rate); |
| 313 | appointed_at = dbs_info->cdbs.work.timer.expires; |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 314 | |
| 315 | if (time_before(next_sampling, appointed_at)) { |
| 316 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 317 | mutex_unlock(&dbs_info->cdbs.timer_mutex); |
| 318 | cancel_delayed_work_sync(&dbs_info->cdbs.work); |
| 319 | mutex_lock(&dbs_info->cdbs.timer_mutex); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 320 | |
Viresh Kumar | 031299b | 2013-02-27 12:24:03 +0530 | [diff] [blame] | 321 | gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, |
| 322 | usecs_to_jiffies(new_rate), true); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 323 | |
| 324 | } |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 325 | mutex_unlock(&dbs_info->cdbs.timer_mutex); |
MyungJoo Ham | fd0ef7a | 2012-02-29 17:54:41 +0900 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 329 | static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf, |
| 330 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
| 332 | unsigned int input; |
| 333 | int ret; |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 334 | ret = sscanf(buf, "%u", &input); |
venkatesh.pallipadi@intel.com | 5a75c82 | 2009-07-02 17:08:32 -0700 | [diff] [blame] | 335 | if (ret != 1) |
| 336 | return -EINVAL; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 337 | |
| 338 | update_sampling_rate(dbs_data, input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return count; |
| 340 | } |
| 341 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 342 | static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf, |
| 343 | size_t count) |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 344 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 345 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 346 | unsigned int input; |
| 347 | int ret; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 348 | unsigned int j; |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 349 | |
| 350 | ret = sscanf(buf, "%u", &input); |
| 351 | if (ret != 1) |
| 352 | return -EINVAL; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 353 | od_tuners->io_is_busy = !!input; |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 354 | |
| 355 | /* we need to re-evaluate prev_cpu_idle */ |
| 356 | for_each_online_cpu(j) { |
| 357 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
| 358 | j); |
| 359 | dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j, |
| 360 | &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy); |
| 361 | } |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 362 | return count; |
| 363 | } |
| 364 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 365 | static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf, |
| 366 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 368 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | unsigned int input; |
| 370 | int ret; |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 371 | ret = sscanf(buf, "%u", &input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 373 | if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD || |
Dave Jones | c29f140 | 2005-05-31 19:03:50 -0700 | [diff] [blame] | 374 | input < MIN_FREQUENCY_UP_THRESHOLD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | return -EINVAL; |
| 376 | } |
Stratos Karafotis | 4bd4e42 | 2013-02-06 13:34:00 +0100 | [diff] [blame] | 377 | /* Calculate the new adj_up_threshold */ |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 378 | od_tuners->adj_up_threshold += input; |
| 379 | od_tuners->adj_up_threshold -= od_tuners->up_threshold; |
Stratos Karafotis | 4bd4e42 | 2013-02-06 13:34:00 +0100 | [diff] [blame] | 380 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 381 | od_tuners->up_threshold = input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | return count; |
| 383 | } |
| 384 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 385 | static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data, |
| 386 | const char *buf, size_t count) |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 387 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 388 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 389 | unsigned int input, j; |
| 390 | int ret; |
| 391 | ret = sscanf(buf, "%u", &input); |
| 392 | |
| 393 | if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1) |
| 394 | return -EINVAL; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 395 | od_tuners->sampling_down_factor = input; |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 396 | |
| 397 | /* Reset down sampling multiplier in case it was active */ |
| 398 | for_each_online_cpu(j) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 399 | struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, |
| 400 | j); |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 401 | dbs_info->rate_mult = 1; |
| 402 | } |
David C Niemi | 3f78a9f | 2010-10-06 16:54:24 -0400 | [diff] [blame] | 403 | return count; |
| 404 | } |
| 405 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 406 | static ssize_t store_ignore_nice(struct dbs_data *dbs_data, const char *buf, |
| 407 | size_t count) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 408 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 409 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 410 | unsigned int input; |
| 411 | int ret; |
| 412 | |
| 413 | unsigned int j; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 414 | |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 415 | ret = sscanf(buf, "%u", &input); |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 416 | if (ret != 1) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 417 | return -EINVAL; |
| 418 | |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 419 | if (input > 1) |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 420 | input = 1; |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 421 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 422 | if (input == od_tuners->ignore_nice) { /* nothing to do */ |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 423 | return count; |
| 424 | } |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 425 | od_tuners->ignore_nice = input; |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 426 | |
Venkatesh Pallipadi | ccb2fe2 | 2006-06-28 13:49:52 -0700 | [diff] [blame] | 427 | /* we need to re-evaluate prev_cpu_idle */ |
Dave Jones | dac1c1a | 2005-05-31 19:03:49 -0700 | [diff] [blame] | 428 | for_each_online_cpu(j) { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 429 | struct od_cpu_dbs_info_s *dbs_info; |
Tejun Heo | 245b2e7 | 2009-06-24 15:13:48 +0900 | [diff] [blame] | 430 | dbs_info = &per_cpu(od_cpu_dbs_info, j); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 431 | dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j, |
Stratos Karafotis | 9366d84 | 2013-02-28 16:57:32 +0000 | [diff] [blame] | 432 | &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 433 | if (od_tuners->ignore_nice) |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 434 | dbs_info->cdbs.prev_cpu_nice = |
| 435 | kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
Venkatesh Pallipadi | 1ca3abd | 2009-01-23 09:25:02 -0500 | [diff] [blame] | 436 | |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 437 | } |
Dave Jones | 3d5ee9e | 2005-05-31 19:03:47 -0700 | [diff] [blame] | 438 | return count; |
| 439 | } |
| 440 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 441 | static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf, |
| 442 | size_t count) |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 443 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 444 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 445 | unsigned int input; |
| 446 | int ret; |
| 447 | ret = sscanf(buf, "%u", &input); |
| 448 | |
| 449 | if (ret != 1) |
| 450 | return -EINVAL; |
| 451 | |
| 452 | if (input > 1000) |
| 453 | input = 1000; |
| 454 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 455 | od_tuners->powersave_bias = input; |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 456 | ondemand_powersave_bias_init(); |
Alexey Starikovskiy | 05ca035 | 2006-07-31 22:28:12 +0400 | [diff] [blame] | 457 | return count; |
| 458 | } |
| 459 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 460 | show_store_one(od, sampling_rate); |
| 461 | show_store_one(od, io_is_busy); |
| 462 | show_store_one(od, up_threshold); |
| 463 | show_store_one(od, sampling_down_factor); |
| 464 | show_store_one(od, ignore_nice); |
| 465 | show_store_one(od, powersave_bias); |
| 466 | declare_show_sampling_rate_min(od); |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 467 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 468 | gov_sys_pol_attr_rw(sampling_rate); |
| 469 | gov_sys_pol_attr_rw(io_is_busy); |
| 470 | gov_sys_pol_attr_rw(up_threshold); |
| 471 | gov_sys_pol_attr_rw(sampling_down_factor); |
| 472 | gov_sys_pol_attr_rw(ignore_nice); |
| 473 | gov_sys_pol_attr_rw(powersave_bias); |
| 474 | gov_sys_pol_attr_ro(sampling_rate_min); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 476 | static struct attribute *dbs_attributes_gov_sys[] = { |
| 477 | &sampling_rate_min_gov_sys.attr, |
| 478 | &sampling_rate_gov_sys.attr, |
| 479 | &up_threshold_gov_sys.attr, |
| 480 | &sampling_down_factor_gov_sys.attr, |
| 481 | &ignore_nice_gov_sys.attr, |
| 482 | &powersave_bias_gov_sys.attr, |
| 483 | &io_is_busy_gov_sys.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | NULL |
| 485 | }; |
| 486 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 487 | static struct attribute_group od_attr_group_gov_sys = { |
| 488 | .attrs = dbs_attributes_gov_sys, |
| 489 | .name = "ondemand", |
| 490 | }; |
| 491 | |
| 492 | static struct attribute *dbs_attributes_gov_pol[] = { |
| 493 | &sampling_rate_min_gov_pol.attr, |
| 494 | &sampling_rate_gov_pol.attr, |
| 495 | &up_threshold_gov_pol.attr, |
| 496 | &sampling_down_factor_gov_pol.attr, |
| 497 | &ignore_nice_gov_pol.attr, |
| 498 | &powersave_bias_gov_pol.attr, |
| 499 | &io_is_busy_gov_pol.attr, |
| 500 | NULL |
| 501 | }; |
| 502 | |
| 503 | static struct attribute_group od_attr_group_gov_pol = { |
| 504 | .attrs = dbs_attributes_gov_pol, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | .name = "ondemand", |
| 506 | }; |
| 507 | |
| 508 | /************************** sysfs end ************************/ |
| 509 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 510 | static int od_init(struct dbs_data *dbs_data) |
| 511 | { |
| 512 | struct od_dbs_tuners *tuners; |
| 513 | u64 idle_time; |
| 514 | int cpu; |
| 515 | |
| 516 | tuners = kzalloc(sizeof(struct od_dbs_tuners), GFP_KERNEL); |
| 517 | if (!tuners) { |
| 518 | pr_err("%s: kzalloc failed\n", __func__); |
| 519 | return -ENOMEM; |
| 520 | } |
| 521 | |
| 522 | cpu = get_cpu(); |
| 523 | idle_time = get_cpu_idle_time_us(cpu, NULL); |
| 524 | put_cpu(); |
| 525 | if (idle_time != -1ULL) { |
| 526 | /* Idle micro accounting is supported. Use finer thresholds */ |
| 527 | tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD; |
| 528 | tuners->adj_up_threshold = MICRO_FREQUENCY_UP_THRESHOLD - |
| 529 | MICRO_FREQUENCY_DOWN_DIFFERENTIAL; |
| 530 | /* |
| 531 | * In nohz/micro accounting case we set the minimum frequency |
| 532 | * not depending on HZ, but fixed (very low). The deferred |
| 533 | * timer might skip some samples if idle/sleeping as needed. |
| 534 | */ |
| 535 | dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE; |
| 536 | } else { |
| 537 | tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD; |
| 538 | tuners->adj_up_threshold = DEF_FREQUENCY_UP_THRESHOLD - |
| 539 | DEF_FREQUENCY_DOWN_DIFFERENTIAL; |
| 540 | |
| 541 | /* For correct statistics, we need 10 ticks for each measure */ |
| 542 | dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO * |
| 543 | jiffies_to_usecs(10); |
| 544 | } |
| 545 | |
| 546 | tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR; |
| 547 | tuners->ignore_nice = 0; |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame^] | 548 | tuners->powersave_bias = default_powersave_bias; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 549 | tuners->io_is_busy = should_io_be_busy(); |
| 550 | |
| 551 | dbs_data->tuners = tuners; |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 552 | mutex_init(&dbs_data->mutex); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static void od_exit(struct dbs_data *dbs_data) |
| 557 | { |
| 558 | kfree(dbs_data->tuners); |
| 559 | } |
| 560 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 561 | define_get_cpu_dbs_routines(od_cpu_dbs_info); |
Mike Chan | 00e299f | 2010-01-26 17:06:47 -0800 | [diff] [blame] | 562 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 563 | static struct od_ops od_ops = { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 564 | .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu, |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 565 | .powersave_bias_target = generic_powersave_bias_target, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 566 | .freq_increase = dbs_freq_increase, |
| 567 | }; |
| 568 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 569 | static struct common_dbs_data od_dbs_cdata = { |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 570 | .governor = GOV_ONDEMAND, |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 571 | .attr_group_gov_sys = &od_attr_group_gov_sys, |
| 572 | .attr_group_gov_pol = &od_attr_group_gov_pol, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 573 | .get_cpu_cdbs = get_cpu_cdbs, |
| 574 | .get_cpu_dbs_info_s = get_cpu_dbs_info_s, |
| 575 | .gov_dbs_timer = od_dbs_timer, |
| 576 | .gov_check_cpu = od_check_cpu, |
| 577 | .gov_ops = &od_ops, |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 578 | .init = od_init, |
| 579 | .exit = od_exit, |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 580 | }; |
| 581 | |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 582 | static void od_set_powersave_bias(unsigned int powersave_bias) |
| 583 | { |
| 584 | struct cpufreq_policy *policy; |
| 585 | struct dbs_data *dbs_data; |
| 586 | struct od_dbs_tuners *od_tuners; |
| 587 | unsigned int cpu; |
| 588 | cpumask_t done; |
| 589 | |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame^] | 590 | default_powersave_bias = powersave_bias; |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 591 | cpumask_clear(&done); |
| 592 | |
| 593 | get_online_cpus(); |
| 594 | for_each_online_cpu(cpu) { |
| 595 | if (cpumask_test_cpu(cpu, &done)) |
| 596 | continue; |
| 597 | |
| 598 | policy = per_cpu(od_cpu_dbs_info, cpu).cdbs.cur_policy; |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame^] | 599 | if (!policy) |
| 600 | continue; |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 601 | |
| 602 | cpumask_or(&done, &done, policy->cpus); |
Jacob Shin | c283755 | 2013-06-25 22:42:37 +0200 | [diff] [blame^] | 603 | |
| 604 | if (policy->governor != &cpufreq_gov_ondemand) |
| 605 | continue; |
| 606 | |
| 607 | dbs_data = policy->governor_data; |
| 608 | od_tuners = dbs_data->tuners; |
| 609 | od_tuners->powersave_bias = default_powersave_bias; |
Jacob Shin | fb30809 | 2013-04-02 09:56:56 -0500 | [diff] [blame] | 610 | } |
| 611 | put_online_cpus(); |
| 612 | } |
| 613 | |
| 614 | void od_register_powersave_bias_handler(unsigned int (*f) |
| 615 | (struct cpufreq_policy *, unsigned int, unsigned int), |
| 616 | unsigned int powersave_bias) |
| 617 | { |
| 618 | od_ops.powersave_bias_target = f; |
| 619 | od_set_powersave_bias(powersave_bias); |
| 620 | } |
| 621 | EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler); |
| 622 | |
| 623 | void od_unregister_powersave_bias_handler(void) |
| 624 | { |
| 625 | od_ops.powersave_bias_target = generic_powersave_bias_target; |
| 626 | od_set_powersave_bias(0); |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler); |
| 629 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 630 | static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy, |
| 631 | unsigned int event) |
| 632 | { |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 633 | return cpufreq_governor_dbs(policy, &od_dbs_cdata, event); |
Mike Chan | 00e299f | 2010-01-26 17:06:47 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 636 | #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
| 637 | static |
Arjan van de Ven | 19379b1 | 2010-05-09 08:26:51 -0700 | [diff] [blame] | 638 | #endif |
Viresh Kumar | 4471a34 | 2012-10-26 00:47:42 +0200 | [diff] [blame] | 639 | struct cpufreq_governor cpufreq_gov_ondemand = { |
| 640 | .name = "ondemand", |
| 641 | .governor = od_cpufreq_governor_dbs, |
| 642 | .max_transition_latency = TRANSITION_LATENCY_LIMIT, |
| 643 | .owner = THIS_MODULE, |
| 644 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | static int __init cpufreq_gov_dbs_init(void) |
| 647 | { |
Tejun Heo | 57df557 | 2011-01-26 12:12:50 +0100 | [diff] [blame] | 648 | return cpufreq_register_governor(&cpufreq_gov_ondemand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | static void __exit cpufreq_gov_dbs_exit(void) |
| 652 | { |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame] | 653 | cpufreq_unregister_governor(&cpufreq_gov_ondemand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 656 | MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>"); |
| 657 | MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>"); |
| 658 | MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for " |
Dave Jones | 2b03f89 | 2009-01-18 01:43:44 -0500 | [diff] [blame] | 659 | "Low Latency Frequency Transition capable processors"); |
Venkatesh Pallipadi | ffac80e | 2006-06-28 13:52:18 -0700 | [diff] [blame] | 660 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 662 | #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND |
| 663 | fs_initcall(cpufreq_gov_dbs_init); |
| 664 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | module_init(cpufreq_gov_dbs_init); |
Johannes Weiner | 6915719 | 2008-01-17 15:21:08 -0800 | [diff] [blame] | 666 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | module_exit(cpufreq_gov_dbs_exit); |