Commit | Line | Data |
---|---|---|
2aacdfff | 1 | /* |
2 | * drivers/cpufreq/cpufreq_governor.c | |
3 | * | |
4 | * CPUFREQ governors common code | |
5 | * | |
4471a34f VK |
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 | * | |
2aacdfff | 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 | ||
4471a34f VK |
17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
18 | ||
2aacdfff | 19 | #include <linux/export.h> |
20 | #include <linux/kernel_stat.h> | |
4d5dcc42 | 21 | #include <linux/slab.h> |
4471a34f VK |
22 | |
23 | #include "cpufreq_governor.h" | |
24 | ||
2bb8d94f RW |
25 | DEFINE_MUTEX(dbs_data_mutex); |
26 | EXPORT_SYMBOL_GPL(dbs_data_mutex); | |
27 | ||
ea59ee0d | 28 | static struct attribute_group *get_sysfs_attr(struct dbs_governor *gov) |
4d5dcc42 | 29 | { |
ea59ee0d RW |
30 | return have_governor_per_policy() ? |
31 | gov->attr_group_gov_pol : gov->attr_group_gov_sys; | |
4d5dcc42 VK |
32 | } |
33 | ||
d10b5eb5 | 34 | void dbs_check_cpu(struct cpufreq_policy *policy) |
4471a34f | 35 | { |
d10b5eb5 | 36 | int cpu = policy->cpu; |
ea59ee0d RW |
37 | struct dbs_governor *gov = dbs_governor_of(policy); |
38 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); | |
39 | struct dbs_data *dbs_data = policy->governor_data; | |
4471a34f VK |
40 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
41 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; | |
18b46abd | 42 | unsigned int sampling_rate; |
4471a34f VK |
43 | unsigned int max_load = 0; |
44 | unsigned int ignore_nice; | |
45 | unsigned int j; | |
46 | ||
ea59ee0d | 47 | if (gov->governor == GOV_ONDEMAND) { |
18b46abd | 48 | struct od_cpu_dbs_info_s *od_dbs_info = |
ea59ee0d | 49 | gov->get_cpu_dbs_info_s(cpu); |
18b46abd SB |
50 | |
51 | /* | |
52 | * Sometimes, the ondemand governor uses an additional | |
53 | * multiplier to give long delays. So apply this multiplier to | |
54 | * the 'sampling_rate', so as to keep the wake-up-from-idle | |
55 | * detection logic a bit conservative. | |
56 | */ | |
57 | sampling_rate = od_tuners->sampling_rate; | |
58 | sampling_rate *= od_dbs_info->rate_mult; | |
59 | ||
6c4640c3 | 60 | ignore_nice = od_tuners->ignore_nice_load; |
18b46abd SB |
61 | } else { |
62 | sampling_rate = cs_tuners->sampling_rate; | |
6c4640c3 | 63 | ignore_nice = cs_tuners->ignore_nice_load; |
18b46abd | 64 | } |
4471a34f | 65 | |
dfa5bb62 | 66 | /* Get Absolute Load */ |
4471a34f | 67 | for_each_cpu(j, policy->cpus) { |
875b8508 | 68 | struct cpu_dbs_info *j_cdbs; |
9366d840 SK |
69 | u64 cur_wall_time, cur_idle_time; |
70 | unsigned int idle_time, wall_time; | |
4471a34f | 71 | unsigned int load; |
9366d840 | 72 | int io_busy = 0; |
4471a34f | 73 | |
ea59ee0d | 74 | j_cdbs = gov->get_cpu_cdbs(j); |
4471a34f | 75 | |
9366d840 SK |
76 | /* |
77 | * For the purpose of ondemand, waiting for disk IO is | |
78 | * an indication that you're performance critical, and | |
79 | * not that the system is actually idle. So do not add | |
80 | * the iowait time to the cpu idle time. | |
81 | */ | |
ea59ee0d | 82 | if (gov->governor == GOV_ONDEMAND) |
9366d840 SK |
83 | io_busy = od_tuners->io_is_busy; |
84 | cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy); | |
4471a34f VK |
85 | |
86 | wall_time = (unsigned int) | |
87 | (cur_wall_time - j_cdbs->prev_cpu_wall); | |
88 | j_cdbs->prev_cpu_wall = cur_wall_time; | |
89 | ||
0df35026 CY |
90 | if (cur_idle_time < j_cdbs->prev_cpu_idle) |
91 | cur_idle_time = j_cdbs->prev_cpu_idle; | |
92 | ||
4471a34f VK |
93 | idle_time = (unsigned int) |
94 | (cur_idle_time - j_cdbs->prev_cpu_idle); | |
95 | j_cdbs->prev_cpu_idle = cur_idle_time; | |
96 | ||
97 | if (ignore_nice) { | |
98 | u64 cur_nice; | |
99 | unsigned long cur_nice_jiffies; | |
100 | ||
101 | cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] - | |
102 | cdbs->prev_cpu_nice; | |
103 | /* | |
104 | * Assumption: nice time between sampling periods will | |
105 | * be less than 2^32 jiffies for 32 bit sys | |
106 | */ | |
107 | cur_nice_jiffies = (unsigned long) | |
108 | cputime64_to_jiffies64(cur_nice); | |
109 | ||
110 | cdbs->prev_cpu_nice = | |
111 | kcpustat_cpu(j).cpustat[CPUTIME_NICE]; | |
112 | idle_time += jiffies_to_usecs(cur_nice_jiffies); | |
113 | } | |
114 | ||
4471a34f VK |
115 | if (unlikely(!wall_time || wall_time < idle_time)) |
116 | continue; | |
117 | ||
18b46abd SB |
118 | /* |
119 | * If the CPU had gone completely idle, and a task just woke up | |
120 | * on this CPU now, it would be unfair to calculate 'load' the | |
121 | * usual way for this elapsed time-window, because it will show | |
122 | * near-zero load, irrespective of how CPU intensive that task | |
123 | * actually is. This is undesirable for latency-sensitive bursty | |
124 | * workloads. | |
125 | * | |
126 | * To avoid this, we reuse the 'load' from the previous | |
127 | * time-window and give this task a chance to start with a | |
128 | * reasonably high CPU frequency. (However, we shouldn't over-do | |
129 | * this copy, lest we get stuck at a high load (high frequency) | |
130 | * for too long, even when the current system load has actually | |
131 | * dropped down. So we perform the copy only once, upon the | |
132 | * first wake-up from idle.) | |
133 | * | |
9be4fd2c RW |
134 | * Detecting this situation is easy: the governor's utilization |
135 | * update handler would not have run during CPU-idle periods. | |
136 | * Hence, an unusually large 'wall_time' (as compared to the | |
137 | * sampling rate) indicates this scenario. | |
c8ae481b VK |
138 | * |
139 | * prev_load can be zero in two cases and we must recalculate it | |
140 | * for both cases: | |
141 | * - during long idle intervals | |
142 | * - explicitly set to zero | |
18b46abd | 143 | */ |
c8ae481b VK |
144 | if (unlikely(wall_time > (2 * sampling_rate) && |
145 | j_cdbs->prev_load)) { | |
18b46abd | 146 | load = j_cdbs->prev_load; |
c8ae481b VK |
147 | |
148 | /* | |
149 | * Perform a destructive copy, to ensure that we copy | |
150 | * the previous load only once, upon the first wake-up | |
151 | * from idle. | |
152 | */ | |
153 | j_cdbs->prev_load = 0; | |
18b46abd SB |
154 | } else { |
155 | load = 100 * (wall_time - idle_time) / wall_time; | |
156 | j_cdbs->prev_load = load; | |
18b46abd | 157 | } |
4471a34f | 158 | |
4471a34f VK |
159 | if (load > max_load) |
160 | max_load = load; | |
161 | } | |
162 | ||
ea59ee0d | 163 | gov->gov_check_cpu(cpu, max_load); |
4471a34f VK |
164 | } |
165 | EXPORT_SYMBOL_GPL(dbs_check_cpu); | |
166 | ||
e40e7b25 | 167 | void gov_set_update_util(struct policy_dbs_info *policy_dbs, |
9be4fd2c | 168 | unsigned int delay_us) |
4471a34f | 169 | { |
e40e7b25 | 170 | struct cpufreq_policy *policy = policy_dbs->policy; |
ea59ee0d | 171 | struct dbs_governor *gov = dbs_governor_of(policy); |
70f43e5e | 172 | int cpu; |
031299b3 | 173 | |
e40e7b25 RW |
174 | gov_update_sample_delay(policy_dbs, delay_us); |
175 | policy_dbs->last_sample_time = 0; | |
9be4fd2c | 176 | |
70f43e5e | 177 | for_each_cpu(cpu, policy->cpus) { |
ea59ee0d | 178 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
9be4fd2c RW |
179 | |
180 | cpufreq_set_update_util_data(cpu, &cdbs->update_util); | |
031299b3 VK |
181 | } |
182 | } | |
9be4fd2c | 183 | EXPORT_SYMBOL_GPL(gov_set_update_util); |
031299b3 | 184 | |
9be4fd2c | 185 | static inline void gov_clear_update_util(struct cpufreq_policy *policy) |
031299b3 | 186 | { |
031299b3 | 187 | int i; |
58ddcead | 188 | |
9be4fd2c RW |
189 | for_each_cpu(i, policy->cpus) |
190 | cpufreq_set_update_util_data(i, NULL); | |
191 | ||
192 | synchronize_rcu(); | |
4471a34f VK |
193 | } |
194 | ||
e40e7b25 | 195 | static void gov_cancel_work(struct policy_dbs_info *policy_dbs) |
70f43e5e | 196 | { |
9be4fd2c | 197 | /* Tell dbs_update_util_handler() to skip queuing up work items. */ |
e40e7b25 | 198 | atomic_inc(&policy_dbs->skip_work); |
70f43e5e | 199 | /* |
9be4fd2c RW |
200 | * If dbs_update_util_handler() is already running, it may not notice |
201 | * the incremented skip_work, so wait for it to complete to prevent its | |
202 | * work item from being queued up after the cancel_work_sync() below. | |
70f43e5e | 203 | */ |
e40e7b25 RW |
204 | gov_clear_update_util(policy_dbs->policy); |
205 | irq_work_sync(&policy_dbs->irq_work); | |
206 | cancel_work_sync(&policy_dbs->work); | |
207 | atomic_set(&policy_dbs->skip_work, 0); | |
70f43e5e | 208 | } |
43e0ee36 | 209 | |
70f43e5e | 210 | static void dbs_work_handler(struct work_struct *work) |
43e0ee36 | 211 | { |
e40e7b25 | 212 | struct policy_dbs_info *policy_dbs; |
3a91b069 | 213 | struct cpufreq_policy *policy; |
ea59ee0d | 214 | struct dbs_governor *gov; |
9be4fd2c | 215 | unsigned int delay; |
43e0ee36 | 216 | |
e40e7b25 RW |
217 | policy_dbs = container_of(work, struct policy_dbs_info, work); |
218 | policy = policy_dbs->policy; | |
ea59ee0d | 219 | gov = dbs_governor_of(policy); |
3a91b069 | 220 | |
70f43e5e | 221 | /* |
9be4fd2c RW |
222 | * Make sure cpufreq_governor_limits() isn't evaluating load or the |
223 | * ondemand governor isn't updating the sampling rate in parallel. | |
70f43e5e | 224 | */ |
e40e7b25 | 225 | mutex_lock(&policy_dbs->timer_mutex); |
ea59ee0d | 226 | delay = gov->gov_dbs_timer(policy); |
e40e7b25 RW |
227 | policy_dbs->sample_delay_ns = jiffies_to_nsecs(delay); |
228 | mutex_unlock(&policy_dbs->timer_mutex); | |
70f43e5e | 229 | |
9be4fd2c RW |
230 | /* |
231 | * If the atomic operation below is reordered with respect to the | |
232 | * sample delay modification, the utilization update handler may end | |
233 | * up using a stale sample delay value. | |
234 | */ | |
235 | smp_mb__before_atomic(); | |
e40e7b25 | 236 | atomic_dec(&policy_dbs->skip_work); |
9be4fd2c RW |
237 | } |
238 | ||
239 | static void dbs_irq_work(struct irq_work *irq_work) | |
240 | { | |
e40e7b25 | 241 | struct policy_dbs_info *policy_dbs; |
70f43e5e | 242 | |
e40e7b25 RW |
243 | policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work); |
244 | schedule_work(&policy_dbs->work); | |
70f43e5e VK |
245 | } |
246 | ||
e40e7b25 | 247 | static inline void gov_queue_irq_work(struct policy_dbs_info *policy_dbs) |
70f43e5e | 248 | { |
9be4fd2c | 249 | #ifdef CONFIG_SMP |
e40e7b25 | 250 | irq_work_queue_on(&policy_dbs->irq_work, smp_processor_id()); |
9be4fd2c | 251 | #else |
e40e7b25 | 252 | irq_work_queue(&policy_dbs->irq_work); |
9be4fd2c RW |
253 | #endif |
254 | } | |
255 | ||
256 | static void dbs_update_util_handler(struct update_util_data *data, u64 time, | |
257 | unsigned long util, unsigned long max) | |
258 | { | |
259 | struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util); | |
e40e7b25 | 260 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
70f43e5e VK |
261 | |
262 | /* | |
9be4fd2c RW |
263 | * The work may not be allowed to be queued up right now. |
264 | * Possible reasons: | |
265 | * - Work has already been queued up or is in progress. | |
266 | * - The governor is being stopped. | |
267 | * - It is too early (too little time from the previous sample). | |
70f43e5e | 268 | */ |
e40e7b25 | 269 | if (atomic_inc_return(&policy_dbs->skip_work) == 1) { |
9be4fd2c RW |
270 | u64 delta_ns; |
271 | ||
e40e7b25 RW |
272 | delta_ns = time - policy_dbs->last_sample_time; |
273 | if ((s64)delta_ns >= policy_dbs->sample_delay_ns) { | |
274 | policy_dbs->last_sample_time = time; | |
275 | gov_queue_irq_work(policy_dbs); | |
9be4fd2c RW |
276 | return; |
277 | } | |
278 | } | |
e40e7b25 | 279 | atomic_dec(&policy_dbs->skip_work); |
43e0ee36 | 280 | } |
4447266b | 281 | |
4d5dcc42 | 282 | static void set_sampling_rate(struct dbs_data *dbs_data, |
ea59ee0d RW |
283 | struct dbs_governor *gov, |
284 | unsigned int sampling_rate) | |
4d5dcc42 | 285 | { |
ea59ee0d | 286 | if (gov->governor == GOV_CONSERVATIVE) { |
4d5dcc42 VK |
287 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
288 | cs_tuners->sampling_rate = sampling_rate; | |
289 | } else { | |
290 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; | |
291 | od_tuners->sampling_rate = sampling_rate; | |
292 | } | |
293 | } | |
294 | ||
e40e7b25 | 295 | static int alloc_policy_dbs_info(struct cpufreq_policy *policy, |
7bdad34d | 296 | struct dbs_governor *gov) |
44152cb8 | 297 | { |
e40e7b25 | 298 | struct policy_dbs_info *policy_dbs; |
44152cb8 VK |
299 | int j; |
300 | ||
301 | /* Allocate memory for the common information for policy->cpus */ | |
e40e7b25 RW |
302 | policy_dbs = kzalloc(sizeof(*policy_dbs), GFP_KERNEL); |
303 | if (!policy_dbs) | |
44152cb8 VK |
304 | return -ENOMEM; |
305 | ||
e40e7b25 | 306 | /* Set policy_dbs for all CPUs, online+offline */ |
44152cb8 | 307 | for_each_cpu(j, policy->related_cpus) |
e40e7b25 | 308 | gov->get_cpu_cdbs(j)->policy_dbs = policy_dbs; |
44152cb8 | 309 | |
e40e7b25 RW |
310 | mutex_init(&policy_dbs->timer_mutex); |
311 | atomic_set(&policy_dbs->skip_work, 0); | |
312 | init_irq_work(&policy_dbs->irq_work, dbs_irq_work); | |
313 | INIT_WORK(&policy_dbs->work, dbs_work_handler); | |
44152cb8 VK |
314 | return 0; |
315 | } | |
316 | ||
e40e7b25 | 317 | static void free_policy_dbs_info(struct cpufreq_policy *policy, |
7bdad34d | 318 | struct dbs_governor *gov) |
44152cb8 | 319 | { |
7bdad34d | 320 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); |
e40e7b25 | 321 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
44152cb8 VK |
322 | int j; |
323 | ||
e40e7b25 | 324 | mutex_destroy(&policy_dbs->timer_mutex); |
5e4500d8 | 325 | |
44152cb8 | 326 | for_each_cpu(j, policy->cpus) |
e40e7b25 | 327 | gov->get_cpu_cdbs(j)->policy_dbs = NULL; |
44152cb8 | 328 | |
e40e7b25 | 329 | kfree(policy_dbs); |
44152cb8 VK |
330 | } |
331 | ||
906a6e5a | 332 | static int cpufreq_governor_init(struct cpufreq_policy *policy) |
4471a34f | 333 | { |
ea59ee0d | 334 | struct dbs_governor *gov = dbs_governor_of(policy); |
7bdad34d | 335 | struct dbs_data *dbs_data = gov->gdbs_data; |
714a2d9c VK |
336 | unsigned int latency; |
337 | int ret; | |
4471a34f | 338 | |
a72c4959 VK |
339 | /* State should be equivalent to EXIT */ |
340 | if (policy->governor_data) | |
341 | return -EBUSY; | |
342 | ||
714a2d9c VK |
343 | if (dbs_data) { |
344 | if (WARN_ON(have_governor_per_policy())) | |
345 | return -EINVAL; | |
44152cb8 | 346 | |
e40e7b25 | 347 | ret = alloc_policy_dbs_info(policy, gov); |
44152cb8 VK |
348 | if (ret) |
349 | return ret; | |
350 | ||
714a2d9c VK |
351 | dbs_data->usage_count++; |
352 | policy->governor_data = dbs_data; | |
353 | return 0; | |
354 | } | |
4d5dcc42 | 355 | |
714a2d9c VK |
356 | dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL); |
357 | if (!dbs_data) | |
358 | return -ENOMEM; | |
4d5dcc42 | 359 | |
e40e7b25 | 360 | ret = alloc_policy_dbs_info(policy, gov); |
44152cb8 VK |
361 | if (ret) |
362 | goto free_dbs_data; | |
363 | ||
714a2d9c | 364 | dbs_data->usage_count = 1; |
4d5dcc42 | 365 | |
7bdad34d | 366 | ret = gov->init(dbs_data, !policy->governor->initialized); |
714a2d9c | 367 | if (ret) |
e40e7b25 | 368 | goto free_policy_dbs_info; |
4d5dcc42 | 369 | |
714a2d9c VK |
370 | /* policy latency is in ns. Convert it to us first */ |
371 | latency = policy->cpuinfo.transition_latency / 1000; | |
372 | if (latency == 0) | |
373 | latency = 1; | |
4d5dcc42 | 374 | |
714a2d9c VK |
375 | /* Bring kernel and HW constraints together */ |
376 | dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate, | |
377 | MIN_LATENCY_MULTIPLIER * latency); | |
ea59ee0d | 378 | set_sampling_rate(dbs_data, gov, max(dbs_data->min_sampling_rate, |
714a2d9c | 379 | latency * LATENCY_MULTIPLIER)); |
2361be23 | 380 | |
8eec1020 | 381 | if (!have_governor_per_policy()) |
7bdad34d | 382 | gov->gdbs_data = dbs_data; |
4d5dcc42 | 383 | |
e4b133cc VK |
384 | policy->governor_data = dbs_data; |
385 | ||
714a2d9c | 386 | ret = sysfs_create_group(get_governor_parent_kobj(policy), |
ea59ee0d | 387 | get_sysfs_attr(gov)); |
714a2d9c | 388 | if (ret) |
8eec1020 | 389 | goto reset_gdbs_data; |
4d5dcc42 | 390 | |
714a2d9c | 391 | return 0; |
4d5dcc42 | 392 | |
8eec1020 | 393 | reset_gdbs_data: |
e4b133cc VK |
394 | policy->governor_data = NULL; |
395 | ||
8eec1020 | 396 | if (!have_governor_per_policy()) |
7bdad34d RW |
397 | gov->gdbs_data = NULL; |
398 | gov->exit(dbs_data, !policy->governor->initialized); | |
e40e7b25 RW |
399 | free_policy_dbs_info: |
400 | free_policy_dbs_info(policy, gov); | |
714a2d9c VK |
401 | free_dbs_data: |
402 | kfree(dbs_data); | |
403 | return ret; | |
404 | } | |
4d5dcc42 | 405 | |
5da3dd1e | 406 | static int cpufreq_governor_exit(struct cpufreq_policy *policy) |
714a2d9c | 407 | { |
ea59ee0d | 408 | struct dbs_governor *gov = dbs_governor_of(policy); |
5da3dd1e | 409 | struct dbs_data *dbs_data = policy->governor_data; |
7bdad34d | 410 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); |
a72c4959 VK |
411 | |
412 | /* State should be equivalent to INIT */ | |
e40e7b25 | 413 | if (!cdbs->policy_dbs || cdbs->policy_dbs->policy) |
a72c4959 | 414 | return -EBUSY; |
4d5dcc42 | 415 | |
714a2d9c VK |
416 | if (!--dbs_data->usage_count) { |
417 | sysfs_remove_group(get_governor_parent_kobj(policy), | |
ea59ee0d | 418 | get_sysfs_attr(gov)); |
2361be23 | 419 | |
e4b133cc VK |
420 | policy->governor_data = NULL; |
421 | ||
8eec1020 | 422 | if (!have_governor_per_policy()) |
7bdad34d | 423 | gov->gdbs_data = NULL; |
4471a34f | 424 | |
7bdad34d | 425 | gov->exit(dbs_data, policy->governor->initialized == 1); |
714a2d9c | 426 | kfree(dbs_data); |
e4b133cc VK |
427 | } else { |
428 | policy->governor_data = NULL; | |
4d5dcc42 | 429 | } |
44152cb8 | 430 | |
e40e7b25 | 431 | free_policy_dbs_info(policy, gov); |
a72c4959 | 432 | return 0; |
714a2d9c | 433 | } |
4d5dcc42 | 434 | |
5da3dd1e | 435 | static int cpufreq_governor_start(struct cpufreq_policy *policy) |
714a2d9c | 436 | { |
ea59ee0d | 437 | struct dbs_governor *gov = dbs_governor_of(policy); |
5da3dd1e | 438 | struct dbs_data *dbs_data = policy->governor_data; |
714a2d9c | 439 | unsigned int sampling_rate, ignore_nice, j, cpu = policy->cpu; |
7bdad34d | 440 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(cpu); |
e40e7b25 | 441 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
714a2d9c VK |
442 | int io_busy = 0; |
443 | ||
444 | if (!policy->cur) | |
445 | return -EINVAL; | |
446 | ||
a72c4959 | 447 | /* State should be equivalent to INIT */ |
e40e7b25 | 448 | if (!policy_dbs || policy_dbs->policy) |
a72c4959 VK |
449 | return -EBUSY; |
450 | ||
7bdad34d | 451 | if (gov->governor == GOV_CONSERVATIVE) { |
714a2d9c | 452 | struct cs_dbs_tuners *cs_tuners = dbs_data->tuners; |
4d5dcc42 | 453 | |
4d5dcc42 | 454 | sampling_rate = cs_tuners->sampling_rate; |
6c4640c3 | 455 | ignore_nice = cs_tuners->ignore_nice_load; |
4471a34f | 456 | } else { |
714a2d9c VK |
457 | struct od_dbs_tuners *od_tuners = dbs_data->tuners; |
458 | ||
4d5dcc42 | 459 | sampling_rate = od_tuners->sampling_rate; |
6c4640c3 | 460 | ignore_nice = od_tuners->ignore_nice_load; |
9366d840 | 461 | io_busy = od_tuners->io_is_busy; |
4471a34f VK |
462 | } |
463 | ||
714a2d9c | 464 | for_each_cpu(j, policy->cpus) { |
7bdad34d | 465 | struct cpu_dbs_info *j_cdbs = gov->get_cpu_cdbs(j); |
714a2d9c | 466 | unsigned int prev_load; |
4471a34f | 467 | |
714a2d9c VK |
468 | j_cdbs->prev_cpu_idle = |
469 | get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy); | |
4471a34f | 470 | |
714a2d9c VK |
471 | prev_load = (unsigned int)(j_cdbs->prev_cpu_wall - |
472 | j_cdbs->prev_cpu_idle); | |
473 | j_cdbs->prev_load = 100 * prev_load / | |
474 | (unsigned int)j_cdbs->prev_cpu_wall; | |
18b46abd | 475 | |
714a2d9c VK |
476 | if (ignore_nice) |
477 | j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE]; | |
18b46abd | 478 | |
9be4fd2c | 479 | j_cdbs->update_util.func = dbs_update_util_handler; |
714a2d9c | 480 | } |
e40e7b25 | 481 | policy_dbs->policy = policy; |
2abfa876 | 482 | |
7bdad34d | 483 | if (gov->governor == GOV_CONSERVATIVE) { |
714a2d9c | 484 | struct cs_cpu_dbs_info_s *cs_dbs_info = |
7bdad34d | 485 | gov->get_cpu_dbs_info_s(cpu); |
4471a34f | 486 | |
714a2d9c | 487 | cs_dbs_info->down_skip = 0; |
714a2d9c VK |
488 | cs_dbs_info->requested_freq = policy->cur; |
489 | } else { | |
7bdad34d RW |
490 | struct od_ops *od_ops = gov->gov_ops; |
491 | struct od_cpu_dbs_info_s *od_dbs_info = gov->get_cpu_dbs_info_s(cpu); | |
4471a34f | 492 | |
714a2d9c VK |
493 | od_dbs_info->rate_mult = 1; |
494 | od_dbs_info->sample_type = OD_NORMAL_SAMPLE; | |
495 | od_ops->powersave_bias_init_cpu(cpu); | |
496 | } | |
4471a34f | 497 | |
e40e7b25 | 498 | gov_set_update_util(policy_dbs, sampling_rate); |
714a2d9c VK |
499 | return 0; |
500 | } | |
501 | ||
5da3dd1e | 502 | static int cpufreq_governor_stop(struct cpufreq_policy *policy) |
714a2d9c | 503 | { |
ea59ee0d RW |
504 | struct dbs_governor *gov = dbs_governor_of(policy); |
505 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); | |
e40e7b25 | 506 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
44152cb8 | 507 | |
a72c4959 | 508 | /* State should be equivalent to START */ |
e40e7b25 | 509 | if (!policy_dbs || !policy_dbs->policy) |
a72c4959 VK |
510 | return -EBUSY; |
511 | ||
e40e7b25 RW |
512 | gov_cancel_work(policy_dbs); |
513 | policy_dbs->policy = NULL; | |
3a91b069 | 514 | |
a72c4959 | 515 | return 0; |
714a2d9c | 516 | } |
4471a34f | 517 | |
5da3dd1e | 518 | static int cpufreq_governor_limits(struct cpufreq_policy *policy) |
714a2d9c | 519 | { |
ea59ee0d | 520 | struct dbs_governor *gov = dbs_governor_of(policy); |
d10b5eb5 | 521 | struct cpu_dbs_info *cdbs = gov->get_cpu_cdbs(policy->cpu); |
e9751894 | 522 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
8eeed095 | 523 | |
a72c4959 | 524 | /* State should be equivalent to START */ |
e9751894 | 525 | if (!policy_dbs || !policy_dbs->policy) |
a72c4959 | 526 | return -EBUSY; |
4471a34f | 527 | |
e9751894 RW |
528 | mutex_lock(&policy_dbs->timer_mutex); |
529 | if (policy->max < policy->cur) | |
530 | __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H); | |
531 | else if (policy->min > policy->cur) | |
532 | __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L); | |
d10b5eb5 | 533 | dbs_check_cpu(policy); |
e9751894 | 534 | mutex_unlock(&policy_dbs->timer_mutex); |
a72c4959 VK |
535 | |
536 | return 0; | |
714a2d9c | 537 | } |
4471a34f | 538 | |
906a6e5a | 539 | int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event) |
714a2d9c | 540 | { |
5da3dd1e | 541 | int ret = -EINVAL; |
714a2d9c | 542 | |
732b6d61 | 543 | /* Lock governor to block concurrent initialization of governor */ |
2bb8d94f | 544 | mutex_lock(&dbs_data_mutex); |
732b6d61 | 545 | |
5da3dd1e | 546 | if (event == CPUFREQ_GOV_POLICY_INIT) { |
906a6e5a | 547 | ret = cpufreq_governor_init(policy); |
5da3dd1e RW |
548 | } else if (policy->governor_data) { |
549 | switch (event) { | |
550 | case CPUFREQ_GOV_POLICY_EXIT: | |
551 | ret = cpufreq_governor_exit(policy); | |
552 | break; | |
553 | case CPUFREQ_GOV_START: | |
554 | ret = cpufreq_governor_start(policy); | |
555 | break; | |
556 | case CPUFREQ_GOV_STOP: | |
557 | ret = cpufreq_governor_stop(policy); | |
558 | break; | |
559 | case CPUFREQ_GOV_LIMITS: | |
560 | ret = cpufreq_governor_limits(policy); | |
561 | break; | |
562 | } | |
4471a34f | 563 | } |
714a2d9c | 564 | |
2bb8d94f | 565 | mutex_unlock(&dbs_data_mutex); |
714a2d9c | 566 | return ret; |
4471a34f VK |
567 | } |
568 | EXPORT_SYMBOL_GPL(cpufreq_governor_dbs); |