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