Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PM / devfreq: Add cpu based scaling support to passive governor

Many CPU architectures have caches that can scale independent of the
CPUs. Frequency scaling of the caches is necessary to make sure that the
cache is not a performance bottleneck that leads to poor performance and
power. The same idea applies for RAM/DDR.

To achieve this, this patch adds support for cpu based scaling to the
passive governor. This is accomplished by taking the current frequency
of each CPU frequency domain and then adjust the frequency of the cache
(or any devfreq device) based on the frequency of the CPUs. It listens
to CPU frequency transition notifiers to keep itself up to date on the
current CPU frequency.

To decide the frequency of the device, the governor does one of the
following:
* Derives the optimal devfreq device opp from required-opps property of
the parent cpu opp_table.

* Scales the device frequency in proportion to the CPU frequency. So, if
the CPUs are running at their max frequency, the device runs at its
max frequency. If the CPUs are running at their min frequency, the
device runs at its min frequency. It is interpolated for frequencies
in between.

Tested-by: Chen-Yu Tsai <wenst@chromium.org>
Tested-by: Johnson Wang <johnson.wang@mediatek.com>
Signed-off-by: Saravana Kannan <skannan@codeaurora.org>
[Sibi: Integrated cpu-freqmap governor into passive_governor]
Signed-off-by: Sibi Sankar <sibis@codeaurora.org>
[Chanwoo: Fix conflict with latest code and cleanup code]
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>

authored by

Saravana Kannan and committed by
Chanwoo Choi
a03dacb0 713472e5

