1 From a03dacb0316f74400846aaf144d6c73f4217ca08 Mon Sep 17 00:00:00 2001
2 From: Saravana Kannan <skannan@codeaurora.org>
3 Date: Tue, 2 Mar 2021 15:58:21 +0900
4 Subject: [PATCH 2/5] PM / devfreq: Add cpu based scaling support to passive
7 Many CPU architectures have caches that can scale independent of the
8 CPUs. Frequency scaling of the caches is necessary to make sure that the
9 cache is not a performance bottleneck that leads to poor performance and
10 power. The same idea applies for RAM/DDR.
12 To achieve this, this patch adds support for cpu based scaling to the
13 passive governor. This is accomplished by taking the current frequency
14 of each CPU frequency domain and then adjust the frequency of the cache
15 (or any devfreq device) based on the frequency of the CPUs. It listens
16 to CPU frequency transition notifiers to keep itself up to date on the
17 current CPU frequency.
19 To decide the frequency of the device, the governor does one of the
21 * Derives the optimal devfreq device opp from required-opps property of
22 the parent cpu opp_table.
24 * Scales the device frequency in proportion to the CPU frequency. So, if
25 the CPUs are running at their max frequency, the device runs at its
26 max frequency. If the CPUs are running at their min frequency, the
27 device runs at its min frequency. It is interpolated for frequencies
30 Tested-by: Chen-Yu Tsai <wenst@chromium.org>
31 Tested-by: Johnson Wang <johnson.wang@mediatek.com>
32 Signed-off-by: Saravana Kannan <skannan@codeaurora.org>
33 [Sibi: Integrated cpu-freqmap governor into passive_governor]
34 Signed-off-by: Sibi Sankar <sibis@codeaurora.org>
35 [Chanwoo: Fix conflict with latest code and cleanup code]
36 Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
38 drivers/devfreq/governor.h | 22 +++
39 drivers/devfreq/governor_passive.c | 298 +++++++++++++++++++++++++++--
40 include/linux/devfreq.h | 17 +-
41 3 files changed, 323 insertions(+), 14 deletions(-)
43 --- a/drivers/devfreq/governor.h
44 +++ b/drivers/devfreq/governor.h
46 #define DEVFREQ_GOV_ATTR_TIMER BIT(1)
49 + * struct devfreq_cpu_data - Hold the per-cpu data
50 + * @dev: reference to cpu device.
51 + * @first_cpu: the cpumask of the first cpu of a policy.
52 + * @opp_table: reference to cpu opp table.
53 + * @cur_freq: the current frequency of the cpu.
54 + * @min_freq: the min frequency of the cpu.
55 + * @max_freq: the max frequency of the cpu.
57 + * This structure stores the required cpu_data of a cpu.
58 + * This is auto-populated by the governor.
60 +struct devfreq_cpu_data {
62 + unsigned int first_cpu;
64 + struct opp_table *opp_table;
65 + unsigned int cur_freq;
66 + unsigned int min_freq;
67 + unsigned int max_freq;
71 * struct devfreq_governor - Devfreq policy governor
72 * @node: list node - contains registered devfreq governors
73 * @name: Governor's name
74 --- a/drivers/devfreq/governor_passive.c
75 +++ b/drivers/devfreq/governor_passive.c
79 #include <linux/module.h>
80 +#include <linux/cpu.h>
81 +#include <linux/cpufreq.h>
82 +#include <linux/cpumask.h>
83 +#include <linux/slab.h>
84 #include <linux/device.h>
85 #include <linux/devfreq.h>
88 -static int devfreq_passive_get_target_freq(struct devfreq *devfreq,
89 +#define HZ_PER_KHZ 1000
91 +static unsigned long get_target_freq_by_required_opp(struct device *p_dev,
92 + struct opp_table *p_opp_table,
93 + struct opp_table *opp_table,
94 + unsigned long *freq)
96 + struct dev_pm_opp *opp = NULL, *p_opp = NULL;
97 + unsigned long target_freq;
99 + if (!p_dev || !p_opp_table || !opp_table || !freq)
102 + p_opp = devfreq_recommended_opp(p_dev, freq, 0);
106 + opp = dev_pm_opp_xlate_required_opp(p_opp_table, opp_table, p_opp);
107 + dev_pm_opp_put(p_opp);
112 + target_freq = dev_pm_opp_get_freq(opp);
113 + dev_pm_opp_put(opp);
115 + return target_freq;
118 +static int get_target_freq_with_cpufreq(struct devfreq *devfreq,
119 + unsigned long *target_freq)
121 + struct devfreq_passive_data *p_data =
122 + (struct devfreq_passive_data *)devfreq->data;
123 + struct devfreq_cpu_data *parent_cpu_data;
124 + unsigned long cpu, cpu_cur, cpu_min, cpu_max, cpu_percent;
125 + unsigned long dev_min, dev_max;
126 + unsigned long freq = 0;
128 + for_each_online_cpu(cpu) {
129 + parent_cpu_data = p_data->parent_cpu_data[cpu];
130 + if (!parent_cpu_data || parent_cpu_data->first_cpu != cpu)
133 + /* Get target freq via required opps */
134 + cpu_cur = parent_cpu_data->cur_freq * HZ_PER_KHZ;
135 + freq = get_target_freq_by_required_opp(parent_cpu_data->dev,
136 + parent_cpu_data->opp_table,
137 + devfreq->opp_table, &cpu_cur);
139 + *target_freq = max(freq, *target_freq);
143 + /* Use interpolation if required opps is not available */
144 + devfreq_get_freq_range(devfreq, &dev_min, &dev_max);
146 + cpu_min = parent_cpu_data->min_freq;
147 + cpu_max = parent_cpu_data->max_freq;
148 + cpu_cur = parent_cpu_data->cur_freq;
150 + cpu_percent = ((cpu_cur - cpu_min) * 100) / (cpu_max - cpu_min);
151 + freq = dev_min + mult_frac(dev_max - dev_min, cpu_percent, 100);
153 + *target_freq = max(freq, *target_freq);
159 +static int get_target_freq_with_devfreq(struct devfreq *devfreq,
162 struct devfreq_passive_data *p_data
163 @@ -99,6 +173,181 @@ no_required_opp:
167 +static int devfreq_passive_get_target_freq(struct devfreq *devfreq,
168 + unsigned long *freq)
170 + struct devfreq_passive_data *p_data =
171 + (struct devfreq_passive_data *)devfreq->data;
178 + * If the devfreq device with passive governor has the specific method
179 + * to determine the next frequency, should use the get_target_freq()
180 + * of struct devfreq_passive_data.
182 + if (p_data->get_target_freq)
183 + return p_data->get_target_freq(devfreq, freq);
185 + switch (p_data->parent_type) {
186 + case DEVFREQ_PARENT_DEV:
187 + ret = get_target_freq_with_devfreq(devfreq, freq);
189 + case CPUFREQ_PARENT_DEV:
190 + ret = get_target_freq_with_cpufreq(devfreq, freq);
194 + dev_err(&devfreq->dev, "Invalid parent type\n");
201 +static int cpufreq_passive_notifier_call(struct notifier_block *nb,
202 + unsigned long event, void *ptr)
204 + struct devfreq_passive_data *p_data =
205 + container_of(nb, struct devfreq_passive_data, nb);
206 + struct devfreq *devfreq = (struct devfreq *)p_data->this;
207 + struct devfreq_cpu_data *parent_cpu_data;
208 + struct cpufreq_freqs *freqs = ptr;
209 + unsigned int cur_freq;
212 + if (event != CPUFREQ_POSTCHANGE || !freqs ||
213 + !p_data->parent_cpu_data[freqs->policy->cpu])
216 + parent_cpu_data = p_data->parent_cpu_data[freqs->policy->cpu];
217 + if (parent_cpu_data->cur_freq == freqs->new)
220 + cur_freq = parent_cpu_data->cur_freq;
221 + parent_cpu_data->cur_freq = freqs->new;
223 + mutex_lock(&devfreq->lock);
224 + ret = devfreq_update_target(devfreq, freqs->new);
225 + mutex_unlock(&devfreq->lock);
227 + parent_cpu_data->cur_freq = cur_freq;
228 + dev_err(&devfreq->dev, "failed to update the frequency.\n");
235 +static int cpufreq_passive_unregister_notifier(struct devfreq *devfreq)
237 + struct devfreq_passive_data *p_data
238 + = (struct devfreq_passive_data *)devfreq->data;
239 + struct devfreq_cpu_data *parent_cpu_data;
242 + if (p_data->nb.notifier_call) {
243 + ret = cpufreq_unregister_notifier(&p_data->nb,
244 + CPUFREQ_TRANSITION_NOTIFIER);
249 + for_each_possible_cpu(cpu) {
250 + parent_cpu_data = p_data->parent_cpu_data[cpu];
251 + if (!parent_cpu_data)
254 + if (parent_cpu_data->opp_table)
255 + dev_pm_opp_put_opp_table(parent_cpu_data->opp_table);
256 + kfree(parent_cpu_data);
262 +static int cpufreq_passive_register_notifier(struct devfreq *devfreq)
264 + struct devfreq_passive_data *p_data
265 + = (struct devfreq_passive_data *)devfreq->data;
266 + struct device *dev = devfreq->dev.parent;
267 + struct opp_table *opp_table = NULL;
268 + struct devfreq_cpu_data *parent_cpu_data;
269 + struct cpufreq_policy *policy;
270 + struct device *cpu_dev;
274 + p_data->nb.notifier_call = cpufreq_passive_notifier_call;
275 + ret = cpufreq_register_notifier(&p_data->nb, CPUFREQ_TRANSITION_NOTIFIER);
277 + dev_err(dev, "failed to register cpufreq notifier\n");
278 + p_data->nb.notifier_call = NULL;
282 + for_each_possible_cpu(cpu) {
283 + if (p_data->parent_cpu_data[cpu])
286 + policy = cpufreq_cpu_get(cpu);
288 + ret = -EPROBE_DEFER;
292 + parent_cpu_data = kzalloc(sizeof(*parent_cpu_data),
294 + if (!parent_cpu_data) {
296 + goto err_put_policy;
299 + cpu_dev = get_cpu_device(cpu);
301 + dev_err(dev, "failed to get cpu device\n");
303 + goto err_free_cpu_data;
306 + opp_table = dev_pm_opp_get_opp_table(cpu_dev);
307 + if (IS_ERR(opp_table)) {
308 + dev_err(dev, "failed to get opp_table of cpu%d\n", cpu);
309 + ret = PTR_ERR(opp_table);
310 + goto err_free_cpu_data;
313 + parent_cpu_data->dev = cpu_dev;
314 + parent_cpu_data->opp_table = opp_table;
315 + parent_cpu_data->first_cpu = cpumask_first(policy->related_cpus);
316 + parent_cpu_data->cur_freq = policy->cur;
317 + parent_cpu_data->min_freq = policy->cpuinfo.min_freq;
318 + parent_cpu_data->max_freq = policy->cpuinfo.max_freq;
320 + p_data->parent_cpu_data[cpu] = parent_cpu_data;
321 + cpufreq_cpu_put(policy);
324 + mutex_lock(&devfreq->lock);
325 + ret = devfreq_update_target(devfreq, 0L);
326 + mutex_unlock(&devfreq->lock);
328 + dev_err(dev, "failed to update the frequency\n");
333 + kfree(parent_cpu_data);
335 + cpufreq_cpu_put(policy);
337 + WARN_ON(cpufreq_passive_unregister_notifier(devfreq));
342 static int devfreq_passive_notifier_call(struct notifier_block *nb,
343 unsigned long event, void *ptr)
345 @@ -131,30 +380,55 @@ static int devfreq_passive_notifier_call
349 -static int devfreq_passive_event_handler(struct devfreq *devfreq,
350 - unsigned int event, void *data)
351 +static int devfreq_passive_unregister_notifier(struct devfreq *devfreq)
353 + struct devfreq_passive_data *p_data
354 + = (struct devfreq_passive_data *)devfreq->data;
355 + struct devfreq *parent = (struct devfreq *)p_data->parent;
356 + struct notifier_block *nb = &p_data->nb;
358 + return devfreq_unregister_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER);
361 +static int devfreq_passive_register_notifier(struct devfreq *devfreq)
363 struct devfreq_passive_data *p_data
364 = (struct devfreq_passive_data *)devfreq->data;
365 struct devfreq *parent = (struct devfreq *)p_data->parent;
366 struct notifier_block *nb = &p_data->nb;
370 return -EPROBE_DEFER;
372 + nb->notifier_call = devfreq_passive_notifier_call;
373 + return devfreq_register_notifier(parent, nb, DEVFREQ_TRANSITION_NOTIFIER);
376 +static int devfreq_passive_event_handler(struct devfreq *devfreq,
377 + unsigned int event, void *data)
379 + struct devfreq_passive_data *p_data
380 + = (struct devfreq_passive_data *)devfreq->data;
387 + p_data->this = devfreq;
390 case DEVFREQ_GOV_START:
392 - p_data->this = devfreq;
394 - nb->notifier_call = devfreq_passive_notifier_call;
395 - ret = devfreq_register_notifier(parent, nb,
396 - DEVFREQ_TRANSITION_NOTIFIER);
397 + if (p_data->parent_type == DEVFREQ_PARENT_DEV)
398 + ret = devfreq_passive_register_notifier(devfreq);
399 + else if (p_data->parent_type == CPUFREQ_PARENT_DEV)
400 + ret = cpufreq_passive_register_notifier(devfreq);
402 case DEVFREQ_GOV_STOP:
403 - WARN_ON(devfreq_unregister_notifier(parent, nb,
404 - DEVFREQ_TRANSITION_NOTIFIER));
405 + if (p_data->parent_type == DEVFREQ_PARENT_DEV)
406 + WARN_ON(devfreq_passive_unregister_notifier(devfreq));
407 + else if (p_data->parent_type == CPUFREQ_PARENT_DEV)
408 + WARN_ON(cpufreq_passive_unregister_notifier(devfreq));
412 --- a/include/linux/devfreq.h
413 +++ b/include/linux/devfreq.h
414 @@ -38,6 +38,7 @@ enum devfreq_timer {
417 struct devfreq_governor;
418 +struct devfreq_cpu_data;
419 struct thermal_cooling_device;
422 @@ -288,6 +289,11 @@ struct devfreq_simple_ondemand_data {
425 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
426 +enum devfreq_parent_dev_type {
427 + DEVFREQ_PARENT_DEV,
428 + CPUFREQ_PARENT_DEV,
432 * struct devfreq_passive_data - ``void *data`` fed to struct devfreq
433 * and devfreq_add_device
434 @@ -299,8 +305,11 @@ struct devfreq_simple_ondemand_data {
435 * using governors except for passive governor.
436 * If the devfreq device has the specific method to decide
437 * the next frequency, should use this callback.
438 - * @this: the devfreq instance of own device.
439 - * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
440 + * @parent_type: the parent type of the device.
441 + * @this: the devfreq instance of own device.
442 + * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER or
443 + * CPUFREQ_TRANSITION_NOTIFIER list.
444 + * @parent_cpu_data: the state min/max/current frequency of all online cpu's.
446 * The devfreq_passive_data have to set the devfreq instance of parent
447 * device with governors except for the passive governor. But, don't need to
448 @@ -314,9 +323,13 @@ struct devfreq_passive_data {
449 /* Optional callback to decide the next frequency of passvice device */
450 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
452 + /* Should set the type of parent device */
453 + enum devfreq_parent_dev_type parent_type;
455 /* For passive governor's internal use. Don't need to set them */
456 struct devfreq *this;
457 struct notifier_block nb;
458 + struct devfreq_cpu_data *parent_cpu_data[NR_CPUS];