blob: 9c68d72244a8c8b2633f5ca482f04c5dadf065da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Rohit Gupta093a2132013-04-04 15:45:16 -07007 * (c) 2013 The Linux Foundation. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/cpufreq.h>
Andrew Morton138a01282006-06-23 03:31:19 -070018#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/jiffies.h>
20#include <linux/kernel_stat.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080021#include <linux/mutex.h>
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070022#include <linux/hrtimer.h>
23#include <linux/tick.h>
24#include <linux/ktime.h>
myfluxi5ece4272014-02-06 21:14:57 +010025#include <linux/smpboot.h>
Thomas Renninger9411b4e2009-02-04 11:54:04 +010026#include <linux/sched.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/input.h>
28#include <linux/workqueue.h>
29#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * dbs is used in this file as a shortform for demandbased switching
33 * It helps to keep variable names smaller, simpler
34 */
35
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -070036#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define DEF_FREQUENCY_UP_THRESHOLD (80)
David C Niemi3f78a9f2010-10-06 16:54:24 -040038#define DEF_SAMPLING_DOWN_FACTOR (1)
39#define MAX_SAMPLING_DOWN_FACTOR (100000)
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070040#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
41#define MICRO_FREQUENCY_UP_THRESHOLD (95)
Thomas Renningercef96152009-04-22 13:48:29 +020042#define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
Dave Jonesc29f1402005-05-31 19:03:50 -070043#define MIN_FREQUENCY_UP_THRESHOLD (11)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define MAX_FREQUENCY_UP_THRESHOLD (100)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045#define MIN_FREQUENCY_DOWN_DIFFERENTIAL (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Dave Jones32ee8c32006-02-28 00:43:23 -050047/*
48 * The polling frequency of this governor depends on the capability of
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * the processor. Default polling frequency is 1000 times the transition
Dave Jones32ee8c32006-02-28 00:43:23 -050050 * latency of the processor. The governor will work on any processor with
51 * transition latency <= 10mS, using appropriate sampling
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * rate.
53 * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
54 * this governor will not work.
55 * All times here are in uS.
56 */
Dave Jonesdf8b59b2005-09-20 12:39:35 -070057#define MIN_SAMPLING_RATE_RATIO (2)
Thomas Renninger112124a2009-02-04 11:55:12 +010058
Thomas Renningercef96152009-04-22 13:48:29 +020059static unsigned int min_sampling_rate;
60
Thomas Renninger112124a2009-02-04 11:55:12 +010061#define LATENCY_MULTIPLIER (1000)
Thomas Renningercef96152009-04-22 13:48:29 +020062#define MIN_LATENCY_MULTIPLIER (100)
Thomas Renninger1c256242007-10-02 13:28:12 -070063#define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Ng8192a2f2012-01-19 14:16:19 -080065#define POWERSAVE_BIAS_MAXLEVEL (1000)
66#define POWERSAVE_BIAS_MINLEVEL (-1000)
67
David Howellsc4028952006-11-22 14:57:56 +000068static void do_dbs_timer(struct work_struct *work);
Thomas Renninger0e625ac2009-07-24 15:25:06 +020069static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
70 unsigned int event);
71
72#ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
73static
74#endif
75struct cpufreq_governor cpufreq_gov_ondemand = {
76 .name = "ondemand",
77 .governor = cpufreq_governor_dbs,
78 .max_transition_latency = TRANSITION_LATENCY_LIMIT,
79 .owner = THIS_MODULE,
80};
David Howellsc4028952006-11-22 14:57:56 +000081
82/* Sampling types */
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080083enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85struct cpu_dbs_info_s {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070086 cputime64_t prev_cpu_idle;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -070087 cputime64_t prev_cpu_iowait;
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -070088 cputime64_t prev_cpu_wall;
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -070089 cputime64_t prev_cpu_nice;
Dave Jones32ee8c32006-02-28 00:43:23 -050090 struct cpufreq_policy *cur_policy;
Dave Jones2b03f892009-01-18 01:43:44 -050091 struct delayed_work work;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +040092 struct cpufreq_frequency_table *freq_table;
93 unsigned int freq_lo;
94 unsigned int freq_lo_jiffies;
95 unsigned int freq_hi_jiffies;
David C Niemi3f78a9f2010-10-06 16:54:24 -040096 unsigned int rate_mult;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -070097 unsigned int prev_load;
98 unsigned int max_load;
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -080099 int cpu;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700100 unsigned int sample_type:1;
101 /*
102 * percpu mutex that serializes governor limit change with
103 * do_dbs_timer invocation. We do not want do_dbs_timer to run
104 * when user is changing the governor or limits.
105 */
106 struct mutex timer_mutex;
Steve Muckled5d59a52013-05-31 10:39:31 -0700107
Steve Muckled5d59a52013-05-31 10:39:31 -0700108 atomic_t src_sync_cpu;
109 atomic_t sync_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
Tejun Heo245b2e72009-06-24 15:13:48 +0900111static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info);
myfluxi5ece4272014-02-06 21:14:57 +0100112static DEFINE_PER_CPU(struct task_struct *, sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Ng8192a2f2012-01-19 14:16:19 -0800114static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info);
115static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info);
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static unsigned int dbs_enable; /* number of CPUs using this policy */
118
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700119/*
Matt Wagantall5b71a822013-05-31 20:02:01 -0700120 * dbs_mutex protects dbs_enable and dbs_info during start/stop.
Venkatesh Pallipadi4ec223d2006-06-21 15:18:34 -0700121 */
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700122static DEFINE_MUTEX(dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Matt Wagantalle3f2f662013-05-23 15:52:49 -0700124static struct workqueue_struct *dbs_wq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125
Stephen Boydb94083c2012-10-31 17:43:08 -0700126struct dbs_work_struct {
127 struct work_struct work;
128 unsigned int cpu;
129};
130
131static DEFINE_PER_CPU(struct dbs_work_struct, dbs_refresh_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400133static struct dbs_tuners {
Dave Jones32ee8c32006-02-28 00:43:23 -0500134 unsigned int sampling_rate;
Dave Jones32ee8c32006-02-28 00:43:23 -0500135 unsigned int up_threshold;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700136 unsigned int up_threshold_multi_core;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700137 unsigned int down_differential;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700138 unsigned int down_differential_multi_core;
139 unsigned int optimal_freq;
140 unsigned int up_threshold_any_cpu_load;
141 unsigned int sync_freq;
Dave Jones32ee8c32006-02-28 00:43:23 -0500142 unsigned int ignore_nice;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400143 unsigned int sampling_down_factor;
David Ng8192a2f2012-01-19 14:16:19 -0800144 int powersave_bias;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700145 unsigned int io_is_busy;
Dilip Gudlur17cbd952013-06-17 13:04:31 -0700146 unsigned int input_boost;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400147} dbs_tuners_ins = {
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700148 .up_threshold_multi_core = DEF_FREQUENCY_UP_THRESHOLD,
Dave Jones32ee8c32006-02-28 00:43:23 -0500149 .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400150 .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700151 .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700152 .down_differential_multi_core = MICRO_FREQUENCY_DOWN_DIFFERENTIAL,
153 .up_threshold_any_cpu_load = DEF_FREQUENCY_UP_THRESHOLD,
Eric Piel9cbad612006-03-10 11:35:27 +0200154 .ignore_nice = 0,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400155 .powersave_bias = 0,
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700156 .sync_freq = 0,
157 .optimal_freq = 0,
Dilip Gudlur17cbd952013-06-17 13:04:31 -0700158 .input_boost = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700161static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
162{
163 u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
164
165 if (iowait_time == -1ULL)
166 return 0;
167
168 return iowait_time;
169}
170
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400171/*
172 * Find right freq to be set now with powersave_bias on.
173 * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
174 * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
175 */
Adrian Bunkb5ecf602006-08-13 23:00:08 +0200176static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
177 unsigned int freq_next,
178 unsigned int relation)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400179{
David Ng8192a2f2012-01-19 14:16:19 -0800180 unsigned int freq_req, freq_avg;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400181 unsigned int freq_hi, freq_lo;
182 unsigned int index = 0;
183 unsigned int jiffies_total, jiffies_hi, jiffies_lo;
David Ng8192a2f2012-01-19 14:16:19 -0800184 int freq_reduc;
Tejun Heo245b2e72009-06-24 15:13:48 +0900185 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
186 policy->cpu);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400187
188 if (!dbs_info->freq_table) {
189 dbs_info->freq_lo = 0;
190 dbs_info->freq_lo_jiffies = 0;
191 return freq_next;
192 }
193
194 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
195 relation, &index);
196 freq_req = dbs_info->freq_table[index].frequency;
197 freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
198 freq_avg = freq_req - freq_reduc;
199
200 /* Find freq bounds for freq_avg in freq_table */
201 index = 0;
202 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
203 CPUFREQ_RELATION_H, &index);
204 freq_lo = dbs_info->freq_table[index].frequency;
205 index = 0;
206 cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
207 CPUFREQ_RELATION_L, &index);
208 freq_hi = dbs_info->freq_table[index].frequency;
209
210 /* Find out how long we have to be in hi and lo freqs */
211 if (freq_hi == freq_lo) {
212 dbs_info->freq_lo = 0;
213 dbs_info->freq_lo_jiffies = 0;
214 return freq_lo;
215 }
216 jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
217 jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
218 jiffies_hi += ((freq_hi - freq_lo) / 2);
219 jiffies_hi /= (freq_hi - freq_lo);
220 jiffies_lo = jiffies_total - jiffies_hi;
221 dbs_info->freq_lo = freq_lo;
222 dbs_info->freq_lo_jiffies = jiffies_lo;
223 dbs_info->freq_hi_jiffies = jiffies_hi;
224 return freq_hi;
225}
226
David Ng8192a2f2012-01-19 14:16:19 -0800227static int ondemand_powersave_bias_setspeed(struct cpufreq_policy *policy,
228 struct cpufreq_policy *altpolicy,
229 int level)
230{
231 if (level == POWERSAVE_BIAS_MAXLEVEL) {
232 /* maximum powersave; set to lowest frequency */
233 __cpufreq_driver_target(policy,
234 (altpolicy) ? altpolicy->min : policy->min,
235 CPUFREQ_RELATION_L);
236 return 1;
237 } else if (level == POWERSAVE_BIAS_MINLEVEL) {
238 /* minimum powersave; set to highest frequency */
239 __cpufreq_driver_target(policy,
240 (altpolicy) ? altpolicy->max : policy->max,
241 CPUFREQ_RELATION_H);
242 return 1;
243 }
244 return 0;
245}
246
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700247static void ondemand_powersave_bias_init_cpu(int cpu)
248{
Tejun Heo384be2b2009-08-14 14:41:02 +0900249 struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700250 dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
251 dbs_info->freq_lo = 0;
252}
253
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400254static void ondemand_powersave_bias_init(void)
255{
256 int i;
257 for_each_online_cpu(i) {
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700258 ondemand_powersave_bias_init_cpu(i);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400259 }
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/************************** sysfs interface ************************/
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200263
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200264static ssize_t show_sampling_rate_min(struct kobject *kobj,
265 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Thomas Renningercef96152009-04-22 13:48:29 +0200267 return sprintf(buf, "%u\n", min_sampling_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200270define_one_global_ro(sampling_rate_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/* cpufreq_ondemand Governor Tunables */
273#define show_one(file_name, object) \
274static ssize_t show_##file_name \
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200275(struct kobject *kobj, struct attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{ \
277 return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
278}
279show_one(sampling_rate, sampling_rate);
Arjan van de Ven19379b12010-05-09 08:26:51 -0700280show_one(io_is_busy, io_is_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281show_one(up_threshold, up_threshold);
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700282show_one(up_threshold_multi_core, up_threshold_multi_core);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283show_one(down_differential, down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400284show_one(sampling_down_factor, sampling_down_factor);
Alexander Clouter001893c2005-12-01 01:09:25 -0800285show_one(ignore_nice_load, ignore_nice);
Krishna Vanka6912b872013-12-11 14:56:15 +0530286show_one(down_differential_multi_core, down_differential_multi_core);
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700287show_one(optimal_freq, optimal_freq);
288show_one(up_threshold_any_cpu_load, up_threshold_any_cpu_load);
289show_one(sync_freq, sync_freq);
Dilip Gudlur17cbd952013-06-17 13:04:31 -0700290show_one(input_boost, input_boost);
David Ng8192a2f2012-01-19 14:16:19 -0800291
292static ssize_t show_powersave_bias
293(struct kobject *kobj, struct attribute *attr, char *buf)
294{
295 return snprintf(buf, PAGE_SIZE, "%d\n", dbs_tuners_ins.powersave_bias);
296}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900298/**
299 * update_sampling_rate - update sampling rate effective immediately if needed.
300 * @new_rate: new sampling rate
301 *
302 * If new rate is smaller than the old, simply updaing
303 * dbs_tuners_int.sampling_rate might not be appropriate. For example,
304 * if the original sampling_rate was 1 second and the requested new sampling
305 * rate is 10 ms because the user needs immediate reaction from ondemand
306 * governor, but not sure if higher frequency will be required or not,
307 * then, the governor may change the sampling rate too late; up to 1 second
308 * later. Thus, if we are reducing the sampling rate, we need to make the
309 * new value effective immediately.
310 */
311static void update_sampling_rate(unsigned int new_rate)
312{
313 int cpu;
314
315 dbs_tuners_ins.sampling_rate = new_rate
316 = max(new_rate, min_sampling_rate);
317
Rohit Guptac4aeacb2013-07-31 15:33:24 -0700318 get_online_cpus();
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900319 for_each_online_cpu(cpu) {
320 struct cpufreq_policy *policy;
321 struct cpu_dbs_info_s *dbs_info;
322 unsigned long next_sampling, appointed_at;
323
324 policy = cpufreq_cpu_get(cpu);
325 if (!policy)
326 continue;
327 dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
328 cpufreq_cpu_put(policy);
329
330 mutex_lock(&dbs_info->timer_mutex);
331
332 if (!delayed_work_pending(&dbs_info->work)) {
333 mutex_unlock(&dbs_info->timer_mutex);
334 continue;
335 }
336
337 next_sampling = jiffies + usecs_to_jiffies(new_rate);
338 appointed_at = dbs_info->work.timer.expires;
339
340
341 if (time_before(next_sampling, appointed_at)) {
342
343 mutex_unlock(&dbs_info->timer_mutex);
344 cancel_delayed_work_sync(&dbs_info->work);
345 mutex_lock(&dbs_info->timer_mutex);
346
Matt Wagantalle3f2f662013-05-23 15:52:49 -0700347 queue_delayed_work_on(dbs_info->cpu, dbs_wq,
348 &dbs_info->work, usecs_to_jiffies(new_rate));
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900349
350 }
351 mutex_unlock(&dbs_info->timer_mutex);
352 }
Rohit Guptac4aeacb2013-07-31 15:33:24 -0700353 put_online_cpus();
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900354}
355
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200356static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
357 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 unsigned int input;
360 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700361 ret = sscanf(buf, "%u", &input);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700362 if (ret != 1)
363 return -EINVAL;
MyungJoo Hamfd0ef7a2012-02-29 17:54:41 +0900364 update_sampling_rate(input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return count;
366}
367
Dilip Gudlur17cbd952013-06-17 13:04:31 -0700368static ssize_t store_input_boost(struct kobject *a, struct attribute *b,
369 const char *buf, size_t count)
370{
371 unsigned int input;
372 int ret;
373 ret = sscanf(buf, "%u", &input);
374 if (ret != 1)
375 return -EINVAL;
376 dbs_tuners_ins.input_boost = input;
377 return count;
378}
379
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700380static ssize_t store_sync_freq(struct kobject *a, struct attribute *b,
381 const char *buf, size_t count)
382{
383 unsigned int input;
384 int ret;
385
386 ret = sscanf(buf, "%u", &input);
387 if (ret != 1)
388 return -EINVAL;
389 dbs_tuners_ins.sync_freq = input;
390 return count;
391}
392
Arjan van de Ven19379b12010-05-09 08:26:51 -0700393static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
394 const char *buf, size_t count)
395{
396 unsigned int input;
397 int ret;
398
399 ret = sscanf(buf, "%u", &input);
400 if (ret != 1)
401 return -EINVAL;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700402 dbs_tuners_ins.io_is_busy = !!input;
Arjan van de Ven19379b12010-05-09 08:26:51 -0700403 return count;
404}
405
Krishna Vanka6912b872013-12-11 14:56:15 +0530406static ssize_t store_down_differential_multi_core(struct kobject *a,
407 struct attribute *b, const char *buf, size_t count)
408{
409 unsigned int input;
410 int ret;
411
412 ret = sscanf(buf, "%u", &input);
413 if (ret != 1)
414 return -EINVAL;
415 dbs_tuners_ins.down_differential_multi_core = input;
416 return count;
417}
418
419
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700420static ssize_t store_optimal_freq(struct kobject *a, struct attribute *b,
421 const char *buf, size_t count)
422{
423 unsigned int input;
424 int ret;
425
426 ret = sscanf(buf, "%u", &input);
427 if (ret != 1)
428 return -EINVAL;
429 dbs_tuners_ins.optimal_freq = input;
430 return count;
431}
432
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200433static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
434 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 unsigned int input;
437 int ret;
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700438 ret = sscanf(buf, "%u", &input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Dave Jones32ee8c32006-02-28 00:43:23 -0500440 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
Dave Jonesc29f1402005-05-31 19:03:50 -0700441 input < MIN_FREQUENCY_UP_THRESHOLD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -EINVAL;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 dbs_tuners_ins.up_threshold = input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return count;
446}
447
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700448static ssize_t store_up_threshold_multi_core(struct kobject *a,
449 struct attribute *b, const char *buf, size_t count)
450{
451 unsigned int input;
452 int ret;
453 ret = sscanf(buf, "%u", &input);
454
455 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
456 input < MIN_FREQUENCY_UP_THRESHOLD) {
457 return -EINVAL;
458 }
459 dbs_tuners_ins.up_threshold_multi_core = input;
460 return count;
461}
462
463static ssize_t store_up_threshold_any_cpu_load(struct kobject *a,
464 struct attribute *b, const char *buf, size_t count)
465{
466 unsigned int input;
467 int ret;
468 ret = sscanf(buf, "%u", &input);
469
470 if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
471 input < MIN_FREQUENCY_UP_THRESHOLD) {
472 return -EINVAL;
473 }
474 dbs_tuners_ins.up_threshold_any_cpu_load = input;
475 return count;
476}
477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478static ssize_t store_down_differential(struct kobject *a, struct attribute *b,
479 const char *buf, size_t count)
480{
481 unsigned int input;
482 int ret;
483 ret = sscanf(buf, "%u", &input);
484
485 if (ret != 1 || input >= dbs_tuners_ins.up_threshold ||
486 input < MIN_FREQUENCY_DOWN_DIFFERENTIAL) {
487 return -EINVAL;
488 }
489
490 dbs_tuners_ins.down_differential = input;
491
492 return count;
493}
494
David C Niemi3f78a9f2010-10-06 16:54:24 -0400495static ssize_t store_sampling_down_factor(struct kobject *a,
496 struct attribute *b, const char *buf, size_t count)
497{
498 unsigned int input, j;
499 int ret;
500 ret = sscanf(buf, "%u", &input);
501
502 if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
503 return -EINVAL;
David C Niemi3f78a9f2010-10-06 16:54:24 -0400504 dbs_tuners_ins.sampling_down_factor = input;
505
506 /* Reset down sampling multiplier in case it was active */
507 for_each_online_cpu(j) {
508 struct cpu_dbs_info_s *dbs_info;
509 dbs_info = &per_cpu(od_cpu_dbs_info, j);
510 dbs_info->rate_mult = 1;
511 }
David C Niemi3f78a9f2010-10-06 16:54:24 -0400512 return count;
513}
514
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200515static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
516 const char *buf, size_t count)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700517{
518 unsigned int input;
519 int ret;
520
521 unsigned int j;
Dave Jones32ee8c32006-02-28 00:43:23 -0500522
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -0700523 ret = sscanf(buf, "%u", &input);
Dave Jones2b03f892009-01-18 01:43:44 -0500524 if (ret != 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700525 return -EINVAL;
526
Dave Jones2b03f892009-01-18 01:43:44 -0500527 if (input > 1)
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700528 input = 1;
Dave Jones32ee8c32006-02-28 00:43:23 -0500529
Dave Jones2b03f892009-01-18 01:43:44 -0500530 if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700531 return count;
532 }
533 dbs_tuners_ins.ignore_nice = input;
534
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700535 /* we need to re-evaluate prev_cpu_idle */
Dave Jonesdac1c1a2005-05-31 19:03:49 -0700536 for_each_online_cpu(j) {
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700537 struct cpu_dbs_info_s *dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +0900538 dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700539 dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
Viresh Kumarefa514d2013-05-17 11:26:32 +0000540 &dbs_info->prev_cpu_wall, dbs_tuners_ins.io_is_busy);
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500541 if (dbs_tuners_ins.ignore_nice)
Glauber Costa3292beb2011-11-28 14:45:17 -0200542 dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500543
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700544 }
Dave Jones3d5ee9e2005-05-31 19:03:47 -0700545 return count;
546}
547
Thomas Renninger0e625ac2009-07-24 15:25:06 +0200548static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
549 const char *buf, size_t count)
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400550{
David Ng8192a2f2012-01-19 14:16:19 -0800551 int input = 0;
552 int bypass = 0;
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530553 int ret, cpu, reenable_timer, j;
David Ng8192a2f2012-01-19 14:16:19 -0800554 struct cpu_dbs_info_s *dbs_info;
555
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530556 struct cpumask cpus_timer_done;
557 cpumask_clear(&cpus_timer_done);
558
David Ng8192a2f2012-01-19 14:16:19 -0800559 ret = sscanf(buf, "%d", &input);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400560
561 if (ret != 1)
562 return -EINVAL;
563
David Ng8192a2f2012-01-19 14:16:19 -0800564 if (input >= POWERSAVE_BIAS_MAXLEVEL) {
565 input = POWERSAVE_BIAS_MAXLEVEL;
566 bypass = 1;
567 } else if (input <= POWERSAVE_BIAS_MINLEVEL) {
568 input = POWERSAVE_BIAS_MINLEVEL;
569 bypass = 1;
570 }
571
572 if (input == dbs_tuners_ins.powersave_bias) {
573 /* no change */
574 return count;
575 }
576
577 reenable_timer = ((dbs_tuners_ins.powersave_bias ==
578 POWERSAVE_BIAS_MAXLEVEL) ||
579 (dbs_tuners_ins.powersave_bias ==
580 POWERSAVE_BIAS_MINLEVEL));
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400581
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400582 dbs_tuners_ins.powersave_bias = input;
Matt Wagantall5b71a822013-05-31 20:02:01 -0700583
Matt Wagantallaa93be02013-05-31 13:14:44 -0700584 get_online_cpus();
Matt Wagantalle1aa7a22013-08-22 10:40:48 -0700585 mutex_lock(&dbs_mutex);
Matt Wagantall5b71a822013-05-31 20:02:01 -0700586
David Ng8192a2f2012-01-19 14:16:19 -0800587 if (!bypass) {
588 if (reenable_timer) {
589 /* reinstate dbs timer */
590 for_each_online_cpu(cpu) {
591 if (lock_policy_rwsem_write(cpu) < 0)
592 continue;
593
594 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530595
596 for_each_cpu(j, &cpus_timer_done) {
597 if (!dbs_info->cur_policy) {
598 pr_err("Dbs policy is NULL\n");
599 goto skip_this_cpu;
600 }
601 if (cpumask_test_cpu(j, dbs_info->
602 cur_policy->cpus))
603 goto skip_this_cpu;
604 }
605
606 cpumask_set_cpu(cpu, &cpus_timer_done);
David Ng8192a2f2012-01-19 14:16:19 -0800607 if (dbs_info->cur_policy) {
Osvaldo Banuelos38eadd22013-12-06 16:15:15 -0800608 dbs_timer_exit(dbs_info);
David Ng8192a2f2012-01-19 14:16:19 -0800609 /* restart dbs timer */
Osvaldo Banuelos38eadd22013-12-06 16:15:15 -0800610 mutex_lock(&dbs_info->timer_mutex);
David Ng8192a2f2012-01-19 14:16:19 -0800611 dbs_timer_init(dbs_info);
Rohit Gupta92daa722013-06-17 17:56:27 -0700612 /* Enable frequency synchronization
613 * of CPUs */
Osvaldo Banuelos38eadd22013-12-06 16:15:15 -0800614 mutex_unlock(&dbs_info->timer_mutex);
Rohit Gupta92daa722013-06-17 17:56:27 -0700615 atomic_set(&dbs_info->sync_enabled, 1);
David Ng8192a2f2012-01-19 14:16:19 -0800616 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530617skip_this_cpu:
David Ng8192a2f2012-01-19 14:16:19 -0800618 unlock_policy_rwsem_write(cpu);
619 }
620 }
621 ondemand_powersave_bias_init();
622 } else {
623 /* running at maximum or minimum frequencies; cancel
624 dbs timer as periodic load sampling is not necessary */
625 for_each_online_cpu(cpu) {
626 if (lock_policy_rwsem_write(cpu) < 0)
627 continue;
628
629 dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530630
631 for_each_cpu(j, &cpus_timer_done) {
632 if (!dbs_info->cur_policy) {
633 pr_err("Dbs policy is NULL\n");
634 goto skip_this_cpu_bypass;
635 }
636 if (cpumask_test_cpu(j, dbs_info->
637 cur_policy->cpus))
638 goto skip_this_cpu_bypass;
639 }
640
641 cpumask_set_cpu(cpu, &cpus_timer_done);
642
David Ng8192a2f2012-01-19 14:16:19 -0800643 if (dbs_info->cur_policy) {
644 /* cpu using ondemand, cancel dbs timer */
David Ng8192a2f2012-01-19 14:16:19 -0800645 dbs_timer_exit(dbs_info);
Rohit Gupta92daa722013-06-17 17:56:27 -0700646 /* Disable frequency synchronization of
647 * CPUs to avoid re-queueing of work from
648 * sync_thread */
649 atomic_set(&dbs_info->sync_enabled, 0);
David Ng8192a2f2012-01-19 14:16:19 -0800650
Rohit Guptaef9c5742013-06-17 16:57:08 -0700651 mutex_lock(&dbs_info->timer_mutex);
David Ng8192a2f2012-01-19 14:16:19 -0800652 ondemand_powersave_bias_setspeed(
653 dbs_info->cur_policy,
654 NULL,
655 input);
David Ng8192a2f2012-01-19 14:16:19 -0800656 mutex_unlock(&dbs_info->timer_mutex);
Rohit Guptaef9c5742013-06-17 16:57:08 -0700657
David Ng8192a2f2012-01-19 14:16:19 -0800658 }
Krishna Vankaebf80eb2012-04-19 13:11:20 +0530659skip_this_cpu_bypass:
David Ng8192a2f2012-01-19 14:16:19 -0800660 unlock_policy_rwsem_write(cpu);
661 }
662 }
Matt Wagantall5b71a822013-05-31 20:02:01 -0700663
Matt Wagantall5b71a822013-05-31 20:02:01 -0700664 mutex_unlock(&dbs_mutex);
Matt Wagantalle1aa7a22013-08-22 10:40:48 -0700665 put_online_cpus();
David Ng8192a2f2012-01-19 14:16:19 -0800666
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400667 return count;
668}
669
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200670define_one_global_rw(sampling_rate);
Linus Torvalds07d77752010-05-18 08:49:13 -0700671define_one_global_rw(io_is_busy);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200672define_one_global_rw(up_threshold);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700673define_one_global_rw(down_differential);
David C Niemi3f78a9f2010-10-06 16:54:24 -0400674define_one_global_rw(sampling_down_factor);
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200675define_one_global_rw(ignore_nice_load);
676define_one_global_rw(powersave_bias);
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700677define_one_global_rw(up_threshold_multi_core);
Krishna Vanka6912b872013-12-11 14:56:15 +0530678define_one_global_rw(down_differential_multi_core);
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700679define_one_global_rw(optimal_freq);
680define_one_global_rw(up_threshold_any_cpu_load);
681define_one_global_rw(sync_freq);
Dilip Gudlur17cbd952013-06-17 13:04:31 -0700682define_one_global_rw(input_boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dave Jones2b03f892009-01-18 01:43:44 -0500684static struct attribute *dbs_attributes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 &sampling_rate_min.attr,
686 &sampling_rate.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 &up_threshold.attr,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688 &down_differential.attr,
David C Niemi3f78a9f2010-10-06 16:54:24 -0400689 &sampling_down_factor.attr,
Alexander Clouter001893c2005-12-01 01:09:25 -0800690 &ignore_nice_load.attr,
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400691 &powersave_bias.attr,
Arjan van de Ven19379b12010-05-09 08:26:51 -0700692 &io_is_busy.attr,
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700693 &up_threshold_multi_core.attr,
Krishna Vanka6912b872013-12-11 14:56:15 +0530694 &down_differential_multi_core.attr,
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700695 &optimal_freq.attr,
696 &up_threshold_any_cpu_load.attr,
697 &sync_freq.attr,
Dilip Gudlur17cbd952013-06-17 13:04:31 -0700698 &input_boost.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 NULL
700};
701
702static struct attribute_group dbs_attr_group = {
703 .attrs = dbs_attributes,
704 .name = "ondemand",
705};
706
707/************************** sysfs end ************************/
708
Mike Chan00e299f2010-01-26 17:06:47 -0800709static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
710{
711 if (dbs_tuners_ins.powersave_bias)
712 freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
713 else if (p->cur == p->max)
714 return;
715
716 __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
717 CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
718}
719
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700720static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800722 /* Extrapolated load of this CPU */
723 unsigned int load_at_max_freq = 0;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700724 unsigned int max_load_freq;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800725 /* Current load across this CPU */
726 unsigned int cur_load = 0;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700727 unsigned int max_load_other_cpu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct cpufreq_policy *policy;
729 unsigned int j;
730
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400731 this_dbs_info->freq_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 policy = this_dbs_info->cur_policy;
Swetha Chikkaboraiah23d4f372013-11-29 12:22:07 +0530733 if(policy == NULL)
734 return;
Venki Pallipadiea487612007-06-20 14:26:24 -0700735
Dave Jones32ee8c32006-02-28 00:43:23 -0500736 /*
Dave Jonesc29f1402005-05-31 19:03:50 -0700737 * Every sampling_rate, we check, if current idle time is less
738 * than 20% (default), then we try to increase frequency
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700739 * Every sampling_rate, we look for a the lowest
Dave Jonesc29f1402005-05-31 19:03:50 -0700740 * frequency which can sustain the load while keeping idle time over
741 * 30%. If such a frequency exist, we try to decrease to this frequency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500743 * Any frequency increase takes it to the maximum frequency.
744 * Frequency reduction happens at minimum steps of
745 * 5% (default) of current frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
747
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700748 /* Get Absolute Load - in terms of freq */
749 max_load_freq = 0;
750
Rusty Russell835481d2009-01-04 05:18:06 -0800751 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct cpu_dbs_info_s *j_dbs_info;
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700753 cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
754 unsigned int idle_time, wall_time, iowait_time;
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800755 unsigned int load_freq;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700756 int freq_avg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Tejun Heo245b2e72009-06-24 15:13:48 +0900758 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700759
Viresh Kumarefa514d2013-05-17 11:26:32 +0000760 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, dbs_tuners_ins.io_is_busy);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700761 cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700762
Martin Schwidefsky64861632011-12-15 14:56:09 +0100763 wall_time = (unsigned int)
764 (cur_wall_time - j_dbs_info->prev_cpu_wall);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700765 j_dbs_info->prev_cpu_wall = cur_wall_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Martin Schwidefsky64861632011-12-15 14:56:09 +0100767 idle_time = (unsigned int)
768 (cur_idle_time - j_dbs_info->prev_cpu_idle);
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700769 j_dbs_info->prev_cpu_idle = cur_idle_time;
770
Martin Schwidefsky64861632011-12-15 14:56:09 +0100771 iowait_time = (unsigned int)
772 (cur_iowait_time - j_dbs_info->prev_cpu_iowait);
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700773 j_dbs_info->prev_cpu_iowait = cur_iowait_time;
774
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500775 if (dbs_tuners_ins.ignore_nice) {
Glauber Costa3292beb2011-11-28 14:45:17 -0200776 u64 cur_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500777 unsigned long cur_nice_jiffies;
778
Glauber Costa3292beb2011-11-28 14:45:17 -0200779 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
780 j_dbs_info->prev_cpu_nice;
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500781 /*
782 * Assumption: nice time between sampling periods will
783 * be less than 2^32 jiffies for 32 bit sys
784 */
785 cur_nice_jiffies = (unsigned long)
786 cputime64_to_jiffies64(cur_nice);
787
Glauber Costa3292beb2011-11-28 14:45:17 -0200788 j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -0500789 idle_time += jiffies_to_usecs(cur_nice_jiffies);
790 }
791
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700792 /*
793 * For the purpose of ondemand, waiting for disk IO is an
794 * indication that you're performance critical, and not that
795 * the system is actually idle. So subtract the iowait time
796 * from the cpu idle time.
797 */
798
Arjan van de Ven19379b12010-05-09 08:26:51 -0700799 if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
Arjan van de Ven6b8fcd92010-05-09 08:26:06 -0700800 idle_time -= iowait_time;
801
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -0700802 if (unlikely(!wall_time || wall_time < idle_time))
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700803 continue;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700804
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800805 cur_load = 100 * (wall_time - idle_time) / wall_time;
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700806 j_dbs_info->max_load = max(cur_load, j_dbs_info->prev_load);
807 j_dbs_info->prev_load = cur_load;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700808 freq_avg = __cpufreq_driver_getavg(policy, j);
Swetha Chikkaboraiah23d4f372013-11-29 12:22:07 +0530809 if (policy == NULL)
810 return;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700811 if (freq_avg <= 0)
812 freq_avg = policy->cur;
813
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800814 load_freq = cur_load * freq_avg;
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700815 if (load_freq > max_load_freq)
816 max_load_freq = load_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700818
819 for_each_online_cpu(j) {
820 struct cpu_dbs_info_s *j_dbs_info;
821 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
822
823 if (j == policy->cpu)
824 continue;
825
826 if (max_load_other_cpu < j_dbs_info->max_load)
827 max_load_other_cpu = j_dbs_info->max_load;
828 /*
829 * The other cpu could be running at higher frequency
830 * but may not have completed it's sampling_down_factor.
831 * For that case consider other cpu is loaded so that
832 * frequency imbalance does not occur.
833 */
834
835 if ((j_dbs_info->cur_policy != NULL)
836 && (j_dbs_info->cur_policy->cur ==
837 j_dbs_info->cur_policy->max)) {
838
839 if (policy->cur >= dbs_tuners_ins.optimal_freq)
840 max_load_other_cpu =
841 dbs_tuners_ins.up_threshold_any_cpu_load;
842 }
843 }
844
Anitha Anandcbeef6a2012-03-05 18:10:52 -0800845 /* calculate the scaled load across CPU */
846 load_at_max_freq = (cur_load * policy->cur)/policy->cpuinfo.max_freq;
847
848 cpufreq_notify_utilization(policy, load_at_max_freq);
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700849 /* Check for frequency increase */
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700850 if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
David C Niemi3f78a9f2010-10-06 16:54:24 -0400851 /* If switching to max speed, apply sampling_down_factor */
852 if (policy->cur < policy->max)
853 this_dbs_info->rate_mult =
854 dbs_tuners_ins.sampling_down_factor;
Mike Chan00e299f2010-01-26 17:06:47 -0800855 dbs_freq_increase(policy, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return;
857 }
858
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700859 if (num_online_cpus() > 1) {
860
861 if (max_load_other_cpu >
862 dbs_tuners_ins.up_threshold_any_cpu_load) {
863 if (policy->cur < dbs_tuners_ins.sync_freq)
864 dbs_freq_increase(policy,
865 dbs_tuners_ins.sync_freq);
866 return;
867 }
868
869 if (max_load_freq > dbs_tuners_ins.up_threshold_multi_core *
870 policy->cur) {
871 if (policy->cur < dbs_tuners_ins.optimal_freq)
872 dbs_freq_increase(policy,
873 dbs_tuners_ins.optimal_freq);
874 return;
875 }
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /* Check for frequency decrease */
Dave Jonesc29f1402005-05-31 19:03:50 -0700879 /* if we cannot reduce the frequency anymore, break out early */
880 if (policy->cur == policy->min)
881 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Jonesc29f1402005-05-31 19:03:50 -0700883 /*
884 * The optimal frequency is the frequency that is the lowest that
885 * can support the current CPU usage without triggering the up
886 * policy. To be safe, we focus 10 points under the threshold.
887 */
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700888 if (max_load_freq <
889 (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
890 policy->cur) {
venkatesh.pallipadi@intel.comc43aa3b2008-08-04 11:59:08 -0700891 unsigned int freq_next;
venkatesh.pallipadi@intel.come9d95bf2008-08-04 11:59:10 -0700892 freq_next = max_load_freq /
893 (dbs_tuners_ins.up_threshold -
894 dbs_tuners_ins.down_differential);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -0700895
David C Niemi3f78a9f2010-10-06 16:54:24 -0400896 /* No longer fully busy, reset rate_mult */
897 this_dbs_info->rate_mult = 1;
898
Nagananda.Chumbalkar@hp.com1dbf5882009-12-21 23:40:52 +0100899 if (freq_next < policy->min)
900 freq_next = policy->min;
901
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700902 if (num_online_cpus() > 1) {
903 if (max_load_other_cpu >
904 (dbs_tuners_ins.up_threshold_multi_core -
905 dbs_tuners_ins.down_differential) &&
906 freq_next < dbs_tuners_ins.sync_freq)
907 freq_next = dbs_tuners_ins.sync_freq;
908
909 if (max_load_freq >
Veena Sambasivan401092b2013-05-14 12:36:48 -0700910 ((dbs_tuners_ins.up_threshold_multi_core -
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700911 dbs_tuners_ins.down_differential_multi_core) *
Veena Sambasivan401092b2013-05-14 12:36:48 -0700912 policy->cur) &&
913 freq_next < dbs_tuners_ins.optimal_freq)
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -0700914 freq_next = dbs_tuners_ins.optimal_freq;
915
916 }
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400917 if (!dbs_tuners_ins.powersave_bias) {
918 __cpufreq_driver_target(policy, freq_next,
919 CPUFREQ_RELATION_L);
920 } else {
921 int freq = powersave_bias_target(policy, freq_next,
922 CPUFREQ_RELATION_L);
923 __cpufreq_driver_target(policy, freq,
924 CPUFREQ_RELATION_L);
925 }
Venkatesh Pallipadiccb2fe22006-06-28 13:49:52 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
David Howellsc4028952006-11-22 14:57:56 +0000929static void do_dbs_timer(struct work_struct *work)
Dave Jones32ee8c32006-02-28 00:43:23 -0500930{
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800931 struct cpu_dbs_info_s *dbs_info =
932 container_of(work, struct cpu_dbs_info_s, work.work);
933 unsigned int cpu = dbs_info->cpu;
934 int sample_type = dbs_info->sample_type;
935
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100936 int delay;
Jocelyn Falempea665df92010-03-11 14:01:11 -0800937
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700938 mutex_lock(&dbs_info->timer_mutex);
Venkatesh Pallipadi56463b72007-02-05 16:12:45 -0800939
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400940 /* Common NORMAL_SAMPLE setup */
David Howellsc4028952006-11-22 14:57:56 +0000941 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400942 if (!dbs_tuners_ins.powersave_bias ||
David Howellsc4028952006-11-22 14:57:56 +0000943 sample_type == DBS_NORMAL_SAMPLE) {
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400944 dbs_check_cpu(dbs_info);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400945 if (dbs_info->freq_lo) {
946 /* Setup timer for SUB_SAMPLE */
David Howellsc4028952006-11-22 14:57:56 +0000947 dbs_info->sample_type = DBS_SUB_SAMPLE;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400948 delay = dbs_info->freq_hi_jiffies;
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100949 } else {
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100950 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
951 * dbs_info->rate_mult);
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400952 }
953 } else {
954 __cpufreq_driver_target(dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -0500955 dbs_info->freq_lo, CPUFREQ_RELATION_H);
Vincent Guittot5cb2c3b2011-02-07 17:14:25 +0100956 delay = dbs_info->freq_lo_jiffies;
Alexey Starikovskiy05ca0352006-07-31 22:28:12 +0400957 }
Matt Wagantalle3f2f662013-05-23 15:52:49 -0700958 queue_delayed_work_on(cpu, dbs_wq, &dbs_info->work, delay);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -0700959 mutex_unlock(&dbs_info->timer_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -0500960}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -0800962static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Alexey Starikovskiy1ce28d62006-07-31 22:25:20 +0400964 /* We want all CPUs to do sampling nearly on same jiffy */
965 int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
Jocelyn Falempea665df92010-03-11 14:01:11 -0800966
967 if (num_online_cpus() > 1)
968 delay -= jiffies % delay;
Venkatesh Pallipadi2f8a8352006-06-28 13:51:19 -0700969
David Howellsc4028952006-11-22 14:57:56 +0000970 dbs_info->sample_type = DBS_NORMAL_SAMPLE;
Venki Pallipadi28287032007-05-08 00:27:47 -0700971 INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
Matt Wagantalle3f2f662013-05-23 15:52:49 -0700972 queue_delayed_work_on(dbs_info->cpu, dbs_wq, &dbs_info->work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -0700975static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Mathieu Desnoyersb14893a2009-05-17 10:30:45 -0400977 cancel_delayed_work_sync(&dbs_info->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Arjan van de Ven19379b12010-05-09 08:26:51 -0700980/*
981 * Not all CPUs want IO time to be accounted as busy; this dependson how
982 * efficient idling at a higher frequency/voltage is.
983 * Pavel Machek says this is not so for various generations of AMD and old
984 * Intel systems.
985 * Mike Chan (androidlcom) calis this is also not true for ARM.
986 * Because of this, whitelist specific known (series) of CPUs by default, and
987 * leave all others up to the user.
988 */
989static int should_io_be_busy(void)
990{
991#if defined(CONFIG_X86)
992 /*
993 * For Intel, Core 2 (model 15) andl later have an efficient idle.
994 */
995 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
996 boot_cpu_data.x86 == 6 &&
997 boot_cpu_data.x86_model >= 15)
998 return 1;
999#endif
1000 return 0;
1001}
1002
Stephen Boydb94083c2012-10-31 17:43:08 -07001003static void dbs_refresh_callback(struct work_struct *work)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004{
1005 struct cpufreq_policy *policy;
1006 struct cpu_dbs_info_s *this_dbs_info;
Stephen Boydb94083c2012-10-31 17:43:08 -07001007 struct dbs_work_struct *dbs_work;
1008 unsigned int cpu;
Dilip Gudlur17cbd952013-06-17 13:04:31 -07001009 unsigned int target_freq;
Stephen Boydb94083c2012-10-31 17:43:08 -07001010
1011 dbs_work = container_of(work, struct dbs_work_struct, work);
1012 cpu = dbs_work->cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013
Krishna Vankaa3e04d82012-06-08 11:35:43 +05301014 get_online_cpus();
1015
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 if (lock_policy_rwsem_write(cpu) < 0)
Krishna Vankaa3e04d82012-06-08 11:35:43 +05301017 goto bail_acq_sema_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018
1019 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
1020 policy = this_dbs_info->cur_policy;
David Ng4a0a0232011-08-03 14:04:43 -07001021 if (!policy) {
1022 /* CPU not using ondemand governor */
Krishna Vankaa3e04d82012-06-08 11:35:43 +05301023 goto bail_incorrect_governor;
David Ng4a0a0232011-08-03 14:04:43 -07001024 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025
Dilip Gudlur17cbd952013-06-17 13:04:31 -07001026 if (dbs_tuners_ins.input_boost)
1027 target_freq = dbs_tuners_ins.input_boost;
1028 else
1029 target_freq = policy->max;
1030
1031 if (policy->cur < target_freq) {
Anji Jonnalab2408f42012-12-13 14:03:54 +05301032 /*
1033 * Arch specific cpufreq driver may fail.
1034 * Don't update governor frequency upon failure.
1035 */
Dilip Gudlur17cbd952013-06-17 13:04:31 -07001036 if (__cpufreq_driver_target(policy, target_freq,
Anji Jonnalab2408f42012-12-13 14:03:54 +05301037 CPUFREQ_RELATION_L) >= 0)
Dilip Gudlur17cbd952013-06-17 13:04:31 -07001038 policy->cur = target_freq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
Viresh Kumarefa514d2013-05-17 11:26:32 +00001041 &this_dbs_info->prev_cpu_wall, dbs_tuners_ins.io_is_busy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042 }
Krishna Vankaa3e04d82012-06-08 11:35:43 +05301043
1044bail_incorrect_governor:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 unlock_policy_rwsem_write(cpu);
Krishna Vankaa3e04d82012-06-08 11:35:43 +05301046
1047bail_acq_sema_failed:
1048 put_online_cpus();
1049 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050}
1051
Rohit Gupta093a2132013-04-04 15:45:16 -07001052static int dbs_migration_notify(struct notifier_block *nb,
1053 unsigned long target_cpu, void *arg)
1054{
Steve Muckled5d59a52013-05-31 10:39:31 -07001055 struct cpu_dbs_info_s *target_dbs_info =
1056 &per_cpu(od_cpu_dbs_info, target_cpu);
Rohit Gupta093a2132013-04-04 15:45:16 -07001057
Steve Muckled5d59a52013-05-31 10:39:31 -07001058 atomic_set(&target_dbs_info->src_sync_cpu, (int)arg);
Rohit Gupta093a2132013-04-04 15:45:16 -07001059
1060 return NOTIFY_OK;
1061}
1062
1063static struct notifier_block dbs_migration_nb = {
1064 .notifier_call = dbs_migration_notify,
1065};
1066
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001067static void dbs_input_event(struct input_handle *handle, unsigned int type,
1068 unsigned int code, int value)
1069{
1070 int i;
1071
David Ng8192a2f2012-01-19 14:16:19 -08001072 if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
1073 (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
1074 /* nothing to do */
1075 return;
1076 }
1077
Matt Wagantalle3f2f662013-05-23 15:52:49 -07001078 for_each_online_cpu(i)
Stephen Boydb94083c2012-10-31 17:43:08 -07001079 queue_work_on(i, dbs_wq, &per_cpu(dbs_refresh_work, i).work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080}
1081
1082static int dbs_input_connect(struct input_handler *handler,
1083 struct input_dev *dev, const struct input_device_id *id)
1084{
1085 struct input_handle *handle;
1086 int error;
1087
1088 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1089 if (!handle)
1090 return -ENOMEM;
1091
1092 handle->dev = dev;
1093 handle->handler = handler;
1094 handle->name = "cpufreq";
1095
1096 error = input_register_handle(handle);
1097 if (error)
1098 goto err2;
1099
1100 error = input_open_device(handle);
1101 if (error)
1102 goto err1;
1103
1104 return 0;
1105err1:
1106 input_unregister_handle(handle);
1107err2:
1108 kfree(handle);
1109 return error;
1110}
1111
1112static void dbs_input_disconnect(struct input_handle *handle)
1113{
1114 input_close_device(handle);
1115 input_unregister_handle(handle);
1116 kfree(handle);
1117}
1118
1119static const struct input_device_id dbs_ids[] = {
Tingwei Zhangf28dade2013-07-03 16:28:24 +08001120 /* multi-touch touchscreen */
1121 {
1122 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1123 INPUT_DEVICE_ID_MATCH_ABSBIT,
1124 .evbit = { BIT_MASK(EV_ABS) },
1125 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
1126 BIT_MASK(ABS_MT_POSITION_X) |
1127 BIT_MASK(ABS_MT_POSITION_Y) },
1128 },
1129 /* touchpad */
1130 {
1131 .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
1132 INPUT_DEVICE_ID_MATCH_ABSBIT,
1133 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
1134 .absbit = { [BIT_WORD(ABS_X)] =
1135 BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
1136 },
1137 /* Keypad */
1138 {
1139 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1140 .evbit = { BIT_MASK(EV_KEY) },
1141 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 { },
1143};
1144
1145static struct input_handler dbs_input_handler = {
1146 .event = dbs_input_event,
1147 .connect = dbs_input_connect,
1148 .disconnect = dbs_input_disconnect,
1149 .name = "cpufreq_ond",
1150 .id_table = dbs_ids,
1151};
1152
myfluxi5ece4272014-02-06 21:14:57 +01001153static int sync_pending(struct cpu_dbs_info_s *this_dbs_info)
1154{
1155 return atomic_read(&this_dbs_info->src_sync_cpu) >= 0;
1156}
1157
1158static int dbs_sync_should_run(unsigned int dest_cpu)
1159{
1160 struct cpu_dbs_info_s *this_dbs_info;
1161
1162 this_dbs_info = &per_cpu(od_cpu_dbs_info, dest_cpu);
1163
1164 return sync_pending(this_dbs_info);
1165}
1166
1167static void run_dbs_sync(unsigned int dest_cpu)
1168{
1169 int src_cpu, cpu = dest_cpu;
1170 unsigned int src_freq, src_max_load;
1171 struct cpu_dbs_info_s *this_dbs_info, *src_dbs_info;
1172 struct cpufreq_policy *policy;
1173 int delay;
1174
1175 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
1176
1177 get_online_cpus();
1178
1179 src_cpu = atomic_read(&this_dbs_info->src_sync_cpu);
1180 src_dbs_info = &per_cpu(od_cpu_dbs_info, src_cpu);
1181 if (src_dbs_info != NULL && src_dbs_info->cur_policy != NULL) {
1182 src_freq = src_dbs_info->cur_policy->cur;
1183 src_max_load = src_dbs_info->max_load;
1184 } else {
1185 src_freq = dbs_tuners_ins.sync_freq;
1186 src_max_load = 0;
1187 }
1188
1189 if (lock_policy_rwsem_write(cpu) < 0)
1190 goto bail_acq_sema_failed;
1191
1192 if (!atomic_read(&this_dbs_info->sync_enabled)) {
1193 atomic_set(&this_dbs_info->src_sync_cpu, -1);
1194 put_online_cpus();
1195 unlock_policy_rwsem_write(cpu);
1196 return;
1197 }
1198
1199 policy = this_dbs_info->cur_policy;
1200 if (!policy) {
1201 /* CPU not using ondemand governor */
1202 goto bail_incorrect_governor;
1203 }
1204 delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
1205
1206
1207 if (policy->cur < src_freq) {
1208 /* cancel the next ondemand sample */
1209 cancel_delayed_work_sync(&this_dbs_info->work);
1210
1211 /*
1212 * Arch specific cpufreq driver may fail.
1213 * Don't update governor frequency upon failure.
1214 */
1215 if (__cpufreq_driver_target(policy, src_freq,
1216 CPUFREQ_RELATION_L) >= 0) {
1217 policy->cur = src_freq;
1218 if (src_max_load > this_dbs_info->max_load) {
1219 this_dbs_info->max_load = src_max_load;
1220 this_dbs_info->prev_load = src_max_load;
1221 }
1222 }
1223
1224 /* reschedule the next ondemand sample */
1225 mutex_lock(&this_dbs_info->timer_mutex);
1226 schedule_delayed_work_on(cpu, &this_dbs_info->work,
1227 delay);
1228 mutex_unlock(&this_dbs_info->timer_mutex);
1229 }
1230
1231bail_incorrect_governor:
1232 unlock_policy_rwsem_write(cpu);
1233bail_acq_sema_failed:
1234 put_online_cpus();
1235 atomic_set(&this_dbs_info->src_sync_cpu, -1);
1236}
1237
1238static struct smp_hotplug_thread dbs_sync_threads = {
1239 .store = &sync_thread,
1240 .thread_should_run = dbs_sync_should_run,
1241 .thread_fn = run_dbs_sync,
1242 .thread_comm = "dbs_sync/%u",
1243};
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
1246 unsigned int event)
1247{
1248 unsigned int cpu = policy->cpu;
1249 struct cpu_dbs_info_s *this_dbs_info;
1250 unsigned int j;
Jeff Garzik914f7c32006-10-20 14:31:00 -07001251 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Tejun Heo245b2e72009-06-24 15:13:48 +09001253 this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 switch (event) {
1256 case CPUFREQ_GOV_START:
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001257 if ((!cpu_online(cpu)) || (!policy->cur))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EINVAL;
1259
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001260 mutex_lock(&dbs_mutex);
Jeff Garzik914f7c32006-10-20 14:31:00 -07001261
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001262 dbs_enable++;
Rusty Russell835481d2009-01-04 05:18:06 -08001263 for_each_cpu(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct cpu_dbs_info_s *j_dbs_info;
Tejun Heo245b2e72009-06-24 15:13:48 +09001265 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 j_dbs_info->cur_policy = policy;
Dave Jones32ee8c32006-02-28 00:43:23 -05001267
venkatesh.pallipadi@intel.com34305022008-08-04 11:59:09 -07001268 j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
Viresh Kumarefa514d2013-05-17 11:26:32 +00001269 &j_dbs_info->prev_cpu_wall, dbs_tuners_ins.io_is_busy);
Glauber Costa3292beb2011-11-28 14:45:17 -02001270 if (dbs_tuners_ins.ignore_nice)
Venkatesh Pallipadi1ca3abd2009-01-23 09:25:02 -05001271 j_dbs_info->prev_cpu_nice =
Glauber Costa3292beb2011-11-28 14:45:17 -02001272 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Rohit Gupta92daa722013-06-17 17:56:27 -07001273 if (!dbs_tuners_ins.powersave_bias)
1274 atomic_set(&j_dbs_info->sync_enabled, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Venkatesh Pallipadi529af7a2007-02-05 16:12:44 -08001276 this_dbs_info->cpu = cpu;
David C Niemi3f78a9f2010-10-06 16:54:24 -04001277 this_dbs_info->rate_mult = 1;
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001278 ondemand_powersave_bias_init_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * Start the timerschedule work, when this governor
1281 * is used for first time
1282 */
1283 if (dbs_enable == 1) {
1284 unsigned int latency;
Thomas Renninger0e625ac2009-07-24 15:25:06 +02001285
1286 rc = sysfs_create_group(cpufreq_global_kobject,
1287 &dbs_attr_group);
1288 if (rc) {
1289 mutex_unlock(&dbs_mutex);
1290 return rc;
1291 }
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* policy latency is in nS. Convert it to uS first */
Dave Jonesdf8b59b2005-09-20 12:39:35 -07001294 latency = policy->cpuinfo.transition_latency / 1000;
1295 if (latency == 0)
1296 latency = 1;
Thomas Renningercef96152009-04-22 13:48:29 +02001297 /* Bring kernel and HW constraints together */
1298 min_sampling_rate = max(min_sampling_rate,
1299 MIN_LATENCY_MULTIPLIER * latency);
1300 dbs_tuners_ins.sampling_rate =
1301 max(min_sampling_rate,
1302 latency * LATENCY_MULTIPLIER);
Arjan van de Ven19379b12010-05-09 08:26:51 -07001303 dbs_tuners_ins.io_is_busy = should_io_be_busy();
Narayanan Gopalakrishnanfabf0f12012-10-19 17:24:53 -07001304
1305 if (dbs_tuners_ins.optimal_freq == 0)
1306 dbs_tuners_ins.optimal_freq = policy->min;
1307
1308 if (dbs_tuners_ins.sync_freq == 0)
1309 dbs_tuners_ins.sync_freq = policy->min;
Rohit Gupta093a2132013-04-04 15:45:16 -07001310
1311 atomic_notifier_chain_register(&migration_notifier_head,
1312 &dbs_migration_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314 if (!cpu)
1315 rc = input_register_handler(&dbs_input_handler);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001316 mutex_unlock(&dbs_mutex);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001317
David Ng8192a2f2012-01-19 14:16:19 -08001318
1319 if (!ondemand_powersave_bias_setspeed(
1320 this_dbs_info->cur_policy,
1321 NULL,
1322 dbs_tuners_ins.powersave_bias))
1323 dbs_timer_init(this_dbs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325
1326 case CPUFREQ_GOV_STOP:
Linus Torvalds2cd7cbd2006-07-23 12:05:00 -07001327 dbs_timer_exit(this_dbs_info);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001328
1329 mutex_lock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 dbs_enable--;
Steve Muckled5d59a52013-05-31 10:39:31 -07001331
1332 for_each_cpu(j, policy->cpus) {
1333 struct cpu_dbs_info_s *j_dbs_info;
1334 j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
1335 atomic_set(&j_dbs_info->sync_enabled, 0);
1336 }
1337
Anitha Anand3dd65092012-01-18 17:17:40 -08001338 /* If device is being removed, policy is no longer
1339 * valid. */
1340 this_dbs_info->cur_policy = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 if (!cpu)
1342 input_unregister_handler(&dbs_input_handler);
Rohit Gupta093a2132013-04-04 15:45:16 -07001343 if (!dbs_enable) {
Thomas Renninger0e625ac2009-07-24 15:25:06 +02001344 sysfs_remove_group(cpufreq_global_kobject,
1345 &dbs_attr_group);
Rohit Gupta093a2132013-04-04 15:45:16 -07001346 atomic_notifier_chain_unregister(
1347 &migration_notifier_head,
1348 &dbs_migration_nb);
1349 }
1350
Swetha Chikkaboraiahfbb12f32013-10-31 17:55:48 +05301351 mutex_unlock(&dbs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 break;
1354
1355 case CPUFREQ_GOV_LIMITS:
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001356 mutex_lock(&this_dbs_info->timer_mutex);
Swetha Chikkaboraiahb69d1592013-11-20 17:24:06 +05301357 if (!this_dbs_info->cur_policy) {
1358 pr_err("Dbs policy is NULL\n");
1359 mutex_unlock(&this_dbs_info->timer_mutex);
1360 return -EINVAL;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (policy->max < this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001363 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -05001364 policy->max, CPUFREQ_RELATION_H);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 else if (policy->min > this_dbs_info->cur_policy->cur)
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001366 __cpufreq_driver_target(this_dbs_info->cur_policy,
Dave Jones2b03f892009-01-18 01:43:44 -05001367 policy->min, CPUFREQ_RELATION_L);
David Ng8192a2f2012-01-19 14:16:19 -08001368 else if (dbs_tuners_ins.powersave_bias != 0)
1369 ondemand_powersave_bias_setspeed(
1370 this_dbs_info->cur_policy,
1371 policy,
1372 dbs_tuners_ins.powersave_bias);
venkatesh.pallipadi@intel.com5a75c822009-07-02 17:08:32 -07001373 mutex_unlock(&this_dbs_info->timer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
1375 }
1376 return 0;
1377}
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379static int __init cpufreq_gov_dbs_init(void)
1380{
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001381 u64 idle_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382 unsigned int i;
myfluxi5ece4272014-02-06 21:14:57 +01001383 int rc, cpu = get_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001384
Kamalesh Babulal21f2e3c2011-12-09 16:18:42 +05301385 idle_time = get_cpu_idle_time_us(cpu, NULL);
Andrea Righi4f6e6b92008-09-18 10:43:40 +00001386 put_cpu();
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001387 if (idle_time != -1ULL) {
1388 /* Idle micro accounting is supported. Use finer thresholds */
1389 dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
1390 dbs_tuners_ins.down_differential =
1391 MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
Thomas Renningercef96152009-04-22 13:48:29 +02001392 /*
Paul Bollebd74b322011-08-06 14:33:43 +02001393 * In nohz/micro accounting case we set the minimum frequency
Thomas Renningercef96152009-04-22 13:48:29 +02001394 * not depending on HZ, but fixed (very low). The deferred
1395 * timer might skip some samples if idle/sleeping as needed.
1396 */
1397 min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
1398 } else {
1399 /* For correct statistics, we need 10 ticks for each measure */
1400 min_sampling_rate =
1401 MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
venkatesh.pallipadi@intel.com80800912008-08-04 11:59:12 -07001402 }
Akinobu Mita888a7942008-07-14 12:00:45 +09001403
Matt Wagantalle3f2f662013-05-23 15:52:49 -07001404 dbs_wq = alloc_workqueue("ondemand_dbs_wq", WQ_HIGHPRI, 0);
1405 if (!dbs_wq) {
1406 printk(KERN_ERR "Failed to create ondemand_dbs_wq workqueue\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407 return -EFAULT;
1408 }
1409 for_each_possible_cpu(i) {
Praveen Chidambaram457a4452012-07-19 10:45:07 -06001410 struct cpu_dbs_info_s *this_dbs_info =
1411 &per_cpu(od_cpu_dbs_info, i);
Stephen Boydb94083c2012-10-31 17:43:08 -07001412 struct dbs_work_struct *dbs_work =
1413 &per_cpu(dbs_refresh_work, i);
Rohit Gupta093a2132013-04-04 15:45:16 -07001414
Praveen Chidambaram457a4452012-07-19 10:45:07 -06001415 mutex_init(&this_dbs_info->timer_mutex);
Stephen Boydb94083c2012-10-31 17:43:08 -07001416 INIT_WORK(&dbs_work->work, dbs_refresh_callback);
1417 dbs_work->cpu = i;
Rohit Gupta093a2132013-04-04 15:45:16 -07001418
Steve Muckled5d59a52013-05-31 10:39:31 -07001419 atomic_set(&this_dbs_info->src_sync_cpu, -1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420 }
1421
myfluxi5ece4272014-02-06 21:14:57 +01001422 rc = smpboot_register_percpu_thread(&dbs_sync_threads);
1423 if (rc)
1424 printk(KERN_ERR "Failed to register dbs_sync threads\n");
1425
Tejun Heo57df5572011-01-26 12:12:50 +01001426 return cpufreq_register_governor(&cpufreq_gov_ondemand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429static void __exit cpufreq_gov_dbs_exit(void)
1430{
Anji Jonnala1676aad2012-11-14 13:34:54 +05301431 unsigned int i;
1432
Thomas Renninger1c256242007-10-02 13:28:12 -07001433 cpufreq_unregister_governor(&cpufreq_gov_ondemand);
Anji Jonnala1676aad2012-11-14 13:34:54 +05301434 for_each_possible_cpu(i) {
1435 struct cpu_dbs_info_s *this_dbs_info =
1436 &per_cpu(od_cpu_dbs_info, i);
1437 mutex_destroy(&this_dbs_info->timer_mutex);
1438 }
Matt Wagantalle3f2f662013-05-23 15:52:49 -07001439 destroy_workqueue(dbs_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
1442
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001443MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
1444MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
1445MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
Dave Jones2b03f892009-01-18 01:43:44 -05001446 "Low Latency Frequency Transition capable processors");
Venkatesh Pallipadiffac80e2006-06-28 13:52:18 -07001447MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Johannes Weiner69157192008-01-17 15:21:08 -08001449#ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
1450fs_initcall(cpufreq_gov_dbs_init);
1451#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452module_init(cpufreq_gov_dbs_init);
Johannes Weiner69157192008-01-17 15:21:08 -08001453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454module_exit(cpufreq_gov_dbs_exit);