blob: 29d6a59b1a15bfac33fd526db452fb70ed764818 [file] [log] [blame]
viresh kumar2aacdff2012-10-23 01:28:05 +02001/*
2 * drivers/cpufreq/cpufreq_governor.c
3 *
4 * CPUFREQ governors common code
5 *
Viresh Kumar4471a342012-10-26 00:47:42 +02006 * 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 *
viresh kumar2aacdff2012-10-23 01:28:05 +020012 * 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
Viresh Kumar4471a342012-10-26 00:47:42 +020017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
viresh kumar2aacdff2012-10-23 01:28:05 +020019#include <asm/cputime.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020020#include <linux/cpufreq.h>
21#include <linux/cpumask.h>
viresh kumar2aacdff2012-10-23 01:28:05 +020022#include <linux/export.h>
23#include <linux/kernel_stat.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020024#include <linux/mutex.h>
viresh kumar2aacdff2012-10-23 01:28:05 +020025#include <linux/tick.h>
26#include <linux/types.h>
Viresh Kumar4471a342012-10-26 00:47:42 +020027#include <linux/workqueue.h>
28
29#include "cpufreq_governor.h"
30
viresh kumar2aacdff2012-10-23 01:28:05 +020031static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
32{
33 u64 idle_time;
34 u64 cur_wall_time;
35 u64 busy_time;
36
37 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
38
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];
45
46 idle_time = cur_wall_time - busy_time;
47 if (wall)
Rafael J. Wysockia0e5af32012-11-24 10:08:47 +010048 *wall = cputime_to_usecs(cur_wall_time);
viresh kumar2aacdff2012-10-23 01:28:05 +020049
Rafael J. Wysockia0e5af32012-11-24 10:08:47 +010050 return cputime_to_usecs(idle_time);
viresh kumar2aacdff2012-10-23 01:28:05 +020051}
52
Viresh Kumar1e7586a2012-10-26 00:51:21 +020053u64 get_cpu_idle_time(unsigned int cpu, u64 *wall)
viresh kumar2aacdff2012-10-23 01:28:05 +020054{
55 u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
56
57 if (idle_time == -1ULL)
58 return get_cpu_idle_time_jiffy(cpu, wall);
59 else
60 idle_time += get_cpu_iowait_time_us(cpu, wall);
61
62 return idle_time;
63}
64EXPORT_SYMBOL_GPL(get_cpu_idle_time);
Viresh Kumar4471a342012-10-26 00:47:42 +020065
66void dbs_check_cpu(struct dbs_data *dbs_data, int cpu)
67{
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;
74 unsigned int j;
75
76 if (dbs_data->governor == GOV_ONDEMAND)
77 ignore_nice = od_tuners->ignore_nice;
78 else
79 ignore_nice = cs_tuners->ignore_nice;
80
81 policy = cdbs->cur_policy;
82
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;
Viresh Kumar1e7586a2012-10-26 00:51:21 +020086 u64 cur_wall_time, cur_idle_time, cur_iowait_time;
Viresh Kumar4471a342012-10-26 00:47:42 +020087 unsigned int idle_time, wall_time, iowait_time;
88 unsigned int load;
89
90 j_cdbs = dbs_data->get_cpu_cdbs(j);
91
92 cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
93
94 wall_time = (unsigned int)
95 (cur_wall_time - j_cdbs->prev_cpu_wall);
96 j_cdbs->prev_cpu_wall = cur_wall_time;
97
98 idle_time = (unsigned int)
99 (cur_idle_time - j_cdbs->prev_cpu_idle);
100 j_cdbs->prev_cpu_idle = cur_idle_time;
101
102 if (ignore_nice) {
103 u64 cur_nice;
104 unsigned long cur_nice_jiffies;
105
106 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
107 cdbs->prev_cpu_nice;
108 /*
109 * Assumption: nice time between sampling periods will
110 * be less than 2^32 jiffies for 32 bit sys
111 */
112 cur_nice_jiffies = (unsigned long)
113 cputime64_to_jiffies64(cur_nice);
114
115 cdbs->prev_cpu_nice =
116 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
117 idle_time += jiffies_to_usecs(cur_nice_jiffies);
118 }
119
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);
123
124 cur_iowait_time = get_cpu_iowait_time_us(j,
125 &cur_wall_time);
126 if (cur_iowait_time == -1ULL)
127 cur_iowait_time = 0;
128
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;
132
133 /*
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.
138 */
139 if (od_tuners->io_is_busy && idle_time >= iowait_time)
140 idle_time -= iowait_time;
141 }
142
143 if (unlikely(!wall_time || wall_time < idle_time))
144 continue;
145
146 load = 100 * (wall_time - idle_time) / wall_time;
147
148 if (dbs_data->governor == GOV_ONDEMAND) {
149 int freq_avg = __cpufreq_driver_getavg(policy, j);
150 if (freq_avg <= 0)
151 freq_avg = policy->cur;
152
153 load *= freq_avg;
154 }
155
156 if (load > max_load)
157 max_load = load;
158 }
159
160 dbs_data->gov_check_cpu(cpu, max_load);
161}
162EXPORT_SYMBOL_GPL(dbs_check_cpu);
163
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000164static inline void dbs_timer_init(struct dbs_data *dbs_data, int cpu,
165 unsigned int sampling_rate)
Viresh Kumar4471a342012-10-26 00:47:42 +0200166{
167 int delay = delay_for_sampling_rate(sampling_rate);
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000168 struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
Viresh Kumar4471a342012-10-26 00:47:42 +0200169
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000170 schedule_delayed_work_on(cpu, &cdbs->work, delay);
Viresh Kumar4471a342012-10-26 00:47:42 +0200171}
172
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000173static inline void dbs_timer_exit(struct dbs_data *dbs_data, int cpu)
Viresh Kumar4471a342012-10-26 00:47:42 +0200174{
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000175 struct cpu_dbs_common_info *cdbs = dbs_data->get_cpu_cdbs(cpu);
176
Viresh Kumar4471a342012-10-26 00:47:42 +0200177 cancel_delayed_work_sync(&cdbs->work);
178}
179
180int cpufreq_governor_dbs(struct dbs_data *dbs_data,
181 struct cpufreq_policy *policy, unsigned int event)
182{
183 struct od_cpu_dbs_info_s *od_dbs_info = NULL;
184 struct cs_cpu_dbs_info_s *cs_dbs_info = NULL;
Viresh Kumar8eeed092013-01-31 17:28:01 +0000185 struct cs_ops *cs_ops = NULL;
186 struct od_ops *od_ops = NULL;
Viresh Kumar4471a342012-10-26 00:47:42 +0200187 struct od_dbs_tuners *od_tuners = dbs_data->tuners;
188 struct cs_dbs_tuners *cs_tuners = dbs_data->tuners;
189 struct cpu_dbs_common_info *cpu_cdbs;
190 unsigned int *sampling_rate, latency, ignore_nice, j, cpu = policy->cpu;
191 int rc;
192
193 cpu_cdbs = dbs_data->get_cpu_cdbs(cpu);
194
195 if (dbs_data->governor == GOV_CONSERVATIVE) {
196 cs_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
197 sampling_rate = &cs_tuners->sampling_rate;
198 ignore_nice = cs_tuners->ignore_nice;
Viresh Kumar8eeed092013-01-31 17:28:01 +0000199 cs_ops = dbs_data->gov_ops;
Viresh Kumar4471a342012-10-26 00:47:42 +0200200 } else {
201 od_dbs_info = dbs_data->get_cpu_dbs_info_s(cpu);
202 sampling_rate = &od_tuners->sampling_rate;
203 ignore_nice = od_tuners->ignore_nice;
Viresh Kumar8eeed092013-01-31 17:28:01 +0000204 od_ops = dbs_data->gov_ops;
Viresh Kumar4471a342012-10-26 00:47:42 +0200205 }
206
207 switch (event) {
208 case CPUFREQ_GOV_START:
209 if ((!cpu_online(cpu)) || (!policy->cur))
210 return -EINVAL;
211
212 mutex_lock(&dbs_data->mutex);
213
Viresh Kumar4471a342012-10-26 00:47:42 +0200214 for_each_cpu(j, policy->cpus) {
Viresh Kumar8eeed092013-01-31 17:28:01 +0000215 struct cpu_dbs_common_info *j_cdbs =
216 dbs_data->get_cpu_cdbs(j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200217
Fabio Baltieri09dca5a2013-01-31 10:39:19 +0000218 j_cdbs->cpu = j;
Viresh Kumar4471a342012-10-26 00:47:42 +0200219 j_cdbs->cur_policy = policy;
220 j_cdbs->prev_cpu_idle = get_cpu_idle_time(j,
221 &j_cdbs->prev_cpu_wall);
222 if (ignore_nice)
223 j_cdbs->prev_cpu_nice =
224 kcpustat_cpu(j).cpustat[CPUTIME_NICE];
Rickard Andersson2abfa872012-12-27 14:55:38 +0000225
226 mutex_init(&j_cdbs->timer_mutex);
227 INIT_DEFERRABLE_WORK(&j_cdbs->work,
228 dbs_data->gov_dbs_timer);
Viresh Kumar4471a342012-10-26 00:47:42 +0200229 }
230
Viresh Kumar4471a342012-10-26 00:47:42 +0200231 rc = sysfs_create_group(cpufreq_global_kobject,
232 dbs_data->attr_group);
233 if (rc) {
234 mutex_unlock(&dbs_data->mutex);
235 return rc;
236 }
237
238 /* policy latency is in nS. Convert it to uS first */
239 latency = policy->cpuinfo.transition_latency / 1000;
240 if (latency == 0)
241 latency = 1;
242
243 /*
244 * conservative does not implement micro like ondemand
245 * governor, thus we are bound to jiffes/HZ
246 */
247 if (dbs_data->governor == GOV_CONSERVATIVE) {
Viresh Kumar8eeed092013-01-31 17:28:01 +0000248 cs_dbs_info->down_skip = 0;
249 cs_dbs_info->enable = 1;
250 cs_dbs_info->requested_freq = policy->cur;
251 cpufreq_register_notifier(cs_ops->notifier_block,
Viresh Kumar4471a342012-10-26 00:47:42 +0200252 CPUFREQ_TRANSITION_NOTIFIER);
253
254 dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
255 jiffies_to_usecs(10);
256 } else {
Viresh Kumar8eeed092013-01-31 17:28:01 +0000257 od_dbs_info->rate_mult = 1;
258 od_dbs_info->sample_type = OD_NORMAL_SAMPLE;
259 od_ops->powersave_bias_init_cpu(cpu);
260 od_tuners->io_is_busy = od_ops->io_busy();
Viresh Kumar4471a342012-10-26 00:47:42 +0200261 }
262
263 /* Bring kernel and HW constraints together */
264 dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate,
265 MIN_LATENCY_MULTIPLIER * latency);
266 *sampling_rate = max(dbs_data->min_sampling_rate, latency *
267 LATENCY_MULTIPLIER);
Viresh Kumar4471a342012-10-26 00:47:42 +0200268 mutex_unlock(&dbs_data->mutex);
269
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000270 /* Initiate timer time stamp */
271 cpu_cdbs->time_stamp = ktime_get();
Fabio Baltierida53d612012-12-27 14:55:40 +0000272
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000273 for_each_cpu(j, policy->cpus)
274 dbs_timer_init(dbs_data, j, *sampling_rate);
Viresh Kumar4471a342012-10-26 00:47:42 +0200275 break;
276
277 case CPUFREQ_GOV_STOP:
278 if (dbs_data->governor == GOV_CONSERVATIVE)
279 cs_dbs_info->enable = 0;
280
Fabio Baltieri58ddcea2013-01-30 13:53:37 +0000281 for_each_cpu(j, policy->cpus)
282 dbs_timer_exit(dbs_data, j);
Viresh Kumar4471a342012-10-26 00:47:42 +0200283
284 mutex_lock(&dbs_data->mutex);
285 mutex_destroy(&cpu_cdbs->timer_mutex);
Viresh Kumar4471a342012-10-26 00:47:42 +0200286
Viresh Kumar8eeed092013-01-31 17:28:01 +0000287 sysfs_remove_group(cpufreq_global_kobject,
288 dbs_data->attr_group);
289 if (dbs_data->governor == GOV_CONSERVATIVE)
290 cpufreq_unregister_notifier(cs_ops->notifier_block,
291 CPUFREQ_TRANSITION_NOTIFIER);
Viresh Kumar4471a342012-10-26 00:47:42 +0200292 mutex_unlock(&dbs_data->mutex);
293
294 break;
295
296 case CPUFREQ_GOV_LIMITS:
297 mutex_lock(&cpu_cdbs->timer_mutex);
298 if (policy->max < cpu_cdbs->cur_policy->cur)
299 __cpufreq_driver_target(cpu_cdbs->cur_policy,
300 policy->max, CPUFREQ_RELATION_H);
301 else if (policy->min > cpu_cdbs->cur_policy->cur)
302 __cpufreq_driver_target(cpu_cdbs->cur_policy,
303 policy->min, CPUFREQ_RELATION_L);
304 dbs_check_cpu(dbs_data, cpu);
305 mutex_unlock(&cpu_cdbs->timer_mutex);
306 break;
307 }
308 return 0;
309}
310EXPORT_SYMBOL_GPL(cpufreq_governor_dbs);