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