Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/cpufreq/cpufreq.c | |
3 | * | |
4 | * Copyright (C) 2001 Russell King | |
5 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> | |
bb176f7d | 6 | * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> |
1da177e4 | 7 | * |
c32b6b8e | 8 | * Oct 2005 - Ashok Raj <ashok.raj@intel.com> |
32ee8c3e | 9 | * Added handling for CPU hotplug |
8ff69732 DJ |
10 | * Feb 2006 - Jacob Shin <jacob.shin@amd.com> |
11 | * Fix handling for CPU hotplug -- affected CPUs | |
c32b6b8e | 12 | * |
1da177e4 LT |
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. | |
1da177e4 LT |
16 | */ |
17 | ||
db701151 VK |
18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
19 | ||
5ff0a268 | 20 | #include <linux/cpu.h> |
1da177e4 | 21 | #include <linux/cpufreq.h> |
5c238a8b | 22 | #include <linux/cpu_cooling.h> |
1da177e4 | 23 | #include <linux/delay.h> |
1da177e4 | 24 | #include <linux/device.h> |
5ff0a268 VK |
25 | #include <linux/init.h> |
26 | #include <linux/kernel_stat.h> | |
27 | #include <linux/module.h> | |
3fc54d37 | 28 | #include <linux/mutex.h> |
5ff0a268 | 29 | #include <linux/slab.h> |
2f0aea93 | 30 | #include <linux/suspend.h> |
90de2a4a | 31 | #include <linux/syscore_ops.h> |
5ff0a268 | 32 | #include <linux/tick.h> |
6f4f2723 TR |
33 | #include <trace/events/power.h> |
34 | ||
b4f0676f | 35 | static LIST_HEAD(cpufreq_policy_list); |
f963735a | 36 | |
f963735a | 37 | /* Macros to iterate over CPU policies */ |
fd7dc7e6 EB |
38 | #define for_each_suitable_policy(__policy, __active) \ |
39 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ | |
40 | if ((__active) == !policy_is_inactive(__policy)) | |
f963735a VK |
41 | |
42 | #define for_each_active_policy(__policy) \ | |
43 | for_each_suitable_policy(__policy, true) | |
44 | #define for_each_inactive_policy(__policy) \ | |
45 | for_each_suitable_policy(__policy, false) | |
46 | ||
47 | #define for_each_policy(__policy) \ | |
b4f0676f VK |
48 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) |
49 | ||
f7b27061 VK |
50 | /* Iterate over governors */ |
51 | static LIST_HEAD(cpufreq_governor_list); | |
52 | #define for_each_governor(__governor) \ | |
53 | list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) | |
54 | ||
1da177e4 | 55 | /** |
cd878479 | 56 | * The "cpufreq driver" - the arch- or hardware-dependent low |
1da177e4 LT |
57 | * level driver of CPUFreq support, and its spinlock. This lock |
58 | * also protects the cpufreq_cpu_data array. | |
59 | */ | |
1c3d85dd | 60 | static struct cpufreq_driver *cpufreq_driver; |
7a6aedfa | 61 | static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); |
bb176f7d | 62 | static DEFINE_RWLOCK(cpufreq_driver_lock); |
bb176f7d | 63 | |
2f0aea93 VK |
64 | /* Flag to suspend/resume CPUFreq governors */ |
65 | static bool cpufreq_suspended; | |
1da177e4 | 66 | |
9c0ebcf7 VK |
67 | static inline bool has_target(void) |
68 | { | |
69 | return cpufreq_driver->target_index || cpufreq_driver->target; | |
70 | } | |
71 | ||
1da177e4 | 72 | /* internal prototypes */ |
d92d50a4 | 73 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy); |
a92604b4 RW |
74 | static int cpufreq_init_governor(struct cpufreq_policy *policy); |
75 | static void cpufreq_exit_governor(struct cpufreq_policy *policy); | |
0a300767 | 76 | static int cpufreq_start_governor(struct cpufreq_policy *policy); |
a92604b4 RW |
77 | static void cpufreq_stop_governor(struct cpufreq_policy *policy); |
78 | static void cpufreq_governor_limits(struct cpufreq_policy *policy); | |
45482c70 | 79 | |
1da177e4 | 80 | /** |
32ee8c3e DJ |
81 | * Two notifier lists: the "policy" list is involved in the |
82 | * validation process for a new CPU frequency policy; the | |
1da177e4 LT |
83 | * "transition" list for kernel code that needs to handle |
84 | * changes to devices when the CPU clock speed changes. | |
85 | * The mutex locks both lists. | |
86 | */ | |
e041c683 | 87 | static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); |
cc85de36 | 88 | SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); |
1da177e4 | 89 | |
a7b422cd | 90 | static int off __read_mostly; |
da584455 | 91 | static int cpufreq_disabled(void) |
a7b422cd KRW |
92 | { |
93 | return off; | |
94 | } | |
95 | void disable_cpufreq(void) | |
96 | { | |
97 | off = 1; | |
98 | } | |
29464f28 | 99 | static DEFINE_MUTEX(cpufreq_governor_mutex); |
1da177e4 | 100 | |
4d5dcc42 VK |
101 | bool have_governor_per_policy(void) |
102 | { | |
0b981e70 | 103 | return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); |
4d5dcc42 | 104 | } |
3f869d6d | 105 | EXPORT_SYMBOL_GPL(have_governor_per_policy); |
4d5dcc42 | 106 | |
944e9a03 VK |
107 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) |
108 | { | |
109 | if (have_governor_per_policy()) | |
110 | return &policy->kobj; | |
111 | else | |
112 | return cpufreq_global_kobject; | |
113 | } | |
114 | EXPORT_SYMBOL_GPL(get_governor_parent_kobj); | |
115 | ||
72a4ce34 VK |
116 | static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) |
117 | { | |
118 | u64 idle_time; | |
119 | u64 cur_wall_time; | |
120 | u64 busy_time; | |
121 | ||
7fb1327e | 122 | cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); |
72a4ce34 VK |
123 | |
124 | busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; | |
125 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; | |
126 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; | |
127 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; | |
128 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; | |
129 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; | |
130 | ||
131 | idle_time = cur_wall_time - busy_time; | |
132 | if (wall) | |
7fb1327e | 133 | *wall = div_u64(cur_wall_time, NSEC_PER_USEC); |
72a4ce34 | 134 | |
7fb1327e | 135 | return div_u64(idle_time, NSEC_PER_USEC); |
72a4ce34 VK |
136 | } |
137 | ||
138 | u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) | |
139 | { | |
140 | u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); | |
141 | ||
142 | if (idle_time == -1ULL) | |
143 | return get_cpu_idle_time_jiffy(cpu, wall); | |
144 | else if (!io_busy) | |
145 | idle_time += get_cpu_iowait_time_us(cpu, wall); | |
146 | ||
147 | return idle_time; | |
148 | } | |
149 | EXPORT_SYMBOL_GPL(get_cpu_idle_time); | |
150 | ||
e7d5459d DE |
151 | __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, |
152 | unsigned long max_freq) | |
153 | { | |
154 | } | |
155 | EXPORT_SYMBOL_GPL(arch_set_freq_scale); | |
156 | ||
70e9e778 VK |
157 | /* |
158 | * This is a generic cpufreq init() routine which can be used by cpufreq | |
159 | * drivers of SMP systems. It will do following: | |
160 | * - validate & show freq table passed | |
161 | * - set policies transition latency | |
162 | * - policy->cpus with all possible CPUs | |
163 | */ | |
164 | int cpufreq_generic_init(struct cpufreq_policy *policy, | |
165 | struct cpufreq_frequency_table *table, | |
166 | unsigned int transition_latency) | |
167 | { | |
92c99d15 | 168 | policy->freq_table = table; |
70e9e778 VK |
169 | policy->cpuinfo.transition_latency = transition_latency; |
170 | ||
171 | /* | |
58405af6 | 172 | * The driver only supports the SMP configuration where all processors |
70e9e778 VK |
173 | * share the clock and voltage and clock. |
174 | */ | |
175 | cpumask_setall(policy->cpus); | |
176 | ||
177 | return 0; | |
178 | } | |
179 | EXPORT_SYMBOL_GPL(cpufreq_generic_init); | |
180 | ||
1f0bd44e | 181 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) |
652ed95d VK |
182 | { |
183 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); | |
184 | ||
988bed09 VK |
185 | return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; |
186 | } | |
1f0bd44e | 187 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); |
988bed09 VK |
188 | |
189 | unsigned int cpufreq_generic_get(unsigned int cpu) | |
190 | { | |
191 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); | |
192 | ||
652ed95d | 193 | if (!policy || IS_ERR(policy->clk)) { |
e837f9b5 JP |
194 | pr_err("%s: No %s associated to cpu: %d\n", |
195 | __func__, policy ? "clk" : "policy", cpu); | |
652ed95d VK |
196 | return 0; |
197 | } | |
198 | ||
199 | return clk_get_rate(policy->clk) / 1000; | |
200 | } | |
201 | EXPORT_SYMBOL_GPL(cpufreq_generic_get); | |
202 | ||
50e9c852 | 203 | /** |
5d094fea RW |
204 | * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. |
205 | * @cpu: CPU to find the policy for. | |
50e9c852 | 206 | * |
5d094fea RW |
207 | * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment |
208 | * the kobject reference counter of that policy. Return a valid policy on | |
209 | * success or NULL on failure. | |
50e9c852 | 210 | * |
5d094fea RW |
211 | * The policy returned by this function has to be released with the help of |
212 | * cpufreq_cpu_put() to balance its kobject reference counter properly. | |
50e9c852 | 213 | */ |
6eed9404 | 214 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
1da177e4 | 215 | { |
6eed9404 | 216 | struct cpufreq_policy *policy = NULL; |
1da177e4 LT |
217 | unsigned long flags; |
218 | ||
1b947c90 | 219 | if (WARN_ON(cpu >= nr_cpu_ids)) |
6eed9404 VK |
220 | return NULL; |
221 | ||
1da177e4 | 222 | /* get the cpufreq driver */ |
1c3d85dd | 223 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
1da177e4 | 224 | |
6eed9404 VK |
225 | if (cpufreq_driver) { |
226 | /* get the CPU */ | |
988bed09 | 227 | policy = cpufreq_cpu_get_raw(cpu); |
6eed9404 VK |
228 | if (policy) |
229 | kobject_get(&policy->kobj); | |
230 | } | |
1da177e4 | 231 | |
6eed9404 | 232 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
1da177e4 | 233 | |
3a3e9e06 | 234 | return policy; |
a9144436 | 235 | } |
1da177e4 LT |
236 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); |
237 | ||
50e9c852 | 238 | /** |
5d094fea RW |
239 | * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. |
240 | * @policy: cpufreq policy returned by cpufreq_cpu_get(). | |
50e9c852 | 241 | */ |
3a3e9e06 | 242 | void cpufreq_cpu_put(struct cpufreq_policy *policy) |
1da177e4 | 243 | { |
6eed9404 | 244 | kobject_put(&policy->kobj); |
1da177e4 LT |
245 | } |
246 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); | |
247 | ||
540a3758 RW |
248 | /** |
249 | * cpufreq_cpu_release - Unlock a policy and decrement its usage counter. | |
250 | * @policy: cpufreq policy returned by cpufreq_cpu_acquire(). | |
251 | */ | |
9083e498 | 252 | void cpufreq_cpu_release(struct cpufreq_policy *policy) |
540a3758 RW |
253 | { |
254 | if (WARN_ON(!policy)) | |
255 | return; | |
256 | ||
257 | lockdep_assert_held(&policy->rwsem); | |
258 | ||
259 | up_write(&policy->rwsem); | |
260 | ||
261 | cpufreq_cpu_put(policy); | |
262 | } | |
263 | ||
264 | /** | |
265 | * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it. | |
266 | * @cpu: CPU to find the policy for. | |
267 | * | |
268 | * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and | |
269 | * if the policy returned by it is not NULL, acquire its rwsem for writing. | |
270 | * Return the policy if it is active or release it and return NULL otherwise. | |
271 | * | |
272 | * The policy returned by this function has to be released with the help of | |
273 | * cpufreq_cpu_release() in order to release its rwsem and balance its usage | |
274 | * counter properly. | |
275 | */ | |
9083e498 | 276 | struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu) |
540a3758 RW |
277 | { |
278 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); | |
279 | ||
280 | if (!policy) | |
281 | return NULL; | |
282 | ||
283 | down_write(&policy->rwsem); | |
284 | ||
285 | if (policy_is_inactive(policy)) { | |
286 | cpufreq_cpu_release(policy); | |
287 | return NULL; | |
288 | } | |
289 | ||
290 | return policy; | |
291 | } | |
292 | ||
1da177e4 LT |
293 | /********************************************************************* |
294 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * | |
295 | *********************************************************************/ | |
296 | ||
297 | /** | |
298 | * adjust_jiffies - adjust the system "loops_per_jiffy" | |
299 | * | |
300 | * This function alters the system "loops_per_jiffy" for the clock | |
301 | * speed change. Note that loops_per_jiffy cannot be updated on SMP | |
32ee8c3e | 302 | * systems as each CPU might be scaled differently. So, use the arch |
1da177e4 LT |
303 | * per-CPU loops_per_jiffy value wherever possible. |
304 | */ | |
858119e1 | 305 | static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
1da177e4 | 306 | { |
39c132ee VK |
307 | #ifndef CONFIG_SMP |
308 | static unsigned long l_p_j_ref; | |
309 | static unsigned int l_p_j_ref_freq; | |
310 | ||
1da177e4 LT |
311 | if (ci->flags & CPUFREQ_CONST_LOOPS) |
312 | return; | |
313 | ||
314 | if (!l_p_j_ref_freq) { | |
315 | l_p_j_ref = loops_per_jiffy; | |
316 | l_p_j_ref_freq = ci->old; | |
e837f9b5 JP |
317 | pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", |
318 | l_p_j_ref, l_p_j_ref_freq); | |
1da177e4 | 319 | } |
0b443ead | 320 | if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { |
e08f5f5b GS |
321 | loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, |
322 | ci->new); | |
e837f9b5 JP |
323 | pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", |
324 | loops_per_jiffy, ci->new); | |
1da177e4 | 325 | } |
1da177e4 | 326 | #endif |
39c132ee | 327 | } |
1da177e4 | 328 | |
20b5324d VK |
329 | /** |
330 | * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies. | |
331 | * @policy: cpufreq policy to enable fast frequency switching for. | |
332 | * @freqs: contain details of the frequency update. | |
333 | * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. | |
334 | * | |
335 | * This function calls the transition notifiers and the "adjust_jiffies" | |
336 | * function. It is called twice on all CPU frequency changes that have | |
337 | * external effects. | |
338 | */ | |
339 | static void cpufreq_notify_transition(struct cpufreq_policy *policy, | |
340 | struct cpufreq_freqs *freqs, | |
341 | unsigned int state) | |
1da177e4 | 342 | { |
df24014a VK |
343 | int cpu; |
344 | ||
1da177e4 LT |
345 | BUG_ON(irqs_disabled()); |
346 | ||
d5aaffa9 DB |
347 | if (cpufreq_disabled()) |
348 | return; | |
349 | ||
df24014a | 350 | freqs->policy = policy; |
1c3d85dd | 351 | freqs->flags = cpufreq_driver->flags; |
2d06d8c4 | 352 | pr_debug("notification %u of frequency transition to %u kHz\n", |
e837f9b5 | 353 | state, freqs->new); |
1da177e4 | 354 | |
1da177e4 LT |
355 | switch (state) { |
356 | case CPUFREQ_PRECHANGE: | |
20b5324d VK |
357 | /* |
358 | * Detect if the driver reported a value as "old frequency" | |
e4472cb3 DJ |
359 | * which is not equal to what the cpufreq core thinks is |
360 | * "old frequency". | |
1da177e4 | 361 | */ |
98015228 VK |
362 | if (policy->cur && policy->cur != freqs->old) { |
363 | pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", | |
364 | freqs->old, policy->cur); | |
365 | freqs->old = policy->cur; | |
1da177e4 | 366 | } |
20b5324d | 367 | |
df24014a VK |
368 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
369 | CPUFREQ_PRECHANGE, freqs); | |
20b5324d | 370 | |
1da177e4 LT |
371 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
372 | break; | |
e4472cb3 | 373 | |
1da177e4 LT |
374 | case CPUFREQ_POSTCHANGE: |
375 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); | |
20b5324d VK |
376 | pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, |
377 | cpumask_pr_args(policy->cpus)); | |
378 | ||
df24014a VK |
379 | for_each_cpu(cpu, policy->cpus) |
380 | trace_cpu_frequency(freqs->new, cpu); | |
381 | ||
382 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, | |
383 | CPUFREQ_POSTCHANGE, freqs); | |
20b5324d | 384 | |
1aefc75b | 385 | cpufreq_stats_record_transition(policy, freqs->new); |
20b5324d | 386 | policy->cur = freqs->new; |
1da177e4 | 387 | } |
1da177e4 | 388 | } |
bb176f7d | 389 | |
f7ba3b41 | 390 | /* Do post notifications when there are chances that transition has failed */ |
236a9800 | 391 | static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, |
f7ba3b41 VK |
392 | struct cpufreq_freqs *freqs, int transition_failed) |
393 | { | |
394 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); | |
395 | if (!transition_failed) | |
396 | return; | |
397 | ||
398 | swap(freqs->old, freqs->new); | |
399 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); | |
400 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); | |
401 | } | |
f7ba3b41 | 402 | |
12478cf0 SB |
403 | void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, |
404 | struct cpufreq_freqs *freqs) | |
405 | { | |
ca654dc3 SB |
406 | |
407 | /* | |
408 | * Catch double invocations of _begin() which lead to self-deadlock. | |
409 | * ASYNC_NOTIFICATION drivers are left out because the cpufreq core | |
410 | * doesn't invoke _begin() on their behalf, and hence the chances of | |
411 | * double invocations are very low. Moreover, there are scenarios | |
412 | * where these checks can emit false-positive warnings in these | |
413 | * drivers; so we avoid that by skipping them altogether. | |
414 | */ | |
415 | WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) | |
416 | && current == policy->transition_task); | |
417 | ||
12478cf0 SB |
418 | wait: |
419 | wait_event(policy->transition_wait, !policy->transition_ongoing); | |
420 | ||
421 | spin_lock(&policy->transition_lock); | |
422 | ||
423 | if (unlikely(policy->transition_ongoing)) { | |
424 | spin_unlock(&policy->transition_lock); | |
425 | goto wait; | |
426 | } | |
427 | ||
428 | policy->transition_ongoing = true; | |
ca654dc3 | 429 | policy->transition_task = current; |
12478cf0 SB |
430 | |
431 | spin_unlock(&policy->transition_lock); | |
432 | ||
433 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); | |
434 | } | |
435 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); | |
436 | ||
437 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, | |
438 | struct cpufreq_freqs *freqs, int transition_failed) | |
439 | { | |
0e7ea2f3 | 440 | if (WARN_ON(!policy->transition_ongoing)) |
12478cf0 SB |
441 | return; |
442 | ||
443 | cpufreq_notify_post_transition(policy, freqs, transition_failed); | |
444 | ||
445 | policy->transition_ongoing = false; | |
ca654dc3 | 446 | policy->transition_task = NULL; |
12478cf0 SB |
447 | |
448 | wake_up(&policy->transition_wait); | |
449 | } | |
450 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); | |
451 | ||
b7898fda RW |
452 | /* |
453 | * Fast frequency switching status count. Positive means "enabled", negative | |
454 | * means "disabled" and 0 means "not decided yet". | |
455 | */ | |
456 | static int cpufreq_fast_switch_count; | |
457 | static DEFINE_MUTEX(cpufreq_fast_switch_lock); | |
458 | ||
459 | static void cpufreq_list_transition_notifiers(void) | |
460 | { | |
461 | struct notifier_block *nb; | |
462 | ||
463 | pr_info("Registered transition notifiers:\n"); | |
464 | ||
465 | mutex_lock(&cpufreq_transition_notifier_list.mutex); | |
466 | ||
467 | for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) | |
d75f773c | 468 | pr_info("%pS\n", nb->notifier_call); |
b7898fda RW |
469 | |
470 | mutex_unlock(&cpufreq_transition_notifier_list.mutex); | |
471 | } | |
472 | ||
473 | /** | |
474 | * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. | |
475 | * @policy: cpufreq policy to enable fast frequency switching for. | |
476 | * | |
477 | * Try to enable fast frequency switching for @policy. | |
478 | * | |
479 | * The attempt will fail if there is at least one transition notifier registered | |
480 | * at this point, as fast frequency switching is quite fundamentally at odds | |
481 | * with transition notifiers. Thus if successful, it will make registration of | |
482 | * transition notifiers fail going forward. | |
483 | */ | |
484 | void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) | |
485 | { | |
486 | lockdep_assert_held(&policy->rwsem); | |
487 | ||
488 | if (!policy->fast_switch_possible) | |
489 | return; | |
490 | ||
491 | mutex_lock(&cpufreq_fast_switch_lock); | |
492 | if (cpufreq_fast_switch_count >= 0) { | |
493 | cpufreq_fast_switch_count++; | |
494 | policy->fast_switch_enabled = true; | |
495 | } else { | |
496 | pr_warn("CPU%u: Fast frequency switching not enabled\n", | |
497 | policy->cpu); | |
498 | cpufreq_list_transition_notifiers(); | |
499 | } | |
500 | mutex_unlock(&cpufreq_fast_switch_lock); | |
501 | } | |
502 | EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); | |
503 | ||
6c9d9c81 RW |
504 | /** |
505 | * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. | |
506 | * @policy: cpufreq policy to disable fast frequency switching for. | |
507 | */ | |
508 | void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) | |
b7898fda RW |
509 | { |
510 | mutex_lock(&cpufreq_fast_switch_lock); | |
511 | if (policy->fast_switch_enabled) { | |
512 | policy->fast_switch_enabled = false; | |
513 | if (!WARN_ON(cpufreq_fast_switch_count <= 0)) | |
514 | cpufreq_fast_switch_count--; | |
515 | } | |
516 | mutex_unlock(&cpufreq_fast_switch_lock); | |
517 | } | |
6c9d9c81 | 518 | EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); |
1da177e4 | 519 | |
e3c06236 SM |
520 | /** |
521 | * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported | |
522 | * one. | |
523 | * @target_freq: target frequency to resolve. | |
524 | * | |
525 | * The target to driver frequency mapping is cached in the policy. | |
526 | * | |
527 | * Return: Lowest driver-supported frequency greater than or equal to the | |
528 | * given target_freq, subject to policy (min/max) and driver limitations. | |
529 | */ | |
530 | unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, | |
531 | unsigned int target_freq) | |
532 | { | |
533 | target_freq = clamp_val(target_freq, policy->min, policy->max); | |
534 | policy->cached_target_freq = target_freq; | |
abe8bd02 VK |
535 | |
536 | if (cpufreq_driver->target_index) { | |
537 | int idx; | |
538 | ||
539 | idx = cpufreq_frequency_table_target(policy, target_freq, | |
540 | CPUFREQ_RELATION_L); | |
541 | policy->cached_resolved_idx = idx; | |
542 | return policy->freq_table[idx].frequency; | |
543 | } | |
544 | ||
e3c06236 SM |
545 | if (cpufreq_driver->resolve_freq) |
546 | return cpufreq_driver->resolve_freq(policy, target_freq); | |
abe8bd02 VK |
547 | |
548 | return target_freq; | |
e3c06236 | 549 | } |
ae2c1ca6 | 550 | EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); |
e3c06236 | 551 | |
aa7519af VK |
552 | unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) |
553 | { | |
554 | unsigned int latency; | |
555 | ||
556 | if (policy->transition_delay_us) | |
557 | return policy->transition_delay_us; | |
558 | ||
559 | latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; | |
e948bc8f VK |
560 | if (latency) { |
561 | /* | |
562 | * For platforms that can change the frequency very fast (< 10 | |
563 | * us), the above formula gives a decent transition delay. But | |
564 | * for platforms where transition_latency is in milliseconds, it | |
565 | * ends up giving unrealistic values. | |
566 | * | |
567 | * Cap the default transition delay to 10 ms, which seems to be | |
568 | * a reasonable amount of time after which we should reevaluate | |
569 | * the frequency. | |
570 | */ | |
571 | return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); | |
572 | } | |
aa7519af VK |
573 | |
574 | return LATENCY_MULTIPLIER; | |
575 | } | |
576 | EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); | |
577 | ||
1da177e4 LT |
578 | /********************************************************************* |
579 | * SYSFS INTERFACE * | |
580 | *********************************************************************/ | |
8a5c74a1 | 581 | static ssize_t show_boost(struct kobject *kobj, |
625c85a6 | 582 | struct kobj_attribute *attr, char *buf) |
6f19efc0 LM |
583 | { |
584 | return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); | |
585 | } | |
586 | ||
625c85a6 VK |
587 | static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, |
588 | const char *buf, size_t count) | |
6f19efc0 LM |
589 | { |
590 | int ret, enable; | |
591 | ||
592 | ret = sscanf(buf, "%d", &enable); | |
593 | if (ret != 1 || enable < 0 || enable > 1) | |
594 | return -EINVAL; | |
595 | ||
596 | if (cpufreq_boost_trigger_state(enable)) { | |
e837f9b5 JP |
597 | pr_err("%s: Cannot %s BOOST!\n", |
598 | __func__, enable ? "enable" : "disable"); | |
6f19efc0 LM |
599 | return -EINVAL; |
600 | } | |
601 | ||
e837f9b5 JP |
602 | pr_debug("%s: cpufreq BOOST %s\n", |
603 | __func__, enable ? "enabled" : "disabled"); | |
6f19efc0 LM |
604 | |
605 | return count; | |
606 | } | |
607 | define_one_global_rw(boost); | |
1da177e4 | 608 | |
42f91fa1 | 609 | static struct cpufreq_governor *find_governor(const char *str_governor) |
3bcb09a3 JF |
610 | { |
611 | struct cpufreq_governor *t; | |
612 | ||
f7b27061 | 613 | for_each_governor(t) |
7c4f4539 | 614 | if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) |
3bcb09a3 JF |
615 | return t; |
616 | ||
617 | return NULL; | |
618 | } | |
619 | ||
ab05d97a YH |
620 | static int cpufreq_parse_policy(char *str_governor, |
621 | struct cpufreq_policy *policy) | |
622 | { | |
623 | if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { | |
624 | policy->policy = CPUFREQ_POLICY_PERFORMANCE; | |
625 | return 0; | |
626 | } | |
627 | if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { | |
628 | policy->policy = CPUFREQ_POLICY_POWERSAVE; | |
629 | return 0; | |
630 | } | |
631 | return -EINVAL; | |
632 | } | |
633 | ||
1da177e4 | 634 | /** |
5ddc6d4e | 635 | * cpufreq_parse_governor - parse a governor string only for has_target() |
1da177e4 | 636 | */ |
ae0ff89f RW |
637 | static int cpufreq_parse_governor(char *str_governor, |
638 | struct cpufreq_policy *policy) | |
1da177e4 | 639 | { |
ab05d97a | 640 | struct cpufreq_governor *t; |
045149e6 | 641 | |
ab05d97a | 642 | mutex_lock(&cpufreq_governor_mutex); |
3bcb09a3 | 643 | |
ab05d97a YH |
644 | t = find_governor(str_governor); |
645 | if (!t) { | |
646 | int ret; | |
3bcb09a3 | 647 | |
ab05d97a | 648 | mutex_unlock(&cpufreq_governor_mutex); |
045149e6 | 649 | |
ab05d97a YH |
650 | ret = request_module("cpufreq_%s", str_governor); |
651 | if (ret) | |
652 | return -EINVAL; | |
045149e6 | 653 | |
ab05d97a | 654 | mutex_lock(&cpufreq_governor_mutex); |
ea714970 | 655 | |
ab05d97a YH |
656 | t = find_governor(str_governor); |
657 | } | |
658 | if (t && !try_module_get(t->owner)) | |
659 | t = NULL; | |
ea714970 | 660 | |
ab05d97a | 661 | mutex_unlock(&cpufreq_governor_mutex); |
045149e6 | 662 | |
ab05d97a YH |
663 | if (t) { |
664 | policy->governor = t; | |
665 | return 0; | |
1da177e4 | 666 | } |
045149e6 RW |
667 | |
668 | return -EINVAL; | |
1da177e4 | 669 | } |
1da177e4 | 670 | |
1da177e4 | 671 | /** |
e08f5f5b GS |
672 | * cpufreq_per_cpu_attr_read() / show_##file_name() - |
673 | * print out cpufreq information | |
1da177e4 LT |
674 | * |
675 | * Write out information from cpufreq_driver->policy[cpu]; object must be | |
676 | * "unsigned int". | |
677 | */ | |
678 | ||
32ee8c3e DJ |
679 | #define show_one(file_name, object) \ |
680 | static ssize_t show_##file_name \ | |
905d77cd | 681 | (struct cpufreq_policy *policy, char *buf) \ |
32ee8c3e | 682 | { \ |
29464f28 | 683 | return sprintf(buf, "%u\n", policy->object); \ |
1da177e4 LT |
684 | } |
685 | ||
686 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); | |
687 | show_one(cpuinfo_max_freq, cpuinfo.max_freq); | |
ed129784 | 688 | show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); |
1da177e4 LT |
689 | show_one(scaling_min_freq, min); |
690 | show_one(scaling_max_freq, max); | |
c034b02e | 691 | |
f8475cef LB |
692 | __weak unsigned int arch_freq_get_on_cpu(int cpu) |
693 | { | |
694 | return 0; | |
695 | } | |
696 | ||
09347b29 | 697 | static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) |
c034b02e DB |
698 | { |
699 | ssize_t ret; | |
f8475cef | 700 | unsigned int freq; |
c034b02e | 701 | |
f8475cef LB |
702 | freq = arch_freq_get_on_cpu(policy->cpu); |
703 | if (freq) | |
704 | ret = sprintf(buf, "%u\n", freq); | |
705 | else if (cpufreq_driver && cpufreq_driver->setpolicy && | |
706 | cpufreq_driver->get) | |
c034b02e DB |
707 | ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); |
708 | else | |
709 | ret = sprintf(buf, "%u\n", policy->cur); | |
710 | return ret; | |
711 | } | |
1da177e4 LT |
712 | |
713 | /** | |
714 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access | |
715 | */ | |
716 | #define store_one(file_name, object) \ | |
717 | static ssize_t store_##file_name \ | |
905d77cd | 718 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
1da177e4 | 719 | { \ |
619c144c | 720 | int ret, temp; \ |
1da177e4 LT |
721 | struct cpufreq_policy new_policy; \ |
722 | \ | |
8fa5b631 | 723 | memcpy(&new_policy, policy, sizeof(*policy)); \ |
c7d1f119 TW |
724 | new_policy.min = policy->user_policy.min; \ |
725 | new_policy.max = policy->user_policy.max; \ | |
1da177e4 | 726 | \ |
29464f28 | 727 | ret = sscanf(buf, "%u", &new_policy.object); \ |
1da177e4 LT |
728 | if (ret != 1) \ |
729 | return -EINVAL; \ | |
730 | \ | |
619c144c | 731 | temp = new_policy.object; \ |
037ce839 | 732 | ret = cpufreq_set_policy(policy, &new_policy); \ |
619c144c VH |
733 | if (!ret) \ |
734 | policy->user_policy.object = temp; \ | |
1da177e4 LT |
735 | \ |
736 | return ret ? ret : count; \ | |
737 | } | |
738 | ||
29464f28 DJ |
739 | store_one(scaling_min_freq, min); |
740 | store_one(scaling_max_freq, max); | |
1da177e4 LT |
741 | |
742 | /** | |
743 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware | |
744 | */ | |
905d77cd DJ |
745 | static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, |
746 | char *buf) | |
1da177e4 | 747 | { |
d92d50a4 | 748 | unsigned int cur_freq = __cpufreq_get(policy); |
9b4f603e RW |
749 | |
750 | if (cur_freq) | |
751 | return sprintf(buf, "%u\n", cur_freq); | |
752 | ||
753 | return sprintf(buf, "<unknown>\n"); | |
1da177e4 LT |
754 | } |
755 | ||
1da177e4 LT |
756 | /** |
757 | * show_scaling_governor - show the current policy for the specified CPU | |
758 | */ | |
905d77cd | 759 | static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 760 | { |
29464f28 | 761 | if (policy->policy == CPUFREQ_POLICY_POWERSAVE) |
1da177e4 LT |
762 | return sprintf(buf, "powersave\n"); |
763 | else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) | |
764 | return sprintf(buf, "performance\n"); | |
765 | else if (policy->governor) | |
4b972f0b | 766 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", |
29464f28 | 767 | policy->governor->name); |
1da177e4 LT |
768 | return -EINVAL; |
769 | } | |
770 | ||
1da177e4 LT |
771 | /** |
772 | * store_scaling_governor - store policy for the specified CPU | |
773 | */ | |
905d77cd DJ |
774 | static ssize_t store_scaling_governor(struct cpufreq_policy *policy, |
775 | const char *buf, size_t count) | |
1da177e4 | 776 | { |
5136fa56 | 777 | int ret; |
1da177e4 LT |
778 | char str_governor[16]; |
779 | struct cpufreq_policy new_policy; | |
780 | ||
8fa5b631 | 781 | memcpy(&new_policy, policy, sizeof(*policy)); |
1da177e4 | 782 | |
29464f28 | 783 | ret = sscanf(buf, "%15s", str_governor); |
1da177e4 LT |
784 | if (ret != 1) |
785 | return -EINVAL; | |
786 | ||
ab05d97a YH |
787 | if (cpufreq_driver->setpolicy) { |
788 | if (cpufreq_parse_policy(str_governor, &new_policy)) | |
789 | return -EINVAL; | |
790 | } else { | |
791 | if (cpufreq_parse_governor(str_governor, &new_policy)) | |
792 | return -EINVAL; | |
793 | } | |
1da177e4 | 794 | |
037ce839 | 795 | ret = cpufreq_set_policy(policy, &new_policy); |
a8b149d3 RW |
796 | |
797 | if (new_policy.governor) | |
798 | module_put(new_policy.governor->owner); | |
799 | ||
88dc4384 | 800 | return ret ? ret : count; |
1da177e4 LT |
801 | } |
802 | ||
803 | /** | |
804 | * show_scaling_driver - show the cpufreq driver currently loaded | |
805 | */ | |
905d77cd | 806 | static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) |
1da177e4 | 807 | { |
1c3d85dd | 808 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); |
1da177e4 LT |
809 | } |
810 | ||
811 | /** | |
812 | * show_scaling_available_governors - show the available CPUfreq governors | |
813 | */ | |
905d77cd DJ |
814 | static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, |
815 | char *buf) | |
1da177e4 LT |
816 | { |
817 | ssize_t i = 0; | |
818 | struct cpufreq_governor *t; | |
819 | ||
9c0ebcf7 | 820 | if (!has_target()) { |
1da177e4 LT |
821 | i += sprintf(buf, "performance powersave"); |
822 | goto out; | |
823 | } | |
824 | ||
f7b27061 | 825 | for_each_governor(t) { |
29464f28 DJ |
826 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) |
827 | - (CPUFREQ_NAME_LEN + 2))) | |
1da177e4 | 828 | goto out; |
4b972f0b | 829 | i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); |
1da177e4 | 830 | } |
7d5e350f | 831 | out: |
1da177e4 LT |
832 | i += sprintf(&buf[i], "\n"); |
833 | return i; | |
834 | } | |
e8628dd0 | 835 | |
f4fd3797 | 836 | ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) |
1da177e4 LT |
837 | { |
838 | ssize_t i = 0; | |
839 | unsigned int cpu; | |
840 | ||
835481d9 | 841 | for_each_cpu(cpu, mask) { |
1da177e4 LT |
842 | if (i) |
843 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); | |
844 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); | |
845 | if (i >= (PAGE_SIZE - 5)) | |
29464f28 | 846 | break; |
1da177e4 LT |
847 | } |
848 | i += sprintf(&buf[i], "\n"); | |
849 | return i; | |
850 | } | |
f4fd3797 | 851 | EXPORT_SYMBOL_GPL(cpufreq_show_cpus); |
1da177e4 | 852 | |
e8628dd0 DW |
853 | /** |
854 | * show_related_cpus - show the CPUs affected by each transition even if | |
855 | * hw coordination is in use | |
856 | */ | |
857 | static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) | |
858 | { | |
f4fd3797 | 859 | return cpufreq_show_cpus(policy->related_cpus, buf); |
e8628dd0 DW |
860 | } |
861 | ||
862 | /** | |
863 | * show_affected_cpus - show the CPUs affected by each transition | |
864 | */ | |
865 | static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) | |
866 | { | |
f4fd3797 | 867 | return cpufreq_show_cpus(policy->cpus, buf); |
e8628dd0 DW |
868 | } |
869 | ||
9e76988e | 870 | static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, |
905d77cd | 871 | const char *buf, size_t count) |
9e76988e VP |
872 | { |
873 | unsigned int freq = 0; | |
874 | unsigned int ret; | |
875 | ||
879000f9 | 876 | if (!policy->governor || !policy->governor->store_setspeed) |
9e76988e VP |
877 | return -EINVAL; |
878 | ||
879 | ret = sscanf(buf, "%u", &freq); | |
880 | if (ret != 1) | |
881 | return -EINVAL; | |
882 | ||
883 | policy->governor->store_setspeed(policy, freq); | |
884 | ||
885 | return count; | |
886 | } | |
887 | ||
888 | static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) | |
889 | { | |
879000f9 | 890 | if (!policy->governor || !policy->governor->show_setspeed) |
9e76988e VP |
891 | return sprintf(buf, "<unsupported>\n"); |
892 | ||
893 | return policy->governor->show_setspeed(policy, buf); | |
894 | } | |
1da177e4 | 895 | |
e2f74f35 | 896 | /** |
8bf1ac72 | 897 | * show_bios_limit - show the current cpufreq HW/BIOS limitation |
e2f74f35 TR |
898 | */ |
899 | static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) | |
900 | { | |
901 | unsigned int limit; | |
902 | int ret; | |
b23aa311 YH |
903 | ret = cpufreq_driver->bios_limit(policy->cpu, &limit); |
904 | if (!ret) | |
905 | return sprintf(buf, "%u\n", limit); | |
e2f74f35 TR |
906 | return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); |
907 | } | |
908 | ||
6dad2a29 BP |
909 | cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); |
910 | cpufreq_freq_attr_ro(cpuinfo_min_freq); | |
911 | cpufreq_freq_attr_ro(cpuinfo_max_freq); | |
912 | cpufreq_freq_attr_ro(cpuinfo_transition_latency); | |
913 | cpufreq_freq_attr_ro(scaling_available_governors); | |
914 | cpufreq_freq_attr_ro(scaling_driver); | |
915 | cpufreq_freq_attr_ro(scaling_cur_freq); | |
916 | cpufreq_freq_attr_ro(bios_limit); | |
917 | cpufreq_freq_attr_ro(related_cpus); | |
918 | cpufreq_freq_attr_ro(affected_cpus); | |
919 | cpufreq_freq_attr_rw(scaling_min_freq); | |
920 | cpufreq_freq_attr_rw(scaling_max_freq); | |
921 | cpufreq_freq_attr_rw(scaling_governor); | |
922 | cpufreq_freq_attr_rw(scaling_setspeed); | |
1da177e4 | 923 | |
905d77cd | 924 | static struct attribute *default_attrs[] = { |
1da177e4 LT |
925 | &cpuinfo_min_freq.attr, |
926 | &cpuinfo_max_freq.attr, | |
ed129784 | 927 | &cpuinfo_transition_latency.attr, |
1da177e4 LT |
928 | &scaling_min_freq.attr, |
929 | &scaling_max_freq.attr, | |
930 | &affected_cpus.attr, | |
e8628dd0 | 931 | &related_cpus.attr, |
1da177e4 LT |
932 | &scaling_governor.attr, |
933 | &scaling_driver.attr, | |
934 | &scaling_available_governors.attr, | |
9e76988e | 935 | &scaling_setspeed.attr, |
1da177e4 LT |
936 | NULL |
937 | }; | |
938 | ||
29464f28 DJ |
939 | #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) |
940 | #define to_attr(a) container_of(a, struct freq_attr, attr) | |
1da177e4 | 941 | |
29464f28 | 942 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) |
1da177e4 | 943 | { |
905d77cd DJ |
944 | struct cpufreq_policy *policy = to_policy(kobj); |
945 | struct freq_attr *fattr = to_attr(attr); | |
1b750e3b | 946 | ssize_t ret; |
6eed9404 | 947 | |
ad7722da | 948 | down_read(&policy->rwsem); |
6541aef0 | 949 | ret = fattr->show(policy, buf); |
ad7722da | 950 | up_read(&policy->rwsem); |
1b750e3b | 951 | |
1da177e4 LT |
952 | return ret; |
953 | } | |
954 | ||
905d77cd DJ |
955 | static ssize_t store(struct kobject *kobj, struct attribute *attr, |
956 | const char *buf, size_t count) | |
1da177e4 | 957 | { |
905d77cd DJ |
958 | struct cpufreq_policy *policy = to_policy(kobj); |
959 | struct freq_attr *fattr = to_attr(attr); | |
a07530b4 | 960 | ssize_t ret = -EINVAL; |
6eed9404 | 961 | |
9b3d9bb3 WL |
962 | /* |
963 | * cpus_read_trylock() is used here to work around a circular lock | |
964 | * dependency problem with respect to the cpufreq_register_driver(). | |
965 | */ | |
966 | if (!cpus_read_trylock()) | |
967 | return -EBUSY; | |
4f750c93 | 968 | |
6541aef0 RW |
969 | if (cpu_online(policy->cpu)) { |
970 | down_write(&policy->rwsem); | |
e08f5f5b | 971 | ret = fattr->store(policy, buf, count); |
6541aef0 RW |
972 | up_write(&policy->rwsem); |
973 | } | |
e08f5f5b | 974 | |
a92551e4 | 975 | cpus_read_unlock(); |
4f750c93 | 976 | |
1da177e4 LT |
977 | return ret; |
978 | } | |
979 | ||
905d77cd | 980 | static void cpufreq_sysfs_release(struct kobject *kobj) |
1da177e4 | 981 | { |
905d77cd | 982 | struct cpufreq_policy *policy = to_policy(kobj); |
2d06d8c4 | 983 | pr_debug("last reference is dropped\n"); |
1da177e4 LT |
984 | complete(&policy->kobj_unregister); |
985 | } | |
986 | ||
52cf25d0 | 987 | static const struct sysfs_ops sysfs_ops = { |
1da177e4 LT |
988 | .show = show, |
989 | .store = store, | |
990 | }; | |
991 | ||
992 | static struct kobj_type ktype_cpufreq = { | |
993 | .sysfs_ops = &sysfs_ops, | |
994 | .default_attrs = default_attrs, | |
995 | .release = cpufreq_sysfs_release, | |
996 | }; | |
997 | ||
2f0ba790 | 998 | static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) |
87549141 | 999 | { |
2f0ba790 RW |
1000 | struct device *dev = get_cpu_device(cpu); |
1001 | ||
1002 | if (!dev) | |
1003 | return; | |
1004 | ||
1005 | if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) | |
1006 | return; | |
1007 | ||
26619804 | 1008 | dev_dbg(dev, "%s: Adding symlink\n", __func__); |
2f0ba790 RW |
1009 | if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) |
1010 | dev_err(dev, "cpufreq symlink creation failed\n"); | |
87549141 VK |
1011 | } |
1012 | ||
26619804 VK |
1013 | static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, |
1014 | struct device *dev) | |
87549141 | 1015 | { |
26619804 VK |
1016 | dev_dbg(dev, "%s: Removing symlink\n", __func__); |
1017 | sysfs_remove_link(&dev->kobj, "cpufreq"); | |
87549141 VK |
1018 | } |
1019 | ||
d9612a49 | 1020 | static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) |
909a694e DJ |
1021 | { |
1022 | struct freq_attr **drv_attr; | |
909a694e | 1023 | int ret = 0; |
909a694e | 1024 | |
909a694e | 1025 | /* set up files for this cpu device */ |
1c3d85dd | 1026 | drv_attr = cpufreq_driver->attr; |
f13f1184 | 1027 | while (drv_attr && *drv_attr) { |
909a694e DJ |
1028 | ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); |
1029 | if (ret) | |
6d4e81ed | 1030 | return ret; |
909a694e DJ |
1031 | drv_attr++; |
1032 | } | |
1c3d85dd | 1033 | if (cpufreq_driver->get) { |
909a694e DJ |
1034 | ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); |
1035 | if (ret) | |
6d4e81ed | 1036 | return ret; |
909a694e | 1037 | } |
c034b02e DB |
1038 | |
1039 | ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); | |
1040 | if (ret) | |
6d4e81ed | 1041 | return ret; |
c034b02e | 1042 | |
1c3d85dd | 1043 | if (cpufreq_driver->bios_limit) { |
e2f74f35 TR |
1044 | ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); |
1045 | if (ret) | |
6d4e81ed | 1046 | return ret; |
e2f74f35 | 1047 | } |
909a694e | 1048 | |
26619804 | 1049 | return 0; |
e18f1682 SB |
1050 | } |
1051 | ||
de1df26b RW |
1052 | __weak struct cpufreq_governor *cpufreq_default_governor(void) |
1053 | { | |
1054 | return NULL; | |
1055 | } | |
1056 | ||
7f0fa40f | 1057 | static int cpufreq_init_policy(struct cpufreq_policy *policy) |
e18f1682 | 1058 | { |
ab05d97a | 1059 | struct cpufreq_governor *gov = NULL, *def_gov = NULL; |
e18f1682 | 1060 | struct cpufreq_policy new_policy; |
e18f1682 | 1061 | |
d5b73cd8 | 1062 | memcpy(&new_policy, policy, sizeof(*policy)); |
a27a9ab7 | 1063 | |
ab05d97a YH |
1064 | def_gov = cpufreq_default_governor(); |
1065 | ||
1066 | if (has_target()) { | |
1067 | /* | |
1068 | * Update governor of new_policy to the governor used before | |
1069 | * hotplug | |
1070 | */ | |
1071 | gov = find_governor(policy->last_governor); | |
1072 | if (gov) { | |
1073 | pr_debug("Restoring governor %s for cpu %d\n", | |
6e2c89d1 | 1074 | policy->governor->name, policy->cpu); |
ab05d97a YH |
1075 | } else { |
1076 | if (!def_gov) | |
1077 | return -ENODATA; | |
1078 | gov = def_gov; | |
1079 | } | |
1080 | new_policy.governor = gov; | |
de1df26b | 1081 | } else { |
ab05d97a YH |
1082 | /* Use the default policy if there is no last_policy. */ |
1083 | if (policy->last_policy) { | |
69030dd1 | 1084 | new_policy.policy = policy->last_policy; |
ab05d97a YH |
1085 | } else { |
1086 | if (!def_gov) | |
1087 | return -ENODATA; | |
1088 | cpufreq_parse_policy(def_gov->name, &new_policy); | |
1089 | } | |
69030dd1 | 1090 | } |
ab05d97a | 1091 | |
7f0fa40f | 1092 | return cpufreq_set_policy(policy, &new_policy); |
909a694e DJ |
1093 | } |
1094 | ||
d9612a49 | 1095 | static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) |
fcf80582 | 1096 | { |
9c0ebcf7 | 1097 | int ret = 0; |
fcf80582 | 1098 | |
bb29ae15 VK |
1099 | /* Has this CPU been taken care of already? */ |
1100 | if (cpumask_test_cpu(cpu, policy->cpus)) | |
1101 | return 0; | |
1102 | ||
49f18560 | 1103 | down_write(&policy->rwsem); |
45482c70 RW |
1104 | if (has_target()) |
1105 | cpufreq_stop_governor(policy); | |
fcf80582 | 1106 | |
fcf80582 | 1107 | cpumask_set_cpu(cpu, policy->cpus); |
2eaa3e2d | 1108 | |
9c0ebcf7 | 1109 | if (has_target()) { |
0a300767 | 1110 | ret = cpufreq_start_governor(policy); |
49f18560 | 1111 | if (ret) |
3de9bdeb | 1112 | pr_err("%s: Failed to start governor\n", __func__); |
820c6ca2 | 1113 | } |
49f18560 VK |
1114 | up_write(&policy->rwsem); |
1115 | return ret; | |
fcf80582 | 1116 | } |
1da177e4 | 1117 | |
11eb69b9 VK |
1118 | static void handle_update(struct work_struct *work) |
1119 | { | |
1120 | struct cpufreq_policy *policy = | |
1121 | container_of(work, struct cpufreq_policy, update); | |
1122 | unsigned int cpu = policy->cpu; | |
1123 | pr_debug("handle_update for cpu %u called\n", cpu); | |
1124 | cpufreq_update_policy(cpu); | |
fcf80582 | 1125 | } |
1da177e4 | 1126 | |
a34e63b1 | 1127 | static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) |
e9698cc5 SB |
1128 | { |
1129 | struct cpufreq_policy *policy; | |
edd4a893 | 1130 | int ret; |
e9698cc5 SB |
1131 | |
1132 | policy = kzalloc(sizeof(*policy), GFP_KERNEL); | |
1133 | if (!policy) | |
1134 | return NULL; | |
1135 | ||
1136 | if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) | |
1137 | goto err_free_policy; | |
1138 | ||
1139 | if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) | |
1140 | goto err_free_cpumask; | |
1141 | ||
559ed407 RW |
1142 | if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) |
1143 | goto err_free_rcpumask; | |
1144 | ||
edd4a893 VK |
1145 | ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, |
1146 | cpufreq_global_kobject, "policy%u", cpu); | |
1147 | if (ret) { | |
1148 | pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); | |
2acb9bda RW |
1149 | /* |
1150 | * The entire policy object will be freed below, but the extra | |
1151 | * memory allocated for the kobject name needs to be freed by | |
1152 | * releasing the kobject. | |
1153 | */ | |
4ebe36c9 | 1154 | kobject_put(&policy->kobj); |
edd4a893 VK |
1155 | goto err_free_real_cpus; |
1156 | } | |
1157 | ||
c88a1f8b | 1158 | INIT_LIST_HEAD(&policy->policy_list); |
ad7722da | 1159 | init_rwsem(&policy->rwsem); |
12478cf0 SB |
1160 | spin_lock_init(&policy->transition_lock); |
1161 | init_waitqueue_head(&policy->transition_wait); | |
818c5712 VK |
1162 | init_completion(&policy->kobj_unregister); |
1163 | INIT_WORK(&policy->update, handle_update); | |
ad7722da | 1164 | |
a34e63b1 | 1165 | policy->cpu = cpu; |
e9698cc5 SB |
1166 | return policy; |
1167 | ||
edd4a893 VK |
1168 | err_free_real_cpus: |
1169 | free_cpumask_var(policy->real_cpus); | |
2fc3384d VK |
1170 | err_free_rcpumask: |
1171 | free_cpumask_var(policy->related_cpus); | |
e9698cc5 SB |
1172 | err_free_cpumask: |
1173 | free_cpumask_var(policy->cpus); | |
1174 | err_free_policy: | |
1175 | kfree(policy); | |
1176 | ||
1177 | return NULL; | |
1178 | } | |
1179 | ||
f9f41e3e | 1180 | static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) |
42f921a6 VK |
1181 | { |
1182 | struct kobject *kobj; | |
1183 | struct completion *cmp; | |
1184 | ||
87549141 | 1185 | down_write(&policy->rwsem); |
1aefc75b | 1186 | cpufreq_stats_free_table(policy); |
42f921a6 VK |
1187 | kobj = &policy->kobj; |
1188 | cmp = &policy->kobj_unregister; | |
87549141 | 1189 | up_write(&policy->rwsem); |
42f921a6 VK |
1190 | kobject_put(kobj); |
1191 | ||
1192 | /* | |
1193 | * We need to make sure that the underlying kobj is | |
1194 | * actually not referenced anymore by anybody before we | |
1195 | * proceed with unloading. | |
1196 | */ | |
1197 | pr_debug("waiting for dropping of refcount\n"); | |
1198 | wait_for_completion(cmp); | |
1199 | pr_debug("wait complete\n"); | |
1200 | } | |
1201 | ||
f9f41e3e | 1202 | static void cpufreq_policy_free(struct cpufreq_policy *policy) |
e9698cc5 | 1203 | { |
988bed09 VK |
1204 | unsigned long flags; |
1205 | int cpu; | |
1206 | ||
1207 | /* Remove policy from list */ | |
1208 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
1209 | list_del(&policy->policy_list); | |
1210 | ||
1211 | for_each_cpu(cpu, policy->related_cpus) | |
1212 | per_cpu(cpufreq_cpu_data, cpu) = NULL; | |
1213 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1214 | ||
f9f41e3e | 1215 | cpufreq_policy_put_kobj(policy); |
559ed407 | 1216 | free_cpumask_var(policy->real_cpus); |
e9698cc5 SB |
1217 | free_cpumask_var(policy->related_cpus); |
1218 | free_cpumask_var(policy->cpus); | |
1219 | kfree(policy); | |
1220 | } | |
1221 | ||
0b275352 | 1222 | static int cpufreq_online(unsigned int cpu) |
1da177e4 | 1223 | { |
7f0c020a | 1224 | struct cpufreq_policy *policy; |
194d99c7 | 1225 | bool new_policy; |
1da177e4 | 1226 | unsigned long flags; |
0b275352 RW |
1227 | unsigned int j; |
1228 | int ret; | |
87549141 | 1229 | |
0b275352 | 1230 | pr_debug("%s: bringing CPU%u online\n", __func__, cpu); |
6eed9404 | 1231 | |
bb29ae15 | 1232 | /* Check if this CPU already has a policy to manage it */ |
9104bb26 | 1233 | policy = per_cpu(cpufreq_cpu_data, cpu); |
11ce707e | 1234 | if (policy) { |
9104bb26 | 1235 | WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); |
11ce707e | 1236 | if (!policy_is_inactive(policy)) |
d9612a49 | 1237 | return cpufreq_add_policy_cpu(policy, cpu); |
1da177e4 | 1238 | |
11ce707e | 1239 | /* This is the only online CPU for the policy. Start over. */ |
194d99c7 | 1240 | new_policy = false; |
11ce707e RW |
1241 | down_write(&policy->rwsem); |
1242 | policy->cpu = cpu; | |
1243 | policy->governor = NULL; | |
1244 | up_write(&policy->rwsem); | |
1245 | } else { | |
194d99c7 | 1246 | new_policy = true; |
a34e63b1 | 1247 | policy = cpufreq_policy_alloc(cpu); |
72368d12 | 1248 | if (!policy) |
d4d854d6 | 1249 | return -ENOMEM; |
72368d12 | 1250 | } |
0d66b91e | 1251 | |
91a12e91 VK |
1252 | if (!new_policy && cpufreq_driver->online) { |
1253 | ret = cpufreq_driver->online(policy); | |
1254 | if (ret) { | |
1255 | pr_debug("%s: %d: initialization failed\n", __func__, | |
1256 | __LINE__); | |
1257 | goto out_exit_policy; | |
1258 | } | |
1da177e4 | 1259 | |
91a12e91 VK |
1260 | /* Recover policy->cpus using related_cpus */ |
1261 | cpumask_copy(policy->cpus, policy->related_cpus); | |
1262 | } else { | |
1263 | cpumask_copy(policy->cpus, cpumask_of(cpu)); | |
643ae6e8 | 1264 | |
91a12e91 VK |
1265 | /* |
1266 | * Call driver. From then on the cpufreq must be able | |
1267 | * to accept all calls to ->verify and ->setpolicy for this CPU. | |
1268 | */ | |
1269 | ret = cpufreq_driver->init(policy); | |
1270 | if (ret) { | |
1271 | pr_debug("%s: %d: initialization failed\n", __func__, | |
1272 | __LINE__); | |
1273 | goto out_free_policy; | |
1274 | } | |
d417e069 | 1275 | |
91a12e91 VK |
1276 | ret = cpufreq_table_validate_and_sort(policy); |
1277 | if (ret) | |
1278 | goto out_exit_policy; | |
6d4e81ed | 1279 | |
4d1f3a5b | 1280 | /* related_cpus should at least include policy->cpus. */ |
0998a03a | 1281 | cpumask_copy(policy->related_cpus, policy->cpus); |
4d1f3a5b | 1282 | } |
559ed407 | 1283 | |
91a12e91 | 1284 | down_write(&policy->rwsem); |
5a7e56a5 VK |
1285 | /* |
1286 | * affected cpus must always be the one, which are online. We aren't | |
1287 | * managing offline cpus here. | |
1288 | */ | |
1289 | cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); | |
1290 | ||
194d99c7 | 1291 | if (new_policy) { |
5a7e56a5 VK |
1292 | policy->user_policy.min = policy->min; |
1293 | policy->user_policy.max = policy->max; | |
6d4e81ed | 1294 | |
2f0ba790 | 1295 | for_each_cpu(j, policy->related_cpus) { |
988bed09 | 1296 | per_cpu(cpufreq_cpu_data, j) = policy; |
2f0ba790 RW |
1297 | add_cpu_dev_symlink(policy, j); |
1298 | } | |
ff010472 VK |
1299 | } else { |
1300 | policy->min = policy->user_policy.min; | |
1301 | policy->max = policy->user_policy.max; | |
988bed09 | 1302 | } |
652ed95d | 1303 | |
5ddc6d4e | 1304 | if (cpufreq_driver->get && has_target()) { |
da60ce9f VK |
1305 | policy->cur = cpufreq_driver->get(policy->cpu); |
1306 | if (!policy->cur) { | |
1307 | pr_err("%s: ->get() failed\n", __func__); | |
d417e069 | 1308 | goto out_destroy_policy; |
da60ce9f VK |
1309 | } |
1310 | } | |
1311 | ||
d3916691 VK |
1312 | /* |
1313 | * Sometimes boot loaders set CPU frequency to a value outside of | |
1314 | * frequency table present with cpufreq core. In such cases CPU might be | |
1315 | * unstable if it has to run on that frequency for long duration of time | |
1316 | * and so its better to set it to a frequency which is specified in | |
1317 | * freq-table. This also makes cpufreq stats inconsistent as | |
1318 | * cpufreq-stats would fail to register because current frequency of CPU | |
1319 | * isn't found in freq-table. | |
1320 | * | |
1321 | * Because we don't want this change to effect boot process badly, we go | |
1322 | * for the next freq which is >= policy->cur ('cur' must be set by now, | |
1323 | * otherwise we will end up setting freq to lowest of the table as 'cur' | |
1324 | * is initialized to zero). | |
1325 | * | |
1326 | * We are passing target-freq as "policy->cur - 1" otherwise | |
1327 | * __cpufreq_driver_target() would simply fail, as policy->cur will be | |
1328 | * equal to target-freq. | |
1329 | */ | |
1330 | if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) | |
1331 | && has_target()) { | |
1332 | /* Are we running at unknown frequency ? */ | |
1333 | ret = cpufreq_frequency_table_get_index(policy, policy->cur); | |
1334 | if (ret == -EINVAL) { | |
1335 | /* Warn user and fix it */ | |
1336 | pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", | |
1337 | __func__, policy->cpu, policy->cur); | |
1338 | ret = __cpufreq_driver_target(policy, policy->cur - 1, | |
1339 | CPUFREQ_RELATION_L); | |
1340 | ||
1341 | /* | |
1342 | * Reaching here after boot in a few seconds may not | |
1343 | * mean that system will remain stable at "unknown" | |
1344 | * frequency for longer duration. Hence, a BUG_ON(). | |
1345 | */ | |
1346 | BUG_ON(ret); | |
1347 | pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", | |
1348 | __func__, policy->cpu, policy->cur); | |
1349 | } | |
1350 | } | |
1351 | ||
194d99c7 | 1352 | if (new_policy) { |
d9612a49 | 1353 | ret = cpufreq_add_dev_interface(policy); |
a82fab29 | 1354 | if (ret) |
d417e069 | 1355 | goto out_destroy_policy; |
1aefc75b RW |
1356 | |
1357 | cpufreq_stats_create_table(policy); | |
8ff69732 | 1358 | |
988bed09 VK |
1359 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1360 | list_add(&policy->policy_list, &cpufreq_policy_list); | |
1361 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1362 | } | |
9515f4d6 | 1363 | |
7f0fa40f VK |
1364 | ret = cpufreq_init_policy(policy); |
1365 | if (ret) { | |
1366 | pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", | |
1367 | __func__, cpu, ret); | |
d417e069 | 1368 | goto out_destroy_policy; |
08fd8c1c | 1369 | } |
e18f1682 | 1370 | |
4e97b631 | 1371 | up_write(&policy->rwsem); |
08fd8c1c | 1372 | |
038c5b3e | 1373 | kobject_uevent(&policy->kobj, KOBJ_ADD); |
7c45cf31 | 1374 | |
7c45cf31 VK |
1375 | /* Callback for handling stuff after policy is ready */ |
1376 | if (cpufreq_driver->ready) | |
1377 | cpufreq_driver->ready(policy); | |
1378 | ||
bcc61569 | 1379 | if (cpufreq_thermal_control_enabled(cpufreq_driver)) |
5c238a8b AK |
1380 | policy->cdev = of_cpufreq_cooling_register(policy); |
1381 | ||
2d06d8c4 | 1382 | pr_debug("initialization complete\n"); |
87c32271 | 1383 | |
1da177e4 LT |
1384 | return 0; |
1385 | ||
d417e069 | 1386 | out_destroy_policy: |
b24b6478 VK |
1387 | for_each_cpu(j, policy->real_cpus) |
1388 | remove_cpu_dev_symlink(policy, get_cpu_device(j)); | |
1389 | ||
7106e02b PB |
1390 | up_write(&policy->rwsem); |
1391 | ||
d417e069 | 1392 | out_exit_policy: |
da60ce9f VK |
1393 | if (cpufreq_driver->exit) |
1394 | cpufreq_driver->exit(policy); | |
2f0ba790 | 1395 | |
8101f997 | 1396 | out_free_policy: |
f9f41e3e | 1397 | cpufreq_policy_free(policy); |
1da177e4 LT |
1398 | return ret; |
1399 | } | |
1400 | ||
0b275352 RW |
1401 | /** |
1402 | * cpufreq_add_dev - the cpufreq interface for a CPU device. | |
1403 | * @dev: CPU device. | |
1404 | * @sif: Subsystem interface structure pointer (not used) | |
1405 | */ | |
1406 | static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) | |
1407 | { | |
a794d613 | 1408 | struct cpufreq_policy *policy; |
0b275352 | 1409 | unsigned cpu = dev->id; |
26619804 | 1410 | int ret; |
0b275352 RW |
1411 | |
1412 | dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); | |
1413 | ||
26619804 VK |
1414 | if (cpu_online(cpu)) { |
1415 | ret = cpufreq_online(cpu); | |
1416 | if (ret) | |
1417 | return ret; | |
1418 | } | |
0b275352 | 1419 | |
26619804 | 1420 | /* Create sysfs link on CPU registration */ |
a794d613 | 1421 | policy = per_cpu(cpufreq_cpu_data, cpu); |
2f0ba790 RW |
1422 | if (policy) |
1423 | add_cpu_dev_symlink(policy, cpu); | |
26619804 | 1424 | |
2f0ba790 | 1425 | return 0; |
1da177e4 LT |
1426 | } |
1427 | ||
27622b06 | 1428 | static int cpufreq_offline(unsigned int cpu) |
1da177e4 | 1429 | { |
3a3e9e06 | 1430 | struct cpufreq_policy *policy; |
69cee714 | 1431 | int ret; |
1da177e4 | 1432 | |
b8eed8af | 1433 | pr_debug("%s: unregistering CPU %u\n", __func__, cpu); |
1da177e4 | 1434 | |
988bed09 | 1435 | policy = cpufreq_cpu_get_raw(cpu); |
3a3e9e06 | 1436 | if (!policy) { |
b8eed8af | 1437 | pr_debug("%s: No cpu_data found\n", __func__); |
27622b06 | 1438 | return 0; |
1da177e4 | 1439 | } |
1da177e4 | 1440 | |
49f18560 | 1441 | down_write(&policy->rwsem); |
45482c70 RW |
1442 | if (has_target()) |
1443 | cpufreq_stop_governor(policy); | |
1da177e4 | 1444 | |
9591becb | 1445 | cpumask_clear_cpu(cpu, policy->cpus); |
4573237b | 1446 | |
9591becb VK |
1447 | if (policy_is_inactive(policy)) { |
1448 | if (has_target()) | |
1449 | strncpy(policy->last_governor, policy->governor->name, | |
1450 | CPUFREQ_NAME_LEN); | |
69030dd1 SP |
1451 | else |
1452 | policy->last_policy = policy->policy; | |
9591becb VK |
1453 | } else if (cpu == policy->cpu) { |
1454 | /* Nominate new CPU */ | |
1455 | policy->cpu = cpumask_any(policy->cpus); | |
1456 | } | |
084f3493 | 1457 | |
9591becb VK |
1458 | /* Start governor again for active policy */ |
1459 | if (!policy_is_inactive(policy)) { | |
1460 | if (has_target()) { | |
0a300767 | 1461 | ret = cpufreq_start_governor(policy); |
9591becb VK |
1462 | if (ret) |
1463 | pr_err("%s: Failed to start governor\n", __func__); | |
1464 | } | |
cedb70af | 1465 | |
49f18560 | 1466 | goto unlock; |
cedb70af SB |
1467 | } |
1468 | ||
bcc61569 | 1469 | if (cpufreq_thermal_control_enabled(cpufreq_driver)) { |
5c238a8b AK |
1470 | cpufreq_cooling_unregister(policy->cdev); |
1471 | policy->cdev = NULL; | |
1472 | } | |
1473 | ||
69cee714 VK |
1474 | if (cpufreq_driver->stop_cpu) |
1475 | cpufreq_driver->stop_cpu(policy); | |
87549141 | 1476 | |
36be3418 RW |
1477 | if (has_target()) |
1478 | cpufreq_exit_governor(policy); | |
1da177e4 | 1479 | |
87549141 | 1480 | /* |
91a12e91 VK |
1481 | * Perform the ->offline() during light-weight tear-down, as |
1482 | * that allows fast recovery when the CPU comes back. | |
87549141 | 1483 | */ |
91a12e91 VK |
1484 | if (cpufreq_driver->offline) { |
1485 | cpufreq_driver->offline(policy); | |
1486 | } else if (cpufreq_driver->exit) { | |
87549141 | 1487 | cpufreq_driver->exit(policy); |
55582bcc SP |
1488 | policy->freq_table = NULL; |
1489 | } | |
49f18560 VK |
1490 | |
1491 | unlock: | |
1492 | up_write(&policy->rwsem); | |
27622b06 | 1493 | return 0; |
1da177e4 LT |
1494 | } |
1495 | ||
cedb70af | 1496 | /** |
27a862e9 | 1497 | * cpufreq_remove_dev - remove a CPU device |
cedb70af SB |
1498 | * |
1499 | * Removes the cpufreq interface for a CPU device. | |
cedb70af | 1500 | */ |
71db87ba | 1501 | static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) |
5a01f2e8 | 1502 | { |
8a25a2fd | 1503 | unsigned int cpu = dev->id; |
559ed407 | 1504 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
87549141 | 1505 | |
559ed407 | 1506 | if (!policy) |
1af115d6 | 1507 | return; |
87549141 | 1508 | |
69cee714 VK |
1509 | if (cpu_online(cpu)) |
1510 | cpufreq_offline(cpu); | |
87549141 | 1511 | |
559ed407 | 1512 | cpumask_clear_cpu(cpu, policy->real_cpus); |
26619804 | 1513 | remove_cpu_dev_symlink(policy, dev); |
87549141 | 1514 | |
91a12e91 VK |
1515 | if (cpumask_empty(policy->real_cpus)) { |
1516 | /* We did light-weight exit earlier, do full tear down now */ | |
1517 | if (cpufreq_driver->offline) | |
1518 | cpufreq_driver->exit(policy); | |
1519 | ||
f9f41e3e | 1520 | cpufreq_policy_free(policy); |
91a12e91 | 1521 | } |
5a01f2e8 VP |
1522 | } |
1523 | ||
1da177e4 | 1524 | /** |
bb176f7d VK |
1525 | * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're |
1526 | * in deep trouble. | |
a1e1dc41 | 1527 | * @policy: policy managing CPUs |
1da177e4 LT |
1528 | * @new_freq: CPU frequency the CPU actually runs at |
1529 | * | |
29464f28 DJ |
1530 | * We adjust to current frequency first, and need to clean up later. |
1531 | * So either call to cpufreq_update_policy() or schedule handle_update()). | |
1da177e4 | 1532 | */ |
a1e1dc41 | 1533 | static void cpufreq_out_of_sync(struct cpufreq_policy *policy, |
e08f5f5b | 1534 | unsigned int new_freq) |
1da177e4 LT |
1535 | { |
1536 | struct cpufreq_freqs freqs; | |
b43a7ffb | 1537 | |
e837f9b5 | 1538 | pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", |
a1e1dc41 | 1539 | policy->cur, new_freq); |
1da177e4 | 1540 | |
a1e1dc41 | 1541 | freqs.old = policy->cur; |
1da177e4 | 1542 | freqs.new = new_freq; |
b43a7ffb | 1543 | |
8fec051e VK |
1544 | cpufreq_freq_transition_begin(policy, &freqs); |
1545 | cpufreq_freq_transition_end(policy, &freqs, 0); | |
1da177e4 LT |
1546 | } |
1547 | ||
32ee8c3e | 1548 | /** |
4ab70df4 | 1549 | * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur |
95235ca2 VP |
1550 | * @cpu: CPU number |
1551 | * | |
1552 | * This is the last known freq, without actually getting it from the driver. | |
1553 | * Return value will be same as what is shown in scaling_cur_freq in sysfs. | |
1554 | */ | |
1555 | unsigned int cpufreq_quick_get(unsigned int cpu) | |
1556 | { | |
9e21ba8b | 1557 | struct cpufreq_policy *policy; |
e08f5f5b | 1558 | unsigned int ret_freq = 0; |
c75361c0 | 1559 | unsigned long flags; |
95235ca2 | 1560 | |
c75361c0 RC |
1561 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
1562 | ||
1563 | if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { | |
1564 | ret_freq = cpufreq_driver->get(cpu); | |
1565 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
1566 | return ret_freq; | |
1567 | } | |
1568 | ||
1569 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
9e21ba8b DB |
1570 | |
1571 | policy = cpufreq_cpu_get(cpu); | |
95235ca2 | 1572 | if (policy) { |
e08f5f5b | 1573 | ret_freq = policy->cur; |
95235ca2 VP |
1574 | cpufreq_cpu_put(policy); |
1575 | } | |
1576 | ||
4d34a67d | 1577 | return ret_freq; |
95235ca2 VP |
1578 | } |
1579 | EXPORT_SYMBOL(cpufreq_quick_get); | |
1580 | ||
3d737108 JB |
1581 | /** |
1582 | * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU | |
1583 | * @cpu: CPU number | |
1584 | * | |
1585 | * Just return the max possible frequency for a given CPU. | |
1586 | */ | |
1587 | unsigned int cpufreq_quick_get_max(unsigned int cpu) | |
1588 | { | |
1589 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); | |
1590 | unsigned int ret_freq = 0; | |
1591 | ||
1592 | if (policy) { | |
1593 | ret_freq = policy->max; | |
1594 | cpufreq_cpu_put(policy); | |
1595 | } | |
1596 | ||
1597 | return ret_freq; | |
1598 | } | |
1599 | EXPORT_SYMBOL(cpufreq_quick_get_max); | |
1600 | ||
d92d50a4 | 1601 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy) |
1da177e4 | 1602 | { |
e08f5f5b | 1603 | unsigned int ret_freq = 0; |
5800043b | 1604 | |
4db7c34c | 1605 | if (unlikely(policy_is_inactive(policy))) |
4d34a67d | 1606 | return ret_freq; |
1da177e4 | 1607 | |
d92d50a4 | 1608 | ret_freq = cpufreq_driver->get(policy->cpu); |
1da177e4 | 1609 | |
b7898fda | 1610 | /* |
2f661962 | 1611 | * If fast frequency switching is used with the given policy, the check |
b7898fda RW |
1612 | * against policy->cur is pointless, so skip it in that case too. |
1613 | */ | |
2f661962 | 1614 | if (policy->fast_switch_enabled) |
11e584cf VK |
1615 | return ret_freq; |
1616 | ||
98015228 | 1617 | if (has_target() && ret_freq && policy->cur) { |
e08f5f5b GS |
1618 | /* verify no discrepancy between actual and |
1619 | saved value exists */ | |
1620 | if (unlikely(ret_freq != policy->cur)) { | |
a1e1dc41 | 1621 | cpufreq_out_of_sync(policy, ret_freq); |
1da177e4 LT |
1622 | schedule_work(&policy->update); |
1623 | } | |
1624 | } | |
1625 | ||
4d34a67d | 1626 | return ret_freq; |
5a01f2e8 | 1627 | } |
1da177e4 | 1628 | |
5a01f2e8 VP |
1629 | /** |
1630 | * cpufreq_get - get the current CPU frequency (in kHz) | |
1631 | * @cpu: CPU number | |
1632 | * | |
1633 | * Get the CPU current (static) CPU frequency | |
1634 | */ | |
1635 | unsigned int cpufreq_get(unsigned int cpu) | |
1636 | { | |
999976e0 | 1637 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
5a01f2e8 | 1638 | unsigned int ret_freq = 0; |
5a01f2e8 | 1639 | |
999976e0 AP |
1640 | if (policy) { |
1641 | down_read(&policy->rwsem); | |
4db7c34c YH |
1642 | if (cpufreq_driver->get) |
1643 | ret_freq = __cpufreq_get(policy); | |
999976e0 | 1644 | up_read(&policy->rwsem); |
5a01f2e8 | 1645 | |
999976e0 AP |
1646 | cpufreq_cpu_put(policy); |
1647 | } | |
6eed9404 | 1648 | |
4d34a67d | 1649 | return ret_freq; |
1da177e4 LT |
1650 | } |
1651 | EXPORT_SYMBOL(cpufreq_get); | |
1652 | ||
999f5729 RW |
1653 | static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy) |
1654 | { | |
1655 | unsigned int new_freq; | |
1656 | ||
1657 | new_freq = cpufreq_driver->get(policy->cpu); | |
1658 | if (!new_freq) | |
1659 | return 0; | |
1660 | ||
1661 | if (!policy->cur) { | |
1662 | pr_debug("cpufreq: Driver did not initialize current freq\n"); | |
1663 | policy->cur = new_freq; | |
1664 | } else if (policy->cur != new_freq && has_target()) { | |
1665 | cpufreq_out_of_sync(policy, new_freq); | |
1666 | } | |
1667 | ||
1668 | return new_freq; | |
1669 | } | |
1670 | ||
8a25a2fd KS |
1671 | static struct subsys_interface cpufreq_interface = { |
1672 | .name = "cpufreq", | |
1673 | .subsys = &cpu_subsys, | |
1674 | .add_dev = cpufreq_add_dev, | |
1675 | .remove_dev = cpufreq_remove_dev, | |
e00e56df RW |
1676 | }; |
1677 | ||
e28867ea VK |
1678 | /* |
1679 | * In case platform wants some specific frequency to be configured | |
1680 | * during suspend.. | |
1681 | */ | |
1682 | int cpufreq_generic_suspend(struct cpufreq_policy *policy) | |
1683 | { | |
1684 | int ret; | |
1685 | ||
1686 | if (!policy->suspend_freq) { | |
201f3716 BZ |
1687 | pr_debug("%s: suspend_freq not defined\n", __func__); |
1688 | return 0; | |
e28867ea VK |
1689 | } |
1690 | ||
1691 | pr_debug("%s: Setting suspend-freq: %u\n", __func__, | |
1692 | policy->suspend_freq); | |
1693 | ||
1694 | ret = __cpufreq_driver_target(policy, policy->suspend_freq, | |
1695 | CPUFREQ_RELATION_H); | |
1696 | if (ret) | |
1697 | pr_err("%s: unable to set suspend-freq: %u. err: %d\n", | |
1698 | __func__, policy->suspend_freq, ret); | |
1699 | ||
1700 | return ret; | |
1701 | } | |
1702 | EXPORT_SYMBOL(cpufreq_generic_suspend); | |
1703 | ||
42d4dc3f | 1704 | /** |
2f0aea93 | 1705 | * cpufreq_suspend() - Suspend CPUFreq governors |
e00e56df | 1706 | * |
2f0aea93 VK |
1707 | * Called during system wide Suspend/Hibernate cycles for suspending governors |
1708 | * as some platforms can't change frequency after this point in suspend cycle. | |
1709 | * Because some of the devices (like: i2c, regulators, etc) they use for | |
1710 | * changing frequency are suspended quickly after this point. | |
42d4dc3f | 1711 | */ |
2f0aea93 | 1712 | void cpufreq_suspend(void) |
42d4dc3f | 1713 | { |
3a3e9e06 | 1714 | struct cpufreq_policy *policy; |
42d4dc3f | 1715 | |
2f0aea93 VK |
1716 | if (!cpufreq_driver) |
1717 | return; | |
42d4dc3f | 1718 | |
ba41e1bc | 1719 | if (!has_target() && !cpufreq_driver->suspend) |
b1b12bab | 1720 | goto suspend; |
42d4dc3f | 1721 | |
2f0aea93 VK |
1722 | pr_debug("%s: Suspending Governors\n", __func__); |
1723 | ||
f963735a | 1724 | for_each_active_policy(policy) { |
ba41e1bc RW |
1725 | if (has_target()) { |
1726 | down_write(&policy->rwsem); | |
45482c70 | 1727 | cpufreq_stop_governor(policy); |
ba41e1bc | 1728 | up_write(&policy->rwsem); |
ba41e1bc RW |
1729 | } |
1730 | ||
1731 | if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) | |
2f0aea93 VK |
1732 | pr_err("%s: Failed to suspend driver: %p\n", __func__, |
1733 | policy); | |
42d4dc3f | 1734 | } |
b1b12bab VK |
1735 | |
1736 | suspend: | |
1737 | cpufreq_suspended = true; | |
42d4dc3f BH |
1738 | } |
1739 | ||
1da177e4 | 1740 | /** |
2f0aea93 | 1741 | * cpufreq_resume() - Resume CPUFreq governors |
1da177e4 | 1742 | * |
2f0aea93 VK |
1743 | * Called during system wide Suspend/Hibernate cycle for resuming governors that |
1744 | * are suspended with cpufreq_suspend(). | |
1da177e4 | 1745 | */ |
2f0aea93 | 1746 | void cpufreq_resume(void) |
1da177e4 | 1747 | { |
3a3e9e06 | 1748 | struct cpufreq_policy *policy; |
49f18560 | 1749 | int ret; |
1da177e4 | 1750 | |
2f0aea93 | 1751 | if (!cpufreq_driver) |
703cbaa6 BY |
1752 | return; |
1753 | ||
1754 | if (unlikely(!cpufreq_suspended)) | |
2f0aea93 | 1755 | return; |
1da177e4 | 1756 | |
8e30444e LT |
1757 | cpufreq_suspended = false; |
1758 | ||
ba41e1bc | 1759 | if (!has_target() && !cpufreq_driver->resume) |
e00e56df | 1760 | return; |
1da177e4 | 1761 | |
2f0aea93 | 1762 | pr_debug("%s: Resuming Governors\n", __func__); |
1da177e4 | 1763 | |
f963735a | 1764 | for_each_active_policy(policy) { |
49f18560 | 1765 | if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { |
0c5aa405 VK |
1766 | pr_err("%s: Failed to resume driver: %p\n", __func__, |
1767 | policy); | |
ba41e1bc | 1768 | } else if (has_target()) { |
49f18560 | 1769 | down_write(&policy->rwsem); |
0a300767 | 1770 | ret = cpufreq_start_governor(policy); |
49f18560 VK |
1771 | up_write(&policy->rwsem); |
1772 | ||
1773 | if (ret) | |
1774 | pr_err("%s: Failed to start governor for policy: %p\n", | |
1775 | __func__, policy); | |
1776 | } | |
2f0aea93 VK |
1777 | } |
1778 | } | |
1da177e4 | 1779 | |
9d95046e BP |
1780 | /** |
1781 | * cpufreq_get_current_driver - return current driver's name | |
1782 | * | |
1783 | * Return the name string of the currently loaded cpufreq driver | |
1784 | * or NULL, if none. | |
1785 | */ | |
1786 | const char *cpufreq_get_current_driver(void) | |
1787 | { | |
1c3d85dd RW |
1788 | if (cpufreq_driver) |
1789 | return cpufreq_driver->name; | |
1790 | ||
1791 | return NULL; | |
9d95046e BP |
1792 | } |
1793 | EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); | |
1da177e4 | 1794 | |
51315cdf TP |
1795 | /** |
1796 | * cpufreq_get_driver_data - return current driver data | |
1797 | * | |
1798 | * Return the private data of the currently loaded cpufreq | |
1799 | * driver, or NULL if no cpufreq driver is loaded. | |
1800 | */ | |
1801 | void *cpufreq_get_driver_data(void) | |
1802 | { | |
1803 | if (cpufreq_driver) | |
1804 | return cpufreq_driver->driver_data; | |
1805 | ||
1806 | return NULL; | |
1807 | } | |
1808 | EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); | |
1809 | ||
1da177e4 LT |
1810 | /********************************************************************* |
1811 | * NOTIFIER LISTS INTERFACE * | |
1812 | *********************************************************************/ | |
1813 | ||
1814 | /** | |
1815 | * cpufreq_register_notifier - register a driver with cpufreq | |
1816 | * @nb: notifier function to register | |
1817 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER | |
1818 | * | |
32ee8c3e | 1819 | * Add a driver to one of two lists: either a list of drivers that |
1da177e4 LT |
1820 | * are notified about clock rate changes (once before and once after |
1821 | * the transition), or a list of drivers that are notified about | |
1822 | * changes in cpufreq policy. | |
1823 | * | |
1824 | * This function may sleep, and has the same return conditions as | |
e041c683 | 1825 | * blocking_notifier_chain_register. |
1da177e4 LT |
1826 | */ |
1827 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) | |
1828 | { | |
1829 | int ret; | |
1830 | ||
d5aaffa9 DB |
1831 | if (cpufreq_disabled()) |
1832 | return -EINVAL; | |
1833 | ||
1da177e4 LT |
1834 | switch (list) { |
1835 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b7898fda RW |
1836 | mutex_lock(&cpufreq_fast_switch_lock); |
1837 | ||
1838 | if (cpufreq_fast_switch_count > 0) { | |
1839 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1840 | return -EBUSY; | |
1841 | } | |
b4dfdbb3 | 1842 | ret = srcu_notifier_chain_register( |
e041c683 | 1843 | &cpufreq_transition_notifier_list, nb); |
b7898fda RW |
1844 | if (!ret) |
1845 | cpufreq_fast_switch_count--; | |
1846 | ||
1847 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1da177e4 LT |
1848 | break; |
1849 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
1850 | ret = blocking_notifier_chain_register( |
1851 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
1852 | break; |
1853 | default: | |
1854 | ret = -EINVAL; | |
1855 | } | |
1da177e4 LT |
1856 | |
1857 | return ret; | |
1858 | } | |
1859 | EXPORT_SYMBOL(cpufreq_register_notifier); | |
1860 | ||
1da177e4 LT |
1861 | /** |
1862 | * cpufreq_unregister_notifier - unregister a driver with cpufreq | |
1863 | * @nb: notifier block to be unregistered | |
bb176f7d | 1864 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
1da177e4 LT |
1865 | * |
1866 | * Remove a driver from the CPU frequency notifier list. | |
1867 | * | |
1868 | * This function may sleep, and has the same return conditions as | |
e041c683 | 1869 | * blocking_notifier_chain_unregister. |
1da177e4 LT |
1870 | */ |
1871 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) | |
1872 | { | |
1873 | int ret; | |
1874 | ||
d5aaffa9 DB |
1875 | if (cpufreq_disabled()) |
1876 | return -EINVAL; | |
1877 | ||
1da177e4 LT |
1878 | switch (list) { |
1879 | case CPUFREQ_TRANSITION_NOTIFIER: | |
b7898fda RW |
1880 | mutex_lock(&cpufreq_fast_switch_lock); |
1881 | ||
b4dfdbb3 | 1882 | ret = srcu_notifier_chain_unregister( |
e041c683 | 1883 | &cpufreq_transition_notifier_list, nb); |
b7898fda RW |
1884 | if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) |
1885 | cpufreq_fast_switch_count++; | |
1886 | ||
1887 | mutex_unlock(&cpufreq_fast_switch_lock); | |
1da177e4 LT |
1888 | break; |
1889 | case CPUFREQ_POLICY_NOTIFIER: | |
e041c683 AS |
1890 | ret = blocking_notifier_chain_unregister( |
1891 | &cpufreq_policy_notifier_list, nb); | |
1da177e4 LT |
1892 | break; |
1893 | default: | |
1894 | ret = -EINVAL; | |
1895 | } | |
1da177e4 LT |
1896 | |
1897 | return ret; | |
1898 | } | |
1899 | EXPORT_SYMBOL(cpufreq_unregister_notifier); | |
1900 | ||
1901 | ||
1902 | /********************************************************************* | |
1903 | * GOVERNORS * | |
1904 | *********************************************************************/ | |
1905 | ||
b7898fda RW |
1906 | /** |
1907 | * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. | |
1908 | * @policy: cpufreq policy to switch the frequency for. | |
1909 | * @target_freq: New frequency to set (may be approximate). | |
1910 | * | |
1911 | * Carry out a fast frequency switch without sleeping. | |
1912 | * | |
1913 | * The driver's ->fast_switch() callback invoked by this function must be | |
1914 | * suitable for being called from within RCU-sched read-side critical sections | |
1915 | * and it is expected to select the minimum available frequency greater than or | |
1916 | * equal to @target_freq (CPUFREQ_RELATION_L). | |
1917 | * | |
1918 | * This function must not be called if policy->fast_switch_enabled is unset. | |
1919 | * | |
1920 | * Governors calling this function must guarantee that it will never be invoked | |
1921 | * twice in parallel for the same policy and that it will never be called in | |
1922 | * parallel with either ->target() or ->target_index() for the same policy. | |
1923 | * | |
209887e6 VK |
1924 | * Returns the actual frequency set for the CPU. |
1925 | * | |
1926 | * If 0 is returned by the driver's ->fast_switch() callback to indicate an | |
1927 | * error condition, the hardware configuration must be preserved. | |
b7898fda RW |
1928 | */ |
1929 | unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, | |
1930 | unsigned int target_freq) | |
1931 | { | |
b9af6948 | 1932 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
b7898fda RW |
1933 | |
1934 | return cpufreq_driver->fast_switch(policy, target_freq); | |
1935 | } | |
1936 | EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); | |
1937 | ||
1c03a2d0 VK |
1938 | /* Must set freqs->new to intermediate frequency */ |
1939 | static int __target_intermediate(struct cpufreq_policy *policy, | |
1940 | struct cpufreq_freqs *freqs, int index) | |
1941 | { | |
1942 | int ret; | |
1943 | ||
1944 | freqs->new = cpufreq_driver->get_intermediate(policy, index); | |
1945 | ||
1946 | /* We don't need to switch to intermediate freq */ | |
1947 | if (!freqs->new) | |
1948 | return 0; | |
1949 | ||
1950 | pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", | |
1951 | __func__, policy->cpu, freqs->old, freqs->new); | |
1952 | ||
1953 | cpufreq_freq_transition_begin(policy, freqs); | |
1954 | ret = cpufreq_driver->target_intermediate(policy, index); | |
1955 | cpufreq_freq_transition_end(policy, freqs, ret); | |
1956 | ||
1957 | if (ret) | |
1958 | pr_err("%s: Failed to change to intermediate frequency: %d\n", | |
1959 | __func__, ret); | |
1960 | ||
1961 | return ret; | |
1962 | } | |
1963 | ||
23727845 | 1964 | static int __target_index(struct cpufreq_policy *policy, int index) |
8d65775d | 1965 | { |
1c03a2d0 VK |
1966 | struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; |
1967 | unsigned int intermediate_freq = 0; | |
23727845 | 1968 | unsigned int newfreq = policy->freq_table[index].frequency; |
8d65775d VK |
1969 | int retval = -EINVAL; |
1970 | bool notify; | |
1971 | ||
23727845 VK |
1972 | if (newfreq == policy->cur) |
1973 | return 0; | |
1974 | ||
8d65775d | 1975 | notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); |
8d65775d | 1976 | if (notify) { |
1c03a2d0 VK |
1977 | /* Handle switching to intermediate frequency */ |
1978 | if (cpufreq_driver->get_intermediate) { | |
1979 | retval = __target_intermediate(policy, &freqs, index); | |
1980 | if (retval) | |
1981 | return retval; | |
1982 | ||
1983 | intermediate_freq = freqs.new; | |
1984 | /* Set old freq to intermediate */ | |
1985 | if (intermediate_freq) | |
1986 | freqs.old = freqs.new; | |
1987 | } | |
8d65775d | 1988 | |
23727845 | 1989 | freqs.new = newfreq; |
8d65775d VK |
1990 | pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", |
1991 | __func__, policy->cpu, freqs.old, freqs.new); | |
1992 | ||
1993 | cpufreq_freq_transition_begin(policy, &freqs); | |
1994 | } | |
1995 | ||
1996 | retval = cpufreq_driver->target_index(policy, index); | |
1997 | if (retval) | |
1998 | pr_err("%s: Failed to change cpu frequency: %d\n", __func__, | |
1999 | retval); | |
2000 | ||
1c03a2d0 | 2001 | if (notify) { |
8d65775d VK |
2002 | cpufreq_freq_transition_end(policy, &freqs, retval); |
2003 | ||
1c03a2d0 VK |
2004 | /* |
2005 | * Failed after setting to intermediate freq? Driver should have | |
2006 | * reverted back to initial frequency and so should we. Check | |
2007 | * here for intermediate_freq instead of get_intermediate, in | |
58405af6 | 2008 | * case we haven't switched to intermediate freq at all. |
1c03a2d0 VK |
2009 | */ |
2010 | if (unlikely(retval && intermediate_freq)) { | |
2011 | freqs.old = intermediate_freq; | |
2012 | freqs.new = policy->restore_freq; | |
2013 | cpufreq_freq_transition_begin(policy, &freqs); | |
2014 | cpufreq_freq_transition_end(policy, &freqs, 0); | |
2015 | } | |
2016 | } | |
2017 | ||
8d65775d VK |
2018 | return retval; |
2019 | } | |
2020 | ||
1da177e4 LT |
2021 | int __cpufreq_driver_target(struct cpufreq_policy *policy, |
2022 | unsigned int target_freq, | |
2023 | unsigned int relation) | |
2024 | { | |
7249924e | 2025 | unsigned int old_target_freq = target_freq; |
d218ed77 | 2026 | int index; |
c32b6b8e | 2027 | |
a7b422cd KRW |
2028 | if (cpufreq_disabled()) |
2029 | return -ENODEV; | |
2030 | ||
7249924e | 2031 | /* Make sure that target_freq is within supported range */ |
910c6e88 | 2032 | target_freq = clamp_val(target_freq, policy->min, policy->max); |
7249924e VK |
2033 | |
2034 | pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", | |
e837f9b5 | 2035 | policy->cpu, target_freq, relation, old_target_freq); |
5a1c0228 | 2036 | |
9c0ebcf7 VK |
2037 | /* |
2038 | * This might look like a redundant call as we are checking it again | |
2039 | * after finding index. But it is left intentionally for cases where | |
2040 | * exactly same freq is called again and so we can save on few function | |
2041 | * calls. | |
2042 | */ | |
5a1c0228 VK |
2043 | if (target_freq == policy->cur) |
2044 | return 0; | |
2045 | ||
1c03a2d0 VK |
2046 | /* Save last value to restore later on errors */ |
2047 | policy->restore_freq = policy->cur; | |
2048 | ||
1c3d85dd | 2049 | if (cpufreq_driver->target) |
6019d23a | 2050 | return cpufreq_driver->target(policy, target_freq, relation); |
9c0ebcf7 | 2051 | |
6019d23a RW |
2052 | if (!cpufreq_driver->target_index) |
2053 | return -EINVAL; | |
9c0ebcf7 | 2054 | |
d218ed77 | 2055 | index = cpufreq_frequency_table_target(policy, target_freq, relation); |
6019d23a | 2056 | |
23727845 | 2057 | return __target_index(policy, index); |
1da177e4 LT |
2058 | } |
2059 | EXPORT_SYMBOL_GPL(__cpufreq_driver_target); | |
2060 | ||
1da177e4 LT |
2061 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
2062 | unsigned int target_freq, | |
2063 | unsigned int relation) | |
2064 | { | |
f1829e4a | 2065 | int ret = -EINVAL; |
1da177e4 | 2066 | |
ad7722da | 2067 | down_write(&policy->rwsem); |
1da177e4 LT |
2068 | |
2069 | ret = __cpufreq_driver_target(policy, target_freq, relation); | |
2070 | ||
ad7722da | 2071 | up_write(&policy->rwsem); |
1da177e4 | 2072 | |
1da177e4 LT |
2073 | return ret; |
2074 | } | |
2075 | EXPORT_SYMBOL_GPL(cpufreq_driver_target); | |
2076 | ||
de1df26b RW |
2077 | __weak struct cpufreq_governor *cpufreq_fallback_governor(void) |
2078 | { | |
2079 | return NULL; | |
2080 | } | |
2081 | ||
a92604b4 | 2082 | static int cpufreq_init_governor(struct cpufreq_policy *policy) |
1da177e4 | 2083 | { |
cc993cab | 2084 | int ret; |
6afde10c | 2085 | |
2f0aea93 VK |
2086 | /* Don't start any governor operations if we are entering suspend */ |
2087 | if (cpufreq_suspended) | |
2088 | return 0; | |
cb57720b EZ |
2089 | /* |
2090 | * Governor might not be initiated here if ACPI _PPC changed | |
2091 | * notification happened, so check it. | |
2092 | */ | |
2093 | if (!policy->governor) | |
2094 | return -EINVAL; | |
2f0aea93 | 2095 | |
ed4676e2 VK |
2096 | /* Platform doesn't want dynamic frequency switching ? */ |
2097 | if (policy->governor->dynamic_switching && | |
fc4c709f | 2098 | cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { |
de1df26b RW |
2099 | struct cpufreq_governor *gov = cpufreq_fallback_governor(); |
2100 | ||
2101 | if (gov) { | |
fe829ed8 | 2102 | pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", |
e837f9b5 | 2103 | policy->governor->name, gov->name); |
6afde10c | 2104 | policy->governor = gov; |
de1df26b RW |
2105 | } else { |
2106 | return -EINVAL; | |
6afde10c | 2107 | } |
1c256245 | 2108 | } |
1da177e4 | 2109 | |
a92604b4 RW |
2110 | if (!try_module_get(policy->governor->owner)) |
2111 | return -EINVAL; | |
95731ebb | 2112 | |
a92604b4 | 2113 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); |
1da177e4 | 2114 | |
e788892b RW |
2115 | if (policy->governor->init) { |
2116 | ret = policy->governor->init(policy); | |
2117 | if (ret) { | |
36be3418 | 2118 | module_put(policy->governor->owner); |
e788892b RW |
2119 | return ret; |
2120 | } | |
36be3418 | 2121 | } |
1da177e4 | 2122 | |
a92604b4 RW |
2123 | return 0; |
2124 | } | |
2125 | ||
2126 | static void cpufreq_exit_governor(struct cpufreq_policy *policy) | |
2127 | { | |
2128 | if (cpufreq_suspended || !policy->governor) | |
2129 | return; | |
2130 | ||
2131 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2132 | ||
e788892b RW |
2133 | if (policy->governor->exit) |
2134 | policy->governor->exit(policy); | |
a92604b4 | 2135 | |
a92604b4 | 2136 | module_put(policy->governor->owner); |
1da177e4 LT |
2137 | } |
2138 | ||
0a300767 RW |
2139 | static int cpufreq_start_governor(struct cpufreq_policy *policy) |
2140 | { | |
2141 | int ret; | |
2142 | ||
a92604b4 RW |
2143 | if (cpufreq_suspended) |
2144 | return 0; | |
2145 | ||
2146 | if (!policy->governor) | |
2147 | return -EINVAL; | |
2148 | ||
2149 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2150 | ||
407d0fff | 2151 | if (cpufreq_driver->get) |
3bbf8fe3 RW |
2152 | cpufreq_update_current_freq(policy); |
2153 | ||
e788892b RW |
2154 | if (policy->governor->start) { |
2155 | ret = policy->governor->start(policy); | |
2156 | if (ret) | |
2157 | return ret; | |
2158 | } | |
2159 | ||
2160 | if (policy->governor->limits) | |
2161 | policy->governor->limits(policy); | |
d6ff44d6 | 2162 | |
d6ff44d6 | 2163 | return 0; |
0a300767 RW |
2164 | } |
2165 | ||
a92604b4 RW |
2166 | static void cpufreq_stop_governor(struct cpufreq_policy *policy) |
2167 | { | |
2168 | if (cpufreq_suspended || !policy->governor) | |
2169 | return; | |
2170 | ||
2171 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2172 | ||
e788892b RW |
2173 | if (policy->governor->stop) |
2174 | policy->governor->stop(policy); | |
a92604b4 RW |
2175 | } |
2176 | ||
2177 | static void cpufreq_governor_limits(struct cpufreq_policy *policy) | |
2178 | { | |
2179 | if (cpufreq_suspended || !policy->governor) | |
2180 | return; | |
2181 | ||
2182 | pr_debug("%s: for CPU %u\n", __func__, policy->cpu); | |
2183 | ||
e788892b RW |
2184 | if (policy->governor->limits) |
2185 | policy->governor->limits(policy); | |
0a300767 RW |
2186 | } |
2187 | ||
1da177e4 LT |
2188 | int cpufreq_register_governor(struct cpufreq_governor *governor) |
2189 | { | |
3bcb09a3 | 2190 | int err; |
1da177e4 LT |
2191 | |
2192 | if (!governor) | |
2193 | return -EINVAL; | |
2194 | ||
a7b422cd KRW |
2195 | if (cpufreq_disabled()) |
2196 | return -ENODEV; | |
2197 | ||
3fc54d37 | 2198 | mutex_lock(&cpufreq_governor_mutex); |
32ee8c3e | 2199 | |
3bcb09a3 | 2200 | err = -EBUSY; |
42f91fa1 | 2201 | if (!find_governor(governor->name)) { |
3bcb09a3 JF |
2202 | err = 0; |
2203 | list_add(&governor->governor_list, &cpufreq_governor_list); | |
1da177e4 | 2204 | } |
1da177e4 | 2205 | |
32ee8c3e | 2206 | mutex_unlock(&cpufreq_governor_mutex); |
3bcb09a3 | 2207 | return err; |
1da177e4 LT |
2208 | } |
2209 | EXPORT_SYMBOL_GPL(cpufreq_register_governor); | |
2210 | ||
1da177e4 LT |
2211 | void cpufreq_unregister_governor(struct cpufreq_governor *governor) |
2212 | { | |
4573237b VK |
2213 | struct cpufreq_policy *policy; |
2214 | unsigned long flags; | |
90e41bac | 2215 | |
1da177e4 LT |
2216 | if (!governor) |
2217 | return; | |
2218 | ||
a7b422cd KRW |
2219 | if (cpufreq_disabled()) |
2220 | return; | |
2221 | ||
4573237b VK |
2222 | /* clear last_governor for all inactive policies */ |
2223 | read_lock_irqsave(&cpufreq_driver_lock, flags); | |
2224 | for_each_inactive_policy(policy) { | |
18bf3a12 VK |
2225 | if (!strcmp(policy->last_governor, governor->name)) { |
2226 | policy->governor = NULL; | |
4573237b | 2227 | strcpy(policy->last_governor, "\0"); |
18bf3a12 | 2228 | } |
90e41bac | 2229 | } |
4573237b | 2230 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
90e41bac | 2231 | |
3fc54d37 | 2232 | mutex_lock(&cpufreq_governor_mutex); |
1da177e4 | 2233 | list_del(&governor->governor_list); |
3fc54d37 | 2234 | mutex_unlock(&cpufreq_governor_mutex); |
1da177e4 LT |
2235 | } |
2236 | EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); | |
2237 | ||
2238 | ||
1da177e4 LT |
2239 | /********************************************************************* |
2240 | * POLICY INTERFACE * | |
2241 | *********************************************************************/ | |
2242 | ||
2243 | /** | |
2244 | * cpufreq_get_policy - get the current cpufreq_policy | |
29464f28 DJ |
2245 | * @policy: struct cpufreq_policy into which the current cpufreq_policy |
2246 | * is written | |
1da177e4 LT |
2247 | * |
2248 | * Reads the current cpufreq policy. | |
2249 | */ | |
2250 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) | |
2251 | { | |
2252 | struct cpufreq_policy *cpu_policy; | |
2253 | if (!policy) | |
2254 | return -EINVAL; | |
2255 | ||
2256 | cpu_policy = cpufreq_cpu_get(cpu); | |
2257 | if (!cpu_policy) | |
2258 | return -EINVAL; | |
2259 | ||
d5b73cd8 | 2260 | memcpy(policy, cpu_policy, sizeof(*policy)); |
1da177e4 LT |
2261 | |
2262 | cpufreq_cpu_put(cpu_policy); | |
1da177e4 LT |
2263 | return 0; |
2264 | } | |
2265 | EXPORT_SYMBOL(cpufreq_get_policy); | |
2266 | ||
a0dbb819 RW |
2267 | /** |
2268 | * cpufreq_set_policy - Modify cpufreq policy parameters. | |
2269 | * @policy: Policy object to modify. | |
2270 | * @new_policy: New policy data. | |
2271 | * | |
2272 | * Pass @new_policy to the cpufreq driver's ->verify() callback, run the | |
2273 | * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to | |
2274 | * the driver's ->verify() callback again and run the notifiers for it again | |
2275 | * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters | |
2276 | * of @new_policy to @policy and either invoke the driver's ->setpolicy() | |
2277 | * callback (if present) or carry out a governor update for @policy. That is, | |
2278 | * run the current governor's ->limits() callback (if the governor field in | |
2279 | * @new_policy points to the same object as the one in @policy) or replace the | |
2280 | * governor for @policy with the new one stored in @new_policy. | |
2281 | * | |
2282 | * The cpuinfo part of @policy is not updated by this function. | |
153d7f3f | 2283 | */ |
9083e498 RW |
2284 | int cpufreq_set_policy(struct cpufreq_policy *policy, |
2285 | struct cpufreq_policy *new_policy) | |
1da177e4 | 2286 | { |
d9a789c7 RW |
2287 | struct cpufreq_governor *old_gov; |
2288 | int ret; | |
1da177e4 | 2289 | |
e837f9b5 JP |
2290 | pr_debug("setting new policy for CPU %u: %u - %u kHz\n", |
2291 | new_policy->cpu, new_policy->min, new_policy->max); | |
1da177e4 | 2292 | |
d5b73cd8 | 2293 | memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); |
1da177e4 | 2294 | |
fba9573b PX |
2295 | /* |
2296 | * This check works well when we store new min/max freq attributes, | |
2297 | * because new_policy is a copy of policy with one field updated. | |
2298 | */ | |
2299 | if (new_policy->min > new_policy->max) | |
d9a789c7 | 2300 | return -EINVAL; |
9c9a43ed | 2301 | |
1da177e4 | 2302 | /* verify the cpu speed can be set within this limit */ |
3a3e9e06 | 2303 | ret = cpufreq_driver->verify(new_policy); |
1da177e4 | 2304 | if (ret) |
d9a789c7 | 2305 | return ret; |
1da177e4 | 2306 | |
1da177e4 | 2307 | /* adjust if necessary - all reasons */ |
e041c683 | 2308 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
3a3e9e06 | 2309 | CPUFREQ_ADJUST, new_policy); |
1da177e4 | 2310 | |
bb176f7d VK |
2311 | /* |
2312 | * verify the cpu speed can be set within this limit, which might be | |
2313 | * different to the first one | |
2314 | */ | |
3a3e9e06 | 2315 | ret = cpufreq_driver->verify(new_policy); |
e041c683 | 2316 | if (ret) |
d9a789c7 | 2317 | return ret; |
1da177e4 LT |
2318 | |
2319 | /* notification of the new policy */ | |
e041c683 | 2320 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
3a3e9e06 | 2321 | CPUFREQ_NOTIFY, new_policy); |
1da177e4 | 2322 | |
3a3e9e06 VK |
2323 | policy->min = new_policy->min; |
2324 | policy->max = new_policy->max; | |
601b2185 | 2325 | trace_cpu_frequency_limits(policy); |
1da177e4 | 2326 | |
e3c06236 SM |
2327 | policy->cached_target_freq = UINT_MAX; |
2328 | ||
2d06d8c4 | 2329 | pr_debug("new min and max freqs are %u - %u kHz\n", |
e837f9b5 | 2330 | policy->min, policy->max); |
1da177e4 | 2331 | |
1c3d85dd | 2332 | if (cpufreq_driver->setpolicy) { |
3a3e9e06 | 2333 | policy->policy = new_policy->policy; |
2d06d8c4 | 2334 | pr_debug("setting range\n"); |
167a38dc | 2335 | return cpufreq_driver->setpolicy(policy); |
d9a789c7 | 2336 | } |
1da177e4 | 2337 | |
0a300767 | 2338 | if (new_policy->governor == policy->governor) { |
2bb4059e | 2339 | pr_debug("governor limits update\n"); |
a92604b4 | 2340 | cpufreq_governor_limits(policy); |
d6ff44d6 | 2341 | return 0; |
0a300767 | 2342 | } |
7bd353a9 | 2343 | |
d9a789c7 RW |
2344 | pr_debug("governor switch\n"); |
2345 | ||
2346 | /* save old, working values */ | |
2347 | old_gov = policy->governor; | |
2348 | /* end old governor */ | |
2349 | if (old_gov) { | |
45482c70 | 2350 | cpufreq_stop_governor(policy); |
36be3418 | 2351 | cpufreq_exit_governor(policy); |
1da177e4 LT |
2352 | } |
2353 | ||
d9a789c7 RW |
2354 | /* start new governor */ |
2355 | policy->governor = new_policy->governor; | |
a92604b4 | 2356 | ret = cpufreq_init_governor(policy); |
4bc384ae | 2357 | if (!ret) { |
0a300767 RW |
2358 | ret = cpufreq_start_governor(policy); |
2359 | if (!ret) { | |
2bb4059e | 2360 | pr_debug("governor change\n"); |
531b5c9f | 2361 | sched_cpufreq_governor_change(policy, old_gov); |
0a300767 RW |
2362 | return 0; |
2363 | } | |
b7898fda | 2364 | cpufreq_exit_governor(policy); |
d9a789c7 RW |
2365 | } |
2366 | ||
2367 | /* new governor failed, so re-start old one */ | |
2368 | pr_debug("starting governor %s failed\n", policy->governor->name); | |
2369 | if (old_gov) { | |
2370 | policy->governor = old_gov; | |
a92604b4 | 2371 | if (cpufreq_init_governor(policy)) |
4bc384ae VK |
2372 | policy->governor = NULL; |
2373 | else | |
0a300767 | 2374 | cpufreq_start_governor(policy); |
d9a789c7 RW |
2375 | } |
2376 | ||
4bc384ae | 2377 | return ret; |
1da177e4 LT |
2378 | } |
2379 | ||
1da177e4 | 2380 | /** |
a0dbb819 RW |
2381 | * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. |
2382 | * @cpu: CPU to re-evaluate the policy for. | |
1da177e4 | 2383 | * |
a0dbb819 RW |
2384 | * Update the current frequency for the cpufreq policy of @cpu and use |
2385 | * cpufreq_set_policy() to re-apply the min and max limits saved in the | |
2386 | * user_policy sub-structure of that policy, which triggers the evaluation | |
2387 | * of policy notifiers and the cpufreq driver's ->verify() callback for the | |
2388 | * policy in question, among other things. | |
1da177e4 | 2389 | */ |
30248fef | 2390 | void cpufreq_update_policy(unsigned int cpu) |
1da177e4 | 2391 | { |
540a3758 | 2392 | struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); |
3a3e9e06 | 2393 | struct cpufreq_policy new_policy; |
1da177e4 | 2394 | |
fefa8ff8 | 2395 | if (!policy) |
30248fef | 2396 | return; |
1da177e4 | 2397 | |
bb176f7d VK |
2398 | /* |
2399 | * BIOS might change freq behind our back | |
2400 | * -> ask driver for current freq and notify governors about a change | |
2401 | */ | |
5ddc6d4e | 2402 | if (cpufreq_driver->get && has_target() && |
348a2ec5 RW |
2403 | (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy)))) |
2404 | goto unlock; | |
30248fef | 2405 | |
348a2ec5 RW |
2406 | pr_debug("updating policy for CPU %u\n", cpu); |
2407 | memcpy(&new_policy, policy, sizeof(*policy)); | |
2408 | new_policy.min = policy->user_policy.min; | |
2409 | new_policy.max = policy->user_policy.max; | |
0961dd0d | 2410 | |
30248fef | 2411 | cpufreq_set_policy(policy, &new_policy); |
1da177e4 | 2412 | |
fefa8ff8 | 2413 | unlock: |
540a3758 | 2414 | cpufreq_cpu_release(policy); |
1da177e4 LT |
2415 | } |
2416 | EXPORT_SYMBOL(cpufreq_update_policy); | |
2417 | ||
5a25e3f7 RW |
2418 | /** |
2419 | * cpufreq_update_limits - Update policy limits for a given CPU. | |
2420 | * @cpu: CPU to update the policy limits for. | |
2421 | * | |
2422 | * Invoke the driver's ->update_limits callback if present or call | |
2423 | * cpufreq_update_policy() for @cpu. | |
2424 | */ | |
2425 | void cpufreq_update_limits(unsigned int cpu) | |
2426 | { | |
2427 | if (cpufreq_driver->update_limits) | |
2428 | cpufreq_driver->update_limits(cpu); | |
2429 | else | |
2430 | cpufreq_update_policy(cpu); | |
2431 | } | |
2432 | EXPORT_SYMBOL_GPL(cpufreq_update_limits); | |
2433 | ||
6f19efc0 LM |
2434 | /********************************************************************* |
2435 | * BOOST * | |
2436 | *********************************************************************/ | |
2437 | static int cpufreq_boost_set_sw(int state) | |
2438 | { | |
6f19efc0 LM |
2439 | struct cpufreq_policy *policy; |
2440 | int ret = -EINVAL; | |
2441 | ||
f963735a | 2442 | for_each_active_policy(policy) { |
f8bfc116 VK |
2443 | if (!policy->freq_table) |
2444 | continue; | |
49f18560 | 2445 | |
f8bfc116 VK |
2446 | ret = cpufreq_frequency_table_cpuinfo(policy, |
2447 | policy->freq_table); | |
2448 | if (ret) { | |
2449 | pr_err("%s: Policy frequency update failed\n", | |
2450 | __func__); | |
2451 | break; | |
6f19efc0 | 2452 | } |
f8bfc116 VK |
2453 | |
2454 | down_write(&policy->rwsem); | |
2455 | policy->user_policy.max = policy->max; | |
2456 | cpufreq_governor_limits(policy); | |
2457 | up_write(&policy->rwsem); | |
6f19efc0 LM |
2458 | } |
2459 | ||
2460 | return ret; | |
2461 | } | |
2462 | ||
2463 | int cpufreq_boost_trigger_state(int state) | |
2464 | { | |
2465 | unsigned long flags; | |
2466 | int ret = 0; | |
2467 | ||
2468 | if (cpufreq_driver->boost_enabled == state) | |
2469 | return 0; | |
2470 | ||
2471 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
2472 | cpufreq_driver->boost_enabled = state; | |
2473 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
2474 | ||
2475 | ret = cpufreq_driver->set_boost(state); | |
2476 | if (ret) { | |
2477 | write_lock_irqsave(&cpufreq_driver_lock, flags); | |
2478 | cpufreq_driver->boost_enabled = !state; | |
2479 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); | |
2480 | ||
e837f9b5 JP |
2481 | pr_err("%s: Cannot %s BOOST\n", |
2482 | __func__, state ? "enable" : "disable"); | |
6f19efc0 LM |
2483 | } |
2484 | ||
2485 | return ret; | |
2486 | } | |
2487 | ||
41669da0 | 2488 | static bool cpufreq_boost_supported(void) |
6f19efc0 | 2489 | { |
89f98d7e | 2490 | return cpufreq_driver->set_boost; |
6f19efc0 | 2491 | } |
6f19efc0 | 2492 | |
44139ed4 VK |
2493 | static int create_boost_sysfs_file(void) |
2494 | { | |
2495 | int ret; | |
2496 | ||
c82bd444 | 2497 | ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); |
44139ed4 VK |
2498 | if (ret) |
2499 | pr_err("%s: cannot register global BOOST sysfs file\n", | |
2500 | __func__); | |
2501 | ||
2502 | return ret; | |
2503 | } | |
2504 | ||
2505 | static void remove_boost_sysfs_file(void) | |
2506 | { | |
2507 | if (cpufreq_boost_supported()) | |
c82bd444 | 2508 | sysfs_remove_file(cpufreq_global_kobject, &boost.attr); |
44139ed4 VK |
2509 | } |
2510 | ||
2511 | int cpufreq_enable_boost_support(void) | |
2512 | { | |
2513 | if (!cpufreq_driver) | |
2514 | return -EINVAL; | |
2515 | ||
2516 | if (cpufreq_boost_supported()) | |
2517 | return 0; | |
2518 | ||
7a6c79f2 | 2519 | cpufreq_driver->set_boost = cpufreq_boost_set_sw; |
44139ed4 VK |
2520 | |
2521 | /* This will get removed on driver unregister */ | |
2522 | return create_boost_sysfs_file(); | |
2523 | } | |
2524 | EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); | |
2525 | ||
6f19efc0 LM |
2526 | int cpufreq_boost_enabled(void) |
2527 | { | |
2528 | return cpufreq_driver->boost_enabled; | |
2529 | } | |
2530 | EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); | |
2531 | ||
1da177e4 LT |
2532 | /********************************************************************* |
2533 | * REGISTER / UNREGISTER CPUFREQ DRIVER * | |
2534 | *********************************************************************/ | |
27622b06 | 2535 | static enum cpuhp_state hp_online; |
1da177e4 | 2536 | |
c4a3fa26 CY |
2537 | static int cpuhp_cpufreq_online(unsigned int cpu) |
2538 | { | |
2539 | cpufreq_online(cpu); | |
2540 | ||
2541 | return 0; | |
2542 | } | |
2543 | ||
2544 | static int cpuhp_cpufreq_offline(unsigned int cpu) | |
2545 | { | |
2546 | cpufreq_offline(cpu); | |
2547 | ||
2548 | return 0; | |
2549 | } | |
2550 | ||
1da177e4 LT |
2551 | /** |
2552 | * cpufreq_register_driver - register a CPU Frequency driver | |
2553 | * @driver_data: A struct cpufreq_driver containing the values# | |
2554 | * submitted by the CPU Frequency driver. | |
2555 | * | |
bb176f7d | 2556 | * Registers a CPU Frequency driver to this core code. This code |
63af4055 | 2557 | * returns zero on success, -EEXIST when another driver got here first |
32ee8c3e | 2558 | * (and isn't unregistered in the meantime). |
1da177e4 LT |
2559 | * |
2560 | */ | |
221dee28 | 2561 | int cpufreq_register_driver(struct cpufreq_driver *driver_data) |
1da177e4 LT |
2562 | { |
2563 | unsigned long flags; | |
2564 | int ret; | |
2565 | ||
a7b422cd KRW |
2566 | if (cpufreq_disabled()) |
2567 | return -ENODEV; | |
2568 | ||
1da177e4 | 2569 | if (!driver_data || !driver_data->verify || !driver_data->init || |
9c0ebcf7 | 2570 | !(driver_data->setpolicy || driver_data->target_index || |
9832235f RW |
2571 | driver_data->target) || |
2572 | (driver_data->setpolicy && (driver_data->target_index || | |
1c03a2d0 | 2573 | driver_data->target)) || |
a9a22b57 | 2574 | (!driver_data->get_intermediate != !driver_data->target_intermediate) || |
91a12e91 | 2575 | (!driver_data->online != !driver_data->offline)) |
1da177e4 LT |
2576 | return -EINVAL; |
2577 | ||
2d06d8c4 | 2578 | pr_debug("trying to register driver %s\n", driver_data->name); |
1da177e4 | 2579 | |
fdd320da | 2580 | /* Protect against concurrent CPU online/offline. */ |
a92551e4 | 2581 | cpus_read_lock(); |
fdd320da | 2582 | |
0d1857a1 | 2583 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1c3d85dd | 2584 | if (cpufreq_driver) { |
0d1857a1 | 2585 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
fdd320da RW |
2586 | ret = -EEXIST; |
2587 | goto out; | |
1da177e4 | 2588 | } |
1c3d85dd | 2589 | cpufreq_driver = driver_data; |
0d1857a1 | 2590 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
1da177e4 | 2591 | |
bc68b7df VK |
2592 | if (driver_data->setpolicy) |
2593 | driver_data->flags |= CPUFREQ_CONST_LOOPS; | |
2594 | ||
7a6c79f2 RW |
2595 | if (cpufreq_boost_supported()) { |
2596 | ret = create_boost_sysfs_file(); | |
2597 | if (ret) | |
2598 | goto err_null_driver; | |
2599 | } | |
6f19efc0 | 2600 | |
8a25a2fd | 2601 | ret = subsys_interface_register(&cpufreq_interface); |
8f5bc2ab | 2602 | if (ret) |
6f19efc0 | 2603 | goto err_boost_unreg; |
1da177e4 | 2604 | |
ce1bcfe9 VK |
2605 | if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && |
2606 | list_empty(&cpufreq_policy_list)) { | |
1da177e4 | 2607 | /* if all ->init() calls failed, unregister */ |
6c770036 | 2608 | ret = -ENODEV; |
ce1bcfe9 VK |
2609 | pr_debug("%s: No CPU initialized for driver %s\n", __func__, |
2610 | driver_data->name); | |
2611 | goto err_if_unreg; | |
1da177e4 LT |
2612 | } |
2613 | ||
a92551e4 SAS |
2614 | ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, |
2615 | "cpufreq:online", | |
2616 | cpuhp_cpufreq_online, | |
2617 | cpuhp_cpufreq_offline); | |
27622b06 SAS |
2618 | if (ret < 0) |
2619 | goto err_if_unreg; | |
2620 | hp_online = ret; | |
5372e054 | 2621 | ret = 0; |
27622b06 | 2622 | |
2d06d8c4 | 2623 | pr_debug("driver %s up and running\n", driver_data->name); |
3834abb4 | 2624 | goto out; |
fdd320da | 2625 | |
8a25a2fd KS |
2626 | err_if_unreg: |
2627 | subsys_interface_unregister(&cpufreq_interface); | |
6f19efc0 | 2628 | err_boost_unreg: |
44139ed4 | 2629 | remove_boost_sysfs_file(); |
8f5bc2ab | 2630 | err_null_driver: |
0d1857a1 | 2631 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
1c3d85dd | 2632 | cpufreq_driver = NULL; |
0d1857a1 | 2633 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
3834abb4 | 2634 | out: |
a92551e4 | 2635 | cpus_read_unlock(); |
3834abb4 | 2636 | return ret; |
1da177e4 LT |
2637 | } |
2638 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); | |
2639 | ||
1da177e4 LT |
2640 | /** |
2641 | * cpufreq_unregister_driver - unregister the current CPUFreq driver | |
2642 | * | |
bb176f7d | 2643 | * Unregister the current CPUFreq driver. Only call this if you have |
1da177e4 LT |
2644 | * the right to do so, i.e. if you have succeeded in initialising before! |
2645 | * Returns zero if successful, and -EINVAL if the cpufreq_driver is | |
2646 | * currently not initialised. | |
2647 | */ | |
221dee28 | 2648 | int cpufreq_unregister_driver(struct cpufreq_driver *driver) |
1da177e4 LT |
2649 | { |
2650 | unsigned long flags; | |
2651 | ||
1c3d85dd | 2652 | if (!cpufreq_driver || (driver != cpufreq_driver)) |
1da177e4 | 2653 | return -EINVAL; |
1da177e4 | 2654 | |
2d06d8c4 | 2655 | pr_debug("unregistering driver %s\n", driver->name); |
1da177e4 | 2656 | |
454d3a25 | 2657 | /* Protect against concurrent cpu hotplug */ |
a92551e4 | 2658 | cpus_read_lock(); |
8a25a2fd | 2659 | subsys_interface_unregister(&cpufreq_interface); |
44139ed4 | 2660 | remove_boost_sysfs_file(); |
a92551e4 | 2661 | cpuhp_remove_state_nocalls_cpuslocked(hp_online); |
1da177e4 | 2662 | |
0d1857a1 | 2663 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
6eed9404 | 2664 | |
1c3d85dd | 2665 | cpufreq_driver = NULL; |
6eed9404 | 2666 | |
0d1857a1 | 2667 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
a92551e4 | 2668 | cpus_read_unlock(); |
1da177e4 LT |
2669 | |
2670 | return 0; | |
2671 | } | |
2672 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); | |
5a01f2e8 | 2673 | |
90de2a4a DA |
2674 | /* |
2675 | * Stop cpufreq at shutdown to make sure it isn't holding any locks | |
2676 | * or mutexes when secondary CPUs are halted. | |
2677 | */ | |
2678 | static struct syscore_ops cpufreq_syscore_ops = { | |
2679 | .shutdown = cpufreq_suspend, | |
2680 | }; | |
2681 | ||
c82bd444 VK |
2682 | struct kobject *cpufreq_global_kobject; |
2683 | EXPORT_SYMBOL(cpufreq_global_kobject); | |
2684 | ||
5a01f2e8 VP |
2685 | static int __init cpufreq_core_init(void) |
2686 | { | |
a7b422cd KRW |
2687 | if (cpufreq_disabled()) |
2688 | return -ENODEV; | |
2689 | ||
8eec1020 | 2690 | cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); |
8aa84ad8 TR |
2691 | BUG_ON(!cpufreq_global_kobject); |
2692 | ||
90de2a4a DA |
2693 | register_syscore_ops(&cpufreq_syscore_ops); |
2694 | ||
5a01f2e8 VP |
2695 | return 0; |
2696 | } | |
d82f2692 | 2697 | module_param(off, int, 0444); |
5a01f2e8 | 2698 | core_initcall(cpufreq_core_init); |