2 * drivers/cpufreq/cpufreq_governor.c
4 * CPUFREQ governors common code
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>
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.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/tick.h>
26 #include <linux/types.h>
27 #include <linux/workqueue.h>
29 #include "cpufreq_governor.h"
31 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
37 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
39 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
40 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
41 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
42 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
43 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
44 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
46 idle_time = cur_wall_time - busy_time;
48 *wall = cputime_to_usecs(cur_wall_time);
50 return cputime_to_usecs(idle_time);
53 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall)
55 u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
57 if (idle_time == -1ULL)
58 return get_cpu_idle_time_jiffy(cpu, wall);
60 idle_time += get_cpu_iowait_time_us(cpu, wall);
64 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
66 void dbs_check_cpu(struct dbs_data *dbs_data, int cpu)
68 struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
69 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
70 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
71 struct cpufreq_policy *policy;
72 unsigned int max_load = 0;
73 unsigned int ignore_nice;
76 if (dbs_data->governor == GOV_ONDEMAND)
77 ignore_nice = od_tuners->ignore_nice;
79 ignore_nice = cs_tuners->ignore_nice;
81 policy = cdbs->cur_policy;
83 /* Get Absolute Load (in terms of freq for ondemand gov) */
84 for_each_cpu(j, policy->cpus) {
85 struct cpu_dbs_common_info *j_cdbs;
86 u64 cur_wall_time, cur_idle_time, cur_iowait_time;
87 unsigned int idle_time, wall_time, iowait_time;
90 j_cdbs = dbs_data->get_cpu_cdbs(j);
92 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
94 wall_time = (unsigned int)
95 (cur_wall_time - j_cdbs->prev_cpu_wall);
96 j_cdbs->prev_cpu_wall = cur_wall_time;
98 idle_time = (unsigned int)
99 (cur_idle_time - j_cdbs->prev_cpu_idle);
100 j_cdbs->prev_cpu_idle = cur_idle_time;
104 unsigned long cur_nice_jiffies;
106 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
109 * Assumption: nice time between sampling periods will
110 * be less than 2^32 jiffies for 32 bit sys
112 cur_nice_jiffies = (unsigned long)
113 cputime64_to_jiffies64(cur_nice);
115 cdbs->prev_cpu_nice =
116 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
117 idle_time += jiffies_to_usecs(cur_nice_jiffies);
120 if (dbs_data->governor == GOV_ONDEMAND) {
121 struct od_cpu_dbs_info_s *od_j_dbs_info =
122 dbs_data->get_cpu_dbs_info_s(cpu);
124 cur_iowait_time = get_cpu_iowait_time_us(j,
126 if (cur_iowait_time == -1ULL)
129 iowait_time = (unsigned int) (cur_iowait_time -
130 od_j_dbs_info->prev_cpu_iowait);
131 od_j_dbs_info->prev_cpu_iowait = cur_iowait_time;
134 * For the purpose of ondemand, waiting for disk IO is
135 * an indication that you're performance critical, and
136 * not that the system is actually idle. So subtract the
137 * iowait time from the cpu idle time.
139 if (od_tuners->io_is_busy && idle_time >= iowait_time)
140 idle_time -= iowait_time;
143 if (unlikely(!wall_time || wall_time < idle_time))
146 load = 100 * (wall_time - idle_time) / wall_time;
148 if (dbs_data->governor == GOV_ONDEMAND) {
149 int freq_avg = __cpufreq_driver_getavg(policy, j);
151 freq_avg = policy->cur;
160 dbs_data->gov_check_cpu(cpu, max_load);
162 EXPORT_SYMBOL_GPL(dbs_check_cpu);
164 static inline void dbs_timer_init(struct dbs_data *dbs_data, int cpu,
165 unsigned int sampling_rate)
167 int delay = delay_for_sampling_rate(sampling_rate);
168 struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
170 schedule_delayed_work_on(cpu, &cdbs->work, delay);
173 static inline void dbs_timer_exit(struct dbs_data *dbs_data, int cpu)
175 struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
177 cancel_delayed_work_sync(&cdbs->work);
180 int cpufreq_governor_dbs(struct dbs_data *dbs_data,
181 struct cpufreq_policy *policy, unsigned int event)
183 struct od_cpu_dbs_info_s *od_dbs_info = NULL;
184 struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
185 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
186 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
187 struct cpu_dbs_common_info *cpu_cdbs;
188 unsigned int *sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
191 cpu_cdbs = dbs_data->get_cpu_cdbs(cpu);
193 if (dbs_data->governor == GOV_CONSERVATIVE) {
194 cs_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
195 sampling_rate = &cs_tuners->sampling_rate;
196 ignore_nice = cs_tuners->ignore_nice;
198 od_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
199 sampling_rate = &od_tuners->sampling_rate;
200 ignore_nice = od_tuners->ignore_nice;
204 case CPUFREQ_GOV_START:
205 if ((!cpu_online(cpu)) || (!policy->cur))
208 mutex_lock(&dbs_data->mutex);
211 for_each_cpu(j, policy->cpus) {
212 struct cpu_dbs_common_info *j_cdbs;
213 j_cdbs = dbs_data->get_cpu_cdbs(j);
216 j_cdbs->cur_policy = policy;
217 j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
218 &j_cdbs->prev_cpu_wall);
220 j_cdbs->prev_cpu_nice =
221 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
223 mutex_init(&j_cdbs->timer_mutex);
224 INIT_DEFERRABLE_WORK(&j_cdbs->work,
225 dbs_data->gov_dbs_timer);
229 * Start the timerschedule work, when this governor is used for
232 if (dbs_data->enable != 1)
235 rc = sysfs_create_group(cpufreq_global_kobject,
236 dbs_data->attr_group);
238 mutex_unlock(&dbs_data->mutex);
242 /* policy latency is in nS. Convert it to uS first */
243 latency = policy->cpuinfo.transition_latency / 1000;
248 * conservative does not implement micro like ondemand
249 * governor, thus we are bound to jiffes/HZ
251 if (dbs_data->governor == GOV_CONSERVATIVE) {
252 struct cs_ops *ops = dbs_data->gov_ops;
254 cpufreq_register_notifier(ops->notifier_block,
255 CPUFREQ_TRANSITION_NOTIFIER);
257 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
258 jiffies_to_usecs(10);
260 struct od_ops *ops = dbs_data->gov_ops;
262 od_tuners->io_is_busy = ops->io_busy();
265 /* Bring kernel and HW constraints together */
266 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
267 MIN_LATENCY_MULTIPLIER * latency);
268 *sampling_rate = max(dbs_data->min_sampling_rate, latency *
272 if (dbs_data->governor == GOV_CONSERVATIVE) {
273 cs_dbs_info->down_skip = 0;
274 cs_dbs_info->enable = 1;
275 cs_dbs_info->requested_freq = policy->cur;
277 struct od_ops *ops = dbs_data->gov_ops;
278 od_dbs_info->rate_mult = 1;
279 od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
280 ops->powersave_bias_init_cpu(cpu);
282 mutex_unlock(&dbs_data->mutex);
284 /* Initiate timer time stamp */
285 cpu_cdbs->time_stamp = ktime_get();
287 for_each_cpu(j, policy->cpus)
288 dbs_timer_init(dbs_data, j, *sampling_rate);
291 case CPUFREQ_GOV_STOP:
292 if (dbs_data->governor == GOV_CONSERVATIVE)
293 cs_dbs_info->enable = 0;
295 for_each_cpu(j, policy->cpus)
296 dbs_timer_exit(dbs_data, j);
298 mutex_lock(&dbs_data->mutex);
299 mutex_destroy(&cpu_cdbs->timer_mutex);
301 if (!dbs_data->enable) {
302 struct cs_ops *ops = dbs_data->gov_ops;
304 sysfs_remove_group(cpufreq_global_kobject,
305 dbs_data->attr_group);
306 if (dbs_data->governor == GOV_CONSERVATIVE)
307 cpufreq_unregister_notifier(ops->notifier_block,
308 CPUFREQ_TRANSITION_NOTIFIER);
310 mutex_unlock(&dbs_data->mutex);
314 case CPUFREQ_GOV_LIMITS:
315 mutex_lock(&cpu_cdbs->timer_mutex);
316 if (policy->max < cpu_cdbs->cur_policy->cur)
317 __cpufreq_driver_target(cpu_cdbs->cur_policy,
318 policy->max, CPUFREQ_RELATION_H);
319 else if (policy->min > cpu_cdbs->cur_policy->cur)
320 __cpufreq_driver_target(cpu_cdbs->cur_policy,
321 policy->min, CPUFREQ_RELATION_L);
322 dbs_check_cpu(dbs_data, cpu);
323 mutex_unlock(&cpu_cdbs->timer_mutex);
328 EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);