cpufreq: governor: Make governor private data per-policy
[deliverable/linux.git] / drivers / cpufreq / cpufreq_ondemand.c
CommitLineData
1da177e4
LT
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
4471a34f
VK
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
5ff0a268 15#include <linux/cpu.h>
4471a34f 16#include <linux/percpu-defs.h>
4d5dcc42 17#include <linux/slab.h>
80800913 18#include <linux/tick.h>
7d5a9956
RW
19
20#include "cpufreq_ondemand.h"
1da177e4 21
06eb09d1 22/* On-demand governor macros */
1da177e4 23#define DEF_FREQUENCY_UP_THRESHOLD (80)
3f78a9f7
DN
24#define DEF_SAMPLING_DOWN_FACTOR (1)
25#define MAX_SAMPLING_DOWN_FACTOR (100000)
80800913 26#define MICRO_FREQUENCY_UP_THRESHOLD (95)
cef9615a 27#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
c29f1403 28#define MIN_FREQUENCY_UP_THRESHOLD (11)
1da177e4
LT
29#define MAX_FREQUENCY_UP_THRESHOLD (100)
30
4471a34f 31static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
1da177e4 32
a33cce1c 33static struct dbs_governor od_dbs_gov;
fb30809e
JS
34static struct od_ops od_ops;
35
c2837558
JS
36static unsigned int default_powersave_bias;
37
4471a34f
VK
38/*
39 * Not all CPUs want IO time to be accounted as busy; this depends on how
40 * efficient idling at a higher frequency/voltage is.
41 * Pavel Machek says this is not so for various generations of AMD and old
42 * Intel systems.
06eb09d1 43 * Mike Chan (android.com) claims this is also not true for ARM.
4471a34f
VK
44 * Because of this, whitelist specific known (series) of CPUs by default, and
45 * leave all others up to the user.
46 */
47static int should_io_be_busy(void)
48{
49#if defined(CONFIG_X86)
50 /*
06eb09d1 51 * For Intel, Core 2 (model 15) and later have an efficient idle.
4471a34f
VK
52 */
53 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
54 boot_cpu_data.x86 == 6 &&
55 boot_cpu_data.x86_model >= 15)
56 return 1;
57#endif
58 return 0;
6b8fcd90
AV
59}
60
05ca0350
AS
61/*
62 * Find right freq to be set now with powersave_bias on.
07aa4402
RW
63 * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
64 * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
05ca0350 65 */
fb30809e 66static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
4471a34f 67 unsigned int freq_next, unsigned int relation)
05ca0350
AS
68{
69 unsigned int freq_req, freq_reduc, freq_avg;
70 unsigned int freq_hi, freq_lo;
71 unsigned int index = 0;
07aa4402 72 unsigned int delay_hi_us;
bc505475 73 struct policy_dbs_info *policy_dbs = policy->governor_data;
7d5a9956 74 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
bc505475 75 struct dbs_data *dbs_data = policy_dbs->dbs_data;
4d5dcc42 76 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
05ca0350
AS
77
78 if (!dbs_info->freq_table) {
79 dbs_info->freq_lo = 0;
07aa4402 80 dbs_info->freq_lo_delay_us = 0;
05ca0350
AS
81 return freq_next;
82 }
83
84 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
85 relation, &index);
86 freq_req = dbs_info->freq_table[index].frequency;
4d5dcc42 87 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
05ca0350
AS
88 freq_avg = freq_req - freq_reduc;
89
90 /* Find freq bounds for freq_avg in freq_table */
91 index = 0;
92 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
93 CPUFREQ_RELATION_H, &index);
94 freq_lo = dbs_info->freq_table[index].frequency;
95 index = 0;
96 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
97 CPUFREQ_RELATION_L, &index);
98 freq_hi = dbs_info->freq_table[index].frequency;
99
100 /* Find out how long we have to be in hi and lo freqs */
101 if (freq_hi == freq_lo) {
102 dbs_info->freq_lo = 0;
07aa4402 103 dbs_info->freq_lo_delay_us = 0;
05ca0350
AS
104 return freq_lo;
105 }
07aa4402
RW
106 delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
107 delay_hi_us += (freq_hi - freq_lo) / 2;
108 delay_hi_us /= freq_hi - freq_lo;
109 dbs_info->freq_hi_delay_us = delay_hi_us;
05ca0350 110 dbs_info->freq_lo = freq_lo;
07aa4402 111 dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
05ca0350
AS
112 return freq_hi;
113}
114
d1db75ff 115static void ondemand_powersave_bias_init(struct cpufreq_policy *policy)
05ca0350 116{
7d5a9956 117 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
d1db75ff 118
7d5a9956 119 dbs_info->freq_table = cpufreq_frequency_get_table(policy->cpu);
d1db75ff 120 dbs_info->freq_lo = 0;
05ca0350
AS
121}
122
3a3e9e06 123static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
4471a34f 124{
bc505475
RW
125 struct policy_dbs_info *policy_dbs = policy->governor_data;
126 struct dbs_data *dbs_data = policy_dbs->dbs_data;
4d5dcc42
VK
127 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
128
129 if (od_tuners->powersave_bias)
3a3e9e06 130 freq = od_ops.powersave_bias_target(policy, freq,
fb30809e 131 CPUFREQ_RELATION_H);
3a3e9e06 132 else if (policy->cur == policy->max)
4471a34f 133 return;
0e625ac1 134
3a3e9e06 135 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
4471a34f
VK
136 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
137}
138
139/*
140 * Every sampling_rate, we check, if current idle time is less than 20%
dfa5bb62
SK
141 * (default), then we try to increase frequency. Else, we adjust the frequency
142 * proportional to load.
4471a34f 143 */
4cccf755 144static void od_update(struct cpufreq_policy *policy)
1da177e4 145{
7d5a9956
RW
146 struct policy_dbs_info *policy_dbs = policy->governor_data;
147 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
bc505475 148 struct dbs_data *dbs_data = policy_dbs->dbs_data;
4d5dcc42 149 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
4cccf755 150 unsigned int load = dbs_update(policy);
4471a34f
VK
151
152 dbs_info->freq_lo = 0;
153
154 /* Check for frequency increase */
ff4b1789 155 if (load > dbs_data->up_threshold) {
4471a34f
VK
156 /* If switching to max speed, apply sampling_down_factor */
157 if (policy->cur < policy->max)
57dc3bcd 158 policy_dbs->rate_mult = dbs_data->sampling_down_factor;
4471a34f 159 dbs_freq_increase(policy, policy->max);
dfa5bb62
SK
160 } else {
161 /* Calculate the next frequency proportional to load */
6393d6a1
SK
162 unsigned int freq_next, min_f, max_f;
163
164 min_f = policy->cpuinfo.min_freq;
165 max_f = policy->cpuinfo.max_freq;
166 freq_next = min_f + load * (max_f - min_f) / 100;
4471a34f
VK
167
168 /* No longer fully busy, reset rate_mult */
57dc3bcd 169 policy_dbs->rate_mult = 1;
4471a34f 170
a7f35cff
RW
171 if (od_tuners->powersave_bias)
172 freq_next = od_ops.powersave_bias_target(policy,
173 freq_next,
174 CPUFREQ_RELATION_L);
175
6393d6a1 176 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
4471a34f 177 }
1da177e4
LT
178}
179
9be4fd2c 180static unsigned int od_dbs_timer(struct cpufreq_policy *policy)
4471a34f 181{
bc505475
RW
182 struct policy_dbs_info *policy_dbs = policy->governor_data;
183 struct dbs_data *dbs_data = policy_dbs->dbs_data;
7d5a9956 184 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy_dbs);
6e96c5b3 185 int sample_type = dbs_info->sample_type;
4447266b 186
4471a34f 187 /* Common NORMAL_SAMPLE setup */
43e0ee36 188 dbs_info->sample_type = OD_NORMAL_SAMPLE;
4cccf755
RW
189 /*
190 * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
191 * it then.
192 */
193 if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
43e0ee36 194 __cpufreq_driver_target(policy, dbs_info->freq_lo,
42994af6 195 CPUFREQ_RELATION_H);
07aa4402 196 return dbs_info->freq_lo_delay_us;
6e96c5b3
RW
197 }
198
199 od_update(policy);
200
201 if (dbs_info->freq_lo) {
202 /* Setup timer for SUB_SAMPLE */
203 dbs_info->sample_type = OD_SUB_SAMPLE;
07aa4402 204 return dbs_info->freq_hi_delay_us;
4471a34f
VK
205 }
206
07aa4402 207 return dbs_data->sampling_rate * policy_dbs->rate_mult;
da53d61e
FB
208}
209
4471a34f 210/************************** sysfs interface ************************/
7bdad34d 211static struct dbs_governor od_dbs_gov;
1da177e4 212
4d5dcc42
VK
213static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
214 size_t count)
19379b11
AV
215{
216 unsigned int input;
217 int ret;
218
219 ret = sscanf(buf, "%u", &input);
220 if (ret != 1)
221 return -EINVAL;
8847e038 222 dbs_data->io_is_busy = !!input;
9366d840
SK
223
224 /* we need to re-evaluate prev_cpu_idle */
a33cce1c
RW
225 gov_update_cpu_data(&od_dbs_gov, dbs_data);
226
19379b11
AV
227 return count;
228}
229
4d5dcc42
VK
230static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
231 size_t count)
1da177e4
LT
232{
233 unsigned int input;
234 int ret;
ffac80e9 235 ret = sscanf(buf, "%u", &input);
1da177e4 236
32ee8c3e 237 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
c29f1403 238 input < MIN_FREQUENCY_UP_THRESHOLD) {
1da177e4
LT
239 return -EINVAL;
240 }
4bd4e428 241
ff4b1789 242 dbs_data->up_threshold = input;
1da177e4
LT
243 return count;
244}
245
4d5dcc42
VK
246static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
247 const char *buf, size_t count)
3f78a9f7 248{
57dc3bcd
RW
249 struct policy_dbs_info *policy_dbs;
250 unsigned int input;
3f78a9f7
DN
251 int ret;
252 ret = sscanf(buf, "%u", &input);
253
254 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
255 return -EINVAL;
57dc3bcd 256
ff4b1789 257 dbs_data->sampling_down_factor = input;
3f78a9f7
DN
258
259 /* Reset down sampling multiplier in case it was active */
57dc3bcd
RW
260 list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) {
261 /*
262 * Doing this without locking might lead to using different
263 * rate_mult values in od_update() and od_dbs_timer().
264 */
265 mutex_lock(&policy_dbs->timer_mutex);
266 policy_dbs->rate_mult = 1;
267 mutex_unlock(&policy_dbs->timer_mutex);
3f78a9f7 268 }
57dc3bcd 269
3f78a9f7
DN
270 return count;
271}
272
6c4640c3
VK
273static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
274 const char *buf, size_t count)
3d5ee9e5
DJ
275{
276 unsigned int input;
277 int ret;
278
ffac80e9 279 ret = sscanf(buf, "%u", &input);
2b03f891 280 if (ret != 1)
3d5ee9e5
DJ
281 return -EINVAL;
282
2b03f891 283 if (input > 1)
3d5ee9e5 284 input = 1;
32ee8c3e 285
ff4b1789 286 if (input == dbs_data->ignore_nice_load) { /* nothing to do */
3d5ee9e5
DJ
287 return count;
288 }
ff4b1789 289 dbs_data->ignore_nice_load = input;
3d5ee9e5 290
ccb2fe20 291 /* we need to re-evaluate prev_cpu_idle */
a33cce1c 292 gov_update_cpu_data(&od_dbs_gov, dbs_data);
1ca3abdb 293
3d5ee9e5
DJ
294 return count;
295}
296
4d5dcc42
VK
297static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
298 size_t count)
05ca0350 299{
4d5dcc42 300 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
d1db75ff 301 struct policy_dbs_info *policy_dbs;
05ca0350
AS
302 unsigned int input;
303 int ret;
304 ret = sscanf(buf, "%u", &input);
305
306 if (ret != 1)
307 return -EINVAL;
308
309 if (input > 1000)
310 input = 1000;
311
4d5dcc42 312 od_tuners->powersave_bias = input;
d1db75ff
RW
313
314 list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list)
315 ondemand_powersave_bias_init(policy_dbs->policy);
316
05ca0350
AS
317 return count;
318}
319
c4435630
VK
320gov_show_one_common(sampling_rate);
321gov_show_one_common(up_threshold);
322gov_show_one_common(sampling_down_factor);
323gov_show_one_common(ignore_nice_load);
324gov_show_one_common(min_sampling_rate);
8847e038 325gov_show_one_common(io_is_busy);
c4435630
VK
326gov_show_one(od, powersave_bias);
327
328gov_attr_rw(sampling_rate);
329gov_attr_rw(io_is_busy);
330gov_attr_rw(up_threshold);
331gov_attr_rw(sampling_down_factor);
332gov_attr_rw(ignore_nice_load);
333gov_attr_rw(powersave_bias);
334gov_attr_ro(min_sampling_rate);
335
336static struct attribute *od_attributes[] = {
337 &min_sampling_rate.attr,
338 &sampling_rate.attr,
339 &up_threshold.attr,
340 &sampling_down_factor.attr,
341 &ignore_nice_load.attr,
342 &powersave_bias.attr,
343 &io_is_busy.attr,
1da177e4
LT
344 NULL
345};
346
1da177e4
LT
347/************************** sysfs end ************************/
348
7d5a9956
RW
349static struct policy_dbs_info *od_alloc(void)
350{
351 struct od_policy_dbs_info *dbs_info;
352
353 dbs_info = kzalloc(sizeof(*dbs_info), GFP_KERNEL);
354 return dbs_info ? &dbs_info->policy_dbs : NULL;
355}
356
357static void od_free(struct policy_dbs_info *policy_dbs)
358{
359 kfree(to_dbs_info(policy_dbs));
360}
361
8e0484d2 362static int od_init(struct dbs_data *dbs_data, bool notify)
4d5dcc42
VK
363{
364 struct od_dbs_tuners *tuners;
365 u64 idle_time;
366 int cpu;
367
d5b73cd8 368 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
4d5dcc42
VK
369 if (!tuners) {
370 pr_err("%s: kzalloc failed\n", __func__);
371 return -ENOMEM;
372 }
373
374 cpu = get_cpu();
375 idle_time = get_cpu_idle_time_us(cpu, NULL);
376 put_cpu();
377 if (idle_time != -1ULL) {
378 /* Idle micro accounting is supported. Use finer thresholds */
ff4b1789 379 dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
4d5dcc42
VK
380 /*
381 * In nohz/micro accounting case we set the minimum frequency
382 * not depending on HZ, but fixed (very low). The deferred
383 * timer might skip some samples if idle/sleeping as needed.
384 */
385 dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
386 } else {
ff4b1789 387 dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
4d5dcc42
VK
388
389 /* For correct statistics, we need 10 ticks for each measure */
390 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
391 jiffies_to_usecs(10);
392 }
393
ff4b1789
VK
394 dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
395 dbs_data->ignore_nice_load = 0;
c2837558 396 tuners->powersave_bias = default_powersave_bias;
8847e038 397 dbs_data->io_is_busy = should_io_be_busy();
4d5dcc42
VK
398
399 dbs_data->tuners = tuners;
4d5dcc42
VK
400 return 0;
401}
402
8e0484d2 403static void od_exit(struct dbs_data *dbs_data, bool notify)
4d5dcc42
VK
404{
405 kfree(dbs_data->tuners);
406}
407
702c9e54
RW
408static void od_start(struct cpufreq_policy *policy)
409{
7d5a9956 410 struct od_policy_dbs_info *dbs_info = to_dbs_info(policy->governor_data);
702c9e54
RW
411
412 dbs_info->sample_type = OD_NORMAL_SAMPLE;
d1db75ff 413 ondemand_powersave_bias_init(policy);
702c9e54
RW
414}
415
4471a34f 416define_get_cpu_dbs_routines(od_cpu_dbs_info);
6b8fcd90 417
4471a34f 418static struct od_ops od_ops = {
fb30809e 419 .powersave_bias_target = generic_powersave_bias_target,
4471a34f 420};
2f8a835c 421
7bdad34d 422static struct dbs_governor od_dbs_gov = {
af926185
RW
423 .gov = {
424 .name = "ondemand",
906a6e5a 425 .governor = cpufreq_governor_dbs,
af926185
RW
426 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
427 .owner = THIS_MODULE,
428 },
c4435630 429 .kobj_type = { .default_attrs = od_attributes },
4471a34f 430 .get_cpu_cdbs = get_cpu_cdbs,
4471a34f 431 .gov_dbs_timer = od_dbs_timer,
7d5a9956
RW
432 .alloc = od_alloc,
433 .free = od_free,
4d5dcc42
VK
434 .init = od_init,
435 .exit = od_exit,
702c9e54 436 .start = od_start,
4471a34f 437};
1da177e4 438
7bdad34d 439#define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
af926185 440
fb30809e
JS
441static void od_set_powersave_bias(unsigned int powersave_bias)
442{
443 struct cpufreq_policy *policy;
444 struct dbs_data *dbs_data;
445 struct od_dbs_tuners *od_tuners;
446 unsigned int cpu;
447 cpumask_t done;
448
c2837558 449 default_powersave_bias = powersave_bias;
fb30809e
JS
450 cpumask_clear(&done);
451
452 get_online_cpus();
453 for_each_online_cpu(cpu) {
e40e7b25 454 struct policy_dbs_info *policy_dbs;
44152cb8 455
fb30809e
JS
456 if (cpumask_test_cpu(cpu, &done))
457 continue;
458
e40e7b25
RW
459 policy_dbs = per_cpu(od_cpu_dbs_info, cpu).cdbs.policy_dbs;
460 if (!policy_dbs)
c2837558 461 continue;
fb30809e 462
e40e7b25 463 policy = policy_dbs->policy;
fb30809e 464 cpumask_or(&done, &done, policy->cpus);
c2837558 465
af926185 466 if (policy->governor != CPU_FREQ_GOV_ONDEMAND)
c2837558
JS
467 continue;
468
bc505475 469 dbs_data = policy_dbs->dbs_data;
c2837558
JS
470 od_tuners = dbs_data->tuners;
471 od_tuners->powersave_bias = default_powersave_bias;
fb30809e
JS
472 }
473 put_online_cpus();
474}
475
476void od_register_powersave_bias_handler(unsigned int (*f)
477 (struct cpufreq_policy *, unsigned int, unsigned int),
478 unsigned int powersave_bias)
479{
480 od_ops.powersave_bias_target = f;
481 od_set_powersave_bias(powersave_bias);
482}
483EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
484
485void od_unregister_powersave_bias_handler(void)
486{
487 od_ops.powersave_bias_target = generic_powersave_bias_target;
488 od_set_powersave_bias(0);
489}
490EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
491
1da177e4
LT
492static int __init cpufreq_gov_dbs_init(void)
493{
af926185 494 return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
1da177e4
LT
495}
496
497static void __exit cpufreq_gov_dbs_exit(void)
498{
af926185 499 cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
1da177e4
LT
500}
501
ffac80e9
VP
502MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
503MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
504MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
2b03f891 505 "Low Latency Frequency Transition capable processors");
ffac80e9 506MODULE_LICENSE("GPL");
1da177e4 507
6915719b 508#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
de1df26b
RW
509struct cpufreq_governor *cpufreq_default_governor(void)
510{
af926185 511 return CPU_FREQ_GOV_ONDEMAND;
de1df26b
RW
512}
513
6915719b
JW
514fs_initcall(cpufreq_gov_dbs_init);
515#else
1da177e4 516module_init(cpufreq_gov_dbs_init);
6915719b 517#endif
1da177e4 518module_exit(cpufreq_gov_dbs_exit);
This page took 0.797224 seconds and 5 git commands to generate.