]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/cpufreq/cpufreq_ondemand.c
[CPUFREQ] Add queue_delayed_work_on() interface for workqueues.
[mirror_ubuntu-artful-kernel.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>
15#include <linux/smp.h>
16#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/ctype.h>
19#include <linux/cpufreq.h>
20#include <linux/sysctl.h>
21#include <linux/types.h>
22#include <linux/fs.h>
23#include <linux/sysfs.h>
138a0128 24#include <linux/cpu.h>
1da177e4
LT
25#include <linux/sched.h>
26#include <linux/kmod.h>
27#include <linux/workqueue.h>
28#include <linux/jiffies.h>
29#include <linux/kernel_stat.h>
30#include <linux/percpu.h>
3fc54d37 31#include <linux/mutex.h>
1da177e4
LT
32
33/*
34 * dbs is used in this file as a shortform for demandbased switching
35 * It helps to keep variable names smaller, simpler
36 */
37
38#define DEF_FREQUENCY_UP_THRESHOLD (80)
c29f1403 39#define MIN_FREQUENCY_UP_THRESHOLD (11)
1da177e4
LT
40#define MAX_FREQUENCY_UP_THRESHOLD (100)
41
32ee8c3e
DJ
42/*
43 * The polling frequency of this governor depends on the capability of
1da177e4 44 * the processor. Default polling frequency is 1000 times the transition
32ee8c3e
DJ
45 * latency of the processor. The governor will work on any processor with
46 * transition latency <= 10mS, using appropriate sampling
1da177e4
LT
47 * rate.
48 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
49 * this governor will not work.
50 * All times here are in uS.
51 */
32ee8c3e 52static unsigned int def_sampling_rate;
df8b59be
DJ
53#define MIN_SAMPLING_RATE_RATIO (2)
54/* for correct statistics, we need at least 10 ticks between each measure */
55#define MIN_STAT_SAMPLING_RATE (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
56#define MIN_SAMPLING_RATE (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
1da177e4
LT
57#define MAX_SAMPLING_RATE (500 * def_sampling_rate)
58#define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
1da177e4 59#define TRANSITION_LATENCY_LIMIT (10 * 1000)
1da177e4
LT
60
61static void do_dbs_timer(void *data);
62
63struct cpu_dbs_info_s {
ccb2fe20
VP
64 cputime64_t prev_cpu_idle;
65 cputime64_t prev_cpu_wall;
32ee8c3e 66 struct cpufreq_policy *cur_policy;
32ee8c3e 67 unsigned int enable;
1da177e4
LT
68};
69static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
70
71static unsigned int dbs_enable; /* number of CPUs using this policy */
72
4ec223d0
VP
73/*
74 * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
75 * lock and dbs_mutex. cpu_hotplug lock should always be held before
76 * dbs_mutex. If any function that can potentially take cpu_hotplug lock
77 * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
78 * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
79 * is recursive for the same process. -Venki
80 */
32ee8c3e 81static DEFINE_MUTEX (dbs_mutex);
1da177e4
LT
82static DECLARE_WORK (dbs_work, do_dbs_timer, NULL);
83
6810b548
AK
84static struct workqueue_struct *dbs_workq;
85
1da177e4 86struct dbs_tuners {
32ee8c3e 87 unsigned int sampling_rate;
32ee8c3e
DJ
88 unsigned int up_threshold;
89 unsigned int ignore_nice;
1da177e4
LT
90};
91
92static struct dbs_tuners dbs_tuners_ins = {
32ee8c3e 93 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
9cbad61b 94 .ignore_nice = 0,
1da177e4
LT
95};
96
ccb2fe20 97static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
dac1c1a5 98{
ccb2fe20
VP
99 cputime64_t retval;
100
101 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
102 kstat_cpu(cpu).cpustat.iowait);
103
104 if (dbs_tuners_ins.ignore_nice)
105 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
106
107 return retval;
dac1c1a5
DJ
108}
109
1da177e4
LT
110/************************** sysfs interface ************************/
111static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
112{
113 return sprintf (buf, "%u\n", MAX_SAMPLING_RATE);
114}
115
116static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
117{
118 return sprintf (buf, "%u\n", MIN_SAMPLING_RATE);
119}
120
32ee8c3e
DJ
121#define define_one_ro(_name) \
122static struct freq_attr _name = \
1da177e4
LT
123__ATTR(_name, 0444, show_##_name, NULL)
124
125define_one_ro(sampling_rate_max);
126define_one_ro(sampling_rate_min);
127
128/* cpufreq_ondemand Governor Tunables */
129#define show_one(file_name, object) \
130static ssize_t show_##file_name \
131(struct cpufreq_policy *unused, char *buf) \
132{ \
133 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
134}
135show_one(sampling_rate, sampling_rate);
1da177e4 136show_one(up_threshold, up_threshold);
001893cd 137show_one(ignore_nice_load, ignore_nice);
1da177e4 138
32ee8c3e 139static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
1da177e4
LT
140 const char *buf, size_t count)
141{
142 unsigned int input;
143 int ret;
144 ret = sscanf (buf, "%u", &input);
145
3fc54d37 146 mutex_lock(&dbs_mutex);
1da177e4 147 if (ret != 1 || input > MAX_SAMPLING_RATE || input < MIN_SAMPLING_RATE) {
3fc54d37 148 mutex_unlock(&dbs_mutex);
1da177e4
LT
149 return -EINVAL;
150 }
151
152 dbs_tuners_ins.sampling_rate = input;
3fc54d37 153 mutex_unlock(&dbs_mutex);
1da177e4
LT
154
155 return count;
156}
157
32ee8c3e 158static ssize_t store_up_threshold(struct cpufreq_policy *unused,
1da177e4
LT
159 const char *buf, size_t count)
160{
161 unsigned int input;
162 int ret;
163 ret = sscanf (buf, "%u", &input);
164
3fc54d37 165 mutex_lock(&dbs_mutex);
32ee8c3e 166 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
c29f1403 167 input < MIN_FREQUENCY_UP_THRESHOLD) {
3fc54d37 168 mutex_unlock(&dbs_mutex);
1da177e4
LT
169 return -EINVAL;
170 }
171
172 dbs_tuners_ins.up_threshold = input;
3fc54d37 173 mutex_unlock(&dbs_mutex);
1da177e4
LT
174
175 return count;
176}
177
001893cd 178static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
3d5ee9e5
DJ
179 const char *buf, size_t count)
180{
181 unsigned int input;
182 int ret;
183
184 unsigned int j;
32ee8c3e 185
3d5ee9e5
DJ
186 ret = sscanf (buf, "%u", &input);
187 if ( ret != 1 )
188 return -EINVAL;
189
190 if ( input > 1 )
191 input = 1;
32ee8c3e 192
3fc54d37 193 mutex_lock(&dbs_mutex);
3d5ee9e5 194 if ( input == dbs_tuners_ins.ignore_nice ) { /* nothing to do */
3fc54d37 195 mutex_unlock(&dbs_mutex);
3d5ee9e5
DJ
196 return count;
197 }
198 dbs_tuners_ins.ignore_nice = input;
199
ccb2fe20 200 /* we need to re-evaluate prev_cpu_idle */
dac1c1a5 201 for_each_online_cpu(j) {
ccb2fe20
VP
202 struct cpu_dbs_info_s *dbs_info;
203 dbs_info = &per_cpu(cpu_dbs_info, j);
204 dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
205 dbs_info->prev_cpu_wall = get_jiffies_64();
3d5ee9e5 206 }
3fc54d37 207 mutex_unlock(&dbs_mutex);
3d5ee9e5
DJ
208
209 return count;
210}
211
1da177e4
LT
212#define define_one_rw(_name) \
213static struct freq_attr _name = \
214__ATTR(_name, 0644, show_##_name, store_##_name)
215
216define_one_rw(sampling_rate);
1da177e4 217define_one_rw(up_threshold);
001893cd 218define_one_rw(ignore_nice_load);
1da177e4
LT
219
220static struct attribute * dbs_attributes[] = {
221 &sampling_rate_max.attr,
222 &sampling_rate_min.attr,
223 &sampling_rate.attr,
1da177e4 224 &up_threshold.attr,
001893cd 225 &ignore_nice_load.attr,
1da177e4
LT
226 NULL
227};
228
229static struct attribute_group dbs_attr_group = {
230 .attrs = dbs_attributes,
231 .name = "ondemand",
232};
233
234/************************** sysfs end ************************/
235
236static void dbs_check_cpu(int cpu)
237{
ccb2fe20
VP
238 unsigned int idle_ticks, total_ticks;
239 unsigned int load;
1da177e4 240 struct cpu_dbs_info_s *this_dbs_info;
ccb2fe20 241 cputime64_t cur_jiffies;
1da177e4
LT
242
243 struct cpufreq_policy *policy;
244 unsigned int j;
245
246 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
247 if (!this_dbs_info->enable)
248 return;
249
250 policy = this_dbs_info->cur_policy;
ccb2fe20
VP
251 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
252 total_ticks = (unsigned int) cputime64_sub(cur_jiffies,
253 this_dbs_info->prev_cpu_wall);
254 this_dbs_info->prev_cpu_wall = cur_jiffies;
32ee8c3e 255 /*
c29f1403
DJ
256 * Every sampling_rate, we check, if current idle time is less
257 * than 20% (default), then we try to increase frequency
ccb2fe20 258 * Every sampling_rate, we look for a the lowest
c29f1403
DJ
259 * frequency which can sustain the load while keeping idle time over
260 * 30%. If such a frequency exist, we try to decrease to this frequency.
1da177e4 261 *
32ee8c3e
DJ
262 * Any frequency increase takes it to the maximum frequency.
263 * Frequency reduction happens at minimum steps of
264 * 5% (default) of current frequency
1da177e4
LT
265 */
266
ccb2fe20 267 /* Get Idle Time */
9c7d269b 268 idle_ticks = UINT_MAX;
1da177e4 269 for_each_cpu_mask(j, policy->cpus) {
ccb2fe20
VP
270 cputime64_t total_idle_ticks;
271 unsigned int tmp_idle_ticks;
1da177e4
LT
272 struct cpu_dbs_info_s *j_dbs_info;
273
1da177e4 274 j_dbs_info = &per_cpu(cpu_dbs_info, j);
dac1c1a5 275 total_idle_ticks = get_cpu_idle_time(j);
ccb2fe20
VP
276 tmp_idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
277 j_dbs_info->prev_cpu_idle);
278 j_dbs_info->prev_cpu_idle = total_idle_ticks;
1da177e4
LT
279
280 if (tmp_idle_ticks < idle_ticks)
281 idle_ticks = tmp_idle_ticks;
282 }
ccb2fe20 283 load = (100 * (total_ticks - idle_ticks)) / total_ticks;
1da177e4 284
ccb2fe20
VP
285 /* Check for frequency increase */
286 if (load > dbs_tuners_ins.up_threshold) {
c11420a6
DJ
287 /* if we are already at full speed then break out early */
288 if (policy->cur == policy->max)
289 return;
32ee8c3e
DJ
290
291 __cpufreq_driver_target(policy, policy->max,
1da177e4 292 CPUFREQ_RELATION_H);
1da177e4
LT
293 return;
294 }
295
296 /* Check for frequency decrease */
c29f1403
DJ
297 /* if we cannot reduce the frequency anymore, break out early */
298 if (policy->cur == policy->min)
299 return;
1da177e4 300
c29f1403
DJ
301 /*
302 * The optimal frequency is the frequency that is the lowest that
303 * can support the current CPU usage without triggering the up
304 * policy. To be safe, we focus 10 points under the threshold.
305 */
ccb2fe20
VP
306 if (load < (dbs_tuners_ins.up_threshold - 10)) {
307 unsigned int freq_next;
308 freq_next = (policy->cur * load) /
c29f1403 309 (dbs_tuners_ins.up_threshold - 10);
1da177e4 310
c29f1403 311 __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
ccb2fe20 312 }
1da177e4
LT
313}
314
315static void do_dbs_timer(void *data)
32ee8c3e 316{
1da177e4 317 int i;
4ec223d0 318 lock_cpu_hotplug();
3fc54d37 319 mutex_lock(&dbs_mutex);
6fe71165
DJ
320 for_each_online_cpu(i)
321 dbs_check_cpu(i);
6810b548
AK
322 queue_delayed_work(dbs_workq, &dbs_work,
323 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
3fc54d37 324 mutex_unlock(&dbs_mutex);
4ec223d0 325 unlock_cpu_hotplug();
32ee8c3e 326}
1da177e4
LT
327
328static inline void dbs_timer_init(void)
329{
330 INIT_WORK(&dbs_work, do_dbs_timer, NULL);
6810b548
AK
331 if (!dbs_workq)
332 dbs_workq = create_singlethread_workqueue("ondemand");
333 if (!dbs_workq) {
334 printk(KERN_ERR "ondemand: Cannot initialize kernel thread\n");
335 return;
336 }
337 queue_delayed_work(dbs_workq, &dbs_work,
338 usecs_to_jiffies(dbs_tuners_ins.sampling_rate));
1da177e4
LT
339 return;
340}
341
342static inline void dbs_timer_exit(void)
343{
6810b548
AK
344 if (dbs_workq)
345 cancel_rearming_delayed_workqueue(dbs_workq, &dbs_work);
1da177e4
LT
346}
347
348static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
349 unsigned int event)
350{
351 unsigned int cpu = policy->cpu;
352 struct cpu_dbs_info_s *this_dbs_info;
353 unsigned int j;
354
355 this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
356
357 switch (event) {
358 case CPUFREQ_GOV_START:
32ee8c3e 359 if ((!cpu_online(cpu)) ||
1da177e4
LT
360 (!policy->cur))
361 return -EINVAL;
362
363 if (policy->cpuinfo.transition_latency >
ff8c288d
EP
364 (TRANSITION_LATENCY_LIMIT * 1000)) {
365 printk(KERN_WARNING "ondemand governor failed to load "
366 "due to too long transition latency\n");
1da177e4 367 return -EINVAL;
ff8c288d 368 }
1da177e4
LT
369 if (this_dbs_info->enable) /* Already enabled */
370 break;
32ee8c3e 371
3fc54d37 372 mutex_lock(&dbs_mutex);
1da177e4
LT
373 for_each_cpu_mask(j, policy->cpus) {
374 struct cpu_dbs_info_s *j_dbs_info;
375 j_dbs_info = &per_cpu(cpu_dbs_info, j);
376 j_dbs_info->cur_policy = policy;
32ee8c3e 377
ccb2fe20
VP
378 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j);
379 j_dbs_info->prev_cpu_wall = get_jiffies_64();
1da177e4
LT
380 }
381 this_dbs_info->enable = 1;
382 sysfs_create_group(&policy->kobj, &dbs_attr_group);
383 dbs_enable++;
384 /*
385 * Start the timerschedule work, when this governor
386 * is used for first time
387 */
388 if (dbs_enable == 1) {
389 unsigned int latency;
390 /* policy latency is in nS. Convert it to uS first */
df8b59be
DJ
391 latency = policy->cpuinfo.transition_latency / 1000;
392 if (latency == 0)
393 latency = 1;
1da177e4 394
df8b59be 395 def_sampling_rate = latency *
1da177e4 396 DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
df8b59be
DJ
397
398 if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
399 def_sampling_rate = MIN_STAT_SAMPLING_RATE;
400
1da177e4 401 dbs_tuners_ins.sampling_rate = def_sampling_rate;
1da177e4
LT
402 dbs_timer_init();
403 }
32ee8c3e 404
3fc54d37 405 mutex_unlock(&dbs_mutex);
1da177e4
LT
406 break;
407
408 case CPUFREQ_GOV_STOP:
3fc54d37 409 mutex_lock(&dbs_mutex);
1da177e4
LT
410 this_dbs_info->enable = 0;
411 sysfs_remove_group(&policy->kobj, &dbs_attr_group);
412 dbs_enable--;
413 /*
414 * Stop the timerschedule work, when this governor
415 * is used for first time
416 */
32ee8c3e 417 if (dbs_enable == 0)
1da177e4 418 dbs_timer_exit();
32ee8c3e 419
3fc54d37 420 mutex_unlock(&dbs_mutex);
1da177e4
LT
421
422 break;
423
424 case CPUFREQ_GOV_LIMITS:
4ec223d0 425 lock_cpu_hotplug();
3fc54d37 426 mutex_lock(&dbs_mutex);
1da177e4
LT
427 if (policy->max < this_dbs_info->cur_policy->cur)
428 __cpufreq_driver_target(
429 this_dbs_info->cur_policy,
32ee8c3e 430 policy->max, CPUFREQ_RELATION_H);
1da177e4
LT
431 else if (policy->min > this_dbs_info->cur_policy->cur)
432 __cpufreq_driver_target(
433 this_dbs_info->cur_policy,
32ee8c3e 434 policy->min, CPUFREQ_RELATION_L);
3fc54d37 435 mutex_unlock(&dbs_mutex);
4ec223d0 436 unlock_cpu_hotplug();
1da177e4
LT
437 break;
438 }
439 return 0;
440}
441
7f335d4e 442static struct cpufreq_governor cpufreq_gov_dbs = {
1da177e4
LT
443 .name = "ondemand",
444 .governor = cpufreq_governor_dbs,
445 .owner = THIS_MODULE,
446};
1da177e4
LT
447
448static int __init cpufreq_gov_dbs_init(void)
449{
450 return cpufreq_register_governor(&cpufreq_gov_dbs);
451}
452
453static void __exit cpufreq_gov_dbs_exit(void)
454{
6810b548
AK
455 /* Make sure that the scheduled work is indeed not running.
456 Assumes the timer has been cancelled first. */
457 if (dbs_workq) {
458 flush_workqueue(dbs_workq);
459 destroy_workqueue(dbs_workq);
460 }
1da177e4
LT
461
462 cpufreq_unregister_governor(&cpufreq_gov_dbs);
463}
464
465
466MODULE_AUTHOR ("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
467MODULE_DESCRIPTION ("'cpufreq_ondemand' - A dynamic cpufreq governor for "
468 "Low Latency Frequency Transition capable processors");
469MODULE_LICENSE ("GPL");
470
471module_init(cpufreq_gov_dbs_init);
472module_exit(cpufreq_gov_dbs_exit);