cpufreq: governor: Simplify cpufreq_governor_limits()
[linux-block.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>
4471a34f 19#include "cpufreq_governor.h"
1da177e4 20
06eb09d1 21/* On-demand governor macros */
1da177e4 22#define DEF_FREQUENCY_UP_THRESHOLD (80)
3f78a9f7
DN
23#define DEF_SAMPLING_DOWN_FACTOR (1)
24#define MAX_SAMPLING_DOWN_FACTOR (100000)
80800913 25#define MICRO_FREQUENCY_UP_THRESHOLD (95)
cef9615a 26#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
c29f1403 27#define MIN_FREQUENCY_UP_THRESHOLD (11)
1da177e4
LT
28#define MAX_FREQUENCY_UP_THRESHOLD (100)
29
4471a34f 30static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
1da177e4 31
fb30809e
JS
32static struct od_ops od_ops;
33
c2837558
JS
34static unsigned int default_powersave_bias;
35
4471a34f 36static void ondemand_powersave_bias_init_cpu(int cpu)
6b8fcd90 37{
4471a34f 38 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
6b8fcd90 39
4471a34f
VK
40 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
41 dbs_info->freq_lo = 0;
42}
6b8fcd90 43
4471a34f
VK
44/*
45 * Not all CPUs want IO time to be accounted as busy; this depends on how
46 * efficient idling at a higher frequency/voltage is.
47 * Pavel Machek says this is not so for various generations of AMD and old
48 * Intel systems.
06eb09d1 49 * Mike Chan (android.com) claims this is also not true for ARM.
4471a34f
VK
50 * Because of this, whitelist specific known (series) of CPUs by default, and
51 * leave all others up to the user.
52 */
53static int should_io_be_busy(void)
54{
55#if defined(CONFIG_X86)
56 /*
06eb09d1 57 * For Intel, Core 2 (model 15) and later have an efficient idle.
4471a34f
VK
58 */
59 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
60 boot_cpu_data.x86 == 6 &&
61 boot_cpu_data.x86_model >= 15)
62 return 1;
63#endif
64 return 0;
6b8fcd90
AV
65}
66
05ca0350
AS
67/*
68 * Find right freq to be set now with powersave_bias on.
69 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
70 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
71 */
fb30809e 72static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
4471a34f 73 unsigned int freq_next, unsigned int relation)
05ca0350
AS
74{
75 unsigned int freq_req, freq_reduc, freq_avg;
76 unsigned int freq_hi, freq_lo;
77 unsigned int index = 0;
78 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
4471a34f 79 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
245b2e70 80 policy->cpu);
4d5dcc42
VK
81 struct dbs_data *dbs_data = policy->governor_data;
82 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
05ca0350
AS
83
84 if (!dbs_info->freq_table) {
85 dbs_info->freq_lo = 0;
86 dbs_info->freq_lo_jiffies = 0;
87 return freq_next;
88 }
89
90 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
91 relation, &index);
92 freq_req = dbs_info->freq_table[index].frequency;
4d5dcc42 93 freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
05ca0350
AS
94 freq_avg = freq_req - freq_reduc;
95
96 /* Find freq bounds for freq_avg in freq_table */
97 index = 0;
98 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
99 CPUFREQ_RELATION_H, &index);
100 freq_lo = dbs_info->freq_table[index].frequency;
101 index = 0;
102 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
103 CPUFREQ_RELATION_L, &index);
104 freq_hi = dbs_info->freq_table[index].frequency;
105
106 /* Find out how long we have to be in hi and lo freqs */
107 if (freq_hi == freq_lo) {
108 dbs_info->freq_lo = 0;
109 dbs_info->freq_lo_jiffies = 0;
110 return freq_lo;
111 }
4d5dcc42 112 jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate);
05ca0350
AS
113 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
114 jiffies_hi += ((freq_hi - freq_lo) / 2);
115 jiffies_hi /= (freq_hi - freq_lo);
116 jiffies_lo = jiffies_total - jiffies_hi;
117 dbs_info->freq_lo = freq_lo;
118 dbs_info->freq_lo_jiffies = jiffies_lo;
119 dbs_info->freq_hi_jiffies = jiffies_hi;
120 return freq_hi;
121}
122
123static void ondemand_powersave_bias_init(void)
124{
125 int i;
126 for_each_online_cpu(i) {
5a75c828 127 ondemand_powersave_bias_init_cpu(i);
05ca0350
AS
128 }
129}
130
3a3e9e06 131static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
4471a34f 132{
3a3e9e06 133 struct dbs_data *dbs_data = policy->governor_data;
4d5dcc42
VK
134 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
135
136 if (od_tuners->powersave_bias)
3a3e9e06 137 freq = od_ops.powersave_bias_target(policy, freq,
fb30809e 138 CPUFREQ_RELATION_H);
3a3e9e06 139 else if (policy->cur == policy->max)
4471a34f 140 return;
0e625ac1 141
3a3e9e06 142 __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
4471a34f
VK
143 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
144}
145
146/*
147 * Every sampling_rate, we check, if current idle time is less than 20%
dfa5bb62
SK
148 * (default), then we try to increase frequency. Else, we adjust the frequency
149 * proportional to load.
4471a34f 150 */
dfa5bb62 151static void od_check_cpu(int cpu, unsigned int load)
1da177e4 152{
4471a34f 153 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
e40e7b25 154 struct cpufreq_policy *policy = dbs_info->cdbs.policy_dbs->policy;
4d5dcc42
VK
155 struct dbs_data *dbs_data = policy->governor_data;
156 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
4471a34f
VK
157
158 dbs_info->freq_lo = 0;
159
160 /* Check for frequency increase */
dfa5bb62 161 if (load > od_tuners->up_threshold) {
4471a34f
VK
162 /* If switching to max speed, apply sampling_down_factor */
163 if (policy->cur < policy->max)
164 dbs_info->rate_mult =
4d5dcc42 165 od_tuners->sampling_down_factor;
4471a34f 166 dbs_freq_increase(policy, policy->max);
dfa5bb62
SK
167 } else {
168 /* Calculate the next frequency proportional to load */
6393d6a1
SK
169 unsigned int freq_next, min_f, max_f;
170
171 min_f = policy->cpuinfo.min_freq;
172 max_f = policy->cpuinfo.max_freq;
173 freq_next = min_f + load * (max_f - min_f) / 100;
4471a34f
VK
174
175 /* No longer fully busy, reset rate_mult */
176 dbs_info->rate_mult = 1;
177
4d5dcc42 178 if (!od_tuners->powersave_bias) {
4471a34f 179 __cpufreq_driver_target(policy, freq_next,
6393d6a1 180 CPUFREQ_RELATION_C);
fb30809e 181 return;
4471a34f 182 }
fb30809e
JS
183
184 freq_next = od_ops.powersave_bias_target(policy, freq_next,
185 CPUFREQ_RELATION_L);
6393d6a1 186 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
4471a34f 187 }
1da177e4
LT
188}
189
9be4fd2c 190static unsigned int od_dbs_timer(struct cpufreq_policy *policy)
4471a34f 191{
affde5d0 192 struct dbs_data *dbs_data = policy->governor_data;
d10b5eb5 193 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
4d5dcc42 194 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
43e0ee36 195 int delay = 0, sample_type = dbs_info->sample_type;
4447266b 196
4471a34f 197 /* Common NORMAL_SAMPLE setup */
43e0ee36 198 dbs_info->sample_type = OD_NORMAL_SAMPLE;
4471a34f 199 if (sample_type == OD_SUB_SAMPLE) {
43e0ee36
VK
200 delay = dbs_info->freq_lo_jiffies;
201 __cpufreq_driver_target(policy, dbs_info->freq_lo,
42994af6 202 CPUFREQ_RELATION_H);
4471a34f 203 } else {
d10b5eb5 204 dbs_check_cpu(policy);
43e0ee36 205 if (dbs_info->freq_lo) {
4471a34f 206 /* Setup timer for SUB_SAMPLE */
43e0ee36
VK
207 dbs_info->sample_type = OD_SUB_SAMPLE;
208 delay = dbs_info->freq_hi_jiffies;
4471a34f
VK
209 }
210 }
211
9d445920
VK
212 if (!delay)
213 delay = delay_for_sampling_rate(od_tuners->sampling_rate
43e0ee36 214 * dbs_info->rate_mult);
9d445920 215
43e0ee36 216 return delay;
da53d61e
FB
217}
218
4471a34f 219/************************** sysfs interface ************************/
7bdad34d 220static struct dbs_governor od_dbs_gov;
1da177e4 221
fd0ef7a0
MH
222/**
223 * update_sampling_rate - update sampling rate effective immediately if needed.
224 * @new_rate: new sampling rate
225 *
06eb09d1 226 * If new rate is smaller than the old, simply updating
4471a34f
VK
227 * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
228 * original sampling_rate was 1 second and the requested new sampling rate is 10
229 * ms because the user needs immediate reaction from ondemand governor, but not
230 * sure if higher frequency will be required or not, then, the governor may
231 * change the sampling rate too late; up to 1 second later. Thus, if we are
232 * reducing the sampling rate, we need to make the new value effective
233 * immediately.
fd0ef7a0 234 */
4d5dcc42
VK
235static void update_sampling_rate(struct dbs_data *dbs_data,
236 unsigned int new_rate)
fd0ef7a0 237{
4d5dcc42 238 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
f08f638b 239 struct cpumask cpumask;
fd0ef7a0
MH
240 int cpu;
241
4d5dcc42
VK
242 od_tuners->sampling_rate = new_rate = max(new_rate,
243 dbs_data->min_sampling_rate);
fd0ef7a0 244
e128c864
VK
245 /*
246 * Lock governor so that governor start/stop can't execute in parallel.
247 */
2bb8d94f 248 mutex_lock(&dbs_data_mutex);
e128c864 249
f08f638b
VK
250 cpumask_copy(&cpumask, cpu_online_mask);
251
252 for_each_cpu(cpu, &cpumask) {
fd0ef7a0 253 struct cpufreq_policy *policy;
4471a34f 254 struct od_cpu_dbs_info_s *dbs_info;
e128c864 255 struct cpu_dbs_info *cdbs;
e40e7b25 256 struct policy_dbs_info *policy_dbs;
fd0ef7a0 257
e128c864
VK
258 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
259 cdbs = &dbs_info->cdbs;
e40e7b25 260 policy_dbs = cdbs->policy_dbs;
e128c864
VK
261
262 /*
e40e7b25
RW
263 * A valid policy_dbs and policy_dbs->policy means governor
264 * hasn't stopped or exited yet.
e128c864 265 */
e40e7b25 266 if (!policy_dbs || !policy_dbs->policy)
fd0ef7a0 267 continue;
e128c864 268
e40e7b25 269 policy = policy_dbs->policy;
e128c864 270
f08f638b
VK
271 /* clear all CPUs of this policy */
272 cpumask_andnot(&cpumask, &cpumask, policy->cpus);
273
e128c864
VK
274 /*
275 * Update sampling rate for CPUs whose policy is governed by
276 * dbs_data. In case of governor_per_policy, only a single
277 * policy will be governed by dbs_data, otherwise there can be
278 * multiple policies that are governed by the same dbs_data.
279 */
9be4fd2c 280 if (dbs_data == policy->governor_data) {
e40e7b25 281 mutex_lock(&policy_dbs->timer_mutex);
9be4fd2c
RW
282 /*
283 * On 32-bit architectures this may race with the
284 * sample_delay_ns read in dbs_update_util_handler(),
285 * but that really doesn't matter. If the read returns
286 * a value that's too big, the sample will be skipped,
287 * but the next invocation of dbs_update_util_handler()
288 * (when the update has been completed) will take a
289 * sample. If the returned value is too small, the
290 * sample will be taken immediately, but that isn't a
291 * problem, as we want the new rate to take effect
292 * immediately anyway.
293 *
294 * If this runs in parallel with dbs_work_handler(), we
295 * may end up overwriting the sample_delay_ns value that
296 * it has just written, but the difference should not be
297 * too big and it will be corrected next time a sample
298 * is taken, so it shouldn't be significant.
299 */
e40e7b25
RW
300 gov_update_sample_delay(policy_dbs, new_rate);
301 mutex_unlock(&policy_dbs->timer_mutex);
fd0ef7a0 302 }
fd0ef7a0 303 }
e128c864 304
2bb8d94f 305 mutex_unlock(&dbs_data_mutex);
fd0ef7a0
MH
306}
307
4d5dcc42
VK
308static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
309 size_t count)
1da177e4
LT
310{
311 unsigned int input;
312 int ret;
ffac80e9 313 ret = sscanf(buf, "%u", &input);
5a75c828 314 if (ret != 1)
315 return -EINVAL;
4d5dcc42
VK
316
317 update_sampling_rate(dbs_data, input);
1da177e4
LT
318 return count;
319}
320
4d5dcc42
VK
321static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
322 size_t count)
19379b11 323{
4d5dcc42 324 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
19379b11
AV
325 unsigned int input;
326 int ret;
9366d840 327 unsigned int j;
19379b11
AV
328
329 ret = sscanf(buf, "%u", &input);
330 if (ret != 1)
331 return -EINVAL;
4d5dcc42 332 od_tuners->io_is_busy = !!input;
9366d840
SK
333
334 /* we need to re-evaluate prev_cpu_idle */
335 for_each_online_cpu(j) {
336 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
337 j);
338 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
339 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
340 }
19379b11
AV
341 return count;
342}
343
4d5dcc42
VK
344static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
345 size_t count)
1da177e4 346{
4d5dcc42 347 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
1da177e4
LT
348 unsigned int input;
349 int ret;
ffac80e9 350 ret = sscanf(buf, "%u", &input);
1da177e4 351
32ee8c3e 352 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
c29f1403 353 input < MIN_FREQUENCY_UP_THRESHOLD) {
1da177e4
LT
354 return -EINVAL;
355 }
4bd4e428 356
4d5dcc42 357 od_tuners->up_threshold = input;
1da177e4
LT
358 return count;
359}
360
4d5dcc42
VK
361static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
362 const char *buf, size_t count)
3f78a9f7 363{
4d5dcc42 364 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
3f78a9f7
DN
365 unsigned int input, j;
366 int ret;
367 ret = sscanf(buf, "%u", &input);
368
369 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
370 return -EINVAL;
4d5dcc42 371 od_tuners->sampling_down_factor = input;
3f78a9f7
DN
372
373 /* Reset down sampling multiplier in case it was active */
374 for_each_online_cpu(j) {
4471a34f
VK
375 struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
376 j);
3f78a9f7
DN
377 dbs_info->rate_mult = 1;
378 }
3f78a9f7
DN
379 return count;
380}
381
6c4640c3
VK
382static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
383 const char *buf, size_t count)
3d5ee9e5 384{
4d5dcc42 385 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
3d5ee9e5
DJ
386 unsigned int input;
387 int ret;
388
389 unsigned int j;
32ee8c3e 390
ffac80e9 391 ret = sscanf(buf, "%u", &input);
2b03f891 392 if (ret != 1)
3d5ee9e5
DJ
393 return -EINVAL;
394
2b03f891 395 if (input > 1)
3d5ee9e5 396 input = 1;
32ee8c3e 397
6c4640c3 398 if (input == od_tuners->ignore_nice_load) { /* nothing to do */
3d5ee9e5
DJ
399 return count;
400 }
6c4640c3 401 od_tuners->ignore_nice_load = input;
3d5ee9e5 402
ccb2fe20 403 /* we need to re-evaluate prev_cpu_idle */
dac1c1a5 404 for_each_online_cpu(j) {
4471a34f 405 struct od_cpu_dbs_info_s *dbs_info;
245b2e70 406 dbs_info = &per_cpu(od_cpu_dbs_info, j);
4471a34f 407 dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
9366d840 408 &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
6c4640c3 409 if (od_tuners->ignore_nice_load)
4471a34f
VK
410 dbs_info->cdbs.prev_cpu_nice =
411 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
1ca3abdb 412
3d5ee9e5 413 }
3d5ee9e5
DJ
414 return count;
415}
416
4d5dcc42
VK
417static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
418 size_t count)
05ca0350 419{
4d5dcc42 420 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
05ca0350
AS
421 unsigned int input;
422 int ret;
423 ret = sscanf(buf, "%u", &input);
424
425 if (ret != 1)
426 return -EINVAL;
427
428 if (input > 1000)
429 input = 1000;
430
4d5dcc42 431 od_tuners->powersave_bias = input;
05ca0350 432 ondemand_powersave_bias_init();
05ca0350
AS
433 return count;
434}
435
4d5dcc42
VK
436show_store_one(od, sampling_rate);
437show_store_one(od, io_is_busy);
438show_store_one(od, up_threshold);
439show_store_one(od, sampling_down_factor);
6c4640c3 440show_store_one(od, ignore_nice_load);
4d5dcc42
VK
441show_store_one(od, powersave_bias);
442declare_show_sampling_rate_min(od);
443
444gov_sys_pol_attr_rw(sampling_rate);
445gov_sys_pol_attr_rw(io_is_busy);
446gov_sys_pol_attr_rw(up_threshold);
447gov_sys_pol_attr_rw(sampling_down_factor);
6c4640c3 448gov_sys_pol_attr_rw(ignore_nice_load);
4d5dcc42
VK
449gov_sys_pol_attr_rw(powersave_bias);
450gov_sys_pol_attr_ro(sampling_rate_min);
451
452static struct attribute *dbs_attributes_gov_sys[] = {
453 &sampling_rate_min_gov_sys.attr,
454 &sampling_rate_gov_sys.attr,
455 &up_threshold_gov_sys.attr,
456 &sampling_down_factor_gov_sys.attr,
6c4640c3 457 &ignore_nice_load_gov_sys.attr,
4d5dcc42
VK
458 &powersave_bias_gov_sys.attr,
459 &io_is_busy_gov_sys.attr,
1da177e4
LT
460 NULL
461};
462
4d5dcc42
VK
463static struct attribute_group od_attr_group_gov_sys = {
464 .attrs = dbs_attributes_gov_sys,
465 .name = "ondemand",
466};
467
468static struct attribute *dbs_attributes_gov_pol[] = {
469 &sampling_rate_min_gov_pol.attr,
470 &sampling_rate_gov_pol.attr,
471 &up_threshold_gov_pol.attr,
472 &sampling_down_factor_gov_pol.attr,
6c4640c3 473 &ignore_nice_load_gov_pol.attr,
4d5dcc42
VK
474 &powersave_bias_gov_pol.attr,
475 &io_is_busy_gov_pol.attr,
476 NULL
477};
478
479static struct attribute_group od_attr_group_gov_pol = {
480 .attrs = dbs_attributes_gov_pol,
1da177e4
LT
481 .name = "ondemand",
482};
483
484/************************** sysfs end ************************/
485
8e0484d2 486static int od_init(struct dbs_data *dbs_data, bool notify)
4d5dcc42
VK
487{
488 struct od_dbs_tuners *tuners;
489 u64 idle_time;
490 int cpu;
491
d5b73cd8 492 tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
4d5dcc42
VK
493 if (!tuners) {
494 pr_err("%s: kzalloc failed\n", __func__);
495 return -ENOMEM;
496 }
497
498 cpu = get_cpu();
499 idle_time = get_cpu_idle_time_us(cpu, NULL);
500 put_cpu();
501 if (idle_time != -1ULL) {
502 /* Idle micro accounting is supported. Use finer thresholds */
503 tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
4d5dcc42
VK
504 /*
505 * In nohz/micro accounting case we set the minimum frequency
506 * not depending on HZ, but fixed (very low). The deferred
507 * timer might skip some samples if idle/sleeping as needed.
508 */
509 dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
510 } else {
511 tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
4d5dcc42
VK
512
513 /* For correct statistics, we need 10 ticks for each measure */
514 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
515 jiffies_to_usecs(10);
516 }
517
518 tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
6c4640c3 519 tuners->ignore_nice_load = 0;
c2837558 520 tuners->powersave_bias = default_powersave_bias;
4d5dcc42
VK
521 tuners->io_is_busy = should_io_be_busy();
522
523 dbs_data->tuners = tuners;
4d5dcc42
VK
524 return 0;
525}
526
8e0484d2 527static void od_exit(struct dbs_data *dbs_data, bool notify)
4d5dcc42
VK
528{
529 kfree(dbs_data->tuners);
530}
531
4471a34f 532define_get_cpu_dbs_routines(od_cpu_dbs_info);
6b8fcd90 533
4471a34f 534static struct od_ops od_ops = {
4471a34f 535 .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
fb30809e 536 .powersave_bias_target = generic_powersave_bias_target,
4471a34f
VK
537 .freq_increase = dbs_freq_increase,
538};
2f8a835c 539
7bdad34d 540static struct dbs_governor od_dbs_gov = {
af926185
RW
541 .gov = {
542 .name = "ondemand",
906a6e5a 543 .governor = cpufreq_governor_dbs,
af926185
RW
544 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
545 .owner = THIS_MODULE,
546 },
4471a34f 547 .governor = GOV_ONDEMAND,
4d5dcc42
VK
548 .attr_group_gov_sys = &od_attr_group_gov_sys,
549 .attr_group_gov_pol = &od_attr_group_gov_pol,
4471a34f
VK
550 .get_cpu_cdbs = get_cpu_cdbs,
551 .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
552 .gov_dbs_timer = od_dbs_timer,
553 .gov_check_cpu = od_check_cpu,
554 .gov_ops = &od_ops,
4d5dcc42
VK
555 .init = od_init,
556 .exit = od_exit,
4471a34f 557};
1da177e4 558
7bdad34d 559#define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
af926185 560
fb30809e
JS
561static void od_set_powersave_bias(unsigned int powersave_bias)
562{
563 struct cpufreq_policy *policy;
564 struct dbs_data *dbs_data;
565 struct od_dbs_tuners *od_tuners;
566 unsigned int cpu;
567 cpumask_t done;
568
c2837558 569 default_powersave_bias = powersave_bias;
fb30809e
JS
570 cpumask_clear(&done);
571
572 get_online_cpus();
573 for_each_online_cpu(cpu) {
e40e7b25 574 struct policy_dbs_info *policy_dbs;
44152cb8 575
fb30809e
JS
576 if (cpumask_test_cpu(cpu, &done))
577 continue;
578
e40e7b25
RW
579 policy_dbs = per_cpu(od_cpu_dbs_info, cpu).cdbs.policy_dbs;
580 if (!policy_dbs)
c2837558 581 continue;
fb30809e 582
e40e7b25 583 policy = policy_dbs->policy;
fb30809e 584 cpumask_or(&done, &done, policy->cpus);
c2837558 585
af926185 586 if (policy->governor != CPU_FREQ_GOV_ONDEMAND)
c2837558
JS
587 continue;
588
589 dbs_data = policy->governor_data;
590 od_tuners = dbs_data->tuners;
591 od_tuners->powersave_bias = default_powersave_bias;
fb30809e
JS
592 }
593 put_online_cpus();
594}
595
596void od_register_powersave_bias_handler(unsigned int (*f)
597 (struct cpufreq_policy *, unsigned int, unsigned int),
598 unsigned int powersave_bias)
599{
600 od_ops.powersave_bias_target = f;
601 od_set_powersave_bias(powersave_bias);
602}
603EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
604
605void od_unregister_powersave_bias_handler(void)
606{
607 od_ops.powersave_bias_target = generic_powersave_bias_target;
608 od_set_powersave_bias(0);
609}
610EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
611
1da177e4
LT
612static int __init cpufreq_gov_dbs_init(void)
613{
af926185 614 return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
1da177e4
LT
615}
616
617static void __exit cpufreq_gov_dbs_exit(void)
618{
af926185 619 cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
1da177e4
LT
620}
621
ffac80e9
VP
622MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
623MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
624MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
2b03f891 625 "Low Latency Frequency Transition capable processors");
ffac80e9 626MODULE_LICENSE("GPL");
1da177e4 627
6915719b 628#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
de1df26b
RW
629struct cpufreq_governor *cpufreq_default_governor(void)
630{
af926185 631 return CPU_FREQ_GOV_ONDEMAND;
de1df26b
RW
632}
633
6915719b
JW
634fs_initcall(cpufreq_gov_dbs_init);
635#else
1da177e4 636module_init(cpufreq_gov_dbs_init);
6915719b 637#endif
1da177e4 638module_exit(cpufreq_gov_dbs_exit);