+323 -14
+22
drivers/devfreq/governor.h
··· 48 48 #define DEVFREQ_GOV_ATTR_TIMER BIT(1) 49 49 50 50 /** 51 + * struct devfreq_cpu_data - Hold the per-cpu data 52 + * @dev: reference to cpu device. 53 + * @first_cpu: the cpumask of the first cpu of a policy. 54 + * @opp_table: reference to cpu opp table. 55 + * @cur_freq: the current frequency of the cpu. 56 + * @min_freq: the min frequency of the cpu. 57 + * @max_freq: the max frequency of the cpu. 58 + * 59 + * This structure stores the required cpu_data of a cpu. 60 + * This is auto-populated by the governor. 61 + */ 62 + struct devfreq_cpu_data { 63 + struct device *dev; 64 + unsigned int first_cpu; 65 + 66 + struct opp_table *opp_table; 67 + unsigned int cur_freq; 68 + unsigned int min_freq; 69 + unsigned int max_freq; 70 + }; 71 + 72 + /** 51 73 * struct devfreq_governor - Devfreq policy governor 52 74 * @node: list node - contains registered devfreq governors 53 75 * @name: Governor's name
+286 -12
drivers/devfreq/governor_passive.c
··· 8 8 */ 9 9 10 10 #include <linux/module.h> 11 + #include <linux/cpu.h> 12 + #include <linux/cpufreq.h> 13 + #include <linux/cpumask.h> 14 + #include <linux/slab.h> 11 15 #include <linux/device.h> 12 16 #include <linux/devfreq.h> 13 17 #include "governor.h" 14 18 15 - static int devfreq_passive_get_target_freq(struct devfreq *devfreq, 19 + #define HZ_PER_KHZ 1000 20 + 21 + static unsigned long get_target_freq_by_required_opp(struct device *p_dev, 22 + struct opp_table *p_opp_table, 23 + struct opp_table *opp_table, 24 + unsigned long *freq) 25 + { 26 + struct dev_pm_opp *opp = NULL, *p_opp = NULL; 27 + unsigned long target_freq; 28 + 29 + if (!p_dev || !p_opp_table || !opp_table || !freq) 30 + return 0; 31 + 32 + p_opp = devfreq_recommended_opp(p_dev, freq, 0); 33 + if (IS_ERR(p_opp)) 34 + return 0; 35 + 36 + opp = dev_pm_opp_xlate_required_opp(p_opp_table, opp_table, p_opp); 37 + dev_pm_opp_put(p_opp); 38 + 39 + if (IS_ERR(opp)) 40 + return 0; 41 + 42 + target_freq = dev_pm_opp_get_freq(opp); 43 + dev_pm_opp_put(opp); 44 + 45 + return target_freq; 46 + } 47 + 48 + static int get_target_freq_with_cpufreq(struct devfreq *devfreq, 49 + unsigned long *target_freq) 50 + { 51 + struct devfreq_passive_data *p_data = 52 + (struct devfreq_passive_data *)devfreq->data; 53 + struct devfreq_cpu_data *parent_cpu_data; 54 + unsigned long cpu, cpu_cur, cpu_min, cpu_max, cpu_percent; 55 + unsigned long dev_min, dev_max; 56 + unsigned long freq = 0; 57 + 58 + for_each_online_cpu(cpu) { 59 + parent_cpu_data = p_data->parent_cpu_data[cpu]; 60 + if (!parent_cpu_data || parent_cpu_data->first_cpu != cpu) 61 + continue; 62 + 63 + /* Get target freq via required opps */ 64 + cpu_cur = parent_cpu_data->cur_freq * HZ_PER_KHZ; 65 + freq = get_target_freq_by_required_opp(parent_cpu_data->dev, 66 + parent_cpu_data->opp_table, 67 + devfreq->opp_table, &cpu_cur); 68 + if (freq) { 69 + *target_freq = max(freq, *target_freq); 70 + continue; 71 + } 72 + 73 + /* Use interpolation if required opps is not available */ 74 + devfreq_get_freq_range(devfreq, &dev_min, &dev_max); 75 + 76 + cpu_min = parent_cpu_data->min_freq; 77 + cpu_max = parent_cpu_data->max_freq; 78 + cpu_cur = parent_cpu_data->cur_freq; 79 + 80 + cpu_percent = ((cpu_cur - cpu_min) * 100) / (cpu_max - cpu_min); 81 + freq = dev_min + mult_frac(dev_max - dev_min, cpu_percent, 100); 82 + 83 + *target_freq = max(freq, *target_freq); 84 + } 85 + 86 + return 0; 87 + } 88 + 89 + static int get_target_freq_with_devfreq(struct devfreq *devfreq, 16 90 unsigned long *freq) 17 91 { 18 92 struct devfreq_passive_data *p_data ··· 173 99 return 0; 174 100 } 175 101 102 + static int devfreq_passive_get_target_freq(struct devfreq *devfreq, 103 + unsigned long *freq) 104 + { 105 + struct devfreq_passive_data *p_data = 106 + (struct devfreq_passive_data *)devfreq->data; 107 + int ret; 108 + 109 + if (!p_data) 110 + return -EINVAL; 111 + 112 + /* 113 + * If the devfreq device with passive governor has the specific method 114 + * to determine the next frequency, should use the get_target_freq() 115 + * of struct devfreq_passive_data. 116 + */ 117 + if (p_data->get_target_freq) 118 + return p_data->get_target_freq(devfreq, freq); 119 + 120 + switch (p_data->parent_type) { 121 + case DEVFREQ_PARENT_DEV: 122 + ret = get_target_freq_with_devfreq(devfreq, freq); 123 + break; 124 + case CPUFREQ_PARENT_DEV: 125 + ret = get_target_freq_with_cpufreq(devfreq, freq); 126 + break; 127 + default: 128 + ret = -EINVAL; 129 + dev_err(&devfreq->dev, "Invalid parent type\n"); 130 + break; 131 + } 132 + 133 + return ret; 134 + } 135 + 136 + static int cpufreq_passive_notifier_call(struct notifier_block *nb, 137 + unsigned long event, void *ptr) 138 + { 139 + struct devfreq_passive_data *p_data = 140 + container_of(nb, struct devfreq_passive_data, nb); 141 + struct devfreq *devfreq = (struct devfreq *)p_data->this; 142 + struct devfreq_cpu_data *parent_cpu_data; 143 + struct cpufreq_freqs *freqs = ptr; 144 + unsigned int cur_freq; 145 + int ret; 146 + 147 + if (event != CPUFREQ_POSTCHANGE || !freqs || 148 + !p_data->parent_cpu_data[freqs->policy->cpu]) 149 + return 0; 150 + 151 + parent_cpu_data = p_data->parent_cpu_data[freqs->policy->cpu]; 152 + if (parent_cpu_data->cur_freq == freqs->new) 153 + return 0; 154 + 155 + cur_freq = parent_cpu_data->cur_freq; 156 + parent_cpu_data->cur_freq = freqs->new; 157 + 158 + mutex_lock(&devfreq->lock); 159 + ret = devfreq_update_target(devfreq, freqs->new); 160 + mutex_unlock(&devfreq->lock); 161 + if (ret) { 162 + parent_cpu_data->cur_freq = cur_freq; 163 + dev_err(&devfreq->dev, "failed to update the frequency.\n"); 164 + return ret; 165 + } 166 + 167 + return 0; 168 + } 169 + 170 + static int cpufreq_passive_unregister_notifier(struct devfreq *devfreq) 171 + { 172 + struct devfreq_passive_data *p_data 173 + = (struct devfreq_passive_data *)devfreq->data; 174 + struct devfreq_cpu_data *parent_cpu_data; 175 + int cpu, ret; 176 + 177 + if (p_data->nb.notifier_call) { 178 + ret = cpufreq_unregister_notifier(&p_data->nb, 179 + CPUFREQ_TRANSITION_NOTIFIER); 180 + if (ret < 0) 181 + return ret; 182 + } 183 + 184 + for_each_possible_cpu(cpu) { 185 + parent_cpu_data = p_data->parent_cpu_data[cpu]; 186 + if (!parent_cpu_data) 187 + continue; 188 + 189 + if (parent_cpu_data->opp_table) 190 + dev_pm_opp_put_opp_table(parent_cpu_data->opp_table); 191 + kfree(parent_cpu_data); 192 + } 193 + 194 + return 0; 195 + } 196 + 197 + static int cpufreq_passive_register_notifier(struct devfreq *devfreq) 198 + { 199 + struct devfreq_passive_data *p_data 200 + = (struct devfreq_passive_data *)devfreq->data; 201 + struct device *dev = devfreq->dev.parent; 202 + struct opp_table *opp_table = NULL; 203 + struct devfreq_cpu_data *parent_cpu_data; 204 + struct cpufreq_policy *policy; 205 + struct device *cpu_dev; 206 + unsigned int cpu; 207 + int ret; 208 + 209 + p_data->nb.notifier_call = cpufreq_passive_notifier_call; 210 + ret = cpufreq_register_notifier(&p_data->nb, CPUFREQ_TRANSITION_NOTIFIER); 211 + if (ret) { 212 + dev_err(dev, "failed to register cpufreq notifier\n"); 213 + p_data->nb.notifier_call = NULL; 214 + goto err; 215 + } 216 + 217 + for_each_possible_cpu(cpu) { 218 + if (p_data->parent_cpu_data[cpu]) 219 + continue; 220 + 221 + policy = cpufreq_cpu_get(cpu); 222 + if (!policy) { 223 + ret = -EPROBE_DEFER; 224 + goto err; 225 + } 226 + 227 + parent_cpu_data = kzalloc(sizeof(*parent_cpu_data), 228 + GFP_KERNEL); 229 + if (!parent_cpu_data) { 230 + ret = -ENOMEM; 231 + goto err_put_policy; 232 + } 233 + 234 + cpu_dev = get_cpu_device(cpu); 235 + if (!cpu_dev) { 236 + dev_err(dev, "failed to get cpu device\n"); 237 + ret = -ENODEV; 238 + goto err_free_cpu_data; 239 + } 240 + 241 + opp_table = dev_pm_opp_get_opp_table(cpu_dev); 242 + if (IS_ERR(opp_table)) { 243 + dev_err(dev, "failed to get opp_table of cpu%d\n", cpu); 244 + ret = PTR_ERR(opp_table); 245 + goto err_free_cpu_data; 246 + } 247 + 248 + parent_cpu_data->dev = cpu_dev; 249 + parent_cpu_data->opp_table = opp_table; 250 + parent_cpu_data->first_cpu = cpumask_first(policy->related_cpus); 251 + parent_cpu_data->cur_freq = policy->cur; 252 + parent_cpu_data->min_freq = policy->cpuinfo.min_freq; 253 + parent_cpu_data->max_freq = policy->cpuinfo.max_freq; 254 + 255 + p_data->parent_cpu_data[cpu] = parent_cpu_data; 256 + cpufreq_cpu_put(policy); 257 + } 258 + 259 + mutex_lock(&devfreq->lock); 260 + ret = devfreq_update_target(devfreq, 0L); 261 + mutex_unlock(&devfreq->lock); 262 + if (ret) 263 + dev_err(dev, "failed to update the frequency\n"); 264 + 265 + return ret; 266 + 267 + err_free_cpu_data: 268 + kfree(parent_cpu_data); 269 + err_put_policy: 270 + cpufreq_cpu_put(policy); 271 + err: 272 + WARN_ON(cpufreq_passive_unregister_notifier(devfreq)); 273 + 274 + return ret; 275 + } 276 + 176 277 static int devfreq_passive_notifier_call(struct notifier_block *nb, 177 278 unsigned long event, void *ptr) 178 279 { ··· 380 131 return NOTIFY_DONE; 381 132 } 382 133 383 - static int devfreq_passive_event_handler(struct devfreq *devfreq, 384 - unsigned int event, void *data) 134 + static int devfreq_passive_unregister_notifier(struct devfreq *devfreq) 385 135 { 386 136 struct devfreq_passive_data *p_data 387 137 = (struct devfreq_passive_data *)devfreq->data; 388 138 struct devfreq *parent = (struct devfreq *)p_data->parent; 389 139 struct notifier_block *nb = &p_data->nb; 390 - int ret = 0; 140 + 141 + return devfreq_unregister_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER); 142 + } 143 + 144 + static int devfreq_passive_register_notifier(struct devfreq *devfreq) 145 + { 146 + struct devfreq_passive_data *p_data 147 + = (struct devfreq_passive_data *)devfreq->data; 148 + struct devfreq *parent = (struct devfreq *)p_data->parent; 149 + struct notifier_block *nb = &p_data->nb; 391 150 392 151 if (!parent) 393 152 return -EPROBE_DEFER; 394 153 154 + nb->notifier_call = devfreq_passive_notifier_call; 155 + return devfreq_register_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER); 156 + } 157 + 158 + static int devfreq_passive_event_handler(struct devfreq *devfreq, 159 + unsigned int event, void *data) 160 + { 161 + struct devfreq_passive_data *p_data 162 + = (struct devfreq_passive_data *)devfreq->data; 163 + int ret = -EINVAL; 164 + 165 + if (!p_data) 166 + return -EINVAL; 167 + 168 + if (!p_data->this) 169 + p_data->this = devfreq; 170 + 395 171 switch (event) { 396 172 case DEVFREQ_GOV_START: 397 - if (!p_data->this) 398 - p_data->this = devfreq; 399 - 400 - nb->notifier_call = devfreq_passive_notifier_call; 401 - ret = devfreq_register_notifier(parent, nb, 402 - DEVFREQ_TRANSITION_NOTIFIER); 173 + if (p_data->parent_type == DEVFREQ_PARENT_DEV) 174 + ret = devfreq_passive_register_notifier(devfreq); 175 + else if (p_data->parent_type == CPUFREQ_PARENT_DEV) 176 + ret = cpufreq_passive_register_notifier(devfreq); 403 177 break; 404 178 case DEVFREQ_GOV_STOP: 405 - WARN_ON(devfreq_unregister_notifier(parent, nb, 406 - DEVFREQ_TRANSITION_NOTIFIER)); 179 + if (p_data->parent_type == DEVFREQ_PARENT_DEV) 180 + WARN_ON(devfreq_passive_unregister_notifier(devfreq)); 181 + else if (p_data->parent_type == CPUFREQ_PARENT_DEV) 182 + WARN_ON(cpufreq_passive_unregister_notifier(devfreq)); 407 183 break; 408 184 default: 409 185 break;
+15 -2
include/linux/devfreq.h
··· 38 38 39 39 struct devfreq; 40 40 struct devfreq_governor; 41 + struct devfreq_cpu_data; 41 42 struct thermal_cooling_device; 42 43 43 44 /** ··· 289 288 #endif 290 289 291 290 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE) 291 + enum devfreq_parent_dev_type { 292 + DEVFREQ_PARENT_DEV, 293 + CPUFREQ_PARENT_DEV, 294 + }; 295 + 292 296 /** 293 297 * struct devfreq_passive_data - ``void *data`` fed to struct devfreq 294 298 * and devfreq_add_device ··· 305 299 * using governors except for passive governor. 306 300 * If the devfreq device has the specific method to decide 307 301 * the next frequency, should use this callback. 308 - * @this: the devfreq instance of own device. 309 - * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list 302 + * @parent_type: the parent type of the device. 303 + * @this: the devfreq instance of own device. 304 + * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER or 305 + * CPUFREQ_TRANSITION_NOTIFIER list. 306 + * @parent_cpu_data: the state min/max/current frequency of all online cpu's. 310 307 * 311 308 * The devfreq_passive_data have to set the devfreq instance of parent 312 309 * device with governors except for the passive governor. But, don't need to ··· 323 314 /* Optional callback to decide the next frequency of passvice device */ 324 315 int (*get_target_freq)(struct devfreq *this, unsigned long *freq); 325 316 317 + /* Should set the type of parent device */ 318 + enum devfreq_parent_dev_type parent_type; 319 + 326 320 /* For passive governor's internal use. Don't need to set them */ 327 321 struct devfreq *this; 328 322 struct notifier_block nb; 323 + struct devfreq_cpu_data *parent_cpu_data[NR_CPUS]; 329 324 }; 330 325 #endif 331 326