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