[CPUFREQ] Mark policy_rwsem as going static in cpufreq.c wont be exported
[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
13#include <linux/kernel.h>
14#include <linux/module.h>
1da177e4 15#include <linux/init.h>
1da177e4 16#include <linux/cpufreq.h>
138a0128 17#include <linux/cpu.h>
1da177e4
LT
18#include <linux/jiffies.h>
19#include <linux/kernel_stat.h>
3fc54d37 20#include <linux/mutex.h>
80800913 21#include <linux/hrtimer.h>
22#include <linux/tick.h>
23#include <linux/ktime.h>
9411b4ef 24#include <linux/sched.h>
1da177e4
LT
25
26/*
27 * dbs is used in this file as a shortform for demandbased switching
28 * It helps to keep variable names smaller, simpler
29 */
30
e9d95bf7 31#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
1da177e4 32#define DEF_FREQUENCY_UP_THRESHOLD (80)
80800913 33#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
34#define MICRO_FREQUENCY_UP_THRESHOLD (95)
cef9615a 35#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
c29f1403 36#define MIN_FREQUENCY_UP_THRESHOLD (11)
1da177e4
LT
37#define MAX_FREQUENCY_UP_THRESHOLD (100)
38
32ee8c3e
DJ
39/*
40 * The polling frequency of this governor depends on the capability of
1da177e4 41 * the processor. Default polling frequency is 1000 times the transition
32ee8c3e
DJ
42 * latency of the processor. The governor will work on any processor with
43 * transition latency <= 10mS, using appropriate sampling
1da177e4
LT
44 * rate.
45 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
46 * this governor will not work.
47 * All times here are in uS.
48 */
df8b59be 49#define MIN_SAMPLING_RATE_RATIO (2)
112124ab 50
cef9615a
TR
51static unsigned int min_sampling_rate;
52
112124ab 53#define LATENCY_MULTIPLIER (1000)
cef9615a 54#define MIN_LATENCY_MULTIPLIER (100)
1c256245 55#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
1da177e4 56
c4028958
DH
57static void do_dbs_timer(struct work_struct *work);
58
59/* Sampling types */
529af7a1 60enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
1da177e4
LT
61
62struct cpu_dbs_info_s {
ccb2fe20
VP
63 cputime64_t prev_cpu_idle;
64 cputime64_t prev_cpu_wall;
80800913 65 cputime64_t prev_cpu_nice;
32ee8c3e 66 struct cpufreq_policy *cur_policy;
2b03f891 67 struct delayed_work work;
05ca0350
AS
68 struct cpufreq_frequency_table *freq_table;
69 unsigned int freq_lo;
70 unsigned int freq_lo_jiffies;
71 unsigned int freq_hi_jiffies;
529af7a1
VP
72 int cpu;
73 unsigned int enable:1,
2b03f891 74 sample_type:1;
1da177e4
LT
75};
76static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
77
78static unsigned int dbs_enable; /* number of CPUs using this policy */
79
4ec223d0 80/*
7d26e2d5 81 * dbs_mutex protects data in dbs_tuners_ins from concurrent changes on
82 * different CPUs. It protects dbs_enable in governor start/stop. It also
83 * serializes governor limit_change with do_dbs_timer. We do not want
84 * do_dbs_timer to run when user is changing the governor or limits.
4ec223d0 85 */
ffac80e9 86static DEFINE_MUTEX(dbs_mutex);
1da177e4 87
2f8a835c 88static struct workqueue_struct *kondemand_wq;
6810b548 89
05ca0350 90static struct dbs_tuners {
32ee8c3e 91 unsigned int sampling_rate;
32ee8c3e 92 unsigned int up_threshold;
e9d95bf7 93 unsigned int down_differential;
32ee8c3e 94 unsigned int ignore_nice;
05ca0350
AS
95 unsigned int powersave_bias;
96} dbs_tuners_ins = {
32ee8c3e 97 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
e9d95bf7 98 .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
9cbad61b 99 .ignore_nice = 0,
05ca0350 100 .powersave_bias = 0,
1da177e4
LT
101};
102
80800913 103static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
104 cputime64_t *wall)
dac1c1a5 105{
ea487615 106 cputime64_t idle_time;
3430502d 107 cputime64_t cur_wall_time;
ea487615 108 cputime64_t busy_time;
ccb2fe20 109
3430502d 110 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
ea487615
VP
111 busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
112 kstat_cpu(cpu).cpustat.system);
ccb2fe20 113
ea487615
VP
114 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
115 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
116 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
1ca3abdb 117 busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
ea487615 118
3430502d 119 idle_time = cputime64_sub(cur_wall_time, busy_time);
120 if (wall)
121 *wall = cur_wall_time;
122
ea487615 123 return idle_time;
dac1c1a5
DJ
124}
125
80800913 126static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
127{
128 u64 idle_time = get_cpu_idle_time_us(cpu, wall);
129
130 if (idle_time == -1ULL)
131 return get_cpu_idle_time_jiffy(cpu, wall);
132
80800913 133 return idle_time;
134}
135
05ca0350
AS
136/*
137 * Find right freq to be set now with powersave_bias on.
138 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
139 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
140 */
b5ecf60f
AB
141static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
142 unsigned int freq_next,
143 unsigned int relation)
05ca0350
AS
144{
145 unsigned int freq_req, freq_reduc, freq_avg;
146 unsigned int freq_hi, freq_lo;
147 unsigned int index = 0;
148 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
149 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
150
151 if (!dbs_info->freq_table) {
152 dbs_info->freq_lo = 0;
153 dbs_info->freq_lo_jiffies = 0;
154 return freq_next;
155 }
156
157 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
158 relation, &index);
159 freq_req = dbs_info->freq_table[index].frequency;
160 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
161 freq_avg = freq_req - freq_reduc;
162
163 /* Find freq bounds for freq_avg in freq_table */
164 index = 0;
165 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
166 CPUFREQ_RELATION_H, &index);
167 freq_lo = dbs_info->freq_table[index].frequency;
168 index = 0;
169 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
170 CPUFREQ_RELATION_L, &index);
171 freq_hi = dbs_info->freq_table[index].frequency;
172
173 /* Find out how long we have to be in hi and lo freqs */
174 if (freq_hi == freq_lo) {
175 dbs_info->freq_lo = 0;
176 dbs_info->freq_lo_jiffies = 0;
177 return freq_lo;
178 }
179 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
180 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
181 jiffies_hi += ((freq_hi - freq_lo) / 2);
182 jiffies_hi /= (freq_hi - freq_lo);
183 jiffies_lo = jiffies_total - jiffies_hi;
184 dbs_info->freq_lo = freq_lo;
185 dbs_info->freq_lo_jiffies = jiffies_lo;
186 dbs_info->freq_hi_jiffies = jiffies_hi;
187 return freq_hi;
188}
189
190static void ondemand_powersave_bias_init(void)
191{
192 int i;
193 for_each_online_cpu(i) {
194 struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
195 dbs_info->freq_table = cpufreq_frequency_get_table(i);
196 dbs_info->freq_lo = 0;
197 }
198}
199
1da177e4
LT
200/************************** sysfs interface ************************/
201static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
202{
4f4d1ad6
TR
203 printk_once(KERN_INFO "CPUFREQ: ondemand sampling_rate_max "
204 "sysfs file is deprecated - used by: %s\n", current->comm);
cef9615a 205 return sprintf(buf, "%u\n", -1U);
1da177e4
LT
206}
207
208static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
209{
cef9615a 210 return sprintf(buf, "%u\n", min_sampling_rate);
1da177e4
LT
211}
212
32ee8c3e
DJ
213#define define_one_ro(_name) \
214static struct freq_attr _name = \
1da177e4
LT
215__ATTR(_name, 0444, show_##_name, NULL)
216
217define_one_ro(sampling_rate_max);
218define_one_ro(sampling_rate_min);
219
220/* cpufreq_ondemand Governor Tunables */
221#define show_one(file_name, object) \
222static ssize_t show_##file_name \
223(struct cpufreq_policy *unused, char *buf) \
224{ \
225 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
226}
227show_one(sampling_rate, sampling_rate);
1da177e4 228show_one(up_threshold, up_threshold);
001893cd 229show_one(ignore_nice_load, ignore_nice);
05ca0350 230show_one(powersave_bias, powersave_bias);
1da177e4 231
32ee8c3e 232static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
1da177e4
LT
233 const char *buf, size_t count)
234{
235 unsigned int input;
236 int ret;
ffac80e9 237 ret = sscanf(buf, "%u", &input);
1da177e4 238
3fc54d37 239 mutex_lock(&dbs_mutex);
112124ab 240 if (ret != 1) {
3fc54d37 241 mutex_unlock(&dbs_mutex);
1da177e4
LT
242 return -EINVAL;
243 }
cef9615a 244 dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
3fc54d37 245 mutex_unlock(&dbs_mutex);
1da177e4
LT
246
247 return count;
248}
249
32ee8c3e 250static ssize_t store_up_threshold(struct cpufreq_policy *unused,
1da177e4
LT
251 const char *buf, size_t count)
252{
253 unsigned int input;
254 int ret;
ffac80e9 255 ret = sscanf(buf, "%u", &input);
1da177e4 256
3fc54d37 257 mutex_lock(&dbs_mutex);
32ee8c3e 258 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
c29f1403 259 input < MIN_FREQUENCY_UP_THRESHOLD) {
3fc54d37 260 mutex_unlock(&dbs_mutex);
1da177e4
LT
261 return -EINVAL;
262 }
263
264 dbs_tuners_ins.up_threshold = input;
3fc54d37 265 mutex_unlock(&dbs_mutex);
1da177e4
LT
266
267 return count;
268}
269
001893cd 270static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
3d5ee9e5
DJ
271 const char *buf, size_t count)
272{
273 unsigned int input;
274 int ret;
275
276 unsigned int j;
32ee8c3e 277
ffac80e9 278 ret = sscanf(buf, "%u", &input);
2b03f891 279 if (ret != 1)
3d5ee9e5
DJ
280 return -EINVAL;
281
2b03f891 282 if (input > 1)
3d5ee9e5 283 input = 1;
32ee8c3e 284
3fc54d37 285 mutex_lock(&dbs_mutex);
2b03f891 286 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
3fc54d37 287 mutex_unlock(&dbs_mutex);
3d5ee9e5
DJ
288 return count;
289 }
290 dbs_tuners_ins.ignore_nice = input;
291
ccb2fe20 292 /* we need to re-evaluate prev_cpu_idle */
dac1c1a5 293 for_each_online_cpu(j) {
ccb2fe20
VP
294 struct cpu_dbs_info_s *dbs_info;
295 dbs_info = &per_cpu(cpu_dbs_info, j);
3430502d 296 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
297 &dbs_info->prev_cpu_wall);
1ca3abdb
VP
298 if (dbs_tuners_ins.ignore_nice)
299 dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
300
3d5ee9e5 301 }
3fc54d37 302 mutex_unlock(&dbs_mutex);
3d5ee9e5
DJ
303
304 return count;
305}
306
05ca0350
AS
307static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
308 const char *buf, size_t count)
309{
310 unsigned int input;
311 int ret;
312 ret = sscanf(buf, "%u", &input);
313
314 if (ret != 1)
315 return -EINVAL;
316
317 if (input > 1000)
318 input = 1000;
319
320 mutex_lock(&dbs_mutex);
321 dbs_tuners_ins.powersave_bias = input;
322 ondemand_powersave_bias_init();
323 mutex_unlock(&dbs_mutex);
324
325 return count;
326}
327
1da177e4
LT
328#define define_one_rw(_name) \
329static struct freq_attr _name = \
330__ATTR(_name, 0644, show_##_name, store_##_name)
331
332define_one_rw(sampling_rate);
1da177e4 333define_one_rw(up_threshold);
001893cd 334define_one_rw(ignore_nice_load);
05ca0350 335define_one_rw(powersave_bias);
1da177e4 336
2b03f891 337static struct attribute *dbs_attributes[] = {
1da177e4
LT
338 &sampling_rate_max.attr,
339 &sampling_rate_min.attr,
340 &sampling_rate.attr,
1da177e4 341 &up_threshold.attr,
001893cd 342 &ignore_nice_load.attr,
05ca0350 343 &powersave_bias.attr,
1da177e4
LT
344 NULL
345};
346
347static struct attribute_group dbs_attr_group = {
348 .attrs = dbs_attributes,
349 .name = "ondemand",
350};
351
352/************************** sysfs end ************************/
353
2f8a835c 354static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
1da177e4 355{
c43aa3bd 356 unsigned int max_load_freq;
1da177e4
LT
357
358 struct cpufreq_policy *policy;
359 unsigned int j;
360
1da177e4
LT
361 if (!this_dbs_info->enable)
362 return;
363
05ca0350 364 this_dbs_info->freq_lo = 0;
1da177e4 365 policy = this_dbs_info->cur_policy;
ea487615 366
32ee8c3e 367 /*
c29f1403
DJ
368 * Every sampling_rate, we check, if current idle time is less
369 * than 20% (default), then we try to increase frequency
ccb2fe20 370 * Every sampling_rate, we look for a the lowest
c29f1403
DJ
371 * frequency which can sustain the load while keeping idle time over
372 * 30%. If such a frequency exist, we try to decrease to this frequency.
1da177e4 373 *
32ee8c3e
DJ
374 * Any frequency increase takes it to the maximum frequency.
375 * Frequency reduction happens at minimum steps of
376 * 5% (default) of current frequency
1da177e4
LT
377 */
378
c43aa3bd 379 /* Get Absolute Load - in terms of freq */
380 max_load_freq = 0;
381
835481d9 382 for_each_cpu(j, policy->cpus) {
1da177e4 383 struct cpu_dbs_info_s *j_dbs_info;
c43aa3bd 384 cputime64_t cur_wall_time, cur_idle_time;
385 unsigned int idle_time, wall_time;
386 unsigned int load, load_freq;
387 int freq_avg;
1da177e4 388
1da177e4 389 j_dbs_info = &per_cpu(cpu_dbs_info, j);
3430502d 390
391 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
392
c43aa3bd 393 wall_time = (unsigned int) cputime64_sub(cur_wall_time,
394 j_dbs_info->prev_cpu_wall);
395 j_dbs_info->prev_cpu_wall = cur_wall_time;
396
c43aa3bd 397 idle_time = (unsigned int) cputime64_sub(cur_idle_time,
ccb2fe20 398 j_dbs_info->prev_cpu_idle);
c43aa3bd 399 j_dbs_info->prev_cpu_idle = cur_idle_time;
1da177e4 400
1ca3abdb
VP
401 if (dbs_tuners_ins.ignore_nice) {
402 cputime64_t cur_nice;
403 unsigned long cur_nice_jiffies;
404
405 cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
406 j_dbs_info->prev_cpu_nice);
407 /*
408 * Assumption: nice time between sampling periods will
409 * be less than 2^32 jiffies for 32 bit sys
410 */
411 cur_nice_jiffies = (unsigned long)
412 cputime64_to_jiffies64(cur_nice);
413
414 j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
415 idle_time += jiffies_to_usecs(cur_nice_jiffies);
416 }
417
3430502d 418 if (unlikely(!wall_time || wall_time < idle_time))
c43aa3bd 419 continue;
c43aa3bd 420
421 load = 100 * (wall_time - idle_time) / wall_time;
422
423 freq_avg = __cpufreq_driver_getavg(policy, j);
424 if (freq_avg <= 0)
425 freq_avg = policy->cur;
426
427 load_freq = load * freq_avg;
428 if (load_freq > max_load_freq)
429 max_load_freq = load_freq;
1da177e4
LT
430 }
431
ccb2fe20 432 /* Check for frequency increase */
c43aa3bd 433 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
c11420a6 434 /* if we are already at full speed then break out early */
05ca0350
AS
435 if (!dbs_tuners_ins.powersave_bias) {
436 if (policy->cur == policy->max)
437 return;
438
439 __cpufreq_driver_target(policy, policy->max,
440 CPUFREQ_RELATION_H);
441 } else {
442 int freq = powersave_bias_target(policy, policy->max,
443 CPUFREQ_RELATION_H);
444 __cpufreq_driver_target(policy, freq,
445 CPUFREQ_RELATION_L);
446 }
1da177e4
LT
447 return;
448 }
449
450 /* Check for frequency decrease */
c29f1403
DJ
451 /* if we cannot reduce the frequency anymore, break out early */
452 if (policy->cur == policy->min)
453 return;
1da177e4 454
c29f1403
DJ
455 /*
456 * The optimal frequency is the frequency that is the lowest that
457 * can support the current CPU usage without triggering the up
458 * policy. To be safe, we focus 10 points under the threshold.
459 */
e9d95bf7 460 if (max_load_freq <
461 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
462 policy->cur) {
c43aa3bd 463 unsigned int freq_next;
e9d95bf7 464 freq_next = max_load_freq /
465 (dbs_tuners_ins.up_threshold -
466 dbs_tuners_ins.down_differential);
dfde5d62 467
05ca0350
AS
468 if (!dbs_tuners_ins.powersave_bias) {
469 __cpufreq_driver_target(policy, freq_next,
470 CPUFREQ_RELATION_L);
471 } else {
472 int freq = powersave_bias_target(policy, freq_next,
473 CPUFREQ_RELATION_L);
474 __cpufreq_driver_target(policy, freq,
475 CPUFREQ_RELATION_L);
476 }
ccb2fe20 477 }
1da177e4
LT
478}
479
c4028958 480static void do_dbs_timer(struct work_struct *work)
32ee8c3e 481{
529af7a1
VP
482 struct cpu_dbs_info_s *dbs_info =
483 container_of(work, struct cpu_dbs_info_s, work.work);
484 unsigned int cpu = dbs_info->cpu;
485 int sample_type = dbs_info->sample_type;
486
1ce28d6b
AS
487 /* We want all CPUs to do sampling nearly on same jiffy */
488 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
c4028958 489
1ce28d6b 490 delay -= jiffies % delay;
2f8a835c 491
7d26e2d5 492 mutex_lock(&dbs_mutex);
56463b78
VP
493
494 if (!dbs_info->enable) {
7d26e2d5 495 mutex_unlock(&dbs_mutex);
56463b78
VP
496 return;
497 }
498
05ca0350 499 /* Common NORMAL_SAMPLE setup */
c4028958 500 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
05ca0350 501 if (!dbs_tuners_ins.powersave_bias ||
c4028958 502 sample_type == DBS_NORMAL_SAMPLE) {
05ca0350 503 dbs_check_cpu(dbs_info);
05ca0350
AS
504 if (dbs_info->freq_lo) {
505 /* Setup timer for SUB_SAMPLE */
c4028958 506 dbs_info->sample_type = DBS_SUB_SAMPLE;
05ca0350
AS
507 delay = dbs_info->freq_hi_jiffies;
508 }
509 } else {
510 __cpufreq_driver_target(dbs_info->cur_policy,
2b03f891 511 dbs_info->freq_lo, CPUFREQ_RELATION_H);
05ca0350 512 }
1ce28d6b 513 queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
7d26e2d5 514 mutex_unlock(&dbs_mutex);
32ee8c3e 515}
1da177e4 516
529af7a1 517static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
1da177e4 518{
1ce28d6b
AS
519 /* We want all CPUs to do sampling nearly on same jiffy */
520 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
521 delay -= jiffies % delay;
2f8a835c 522
c18a1483 523 dbs_info->enable = 1;
05ca0350 524 ondemand_powersave_bias_init();
c4028958 525 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
28287033 526 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
529af7a1 527 queue_delayed_work_on(dbs_info->cpu, kondemand_wq, &dbs_info->work,
2b03f891 528 delay);
1da177e4
LT
529}
530
2cd7cbdf 531static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
1da177e4 532{
2cd7cbdf 533 dbs_info->enable = 0;
b14893a6 534 cancel_delayed_work_sync(&dbs_info->work);
1da177e4
LT
535}
536
537static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
538 unsigned int event)
539{
540 unsigned int cpu = policy->cpu;
541 struct cpu_dbs_info_s *this_dbs_info;
542 unsigned int j;
914f7c31 543 int rc;
1da177e4
LT
544
545 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
546
547 switch (event) {
548 case CPUFREQ_GOV_START:
ffac80e9 549 if ((!cpu_online(cpu)) || (!policy->cur))
1da177e4
LT
550 return -EINVAL;
551
1da177e4
LT
552 if (this_dbs_info->enable) /* Already enabled */
553 break;
32ee8c3e 554
3fc54d37 555 mutex_lock(&dbs_mutex);
2f8a835c 556 dbs_enable++;
914f7c31
JG
557
558 rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
559 if (rc) {
914f7c31
JG
560 dbs_enable--;
561 mutex_unlock(&dbs_mutex);
562 return rc;
563 }
564
835481d9 565 for_each_cpu(j, policy->cpus) {
1da177e4
LT
566 struct cpu_dbs_info_s *j_dbs_info;
567 j_dbs_info = &per_cpu(cpu_dbs_info, j);
568 j_dbs_info->cur_policy = policy;
32ee8c3e 569
3430502d 570 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
571 &j_dbs_info->prev_cpu_wall);
1ca3abdb
VP
572 if (dbs_tuners_ins.ignore_nice) {
573 j_dbs_info->prev_cpu_nice =
574 kstat_cpu(j).cpustat.nice;
575 }
1da177e4 576 }
529af7a1 577 this_dbs_info->cpu = cpu;
1da177e4
LT
578 /*
579 * Start the timerschedule work, when this governor
580 * is used for first time
581 */
582 if (dbs_enable == 1) {
583 unsigned int latency;
584 /* policy latency is in nS. Convert it to uS first */
df8b59be
DJ
585 latency = policy->cpuinfo.transition_latency / 1000;
586 if (latency == 0)
587 latency = 1;
cef9615a
TR
588 /* Bring kernel and HW constraints together */
589 min_sampling_rate = max(min_sampling_rate,
590 MIN_LATENCY_MULTIPLIER * latency);
591 dbs_tuners_ins.sampling_rate =
592 max(min_sampling_rate,
593 latency * LATENCY_MULTIPLIER);
1da177e4 594 }
3fc54d37 595 mutex_unlock(&dbs_mutex);
7d26e2d5 596
597 dbs_timer_init(this_dbs_info);
1da177e4
LT
598 break;
599
600 case CPUFREQ_GOV_STOP:
2cd7cbdf 601 dbs_timer_exit(this_dbs_info);
7d26e2d5 602
603 mutex_lock(&dbs_mutex);
1da177e4
LT
604 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
605 dbs_enable--;
3fc54d37 606 mutex_unlock(&dbs_mutex);
1da177e4
LT
607
608 break;
609
610 case CPUFREQ_GOV_LIMITS:
3fc54d37 611 mutex_lock(&dbs_mutex);
1da177e4 612 if (policy->max < this_dbs_info->cur_policy->cur)
ffac80e9 613 __cpufreq_driver_target(this_dbs_info->cur_policy,
2b03f891 614 policy->max, CPUFREQ_RELATION_H);
1da177e4 615 else if (policy->min > this_dbs_info->cur_policy->cur)
ffac80e9 616 __cpufreq_driver_target(this_dbs_info->cur_policy,
2b03f891 617 policy->min, CPUFREQ_RELATION_L);
3fc54d37 618 mutex_unlock(&dbs_mutex);
1da177e4
LT
619 break;
620 }
621 return 0;
622}
623
c4d14bc0
SW
624#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
625static
626#endif
1c256245
TR
627struct cpufreq_governor cpufreq_gov_ondemand = {
628 .name = "ondemand",
629 .governor = cpufreq_governor_dbs,
630 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
631 .owner = THIS_MODULE,
1da177e4 632};
1da177e4
LT
633
634static int __init cpufreq_gov_dbs_init(void)
635{
888a794c 636 int err;
80800913 637 cputime64_t wall;
4f6e6b9f
AR
638 u64 idle_time;
639 int cpu = get_cpu();
80800913 640
4f6e6b9f
AR
641 idle_time = get_cpu_idle_time_us(cpu, &wall);
642 put_cpu();
80800913 643 if (idle_time != -1ULL) {
644 /* Idle micro accounting is supported. Use finer thresholds */
645 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
646 dbs_tuners_ins.down_differential =
647 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
cef9615a
TR
648 /*
649 * In no_hz/micro accounting case we set the minimum frequency
650 * not depending on HZ, but fixed (very low). The deferred
651 * timer might skip some samples if idle/sleeping as needed.
652 */
653 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
654 } else {
655 /* For correct statistics, we need 10 ticks for each measure */
656 min_sampling_rate =
657 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
80800913 658 }
888a794c 659
56463b78
VP
660 kondemand_wq = create_workqueue("kondemand");
661 if (!kondemand_wq) {
662 printk(KERN_ERR "Creation of kondemand failed\n");
663 return -EFAULT;
664 }
888a794c
AM
665 err = cpufreq_register_governor(&cpufreq_gov_ondemand);
666 if (err)
667 destroy_workqueue(kondemand_wq);
668
669 return err;
1da177e4
LT
670}
671
672static void __exit cpufreq_gov_dbs_exit(void)
673{
1c256245 674 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
56463b78 675 destroy_workqueue(kondemand_wq);
1da177e4
LT
676}
677
678
ffac80e9
VP
679MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
680MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
681MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
2b03f891 682 "Low Latency Frequency Transition capable processors");
ffac80e9 683MODULE_LICENSE("GPL");
1da177e4 684
6915719b
JW
685#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
686fs_initcall(cpufreq_gov_dbs_init);
687#else
1da177e4 688module_init(cpufreq_gov_dbs_init);
6915719b 689#endif
1da177e4 690module_exit(cpufreq_gov_dbs_exit);
This page took 0.413717 seconds and 5 git commands to generate.