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 | ||
8c8f77fd RW |
25 | static DEFINE_PER_CPU(struct cpu_dbs_info, cpu_dbs); |
26 | ||
1112e9d8 | 27 | static DEFINE_MUTEX(gov_dbs_data_mutex); |
2bb8d94f | 28 | |
aded387b VK |
29 | /* Common sysfs tunables */ |
30 | /** | |
31 | * store_sampling_rate - update sampling rate effective immediately if needed. | |
32 | * | |
33 | * If new rate is smaller than the old, simply updating | |
34 | * dbs.sampling_rate might not be appropriate. For example, if the | |
35 | * original sampling_rate was 1 second and the requested new sampling rate is 10 | |
36 | * ms because the user needs immediate reaction from ondemand governor, but not | |
37 | * sure if higher frequency will be required or not, then, the governor may | |
38 | * change the sampling rate too late; up to 1 second later. Thus, if we are | |
39 | * reducing the sampling rate, we need to make the new value effective | |
40 | * immediately. | |
41 | * | |
aded387b VK |
42 | * This must be called with dbs_data->mutex held, otherwise traversing |
43 | * policy_dbs_list isn't safe. | |
44 | */ | |
45 | ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf, | |
46 | size_t count) | |
47 | { | |
48 | struct policy_dbs_info *policy_dbs; | |
49 | unsigned int rate; | |
50 | int ret; | |
51 | ret = sscanf(buf, "%u", &rate); | |
52 | if (ret != 1) | |
53 | return -EINVAL; | |
54 | ||
55 | dbs_data->sampling_rate = max(rate, dbs_data->min_sampling_rate); | |
56 | ||
57 | /* | |
58 | * We are operating under dbs_data->mutex and so the list and its | |
59 | * entries can't be freed concurrently. | |
60 | */ | |
61 | list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) { | |
62 | mutex_lock(&policy_dbs->timer_mutex); | |
63 | /* | |
64 | * On 32-bit architectures this may race with the | |
65 | * sample_delay_ns read in dbs_update_util_handler(), but that | |
66 | * really doesn't matter. If the read returns a value that's | |
67 | * too big, the sample will be skipped, but the next invocation | |
68 | * of dbs_update_util_handler() (when the update has been | |
78347cdb | 69 | * completed) will take a sample. |
aded387b VK |
70 | * |
71 | * If this runs in parallel with dbs_work_handler(), we may end | |
72 | * up overwriting the sample_delay_ns value that it has just | |
78347cdb RW |
73 | * written, but it will be corrected next time a sample is |
74 | * taken, so it shouldn't be significant. | |
aded387b | 75 | */ |
78347cdb | 76 | gov_update_sample_delay(policy_dbs, 0); |
aded387b VK |
77 | mutex_unlock(&policy_dbs->timer_mutex); |
78 | } | |
79 | ||
80 | return count; | |
81 | } | |
82 | EXPORT_SYMBOL_GPL(store_sampling_rate); | |
83 | ||
a33cce1c RW |
84 | /** |
85 | * gov_update_cpu_data - Update CPU load data. | |
a33cce1c RW |
86 | * @dbs_data: Top-level governor data pointer. |
87 | * | |
88 | * Update CPU load data for all CPUs in the domain governed by @dbs_data | |
89 | * (that may be a single policy or a bunch of them if governor tunables are | |
90 | * system-wide). | |
91 | * | |
92 | * Call under the @dbs_data mutex. | |
93 | */ | |
8c8f77fd | 94 | void gov_update_cpu_data(struct dbs_data *dbs_data) |
a33cce1c RW |
95 | { |
96 | struct policy_dbs_info *policy_dbs; | |
97 | ||
98 | list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) { | |
99 | unsigned int j; | |
100 | ||
101 | for_each_cpu(j, policy_dbs->policy->cpus) { | |
8c8f77fd | 102 | struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); |
a33cce1c RW |
103 | |
104 | j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, | |
105 | dbs_data->io_is_busy); | |
106 | if (dbs_data->ignore_nice_load) | |
107 | j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE]; | |
108 | } | |
109 | } | |
110 | } | |
111 | EXPORT_SYMBOL_GPL(gov_update_cpu_data); | |
112 | ||
c4435630 | 113 | static inline struct dbs_data *to_dbs_data(struct kobject *kobj) |
4d5dcc42 | 114 | { |
c4435630 | 115 | return container_of(kobj, struct dbs_data, kobj); |
4d5dcc42 VK |
116 | } |
117 | ||
c4435630 VK |
118 | static inline struct governor_attr *to_gov_attr(struct attribute *attr) |
119 | { | |
120 | return container_of(attr, struct governor_attr, attr); | |
121 | } | |
122 | ||
123 | static ssize_t governor_show(struct kobject *kobj, struct attribute *attr, | |
124 | char *buf) | |
125 | { | |
126 | struct dbs_data *dbs_data = to_dbs_data(kobj); | |
127 | struct governor_attr *gattr = to_gov_attr(attr); | |
128 | int ret = -EIO; | |
129 | ||
130 | if (gattr->show) | |
131 | ret = gattr->show(dbs_data, buf); | |
132 | ||
133 | return ret; | |
134 | } | |
135 | ||
136 | static ssize_t governor_store(struct kobject *kobj, struct attribute *attr, | |
137 | const char *buf, size_t count) | |
138 | { | |
139 | struct dbs_data *dbs_data = to_dbs_data(kobj); | |
140 | struct governor_attr *gattr = to_gov_attr(attr); | |
141 | int ret = -EIO; | |
142 | ||
143 | mutex_lock(&dbs_data->mutex); | |
144 | ||
574ef14d | 145 | if (dbs_data->usage_count && gattr->store) |
c4435630 VK |
146 | ret = gattr->store(dbs_data, buf, count); |
147 | ||
148 | mutex_unlock(&dbs_data->mutex); | |
149 | ||
150 | return ret; | |
151 | } | |
152 | ||
153 | /* | |
154 | * Sysfs Ops for accessing governor attributes. | |
155 | * | |
156 | * All show/store invocations for governor specific sysfs attributes, will first | |
157 | * call the below show/store callbacks and the attribute specific callback will | |
158 | * be called from within it. | |
159 | */ | |
160 | static const struct sysfs_ops governor_sysfs_ops = { | |
161 | .show = governor_show, | |
162 | .store = governor_store, | |
163 | }; | |
164 | ||
4cccf755 | 165 | unsigned int dbs_update(struct cpufreq_policy *policy) |
4471a34f | 166 | { |
bc505475 RW |
167 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
168 | struct dbs_data *dbs_data = policy_dbs->dbs_data; | |
ff4b1789 | 169 | unsigned int ignore_nice = dbs_data->ignore_nice_load; |
4471a34f | 170 | unsigned int max_load = 0; |
8847e038 | 171 | unsigned int sampling_rate, io_busy, j; |
4471a34f | 172 | |
57dc3bcd RW |
173 | /* |
174 | * Sometimes governors may use an additional multiplier to increase | |
175 | * sample delays temporarily. Apply that multiplier to sampling_rate | |
176 | * so as to keep the wake-up-from-idle detection logic a bit | |
177 | * conservative. | |
178 | */ | |
179 | sampling_rate = dbs_data->sampling_rate * policy_dbs->rate_mult; | |
8847e038 RW |
180 | /* |
181 | * For the purpose of ondemand, waiting for disk IO is an indication | |
182 | * that you're performance critical, and not that the system is actually | |
183 | * idle, so do not add the iowait time to the CPU idle time then. | |
184 | */ | |
185 | io_busy = dbs_data->io_is_busy; | |
4471a34f | 186 | |
dfa5bb62 | 187 | /* Get Absolute Load */ |
4471a34f | 188 | for_each_cpu(j, policy->cpus) { |
8c8f77fd | 189 | struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); |
9366d840 SK |
190 | u64 cur_wall_time, cur_idle_time; |
191 | unsigned int idle_time, wall_time; | |
4471a34f VK |
192 | unsigned int load; |
193 | ||
9366d840 | 194 | cur_idle_time = get_cpu_idle_time(j, &cur_wall_time, io_busy); |
4471a34f | 195 | |
57eb832f | 196 | wall_time = cur_wall_time - j_cdbs->prev_cpu_wall; |
4471a34f VK |
197 | j_cdbs->prev_cpu_wall = cur_wall_time; |
198 | ||
57eb832f RW |
199 | if (cur_idle_time <= j_cdbs->prev_cpu_idle) { |
200 | idle_time = 0; | |
201 | } else { | |
202 | idle_time = cur_idle_time - j_cdbs->prev_cpu_idle; | |
203 | j_cdbs->prev_cpu_idle = cur_idle_time; | |
204 | } | |
4471a34f VK |
205 | |
206 | if (ignore_nice) { | |
679b8fe4 RW |
207 | u64 cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE]; |
208 | ||
209 | idle_time += cputime_to_usecs(cur_nice - j_cdbs->prev_cpu_nice); | |
210 | j_cdbs->prev_cpu_nice = cur_nice; | |
4471a34f VK |
211 | } |
212 | ||
4471a34f VK |
213 | if (unlikely(!wall_time || wall_time < idle_time)) |
214 | continue; | |
215 | ||
18b46abd SB |
216 | /* |
217 | * If the CPU had gone completely idle, and a task just woke up | |
218 | * on this CPU now, it would be unfair to calculate 'load' the | |
219 | * usual way for this elapsed time-window, because it will show | |
220 | * near-zero load, irrespective of how CPU intensive that task | |
221 | * actually is. This is undesirable for latency-sensitive bursty | |
222 | * workloads. | |
223 | * | |
224 | * To avoid this, we reuse the 'load' from the previous | |
225 | * time-window and give this task a chance to start with a | |
226 | * reasonably high CPU frequency. (However, we shouldn't over-do | |
227 | * this copy, lest we get stuck at a high load (high frequency) | |
228 | * for too long, even when the current system load has actually | |
229 | * dropped down. So we perform the copy only once, upon the | |
230 | * first wake-up from idle.) | |
231 | * | |
9be4fd2c RW |
232 | * Detecting this situation is easy: the governor's utilization |
233 | * update handler would not have run during CPU-idle periods. | |
234 | * Hence, an unusually large 'wall_time' (as compared to the | |
235 | * sampling rate) indicates this scenario. | |
c8ae481b VK |
236 | * |
237 | * prev_load can be zero in two cases and we must recalculate it | |
238 | * for both cases: | |
239 | * - during long idle intervals | |
240 | * - explicitly set to zero | |
18b46abd | 241 | */ |
c8ae481b VK |
242 | if (unlikely(wall_time > (2 * sampling_rate) && |
243 | j_cdbs->prev_load)) { | |
18b46abd | 244 | load = j_cdbs->prev_load; |
c8ae481b VK |
245 | |
246 | /* | |
247 | * Perform a destructive copy, to ensure that we copy | |
248 | * the previous load only once, upon the first wake-up | |
249 | * from idle. | |
250 | */ | |
251 | j_cdbs->prev_load = 0; | |
18b46abd SB |
252 | } else { |
253 | load = 100 * (wall_time - idle_time) / wall_time; | |
254 | j_cdbs->prev_load = load; | |
18b46abd | 255 | } |
4471a34f | 256 | |
4471a34f VK |
257 | if (load > max_load) |
258 | max_load = load; | |
259 | } | |
4cccf755 | 260 | return max_load; |
4471a34f | 261 | } |
4cccf755 | 262 | EXPORT_SYMBOL_GPL(dbs_update); |
4471a34f | 263 | |
e40e7b25 | 264 | void gov_set_update_util(struct policy_dbs_info *policy_dbs, |
9be4fd2c | 265 | unsigned int delay_us) |
4471a34f | 266 | { |
e40e7b25 | 267 | struct cpufreq_policy *policy = policy_dbs->policy; |
70f43e5e | 268 | int cpu; |
031299b3 | 269 | |
e40e7b25 RW |
270 | gov_update_sample_delay(policy_dbs, delay_us); |
271 | policy_dbs->last_sample_time = 0; | |
9be4fd2c | 272 | |
70f43e5e | 273 | for_each_cpu(cpu, policy->cpus) { |
8c8f77fd | 274 | struct cpu_dbs_info *cdbs = &per_cpu(cpu_dbs, cpu); |
9be4fd2c RW |
275 | |
276 | cpufreq_set_update_util_data(cpu, &cdbs->update_util); | |
031299b3 VK |
277 | } |
278 | } | |
9be4fd2c | 279 | EXPORT_SYMBOL_GPL(gov_set_update_util); |
031299b3 | 280 | |
9be4fd2c | 281 | static inline void gov_clear_update_util(struct cpufreq_policy *policy) |
031299b3 | 282 | { |
031299b3 | 283 | int i; |
58ddcead | 284 | |
9be4fd2c RW |
285 | for_each_cpu(i, policy->cpus) |
286 | cpufreq_set_update_util_data(i, NULL); | |
287 | ||
288 | synchronize_rcu(); | |
4471a34f VK |
289 | } |
290 | ||
581c214b | 291 | static void gov_cancel_work(struct cpufreq_policy *policy) |
70f43e5e | 292 | { |
581c214b VK |
293 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
294 | ||
e40e7b25 RW |
295 | gov_clear_update_util(policy_dbs->policy); |
296 | irq_work_sync(&policy_dbs->irq_work); | |
297 | cancel_work_sync(&policy_dbs->work); | |
686cc637 | 298 | atomic_set(&policy_dbs->work_count, 0); |
e4db2813 | 299 | policy_dbs->work_in_progress = false; |
70f43e5e | 300 | } |
43e0ee36 | 301 | |
70f43e5e | 302 | static void dbs_work_handler(struct work_struct *work) |
43e0ee36 | 303 | { |
e40e7b25 | 304 | struct policy_dbs_info *policy_dbs; |
3a91b069 | 305 | struct cpufreq_policy *policy; |
ea59ee0d | 306 | struct dbs_governor *gov; |
43e0ee36 | 307 | |
e40e7b25 RW |
308 | policy_dbs = container_of(work, struct policy_dbs_info, work); |
309 | policy = policy_dbs->policy; | |
ea59ee0d | 310 | gov = dbs_governor_of(policy); |
3a91b069 | 311 | |
70f43e5e | 312 | /* |
9be4fd2c RW |
313 | * Make sure cpufreq_governor_limits() isn't evaluating load or the |
314 | * ondemand governor isn't updating the sampling rate in parallel. | |
70f43e5e | 315 | */ |
e40e7b25 | 316 | mutex_lock(&policy_dbs->timer_mutex); |
07aa4402 | 317 | gov_update_sample_delay(policy_dbs, gov->gov_dbs_timer(policy)); |
e40e7b25 | 318 | mutex_unlock(&policy_dbs->timer_mutex); |
70f43e5e | 319 | |
e4db2813 RW |
320 | /* Allow the utilization update handler to queue up more work. */ |
321 | atomic_set(&policy_dbs->work_count, 0); | |
9be4fd2c | 322 | /* |
e4db2813 RW |
323 | * If the update below is reordered with respect to the sample delay |
324 | * modification, the utilization update handler may end up using a stale | |
325 | * sample delay value. | |
9be4fd2c | 326 | */ |
e4db2813 RW |
327 | smp_wmb(); |
328 | policy_dbs->work_in_progress = false; | |
9be4fd2c RW |
329 | } |
330 | ||
331 | static void dbs_irq_work(struct irq_work *irq_work) | |
332 | { | |
e40e7b25 | 333 | struct policy_dbs_info *policy_dbs; |
70f43e5e | 334 | |
e40e7b25 RW |
335 | policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work); |
336 | schedule_work(&policy_dbs->work); | |
70f43e5e VK |
337 | } |
338 | ||
9be4fd2c RW |
339 | static void dbs_update_util_handler(struct update_util_data *data, u64 time, |
340 | unsigned long util, unsigned long max) | |
341 | { | |
342 | struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util); | |
e40e7b25 | 343 | struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; |
e4db2813 | 344 | u64 delta_ns; |
70f43e5e VK |
345 | |
346 | /* | |
9be4fd2c RW |
347 | * The work may not be allowed to be queued up right now. |
348 | * Possible reasons: | |
349 | * - Work has already been queued up or is in progress. | |
9be4fd2c | 350 | * - It is too early (too little time from the previous sample). |
70f43e5e | 351 | */ |
e4db2813 RW |
352 | if (policy_dbs->work_in_progress) |
353 | return; | |
354 | ||
355 | /* | |
356 | * If the reads below are reordered before the check above, the value | |
357 | * of sample_delay_ns used in the computation may be stale. | |
358 | */ | |
359 | smp_rmb(); | |
360 | delta_ns = time - policy_dbs->last_sample_time; | |
361 | if ((s64)delta_ns < policy_dbs->sample_delay_ns) | |
362 | return; | |
363 | ||
364 | /* | |
365 | * If the policy is not shared, the irq_work may be queued up right away | |
366 | * at this point. Otherwise, we need to ensure that only one of the | |
367 | * CPUs sharing the policy will do that. | |
368 | */ | |
369 | if (policy_dbs->is_shared && | |
370 | !atomic_add_unless(&policy_dbs->work_count, 1, 1)) | |
371 | return; | |
372 | ||
373 | policy_dbs->last_sample_time = time; | |
374 | policy_dbs->work_in_progress = true; | |
375 | irq_work_queue(&policy_dbs->irq_work); | |
43e0ee36 | 376 | } |
4447266b | 377 | |
bc505475 RW |
378 | static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy, |
379 | struct dbs_governor *gov) | |
44152cb8 | 380 | { |
e40e7b25 | 381 | struct policy_dbs_info *policy_dbs; |
44152cb8 VK |
382 | int j; |
383 | ||
7d5a9956 RW |
384 | /* Allocate memory for per-policy governor data. */ |
385 | policy_dbs = gov->alloc(); | |
e40e7b25 | 386 | if (!policy_dbs) |
bc505475 | 387 | return NULL; |
44152cb8 | 388 | |
581c214b | 389 | policy_dbs->policy = policy; |
e40e7b25 | 390 | mutex_init(&policy_dbs->timer_mutex); |
686cc637 | 391 | atomic_set(&policy_dbs->work_count, 0); |
e40e7b25 RW |
392 | init_irq_work(&policy_dbs->irq_work, dbs_irq_work); |
393 | INIT_WORK(&policy_dbs->work, dbs_work_handler); | |
cea6a9e7 RW |
394 | |
395 | /* Set policy_dbs for all CPUs, online+offline */ | |
396 | for_each_cpu(j, policy->related_cpus) { | |
8c8f77fd | 397 | struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); |
cea6a9e7 RW |
398 | |
399 | j_cdbs->policy_dbs = policy_dbs; | |
400 | j_cdbs->update_util.func = dbs_update_util_handler; | |
401 | } | |
bc505475 | 402 | return policy_dbs; |
44152cb8 VK |
403 | } |
404 | ||
8c8f77fd | 405 | static void free_policy_dbs_info(struct policy_dbs_info *policy_dbs, |
7bdad34d | 406 | struct dbs_governor *gov) |
44152cb8 | 407 | { |
44152cb8 VK |
408 | int j; |
409 | ||
e40e7b25 | 410 | mutex_destroy(&policy_dbs->timer_mutex); |
5e4500d8 | 411 | |
8c8f77fd RW |
412 | for_each_cpu(j, policy_dbs->policy->related_cpus) { |
413 | struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); | |
44152cb8 | 414 | |
cea6a9e7 RW |
415 | j_cdbs->policy_dbs = NULL; |
416 | j_cdbs->update_util.func = NULL; | |
417 | } | |
7d5a9956 | 418 | gov->free(policy_dbs); |
44152cb8 VK |
419 | } |
420 | ||
906a6e5a | 421 | static int cpufreq_governor_init(struct cpufreq_policy *policy) |
4471a34f | 422 | { |
ea59ee0d | 423 | struct dbs_governor *gov = dbs_governor_of(policy); |
1112e9d8 | 424 | struct dbs_data *dbs_data; |
bc505475 | 425 | struct policy_dbs_info *policy_dbs; |
714a2d9c | 426 | unsigned int latency; |
1112e9d8 | 427 | int ret = 0; |
4471a34f | 428 | |
a72c4959 VK |
429 | /* State should be equivalent to EXIT */ |
430 | if (policy->governor_data) | |
431 | return -EBUSY; | |
432 | ||
bc505475 RW |
433 | policy_dbs = alloc_policy_dbs_info(policy, gov); |
434 | if (!policy_dbs) | |
435 | return -ENOMEM; | |
44152cb8 | 436 | |
1112e9d8 RW |
437 | /* Protect gov->gdbs_data against concurrent updates. */ |
438 | mutex_lock(&gov_dbs_data_mutex); | |
439 | ||
440 | dbs_data = gov->gdbs_data; | |
bc505475 RW |
441 | if (dbs_data) { |
442 | if (WARN_ON(have_governor_per_policy())) { | |
443 | ret = -EINVAL; | |
444 | goto free_policy_dbs_info; | |
445 | } | |
bc505475 RW |
446 | policy_dbs->dbs_data = dbs_data; |
447 | policy->governor_data = policy_dbs; | |
c54df071 VK |
448 | |
449 | mutex_lock(&dbs_data->mutex); | |
450 | dbs_data->usage_count++; | |
451 | list_add(&policy_dbs->list, &dbs_data->policy_dbs_list); | |
452 | mutex_unlock(&dbs_data->mutex); | |
1112e9d8 | 453 | goto out; |
714a2d9c | 454 | } |
4d5dcc42 | 455 | |
714a2d9c | 456 | dbs_data = kzalloc(sizeof(*dbs_data), GFP_KERNEL); |
bc505475 RW |
457 | if (!dbs_data) { |
458 | ret = -ENOMEM; | |
459 | goto free_policy_dbs_info; | |
460 | } | |
44152cb8 | 461 | |
c54df071 | 462 | INIT_LIST_HEAD(&dbs_data->policy_dbs_list); |
c4435630 | 463 | mutex_init(&dbs_data->mutex); |
4d5dcc42 | 464 | |
7bdad34d | 465 | ret = gov->init(dbs_data, !policy->governor->initialized); |
714a2d9c | 466 | if (ret) |
e40e7b25 | 467 | goto free_policy_dbs_info; |
4d5dcc42 | 468 | |
714a2d9c VK |
469 | /* policy latency is in ns. Convert it to us first */ |
470 | latency = policy->cpuinfo.transition_latency / 1000; | |
471 | if (latency == 0) | |
472 | latency = 1; | |
4d5dcc42 | 473 | |
714a2d9c VK |
474 | /* Bring kernel and HW constraints together */ |
475 | dbs_data->min_sampling_rate = max(dbs_data->min_sampling_rate, | |
476 | MIN_LATENCY_MULTIPLIER * latency); | |
ff4b1789 VK |
477 | dbs_data->sampling_rate = max(dbs_data->min_sampling_rate, |
478 | LATENCY_MULTIPLIER * latency); | |
2361be23 | 479 | |
8eec1020 | 480 | if (!have_governor_per_policy()) |
7bdad34d | 481 | gov->gdbs_data = dbs_data; |
4d5dcc42 | 482 | |
bc505475 | 483 | policy->governor_data = policy_dbs; |
e4b133cc | 484 | |
c54df071 VK |
485 | policy_dbs->dbs_data = dbs_data; |
486 | dbs_data->usage_count = 1; | |
487 | list_add(&policy_dbs->list, &dbs_data->policy_dbs_list); | |
488 | ||
c4435630 VK |
489 | gov->kobj_type.sysfs_ops = &governor_sysfs_ops; |
490 | ret = kobject_init_and_add(&dbs_data->kobj, &gov->kobj_type, | |
491 | get_governor_parent_kobj(policy), | |
492 | "%s", gov->gov.name); | |
fafd5e8a | 493 | if (!ret) |
1112e9d8 | 494 | goto out; |
4d5dcc42 | 495 | |
fafd5e8a | 496 | /* Failure, so roll back. */ |
c4435630 | 497 | pr_err("cpufreq: Governor initialization failed (dbs_data kobject init error %d)\n", ret); |
4d5dcc42 | 498 | |
e4b133cc VK |
499 | policy->governor_data = NULL; |
500 | ||
8eec1020 | 501 | if (!have_governor_per_policy()) |
7bdad34d RW |
502 | gov->gdbs_data = NULL; |
503 | gov->exit(dbs_data, !policy->governor->initialized); | |
bc505475 RW |
504 | kfree(dbs_data); |
505 | ||
e40e7b25 | 506 | free_policy_dbs_info: |
8c8f77fd | 507 | free_policy_dbs_info(policy_dbs, gov); |
1112e9d8 RW |
508 | |
509 | out: | |
510 | mutex_unlock(&gov_dbs_data_mutex); | |
714a2d9c VK |
511 | return ret; |
512 | } | |
4d5dcc42 | 513 | |
5da3dd1e | 514 | static int cpufreq_governor_exit(struct cpufreq_policy *policy) |
714a2d9c | 515 | { |
ea59ee0d | 516 | struct dbs_governor *gov = dbs_governor_of(policy); |
bc505475 RW |
517 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
518 | struct dbs_data *dbs_data = policy_dbs->dbs_data; | |
c54df071 | 519 | int count; |
a72c4959 | 520 | |
1112e9d8 RW |
521 | /* Protect gov->gdbs_data against concurrent updates. */ |
522 | mutex_lock(&gov_dbs_data_mutex); | |
523 | ||
c54df071 VK |
524 | mutex_lock(&dbs_data->mutex); |
525 | list_del(&policy_dbs->list); | |
526 | count = --dbs_data->usage_count; | |
527 | mutex_unlock(&dbs_data->mutex); | |
528 | ||
529 | if (!count) { | |
c4435630 | 530 | kobject_put(&dbs_data->kobj); |
2361be23 | 531 | |
e4b133cc VK |
532 | policy->governor_data = NULL; |
533 | ||
8eec1020 | 534 | if (!have_governor_per_policy()) |
7bdad34d | 535 | gov->gdbs_data = NULL; |
4471a34f | 536 | |
7bdad34d | 537 | gov->exit(dbs_data, policy->governor->initialized == 1); |
c4435630 | 538 | mutex_destroy(&dbs_data->mutex); |
714a2d9c | 539 | kfree(dbs_data); |
e4b133cc VK |
540 | } else { |
541 | policy->governor_data = NULL; | |
4d5dcc42 | 542 | } |
44152cb8 | 543 | |
8c8f77fd | 544 | free_policy_dbs_info(policy_dbs, gov); |
1112e9d8 RW |
545 | |
546 | mutex_unlock(&gov_dbs_data_mutex); | |
a72c4959 | 547 | return 0; |
714a2d9c | 548 | } |
4d5dcc42 | 549 | |
5da3dd1e | 550 | static int cpufreq_governor_start(struct cpufreq_policy *policy) |
714a2d9c | 551 | { |
ea59ee0d | 552 | struct dbs_governor *gov = dbs_governor_of(policy); |
bc505475 RW |
553 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
554 | struct dbs_data *dbs_data = policy_dbs->dbs_data; | |
702c9e54 | 555 | unsigned int sampling_rate, ignore_nice, j; |
8847e038 | 556 | unsigned int io_busy; |
714a2d9c VK |
557 | |
558 | if (!policy->cur) | |
559 | return -EINVAL; | |
560 | ||
e4db2813 | 561 | policy_dbs->is_shared = policy_is_shared(policy); |
57dc3bcd | 562 | policy_dbs->rate_mult = 1; |
e4db2813 | 563 | |
ff4b1789 VK |
564 | sampling_rate = dbs_data->sampling_rate; |
565 | ignore_nice = dbs_data->ignore_nice_load; | |
8847e038 | 566 | io_busy = dbs_data->io_is_busy; |
4471a34f | 567 | |
714a2d9c | 568 | for_each_cpu(j, policy->cpus) { |
8c8f77fd | 569 | struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); |
714a2d9c | 570 | unsigned int prev_load; |
4471a34f | 571 | |
57eb832f | 572 | j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_cpu_wall, io_busy); |
4471a34f | 573 | |
57eb832f RW |
574 | prev_load = j_cdbs->prev_cpu_wall - j_cdbs->prev_cpu_idle; |
575 | j_cdbs->prev_load = 100 * prev_load / (unsigned int)j_cdbs->prev_cpu_wall; | |
18b46abd | 576 | |
714a2d9c VK |
577 | if (ignore_nice) |
578 | j_cdbs->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE]; | |
714a2d9c | 579 | } |
2abfa876 | 580 | |
702c9e54 | 581 | gov->start(policy); |
4471a34f | 582 | |
e40e7b25 | 583 | gov_set_update_util(policy_dbs, sampling_rate); |
714a2d9c VK |
584 | return 0; |
585 | } | |
586 | ||
5da3dd1e | 587 | static int cpufreq_governor_stop(struct cpufreq_policy *policy) |
714a2d9c | 588 | { |
581c214b | 589 | gov_cancel_work(policy); |
a72c4959 | 590 | return 0; |
714a2d9c | 591 | } |
4471a34f | 592 | |
5da3dd1e | 593 | static int cpufreq_governor_limits(struct cpufreq_policy *policy) |
714a2d9c | 594 | { |
bc505475 | 595 | struct policy_dbs_info *policy_dbs = policy->governor_data; |
8eeed095 | 596 | |
e9751894 | 597 | mutex_lock(&policy_dbs->timer_mutex); |
4cccf755 | 598 | |
e9751894 RW |
599 | if (policy->max < policy->cur) |
600 | __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H); | |
601 | else if (policy->min > policy->cur) | |
602 | __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L); | |
4cccf755 RW |
603 | |
604 | gov_update_sample_delay(policy_dbs, 0); | |
605 | ||
e9751894 | 606 | mutex_unlock(&policy_dbs->timer_mutex); |
a72c4959 VK |
607 | |
608 | return 0; | |
714a2d9c | 609 | } |
4471a34f | 610 | |
906a6e5a | 611 | int cpufreq_governor_dbs(struct cpufreq_policy *policy, unsigned int event) |
714a2d9c | 612 | { |
5da3dd1e | 613 | if (event == CPUFREQ_GOV_POLICY_INIT) { |
1112e9d8 | 614 | return cpufreq_governor_init(policy); |
5da3dd1e RW |
615 | } else if (policy->governor_data) { |
616 | switch (event) { | |
617 | case CPUFREQ_GOV_POLICY_EXIT: | |
1112e9d8 | 618 | return cpufreq_governor_exit(policy); |
5da3dd1e | 619 | case CPUFREQ_GOV_START: |
1112e9d8 | 620 | return cpufreq_governor_start(policy); |
5da3dd1e | 621 | case CPUFREQ_GOV_STOP: |
1112e9d8 | 622 | return cpufreq_governor_stop(policy); |
5da3dd1e | 623 | case CPUFREQ_GOV_LIMITS: |
1112e9d8 | 624 | return cpufreq_governor_limits(policy); |
5da3dd1e | 625 | } |
4471a34f | 626 | } |
1112e9d8 | 627 | return -EINVAL; |
4471a34f VK |
628 | } |
629 | EXPORT_SYMBOL_GPL(cpufreq_governor_dbs); |