usb: gadget: g_ffs: Allow to set bmAttributes of configuration
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / cpufreq / cpufreq_governor.c
1 /*
2  * drivers/cpufreq/cpufreq_governor.c
3  *
4  * CPUFREQ governors common code
5  *
6  * Copyright    (C) 2001 Russell King
7  *              (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
8  *              (C) 2003 Jun Nakajima <jun.nakajima@intel.com>
9  *              (C) 2009 Alexander Clouter <alex@digriz.org.uk>
10  *              (c) 2012 Viresh Kumar <viresh.kumar@linaro.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <asm/cputime.h>
20 #include <linux/cpufreq.h>
21 #include <linux/cpumask.h>
22 #include <linux/export.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/types.h>
27 #include <linux/workqueue.h>
28 #if defined(CONFIG_SYSTEM_LOAD_ANALYZER)
29 #include <linux/load_analyzer.h>
30 #endif
31
32 #include "cpufreq_governor.h"
33
34 static struct attribute_group *get_sysfs_attr(struct dbs_data *dbs_data)
35 {
36         if (have_governor_per_policy())
37                 return dbs_data->cdata->attr_group_gov_pol;
38         else
39                 return dbs_data->cdata->attr_group_gov_sys;
40 }
41
42 void dbs_check_cpu(struct dbs_data *dbs_data, int cpu)
43 {
44         struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
45         struct od_dbs_tuners *od_tuners = dbs_data->tuners;
46         struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
47         struct cpufreq_policy *policy;
48         unsigned int max_load = 0;
49         unsigned int ignore_nice;
50         unsigned int j;
51
52 #if defined(CONFIG_SYSTEM_LOAD_ANALYZER)
53         unsigned int cpu_load[CPU_NUM];
54         unsigned int cpufreq[CPU_NUM];
55 #endif
56
57         if (dbs_data->cdata->governor == GOV_ONDEMAND)
58                 ignore_nice = od_tuners->ignore_nice_load;
59         else
60                 ignore_nice = cs_tuners->ignore_nice_load;
61
62         policy = cdbs->cur_policy;
63
64 #if defined(CONFIG_SYSTEM_LOAD_ANALYZER)
65         for (j = 0; j < CPU_NUM; j++){
66                 cpu_load[j] = 0;
67                 cpufreq[j] = 0;
68         }
69 #endif
70
71         /* Get Absolute Load */
72         for_each_cpu(j, policy->cpus) {
73                 struct cpu_dbs_common_info *j_cdbs;
74                 u64 cur_wall_time, cur_idle_time;
75                 unsigned int idle_time, wall_time;
76                 unsigned int load;
77                 int io_busy = 0;
78
79                 j_cdbs = dbs_data->cdata->get_cpu_cdbs(j);
80
81                 /*
82                  * For the purpose of ondemand, waiting for disk IO is
83                  * an indication that you're performance critical, and
84                  * not that the system is actually idle. So do not add
85                  * the iowait time to the cpu idle time.
86                  */
87                 if (dbs_data->cdata->governor == GOV_ONDEMAND)
88                         io_busy = od_tuners->io_is_busy;
89                 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy);
90
91                 wall_time = (unsigned int)
92                         (cur_wall_time - j_cdbs->prev_cpu_wall);
93                 j_cdbs->prev_cpu_wall = cur_wall_time;
94
95                 idle_time = (unsigned int)
96                         (cur_idle_time - j_cdbs->prev_cpu_idle);
97                 j_cdbs->prev_cpu_idle = cur_idle_time;
98
99                 if (ignore_nice) {
100                         u64 cur_nice;
101                         unsigned long cur_nice_jiffies;
102
103                         cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
104                                          cdbs->prev_cpu_nice;
105                         /*
106                          * Assumption: nice time between sampling periods will
107                          * be less than 2^32 jiffies for 32 bit sys
108                          */
109                         cur_nice_jiffies = (unsigned long)
110                                         cputime64_to_jiffies64(cur_nice);
111
112                         cdbs->prev_cpu_nice =
113                                 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
114                         idle_time += jiffies_to_usecs(cur_nice_jiffies);
115                 }
116
117                 if (unlikely(!wall_time || wall_time < idle_time))
118                         continue;
119
120                 load = 100 * (wall_time - idle_time) / wall_time;
121         #if defined(CONFIG_SYSTEM_LOAD_ANALYZER)
122                 cpu_load[j] = load;
123         #endif
124
125                 if (load > max_load)
126                         max_load = load;
127         }
128
129 #if defined(CONFIG_SYSTEM_LOAD_ANALYZER)
130         for(j=0;j<CPU_NUM; j++) {
131                 struct cpufreq_policy *policy;
132                 policy = cpufreq_cpu_get(j);
133                 if (policy !=NULL){
134                         cpufreq[j] = policy->cur;
135                 }
136         }
137
138         store_cpu_load(cpufreq, cpu_load);
139 #endif
140
141         dbs_data->cdata->gov_check_cpu(cpu, max_load);
142 }
143 EXPORT_SYMBOL_GPL(dbs_check_cpu);
144
145 static inline void __gov_queue_work(int cpu, struct dbs_data *dbs_data,
146                 unsigned int delay)
147 {
148         struct cpu_dbs_common_info *cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
149
150         mod_delayed_work_on(cpu, system_wq, &cdbs->work, delay);
151 }
152
153 void gov_queue_work(struct dbs_data *dbs_data, struct cpufreq_policy *policy,
154                 unsigned int delay, bool all_cpus)
155 {
156         int i;
157
158         if (!policy->governor_enabled)
159                 return;
160
161         if (!all_cpus) {
162                 __gov_queue_work(smp_processor_id(), dbs_data, delay);
163         } else {
164                 for_each_cpu(i, policy->cpus)
165                         __gov_queue_work(i, dbs_data, delay);
166         }
167 }
168 EXPORT_SYMBOL_GPL(gov_queue_work);
169
170 static inline void gov_cancel_work(struct dbs_data *dbs_data,
171                 struct cpufreq_policy *policy)
172 {
173         struct cpu_dbs_common_info *cdbs;
174         int i;
175
176         for_each_cpu(i, policy->cpus) {
177                 cdbs = dbs_data->cdata->get_cpu_cdbs(i);
178                 cancel_delayed_work_sync(&cdbs->work);
179         }
180 }
181
182 /* Will return if we need to evaluate cpu load again or not */
183 bool need_load_eval(struct cpu_dbs_common_info *cdbs,
184                 unsigned int sampling_rate)
185 {
186         if (policy_is_shared(cdbs->cur_policy)) {
187                 ktime_t time_now = ktime_get();
188                 s64 delta_us = ktime_us_delta(time_now, cdbs->time_stamp);
189
190                 /* Do nothing if we recently have sampled */
191                 if (delta_us < (s64)(sampling_rate / 2))
192                         return false;
193                 else
194                         cdbs->time_stamp = time_now;
195         }
196
197         return true;
198 }
199 EXPORT_SYMBOL_GPL(need_load_eval);
200
201 static void set_sampling_rate(struct dbs_data *dbs_data,
202                 unsigned int sampling_rate)
203 {
204         if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
205                 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
206                 cs_tuners->sampling_rate = sampling_rate;
207         } else {
208                 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
209                 od_tuners->sampling_rate = sampling_rate;
210         }
211 }
212
213 int cpufreq_governor_dbs(struct cpufreq_policy *policy,
214                 struct common_dbs_data *cdata, unsigned int event)
215 {
216         struct dbs_data *dbs_data;
217         struct od_cpu_dbs_info_s *od_dbs_info = NULL;
218         struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
219         struct od_ops *od_ops = NULL;
220         struct od_dbs_tuners *od_tuners = NULL;
221         struct cs_dbs_tuners *cs_tuners = NULL;
222         struct cpu_dbs_common_info *cpu_cdbs;
223         unsigned int sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
224         int io_busy = 0;
225         int rc;
226
227         if (have_governor_per_policy())
228                 dbs_data = policy->governor_data;
229         else
230                 dbs_data = cdata->gdbs_data;
231         WARN_ON(!dbs_data && (event != CPUFREQ_GOV_POLICY_INIT));
232
233         switch (event) {
234         case CPUFREQ_GOV_POLICY_INIT:
235                 if (have_governor_per_policy()) {
236                         WARN_ON(dbs_data);
237                 } else if (dbs_data) {
238                         dbs_data->usage_count++;
239                         policy->governor_data = dbs_data;
240                         return 0;
241                 }
242
243                 dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL);
244                 if (!dbs_data) {
245                         pr_err("%s: POLICY_INIT: kzalloc failed\n", __func__);
246                         return -ENOMEM;
247                 }
248
249                 dbs_data->cdata = cdata;
250                 dbs_data->usage_count = 1;
251                 rc = cdata->init(dbs_data);
252                 if (rc) {
253                         pr_err("%s: POLICY_INIT: init() failed\n", __func__);
254                         kfree(dbs_data);
255                         return rc;
256                 }
257
258                 rc = sysfs_create_group(get_governor_parent_kobj(policy),
259                                 get_sysfs_attr(dbs_data));
260                 if (rc) {
261                         cdata->exit(dbs_data);
262                         kfree(dbs_data);
263                         return rc;
264                 }
265
266                 policy->governor_data = dbs_data;
267
268                 /* policy latency is in nS. Convert it to uS first */
269                 latency = policy->cpuinfo.transition_latency / 1000;
270                 if (latency == 0)
271                         latency = 1;
272
273                 /* Bring kernel and HW constraints together */
274                 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
275                                 MIN_LATENCY_MULTIPLIER * latency);
276                 set_sampling_rate(dbs_data, max(dbs_data->min_sampling_rate,
277                                         latency * LATENCY_MULTIPLIER));
278
279                 if ((cdata->governor == GOV_CONSERVATIVE) &&
280                                 (!policy->governor->initialized)) {
281                         struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
282
283                         cpufreq_register_notifier(cs_ops->notifier_block,
284                                         CPUFREQ_TRANSITION_NOTIFIER);
285                 }
286
287                 if (!have_governor_per_policy())
288                         cdata->gdbs_data = dbs_data;
289
290                 return 0;
291         case CPUFREQ_GOV_POLICY_EXIT:
292                 if (!--dbs_data->usage_count) {
293                         sysfs_remove_group(get_governor_parent_kobj(policy),
294                                         get_sysfs_attr(dbs_data));
295
296                         if ((dbs_data->cdata->governor == GOV_CONSERVATIVE) &&
297                                 (policy->governor->initialized == 1)) {
298                                 struct cs_ops *cs_ops = dbs_data->cdata->gov_ops;
299
300                                 cpufreq_unregister_notifier(cs_ops->notifier_block,
301                                                 CPUFREQ_TRANSITION_NOTIFIER);
302                         }
303
304                         cdata->exit(dbs_data);
305                         kfree(dbs_data);
306                         cdata->gdbs_data = NULL;
307                 }
308
309                 policy->governor_data = NULL;
310                 return 0;
311         }
312
313         cpu_cdbs = dbs_data->cdata->get_cpu_cdbs(cpu);
314
315         if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
316                 cs_tuners = dbs_data->tuners;
317                 cs_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
318                 sampling_rate = cs_tuners->sampling_rate;
319                 ignore_nice = cs_tuners->ignore_nice_load;
320         } else {
321                 od_tuners = dbs_data->tuners;
322                 od_dbs_info = dbs_data->cdata->get_cpu_dbs_info_s(cpu);
323                 sampling_rate = od_tuners->sampling_rate;
324                 ignore_nice = od_tuners->ignore_nice_load;
325                 od_ops = dbs_data->cdata->gov_ops;
326                 io_busy = od_tuners->io_is_busy;
327         }
328
329         switch (event) {
330         case CPUFREQ_GOV_START:
331                 if (!policy->cur)
332                         return -EINVAL;
333
334                 mutex_lock(&dbs_data->mutex);
335
336                 for_each_cpu(j, policy->cpus) {
337                         struct cpu_dbs_common_info *j_cdbs =
338                                 dbs_data->cdata->get_cpu_cdbs(j);
339
340                         j_cdbs->cpu = j;
341                         j_cdbs->cur_policy = policy;
342                         j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
343                                                &j_cdbs->prev_cpu_wall, io_busy);
344
345                         if (ignore_nice)
346                                 j_cdbs->prev_cpu_nice =
347                                         kcpustat_cpu(j).cpustat[CPUTIME_NICE];
348
349                         mutex_init(&j_cdbs->timer_mutex);
350                         INIT_DEFERRABLE_WORK(&j_cdbs->work,
351                                              dbs_data->cdata->gov_dbs_timer);
352                 }
353
354                 /*
355                  * conservative does not implement micro like ondemand
356                  * governor, thus we are bound to jiffes/HZ
357                  */
358                 if (dbs_data->cdata->governor == GOV_CONSERVATIVE) {
359                         cs_dbs_info->down_skip = 0;
360                         cs_dbs_info->enable = 1;
361                         cs_dbs_info->requested_freq = policy->cur;
362                 } else {
363                         od_dbs_info->rate_mult = 1;
364                         od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
365                         od_ops->powersave_bias_init_cpu(cpu);
366                 }
367
368                 mutex_unlock(&dbs_data->mutex);
369
370                 /* Initiate timer time stamp */
371                 cpu_cdbs->time_stamp = ktime_get();
372
373                 gov_queue_work(dbs_data, policy,
374                                 delay_for_sampling_rate(sampling_rate), true);
375                 break;
376
377         case CPUFREQ_GOV_STOP:
378                 if (dbs_data->cdata->governor == GOV_CONSERVATIVE)
379                         cs_dbs_info->enable = 0;
380
381                 gov_cancel_work(dbs_data, policy);
382
383                 mutex_lock(&dbs_data->mutex);
384                 mutex_destroy(&cpu_cdbs->timer_mutex);
385                 cpu_cdbs->cur_policy = NULL;
386
387                 mutex_unlock(&dbs_data->mutex);
388
389                 break;
390
391         case CPUFREQ_GOV_LIMITS:
392                 mutex_lock(&dbs_data->mutex);
393                 if (!cpu_cdbs->cur_policy) {
394                         mutex_unlock(&dbs_data->mutex);
395                         break;
396                 }
397                 mutex_lock(&cpu_cdbs->timer_mutex);
398                 if (policy->max < cpu_cdbs->cur_policy->cur)
399                         __cpufreq_driver_target(cpu_cdbs->cur_policy,
400                                         policy->max, CPUFREQ_RELATION_H);
401                 else if (policy->min > cpu_cdbs->cur_policy->cur)
402                         __cpufreq_driver_target(cpu_cdbs->cur_policy,
403                                         policy->min, CPUFREQ_RELATION_L);
404 #ifndef CONFIG_CPU_FREQ_GOV_SPRDEMAND
405                 dbs_check_cpu(dbs_data, cpu);
406 #endif
407                 mutex_unlock(&cpu_cdbs->timer_mutex);
408                 mutex_unlock(&dbs_data->mutex);
409                 break;
410         }
411         return 0;
412 }
413 EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);